-
Notifications
You must be signed in to change notification settings - Fork 47
Feat: add ValidatingAdmissionPolicy to protect kagenti.io/type label #418
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bcceefb
Feat: add ValidatingAdmissionPolicy to protect kagenti.io/type label
rh-dnagornuks a5dca32
Test: update E2E tests to use AgentRuntime for label application
rh-dnagornuks 57f9f19
Refactor: remove DefaultsConfigReconciler
rh-dnagornuks 4230ba6
Docs: update documentation and samples for VAP enforcement
rh-dnagornuks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
charts/kagenti-operator/templates/vap/validating-admission-policy-binding.yaml
This file contains hidden or 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,12 @@ | ||
| {{- if .Values.vap.agentLabelProtection.enable }} | ||
| apiVersion: admissionregistration.k8s.io/v1 | ||
| kind: ValidatingAdmissionPolicyBinding | ||
| metadata: | ||
| name: agent-label-protection | ||
| labels: | ||
| {{- include "chart.labels" . | nindent 4 }} | ||
| app.kubernetes.io/component: admission | ||
| spec: | ||
| policyName: agent-label-protection | ||
| validationActions: [Deny] | ||
| {{- end }} |
67 changes: 67 additions & 0 deletions
67
charts/kagenti-operator/templates/vap/validating-admission-policy.yaml
This file contains hidden or 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,67 @@ | ||
| {{- if .Values.vap.agentLabelProtection.enable }} | ||
| apiVersion: admissionregistration.k8s.io/v1 | ||
| kind: ValidatingAdmissionPolicy | ||
| metadata: | ||
| name: agent-label-protection | ||
| labels: | ||
| {{- include "chart.labels" . | nindent 4 }} | ||
| app.kubernetes.io/component: admission | ||
| spec: | ||
| failurePolicy: Fail | ||
| matchConstraints: | ||
| resourceRules: | ||
| - apiGroups: ["apps"] | ||
| apiVersions: ["v1"] | ||
| operations: ["CREATE", "UPDATE"] | ||
| resources: ["deployments", "statefulsets"] | ||
|
|
||
| matchConditions: | ||
| - name: 'has-agent-type-label' | ||
| expression: >- | ||
| (has(object.metadata.labels) && 'kagenti.io/type' in object.metadata.labels) | ||
| || (has(object.spec.template.metadata) | ||
| && has(object.spec.template.metadata.labels) | ||
| && 'kagenti.io/type' in object.spec.template.metadata.labels) | ||
|
|
||
| - name: 'not-exempt-service-account' | ||
| expression: >- | ||
| !(request.userInfo.username == 'system:serviceaccount:{{ .Release.Namespace }}:{{ .Values.controllerManager.serviceAccountName }}') | ||
| {{- range .Values.vap.agentLabelProtection.exemptServiceAccounts }} | ||
| && !(request.userInfo.username == 'system:serviceaccount:{{ . }}') | ||
| {{- end }} | ||
|
|
||
| variables: | ||
| - name: metaLabelUnchanged | ||
| expression: >- | ||
| !('kagenti.io/type' in object.metadata.labels) | ||
| || (request.operation == 'UPDATE' | ||
| && 'kagenti.io/type' in oldObject.metadata.labels | ||
| && oldObject.metadata.labels['kagenti.io/type'] == object.metadata.labels['kagenti.io/type']) | ||
|
|
||
| - name: podTemplateLabelUnchanged | ||
| expression: >- | ||
| !(has(object.spec.template.metadata) | ||
| && has(object.spec.template.metadata.labels) | ||
| && 'kagenti.io/type' in object.spec.template.metadata.labels) | ||
| || (request.operation == 'UPDATE' | ||
| && has(oldObject.spec.template.metadata) | ||
| && has(oldObject.spec.template.metadata.labels) | ||
| && 'kagenti.io/type' in oldObject.spec.template.metadata.labels | ||
| && oldObject.spec.template.metadata.labels['kagenti.io/type'] == object.spec.template.metadata.labels['kagenti.io/type']) | ||
|
|
||
| validations: | ||
| - expression: "variables.metaLabelUnchanged" | ||
| messageExpression: >- | ||
| 'The kagenti.io/type label on ' | ||
| + object.metadata.namespace + '/' + object.metadata.name | ||
| + ' can only be applied by the kagenti-operator via an AgentRuntime CR.' | ||
| + ' Create an AgentRuntime targeting this workload instead of manually setting the label.' | ||
| reason: Forbidden | ||
| - expression: "variables.podTemplateLabelUnchanged" | ||
| messageExpression: >- | ||
| 'The kagenti.io/type label in the pod template of ' | ||
| + object.metadata.namespace + '/' + object.metadata.name | ||
| + ' can only be applied by the kagenti-operator via an AgentRuntime CR.' | ||
| + ' Create an AgentRuntime targeting this workload instead of manually setting the label.' | ||
| reason: Forbidden | ||
| {{- end }} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
| resources: | ||
| - validating-admission-policy.yaml | ||
| - validating-admission-policy-binding.yaml |
10 changes: 10 additions & 0 deletions
10
kagenti-operator/config/vap/validating-admission-policy-binding.yaml
This file contains hidden or 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 @@ | ||
| apiVersion: admissionregistration.k8s.io/v1 | ||
| kind: ValidatingAdmissionPolicyBinding | ||
| metadata: | ||
| name: agent-label-protection | ||
| labels: | ||
| app.kubernetes.io/name: kagenti-operator | ||
| app.kubernetes.io/component: admission | ||
| spec: | ||
| policyName: agent-label-protection | ||
| validationActions: [Deny] |
90 changes: 90 additions & 0 deletions
90
kagenti-operator/config/vap/validating-admission-policy.yaml
This file contains hidden or 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,90 @@ | ||
| # ValidatingAdmissionPolicy: prevent manual application of the kagenti.io/type | ||
| # label on Deployments and StatefulSets. Only the kagenti-operator controller | ||
| # (via an AgentRuntime CR) is allowed to set this label. Users who need the | ||
| # label must create an AgentRuntime targeting their workload instead. | ||
| # | ||
| # The policy protects the label in TWO locations: | ||
| # 1. object.metadata.labels — top-level workload label | ||
| # 2. object.spec.template.metadata.labels — pod template label (propagated | ||
| # to every Pod the workload creates) | ||
| # | ||
| # The policy is split into two layers: | ||
| # | ||
| # matchConditions — skip evaluation entirely when: | ||
| # 1. The label is absent from BOTH locations (irrelevant request), OR | ||
| # 2. The request originates from a trusted service account. | ||
| # Two operator SA identities are listed because kustomize (make deploy) | ||
| # and Helm use different namespaces and SA names: | ||
| # - kagenti-operator-system/kagenti-operator-controller-manager (kustomize) | ||
| # - kagenti-system/controller-manager (Helm) | ||
| # | ||
| # validations — for everyone else, allow only UPDATE requests where the | ||
| # label already existed on the previous revision with the same value in | ||
| # whichever location(s) it appears. | ||
| # | ||
| # CREATE requests that carry the label are always rejected for non-operator | ||
| # callers, because the "label unchanged" check evaluates to false on CREATE | ||
| # (there is no oldObject). | ||
| apiVersion: admissionregistration.k8s.io/v1 | ||
| kind: ValidatingAdmissionPolicy | ||
| metadata: | ||
| name: agent-label-protection | ||
| labels: | ||
| app.kubernetes.io/name: kagenti-operator | ||
| app.kubernetes.io/component: admission | ||
| spec: | ||
| failurePolicy: Fail | ||
| matchConstraints: | ||
| resourceRules: | ||
| - apiGroups: ["apps"] | ||
| apiVersions: ["v1"] | ||
| operations: ["CREATE", "UPDATE"] | ||
| resources: ["deployments", "statefulsets"] | ||
|
|
||
| matchConditions: | ||
| - name: 'has-agent-type-label' | ||
| expression: >- | ||
| (has(object.metadata.labels) && 'kagenti.io/type' in object.metadata.labels) | ||
| || (has(object.spec.template.metadata) | ||
| && has(object.spec.template.metadata.labels) | ||
| && 'kagenti.io/type' in object.spec.template.metadata.labels) | ||
|
|
||
| - name: 'not-operator-service-account' | ||
| expression: >- | ||
| !(request.userInfo.username == 'system:serviceaccount:kagenti-operator-system:kagenti-operator-controller-manager') | ||
| && !(request.userInfo.username == 'system:serviceaccount:kagenti-system:controller-manager') | ||
|
|
||
| variables: | ||
| - name: metaLabelUnchanged | ||
| expression: >- | ||
| !('kagenti.io/type' in object.metadata.labels) | ||
| || (request.operation == 'UPDATE' | ||
| && 'kagenti.io/type' in oldObject.metadata.labels | ||
| && oldObject.metadata.labels['kagenti.io/type'] == object.metadata.labels['kagenti.io/type']) | ||
|
|
||
| - name: podTemplateLabelUnchanged | ||
| expression: >- | ||
| !(has(object.spec.template.metadata) | ||
| && has(object.spec.template.metadata.labels) | ||
| && 'kagenti.io/type' in object.spec.template.metadata.labels) | ||
| || (request.operation == 'UPDATE' | ||
| && has(oldObject.spec.template.metadata) | ||
| && has(oldObject.spec.template.metadata.labels) | ||
| && 'kagenti.io/type' in oldObject.spec.template.metadata.labels | ||
| && oldObject.spec.template.metadata.labels['kagenti.io/type'] == object.spec.template.metadata.labels['kagenti.io/type']) | ||
|
|
||
| validations: | ||
| - expression: "variables.metaLabelUnchanged" | ||
| messageExpression: >- | ||
| 'The kagenti.io/type label on ' | ||
| + object.metadata.namespace + '/' + object.metadata.name | ||
| + ' can only be applied by the kagenti-operator via an AgentRuntime CR.' | ||
| + ' Create an AgentRuntime targeting this workload instead of manually setting the label.' | ||
| reason: Forbidden | ||
| - expression: "variables.podTemplateLabelUnchanged" | ||
| messageExpression: >- | ||
| 'The kagenti.io/type label in the pod template of ' | ||
| + object.metadata.namespace + '/' + object.metadata.name | ||
| + ' can only be applied by the kagenti-operator via an AgentRuntime CR.' | ||
| + ' Create an AgentRuntime targeting this workload instead of manually setting the label.' | ||
| reason: Forbidden | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
object.metadata.labelsis accessed here without ahas()guard, unlike thehas-agent-type-labelmatchCondition and the parallelpodTemplateLabelUnchangedvariable (both of which guard withhas(...)). If a non-operator submits a workload carryingkagenti.io/typeonly in the pod template with no top-levelmetadata.labels, this expression accesses an absent map; underfailurePolicy: Failthat risks an eval-error denial instead of the intendedmessageExpression. The outcome (deny) is still correct, so this is low severity, but suggest guarding for symmetry:The same applies to the identical Helm template at
charts/kagenti-operator/templates/vap/validating-admission-policy.yaml.