Skip to content
This repository was archived by the owner on Jul 31, 2024. It is now read-only.

Commit 9150250

Browse files
authored
Merge pull request #82 from holidayworking/add_role_name
Add roleName option
2 parents 14c2739 + c9c0a6b commit 9150250

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

aws/saml.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func ParseSAMLResponse(base64Response string) (*SAMLResponse, error) {
103103
}
104104

105105
// ExtractRoleArnAndPrincipalArn extracts role ARN and principal ARN from SAML response
106-
func ExtractRoleArnAndPrincipalArn(samlResponse SAMLResponse) (string, string, error) {
106+
func ExtractRoleArnAndPrincipalArn(samlResponse SAMLResponse, roleName string) (string, string, error) {
107107
for _, attr := range samlResponse.Assertion.AttributeStatement.Attributes {
108108
if attr.Name != roleAttributeName {
109109
continue
@@ -113,6 +113,9 @@ func ExtractRoleArnAndPrincipalArn(samlResponse SAMLResponse) (string, string, e
113113
s := strings.Split(v.Value, ",")
114114
roleArn := s[0]
115115
principalArn := s[1]
116+
if roleName != "" && strings.Split(roleArn, "/")[1] != roleName {
117+
continue
118+
}
116119
return roleArn, principalArn, nil
117120
}
118121
}

aws/saml_test.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ func TestParseSAMLResponse(t *testing.T) {
138138
func TestExtractRoleArnAndPrincipalArn(t *testing.T) {
139139
type args struct {
140140
samlResponse SAMLResponse
141+
roleName string
141142
}
142143
tests := []struct {
143144
name string
@@ -173,10 +174,81 @@ func TestExtractRoleArnAndPrincipalArn(t *testing.T) {
173174
},
174175
},
175176
},
177+
roleName: "",
176178
},
177179
wantRoleArn: "arn:aws:iam::012345678901:role/TestRole",
178180
wantPrincipalArn: "arn:aws:iam::012345678901:saml-provider/TestProvider",
179181
},
182+
{
183+
name: "returns first role when role attribute are multi and no roleName argument",
184+
args: args{
185+
samlResponse: SAMLResponse{
186+
Assertion: Assertion{
187+
AttributeStatement: AttributeStatement{
188+
Attributes: []Attribute{
189+
{
190+
Name: "dummy",
191+
AttributeValues: []AttributeValue{
192+
{
193+
Value: "dummy",
194+
},
195+
},
196+
},
197+
{
198+
Name: roleAttributeName,
199+
AttributeValues: []AttributeValue{
200+
{
201+
Value: "arn:aws:iam::012345678901:role/TestRole1,arn:aws:iam::012345678901:saml-provider/TestProvider1",
202+
},
203+
{
204+
Value: "arn:aws:iam::012345678901:role/TestRole2,arn:aws:iam::012345678901:saml-provider/TestProvider2",
205+
},
206+
},
207+
},
208+
},
209+
},
210+
},
211+
},
212+
roleName: "",
213+
},
214+
wantRoleArn: "arn:aws:iam::012345678901:role/TestRole1",
215+
wantPrincipalArn: "arn:aws:iam::012345678901:saml-provider/TestProvider1",
216+
},
217+
{
218+
name: "returns specify role when role attribute are multi and roleName argument",
219+
args: args{
220+
samlResponse: SAMLResponse{
221+
Assertion: Assertion{
222+
AttributeStatement: AttributeStatement{
223+
Attributes: []Attribute{
224+
{
225+
Name: "dummy",
226+
AttributeValues: []AttributeValue{
227+
{
228+
Value: "dummy",
229+
},
230+
},
231+
},
232+
{
233+
Name: roleAttributeName,
234+
AttributeValues: []AttributeValue{
235+
{
236+
Value: "arn:aws:iam::012345678901:role/TestRole1,arn:aws:iam::012345678901:saml-provider/TestProvider1",
237+
},
238+
{
239+
Value: "arn:aws:iam::012345678901:role/TestRole2,arn:aws:iam::012345678901:saml-provider/TestProvider2",
240+
},
241+
},
242+
},
243+
},
244+
},
245+
},
246+
},
247+
roleName: "TestRole2",
248+
},
249+
wantRoleArn: "arn:aws:iam::012345678901:role/TestRole2",
250+
wantPrincipalArn: "arn:aws:iam::012345678901:saml-provider/TestProvider2",
251+
},
180252
{
181253
name: "returns an error when role attribute does not exist",
182254
args: args{
@@ -196,13 +268,14 @@ func TestExtractRoleArnAndPrincipalArn(t *testing.T) {
196268
},
197269
},
198270
},
271+
roleName: "",
199272
},
200273
wantErr: true,
201274
},
202275
}
203276
for _, tt := range tests {
204277
t.Run(tt.name, func(t *testing.T) {
205-
got, got1, err := ExtractRoleArnAndPrincipalArn(tt.args.samlResponse)
278+
got, got1, err := ExtractRoleArnAndPrincipalArn(tt.args.samlResponse, tt.args.roleName)
206279
if (err != nil) != tt.wantErr {
207280
t.Errorf("ExtractRoleArnAndPrincipalArn() error = %v, wantErr %v", err, tt.wantErr)
208281
return

cmd/root.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func Execute() {
3434

3535
func newRootCmd() *cobra.Command {
3636
var configure bool
37+
var roleName string
3738
var profile string
3839
var showVersion bool
3940

@@ -82,7 +83,7 @@ func newRootCmd() *cobra.Command {
8283
return err
8384
}
8485

85-
roleArn, principalArn, err := aws.ExtractRoleArnAndPrincipalArn(*response)
86+
roleArn, principalArn, err := aws.ExtractRoleArnAndPrincipalArn(*response, roleName)
8687
if err != nil {
8788
return err
8889
}
@@ -102,6 +103,7 @@ func newRootCmd() *cobra.Command {
102103
}
103104
cmd.PersistentFlags().BoolVarP(&configure, "configure", "c", false, "configure initial settings")
104105
cmd.PersistentFlags().StringVarP(&profile, "profile", "p", "default", "AWS profile")
106+
cmd.PersistentFlags().StringVarP(&roleName, "role", "r", "", "AWS IAM role name")
105107
cmd.PersistentFlags().BoolVarP(&showVersion, "version", "v", false, "Show version")
106108

107109
return cmd

0 commit comments

Comments
 (0)