Skip to content

Commit

Permalink
fixes a token refresh issue due to the SSO region not being set
Browse files Browse the repository at this point in the history
  • Loading branch information
chrnorm committed Mar 4, 2024
1 parent e0b9ef5 commit 5affe20
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/cfaws/assumer_aws_sso.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ func (c *Profile) SSOLogin(ctx context.Context, configOpts ConfigOpts) (aws.Cred
cmd += " --sso-region " + region
}

// if the token exists but is invalid, attempt to clear it so that next login works.
secureSSOTokenStorage.ClearSSOToken(ssoTokenKey)

return aws.Credentials{}, fmt.Errorf("error when retrieving credentials from custom process. please login using '%s'", cmd)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/idclogin/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func Login(ctx context.Context, cfg aws.Config, startUrl string, scopes []string
ClientSecret: *client.ClientSecret,
RegistrationExpiresAt: time.Unix(client.ClientSecretExpiresAt, 0),
RefreshToken: token.RefreshToken,
Region: cfg.Region,
}

return &result, nil
Expand Down
12 changes: 12 additions & 0 deletions pkg/securestorage/sso_token_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type SSOToken struct {
ClientID string `json:"clientId,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
RegistrationExpiresAt time.Time `json:"registrationExpiresAt,omitempty"`
Region string `json:"region,omitempty"`
RefreshToken *string `json:"refreshToken,omitempty"`
}

Expand Down Expand Up @@ -72,6 +73,16 @@ func (s *SSOTokensSecureStorage) GetValidSSOToken(ctx context.Context, profileKe
return nil
}

if t.Region == "" {
// if the region is not set, the AWS SSO OIDC client will make an invalid API call and will return an
// 'InvalidGrantException' error.
clio.Errorf("existing token had no SSO region set")
// token is invalid
return nil
}

cfg.Region = t.Region

client := ssooidc.NewFromConfig(cfg)

res, err := client.CreateToken(ctx, &ssooidc.CreateTokenInput{
Expand All @@ -93,6 +104,7 @@ func (s *SSOTokensSecureStorage) GetValidSSOToken(ctx context.Context, profileKe
ClientSecret: t.ClientSecret, // same as the previous token, because the same client was used to refresh
RegistrationExpiresAt: t.RegistrationExpiresAt, // same as the previous token, because the same client was used to refresh
RefreshToken: res.RefreshToken,
Region: t.Region,
}

// save the refreshed token to secure storage
Expand Down

0 comments on commit 5affe20

Please sign in to comment.