env_minio.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 EnvMinio retrieves credentials from the environment variables of the
  20. // running process. EnvMinioironment credentials never expire.
  21. //
  22. // Environment variables used:
  23. //
  24. // * Access Key ID: MINIO_ACCESS_KEY.
  25. // * Secret Access Key: MINIO_SECRET_KEY.
  26. // * Access Key ID: MINIO_ROOT_USER.
  27. // * Secret Access Key: MINIO_ROOT_PASSWORD.
  28. type EnvMinio struct {
  29. retrieved bool
  30. }
  31. // NewEnvMinio returns a pointer to a new Credentials object
  32. // wrapping the environment variable provider.
  33. func NewEnvMinio() *Credentials {
  34. return New(&EnvMinio{})
  35. }
  36. // Retrieve retrieves the keys from the environment.
  37. func (e *EnvMinio) Retrieve() (Value, error) {
  38. e.retrieved = false
  39. id := os.Getenv("MINIO_ROOT_USER")
  40. secret := os.Getenv("MINIO_ROOT_PASSWORD")
  41. signerType := SignatureV4
  42. if id == "" || secret == "" {
  43. id = os.Getenv("MINIO_ACCESS_KEY")
  44. secret = os.Getenv("MINIO_SECRET_KEY")
  45. if id == "" || secret == "" {
  46. signerType = SignatureAnonymous
  47. }
  48. }
  49. e.retrieved = true
  50. return Value{
  51. AccessKeyID: id,
  52. SecretAccessKey: secret,
  53. SignerType: signerType,
  54. }, nil
  55. }
  56. // IsExpired returns if the credentials have been retrieved.
  57. func (e *EnvMinio) IsExpired() bool {
  58. return !e.retrieved
  59. }