credentials.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Package credentials provides credential retrieval and management
  2. //
  3. // The Credentials is the primary method of getting access to and managing
  4. // credentials Values. Using dependency injection retrieval of the credential
  5. // values is handled by a object which satisfies the Provider interface.
  6. //
  7. // By default the Credentials.Get() will cache the successful result of a
  8. // Provider's Retrieve() until Provider.IsExpired() returns true. At which
  9. // point Credentials will call Provider's Retrieve() to get new credential Value.
  10. //
  11. // The Provider is responsible for determining when credentials Value have expired.
  12. // It is also important to note that Credentials will always call Retrieve the
  13. // first time Credentials.Get() is called.
  14. //
  15. // Example of using the environment variable credentials.
  16. //
  17. // creds := NewEnvCredentials()
  18. //
  19. // // Retrieve the credentials value
  20. // credValue, err := creds.Get()
  21. // if err != nil {
  22. // // handle error
  23. // }
  24. //
  25. // Example of forcing credentials to expire and be refreshed on the next Get().
  26. // This may be helpful to proactively expire credentials and refresh them sooner
  27. // than they would naturally expire on their own.
  28. //
  29. // creds := NewCredentials(&EC2RoleProvider{})
  30. // creds.Expire()
  31. // credsValue, err := creds.Get()
  32. // // New credentials will be retrieved instead of from cache.
  33. //
  34. //
  35. // Custom Provider
  36. //
  37. // Each Provider built into this package also provides a helper method to generate
  38. // a Credentials pointer setup with the provider. To use a custom Provider just
  39. // create a type which satisfies the Provider interface and pass it to the
  40. // NewCredentials method.
  41. //
  42. // type MyProvider struct{}
  43. // func (m *MyProvider) Retrieve() (Value, error) {...}
  44. // func (m *MyProvider) IsExpired() bool {...}
  45. //
  46. // creds := NewCredentials(&MyProvider{})
  47. // credValue, err := creds.Get()
  48. //
  49. package credentials
  50. import (
  51. "sync"
  52. "time"
  53. )
  54. // Create an empty Credential object that can be used as dummy placeholder
  55. // credentials for requests that do not need signed.
  56. //
  57. // This Credentials can be used to configure a service to not sign requests
  58. // when making service API calls. For example, when accessing public
  59. // s3 buckets.
  60. //
  61. // svc := s3.New(&aws.Config{Credentials: AnonymousCredentials})
  62. // // Access public S3 buckets.
  63. //
  64. var AnonymousCredentials = NewStaticCredentials("", "", "")
  65. // A Value is the AWS credentials value for individual credential fields.
  66. type Value struct {
  67. // AWS Access key ID
  68. AccessKeyID string
  69. // AWS Secret Access Key
  70. SecretAccessKey string
  71. // AWS Session Token
  72. SessionToken string
  73. }
  74. // A Provider is the interface for any component which will provide credentials
  75. // Value. A provider is required to manage its own Expired state, and what to
  76. // be expired means.
  77. //
  78. // The Provider should not need to implement its own mutexes, because
  79. // that will be managed by Credentials.
  80. type Provider interface {
  81. // Refresh returns nil if it successfully retrieved the value.
  82. // Error is returned if the value were not obtainable, or empty.
  83. Retrieve() (Value, error)
  84. // IsExpired returns if the credentials are no longer valid, and need
  85. // to be retrieved.
  86. IsExpired() bool
  87. }
  88. // A Credentials provides synchronous safe retrieval of AWS credentials Value.
  89. // Credentials will cache the credentials value until they expire. Once the value
  90. // expires the next Get will attempt to retrieve valid credentials.
  91. //
  92. // Credentials is safe to use across multiple goroutines and will manage the
  93. // synchronous state so the Providers do not need to implement their own
  94. // synchronization.
  95. //
  96. // The first Credentials.Get() will always call Provider.Retrieve() to get the
  97. // first instance of the credentials Value. All calls to Get() after that
  98. // will return the cached credentials Value until IsExpired() returns true.
  99. type Credentials struct {
  100. creds Value
  101. forceRefresh bool
  102. m sync.Mutex
  103. provider Provider
  104. }
  105. // NewCredentials returns a pointer to a new Credentials with the provider set.
  106. func NewCredentials(provider Provider) *Credentials {
  107. return &Credentials{
  108. provider: provider,
  109. forceRefresh: true,
  110. }
  111. }
  112. // Get returns the credentials value, or error if the credentials Value failed
  113. // to be retrieved.
  114. //
  115. // Will return the cached credentials Value if it has not expired. If the
  116. // credentials Value has expired the Provider's Retrieve() will be called
  117. // to refresh the credentials.
  118. //
  119. // If Credentials.Expire() was called the credentials Value will be force
  120. // expired, and the next call to Get() will cause them to be refreshed.
  121. func (c *Credentials) Get() (Value, error) {
  122. c.m.Lock()
  123. defer c.m.Unlock()
  124. if c.isExpired() {
  125. creds, err := c.provider.Retrieve()
  126. if err != nil {
  127. return Value{}, err
  128. }
  129. c.creds = creds
  130. c.forceRefresh = false
  131. }
  132. return c.creds, nil
  133. }
  134. // Expire expires the credentials and forces them to be retrieved on the
  135. // next call to Get().
  136. //
  137. // This will override the Provider's expired state, and force Credentials
  138. // to call the Provider's Retrieve().
  139. func (c *Credentials) Expire() {
  140. c.m.Lock()
  141. defer c.m.Unlock()
  142. c.forceRefresh = true
  143. }
  144. // IsExpired returns if the credentials are no longer valid, and need
  145. // to be retrieved.
  146. //
  147. // If the Credentials were forced to be expired with Expire() this will
  148. // reflect that override.
  149. func (c *Credentials) IsExpired() bool {
  150. c.m.Lock()
  151. defer c.m.Unlock()
  152. return c.isExpired()
  153. }
  154. // isExpired helper method wrapping the definition of expired credentials.
  155. func (c *Credentials) isExpired() bool {
  156. return c.forceRefresh || c.provider.IsExpired()
  157. }
  158. // Provide a stub-able time.Now for unit tests so expiry can be tested.
  159. var currentTime = time.Now