Skip to content

Commit c938de6

Browse files
bzubldez
authored andcommitted
Use GCE_PROJECT for project always, if specified (#750)
1 parent 43401f2 commit c938de6

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

providers/dns/gcloud/googlecloud.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ type DNSProvider struct {
5555
// Project name must be passed in the environment variable: GCE_PROJECT.
5656
// A Service Account file can be passed in the environment variable: GCE_SERVICE_ACCOUNT_FILE
5757
func NewDNSProvider() (*DNSProvider, error) {
58+
// Use a service account file if specified via environment variable.
5859
if saFile, ok := os.LookupEnv("GCE_SERVICE_ACCOUNT_FILE"); ok {
5960
return NewDNSProviderServiceAccount(saFile)
6061
}
6162

63+
// Use default credentials.
6264
project := os.Getenv("GCE_PROJECT")
6365
return NewDNSProviderCredentials(project)
6466
}
@@ -94,15 +96,20 @@ func NewDNSProviderServiceAccount(saFile string) (*DNSProvider, error) {
9496
return nil, fmt.Errorf("googlecloud: unable to read Service Account file: %v", err)
9597
}
9698

97-
// read project id from service account file
98-
var datJSON struct {
99-
ProjectID string `json:"project_id"`
100-
}
101-
err = json.Unmarshal(dat, &datJSON)
102-
if err != nil || datJSON.ProjectID == "" {
103-
return nil, fmt.Errorf("googlecloud: project ID not found in Google Cloud Service Account file")
99+
// If GCE_PROJECT is non-empty it overrides the project in the service
100+
// account file.
101+
project := os.Getenv("GCE_PROJECT")
102+
if project != "" {
103+
// read project id from service account file
104+
var datJSON struct {
105+
ProjectID string `json:"project_id"`
106+
}
107+
err = json.Unmarshal(dat, &datJSON)
108+
if err != nil || datJSON.ProjectID == "" {
109+
return nil, fmt.Errorf("googlecloud: project ID not found in Google Cloud Service Account file")
110+
}
111+
project = datJSON.ProjectID
104112
}
105-
project := datJSON.ProjectID
106113

107114
conf, err := google.JWTConfigFromJSON(dat, dns.NdevClouddnsReadwriteScope)
108115
if err != nil {

0 commit comments

Comments
 (0)