Skip to content

Commit 6385e63

Browse files
Ihar LichkoCodeSpaceiiii
andauthored
CVO-17 Add an External ID param on RAM Role Authentication (#1153)
Co-authored-by: Mingming Zhu <[email protected]>
1 parent 0f9028d commit 6385e63

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+169
-19
lines changed

config/configure.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ func configureRamRoleArn(w io.Writer, cp *Profile) error {
265265
if cp.ExpiredSeconds == 0 {
266266
cp.ExpiredSeconds = 900
267267
}
268+
cli.Printf(w, "External ID [%s]: ", cp.ExternalId)
269+
cp.ExternalId = ReadInput(cp.ExternalId)
268270
cli.Printf(w, "Expired Seconds [%v]: ", cp.ExpiredSeconds)
269271
cp.ExpiredSeconds, _ = strconv.Atoi(ReadInput(strconv.Itoa(cp.ExpiredSeconds)))
270272
return nil
@@ -305,6 +307,8 @@ func configureChainableRamRoleArn(w io.Writer, cp *Profile) error {
305307
if cp.ExpiredSeconds == 0 {
306308
cp.ExpiredSeconds = 900
307309
}
310+
cli.Printf(w, "External ID [%s]: ", cp.ExternalId)
311+
cp.ExternalId = ReadInput(cp.ExternalId)
308312
cli.Printf(w, "Expired Seconds [%v]: ", cp.ExpiredSeconds)
309313
cp.ExpiredSeconds, _ = strconv.Atoi(ReadInput(strconv.Itoa(cp.ExpiredSeconds)))
310314
return nil

config/configure_get.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ func doConfigureGet(c *cli.Context, args []string) {
8585
cli.Printf(c.Stdout(), "ram-role-name=%s\n", profile.RamRoleName)
8686
case RamRoleArnFlagName:
8787
cli.Printf(c.Stdout(), "ram-role-arn=%s\n", profile.RamRoleArn)
88+
case ExternalIdFlagName:
89+
cli.Printf(c.Stdout(), "external-id=%s\n", profile.ExternalId)
8890
case RoleSessionNameFlagName:
8991
cli.Printf(c.Stdout(), "role-session-name=%s\n", profile.RoleSessionName)
9092
case KeyPairNameFlagName:

config/configure_get_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func TestDoConfigureGet(t *testing.T) {
7272
w.Reset()
7373
stderr.Reset()
7474
ctx.Flags().Flags()[1].SetAssigned(false)
75-
doConfigureGet(ctx, []string{"profile", "mode", "access-key-id", "access-key-secret", "sts-token", "ram-role-name", "ram-role-arn", "role-session-name", "private-key", "key-pair-name", "region", "language"})
76-
assert.Equal(t, "profile=default\nmode=AK\naccess-key-id=*************************_id\naccess-key-secret=*****************************ret\nsts-token=\nram-role-name=\nram-role-arn=\nrole-session-name=\nprivate-key=\nkey-pair-name=\nlanguage=\n\n", w.String())
75+
doConfigureGet(ctx, []string{"profile", "mode", "access-key-id", "access-key-secret", "sts-token", "ram-role-name", "ram-role-arn", "role-session-name", "external-id", "private-key", "key-pair-name", "region", "language"})
76+
assert.Equal(t, "profile=default\nmode=AK\naccess-key-id=*************************_id\naccess-key-secret=*****************************ret\nsts-token=\nram-role-name=\nram-role-arn=\nrole-session-name=\nexternal-id=\nprivate-key=\nkey-pair-name=\nlanguage=\n\n", w.String())
7777

7878
//TESTCASE 4
7979
hookLoadConfiguration = func(fn func(path string) (*Configuration, error)) func(path string) (*Configuration, error) {

config/configure_list.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,18 @@ func doConfigureList(w io.Writer) {
6161
cred = "StsToken:" + "***" + GetLastChars(pf.AccessKeyId, 3)
6262
case RamRoleArn:
6363
cred = "RamRoleArn:" + "***" + GetLastChars(pf.AccessKeyId, 3)
64+
if pf.ExternalId != "" {
65+
cred = cred + ":" + GetLastChars(pf.ExternalId, 3)
66+
}
6467
case EcsRamRole:
6568
cred = "EcsRamRole:" + pf.RamRoleName
6669
case RamRoleArnWithEcs:
6770
cred = "arn:" + "***" + GetLastChars(pf.AccessKeyId, 3)
6871
case ChainableRamRoleArn:
6972
cred = "ChainableRamRoleArn:" + pf.SourceProfile + ":" + pf.RamRoleArn
73+
if pf.ExternalId != "" {
74+
cred = cred + ":" + GetLastChars(pf.ExternalId, 3)
75+
}
7076
case RsaKeyPair:
7177
cred = "RsaKeyPair:" + pf.KeyPairName
7278
case External:

config/configure_list_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ func TestDoConfigureList(t *testing.T) {
5858
RamRoleArn: "RamRoleArn",
5959
RoleSessionName: "RoleSessionName",
6060
},
61+
{
62+
Name: "bbbe",
63+
Mode: RamRoleArn,
64+
AccessKeyId: "sdf",
65+
AccessKeySecret: "ddf",
66+
OutputFormat: "json",
67+
RamRoleArn: "RamRoleArn",
68+
RoleSessionName: "RoleSessionName",
69+
ExternalId: "ExternalId",
70+
},
6171
{
6272
Name: "ccc",
6373
Mode: EcsRamRole,
@@ -92,6 +102,7 @@ func TestDoConfigureList(t *testing.T) {
92102
"default * | AK:***_id | Invalid | | \n"+
93103
"aaa | StsToken:****** | Invalid | | \n"+
94104
"bbb | RamRoleArn:****** | Invalid | | \n"+
105+
"bbbe | RamRoleArn:******:lId | Invalid | | \n"+
95106
"ccc | EcsRamRole:RamRoleName | Invalid | | \n"+
96107
"ddd | RsaKeyPair:KeyPairName | Invalid | | \n"+
97108
"eee | CloudSSO:a@b | Invalid | | \n", w.String())

config/configure_set.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func doConfigureSet(w io.Writer, flags *cli.FlagSet) {
8686
profile.AccessKeySecret = AccessKeySecretFlag(flags).GetStringOrDefault(profile.AccessKeySecret)
8787
profile.RamRoleArn = RamRoleArnFlag(flags).GetStringOrDefault(profile.RamRoleArn)
8888
profile.RoleSessionName = RoleSessionNameFlag(flags).GetStringOrDefault(profile.RoleSessionName)
89+
profile.ExternalId = ExternalIdFlag(flags).GetStringOrDefault(profile.ExternalId)
8990
profile.ExpiredSeconds = ExpiredSecondsFlag(flags).GetIntegerOrDefault(profile.ExpiredSeconds)
9091
case EcsRamRole:
9192
profile.RamRoleName = RamRoleNameFlag(flags).GetStringOrDefault(profile.RamRoleName)
@@ -98,6 +99,7 @@ func doConfigureSet(w io.Writer, flags *cli.FlagSet) {
9899
profile.SourceProfile = SourceProfileFlag(flags).GetStringOrDefault(profile.SourceProfile)
99100
profile.RamRoleArn = RamRoleArnFlag(flags).GetStringOrDefault(profile.RamRoleArn)
100101
profile.RoleSessionName = RoleSessionNameFlag(flags).GetStringOrDefault(profile.RoleSessionName)
102+
profile.ExternalId = ExternalIdFlag(flags).GetStringOrDefault(profile.ExternalId)
101103
profile.ExpiredSeconds = ExpiredSecondsFlag(flags).GetIntegerOrDefault(profile.ExpiredSeconds)
102104
case RsaKeyPair:
103105
profile.PrivateKey = PrivateKeyFlag(flags).GetStringOrDefault(profile.PrivateKey)

config/configure_set_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func TestDoConfigureSet(t *testing.T) {
9797
return &Configuration{
9898
CurrentProfile: "default",
9999
Profiles: []Profile{
100-
{Name: "default", Mode: RamRoleArn, RoleSessionName: "RoleSessionName", RamRoleArn: "RamRoleArn", AccessKeyId: "default_aliyun_access_key_id", AccessKeySecret: "default_aliyun_access_key_secret", OutputFormat: "json", RegionId: "cn-hangzhou"},
100+
{Name: "default", Mode: RamRoleArn, RoleSessionName: "RoleSessionName", RamRoleArn: "RamRoleArn", ExternalId: "ExternalId", AccessKeyId: "default_aliyun_access_key_id", AccessKeySecret: "default_aliyun_access_key_secret", OutputFormat: "json", RegionId: "cn-hangzhou"},
101101
{Name: "aaa", Mode: AK, AccessKeyId: "sdf", AccessKeySecret: "ddf", OutputFormat: "json"}}}, nil
102102
}
103103
}

config/configure_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ func TestConfigureStsToken(t *testing.T) {
192192

193193
func TestConfigureRamRoleArn(t *testing.T) {
194194
w := new(bytes.Buffer)
195-
err := configureRamRoleArn(w, &Profile{Name: "default", Mode: AK, AccessKeyId: "access_key_id", AccessKeySecret: "access_key_secret", RamRoleArn: "RamRoleArn", RoleSessionName: "RoleSessionName", RegionId: "cn-hangzhou", OutputFormat: "json"})
196-
assert.Equal(t, "Access Key Id [**********_id]: Access Key Secret [**************ret]: Sts Region []: Ram Role Arn [RamRoleArn]: Role Session Name [RoleSessionName]: Expired Seconds [900]: ", w.String())
195+
err := configureRamRoleArn(w, &Profile{Name: "default", Mode: AK, AccessKeyId: "access_key_id", AccessKeySecret: "access_key_secret", RamRoleArn: "RamRoleArn", RoleSessionName: "RoleSessionName", ExternalId: "ExternalId", RegionId: "cn-hangzhou", OutputFormat: "json"})
196+
assert.Equal(t, "Access Key Id [**********_id]: Access Key Secret [**************ret]: Sts Region []: Ram Role Arn [RamRoleArn]: Role Session Name [RoleSessionName]: External ID [ExternalId]: Expired Seconds [900]: ", w.String())
197197
assert.Nil(t, err)
198198
}
199199

@@ -246,11 +246,12 @@ func TestConfigureChainableRamRoleArn(t *testing.T) {
246246
RamRoleArn: "rra",
247247
StsRegion: "cn-hangzhou",
248248
RoleSessionName: "rsn",
249+
ExternalId: "eid",
249250
RegionId: "cn-hangzhou",
250251
ExpiredSeconds: 3600,
251252
OutputFormat: "json",
252253
})
253-
assert.Equal(t, "Source Profile [source]: Sts Region [cn-hangzhou]: Ram Role Arn [rra]: Role Session Name [rsn]: Expired Seconds [3600]: ", w.String())
254+
assert.Equal(t, "Source Profile [source]: Sts Region [cn-hangzhou]: Ram Role Arn [rra]: Role Session Name [rsn]: External ID [eid]: Expired Seconds [3600]: ", w.String())
254255
assert.Nil(t, err)
255256
}
256257

@@ -265,8 +266,9 @@ func TestConfigureChainableRamRoleArnWhenZeroExpiredSeconds(t *testing.T) {
265266
RoleSessionName: "rsn",
266267
RegionId: "cn-hangzhou",
267268
OutputFormat: "json",
269+
ExternalId: "eid",
268270
})
269-
assert.Equal(t, "Source Profile [source]: Sts Region [cn-hangzhou]: Ram Role Arn [rra]: Role Session Name [rsn]: Expired Seconds [900]: ", w.String())
271+
assert.Equal(t, "Source Profile [source]: Sts Region [cn-hangzhou]: Ram Role Arn [rra]: Role Session Name [rsn]: External ID [eid]: Expired Seconds [900]: ", w.String())
270272
assert.Nil(t, err)
271273
}
272274

config/flags.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
RamRoleNameFlagName = "ram-role-name"
3030
RamRoleArnFlagName = "ram-role-arn"
3131
RoleSessionNameFlagName = "role-session-name"
32+
ExternalIdFlagName = "external-id"
3233
SourceProfileFlagName = "source-profile"
3334
PrivateKeyFlagName = "private-key"
3435
KeyPairNameFlagName = "key-pair-name"
@@ -63,6 +64,7 @@ func AddFlags(fs *cli.FlagSet) {
6364
fs.Add(NewRamRoleNameFlag())
6465
fs.Add(NewRamRoleArnFlag())
6566
fs.Add(NewRoleSessionNameFlag())
67+
fs.Add(NewExternalIdFlag())
6668
fs.Add(NewPrivateKeyFlag())
6769
fs.Add(NewKeyPairNameFlag())
6870
fs.Add(NewReadTimeoutFlag())
@@ -122,6 +124,10 @@ func RoleSessionNameFlag(fs *cli.FlagSet) *cli.Flag {
122124
return fs.Get(RoleSessionNameFlagName)
123125
}
124126

127+
func ExternalIdFlag(fs *cli.FlagSet) *cli.Flag {
128+
return fs.Get(ExternalIdFlagName)
129+
}
130+
125131
func PrivateKeyFlag(fs *cli.FlagSet) *cli.Flag {
126132
return fs.Get(PrivateKeyFlagName)
127133
}
@@ -292,6 +298,17 @@ func NewRoleSessionNameFlag() *cli.Flag {
292298
}
293299
}
294300

301+
func NewExternalIdFlag() *cli.Flag {
302+
return &cli.Flag{
303+
Category: "config",
304+
Name: ExternalIdFlagName,
305+
AssignedMode: cli.AssignedOnce,
306+
Short: i18n.T(
307+
"use `--external-id <ExternalId>` to assign ExternalId",
308+
"使用 `--external-id <ExternalId>` 指定ExternalId"),
309+
}
310+
}
311+
295312
func NewExpiredSecondsFlag() *cli.Flag {
296313
return &cli.Flag{
297314
Category: "config",

config/flags_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,24 @@ func TestAddFlag(t *testing.T) {
168168
DefaultValue: "",
169169
Persistent: false,
170170
}
171+
newExternalIdFlag = &cli.Flag{
172+
Category: "config",
173+
Name: ExternalIdFlagName,
174+
AssignedMode: cli.AssignedOnce,
175+
Short: i18n.T(
176+
"use `--external-id <ExternalId>` to assign ExternalId",
177+
"使用 `--external-id <ExternalId>` 指定ExternalId"),
178+
Long: nil,
179+
Required: false,
180+
Aliases: nil,
181+
Hidden: false,
182+
Validate: nil,
183+
Fields: nil,
184+
ExcludeWith: nil,
185+
Shorthand: 0,
186+
DefaultValue: "",
187+
Persistent: false,
188+
}
171189
newPrivateKeyFlag = &cli.Flag{
172190
Category: "config",
173191
Name: PrivateKeyFlagName,
@@ -378,6 +396,9 @@ func TestAddFlag(t *testing.T) {
378396
f = NewRoleSessionNameFlag()
379397
assert.Equal(t, newRoleSessionNameFlag, f)
380398

399+
f = NewExternalIdFlag()
400+
assert.Equal(t, newExternalIdFlag, f)
401+
381402
f = NewPrivateKeyFlag()
382403
assert.Equal(t, newPrivateKeyFlag, f)
383404

0 commit comments

Comments
 (0)