Skip to content

Commit

Permalink
Merge branch 'main' into issue-5848-mask-amzn-header
Browse files Browse the repository at this point in the history
  • Loading branch information
colinjlacy authored Nov 19, 2023
2 parents 600a261 + d46bc9d commit d002717
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 42 deletions.
15 changes: 15 additions & 0 deletions ast/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ var DefaultBuiltins = [...]*Builtin{
TrimSpace,
Sprintf,
StringReverse,
RenderTemplate,

// Numbers
NumbersRange,
Expand Down Expand Up @@ -1317,6 +1318,20 @@ var StringReverse = &Builtin{
Categories: stringsCat,
}

var RenderTemplate = &Builtin{
Name: "strings.render_template",
Description: `Renders a templated string with given template variables injected. For a given templated string and key/value mapping, values will be injected into the template where they are referenced by key.
For examples of templating syntax, see https://pkg.go.dev/text/template`,
Decl: types.NewFunction(
types.Args(
types.Named("value", types.S).Description("a templated string"),
types.Named("vars", types.NewObject(nil, types.NewDynamicProperty(types.S, types.A))).Description("a mapping of template variable keys to values"),
),
types.Named("result", types.S).Description("rendered template with template variables injected"),
),
Categories: stringsCat,
}

/**
* Numbers
*/
Expand Down
26 changes: 26 additions & 0 deletions builtin_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
"startswith",
"strings.any_prefix_match",
"strings.any_suffix_match",
"strings.render_template",
"strings.replace_n",
"strings.reverse",
"substring",
Expand Down Expand Up @@ -15898,6 +15899,31 @@
},
"wasm": false
},
"strings.render_template": {
"args": [
{
"description": "a templated string",
"name": "value",
"type": "string"
},
{
"description": "a mapping of template variable keys to values",
"name": "vars",
"type": "object[string: any]"
}
],
"available": [
"edge"
],
"description": "Renders a templated string with given template variables injected. For a given templated string and key/value mapping, values will be injected into the template where they are referenced by key.\n\tFor examples of templating syntax, see https://pkg.go.dev/text/template",
"introduced": "edge",
"result": {
"description": "rendered template with template variables injected",
"name": "result",
"type": "string"
},
"wasm": false
},
"strings.replace_n": {
"args": [
{
Expand Down
25 changes: 25 additions & 0 deletions capabilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -3880,6 +3880,31 @@
"type": "function"
}
},
{
"name": "strings.render_template",
"decl": {
"args": [
{
"type": "string"
},
{
"dynamic": {
"key": {
"type": "string"
},
"value": {
"type": "any"
}
},
"type": "object"
}
],
"result": {
"type": "string"
},
"type": "function"
}
},
{
"name": "strings.replace_n",
"decl": {
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ require (
github.com/tchap/go-patricia/v2 v2.3.1
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
github.com/yashtewari/glob-intersection v0.2.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.0
go.opentelemetry.io/otel v1.20.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0
go.opentelemetry.io/otel/sdk v1.20.0
go.opentelemetry.io/otel/trace v1.20.0
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1
go.opentelemetry.io/otel v1.21.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0
go.opentelemetry.io/otel/sdk v1.21.0
go.opentelemetry.io/otel/trace v1.21.0
go.uber.org/automaxprocs v1.5.3
golang.org/x/net v0.18.0
golang.org/x/time v0.4.0
Expand Down Expand Up @@ -85,7 +85,7 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel/metric v1.20.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/sys v0.14.0 // indirect
Expand Down
28 changes: 14 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,20 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.0 h1:1eHu3/pUSWaOgltNK3WJFaywKsTIr/PwvHyDmi0lQA0=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.0/go.mod h1:HyABWq60Uy1kjJSa2BVOxUVao8Cdick5AWSKPutqy6U=
go.opentelemetry.io/otel v1.20.0 h1:vsb/ggIY+hUjD/zCAQHpzTmndPqv/ml2ArbsbfBYTAc=
go.opentelemetry.io/otel v1.20.0/go.mod h1:oUIGj3D77RwJdM6PPZImDpSZGDvkD9fhesHny69JFrs=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0 h1:DeFD0VgTZ+Cj6hxravYYZE2W4GlneVH81iAOPjZkzk8=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.20.0/go.mod h1:GijYcYmNpX1KazD5JmWGsi4P7dDTTTnfv1UbGn84MnU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0 h1:gvmNvqrPYovvyRmCSygkUDyL8lC5Tl845MLEwqpxhEU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.20.0/go.mod h1:vNUq47TGFioo+ffTSnKNdob241vePmtNZnAODKapKd0=
go.opentelemetry.io/otel/metric v1.20.0 h1:ZlrO8Hu9+GAhnepmRGhSU7/VkpjrNowxRN9GyKR4wzA=
go.opentelemetry.io/otel/metric v1.20.0/go.mod h1:90DRw3nfK4D7Sm/75yQ00gTJxtkBxX+wu6YaNymbpVM=
go.opentelemetry.io/otel/sdk v1.20.0 h1:5Jf6imeFZlZtKv9Qbo6qt2ZkmWtdWx/wzcCbNUlAWGM=
go.opentelemetry.io/otel/sdk v1.20.0/go.mod h1:rmkSx1cZCm/tn16iWDn1GQbLtsW/LvsdEEFzCSRM6V0=
go.opentelemetry.io/otel/trace v1.20.0 h1:+yxVAPZPbQhbC3OfAkeIVTky6iTFpcr4SiY9om7mXSQ=
go.opentelemetry.io/otel/trace v1.20.0/go.mod h1:HJSK7F/hA5RlzpZ0zKDCHCDHm556LCDtKaAo6JmBFUU=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1/go.mod h1:sEGXWArGqc3tVa+ekntsN65DmVbVeW+7lTKTjZF3/Fo=
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 h1:cl5P5/GIfFh4t6xyruOgJP5QiA1pw4fYYdv6nc6CBWw=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0/go.mod h1:zgBdWWAu7oEEMC06MMKc5NLbA/1YDXV1sMpSqEeLQLg=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0 h1:tIqheXEFWAZ7O8A7m+J0aPTmpJN3YQ7qetUAdkkkKpk=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0/go.mod h1:nUeKExfxAQVbiVFn32YXpXZZHZ61Cc3s3Rn1pDBGAb0=
go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4=
go.opentelemetry.io/otel/metric v1.21.0/go.mod h1:o1p3CA8nNHW8j5yuQLdc1eeqEaPfzug24uvsyIEJRWM=
go.opentelemetry.io/otel/sdk v1.21.0 h1:FTt8qirL1EysG6sTQRZ5TokkU8d0ugCj8htOgThZXQ8=
go.opentelemetry.io/otel/sdk v1.21.0/go.mod h1:Nna6Yv7PWTdgJHVRD9hIYywQBRx7pbox6nwBnZIxl/E=
go.opentelemetry.io/otel/trace v1.21.0 h1:WD9i5gzvoUPuXIXH24ZNBudiarZDKuekPqi/E8fpfLc=
go.opentelemetry.io/otel/trace v1.21.0/go.mod h1:LGbsEB0f9LGjN+OZaQQ26sohbOmiMR+BaslueVtS/qQ=
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8=
Expand Down
135 changes: 135 additions & 0 deletions sdk/opa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2150,6 +2150,141 @@ result := {
}
}

func TestOpaRuntimeEnvironmentVariableDefinedInOS(t *testing.T) {
t.Setenv("TOKEN_VERIFY_KEY", "B41BD5F462719C6D6118E673A2389")

ctx := context.Background()

server := sdktest.MustNewServer(
sdktest.MockBundle("/bundles/bundle.tar.gz", map[string]string{
"main.rego": `
package system
rt := opa.runtime()
grant {
authenticatedUser
}
claims := payload {
io.jwt.verify_hs256(input.token, opa.runtime().env.TOKEN_VERIFY_KEY)
[_, payload, _] := io.jwt.decode(input.token)
}
authenticatedUser := a {
claims
a := count(claims) > 0
}
`,
}),
)

defer server.Stop()

testBundleResource := "/bundles/bundle.tar.gz"

config := fmt.Sprintf(`{
"services": {
"test": {
"url": %q
}
},
"bundles": {
"test": {
"resource": %q
}
},
}`, server.URL(), testBundleResource)

opa, err := sdk.New(ctx, sdk.Options{
Config: strings.NewReader(config),
})
if err != nil {
t.Fatal(err)
}

defer opa.Stop(ctx)

exp := true

input := map[string]interface{}{}
input["token"] = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQWxpY2lhIFNtaXRoc29uaWFuIiwicm9sZXMiOlsicmVhZGVyIiwid3JpdGVyIl0sInVzZXJuYW1lIjoiYWxpY2UifQ.md2KPJFH9OgBq-N0RonGdf5doGYRO_1miN8ugTSeTYc"

if result, err := opa.Decision(ctx, sdk.DecisionOptions{Path: "/system/grant", Input: input}); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(result.Result, exp) {
t.Fatalf("expected %v but got %v", exp, result.Result)
}
}

func TestOpaRuntimeEnvironmentVariableDefinedInConfig(t *testing.T) {
ctx := context.Background()

server := sdktest.MustNewServer(
sdktest.MockBundle("/bundles/bundle.tar.gz", map[string]string{
"main.rego": `
package system
rt := opa.runtime()
grant {
authenticatedUser
}
claims := payload {
io.jwt.verify_hs256(input.token, opa.runtime().config.env.TOKEN_VERIFY_KEY)
[_, payload, _] := io.jwt.decode(input.token)
}
authenticatedUser := a {
claims
a := count(claims) > 0
}
`,
}),
)

defer server.Stop()

testBundleResource := "/bundles/bundle.tar.gz"

config := fmt.Sprintf(`{
"services": {
"test": {
"url": %q
}
},
"bundles": {
"test": {
"resource": %q
}
},
"env": {
"TOKEN_VERIFY_KEY" : "B41BD5F462719C6D6118E673A2389"
}
}`, server.URL(), testBundleResource)

opa, err := sdk.New(ctx, sdk.Options{
Config: strings.NewReader(config),
})
if err != nil {
t.Fatal(err)
}

defer opa.Stop(ctx)

exp := true

input := map[string]interface{}{}
input["token"] = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQWxpY2lhIFNtaXRoc29uaWFuIiwicm9sZXMiOlsicmVhZGVyIiwid3JpdGVyIl0sInVzZXJuYW1lIjoiYWxpY2UifQ.md2KPJFH9OgBq-N0RonGdf5doGYRO_1miN8ugTSeTYc"

if result, err := opa.Decision(ctx, sdk.DecisionOptions{Path: "/system/grant", Input: input}); err != nil {
t.Fatal(err)
} else if !reflect.DeepEqual(result.Result, exp) {
t.Fatalf("expected %v but got %v", exp, result.Result)
}
}

func TestPrintStatements(t *testing.T) {

ctx := context.Background()
Expand Down
49 changes: 49 additions & 0 deletions test/cases/testdata/rendertemplate/rendertemplate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
cases:
- note: rendertemplate/simple
query: data.test.p = x
modules:
- |
package test
template_string = `{{.test}}`
template_vars = {`test`: `hello world`}
p = strings.render_template(template_string, template_vars)
want_result:
- x: 'hello world'

- note: rendertemplate/simpleint
query: data.test.p = x
modules:
- |
package test
template_string = `{{.test}}`
template_vars = {`test`: 2023}
p = strings.render_template(template_string, template_vars)
want_result:
- x: '2023'

- note: rendertemplate/complex
query: data.test.p = x
modules:
- |
package test
template_string = `{{range $i, $name := .hellonames}}{{if $i}},{{end}}hello {{$name}}{{end}}`
template_vars = {`hellonames`: [`rohan`, `john doe`]}
p = strings.render_template(template_string, template_vars)
want_result:
- x: 'hello rohan,hello john doe'

- note: rendertemplate/missingkey
query: data.test.p = x
modules:
- |
package test
template_string = `{{.testvarnotprovided}}`
template_vars = {`test`: `hello world`}
p = strings.render_template(template_string, template_vars)
want_error_code: eval_builtin_error
strict_error: true

Loading

0 comments on commit d002717

Please sign in to comment.