Skip to content

Commit

Permalink
fix bug of RAM credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
ailan-gl committed Mar 8, 2018
1 parent cca77d7 commit d9eb5f9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
21 changes: 13 additions & 8 deletions config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewConfigureCommand() (*cli.Command) {
c := &cli.Command{
Name: "configure",
Short: i18n.T("configure credential and settings", "配置身份认证和其他信息"),
Usage: "configure --mode certificatedMode --profile profileName",
Usage: "configure --mode <AuthenticateMode> --profile <profileName>",
Run: func(c *cli.Context, args []string) error {
if len(args) > 0 {
return cli.NewInvalidCommandError(args[0], c)
Expand All @@ -38,7 +38,7 @@ func NewConfigureCommand() (*cli.Command) {
f.Persistent = true

c.Flags().PersistentStringVar(&mode, "mode", "AK",
i18n.T("use `--mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair}` to assign certificate mode",
i18n.T("use `--mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair}` to assign authenticate mode",
"使用 `--mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair}` 指定认证方式"))

c.AddSubCommand(NewConfigureGetCommand())
Expand Down Expand Up @@ -67,7 +67,7 @@ func doConfigure(profileName string) error {

fmt.Printf("Configuring profile '%s' ...\n", profileName)
if mode != "" {
switch CertificateMode(mode) {
switch AuthenticateMode(mode) {
case AK:
cp.Mode = AK
configureAK(&cp)
Expand All @@ -84,7 +84,7 @@ func doConfigure(profileName string) error {
cp.Mode = RsaKeyPair
configureRsaKeyPair(&cp)
default:
return fmt.Errorf("unexcepted certificated mode: %s", mode)
return fmt.Errorf("unexcepted authenticate mode: %s", mode)
}
} else {
configureAK(&cp)
Expand All @@ -94,11 +94,16 @@ func doConfigure(profileName string) error {
// configure common
fmt.Printf("Default Region Id [%s]: ", cp.RegionId)
cp.RegionId = ReadInput(cp.RegionId)
fmt.Printf("Default Output Format [%s]: ", cp.OutputFormat)
cp.OutputFormat = ReadInput(cp.OutputFormat)
fmt.Printf("Default Language [%s]: ", cp.Language)
fmt.Printf("Default Output Format [%s]: json", cp.OutputFormat)
// cp.OutputFormat = ReadInput(cp.OutputFormat)
cp.OutputFormat = "json"
fmt.Printf("Default Language [zh|en] %s: ", cp.Language)
cp.Language = ReadInput(cp.Language)

if cp.Language != "zh" && cp.Language != "en" {
cp.Language = "en"
}
fmt.Printf("User site: [china|international|japan] %s", cp.Site)
cp.Site = ReadInput(cp.Site)

fmt.Printf("Saving profile[%s] ...", profileName)
conf.PutProfile(cp)
Expand Down
6 changes: 5 additions & 1 deletion config/configure_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func NewConfigureSetCommand() (*cli.Command) {
fs.Add(cli.Flag{Name: "language", Assignable: true,
Usage: i18n.T("assign language, support en/zh", "")})

fs.Add(cli.Flag{Name: "site", Assignable: true,
Usage: i18n.T("assign site, support china/international/japan", "")})

return cmd
}

Expand All @@ -87,7 +90,7 @@ func doConfigureSet(c *cli.Context) {

mode, ok := c.Flags().GetValue("mode")
if ok {
profile.Mode = CertificateMode(mode)
profile.Mode = AuthenticateMode(mode)
} else {
if profile.Mode == "" {
profile.Mode = AK
Expand Down Expand Up @@ -118,6 +121,7 @@ func doConfigureSet(c *cli.Context) {
profile.RegionId = fs.GetValueOrDefault("region", profile.RegionId)
profile.Language = fs.GetValueOrDefault("language", profile.Language)
profile.OutputFormat = fs.GetValueOrDefault("output", profile.OutputFormat)
profile.Site = fs.GetValueOrDefault("site", profile.Site)

err = profile.Validate()
if err != nil {
Expand Down
67 changes: 33 additions & 34 deletions config/profile.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package config

/*
* Copyright (C) 2017-2018 Alibaba Group Holding Limited
*/
Expand All @@ -8,40 +9,41 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/auth/credentials"
)

type CertificateMode string
type AuthenticateMode string

const (
AK = CertificateMode("AK")
StsToken = CertificateMode("StsToken")
RamRoleArn = CertificateMode("RamRoleArn")
EcsRamRole = CertificateMode("EcsRamRole")
RsaKeyPair = CertificateMode("RsaKeyPair")
AK = AuthenticateMode("AK")
StsToken = AuthenticateMode("StsToken")
RamRoleArn = AuthenticateMode("RamRoleArn")
EcsRamRole = AuthenticateMode("EcsRamRole")
RsaKeyPair = AuthenticateMode("RsaKeyPair")
)

type Profile struct {
Name string `json:"name"`
Mode CertificateMode `json:"mode"`
AccessKeyId string `json:"access_key_id"`
AccessKeySecret string `json:"access_key_secret"`
StsToken string `json:"sts_token"`
RamRoleName string `json:"ram_role_name"`
RamRoleArn string `json:"ram_role_arn"`
RoleSessionName string `json:"ram_session_name"`
PrivateKey string `json:"private_key"`
KeyPairName string `json:"key_pair_name"`
ExpiredSeconds int `json:"expired_seconds"`
Verified string `json:"verified"`
RegionId string `json:"region_id"`
OutputFormat string `json:"output_format"`
Language string `json:"language"`
Name string `json:"name"`
Mode AuthenticateMode `json:"mode"`
AccessKeyId string `json:"access_key_id"`
AccessKeySecret string `json:"access_key_secret"`
StsToken string `json:"sts_token"`
RamRoleName string `json:"ram_role_name"`
RamRoleArn string `json:"ram_role_arn"`
RoleSessionName string `json:"ram_session_name"`
PrivateKey string `json:"private_key"`
KeyPairName string `json:"key_pair_name"`
ExpiredSeconds int `json:"expired_seconds"`
Verified string `json:"verified"`
RegionId string `json:"region_id"`
OutputFormat string `json:"output_format"`
Language string `json:"language"`
Site string `json:"Site"`
}

func NewProfile(name string) (Profile) {
return Profile {
Name: name,
Mode: AK,
return Profile{
Name: name,
Mode: AK,
OutputFormat: "json",
Language: "en",
Language: "en",
}
}

Expand Down Expand Up @@ -136,32 +138,29 @@ func (cp *Profile) GetClientByEcsRamRole() (*sdk.Client, error) {
return nil, fmt.Errorf("RamRole is empty! run `aliyun configure` first")
}

cred := credentials.NewStsRoleNameOnEcsCredential(cp.RamRoleName)
cred := credentials.NewEcsRamRoleCredential(cp.RamRoleName)
config := sdk.NewConfig()
client, err := sdk.NewClientWithOptions(cp.RegionId, config, &cred)
client, err := sdk.NewClientWithOptions(cp.RegionId, config, cred)
return client, err
}

func (cp *Profile) GetClientBySts() (*sdk.Client, error) {
cred := credentials.NewStsTokenCredential(cp.AccessKeyId, cp.AccessKeySecret, cp.StsToken)
config := sdk.NewConfig()
client, err := sdk.NewClientWithOptions(cp.RegionId, config, &cred)
client, err := sdk.NewClientWithOptions(cp.RegionId, config, cred)
return client, err
}

func (cp *Profile) GetClientByRoleArn() (*sdk.Client, error) {
cred := credentials.NewStsRoleArnCredential(cp.AccessKeyId, cp.AccessKeySecret, cp.RamRoleArn, cp.RoleSessionName, cp.ExpiredSeconds)
cred := credentials.NewRamRoleArnCredential(cp.AccessKeyId, cp.AccessKeySecret, cp.RamRoleArn, cp.RoleSessionName, cp.ExpiredSeconds)
config := sdk.NewConfig()
client, err := sdk.NewClientWithOptions(cp.RegionId, config, &cred)
client, err := sdk.NewClientWithOptions(cp.RegionId, config, cred)
return client, err
}

func (cp *Profile) GetClientByPrivateKey() (*sdk.Client, error) {
cred := credentials.NewRsaKeyPairCredential(cp.PrivateKey, cp.KeyPairName, cp.ExpiredSeconds)
config := sdk.NewConfig()
client, err := sdk.NewClientWithOptions(cp.RegionId, config, &cred)
client, err := sdk.NewClientWithOptions(cp.RegionId, config, cred)
return client, err
}



0 comments on commit d9eb5f9

Please sign in to comment.