@@ -20,9 +20,9 @@ const ProviderName = "EC2RoleProvider"
2020// A Provider retrieves credentials from the EC2 service, and keeps track if
2121// those credentials are expired.
2222//
23- // The NewProvider function must be used to create the Provider.
23+ // The New function must be used to create the Provider.
2424//
25- // p := &ec2rolecreds.NewProvider (ec2metadata.New(cfg ))
25+ // p := &ec2rolecreds.New (ec2metadata.New(options ))
2626//
2727// // Expire the credentials 10 minutes before IAM states they should. Proactivily
2828// // refreshing the credentials.
@@ -31,8 +31,13 @@ type Provider struct {
3131 aws.SafeCredentialsProvider
3232
3333 // Required EC2Metadata client to use when connecting to EC2 metadata service.
34- Client * ec2metadata.Client
34+ client * ec2metadata.Client
3535
36+ options ProviderOptions
37+ }
38+
39+ // ProviderOptions is a list of user settable options for setting the behavior of the Provider.
40+ type ProviderOptions struct {
3641 // ExpiryWindow will allow the credentials to trigger refreshing prior to
3742 // the credentials actually expiring. This is beneficial so race conditions
3843 // with expiring credentials do not cause request to fail unexpectedly
@@ -45,22 +50,26 @@ type Provider struct {
4550 ExpiryWindow time.Duration
4651}
4752
48- // NewProvider returns an initialized Provider value configured to retrieve
53+ // New returns an initialized Provider value configured to retrieve
4954// credentials from EC2 Instance Metadata service.
50- func NewProvider (client * ec2metadata.Client ) * Provider {
51- p := & Provider {
52- Client : client ,
53- }
55+ func New (client * ec2metadata.Client , options ... func ( * ProviderOptions ) ) * Provider {
56+ p := & Provider {}
57+
58+ p . client = client
5459 p .RetrieveFn = p .retrieveFn
5560
61+ for _ , option := range options {
62+ option (& p .options )
63+ }
64+
5665 return p
5766}
5867
5968// Retrieve retrieves credentials from the EC2 service.
6069// Error will be returned if the request fails, or unable to extract
6170// the desired credentials.
6271func (p * Provider ) retrieveFn (ctx context.Context ) (aws.Credentials , error ) {
63- credsList , err := requestCredList (ctx , p .Client )
72+ credsList , err := requestCredList (ctx , p .client )
6473 if err != nil {
6574 return aws.Credentials {}, err
6675 }
@@ -71,7 +80,7 @@ func (p *Provider) retrieveFn(ctx context.Context) (aws.Credentials, error) {
7180 }
7281 credsName := credsList [0 ]
7382
74- roleCreds , err := requestCred (ctx , p .Client , credsName )
83+ roleCreds , err := requestCred (ctx , p .client , credsName )
7584 if err != nil {
7685 return aws.Credentials {}, err
7786 }
@@ -83,7 +92,7 @@ func (p *Provider) retrieveFn(ctx context.Context) (aws.Credentials, error) {
8392 Source : ProviderName ,
8493
8594 CanExpire : true ,
86- Expires : roleCreds .Expiration .Add (- p .ExpiryWindow ),
95+ Expires : roleCreds .Expiration .Add (- p .options . ExpiryWindow ),
8796 }
8897
8998 return creds , nil
0 commit comments