Skip to content

Commit

Permalink
ensure cred context
Browse files Browse the repository at this point in the history
  • Loading branch information
riveryc authored and daxingplay committed Jan 4, 2025
1 parent a447069 commit be89557
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
22 changes: 20 additions & 2 deletions cmd/saml2alibabacloud/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ func Login(loginFlags *flags.LoginExecFlags) error {

sharedCreds := alibabacloudconfig.NewSharedCredentials(account.Profile)

logger.Debug("check if Creds Exist")

// this checks if the credentials file has been created yet
exist, err := sharedCreds.CredsExists()
if err != nil {
return errors.Wrap(err, "error loading credentials")
}
if !exist {
log.Println("unable to load credentials, login required to create them")
return nil
}

if !sharedCreds.Expired() && !loginFlags.Force {
log.Println("credentials are not expired skipping")
return nil
}

loginDetails, err := resolveLoginDetails(account, loginFlags)
if err != nil {
log.Printf("%+v", err)
Expand Down Expand Up @@ -272,7 +289,8 @@ func saveCredentials(alibabacloudCreds *alibabacloudconfig.AliCloudCredentials,
log.Println("")
log.Println("Your new access key pair has been stored in the AlibabaCloud CLI configuration")
// log.Printf("Note that it will expire at %v", alibabacloudCreds.Expires)
log.Println("To use this credential, call the AlibabaCloud CLI with the --profile option (e.g. aliyun --profile", sharedCreds.Profile, "sts GetCallerIdentity --region=cn-hangzhou).")

log.Printf("To use this credential, call the AlibabaCloud CLI with the --profile option (e.g. aliyun --profile %s sts GetCallerIdentity --region=%s)", sharedCreds.Profile, alibabacloudCreds.Region)
log.Println("")
log.Printf("For your convenience, please execute: alias aliyun='aliyun --profile %s'", sharedCreds.Profile)
return nil
}
3 changes: 2 additions & 1 deletion pkg/alibabacloudconfig/alibabacloudconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (p *CredentialsProvider) Save(alibabacloudCreds *AliCloudCredentials) error
AccessKeyId: alibabacloudCreds.AliCloudAccessKey,
AccessKeySecret: alibabacloudCreds.AliCloudSecretKey,
StsToken: alibabacloudCreds.AliCloudSecurityToken,
RegionId: alibabacloudCreds.Region,
OutputFormat: "json",
Language: "en",
}
Expand Down Expand Up @@ -139,7 +140,7 @@ func (p *CredentialsProvider) ensureConfigExists() error {
logger.WithField("dir", dir).Debug("Dir created")

// create an base config file
err = ioutil.WriteFile(filename, []byte("{}"), 0600)
err = ioutil.WriteFile(filename, []byte("["+p.Profile+"]"), 0600)
if err != nil {
return err
}
Expand Down

0 comments on commit be89557

Please sign in to comment.