Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin2host: Handle eval errors on the client side #235

Merged
merged 2 commits into from
Feb 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Handle eval errors on the client side
wata727 committed Feb 25, 2023

Verified

This commit was signed with the committer’s verified signature.
kulmann Benedikt Kulmann
commit e54e3249aff589fe6d922e8252cb54d803690334
4 changes: 2 additions & 2 deletions plugin/fromproto/fromproto.go
Original file line number Diff line number Diff line change
@@ -307,9 +307,9 @@ func Error(err error) error {
case *proto.ErrorDetail:
switch t.Code {
case proto.ErrorCode_ERROR_CODE_UNKNOWN_VALUE:
return fmt.Errorf("%s%w", st.Message(), tflint.ErrUnknownValue)
return tflint.ErrUnknownValue
case proto.ErrorCode_ERROR_CODE_NULL_VALUE:
return fmt.Errorf("%s%w", st.Message(), tflint.ErrNullValue)
return tflint.ErrNullValue
case proto.ErrorCode_ERROR_CODE_UNEVALUABLE:
return fmt.Errorf("%s%w", st.Message(), tflint.ErrUnevaluable)
case proto.ErrorCode_ERROR_CODE_SENSITIVE:
22 changes: 22 additions & 0 deletions plugin/plugin2host/client.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import (
"github.com/hashicorp/hcl/v2/hclsyntax"
hcljson "github.com/hashicorp/hcl/v2/json"
"github.com/terraform-linters/tflint-plugin-sdk/hclext"
"github.com/terraform-linters/tflint-plugin-sdk/logger"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/fromproto"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/proto"
"github.com/terraform-linters/tflint-plugin-sdk/plugin/toproto"
@@ -331,6 +332,27 @@ func (c *GRPCClient) EvaluateExpr(expr hcl.Expression, ret interface{}, opts *tf
return err
}

if ty == cty.DynamicPseudoType {
return gocty.FromCtyValue(val, ret)
}

// Returns an error if the value cannot be decoded to a Go value (e.g. unknown value, null).
// This allows the caller to handle the value by the errors package.
err = cty.Walk(val, func(path cty.Path, v cty.Value) (bool, error) {
if !v.IsKnown() {
logger.Debug(fmt.Sprintf("unknown value found in %s", expr.Range()))
return false, tflint.ErrUnknownValue
}
if v.IsNull() {
logger.Debug(fmt.Sprintf("null value found in %s", expr.Range()))
return false, tflint.ErrNullValue
}
return true, nil
})
if err != nil {
return err
}

return gocty.FromCtyValue(val, ret)
}

110 changes: 110 additions & 0 deletions plugin/plugin2host/plugin2host_test.go
Original file line number Diff line number Diff line change
@@ -1745,6 +1745,116 @@ func TestEvaluateExpr(t *testing.T) {
return !errors.Is(err, tflint.ErrSensitive)
},
},
{
Name: "unknown value",
Expr: hclExpr(`var.foo`),
TargetType: reflect.TypeOf(""),
ServerImpl: func(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
return evalExpr(expr, &hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.MapVal(map[string]cty.Value{
"foo": cty.UnknownVal(cty.String),
}),
},
})
},
Want: "",
GetFileImpl: fileExists,
ErrCheck: func(err error) bool {
return !errors.Is(err, tflint.ErrUnknownValue)
},
},
{
Name: "unknown value as cty.Value",
Expr: hclExpr(`var.foo`),
TargetType: reflect.TypeOf(cty.Value{}),
ServerImpl: func(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
return evalExpr(expr, &hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.MapVal(map[string]cty.Value{
"foo": cty.UnknownVal(cty.String),
}),
},
})
},
Want: cty.UnknownVal(cty.String),
GetFileImpl: fileExists,
ErrCheck: neverHappend,
},
{
Name: "unknown value in object",
Expr: hclExpr(`{ value = var.foo }`),
TargetType: reflect.TypeOf(map[string]string{}),
ServerImpl: func(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
return evalExpr(expr, &hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.MapVal(map[string]cty.Value{
"foo": cty.UnknownVal(cty.String),
}),
},
})
},
Want: (map[string]string)(nil),
GetFileImpl: fileExists,
ErrCheck: func(err error) bool {
return !errors.Is(err, tflint.ErrUnknownValue)
},
},
{
Name: "null",
Expr: hclExpr(`var.foo`),
TargetType: reflect.TypeOf(""),
ServerImpl: func(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
return evalExpr(expr, &hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.MapVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
}),
},
})
},
Want: "",
GetFileImpl: fileExists,
ErrCheck: func(err error) bool {
return !errors.Is(err, tflint.ErrNullValue)
},
},
{
Name: "null as cty.Value",
Expr: hclExpr(`var.foo`),
TargetType: reflect.TypeOf(cty.Value{}),
ServerImpl: func(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
return evalExpr(expr, &hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.MapVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
}),
},
})
},
Want: cty.NullVal(cty.String),
GetFileImpl: fileExists,
ErrCheck: neverHappend,
},
{
Name: "null value in object",
Expr: hclExpr(`{ value = var.foo }`),
TargetType: reflect.TypeOf(map[string]string{}),
ServerImpl: func(expr hcl.Expression, opts tflint.EvaluateExprOption) (cty.Value, error) {
return evalExpr(expr, &hcl.EvalContext{
Variables: map[string]cty.Value{
"var": cty.MapVal(map[string]cty.Value{
"foo": cty.NullVal(cty.String),
}),
},
})
},
Want: (map[string]string)(nil),
GetFileImpl: fileExists,
ErrCheck: func(err error) bool {
return !errors.Is(err, tflint.ErrNullValue)
},
},
}

for _, test := range tests {
5 changes: 3 additions & 2 deletions tflint/errors.go
Original file line number Diff line number Diff line change
@@ -9,10 +9,11 @@ import (
// inside the plugin system, so you usually don't have to worry about it.
var (
// ErrUnknownValue is an error when an unknown value is referenced
ErrUnknownValue = errors.New("")
ErrUnknownValue = errors.New("unknown value found")
// ErrNullValue is an error when null value is referenced
ErrNullValue = errors.New("")
ErrNullValue = errors.New("null value found")
// ErrUnevaluable is an error when a received expression has unevaluable references.
// Deprecated: This error is no longer returned since TFLint v0.41.
ErrUnevaluable = errors.New("")
Copy link
Member Author

@wata727 wata727 Feb 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error is removed in terraform-linters/tflint#1510. The ErrUnknownValue is now returned instead.

// ErrSensitive is an error when a received expression contains a sensitive value.
ErrSensitive = errors.New("")