env_aws.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2017 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package credentials
  18. import "os"
  19. // A EnvAWS retrieves credentials from the environment variables of the
  20. // running process. EnvAWSironment credentials never expire.
  21. //
  22. // EnvAWSironment variables used:
  23. //
  24. // * Access Key ID: AWS_ACCESS_KEY_ID or AWS_ACCESS_KEY.
  25. // * Secret Access Key: AWS_SECRET_ACCESS_KEY or AWS_SECRET_KEY.
  26. // * Secret Token: AWS_SESSION_TOKEN.
  27. type EnvAWS struct {
  28. retrieved bool
  29. }
  30. // NewEnvAWS returns a pointer to a new Credentials object
  31. // wrapping the environment variable provider.
  32. func NewEnvAWS() *Credentials {
  33. return New(&EnvAWS{})
  34. }
  35. // Retrieve retrieves the keys from the environment.
  36. func (e *EnvAWS) Retrieve() (Value, error) {
  37. e.retrieved = false
  38. id := os.Getenv("AWS_ACCESS_KEY_ID")
  39. if id == "" {
  40. id = os.Getenv("AWS_ACCESS_KEY")
  41. }
  42. secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
  43. if secret == "" {
  44. secret = os.Getenv("AWS_SECRET_KEY")
  45. }
  46. signerType := SignatureV4
  47. if id == "" || secret == "" {
  48. signerType = SignatureAnonymous
  49. }
  50. e.retrieved = true
  51. return Value{
  52. AccessKeyID: id,
  53. SecretAccessKey: secret,
  54. SessionToken: os.Getenv("AWS_SESSION_TOKEN"),
  55. SignerType: signerType,
  56. }, nil
  57. }
  58. // IsExpired returns if the credentials have been retrieved.
  59. func (e *EnvAWS) IsExpired() bool {
  60. return !e.retrieved
  61. }