providers/terraform: don't call backend.Configure to validate terraform_remote_state

Validation is supposed to be a local-only operation, but Configure implementations
are allowed to make outgoing requests to remote APIs to validate settings.
This commit is contained in:
Ben Drucker 2020-05-06 17:54:51 -07:00 committed by Martin Atkins
parent de541c4d74
commit 81b8891b90
1 changed files with 14 additions and 15 deletions

View File

@ -48,7 +48,7 @@ func dataSourceRemoteStateValidate(cfg cty.Value) tfdiags.Diagnostics {
// Getting the backend implicitly validates the configuration for it, // Getting the backend implicitly validates the configuration for it,
// but we can only do that if it's all known already. // but we can only do that if it's all known already.
if cfg.GetAttr("config").IsWhollyKnown() && cfg.GetAttr("backend").IsKnown() { if cfg.GetAttr("config").IsWhollyKnown() && cfg.GetAttr("backend").IsKnown() {
_, moreDiags := getBackend(cfg) _, _, moreDiags := getBackend(cfg)
diags = diags.Append(moreDiags) diags = diags.Append(moreDiags)
} else { } else {
// Otherwise we'll just type-check the config object itself. // Otherwise we'll just type-check the config object itself.
@ -81,9 +81,15 @@ func dataSourceRemoteStateValidate(cfg cty.Value) tfdiags.Diagnostics {
func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) { func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
b, moreDiags := getBackend(d) b, cfg, moreDiags := getBackend(d)
diags = diags.Append(moreDiags) diags = diags.Append(moreDiags)
if diags.HasErrors() { if moreDiags.HasErrors() {
return cty.NilVal, diags
}
configureDiags := b.Configure(cfg)
if configureDiags.HasErrors() {
diags = diags.Append(configureDiags.Err())
return cty.NilVal, diags return cty.NilVal, diags
} }
@ -152,7 +158,7 @@ func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) {
return cty.ObjectVal(newState), diags return cty.ObjectVal(newState), diags
} }
func getBackend(cfg cty.Value) (backend.Backend, tfdiags.Diagnostics) { func getBackend(cfg cty.Value) (backend.Backend, cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics var diags tfdiags.Diagnostics
backendType := cfg.GetAttr("backend").AsString() backendType := cfg.GetAttr("backend").AsString()
@ -173,7 +179,7 @@ func getBackend(cfg cty.Value) (backend.Backend, tfdiags.Diagnostics) {
fmt.Sprintf("There is no backend type named %q.", backendType), fmt.Sprintf("There is no backend type named %q.", backendType),
cty.Path(nil).GetAttr("backend"), cty.Path(nil).GetAttr("backend"),
)) ))
return nil, diags return nil, cty.NilVal, diags
} }
b := f() b := f()
@ -199,21 +205,14 @@ func getBackend(cfg cty.Value) (backend.Backend, tfdiags.Diagnostics) {
tfdiags.FormatError(err)), tfdiags.FormatError(err)),
cty.Path(nil).GetAttr("config"), cty.Path(nil).GetAttr("config"),
)) ))
return nil, diags return nil, cty.NilVal, diags
} }
newVal, validateDiags := b.PrepareConfig(configVal) newVal, validateDiags := b.PrepareConfig(configVal)
diags = diags.Append(validateDiags) diags = diags.Append(validateDiags)
if validateDiags.HasErrors() { if validateDiags.HasErrors() {
return nil, diags return nil, cty.NilVal, diags
}
configVal = newVal
configureDiags := b.Configure(configVal)
if configureDiags.HasErrors() {
diags = diags.Append(configureDiags.Err())
return nil, diags
} }
return b, diags return b, newVal, diags
} }