Skip to content

Commit 9463ea7

Browse files
authored
GCP auth logic (#333)
* purposal commit * helper func for loading gcp creds
1 parent 66bc9cd commit 9463ea7

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

iterative/gcp/provider.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strings"
1313
"time"
1414

15+
"terraform-provider-iterative/iterative/utils"
16+
1517
"golang.org/x/oauth2"
1618
"golang.org/x/oauth2/google"
1719
gcp_compute "google.golang.org/api/compute/v1"
@@ -289,7 +291,7 @@ func getProjectService() (string, *gcp_compute.Service, error) {
289291
var credentials *google.Credentials
290292
var err error
291293

292-
if credentialsData := []byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_DATA")); len(credentialsData) > 0 {
294+
if credentialsData := []byte(utils.LoadGCPCredentials()); len(credentialsData) > 0 {
293295
credentials, err = google.CredentialsFromJSON(oauth2.NoContext, credentialsData, gcp_compute.ComputeScope)
294296
} else {
295297
credentials, err = google.FindDefaultCredentials(oauth2.NoContext, gcp_compute.ComputeScope)

iterative/resource_runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func provisionerCode(d *schema.ResourceData) (string, error) {
431431
data["AZURE_CLIENT_SECRET"] = os.Getenv("AZURE_CLIENT_SECRET")
432432
data["AZURE_SUBSCRIPTION_ID"] = os.Getenv("AZURE_SUBSCRIPTION_ID")
433433
data["AZURE_TENANT_ID"] = os.Getenv("AZURE_TENANT_ID")
434-
data["GOOGLE_APPLICATION_CREDENTIALS_DATA"] = os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_DATA")
434+
data["GOOGLE_APPLICATION_CREDENTIALS_DATA"] = utils.LoadGCPCredentials()
435435
data["KUBERNETES_CONFIGURATION"] = os.Getenv("KUBERNETES_CONFIGURATION")
436436
data["container"] = isContainerAvailable(d.Get("cloud").(string))
437437
data["setup"] = strings.Replace(string(setup[:]), "#/bin/sh", "", 1)

iterative/utils/helpers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package utils
22

33
import (
4+
"os"
5+
46
"github.com/aohorodnyk/uid"
57
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
68
)
@@ -31,3 +33,15 @@ func StripAvailabilityZone(region string) string {
3133
}
3234
return region
3335
}
36+
37+
func LoadGCPCredentials() string {
38+
credentialsData := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_DATA")
39+
if len(credentialsData) == 0 {
40+
credentialsPath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
41+
if len(credentialsPath) > 0 {
42+
jsonData, _ := os.ReadFile(credentialsPath)
43+
credentialsData = string(jsonData)
44+
}
45+
}
46+
return credentialsData
47+
}

0 commit comments

Comments
 (0)