Skip to content

Commit

Permalink
Handle sensitive values on the plugin side
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Mar 19, 2023
1 parent 25cfb3b commit 60146bd
Show file tree
Hide file tree
Showing 19 changed files with 40 additions and 61 deletions.
5 changes: 5 additions & 0 deletions docs/developer-guide/api_compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ TFLint version: v0.40.0+
- Expand mode is only supported by SDK v0.14.0+ and TFLint v0.42.0+.
- https://github.com/terraform-linters/tflint/pull/1537
- https://github.com/terraform-linters/tflint-plugin-sdk/pull/208
- Client-side value handling is introduced in SDK v0.16.0 and TFLint v0.46.0. TFLint v0.45.0 returns an error instead of a value.
- https://github.com/terraform-linters/tflint/pull/1700
- https://github.com/terraform-linters/tflint/pull/1722
- https://github.com/terraform-linters/tflint-plugin-sdk/pull/235
- https://github.com/terraform-linters/tflint-plugin-sdk/pull/239
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/sourcegraph/go-lsp v0.0.0-20200429204803-219e11d77f5d
github.com/sourcegraph/jsonrpc2 v0.1.0
github.com/spf13/afero v1.9.5
github.com/terraform-linters/tflint-plugin-sdk v0.15.1-0.20230225141907-dd804b3671af
github.com/terraform-linters/tflint-plugin-sdk v0.15.1-0.20230319075009-18f94f9e79ff
github.com/terraform-linters/tflint-ruleset-terraform v0.2.2
github.com/xeipuuv/gojsonschema v1.2.0
github.com/zclconf/go-cty v1.12.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/terraform-linters/tflint-plugin-sdk v0.15.1-0.20230225141907-dd804b3671af h1:TAsqOUKu3DXg6ZmV3igB8ksKkHkaQrdSdZfCE3Ff7nc=
github.com/terraform-linters/tflint-plugin-sdk v0.15.1-0.20230225141907-dd804b3671af/go.mod h1:g5UIXcskejxp38JWqvYqEb/HkvIX6X6luEdS60yimTw=
github.com/terraform-linters/tflint-plugin-sdk v0.15.1-0.20230319075009-18f94f9e79ff h1:ptMeRR1hlGiQmmkzhv250LF3rCo0H8sZf4W+AMeeHUk=
github.com/terraform-linters/tflint-plugin-sdk v0.15.1-0.20230319075009-18f94f9e79ff/go.mod h1:g5UIXcskejxp38JWqvYqEb/HkvIX6X6luEdS60yimTw=
github.com/terraform-linters/tflint-ruleset-terraform v0.2.2 h1:iTE09KkaZ0DE29xvp6IIM1/gmas9V0h8CER28SyBmQ8=
github.com/terraform-linters/tflint-ruleset-terraform v0.2.2/go.mod h1:bCkvH8Vqzr16bWEE3e6Q3hvdZlmSAOR8i6G3M5y+M+k=
github.com/ulikunitz/xz v0.5.10 h1:t92gobL9l3HE202wg3rlk19F6X+JOxl9BBrCCMYEYd8=
Expand Down
10 changes: 5 additions & 5 deletions plugin/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func (s *GRPCServer) EvaluateExpr(expr hcl.Expression, opts sdk.EvaluateExprOpti
return val, diags
}

// SDK v0.16+ introduces client-side handling of unknown/NULL/sensitive values.
if s.clientSDKVersion != nil && s.clientSDKVersion.GreaterThanOrEqual(version.Must(version.NewVersion("0.16.0"))) {
return val, nil
}

if val.ContainsMarked() {
err := fmt.Errorf(
"sensitive value found in %s:%d%w",
Expand All @@ -138,11 +143,6 @@ func (s *GRPCServer) EvaluateExpr(expr hcl.Expression, opts sdk.EvaluateExprOpti
return cty.NullVal(cty.NilType), err
}

// SDK v0.16+ introduces client-side handling of unknown and NULL values.
if s.clientSDKVersion != nil && s.clientSDKVersion.GreaterThanOrEqual(version.Must(version.NewVersion("0.16.0"))) {
return val, nil
}

if *opts.WantType == cty.DynamicPseudoType {
return val, nil
}
Expand Down
17 changes: 14 additions & 3 deletions plugin/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/spf13/afero"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/host2plugin"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
sdk "github.com/terraform-linters/tflint-plugin-sdk/tflint"
"github.com/terraform-linters/tflint/tflint"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -543,18 +544,28 @@ variable "foo" {
Args: func() (hcl.Expression, sdk.EvaluateExprOption) {
return hclExpr(`var.sensitive`), sdk.EvaluateExprOption{WantType: &cty.String, ModuleCtx: sdk.SelfModuleCtxType}
},
Want: cty.NullVal(cty.NilType),
Want: cty.StringVal("foo").Mark(marks.Sensitive),
ErrCheck: neverHappend,
},
{
Name: "sensitive value (SDK v0.15)",
Args: func() (hcl.Expression, sdk.EvaluateExprOption) {
return hclExpr(`var.sensitive`), sdk.EvaluateExprOption{WantType: &cty.String, ModuleCtx: sdk.SelfModuleCtxType}
},
Want: cty.NullVal(cty.NilType),
SDKVersion: sdkv15,
ErrCheck: func(err error) bool {
return err == nil || !errors.Is(err, sdk.ErrSensitive)
},
},
{
Name: "sensitive value in object",
Name: "sensitive value in object (SDK v0.15)",
Args: func() (hcl.Expression, sdk.EvaluateExprOption) {
ty := cty.Object(map[string]cty.Type{"value": cty.String})
return hclExpr(`{ value = var.sensitive }`), sdk.EvaluateExprOption{WantType: &ty, ModuleCtx: sdk.SelfModuleCtxType}
},
Want: cty.NullVal(cty.NilType),
Want: cty.NullVal(cty.NilType),
SDKVersion: sdkv15,
ErrCheck: func(err error) bool {
return err == nil || !errors.Is(err, sdk.ErrSensitive)
},
Expand Down
2 changes: 1 addition & 1 deletion terraform/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"github.com/agext/levenshtein"
"github.com/hashicorp/hcl/v2"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/terraform-linters/tflint/terraform/addrs"
"github.com/terraform-linters/tflint/terraform/lang"
"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
)
Expand Down
2 changes: 1 addition & 1 deletion terraform/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/spf13/afero"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math"
"testing"

"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package funcs
import (
"strconv"

"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/convert"
"github.com/zclconf/go-cty/cty/function"
Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

homedir "github.com/mitchellh/go-homedir"
"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib"
Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package funcs
import (
"fmt"

"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/redact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package funcs
import (
"testing"

"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/sensitive.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package funcs

import (
"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/funcs/sensitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"testing"

"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
2 changes: 1 addition & 1 deletion terraform/lang/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
homedir "github.com/mitchellh/go-homedir"
"github.com/terraform-linters/tflint/terraform/lang/marks"
"github.com/terraform-linters/tflint-plugin-sdk/terraform/lang/marks"
"github.com/zclconf/go-cty/cty"
)

Expand Down
37 changes: 0 additions & 37 deletions terraform/lang/marks/marks.go

This file was deleted.

0 comments on commit 60146bd

Please sign in to comment.