file_minio_client.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "encoding/json"
  20. "io/ioutil"
  21. "os"
  22. "path/filepath"
  23. "runtime"
  24. homedir "github.com/mitchellh/go-homedir"
  25. )
  26. // A FileMinioClient retrieves credentials from the current user's home
  27. // directory, and keeps track if those credentials are expired.
  28. //
  29. // Configuration file example: $HOME/.mc/config.json
  30. type FileMinioClient struct {
  31. // Path to the shared credentials file.
  32. //
  33. // If empty will look for "MINIO_SHARED_CREDENTIALS_FILE" env variable. If the
  34. // env value is empty will default to current user's home directory.
  35. // Linux/OSX: "$HOME/.mc/config.json"
  36. // Windows: "%USERALIAS%\mc\config.json"
  37. filename string
  38. // Minio Alias to extract credentials from the shared credentials file. If empty
  39. // will default to environment variable "MINIO_ALIAS" or "default" if
  40. // environment variable is also not set.
  41. alias string
  42. // retrieved states if the credentials have been successfully retrieved.
  43. retrieved bool
  44. }
  45. // NewFileMinioClient returns a pointer to a new Credentials object
  46. // wrapping the Alias file provider.
  47. func NewFileMinioClient(filename string, alias string) *Credentials {
  48. return New(&FileMinioClient{
  49. filename: filename,
  50. alias: alias,
  51. })
  52. }
  53. // Retrieve reads and extracts the shared credentials from the current
  54. // users home directory.
  55. func (p *FileMinioClient) Retrieve() (Value, error) {
  56. if p.filename == "" {
  57. homeDir, err := homedir.Dir()
  58. if err != nil {
  59. return Value{}, err
  60. }
  61. p.filename = filepath.Join(homeDir, ".mc", "config.json")
  62. if runtime.GOOS == "windows" {
  63. p.filename = filepath.Join(homeDir, "mc", "config.json")
  64. }
  65. }
  66. if p.alias == "" {
  67. p.alias = os.Getenv("MINIO_ALIAS")
  68. if p.alias == "" {
  69. p.alias = "s3"
  70. }
  71. }
  72. p.retrieved = false
  73. hostCfg, err := loadAlias(p.filename, p.alias)
  74. if err != nil {
  75. return Value{}, err
  76. }
  77. p.retrieved = true
  78. return Value{
  79. AccessKeyID: hostCfg.AccessKey,
  80. SecretAccessKey: hostCfg.SecretKey,
  81. SignerType: parseSignatureType(hostCfg.API),
  82. }, nil
  83. }
  84. // IsExpired returns if the shared credentials have expired.
  85. func (p *FileMinioClient) IsExpired() bool {
  86. return !p.retrieved
  87. }
  88. // hostConfig configuration of a host.
  89. type hostConfig struct {
  90. URL string `json:"url"`
  91. AccessKey string `json:"accessKey"`
  92. SecretKey string `json:"secretKey"`
  93. API string `json:"api"`
  94. }
  95. // config config version.
  96. type config struct {
  97. Version string `json:"version"`
  98. Hosts map[string]hostConfig `json:"hosts"`
  99. }
  100. // loadAliass loads from the file pointed to by shared credentials filename for alias.
  101. // The credentials retrieved from the alias will be returned or error. Error will be
  102. // returned if it fails to read from the file.
  103. func loadAlias(filename, alias string) (hostConfig, error) {
  104. cfg := &config{}
  105. configBytes, err := ioutil.ReadFile(filename)
  106. if err != nil {
  107. return hostConfig{}, err
  108. }
  109. if err = json.Unmarshal(configBytes, cfg); err != nil {
  110. return hostConfig{}, err
  111. }
  112. return cfg.Hosts[alias], nil
  113. }