Skip to content

Commit

Permalink
core: Unmark provisioner config before validation
Browse files Browse the repository at this point in the history
Sensitive values in provisioner configuration would cause errors in the
validate phase. We need to unmark these value before serializing the
config value for the provisioner plugin.
  • Loading branch information
alisdair committed Feb 18, 2021
1 parent 74134a8 commit 6fb010f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
31 changes: 31 additions & 0 deletions terraform/context_validate_test.go
Expand Up @@ -1856,3 +1856,34 @@ output "out" {
}
}
}

func TestContext2Validate_sensitiveProvisionerConfig(t *testing.T) {
m := testModule(t, "validate-sensitive-provisioner-config")
p := testProvider("aws")
pr := simpleMockProvisioner()

c := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("aws"): testProviderFuncFixed(p),
},
Provisioners: map[string]provisioners.Factory{
"test": testProvisionerFuncFixed(pr),
},
})

pr.ValidateProvisionerConfigFn = func(r provisioners.ValidateProvisionerConfigRequest) provisioners.ValidateProvisionerConfigResponse {
if r.Config.ContainsMarked() {
t.Errorf("provisioner config contains marked values")
}
return pr.ValidateProvisionerConfigResponse
}

diags := c.Validate()
if diags.HasErrors() {
t.Fatalf("unexpected error: %s", diags.Err())
}
if !pr.ValidateProvisionerConfigCalled {
t.Fatal("ValidateProvisionerConfig not called")
}
}
4 changes: 3 additions & 1 deletion terraform/eval_validate.go
Expand Up @@ -93,8 +93,10 @@ func (n *EvalValidateProvisioner) Validate(ctx EvalContext) error {
return fmt.Errorf("EvaluateBlock returned nil value")
}

// Use unmarked value for validate request
unmarkedConfigVal, _ := configVal.UnmarkDeep()
req := provisioners.ValidateProvisionerConfigRequest{
Config: configVal,
Config: unmarkedConfigVal,
}

resp := provisioner.ValidateProvisionerConfig(req)
Expand Down
11 changes: 11 additions & 0 deletions terraform/testdata/validate-sensitive-provisioner-config/main.tf
@@ -0,0 +1,11 @@
variable "secret" {
type = string
default = " password123"
sensitive = true
}

resource "aws_instance" "foo" {
provisioner "test" {
test_string = var.secret
}
}

0 comments on commit 6fb010f

Please sign in to comment.