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

eval: improve unknown/secret for complex literals #170

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 6 additions & 10 deletions eval/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,6 @@ func (e *evalContext) evaluateArray(x *expr, repr *arrayExpr) *value {
array, items := make([]*value, len(repr.elements)), make([]schema.Builder, len(repr.elements))
for i, elem := range repr.elements {
ev := e.evaluateExpr(elem)
v.combine(ev)

array[i], items[i] = ev, ev.schema
}

Expand All @@ -516,8 +514,6 @@ func (e *evalContext) evaluateObject(x *expr, repr *objectExpr) *value {
object, properties := make(map[string]*value, len(keys)), make(map[string]schema.Builder, len(keys))
for _, k := range keys {
pv := e.evaluateExpr(repr.properties[k])
v.combine(pv)

object[k], properties[k] = pv, pv.schema
}

Expand All @@ -536,7 +532,7 @@ func (e *evalContext) evaluateInterpolate(x *expr, repr *interpolateExpr) *value
if i.value != nil {
pv := e.evaluatePropertyAccess(x, i.value.accessors)
s, unknown, secret := pv.toString()
v.unknown, v.secret = v.unknown || unknown, v.secret || secret
v.unknown, v.secret = v.containsUnknowns() || unknown, v.containsSecrets() || secret
if !unknown {
b.WriteString(s)
}
Expand All @@ -551,7 +547,7 @@ func (e *evalContext) evaluateInterpolate(x *expr, repr *interpolateExpr) *value
return v
}

// evalutePropertyAccess evaluates a property access.
// evaluatePropertyAccess evaluates a property access.
func (e *evalContext) evaluatePropertyAccess(x *expr, accessors []*propertyAccessor) *value {
// We make a copy of the resolved value here because evaluateExpr will merge it with its base, which mutates the
// value. We also stamp over the def with the provided expression in order to maintain proper error reporting.
Expand All @@ -560,7 +556,7 @@ func (e *evalContext) evaluatePropertyAccess(x *expr, accessors []*propertyAcces
return v
}

// evaluteExprAccess is the primary entrypoint for access evaluation, and begins with the assumption that the receiver
// evaluateExprAccess is the primary entrypoint for access evaluation, and begins with the assumption that the receiver
// is an expression. If the receiver is a list, object, or secret expression, it is _not evaluated_. If the receiver
// is any other type of expression, it is evaluated and the result is passed to evaluateValueAccess. Once all accessors
// have been processed, the resolved expression is evaluated.
Expand Down Expand Up @@ -620,7 +616,7 @@ func (e *evalContext) evaluateExprAccess(x *expr, accessors []*propertyAccessor)
return e.evaluateExpr(receiver)
}

// evaluateValueAccess evalutes a list of accessors relative to a value receiver.
// evaluateValueAccess evaluates a list of accessors relative to a value receiver.
func (e *evalContext) evaluateValueAccess(syntax ast.Expr, receiver *value, accessors []*propertyAccessor) *value {
for len(accessors) > 0 {
accessor := accessors[0]
Expand Down Expand Up @@ -665,7 +661,7 @@ func (e *evalContext) evaluateValueAccess(syntax ast.Expr, receiver *value, acce
return receiver
}

// evaluateValueAccess evalutes a list of accessors relative to an unknown value receiver. Unknown values are
// evaluateValueAccess evaluates a list of accessors relative to an unknown value receiver. Unknown values are
// synthesized for each receiver.
func (e *evalContext) evaluateUnknownAccess(syntax ast.Expr, receiver *schema.Schema, accessors []*propertyAccessor) *value {
var val *value
Expand Down Expand Up @@ -827,7 +823,7 @@ func (e *evalContext) evaluateBuiltinOpen(x *expr, repr *openExpr) *value {
v.schema = x.schema

inputs, ok := e.evaluateTypedExpr(repr.inputs, repr.inputSchema)
if !ok || inputs.unknown || e.validating || err != nil {
if !ok || inputs.containsUnknowns() || e.validating || err != nil {
v.unknown = true
return v
}
Expand Down
17 changes: 12 additions & 5 deletions eval/testdata/eval/builtin-combine/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -809,8 +809,6 @@
}
}
],
"secret": true,
"unknown": true,
"trace": {
"def": {
"environment": "builtin-combine",
Expand Down Expand Up @@ -900,7 +898,12 @@
}
},
"checkJson": {
"builtins": "[secret]",
"builtins": [
"[secret]",
"[secret]",
"[secret]",
"[secret]"
],
"open": "[unknown]",
"password": "[secret]"
},
Expand Down Expand Up @@ -1731,7 +1734,6 @@
}
}
],
"secret": true,
"trace": {
"def": {
"environment": "builtin-combine",
Expand Down Expand Up @@ -1851,7 +1853,12 @@
}
},
"evalJson": {
"builtins": "[secret]",
"builtins": [
"[secret]",
"[secret]",
"[secret]",
"[secret]"
],
"open": {
"foo": "bar"
},
Expand Down
22 changes: 18 additions & 4 deletions eval/testdata/eval/builtin-errs/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,6 @@
}
}
],
"unknown": true,
"trace": {
"def": {
"environment": "builtin-errs",
Expand Down Expand Up @@ -1053,7 +1052,15 @@
}
},
"checkJson": {
"builtins": "[unknown]",
"builtins": [
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]"
],
"number": 42
},
"evalDiags": [
Expand Down Expand Up @@ -2034,7 +2041,6 @@
}
}
],
"unknown": true,
"trace": {
"def": {
"environment": "builtin-errs",
Expand Down Expand Up @@ -2110,7 +2116,15 @@
}
},
"evalJson": {
"builtins": "[unknown]",
"builtins": [
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]"
],
"number": 42
}
}
30 changes: 18 additions & 12 deletions eval/testdata/eval/cycle/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@
}
}
},
"unknown": true,
"trace": {
"def": {
"environment": "cycle",
Expand Down Expand Up @@ -383,7 +382,6 @@
}
}
},
"unknown": true,
"trace": {
"def": {
"environment": "cycle",
Expand Down Expand Up @@ -421,7 +419,6 @@
}
}
},
"unknown": true,
"trace": {
"def": {
"environment": "cycle",
Expand Down Expand Up @@ -478,9 +475,15 @@
}
},
"checkJson": {
"a": "[unknown]",
"b": "[unknown]",
"c": "[unknown]"
"a": {
"p": "[unknown]"
},
"b": {
"p": "[unknown]"
},
"c": {
"p": "[unknown]"
}
},
"evalDiags": [
{
Expand Down Expand Up @@ -828,7 +831,6 @@
}
}
},
"unknown": true,
"trace": {
"def": {
"environment": "cycle",
Expand Down Expand Up @@ -866,7 +868,6 @@
}
}
},
"unknown": true,
"trace": {
"def": {
"environment": "cycle",
Expand Down Expand Up @@ -904,7 +905,6 @@
}
}
},
"unknown": true,
"trace": {
"def": {
"environment": "cycle",
Expand Down Expand Up @@ -961,8 +961,14 @@
}
},
"evalJson": {
"a": "[unknown]",
"b": "[unknown]",
"c": "[unknown]"
"a": {
"p": "[unknown]"
},
"b": {
"p": "[unknown]"
},
"c": {
"p": "[unknown]"
}
}
}
34 changes: 30 additions & 4 deletions eval/testdata/eval/invalid-access/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -3214,7 +3214,6 @@
}
}
],
"unknown": true,
"trace": {
"def": {
"environment": "invalid-access",
Expand Down Expand Up @@ -3871,7 +3870,21 @@
2,
3
],
"errors": "[unknown]",
"errors": [
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]"
],
"myObject": {
"foo": "bar"
},
Expand Down Expand Up @@ -7033,7 +7046,6 @@
}
}
],
"unknown": true,
"trace": {
"def": {
"environment": "invalid-access",
Expand Down Expand Up @@ -8116,7 +8128,21 @@
2,
3
],
"errors": "[unknown]",
"errors": [
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]",
"[unknown]"
],
"myObject": {
"foo": "bar"
},
Expand Down
23 changes: 23 additions & 0 deletions eval/testdata/eval/nested-unknowns-secrets/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
values:
top-level-secret:
fn::secret: top-secret
cloudflare:
username: [email protected]
apiKey:
fn::secret: apiKeySecret
zoneId: zone-id-123
apiToken:
fn::secret: apiTokenSecret
account:
name: account-name
id:
fn::secret: accountIdSecret
jsonOutput:
fn::toJSON: ${cloudflare}
stringOutput:
fn::toString: ${cloudflare}
environmentVariables:
CLOUDFLARE_API_TOKEN: ${cloudflare.apiToken}
pulumiConfig:
accountId: ${cloudflare.account.id}
refTopLevelSecret: ${top-level-secret}
Loading