...
1 package iam
2
3 import (
4 "fmt"
5 "os"
6 "path/filepath"
7
8 "edge-infra.dev/pkg/lib/errors"
9
10 "github.com/mitchellh/go-homedir"
11 )
12
13 const (
14
15
16 GoogleAppCredsEnvVar = "GOOGLE_APPLICATION_CREDENTIALS"
17 )
18
19
20
21
22
23
24 func ResolveGoogleAppCreds() (string, error) {
25 if os.Getenv(GoogleAppCredsEnvVar) != "" {
26 return os.Getenv(GoogleAppCredsEnvVar), nil
27 }
28
29 return DefaultGoogleAppCredsPath()
30 }
31
32 func DefaultGoogleAppCredsPath() (string, error) {
33
34 home, err := homedir.Dir()
35 if err != nil {
36 return "", errors.Wrap(err)
37 }
38
39 home, err = homedir.Expand(home)
40 if err != nil {
41 return "", errors.Wrap(err)
42 }
43
44 p := filepath.Join(home, ".config/gcloud/application_default_credentials.json")
45
46 if _, err := os.Stat(p); err != nil {
47 return "", errors.New(
48 fmt.Sprintf("couldnt find google default application credentials at %s", p),
49 err,
50 )
51 }
52
53 return p, nil
54 }
55
56
57
58 type CredentialsFile struct {
59 Type string `json:"type"`
60
61
62 ClientEmail string `json:"client_email,omitempty"`
63 PrivateKeyID string `json:"private_key_id,omitempty"`
64 PrivateKey string `json:"private_key,omitempty"`
65 TokenURL string `json:"token_uri,omitempty"`
66 ProjectID string `json:"project_id,omitempty"`
67
68
69
70 ClientSecret string `json:"client_secret,omitempty"`
71 ClientID string `json:"client_id,omitempty"`
72 RefreshToken string `json:"refresh_token,omitempty"`
73 QuotaProjectID string `json:"quota_project_id,omitempty"`
74 }
75
View as plain text