Skip to content

Commit

Permalink
Error with invalid API key (#524)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! Please make sure that your code changes
are covered with tests. In case of new features or big changes remember
to adjust the documentation.

In case of an existing issue, reference it using one of the following:

closes: #ISSUE
related: #ISSUE

How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->

## What
*Describe what the change is solving*

Ensure the ngrok-api-manager fails to start with error messages if the API key is invalid.

## How
*Describe the solution*

Dummy request to list API keys using the given API key

tunnel driver already errors for bad authtoken
```
2024-11-18T21:23:13Z	DEBUG	drivers.tunnel	decoded response	{"obj": "csess", "id": "7838d5221914", "sid": 3, "resp": {"Version":"","ClientId":"","Error":"The authtoken you specified is properly formed, but it is invalid.\nYour authtoken: 2lssG3MpQZ5dIyCVLYNRBfXC5tg_INVALID\nThis usually happens when:\n    - You reset your authtoken\n    - Your authtoken was for a team account that you were removed from\n    - You are using ngrok link and this credential was explicitly revoked\nGo to your ngrok dashboard and double check that your authtoken is correct:\nhttps://dashboard.ngrok.com/get-started/your-authtoken\r\n\r\nERR_NGROK_107\r\n","Extra":{"Version":"","Region":"","Cookie":"","AccountName":"","SessionDuration":0,"PlanName":"","Banner":"","DeprecationWarning":null}}, "err": null}```

now api manager errors for bad api key:
```
2024-11-18T21:13:17Z	ERROR	setup	error running api-manager	{"error": "Unable to load ngrokClientSet: Unable to verify API Key or Authtoken: HTTP 403: The API authentication you specified does not look like a valid credential. Your credential: 'BROKEN'. API keys and instructions are available on your dashboard: https://dashboard.ngrok.com/api-keys [ERR_NGROK_202]\n\nOperation ID: op_2p2VwkFzzgSYJFJp6SCH86IMhpa"}```
```

## Breaking Changes
*Are there any breaking changes in this PR?*
No.
  • Loading branch information
hjkatz authored Nov 19, 2024
2 parents a1619a2 + 0c083e4 commit d06d2fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 12 additions & 2 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (

_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/utils/ptr"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -48,6 +49,7 @@ import (
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"

"github.com/ngrok/ngrok-api-go/v6"
"github.com/ngrok/ngrok-api-go/v6/api_keys"

bindingsv1alpha1 "github.com/ngrok/ngrok-operator/api/bindings/v1alpha1"
ingressv1alpha1 "github.com/ngrok/ngrok-operator/api/ingress/v1alpha1"
Expand Down Expand Up @@ -251,8 +253,6 @@ func runNormalMode(ctx context.Context, opts managerOpts, k8sClient client.Clien
return fmt.Errorf("Unable to load ngrokClientSet: %w", err)
}


// TODO(hkatz) for now we are hiding the k8sop API regstration behind the bindings feature flag
if opts.enableFeatureBindings {
// register the k8sop in the ngrok API
if err := createKubernetesOperator(ctx, k8sClient, opts); err != nil {
Expand Down Expand Up @@ -390,6 +390,16 @@ func loadNgrokClientset(ctx context.Context, opts managerOpts) (ngrokapi.Clients
}
setupLog.Info("configured API client", "base_url", ngrokClientConfig.BaseURL)

// validate the API key and Authtoken works with ngrok API
// by making a dummy request to list API keys
// and checking for errors
cApiKeys := api_keys.NewClient(ngrokClientConfig)
cIter := cApiKeys.List(&ngrok.Paging{Limit: ptr.To("1")})
cIter.Next(ctx)
if cIter.Err() != nil {
return nil, fmt.Errorf("Unable to verify API Key: %w", cIter.Err())
}

ngrokClientset := ngrokapi.NewClientSet(ngrokClientConfig)
return ngrokClientset, nil
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/tunneldriver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,9 @@ func New(ctx context.Context, logger logr.Logger, opts TunnelDriverOpts) (*Tunne
}
}),
)
//nolint:errcheck
go ngrok.Connect(ctx, connOpts...)
_, err := ngrok.Connect(ctx, connOpts...)

return td, nil
return td, err
}

// Ready implements the healthcheck.HealthChecker interface for when the TunnelDriver is ready to serve tunnels
Expand Down

0 comments on commit d06d2fd

Please sign in to comment.