Skip to content
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

Add ephemeral containers to policies #191

Merged

Conversation

phillebaba
Copy link
Contributor

@phillebaba phillebaba commented Apr 13, 2022

This change adds ephemeral containers to all policies that currently check the containers and init containers field. It is kind of a quick solution just to bring all the policies to a level where ephemeral containers do not introduce security concerns.

I have on purpose skipped the required probes policy as it currently only looks at the containers field. I have also only updated one of the mutation samples.

One thing that comes to mind when looking at the different policies is that there are a bunch of different ways to solve this problem. A couple of rules have a method which returns the container data from each field, and others duplicate code at different levels. I feel like it would be good to find a standard. Right now it would be easy to make mistakes and not include one of the containers field. At the same time I can see a situation where one would want to require read only root file systems for the init containers and containers fields, but not the ephemeral containers fields. Might be better to solve these challenges in the future.

Fixes #188

@phillebaba phillebaba force-pushed the feature/ephemeral-containers branch from 9a18948 to 5600652 Compare April 13, 2022 20:27
@phillebaba
Copy link
Contributor Author

Currently having a look at the best way to implement tests for both init containers and ephemeral containers.

Copy link
Member

@ritazh ritazh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thanks for the PR @phillebaba! 🎉

@maxsmythe
Copy link
Contributor

Indeed, TY for the PR! LMK when you think you have the tests sorted out.

Rego tests can make it easy to stamp tests out. Adding them to suite.yaml might have a more uniform experience if you're trying to test them all in bulk.

@@ -172,6 +172,10 @@ spec:
general_violation[{"msg": msg, "field": "initContainers"}]
}

violation[{"msg": msg}] {
general_violation[{"msg": msg, "field": "ephemeralContainers"}]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may have one question
It is said Pod resource allocations are immutable, so setting resources is disallowed. in
https://kubernetes.io/docs/concepts/workloads/pods/ephemeral-containers/#what-is-an-ephemeral-container

So, do we need to add ephemeralContainer check in container limit policy?

Copy link
Contributor

@fseldow fseldow Apr 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so do the policy contaienrrequests and containerresourceratios

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fseldow good catch, you are right. It is not needed so will remove. Going to have a second look to see if there are more of these.

@phillebaba phillebaba force-pushed the feature/ephemeral-containers branch 2 times, most recently from efff6e4 to 0460431 Compare April 24, 2022 15:13
@maxsmythe
Copy link
Contributor

@phillebaba How's the testing looking?

@phillebaba phillebaba force-pushed the feature/ephemeral-containers branch 2 times, most recently from 8e23baa to e216525 Compare May 11, 2022 09:47
@phillebaba phillebaba force-pushed the feature/ephemeral-containers branch from e216525 to 7e16782 Compare May 11, 2022 10:22
Copy link
Contributor

@maxsmythe maxsmythe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for doing this!

@phillebaba
Copy link
Contributor Author

@maxsmythe sorry I got a bit carried away with the testing, but started facing issues as certain rules have multiple violations and other do not. Maybe something to look more into in the future. I added the easiest test cases that I could.

I just need to figure out why the integration tests are failing, do you have any insight into this?

@mac-chaffee
Copy link
Contributor

@phillebaba Looks like the kind cluster used in testing is using v1.21 where the ephemeralContainers field is just ignored, which is causing "disallowed" pods to still get created since the unknown field is stripped out before gatekeeper sees it.

I tested upgrading the kind cluster to v1.23 where ephemeralContainers are enabled, but that still doesn't work because spec.ephemeralContainers: Forbidden: cannot be set on create.

I think you just can't integration-test this feature until we refactor test/bats/test.bats. may want to leave that work for another PR.

@ritazh
Copy link
Member

ritazh commented May 13, 2022

To unblock this PR, perhaps remove the deployments with the ephemeral containers for now?

@phillebaba
Copy link
Contributor Author

The issue with removing the deployments using ephemeral containers is that it is part of the actual test. Could we not just update the kind version?

@mac-chaffee
Copy link
Contributor

@phillebaba even if you increase the kind version, the integration tests still fail because you can't "kubectl apply" a Pod with an ephemeralContainer in it: spec.ephemeralContainers: Forbidden: cannot be set on create.

@maxsmythe
Copy link
Contributor

To exempt these ephemeral objects from on-cluster testing, can we rename them to remove the example_ prefix?

for allowed in "$sample"/example_allowed*.yaml; do
if [[ -e "$allowed" ]]; then
# apply resource
run kubectl apply -f "$allowed"
assert_match 'created' "$output"
assert_success
# delete resource
kubectl delete --ignore-not-found -f "$allowed"
fi
done
for inventory in "$sample"/example_inventory*.yaml; do
if [[ -e "$inventory" ]]; then
run kubectl apply -f "$inventory"
assert_match 'created' "$output"
assert_success
fi
done
for disallowed in "$sample"/example_disallowed*.yaml; do
if [[ -e "$disallowed" ]]; then
# apply resource
run kubectl apply -f "$disallowed"
assert_match 'denied the request' "${output}"
assert_failure
# delete resource
kubectl delete --ignore-not-found -f "$disallowed"
fi
done

Then they will only be subject to OPA and gator verify tests

@ritazh
Copy link
Member

ritazh commented May 18, 2022

@phillebaba are you able to make the suggested changes above and in #197 so we can get this PR merged soon? Please let us know how we can help.

@ritazh ritazh merged commit 408222b into open-policy-agent:master May 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PSP Ephemeral Containers
6 participants