file_aws_credentials.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 (
  19. "os"
  20. "path/filepath"
  21. "github.com/go-ini/ini"
  22. homedir "github.com/mitchellh/go-homedir"
  23. )
  24. // A FileAWSCredentials retrieves credentials from the current user's home
  25. // directory, and keeps track if those credentials are expired.
  26. //
  27. // Profile ini file example: $HOME/.aws/credentials
  28. type FileAWSCredentials struct {
  29. // Path to the shared credentials file.
  30. //
  31. // If empty will look for "AWS_SHARED_CREDENTIALS_FILE" env variable. If the
  32. // env value is empty will default to current user's home directory.
  33. // Linux/OSX: "$HOME/.aws/credentials"
  34. // Windows: "%USERPROFILE%\.aws\credentials"
  35. filename string
  36. // AWS Profile to extract credentials from the shared credentials file. If empty
  37. // will default to environment variable "AWS_PROFILE" or "default" if
  38. // environment variable is also not set.
  39. profile string
  40. // retrieved states if the credentials have been successfully retrieved.
  41. retrieved bool
  42. }
  43. // NewFileAWSCredentials returns a pointer to a new Credentials object
  44. // wrapping the Profile file provider.
  45. func NewFileAWSCredentials(filename string, profile string) *Credentials {
  46. return New(&FileAWSCredentials{
  47. filename: filename,
  48. profile: profile,
  49. })
  50. }
  51. // Retrieve reads and extracts the shared credentials from the current
  52. // users home directory.
  53. func (p *FileAWSCredentials) Retrieve() (Value, error) {
  54. if p.filename == "" {
  55. p.filename = os.Getenv("AWS_SHARED_CREDENTIALS_FILE")
  56. if p.filename == "" {
  57. homeDir, err := homedir.Dir()
  58. if err != nil {
  59. return Value{}, err
  60. }
  61. p.filename = filepath.Join(homeDir, ".aws", "credentials")
  62. }
  63. }
  64. if p.profile == "" {
  65. p.profile = os.Getenv("AWS_PROFILE")
  66. if p.profile == "" {
  67. p.profile = "default"
  68. }
  69. }
  70. p.retrieved = false
  71. iniProfile, err := loadProfile(p.filename, p.profile)
  72. if err != nil {
  73. return Value{}, err
  74. }
  75. // Default to empty string if not found.
  76. id := iniProfile.Key("aws_access_key_id")
  77. // Default to empty string if not found.
  78. secret := iniProfile.Key("aws_secret_access_key")
  79. // Default to empty string if not found.
  80. token := iniProfile.Key("aws_session_token")
  81. p.retrieved = true
  82. return Value{
  83. AccessKeyID: id.String(),
  84. SecretAccessKey: secret.String(),
  85. SessionToken: token.String(),
  86. SignerType: SignatureV4,
  87. }, nil
  88. }
  89. // IsExpired returns if the shared credentials have expired.
  90. func (p *FileAWSCredentials) IsExpired() bool {
  91. return !p.retrieved
  92. }
  93. // loadProfiles loads from the file pointed to by shared credentials filename for profile.
  94. // The credentials retrieved from the profile will be returned or error. Error will be
  95. // returned if it fails to read from the file, or the data is invalid.
  96. func loadProfile(filename, profile string) (*ini.Section, error) {
  97. config, err := ini.Load(filename)
  98. if err != nil {
  99. return nil, err
  100. }
  101. iniProfile, err := config.GetSection(profile)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return iniProfile, nil
  106. }