@@ -31,32 +31,79 @@ func (m mockErrorCode) Error() string {
31
31
}
32
32
33
33
func TestWebIdentityProviderRetrieve (t * testing.T ) {
34
- defer func () func () {
35
- o := sdk .NowTime
36
- sdk .NowTime = func () time.Time {
37
- return time.Time {}
38
- }
39
- return func () {
40
- sdk .NowTime = o
41
- }
42
- }()()
34
+ restorTime := sdk .TestingUseReferenceTime (time.Time {})
35
+ defer restorTime ()
43
36
44
37
cases := map [string ]struct {
45
38
mockClient mockAssumeRoleWithWebIdentity
46
39
roleARN string
47
40
tokenFilepath string
48
41
sessionName string
49
- expectedError error
42
+ options func ( * stscreds. WebIdentityRoleOptions )
50
43
expectedCredValue aws.Credentials
51
44
}{
52
- "session name case " : {
45
+ "success " : {
53
46
roleARN : "arn01234567890123456789" ,
54
47
tokenFilepath : "testdata/token.jwt" ,
55
- sessionName : "foo" ,
56
- mockClient : func (ctx context.Context , params * sts.AssumeRoleWithWebIdentityInput , optFns ... func (* sts.Options )) (* sts.AssumeRoleWithWebIdentityOutput , error ) {
48
+ options : func (o * stscreds.WebIdentityRoleOptions ) {
49
+ o .RoleSessionName = "foo"
50
+ },
51
+ mockClient : func (
52
+ ctx context.Context , params * sts.AssumeRoleWithWebIdentityInput , optFns ... func (* sts.Options ),
53
+ ) (
54
+ * sts.AssumeRoleWithWebIdentityOutput , error ,
55
+ ) {
56
+ if e , a := "foo" , * params .RoleSessionName ; e != a {
57
+ return nil , fmt .Errorf ("expected %v, but received %v" , e , a )
58
+ }
59
+ if params .DurationSeconds != nil {
60
+ return nil , fmt .Errorf ("expect no duration seconds, got %v" ,
61
+ * params .DurationSeconds )
62
+ }
63
+ if params .Policy != nil {
64
+ return nil , fmt .Errorf ("expect no policy, got %v" ,
65
+ * params .Policy )
66
+ }
67
+ return & sts.AssumeRoleWithWebIdentityOutput {
68
+ Credentials : & types.Credentials {
69
+ Expiration : aws .Time (sdk .NowTime ()),
70
+ AccessKeyId : aws .String ("access-key-id" ),
71
+ SecretAccessKey : aws .String ("secret-access-key" ),
72
+ SessionToken : aws .String ("session-token" ),
73
+ },
74
+ }, nil
75
+ },
76
+ expectedCredValue : aws.Credentials {
77
+ AccessKeyID : "access-key-id" ,
78
+ SecretAccessKey : "secret-access-key" ,
79
+ SessionToken : "session-token" ,
80
+ Source : stscreds .WebIdentityProviderName ,
81
+ CanExpire : true ,
82
+ Expires : sdk .NowTime (),
83
+ },
84
+ },
85
+ "success with duration and policy" : {
86
+ roleARN : "arn01234567890123456789" ,
87
+ tokenFilepath : "testdata/token.jwt" ,
88
+ options : func (o * stscreds.WebIdentityRoleOptions ) {
89
+ o .Duration = 42 * time .Second
90
+ o .Policy = aws .String ("super secret policy" )
91
+ o .RoleSessionName = "foo"
92
+ },
93
+ mockClient : func (
94
+ ctx context.Context , params * sts.AssumeRoleWithWebIdentityInput , optFns ... func (* sts.Options ),
95
+ ) (
96
+ * sts.AssumeRoleWithWebIdentityOutput , error ,
97
+ ) {
57
98
if e , a := "foo" , * params .RoleSessionName ; e != a {
58
99
return nil , fmt .Errorf ("expected %v, but received %v" , e , a )
59
100
}
101
+ if e , a := int32 (42 ), aws .ToInt32 (params .DurationSeconds ); e != a {
102
+ return nil , fmt .Errorf ("expect %v duration seconds, got %v" , e , a )
103
+ }
104
+ if e , a := "super secret policy" , aws .ToString (params .Policy ); e != a {
105
+ return nil , fmt .Errorf ("expect %v policy, got %v" , e , a )
106
+ }
60
107
return & sts.AssumeRoleWithWebIdentityOutput {
61
108
Credentials : & types.Credentials {
62
109
Expiration : aws .Time (sdk .NowTime ()),
@@ -78,8 +125,14 @@ func TestWebIdentityProviderRetrieve(t *testing.T) {
78
125
"configures token retry" : {
79
126
roleARN : "arn01234567890123456789" ,
80
127
tokenFilepath : "testdata/token.jwt" ,
81
- sessionName : "foo" ,
82
- mockClient : func (ctx context.Context , params * sts.AssumeRoleWithWebIdentityInput , optFns ... func (* sts.Options )) (* sts.AssumeRoleWithWebIdentityOutput , error ) {
128
+ options : func (o * stscreds.WebIdentityRoleOptions ) {
129
+ o .RoleSessionName = "foo"
130
+ },
131
+ mockClient : func (
132
+ ctx context.Context , params * sts.AssumeRoleWithWebIdentityInput , optFns ... func (* sts.Options ),
133
+ ) (
134
+ * sts.AssumeRoleWithWebIdentityOutput , error ,
135
+ ) {
83
136
o := sts.Options {}
84
137
for _ , fn := range optFns {
85
138
fn (& o )
@@ -112,13 +165,19 @@ func TestWebIdentityProviderRetrieve(t *testing.T) {
112
165
113
166
for name , c := range cases {
114
167
t .Run (name , func (t * testing.T ) {
115
- p := stscreds .NewWebIdentityRoleProvider (c .mockClient , c .roleARN , stscreds .IdentityTokenFile (c .tokenFilepath ),
116
- func (o * stscreds.WebIdentityRoleOptions ) {
117
- o .RoleSessionName = c .sessionName
118
- })
168
+ var optFns []func (* stscreds.WebIdentityRoleOptions )
169
+ if c .options != nil {
170
+ optFns = append (optFns , c .options )
171
+ }
172
+ p := stscreds .NewWebIdentityRoleProvider (
173
+ c .mockClient ,
174
+ c .roleARN ,
175
+ stscreds .IdentityTokenFile (c .tokenFilepath ),
176
+ optFns ... ,
177
+ )
119
178
credValue , err := p .Retrieve (context .Background ())
120
- if e , a := c . expectedError , err ; ! reflect . DeepEqual ( e , a ) {
121
- t .Errorf ( "expected %v, but received %v" , e , a )
179
+ if err != nil {
180
+ t .Fatalf ( "expect no error, got %v" , err )
122
181
}
123
182
124
183
if e , a := c .expectedCredValue , credValue ; ! reflect .DeepEqual (e , a ) {
0 commit comments