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

Exemption store #421

Merged
merged 10 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/pepr/operator/controllers/exemptions/exemption-store.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { beforeEach, describe, expect, it } from "@jest/globals";
import { Matcher, MatcherKind, Policy } from "../../crd";
import { ExemptionStore } from "./exemption-store";

const enforcerMatcher = {
namespace: "neuvector",
name: "^neuvector-enforcer-pod.*",
kind: MatcherKind.Pod,
};

const controllerMatcher = {
namespace: "neuvector",
name: "^neuvector-controller-pod.*",
kind: MatcherKind.Pod,
};

const getExemption = (uid: string, matcher: Matcher, policies: Policy[]) => {
return {
metadata: {
uid,
},
spec: {
exemptions: [
{
matcher,
policies,
},
],
},
};
};

describe("Exemption Store", () => {
beforeEach(() => {
ExemptionStore.init();
});

it("Add exemption", async () => {
const e = getExemption("uid", enforcerMatcher, [Policy.DisallowPrivileged]);
ExemptionStore.add(e);
const matchers = ExemptionStore.getByPolicy(Policy.DisallowPrivileged);

expect(matchers).toHaveLength(1);
});

it("Delete exemption", async () => {
const e = getExemption("uid", enforcerMatcher, [Policy.DisallowPrivileged]);
ExemptionStore.add(e);
let matchers = ExemptionStore.getByPolicy(Policy.DisallowPrivileged);
expect(matchers).toHaveLength(1);
ExemptionStore.remove(e);

matchers = ExemptionStore.getByPolicy(Policy.DisallowPrivileged);
expect(matchers).toHaveLength(0);
});

it("Update exemption", async () => {
const enforcerException = getExemption("uid", enforcerMatcher, [Policy.DisallowPrivileged]);
ExemptionStore.add(enforcerException);

let matchers = ExemptionStore.getByPolicy(Policy.DisallowPrivileged);
expect(matchers).toHaveLength(1);

const controllerExemption = getExemption("uid", controllerMatcher, [Policy.RequireNonRootUser]);
ExemptionStore.add(controllerExemption);

matchers = ExemptionStore.getByPolicy(Policy.DisallowPrivileged);
expect(matchers).toHaveLength(0);
});

it("Add multiple policies", async () => {
const enforcerException = getExemption("foo", enforcerMatcher, [Policy.DisallowPrivileged]);
ExemptionStore.add(enforcerException);

let matchers = ExemptionStore.getByPolicy(Policy.DisallowPrivileged);
expect(matchers).toHaveLength(1);

const controllerExemption = getExemption("bar", controllerMatcher, [Policy.RequireNonRootUser]);
ExemptionStore.add(controllerExemption);

matchers = ExemptionStore.getByPolicy(Policy.DisallowPrivileged);
expect(matchers).toHaveLength(1);

matchers = ExemptionStore.getByPolicy(Policy.RequireNonRootUser);
expect(matchers).toHaveLength(1);
});

it("Add duplicate exemptions owned by different owners", async () => {
const enforcerException = getExemption("foo", enforcerMatcher, [Policy.DisallowPrivileged]);
const otherEnforcerException = getExemption("bar", enforcerMatcher, [
Policy.DisallowPrivileged,
]);
ExemptionStore.add(enforcerException);
ExemptionStore.add(otherEnforcerException);

const matchers = ExemptionStore.getByPolicy(Policy.DisallowPrivileged);
expect(matchers).toHaveLength(2);
});
});
83 changes: 83 additions & 0 deletions src/pepr/operator/controllers/exemptions/exemption-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { Log } from "pepr";
import { StoredMatcher } from "../../../policies";
import { Matcher, Policy, UDSExemption } from "../../crd";

export type PolicyOwnerMap = Map<string, UDSExemption>;
export type PolicyMap = Map<Policy, StoredMatcher[]>;
let policyExemptionMap: PolicyMap;
let policyOwnerMap: PolicyOwnerMap;

function init(): void {
policyExemptionMap = new Map();
policyOwnerMap = new Map();
for (const p of Object.values(Policy)) {
policyExemptionMap.set(p, []);
}
}

function getByPolicy(policy: Policy): StoredMatcher[] {
return policyExemptionMap.get(policy) || [];
}

function setByPolicy(policy: Policy, matchers: StoredMatcher[]): void {
policyExemptionMap.set(policy, matchers);
}

function addMatcher(matcher: Matcher, p: Policy, owner: string = ""): void {
const storedMatcher = {
...matcher,
owner,
};

const storedMatchers = getByPolicy(p);
storedMatchers.push(storedMatcher);
}

// Iterate through each exemption block of CR and add matchers to PolicyMap
function add(exemption: UDSExemption, log: boolean = true) {
// Remove any existing exemption for this owner, in case of WatchPhase.Modified
remove(exemption);
const owner = exemption.metadata?.uid || "";
policyOwnerMap.set(owner, exemption);

for (const e of exemption.spec?.exemptions ?? []) {
const policies = e.policies ?? [];
for (const p of policies) {
// Append the matcher to the list of stored matchers for this policy
addMatcher(e.matcher, p, owner);
if (log) {
Log.debug(`Added exemption to ${p}: ${JSON.stringify(e.matcher)}`);
}
}
}
}

function remove(exemption: UDSExemption) {
const owner = exemption.metadata?.uid || "";
const prevExemption = policyOwnerMap.get(owner);

if (prevExemption) {
for (const e of prevExemption.spec?.exemptions ?? []) {
const policies = e.policies ?? [];
for (const p of policies) {
const existingMatchers = getByPolicy(p);
const filteredList = existingMatchers.filter(m => {
return m.owner !== owner;
});
setByPolicy(p, filteredList);
}
}
policyOwnerMap.delete(owner);
Log.debug(`Removed all policy exemptions for ${owner}`);
} else {
Log.debug(`No existing exemption for owner ${owner}`);
}
}

// export object with all included export as properties
export const ExemptionStore = {
init,
add,
remove,
getByPolicy,
};
Loading
Loading