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,
// but we can only do that if it's all known already.
if cfg.GetAttr("config").IsWhollyKnown() && cfg.GetAttr("backend").IsKnown() {
_, moreDiags := getBackend(cfg)
_, _, moreDiags := getBackend(cfg)
diags = diags.Append(moreDiags)
} else {
// 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) {
var diags tfdiags.Diagnostics
b, moreDiags := getBackend(d)
b, cfg, moreDiags := getBackend(d)
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
}
@ -152,7 +158,7 @@ func dataSourceRemoteStateRead(d cty.Value) (cty.Value, tfdiags.Diagnostics) {
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
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),
cty.Path(nil).GetAttr("backend"),
))
return nil, diags
return nil, cty.NilVal, diags
}
b := f()
@ -199,21 +205,14 @@ func getBackend(cfg cty.Value) (backend.Backend, tfdiags.Diagnostics) {
tfdiags.FormatError(err)),
cty.Path(nil).GetAttr("config"),
))
return nil, diags
return nil, cty.NilVal, diags
}
newVal, validateDiags := b.PrepareConfig(configVal)
diags = diags.Append(validateDiags)
if validateDiags.HasErrors() {
return nil, diags
}
configVal = newVal
configureDiags := b.Configure(configVal)
if configureDiags.HasErrors() {
diags = diags.Append(configureDiags.Err())
return nil, diags
return nil, cty.NilVal, diags
}
return b, diags
return b, newVal, diags
}