Skip to content

Commit f18607b

Browse files
committed
Fix our provider validation to allow file paths.
Previously, provider credentials were _supposed_ to be able to be specified as the file contents or the path to the file. We even had a test for the code for this! Then we updated the validation for the provider, and forgot to validate filepaths as ok. So provider validation failed. And because our test only tested the config validation, and not the provider validation, our tests thought this was just fine still. This fixes that oversight, accepting filepaths as valid. It also adds tests to ensure that provider validation allows both file paths and contents.
1 parent 4da0e98 commit f18607b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

google/provider.go

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package google
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67

78
"github.com/hashicorp/terraform/helper/mutexkv"
89
"github.com/hashicorp/terraform/helper/schema"
@@ -198,6 +199,10 @@ func validateCredentials(v interface{}, k string) (warnings []string, errors []e
198199
return
199200
}
200201
creds := v.(string)
202+
// if this is a path and we can stat it, assume it's ok
203+
if _, err := os.Stat(creds); err == nil {
204+
return
205+
}
201206
var account accountFile
202207
if err := json.Unmarshal([]byte(creds), &account); err != nil {
203208
errors = append(errors,

google/provider_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ func TestProvider_getRegionFromZone(t *testing.T) {
8888
}
8989
}
9090

91+
func TestProvider_loadCredentialsFromFile(t *testing.T) {
92+
ws, es := validateCredentials(testFakeCredentialsPath, "")
93+
if len(ws) != 0 {
94+
t.Errorf("Expected %d warnings, got %v", len(ws), ws)
95+
}
96+
if len(es) != 0 {
97+
t.Errorf("Expected %d errors, got %v", len(es), es)
98+
}
99+
}
100+
101+
func TestProvider_loadCredentialsFromJSON(t *testing.T) {
102+
contents, err := ioutil.ReadFile(testFakeCredentialsPath)
103+
if err != nil {
104+
t.Fatalf("Unexpected error: %s", err)
105+
}
106+
ws, es := validateCredentials(string(contents), "")
107+
if len(ws) != 0 {
108+
t.Errorf("Expected %d warnings, got %v", len(ws), ws)
109+
}
110+
if len(es) != 0 {
111+
t.Errorf("Expected %d errors, got %v", len(es), es)
112+
}
113+
}
114+
91115
// getTestRegion has the same logic as the provider's getRegion, to be used in tests.
92116
func getTestRegion(is *terraform.InstanceState, config *Config) (string, error) {
93117
if res, ok := is.Attributes["region"]; ok {

0 commit comments

Comments
 (0)