@@ -11,6 +11,7 @@ package rolecreds
1111
1212import (
1313 "fmt"
14+ "sync"
1415 "time"
1516
1617 "github.com/aws/amazon-ssm-agent/agent/ssm/rsaauth"
@@ -49,13 +50,25 @@ type managedInstancesRoleProvider struct {
4950}
5051
5152var (
52- emptyCredential = credentials.Value {ProviderName : ProviderName }
53+ emptyCredential = credentials.Value {ProviderName : ProviderName }
54+ credentialsSingleton * credentials.Credentials
55+ lock sync.RWMutex
5356)
5457
55- // NewCredentials returns a pointer to a new Credentials object wrapping
56- // the managedInstancesRoleProvider. Takes a ConfigProvider to create a EC2Metadata client.
57- // The ConfigProvider is satisfied by the session.Session type.
58- func NewCredentials (options ... func (* managedInstancesRoleProvider )) * credentials.Credentials {
58+ // ManagedInstanceCredentialsInstance returns a singleton instance of
59+ // Crednetials which provides credentials of a managed instance.
60+ func ManagedInstanceCredentialsInstance () * credentials.Credentials {
61+ lock .Lock ()
62+ defer lock .Unlock ()
63+ if credentialsSingleton == nil {
64+ credentialsSingleton = newManagedInstanceCredentials ()
65+ }
66+ return credentialsSingleton
67+ }
68+
69+ // newManagedInstanceCredentials returns a pointer to a new Credentials object wrapping
70+ // the managedInstancesRoleProvider.
71+ func newManagedInstanceCredentials () * credentials.Credentials {
5972 instanceID := managedInstance .InstanceID ()
6073 region := managedInstance .Region ()
6174 privateKey := managedInstance .PrivateKey ()
@@ -64,10 +77,6 @@ func NewCredentials(options ...func(*managedInstancesRoleProvider)) *credentials
6477 ExpiryWindow : EarlyExpiryTimeWindow ,
6578 }
6679
67- for _ , option := range options {
68- option (p )
69- }
70-
7180 return credentials .NewCredentials (p )
7281}
7382
0 commit comments