-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: retries for policies cm comprised of multiple dependent files
Signed-off-by: Ievgenii Shepeliuk <[email protected]>
- Loading branch information
1 parent
1e3d5d7
commit 58d59b5
Showing
10 changed files
with
151 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +0,0 @@ | ||
Please wait while the OPA is deployed on your cluster. | ||
|
||
For example policies that you can enforce with OPA see https://www.openpolicyagent.org. | ||
|
||
If you installed this chart with the default values, you can exercise the sample policy. | ||
|
||
# 1. Create a namespace called "opa-example" | ||
|
||
kubectl create namespace opa-example | ||
|
||
# 2. Create an Ingress in the "opa-example" namespace that complies with the policy. | ||
|
||
cat > ingress-ok.yaml <<EOF | ||
apiVersion: extensions/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: ingress-ok | ||
spec: | ||
rules: | ||
- host: signin.dev.acmecorp.com | ||
http: | ||
paths: | ||
- backend: | ||
serviceName: nginx | ||
servicePort: 80 | ||
EOF | ||
|
||
kubectl -n opa-example create -f ingress-ok.yaml | ||
|
||
# 3. Try to create an Ingress in the "opa-example" namespace that violates the policy. | ||
|
||
cat > ingress-bad.yaml <<EOF | ||
apiVersion: extensions/v1beta1 | ||
kind: Ingress | ||
metadata: | ||
name: ingress-bad | ||
spec: | ||
rules: | ||
- host: signin.acmecorp.com | ||
http: | ||
paths: | ||
- backend: | ||
serviceName: nginx | ||
servicePort: 80 | ||
EOF | ||
|
||
kubectl -n opa-example create -f ingress-bad.yaml | ||
|
||
If you want to turn off authz for debugging purposes, you can do so by upgrading the chart like so: | ||
helm upgrade {{ .Release.Name }} stable/opa --reuse-values --set authz.enabled=false | ||
|
||
You can query OPA to see the policies it has loaded (you will need to turn off authz as described above): | ||
|
||
export OPA_POD_NAME=$(kubectl get pods --namespace {{ .Release.Namespace }} -l "app={{ template "opa.fullname" . }}" -o jsonpath="{.items[0].metadata.name}") | ||
|
||
kubectl port-forward $OPA_POD_NAME 8080:443 --namespace {{ .Release.Namespace }} | ||
|
||
curl -k -s https://localhost:8080/v1/policies | jq -r '.result[].raw' | ||
|
||
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
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,16 @@ | ||
--- | ||
kind: ConfigMap | ||
metadata: | ||
name: multi-file-fail-policy | ||
labels: | ||
kube-mgmt/e2e: "true" | ||
openpolicyagent.org/policy: rego | ||
apiVersion: v1 | ||
data: | ||
f.rego: | | ||
package my_pkg_fail | ||
import data.my_pkg_fail.functions.my_func | ||
default my_rule := false | ||
my_rule { | ||
my_func(input.hello) | ||
} |
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,19 @@ | ||
--- | ||
kind: ConfigMap | ||
metadata: | ||
name: multi-file-policy | ||
labels: | ||
kube-mgmt/e2e: "true" | ||
openpolicyagent.org/policy: rego | ||
apiVersion: v1 | ||
data: | ||
a.rego: | | ||
package my_pkg | ||
import data.my_pkg.functions.my_func | ||
default my_rule := false | ||
my_rule { | ||
my_func(input.hello) | ||
} | ||
b.rego: | | ||
package my_pkg.functions | ||
my_func(str) := startswith("world", str) |
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,64 @@ | ||
#!/bin/bash | ||
set -ex | ||
|
||
OPA="http --ignore-stdin :8080/v1" | ||
FX_OK="$(dirname $0)/../fixture-multi.yaml" | ||
FX_KO="$(dirname $0)/../fixture-multi-fail.yaml" | ||
|
||
${OPA}/data/my_pkg/my_rule | jq -e '.result==null' | ||
|
||
kubectl apply -f ${FX_OK} | ||
sleep 3 | ||
|
||
${OPA}/policies | jq -e '.result|any(.id=="default/multi-file-policy/a.rego")==true' | ||
${OPA}/policies | jq -e '.result|any(.id=="default/multi-file-policy/b.rego")==true' | ||
|
||
kubectl get cm multi-file-policy -ojson | \ | ||
jq -e '.metadata.annotations["openpolicyagent.org/kube-mgmt-status"]|fromjson|.status=="ok"' | ||
kubectl get cm multi-file-policy -ojson | \ | ||
jq -e '.metadata.annotations["openpolicyagent.org/kube-mgmt-retries"]=="0"' | ||
|
||
${OPA}/data/my_pkg/my_rule input[hello]=world | jq -e '.result==true' | ||
# ${OPA}/data/my_pkg/my_rule input[hello]=incorrect | jq -e '.result==false' | ||
${OPA}/data/my_pkg/my_rule input[hello]=incorrect | jq -e '.result==123' | ||
|
||
###### | ||
# | ||
###### | ||
|
||
kubectl apply -f ${FX_KO} | ||
sleep 3 | ||
|
||
${OPA}/policies | jq -e '.result|length==2' | ||
|
||
kubectl get cm multi-file-fail-policy -ojson | \ | ||
jq -e '.metadata.annotations["openpolicyagent.org/kube-mgmt-status"]|fromjson|.status=="error"' | ||
kubectl get cm multi-file-fail-policy -ojson | \ | ||
jq -e '.metadata.annotations["openpolicyagent.org/kube-mgmt-retries"]=="0"' | ||
|
||
###### | ||
# | ||
###### | ||
|
||
cat ${FX_OK} | \ | ||
yq '.metadata.labels["openpolicyagent.org/policy"]=""' | \ | ||
yq '.metadata.annotations["openpolicyagent.org/kube-mgmt-retries"]="0"' | \ | ||
kubectl apply -f - | ||
sleep 3 | ||
|
||
${OPA}/data/my_pkg/my_rule | jq -e '.result==null' | ||
|
||
kubectl label --overwrite cm multi-file-policy openpolicyagent.org/policy=rego | ||
sleep 3 | ||
|
||
${OPA}/policies | jq -e '.result|any(.id=="default/multi-file-policy/a.rego")==true' | ||
${OPA}/policies | jq -e '.result|any(.id=="default/multi-file-policy/b.rego")==true' | ||
|
||
kubectl get cm multi-file-policy -ojson | \ | ||
jq -e '.metadata.annotations["openpolicyagent.org/kube-mgmt-status"]|fromjson|.status=="ok"' | ||
kubectl get cm multi-file-policy -ojson | \ | ||
jq -e '.metadata.annotations["openpolicyagent.org/kube-mgmt-retries"]=="0"' | ||
|
||
${OPA}/data/my_pkg/my_rule input[hello]=world | jq -e '.result==true' | ||
${OPA}/data/my_pkg/my_rule input[hello]=incorrect | jq -e '.result==false' | ||
|
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,10 @@ | ||
useHttps: false | ||
|
||
opa: null | ||
|
||
authz: | ||
enabled: false | ||
|
||
mgmt: | ||
extraArgs: | ||
- "--log-level=debug" |
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