util.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2022 Google LLC.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // https://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // Package util provides helper functions for the client.
  14. package util
  15. import (
  16. "encoding/json"
  17. "errors"
  18. "io"
  19. "os"
  20. "os/user"
  21. "path/filepath"
  22. "runtime"
  23. )
  24. const configFileName = "certificate_config.json"
  25. // EnterpriseCertificateConfig contains parameters for initializing signer.
  26. type EnterpriseCertificateConfig struct {
  27. Libs Libs `json:"libs"`
  28. }
  29. // Libs specifies the locations of helper libraries.
  30. type Libs struct {
  31. ECP string `json:"ecp"`
  32. }
  33. // ErrConfigUnavailable is a sentinel error that indicates ECP config is unavailable,
  34. // possibly due to entire config missing or missing binary path.
  35. var ErrConfigUnavailable = errors.New("Config is unavailable")
  36. // LoadSignerBinaryPath retrieves the path of the signer binary from the config file.
  37. func LoadSignerBinaryPath(configFilePath string) (path string, err error) {
  38. jsonFile, err := os.Open(configFilePath)
  39. if err != nil {
  40. if errors.Is(err, os.ErrNotExist) {
  41. return "", ErrConfigUnavailable
  42. }
  43. return "", err
  44. }
  45. byteValue, err := io.ReadAll(jsonFile)
  46. if err != nil {
  47. return "", err
  48. }
  49. var config EnterpriseCertificateConfig
  50. err = json.Unmarshal(byteValue, &config)
  51. if err != nil {
  52. return "", err
  53. }
  54. signerBinaryPath := config.Libs.ECP
  55. if signerBinaryPath == "" {
  56. return "", ErrConfigUnavailable
  57. }
  58. return signerBinaryPath, nil
  59. }
  60. func guessHomeDir() string {
  61. // Prefer $HOME over user.Current due to glibc bug: golang.org/issue/13470
  62. if v := os.Getenv("HOME"); v != "" {
  63. return v
  64. }
  65. // Else, fall back to user.Current:
  66. if u, err := user.Current(); err == nil {
  67. return u.HomeDir
  68. }
  69. return ""
  70. }
  71. func getDefaultConfigFileDirectory() (directory string) {
  72. if runtime.GOOS == "windows" {
  73. return filepath.Join(os.Getenv("APPDATA"), "gcloud")
  74. }
  75. return filepath.Join(guessHomeDir(), ".config/gcloud")
  76. }
  77. // GetDefaultConfigFilePath returns the default path of the enterprise certificate config file created by gCloud.
  78. func GetDefaultConfigFilePath() (path string) {
  79. return filepath.Join(getDefaultConfigFileDirectory(), configFileName)
  80. }