-
Notifications
You must be signed in to change notification settings - Fork 35
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
feat: Add logic for Kyverno policy recommendation #701
Merged
nyrahul
merged 1 commit into
accuknox:dev
from
Vyom-Yadav:addKyvernoPolicyRecommendation
Apr 12, 2023
Merged
feat: Add logic for Kyverno policy recommendation #701
nyrahul
merged 1 commit into
accuknox:dev
from
Vyom-Yadav:addKyvernoPolicyRecommendation
Apr 12, 2023
Conversation
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
d0dfd5f
to
1daa1ad
Compare
This was referenced Apr 1, 2023
Final recommended policy example: apiVersion: kyverno.io/v1
kind: Policy
metadata:
annotations:
policies.kyverno.io/minversion: 1.6.0
recommended-policies.kubearmor.io/description: Don't mount service account token
when it is not needed
recommended-policies.kubearmor.io/description-detailed: If the Service Account
Token is not used by a pod, then it should not be automounted. Service account
token provide access to the kubeapi-server which potentially increases the surface
area of attack.
recommended-policies.kubearmor.io/tags: AUTOMOUNT_SERVICE_ACCOUNT
recommended-policies.kubearmor.io/title: Restrict Auto-Mount of Service Account
Tokens
name: nginx-restrict-automount-sa-token
namespace: default
spec:
background: true
rules:
- match:
any:
- resources:
kinds:
- Deployment
name: validate-automountServiceAccountToken
preconditions:
all:
- key: '{{ request.operation || ''BACKGROUND'' }}'
operator: NotEquals
value: DELETE
- key: '{{ request.object.spec.template.metadata.labels.app || '''' }}'
operator: Equals
value: nginx
- key: '{{ request.object.spec.template.metadata.labels.foo || '''' }}'
operator: Equals
value: faa
validate:
message: Auto-mounting of Service Account tokens is not allowed.
pattern:
spec:
template:
spec:
automountServiceAccountToken: "false"
validationFailureAction: Audit
|
1daa1ad
to
9895eef
Compare
nyrahul
requested changes
Apr 1, 2023
789480f
to
16a939e
Compare
nyrahul
approved these changes
Apr 5, 2023
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.
Awesome PR. LGTM. 👍
Signed-off-by: Vyom-Yadav <jackhammervyom@gmail.com>
16a939e
to
b5b01b6
Compare
vishnusomank
approved these changes
Apr 12, 2023
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.
LGTM!
nyrahul
approved these changes
Apr 12, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Signed-off-by: Vyom-Yadav jackhammervyom@gmail.com
Kyverno policy recommendation is spread across 3 PRs including this one:
Explanation:
Kyverno policy recommendation dynamically recommends Kyverno policies based on container runtime data.
The dynamically recommended policy right now:
How it works:
restrict-automount-sa-token
is generated and inserted into the DB.Nuances:
policy-templates
matchesPod
s, but we generate a policy forDeployment
s.Kyverno autogen does not work when label selectors are used, see kyverno/kyverno#4410.
Since we right now only recommend policies for Deployments (this logic will be changed when we shift to owner references),
autogen
for conversion fromPod
->Deployment
is internally implemented. This can be easily extended for other owner references too if Kyverno does not supportautogen
in the future too.policy_yaml
DB.As internally discussed, we will be adding policies to
policy_yaml
so we can extract policies from a single place. Other generated policies are right now added to bothpolicy_yaml
and other DB's likesystem_policy
,network_policy
, etc. The worker protobuf fetches system policies fromsystem_policy
, network policies fromnetwork_policy
, and admission controller policies frompolicy_yaml
table. There is noadmission_controller_policy table
.The worker cannot stop or start the admission controller policy recommendation since it is a part of recommend cronjob. The recommend cronjob is automatically started when DE starts.
..[0-9]{4}_[0-9]{2}_[0-9]{2}.*
:This is a known k8s feature/issue, read more about it at https://stackoverflow.com/q/50685385/15412365. Since KubeArmor resolved symlinks, this is required to match
VolumeMount
path.Sometimes when SA account token is accessed at
/var/run/secrets/kubernetes.io/serviceaccount/token
, the path shown in summary is/run/secrets/kubernetes.io/serviceaccount/..2023_03_28_12_23_09.412453730/token
. This is most likely a bug in symlink resolution, the correct path while resolving symlinks should be/var/run/secrets/kubernetes.io/serviceaccount/..2023_03_28_12_23_09.412453730/token
. Read the discussion at https://accuknox.slack.com/archives/C0229TD83RA/p1680013050747849Process of merging PRs:
kubearmor-client
PR.Testing: