mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
docs: add split and lower built-in examples (#8992)
Another small pass on #3786. Adds playground examples for: - `split` — pull a team segment out of a path - `lower` — normalize mixed-case roles before an allowlist check Same layout as the other string examples. Eval'd with opa 1.19 against the sample input/data. --------- Signed-off-by: Dean Chen <51218137+locker95@users.noreply.github.com> Signed-off-by: Dean Chen <862469039@qq.com>
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"showInput": true,
|
||||
"showData": true,
|
||||
"showTitles": false,
|
||||
"titleSize": 4
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"admin_roles": ["admin", "owner"]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"role": "Admin"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<!-- markdownlint-disable MD041 -->
|
||||
|
||||
`lower` returns the lowercase form of a string. Use it when identity providers
|
||||
send mixed-case roles or emails and you want a stable comparison.
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"allow": true,
|
||||
"role": "admin"
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package play
|
||||
|
||||
role := lower(input.role)
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if role in data.admin_roles
|
||||
@@ -0,0 +1 @@
|
||||
Normalize roles with lower
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"showInput": true,
|
||||
"showData": false,
|
||||
"showTitles": false,
|
||||
"titleSize": 4
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"path": "/teams/payments/deploy"
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<!-- markdownlint-disable MD041 -->
|
||||
|
||||
`split` breaks a string into a list of parts. Policies often use it to pull a
|
||||
segment out of a path or dotted name before comparing against a list of allowed values.
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"allow": true,
|
||||
"parts": [
|
||||
"",
|
||||
"teams",
|
||||
"payments",
|
||||
"deploy"
|
||||
],
|
||||
"team": "payments"
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package play
|
||||
|
||||
# "/teams/payments/deploy" -> ["", "teams", "payments", "deploy"]
|
||||
parts := split(input.path, "/")
|
||||
team := parts[2]
|
||||
|
||||
default allow := false
|
||||
|
||||
allow if team == "payments"
|
||||
@@ -0,0 +1 @@
|
||||
Pull a path segment with split
|
||||
@@ -64,3 +64,17 @@ See also the note at the top of this page about how `sprintf` pre-processes
|
||||
values (for example with `%T`).
|
||||
|
||||
<PlaygroundExample dir={require.context('../_examples/strings/sprintf/deny-message')} />
|
||||
|
||||
### `split`
|
||||
|
||||
`split` returns a list of substrings separated by a delimiter. Use it when a
|
||||
path, hostname, or other structured string needs to be broken into parts.
|
||||
|
||||
<PlaygroundExample dir={require.context('../_examples/strings/split/team-from-path')} />
|
||||
|
||||
### `lower`
|
||||
|
||||
`lower` converts a string to lowercase. It is useful for normalisation and
|
||||
formatting when values may arrive in mixed case.
|
||||
|
||||
<PlaygroundExample dir={require.context('../_examples/strings/lower/normalize-role')} />
|
||||
|
||||
Reference in New Issue
Block a user