static_provider.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package credentials
  2. import (
  3. "github.com/aws/aws-sdk-go/aws/awserr"
  4. )
  5. // StaticProviderName provides a name of Static provider
  6. const StaticProviderName = "StaticProvider"
  7. var (
  8. // ErrStaticCredentialsEmpty is emitted when static credentials are empty.
  9. ErrStaticCredentialsEmpty = awserr.New("EmptyStaticCreds", "static credentials are empty", nil)
  10. )
  11. // A StaticProvider is a set of credentials which are set programmatically,
  12. // and will never expire.
  13. type StaticProvider struct {
  14. Value
  15. }
  16. // NewStaticCredentials returns a pointer to a new Credentials object
  17. // wrapping a static credentials value provider.
  18. func NewStaticCredentials(id, secret, token string) *Credentials {
  19. return NewCredentials(&StaticProvider{Value: Value{
  20. AccessKeyID: id,
  21. SecretAccessKey: secret,
  22. SessionToken: token,
  23. }})
  24. }
  25. // NewStaticCredentialsFromCreds returns a pointer to a new Credentials object
  26. // wrapping the static credentials value provide. Same as NewStaticCredentials
  27. // but takes the creds Value instead of individual fields
  28. func NewStaticCredentialsFromCreds(creds Value) *Credentials {
  29. return NewCredentials(&StaticProvider{Value: creds})
  30. }
  31. // Retrieve returns the credentials or error if the credentials are invalid.
  32. func (s *StaticProvider) Retrieve() (Value, error) {
  33. if s.AccessKeyID == "" || s.SecretAccessKey == "" {
  34. return Value{ProviderName: StaticProviderName}, ErrStaticCredentialsEmpty
  35. }
  36. if len(s.Value.ProviderName) == 0 {
  37. s.Value.ProviderName = StaticProviderName
  38. }
  39. return s.Value, nil
  40. }
  41. // IsExpired returns if the credentials are expired.
  42. //
  43. // For StaticProvider, the credentials never expired.
  44. func (s *StaticProvider) IsExpired() bool {
  45. return false
  46. }