credsfile.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package credsfile is meant to hide implementation details from the pubic
  15. // surface of the detect package. It should not import any other packages in
  16. // this module. It is located under the main internal package so other
  17. // sub-packages can use these parsed types as well.
  18. package credsfile
  19. import (
  20. "os"
  21. "os/user"
  22. "path/filepath"
  23. "runtime"
  24. )
  25. const (
  26. // GoogleAppCredsEnvVar is the environment variable for setting the
  27. // application default credentials.
  28. GoogleAppCredsEnvVar = "GOOGLE_APPLICATION_CREDENTIALS"
  29. userCredsFilename = "application_default_credentials.json"
  30. )
  31. // CredentialType represents different credential filetypes Google credentials
  32. // can be.
  33. type CredentialType int
  34. const (
  35. // UnknownCredType is an unidentified file type.
  36. UnknownCredType CredentialType = iota
  37. // UserCredentialsKey represents a user creds file type.
  38. UserCredentialsKey
  39. // ServiceAccountKey represents a service account file type.
  40. ServiceAccountKey
  41. // ImpersonatedServiceAccountKey represents a impersonated service account
  42. // file type.
  43. ImpersonatedServiceAccountKey
  44. // ExternalAccountKey represents a external account file type.
  45. ExternalAccountKey
  46. // GDCHServiceAccountKey represents a GDCH file type.
  47. GDCHServiceAccountKey
  48. // ExternalAccountAuthorizedUserKey represents a external account authorized
  49. // user file type.
  50. ExternalAccountAuthorizedUserKey
  51. )
  52. // parseCredentialType returns the associated filetype based on the parsed
  53. // typeString provided.
  54. func parseCredentialType(typeString string) CredentialType {
  55. switch typeString {
  56. case "service_account":
  57. return ServiceAccountKey
  58. case "authorized_user":
  59. return UserCredentialsKey
  60. case "impersonated_service_account":
  61. return ImpersonatedServiceAccountKey
  62. case "external_account":
  63. return ExternalAccountKey
  64. case "external_account_authorized_user":
  65. return ExternalAccountAuthorizedUserKey
  66. case "gdch_service_account":
  67. return GDCHServiceAccountKey
  68. default:
  69. return UnknownCredType
  70. }
  71. }
  72. // GetFileNameFromEnv returns the override if provided or detects a filename
  73. // from the environment.
  74. func GetFileNameFromEnv(override string) string {
  75. if override != "" {
  76. return override
  77. }
  78. return os.Getenv(GoogleAppCredsEnvVar)
  79. }
  80. // GetWellKnownFileName tries to locate the filepath for the user credential
  81. // file based on the environment.
  82. func GetWellKnownFileName() string {
  83. if runtime.GOOS == "windows" {
  84. return filepath.Join(os.Getenv("APPDATA"), "gcloud", userCredsFilename)
  85. }
  86. return filepath.Join(guessUnixHomeDir(), ".config", "gcloud", userCredsFilename)
  87. }
  88. // guessUnixHomeDir default to checking for HOME, but not all unix systems have
  89. // this set, do have a fallback.
  90. func guessUnixHomeDir() string {
  91. if v := os.Getenv("HOME"); v != "" {
  92. return v
  93. }
  94. if u, err := user.Current(); err == nil {
  95. return u.HomeDir
  96. }
  97. return ""
  98. }