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

fix: change escaping char #87

Merged
merged 2 commits into from
Oct 10, 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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ foo:
```

Here the `(bar)` key conflict with the projection syntax used.
To workaround this issue, you can escape a projection by surrounding it with `/` characters like this:
To workaround this issue, you can escape a projection by surrounding it with `\` characters like this:

```yaml
apiVersion: json.kyverno.io/v1alpha1
Expand All @@ -344,17 +344,17 @@ spec:
assert:
all:
- foo:
/(bar)/: 10
\(bar)\: 10
Copy link
Member

Choose a reason for hiding this comment

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

Is the trailing \ necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, to escape the binding

```

In this case, the leading and trailing `/` characters will be erased and the projection won't be applied.
In this case, the leading and trailing `\` characters will be erased and the projection won't be applied.

Note that it's still possible to use the `~` modifier or to create a named binding with and escaped projection.

Keys like this are perfectly valid:
- `~index./baz/`
- `/baz/@foo`
- `~index./baz/@foo`
- `~index.\baz\`
- `\baz\@foo`
- `~index.\baz\@foo`

## SDK

Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/assert/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var (
foreachRegex = regexp.MustCompile(`^~(\w+)?\.(.*)`)
bindingRegex = regexp.MustCompile(`(.*)@(\w+)$`)
escapeRegex = regexp.MustCompile(`^/(.+)/$`)
escapeRegex = regexp.MustCompile(`^\\(.+)\\$`)
engineRegex = regexp.MustCompile(`^\((?:(\w+):)?(.+)\)$`)
)

Expand Down
12 changes: 6 additions & 6 deletions pkg/engine/assert/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Test_parseExpressionRegex(t *testing.T) {
},
}, {
name: "escape",
in: "/~(test)@foo/",
in: `\~(test)@foo\`,
want: &expression{
foreach: false,
foreachName: "",
Expand All @@ -124,7 +124,7 @@ func Test_parseExpressionRegex(t *testing.T) {
},
}, {
name: "escape",
in: "/test/",
in: `\test\`,
want: &expression{
foreach: false,
foreachName: "",
Expand All @@ -133,7 +133,7 @@ func Test_parseExpressionRegex(t *testing.T) {
},
}, {
name: "escape",
in: "/(test)/",
in: `\(test)\`,
want: &expression{
foreach: false,
foreachName: "",
Expand All @@ -142,7 +142,7 @@ func Test_parseExpressionRegex(t *testing.T) {
},
}, {
name: "escape",
in: "//test//",
in: `\/test/\`,
want: &expression{
foreach: false,
foreachName: "",
Expand All @@ -151,7 +151,7 @@ func Test_parseExpressionRegex(t *testing.T) {
},
}, {
name: "escape",
in: "~index./(test)/",
in: `~index.\(test)\`,
want: &expression{
foreach: true,
foreachName: "index",
Expand All @@ -160,7 +160,7 @@ func Test_parseExpressionRegex(t *testing.T) {
},
}, {
name: "escape",
in: "~index./(test)/@name",
in: `~index.\(test)\@name`,
want: &expression{
foreach: true,
foreachName: "index",
Expand Down
10 changes: 5 additions & 5 deletions testdata/escaped/policy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ spec:
assert:
all:
- foo:
/(bar)/: 4
\(bar)\: 4
- foo:
/(bar)/@test:
\(bar)\@test:
($test): 4
- foo:
/(bar)@test/: 6
\(bar)@test\: 6
- foo:
/(bar)@test/@test:
\(bar)@test\@test:
($test): 6
- foo:
/~foos/:
\~foos\:
- 1
- 2
- 3
12 changes: 6 additions & 6 deletions website/docs/policies/escaping.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ foo:
```

Here the `(bar)` key conflict with the projection syntax.
To workaround this situation, you can escape a projection by surrounding it with `/` characters like this:
To workaround this situation, you can escape a projection by surrounding it with `\` characters like this:

```yaml
apiVersion: json.kyverno.io/v1alpha1
Expand All @@ -28,15 +28,15 @@ spec:
assert:
all:
- foo:
/(bar)/: 10
\(bar)\: 10
```

In this case, the leading and trailing `/` characters will be erased and the projection won't be applied.
In this case, the leading and trailing `\` characters will be erased and the projection won't be applied.

Note that it's still possible to use the `~` modifier or to create a named binding with and escaped projection.

Keys like this are perfectly valid:

- `~index./baz/`
- `/baz/@foo`
- `~index./baz/@foo`
- `~index.\baz\`
- `\baz\@foo`
- `~index.\baz\@foo`
Loading