Merge pull request #27335 from hashicorp/alisdair/fix-sensitive-data-source-arguments

core: Fix for sensitive data source arguments
This commit is contained in:
Alisdair McDiarmid 2021-01-04 13:53:35 -05:00 committed by GitHub
commit f770f03620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 63 additions and 0 deletions

View File

@ -12363,3 +12363,50 @@ resource "test_instance" "a" {
} }
} }
} }
func TestContext2Apply_dataSensitive(t *testing.T) {
m := testModule(t, "apply-data-sensitive")
p := testProvider("null")
p.ApplyResourceChangeFn = testApplyFn
p.PlanResourceChangeFn = testDiffFn
p.ReadDataSourceFn = func(req providers.ReadDataSourceRequest) providers.ReadDataSourceResponse {
// add the required id
m := req.Config.AsValueMap()
m["id"] = cty.StringVal("foo")
return providers.ReadDataSourceResponse{
State: cty.ObjectVal(m),
}
}
ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("null"): testProviderFuncFixed(p),
},
})
if p, diags := ctx.Plan(); diags.HasErrors() {
t.Fatalf("diags: %s", diags.Err())
} else {
t.Logf(legacyDiffComparisonString(p.Changes))
}
state, diags := ctx.Apply()
assertNoErrors(t, diags)
addr := mustResourceInstanceAddr("data.null_data_source.testing")
dataSourceState := state.ResourceInstance(addr)
pvms := dataSourceState.Current.AttrSensitivePaths
if len(pvms) != 1 {
t.Fatalf("expected 1 sensitive path, got %d", len(pvms))
}
pvm := pvms[0]
if gotPath, wantPath := pvm.Path, cty.GetAttrPath("foo"); !gotPath.Equals(wantPath) {
t.Errorf("wrong path\n got: %#v\nwant: %#v", gotPath, wantPath)
}
if gotMarks, wantMarks := pvm.Marks, cty.NewValueMarks("sensitive"); !gotMarks.Equal(wantMarks) {
t.Errorf("wrong marks\n got: %#v\nwant: %#v", gotMarks, wantMarks)
}
}

View File

@ -1194,6 +1194,10 @@ func (n *NodeAbstractResourceInstance) readDataSource(ctx EvalContext, configVal
return newVal, diags return newVal, diags
} }
// Unmark before sending to provider, will re-mark before returning
var pvm []cty.PathValueMarks
configVal, pvm = configVal.UnmarkDeepWithPaths()
log.Printf("[TRACE] readDataSource: Re-validating config for %s", n.Addr) log.Printf("[TRACE] readDataSource: Re-validating config for %s", n.Addr)
validateResp := provider.ValidateDataSourceConfig( validateResp := provider.ValidateDataSourceConfig(
providers.ValidateDataSourceConfigRequest{ providers.ValidateDataSourceConfigRequest{
@ -1269,6 +1273,10 @@ func (n *NodeAbstractResourceInstance) readDataSource(ctx EvalContext, configVal
newVal = cty.UnknownAsNull(newVal) newVal = cty.UnknownAsNull(newVal)
} }
if len(pvm) > 0 {
newVal = newVal.MarkWithPaths(pvm)
}
return newVal, diags return newVal, diags
} }

View File

@ -0,0 +1,8 @@
variable "foo" {
sensitive = true
default = "foo"
}
data "null_data_source" "testing" {
foo = var.foo
}