-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: preserve metadata annotations on OCI registry push (#750)
* fix: preserve metadata annotations on OCI registry push Annotations can be accessed with the rego.metadata built-in functions. They can be used in policies to populate messages. Adding annotation support, as exposed by the FileLoader interface of open-policy-agent/opa/loader, preserves this functionality for policies pushed to an OCI registry. Signed-off-by: Kevin Swiber <[email protected]> * Setting policyContents to the raw Rego source file on OCI push. Signed-off-by: Kevin Swiber <[email protected]> * Moving OCI push annotations tests to acceptance and e2e. Signed-off-by: Kevin Swiber <[email protected]> Signed-off-by: Kevin Swiber <[email protected]>
- Loading branch information
1 parent
df0b7b1
commit f18b7bb
Showing
7 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
services: | ||
ports: | ||
- 22 | ||
- 21 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import data.services | ||
|
||
name := input.metadata.name | ||
|
||
kind := input.kind | ||
|
||
type := input.spec.type | ||
|
||
# METADATA | ||
# title: Example using annotations | ||
# custom: | ||
# template: 'Cannot expose port %v on LoadBalancer. Denied ports: %v' | ||
deny[msg] { | ||
kind == "Service" | ||
type == "LoadBalancer" | ||
|
||
some p | ||
input.spec.ports[p].port | ||
|
||
input.spec.ports[p].port == services.ports[_] | ||
|
||
metadata := rego.metadata.rule() | ||
msg := sprintf(metadata.custom.template, [input.spec.ports[p].port, services.ports]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
test_service_denied { | ||
input := { | ||
"kind": "Service", | ||
"metadata": {"name": "sample"}, | ||
"spec": { | ||
"type": "LoadBalancer", | ||
"ports": [{"port": 22}], | ||
}, | ||
} | ||
|
||
deny["Cannot expose port 22 on LoadBalancer. Denied ports: [22, 21]"] with input as input | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: hello-kubernetes | ||
spec: | ||
type: LoadBalancer | ||
ports: | ||
- port: 22 | ||
targetPort: 22 | ||
selector: | ||
app: hello-kubernetes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env bats | ||
|
||
@test "Can verify policies that rely on annotations" { | ||
run $CONFTEST verify --data exclusions service.yaml | ||
|
||
[ "$status" -eq 0 ] | ||
echo $output | ||
[[ "$output" =~ "1 test, 1 passed, 0 warnings, 0 failures" ]] | ||
} |