-
Notifications
You must be signed in to change notification settings - Fork 361
fix(appArmor): update Missing AppArmor Profile query for new kubernetes syntax #7979
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
Open
cx-artur-ribeiro
wants to merge
2
commits into
master
Choose a base branch
from
improve-kubernetes-app-armor-profile-new-syntax
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -23,30 +23,63 @@ isValidAppArmorProfile(profile) { | |||||||||||||||||||||
| startswith(profile, "localhost/") | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Valid types for the new securityContext.appArmorProfile API (Kubernetes 1.23+). | ||||||||||||||||||||||
| # "Unconfined" is not a valid type as stated in the documentation. | ||||||||||||||||||||||
| validAppArmorProfileType(t) { | ||||||||||||||||||||||
| t == "RuntimeDefault" | ||||||||||||||||||||||
| } else { | ||||||||||||||||||||||
| t == "Localhost" | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # True when the container has valid AppArmor via securityContext.appArmorProfile | ||||||||||||||||||||||
| hasValidAppArmorProfileNewSyntax(document, typeKey, containerIndex) { | ||||||||||||||||||||||
| specInfo := k8sLib.getSpecInfo(document) | ||||||||||||||||||||||
| container := specInfo.spec[typeKey][containerIndex] | ||||||||||||||||||||||
| containerAppArmor := object.get(object.get(container, "securityContext", {}), "appArmorProfile", {}) | ||||||||||||||||||||||
| containerAppArmor.type != null | ||||||||||||||||||||||
| validAppArmorProfileType(containerAppArmor.type) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # True when the pod has valid AppArmor via securityContext.appArmorProfile | ||||||||||||||||||||||
| hasValidAppArmorProfileNewSyntax(document, typeKey, containerIndex) { | ||||||||||||||||||||||
| specInfo := k8sLib.getSpecInfo(document) | ||||||||||||||||||||||
| container := specInfo.spec[typeKey][containerIndex] | ||||||||||||||||||||||
| containerAppArmor := object.get(object.get(container, "securityContext", {}), "appArmorProfile", {}) | ||||||||||||||||||||||
| not containerAppArmor.type | ||||||||||||||||||||||
| podAppArmor := object.get(object.get(specInfo.spec, "securityContext", {}), "appArmorProfile", {}) | ||||||||||||||||||||||
| podAppArmor.type != null | ||||||||||||||||||||||
| validAppArmorProfileType(podAppArmor.type) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| CxPolicy[result] { | ||||||||||||||||||||||
| document := input.document[i] | ||||||||||||||||||||||
| metadata := document.metadata | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| specInfo := k8sLib.getSpecInfo(document) | ||||||||||||||||||||||
| container := specInfo.spec[types[x]][_].name | ||||||||||||||||||||||
| container := specInfo.spec[types[x]][c].name | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| metadataInfo := getMetadataInfo(document) | ||||||||||||||||||||||
| annotations := object.get(metadataInfo.metadata, "annotations", {}) | ||||||||||||||||||||||
| expectedKey := sprintf("container.apparmor.security.beta.kubernetes.io/%s", [container]) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| not isValidAppArmorProfile(annotations[expectedKey]) | ||||||||||||||||||||||
| not hasValidAppArmorProfileNewSyntax(document, x, c) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| annotationsPath := trim_left(sprintf("%s.annotations", [metadataInfo.path]), ".") | ||||||||||||||||||||||
| searchPath := get_apparmor_search_path(specInfo, x, c, annotationsPath) | ||||||||||||||||||||||
| msg := get_apparmor_messages(searchPath, metadata.name, container, "IncorrectValue", expectedKey, annotationsPath) | ||||||||||||||||||||||
| searchValue := get_apparmor_search_value(searchPath, document.kind, expectedKey, x, c, "IncorrectValue") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| result := { | ||||||||||||||||||||||
| "documentId": document.id, | ||||||||||||||||||||||
| "resourceType": document.kind, | ||||||||||||||||||||||
| "resourceName": metadata.name, | ||||||||||||||||||||||
| "searchKey": trim_right(sprintf("metadata.name={{%s}}.%s", [metadata.name, metadataInfo.path]), "."), | ||||||||||||||||||||||
| "issueType": "IncorrectValue", | ||||||||||||||||||||||
| "searchValue": sprintf("%s%s", [document.kind, expectedKey]), # handle multiple kinds and key combinations | ||||||||||||||||||||||
| "keyExpectedValue": sprintf("metadata.name={{%s}}.%s[%s] should be set to 'runtime/default' or 'localhost'", [metadata.name, annotationsPath, expectedKey]), | ||||||||||||||||||||||
| "keyActualValue": sprintf("metadata.name={{%s}}.%s[%s] does not specify a valid AppArmor profile", [metadata.name, annotationsPath, expectedKey]), | ||||||||||||||||||||||
| "searchLine": search_line_metadata(annotationsPath), | ||||||||||||||||||||||
| "searchKey": sprintf("metadata.name={{%s}}.%s", [metadata.name, searchPath]), | ||||||||||||||||||||||
| "issueType": "IncorrectValue", | ||||||||||||||||||||||
| "searchValue": searchValue, | ||||||||||||||||||||||
| "keyExpectedValue": msg.keyExpectedValue, | ||||||||||||||||||||||
| "keyActualValue": msg.keyActualValue, | ||||||||||||||||||||||
| "searchLine": build_search_line_for_apparmor(searchPath), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -62,24 +95,93 @@ CxPolicy[result] { | |||||||||||||||||||||
| expectedKey := sprintf("container.apparmor.security.beta.kubernetes.io/%s", [container]) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| not common_lib.valid_key(annotations, expectedKey) | ||||||||||||||||||||||
| not hasValidAppArmorProfileNewSyntax(document, x, c) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| annotationsPath := trim_left(sprintf("%s.annotations", [metadataInfo.path]), ".") | ||||||||||||||||||||||
| searchPath := get_apparmor_search_path(specInfo, x, c, annotationsPath) | ||||||||||||||||||||||
| msg := get_apparmor_messages(searchPath, metadata.name, container, "MissingAttribute", expectedKey, annotationsPath) | ||||||||||||||||||||||
| searchValue := get_apparmor_search_value(searchPath, document.kind, expectedKey, x, c, "MissingAttribute") | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| result := { | ||||||||||||||||||||||
| "documentId": document.id, | ||||||||||||||||||||||
| "resourceType": document.kind, | ||||||||||||||||||||||
| "resourceName": metadata.name, | ||||||||||||||||||||||
| "searchKey": sprintf("metadata.name={{%s}}.%s", [metadata.name, annotationsPath]), | ||||||||||||||||||||||
| "searchKey": sprintf("metadata.name={{%s}}.%s", [metadata.name, searchPath]), | ||||||||||||||||||||||
| "issueType": "MissingAttribute", | ||||||||||||||||||||||
| "searchValue": sprintf("%s%s%d", [document.kind, types[x], c]), # handle multiple kinds and container combination | ||||||||||||||||||||||
| "keyExpectedValue": sprintf("metadata.name={{%s}}.%s should specify an AppArmor profile for container {{%s}}", [metadata.name, annotationsPath, container]), | ||||||||||||||||||||||
| "keyActualValue": sprintf("metadata.name={{%s}}.%s does not specify an AppArmor profile for container {{%s}}", [metadata.name, annotationsPath, container]), | ||||||||||||||||||||||
| "searchLine": search_line_metadata(annotationsPath), | ||||||||||||||||||||||
| "searchValue": searchValue, | ||||||||||||||||||||||
| "keyExpectedValue": msg.keyExpectedValue, | ||||||||||||||||||||||
| "keyActualValue": msg.keyActualValue, | ||||||||||||||||||||||
| "searchLine": build_search_line_for_apparmor(searchPath), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| search_line_metadata(annotationsPath) = searchLine { | ||||||||||||||||||||||
| annotationsPath == "annotations" | ||||||||||||||||||||||
| searchLine := common_lib.build_search_line(["metadata", "annotations"], []) | ||||||||||||||||||||||
| # searchValue for similarity ID / result identity: use path when new syntax, else annotation-style value. | ||||||||||||||||||||||
| get_apparmor_search_value(searchPath, kind, expectedKey, typeKey, containerIndex, issueType) = v { | ||||||||||||||||||||||
| contains(searchPath, "securityContext") | ||||||||||||||||||||||
| v := sprintf("%s.%s", [kind, searchPath]) | ||||||||||||||||||||||
| } else = v { | ||||||||||||||||||||||
| issueType == "IncorrectValue" | ||||||||||||||||||||||
| v := sprintf("%s%s", [kind, expectedKey]) | ||||||||||||||||||||||
| } else = v { | ||||||||||||||||||||||
| v := sprintf("%s%s%d", [kind, typeKey, containerIndex]) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Returns keyExpectedValue and keyActualValue aligned with the path we point to (annotations vs securityContext). | ||||||||||||||||||||||
| get_apparmor_messages(searchPath, metadataName, container, issueType, expectedKey, annotationsPath) = msg { | ||||||||||||||||||||||
| contains(searchPath, "securityContext") | ||||||||||||||||||||||
| issueType == "MissingAttribute" | ||||||||||||||||||||||
| msg := { | ||||||||||||||||||||||
| "keyExpectedValue": sprintf("metadata.name={{%s}}.%s should specify an AppArmor profile (e.g. appArmorProfile.type: RuntimeDefault or Localhost) for container {{%s}}", [metadataName, searchPath, container]), | ||||||||||||||||||||||
| "keyActualValue": sprintf("metadata.name={{%s}}.%s does not specify an AppArmor profile for container {{%s}}", [metadataName, searchPath, container]), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } else = msg { | ||||||||||||||||||||||
| contains(searchPath, "securityContext") | ||||||||||||||||||||||
| issueType == "IncorrectValue" | ||||||||||||||||||||||
| msg := { | ||||||||||||||||||||||
| "keyExpectedValue": sprintf("metadata.name={{%s}}.%s.type should be set to 'RuntimeDefault' or 'Localhost'", [metadataName, searchPath]), | ||||||||||||||||||||||
| "keyActualValue": sprintf("metadata.name={{%s}}.%s.type does not specify a valid AppArmor profile (Unconfined is not accepted)", [metadataName, searchPath]), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } else = msg { | ||||||||||||||||||||||
| issueType == "MissingAttribute" | ||||||||||||||||||||||
| msg := { | ||||||||||||||||||||||
| "keyExpectedValue": sprintf("metadata.name={{%s}}.%s should specify an AppArmor profile for container {{%s}}", [metadataName, annotationsPath, container]), | ||||||||||||||||||||||
| "keyActualValue": sprintf("metadata.name={{%s}}.%s does not specify an AppArmor profile for container {{%s}}", [metadataName, annotationsPath, container]), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } else = msg { | ||||||||||||||||||||||
| msg := { | ||||||||||||||||||||||
| "keyExpectedValue": sprintf("metadata.name={{%s}}.%s[%s] should be set to 'runtime/default' or 'localhost'", [metadataName, annotationsPath, expectedKey]), | ||||||||||||||||||||||
| "keyActualValue": sprintf("metadata.name={{%s}}.%s[%s] does not specify a valid AppArmor profile", [metadataName, annotationsPath, expectedKey]), | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # build search path for apparmor profile | ||||||||||||||||||||||
| get_apparmor_search_path(specInfo, typeKey, containerIndex, annotationsPath) = path { | ||||||||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||||||||||||||||||||||
| container := specInfo.spec[typeKey][containerIndex] | ||||||||||||||||||||||
| common_lib.valid_key(container, "securityContext") | ||||||||||||||||||||||
| common_lib.valid_key(object.get(container, "securityContext", {}), "appArmorProfile") | ||||||||||||||||||||||
| path := sprintf("%s.%s.%d.securityContext.appArmorProfile", [specInfo.path, typeKey, containerIndex]) | ||||||||||||||||||||||
| } else = path { | ||||||||||||||||||||||
| container := specInfo.spec[typeKey][containerIndex] | ||||||||||||||||||||||
| common_lib.valid_key(container, "securityContext") | ||||||||||||||||||||||
| path := sprintf("%s.%s.%d.securityContext", [specInfo.path, typeKey, containerIndex]) | ||||||||||||||||||||||
| } else = path { | ||||||||||||||||||||||
| common_lib.valid_key(specInfo.spec, "securityContext") | ||||||||||||||||||||||
| common_lib.valid_key(object.get(specInfo.spec, "securityContext", {}), "appArmorProfile") | ||||||||||||||||||||||
| path := sprintf("%s.securityContext.appArmorProfile", [specInfo.path]) | ||||||||||||||||||||||
| } else = path { | ||||||||||||||||||||||
| common_lib.valid_key(specInfo.spec, "securityContext") | ||||||||||||||||||||||
| path := sprintf("%s.securityContext", [specInfo.path]) | ||||||||||||||||||||||
| } else = path { | ||||||||||||||||||||||
| path := annotationsPath | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # Builds search line for either annotation path (old syntax) or spec path (new syntax). | ||||||||||||||||||||||
| build_search_line_for_apparmor(path) = searchLine { | ||||||||||||||||||||||
| path == "annotations" | ||||||||||||||||||||||
| searchLine := common_lib.build_search_line(["metadata", "annotations"], []) | ||||||||||||||||||||||
| } else = searchLine { | ||||||||||||||||||||||
| contains(path, "securityContext") | ||||||||||||||||||||||
| searchLine := common_lib.build_search_line(split(path, "."), []) | ||||||||||||||||||||||
| } else = searchLine { | ||||||||||||||||||||||
| searchLine := common_lib.build_search_line(split(annotationsPath, "."), []) | ||||||||||||||||||||||
| searchLine := common_lib.build_search_line(split(path, "."), []) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+182
to
187
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||
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
26 changes: 26 additions & 0 deletions
26
assets/queries/k8s/missing_app_armor_config/test/negative2.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,26 @@ | ||
| # New Kubernetes syntax: AppArmor via securityContext.appArmorProfile (no annotations) | ||
| apiVersion: v1 | ||
| kind: Pod | ||
| metadata: | ||
| name: hello-apparmor-runtime-default | ||
| spec: | ||
| securityContext: | ||
| appArmorProfile: | ||
| type: RuntimeDefault | ||
| containers: | ||
| - name: hello | ||
| image: busybox | ||
| command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ] | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Pod | ||
| metadata: | ||
| name: hello-apparmor-runtime-default2 | ||
| spec: | ||
| securityContext: | ||
| appArmorProfile: | ||
| type: Localhost | ||
| containers: | ||
| - name: hello | ||
| image: busybox | ||
| command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ] |
File renamed without changes.
57 changes: 57 additions & 0 deletions
57
assets/queries/k8s/missing_app_armor_config/test/positive2.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,57 @@ | ||
| apiVersion: v1 | ||
| kind: Pod | ||
| metadata: | ||
| name: hello-apparmor-no-profile | ||
| spec: | ||
| securityContext: | ||
| runAsNonRoot: true | ||
| containers: | ||
| - name: hello | ||
| image: busybox | ||
| command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ] | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Pod | ||
| metadata: | ||
| name: hello-apparmor-empty-seccontext | ||
| spec: | ||
| securityContext: {} | ||
| containers: | ||
| - name: hello | ||
| image: busybox | ||
| command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ] | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Pod | ||
| metadata: | ||
| name: hello-apparmor-unconfined | ||
| spec: | ||
| securityContext: | ||
| appArmorProfile: | ||
| type: Unconfined | ||
| containers: | ||
| - name: hello | ||
| image: busybox | ||
| command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ] | ||
| --- | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: ubuntu-apparmor-no-profile | ||
| namespace: testns | ||
| spec: | ||
| replicas: 1 | ||
| selector: | ||
| matchLabels: | ||
| app: ubuntu | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: ubuntu | ||
| spec: | ||
| securityContext: | ||
| runAsNonRoot: true | ||
| containers: | ||
| - name: ubuntu-container | ||
| image: busybox | ||
| command: [ "sh", "-c", "sleep 1h" ] |
24 changes: 24 additions & 0 deletions
24
assets/queries/k8s/missing_app_armor_config/test/positive3.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,24 @@ | ||
| apiVersion: v1 | ||
| kind: Pod | ||
| metadata: | ||
| name: hello-apparmor-container-seccontext | ||
| spec: | ||
| containers: | ||
| - name: hello | ||
| image: busybox | ||
| securityContext: | ||
| runAsNonRoot: true | ||
| command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ] | ||
| --- | ||
| apiVersion: v1 | ||
| kind: Pod | ||
| metadata: | ||
| name: hello-apparmor-container-unconfined | ||
| spec: | ||
| containers: | ||
| - name: hello | ||
| image: busybox | ||
| securityContext: | ||
| appArmorProfile: | ||
| type: Unconfined | ||
| command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ] |
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.
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.