Skip to content

Commit

Permalink
chore: annotate mutations in policies (#236)
Browse files Browse the repository at this point in the history
## Description
Give users better information about when a resource is mutated by a
policy (e.g. Require Non Root User mutates security contexts if
undefined or `uds` labels are set).

## Related Issue

Resolves #212 

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [x] Other (security config, docs update, etc)

## Checklist before merging

- [ ] Test, docs, adr added or updated as needed
- [x] [Contributor Guide
Steps](https://github.com/defenseunicorns/uds-template-capability/blob/main/CONTRIBUTING.md)(https://github.com/defenseunicorns/uds-template-capability/blob/main/CONTRIBUTING.md#submitting-a-pull-request)
followed
  • Loading branch information
TristanHoladay committed Mar 8, 2024
1 parent 05c903e commit cc9db50
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/pepr/policies/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { V1SecurityContext, V1Container } from "@kubernetes/client-node";
import { KubernetesObject, V1Container, V1SecurityContext } from "@kubernetes/client-node";
import { Capability, PeprMutateRequest, PeprValidateRequest, a } from "pepr";
import { Policy } from "../operator/crd";

export type Ctx = {
name?: string;
Expand Down Expand Up @@ -90,3 +91,17 @@ export function isIstioInitContainer(
// If we get here, it's an istio init container
return true;
}

function transform(policy: Policy) {
return policy
.split(/(?=[A-Z])/)
.join("-")
.toLowerCase();
}

export function annotateMutation<T extends KubernetesObject>(
request: PeprMutateRequest<T>,
policy: Policy,
) {
request.SetAnnotation(`policies.uds.core/mutated`, transform(policy));
}
11 changes: 10 additions & 1 deletion src/pepr/policies/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { a } from "pepr";

import { V1SecurityContext } from "@kubernetes/client-node";
import { Policy } from "../operator/crd";
import { When, containers, securityContextContainers, securityContextMessage } from "./common";
import {
When,
annotateMutation,
containers,
securityContextContainers,
securityContextMessage,
} from "./common";
import { isExempt, markExemption } from "./exemptions";

/**
Expand Down Expand Up @@ -97,6 +103,8 @@ When(a.Pod)
if (pod.securityContext.runAsGroup === undefined) {
pod.securityContext.runAsGroup = 1000;
}

annotateMutation(request, Policy.RequireNonRootUser);
})
.Validate(request => {
if (isExempt(request, Policy.RequireNonRootUser)) {
Expand Down Expand Up @@ -316,6 +324,7 @@ When(a.Pod)
container.securityContext.capabilities = container.securityContext.capabilities || {};
container.securityContext.capabilities.drop = ["ALL"];
}
annotateMutation(request, Policy.DropAllCapabilities);
})
.Validate(request => {
if (isExempt(request, Policy.DropAllCapabilities)) {
Expand Down

0 comments on commit cc9db50

Please sign in to comment.