diff --git a/cmd/saml2alibabacloud/commands/login.go b/cmd/saml2alibabacloud/commands/login.go index fe6322b7e..4177b6c99 100644 --- a/cmd/saml2alibabacloud/commands/login.go +++ b/cmd/saml2alibabacloud/commands/login.go @@ -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) @@ -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 } diff --git a/pkg/alibabacloudconfig/alibabacloudconfig.go b/pkg/alibabacloudconfig/alibabacloudconfig.go index 58975ea95..79576b5f1 100644 --- a/pkg/alibabacloudconfig/alibabacloudconfig.go +++ b/pkg/alibabacloudconfig/alibabacloudconfig.go @@ -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", } @@ -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 }