Skip to content

Commit 38b90ef

Browse files
VishnuKarthikRavindrancjinaws
authored andcommitted
Set IPR creds expiry to 30mins for worker
cr: https://code.amazon.com/reviews/CR-113929033
1 parent 0c44030 commit 38b90ef

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

common/identity/credentialproviders/ec2roleprovider/ec2_role_provider.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@ import (
2121
"sync"
2222
"time"
2323

24-
"github.com/aws/aws-sdk-go/aws"
25-
"github.com/aws/aws-sdk-go/aws/awserr"
26-
"github.com/aws/aws-sdk-go/aws/credentials"
27-
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
28-
"github.com/aws/aws-sdk-go/service/ssm"
29-
3024
"github.com/aws/amazon-ssm-agent/agent/appconfig"
3125
"github.com/aws/amazon-ssm-agent/agent/log"
3226
"github.com/aws/amazon-ssm-agent/agent/sdkutil"
3327
"github.com/aws/amazon-ssm-agent/agent/version"
3428
"github.com/aws/amazon-ssm-agent/common/identity/credentialproviders/ssmec2roleprovider"
3529
"github.com/aws/amazon-ssm-agent/common/runtimeconfig"
30+
"github.com/aws/aws-sdk-go/aws"
31+
"github.com/aws/aws-sdk-go/aws/awserr"
32+
"github.com/aws/aws-sdk-go/aws/credentials"
33+
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
34+
"github.com/aws/aws-sdk-go/service/ssm"
3635
)
3736

3837
// EC2RoleProvider provides credentials for the agent when on an EC2 instance
@@ -112,6 +111,9 @@ func (p *EC2RoleProvider) RetrieveWithContext(ctx context.Context) (credentials.
112111
p.Log.Error(err)
113112
return iprEmptyCredential, err
114113
}
114+
115+
// set expiration to 30 minutes
116+
p.InnerProviders.IPRProvider.SetExpiration(timeNowFunc().Add(30*time.Minute), 0)
115117
p.credentialSource = CredentialSourceEC2
116118

117119
return iprCredentials, nil

common/identity/credentialproviders/ec2roleprovider/ec2_role_provider_test.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ import (
2222
"testing"
2323
"time"
2424

25-
"github.com/aws/aws-sdk-go/aws/awserr"
26-
"github.com/aws/aws-sdk-go/aws/credentials"
27-
"github.com/aws/aws-sdk-go/service/ssm"
28-
"github.com/stretchr/testify/assert"
29-
"github.com/stretchr/testify/mock"
30-
3125
"github.com/aws/amazon-ssm-agent/agent/appconfig"
3226
"github.com/aws/amazon-ssm-agent/agent/log"
3327
logmocks "github.com/aws/amazon-ssm-agent/agent/mocks/log"
@@ -37,6 +31,11 @@ import (
3731
"github.com/aws/amazon-ssm-agent/common/identity/credentialproviders/ssmec2roleprovider"
3832
"github.com/aws/amazon-ssm-agent/common/runtimeconfig"
3933
runtimeConfigMocks "github.com/aws/amazon-ssm-agent/common/runtimeconfig/mocks"
34+
"github.com/aws/aws-sdk-go/aws/awserr"
35+
"github.com/aws/aws-sdk-go/aws/credentials"
36+
"github.com/aws/aws-sdk-go/service/ssm"
37+
"github.com/stretchr/testify/assert"
38+
"github.com/stretchr/testify/mock"
4039
)
4140

4241
const (
@@ -253,9 +252,16 @@ func TestEC2RoleProvider_Retrieve_ReturnsIPRCredentials(t *testing.T) {
253252
ec2RoleProvider.RuntimeConfigClient = runtimeConfigClient
254253

255254
// Act
255+
flag := false
256+
timeNowFunc = func() time.Time {
257+
flag = true
258+
return time.Now()
259+
}
256260
creds, err := ec2RoleProvider.Retrieve()
257-
261+
expiryMins := time.Now().Sub(ec2RoleProvider.ExpiresAt()).Minutes()
258262
//Assert
263+
assert.True(t, flag)
264+
assert.True(t, 28 >= expiryMins && expiryMins <= 30)
259265
assert.NoError(t, err)
260266
assert.Equal(t, iprProvider.ProviderName, creds.ProviderName)
261267
assert.Equal(t, CredentialSourceEC2, ec2RoleProvider.credentialSource)
@@ -404,7 +410,6 @@ func TestEC2RoleProvider_RetrieveRemote_ReturnsEmptyCredentials(t *testing.T) {
404410
assert.Equal(t, CredentialSourceNone, ec2RoleProvider.credentialSource)
405411
})
406412
}
407-
408413
}
409414

410415
func arrangeRetrieveEmptyTest(j testCase) *EC2RoleProvider {

0 commit comments

Comments
 (0)