Skip to content

Commit

Permalink
Removed 404 check, added bearer token refresh logic when hitting 401
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenvervaeke committed Oct 9, 2024
1 parent 539fc2c commit e39975f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
46 changes: 30 additions & 16 deletions internal/cli/clusters/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package clusters

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
Expand All @@ -31,6 +33,7 @@ import (
type WatchOpts struct {
cli.GlobalOpts
cli.WatchOpts
cli.RefresherOpts
name string
store store.ClusterDescriber
}
Expand All @@ -45,24 +48,34 @@ func (opts *WatchOpts) initStore(ctx context.Context) func() error {
}
}

func isRetryable(err error) bool {
atlasErr, ok := atlasClustersPinned.AsError(err)
return ok && atlasErr.GetErrorCode() == "CLUSTER_NOT_FOUND"
}
func (opts *WatchOpts) watcher(ctx context.Context) func() (any, bool, error) {
return func() (any, bool, error) {
result, err := opts.store.AtlasCluster(opts.ConfigProjectID(), opts.name)
if err != nil {
var atlasClustersPinnedErr *atlasClustersPinned.GenericOpenAPIError

func (opts *WatchOpts) watcher() (any, bool, error) {
result, err := opts.store.AtlasCluster(opts.ConfigProjectID(), opts.name)
if err != nil {
return nil, false, err
}
if result.GetStateName() == "UPDATING" {
opts.IsRetryableErr = isRetryable
if errors.As(err, &atlasClustersPinnedErr) {
if *atlasClustersPinnedErr.Model().Error == http.StatusUnauthorized {
// Refresh the access token
// Note: this only updates the config, so we have to re-initialize the store
if err := opts.RefreshAccessToken(ctx); err != nil {
return nil, false, err
}

// Re-initialize store, refreshAccessToken only refreshes the config
return nil, false, opts.initStore(ctx)()
}
}
}
if err != nil {
return nil, false, err
}
return nil, result.GetStateName() == "IDLE", nil
}
return nil, result.GetStateName() == "IDLE", nil
}

func (opts *WatchOpts) Run() error {
if _, err := opts.Watch(opts.watcher); err != nil {
func (opts *WatchOpts) Run(ctx context.Context) error {
if _, err := opts.Watch(opts.watcher(ctx)); err != nil {
return err
}

Expand Down Expand Up @@ -93,11 +106,12 @@ You can interrupt the command's polling at any time with CTRL-C.
opts.ValidateProjectID,
opts.initStore(cmd.Context()),
opts.InitOutput(cmd.OutOrStdout(), watchTemplate),
opts.InitFlow(config.Default()),
)
},
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, args []string) error {
opts.name = args[0]
return opts.Run()
return opts.Run(cmd.Context())
},
}

Expand Down
3 changes: 2 additions & 1 deletion internal/cli/clusters/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package clusters

import (
"context"
"testing"

"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -44,7 +45,7 @@ func TestWatch_Run(t *testing.T) {
Return(expected, nil).
Times(1)

if err := opts.Run(); err != nil {
if err := opts.Run(context.Background()); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/decryption/log_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (logLine *AuditLogLine) logAdditionalAuthData() []byte {
const AADByteSize = 8

additionalAuthData := make([]byte, AADByteSize)
binary.LittleEndian.PutUint64(additionalAuthData, uint64(logLine.TS.UnixMilli())) //nolint:gosec
binary.LittleEndian.PutUint64(additionalAuthData, uint64(logLine.TS.UnixMilli()))

Check failure on line 109 in internal/decryption/log_record.go

View workflow job for this annotation

GitHub Actions / lint

G115: integer overflow conversion int64 -> uint64 (gosec)
return additionalAuthData
}

Expand Down

0 comments on commit e39975f

Please sign in to comment.