env_provider.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package credentials
  2. import (
  3. "os"
  4. "github.com/ks3sdklib/aws-sdk-go/internal/apierr"
  5. )
  6. var (
  7. // ErrAccessKeyIDNotFound is returned when the AWS Access Key ID can't be
  8. // found in the process's environment.
  9. ErrAccessKeyIDNotFound = apierr.New("EnvAccessKeyNotFound", "AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY not found in environment", nil)
  10. // ErrSecretAccessKeyNotFound is returned when the AWS Secret Access Key
  11. // can't be found in the process's environment.
  12. ErrSecretAccessKeyNotFound = apierr.New("EnvSecretNotFound", "AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY not found in environment", nil)
  13. )
  14. // A EnvProvider retrieves credentials from the environment variables of the
  15. // running process. Environment credentials never expire.
  16. //
  17. // Environment variables used:
  18. // - Access Key ID: AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY
  19. // - Secret Access Key: AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY
  20. type EnvProvider struct {
  21. retrieved bool
  22. }
  23. // NewEnvCredentials returns a pointer to a new Credentials object
  24. // wrapping the environment variable provider.
  25. func NewEnvCredentials() *Credentials {
  26. return NewCredentials(&EnvProvider{})
  27. }
  28. // Retrieve retrieves the keys from the environment.
  29. func (e *EnvProvider) Retrieve() (Value, error) {
  30. e.retrieved = false
  31. id := os.Getenv("AWS_ACCESS_KEY_ID")
  32. if id == "" {
  33. id = os.Getenv("AWS_ACCESS_KEY")
  34. }
  35. secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
  36. if secret == "" {
  37. secret = os.Getenv("AWS_SECRET_KEY")
  38. }
  39. if id == "" {
  40. return Value{}, ErrAccessKeyIDNotFound
  41. }
  42. if secret == "" {
  43. return Value{}, ErrSecretAccessKeyNotFound
  44. }
  45. e.retrieved = true
  46. return Value{
  47. AccessKeyID: id,
  48. SecretAccessKey: secret,
  49. SessionToken: os.Getenv("AWS_SESSION_TOKEN"),
  50. }, nil
  51. }
  52. // IsExpired returns if the credentials have been retrieved.
  53. func (e *EnvProvider) IsExpired() bool {
  54. return !e.retrieved
  55. }