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

fix: exemption race conditions #407

Merged
merged 44 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
3c570f0
fix: exemption store overwrites
TristanHoladay May 14, 2024
bb5ab32
Merge branch 'main' into fix-exemptions
TristanHoladay May 14, 2024
a0ff450
refactor to kfc watch
TristanHoladay May 15, 2024
b56875a
pepr format
TristanHoladay May 15, 2024
22b344b
update reconciler tests; added WatchPhase enum to exemptions.
TristanHoladay May 15, 2024
b0833c6
Merge branch 'main' into fix-exemptions
TristanHoladay May 15, 2024
dae141d
Update src/pepr/policies/index.ts
TristanHoladay May 15, 2024
6c49098
fix tests, clean up validator
TristanHoladay May 16, 2024
8fe77c7
refactor to exemption controller logic.
TristanHoladay May 16, 2024
2f867af
Merge branch 'main' into fix-exemptions
TristanHoladay May 16, 2024
3d9bcf2
format and lint
TristanHoladay May 16, 2024
8f8bd0a
adding logging to exemption controller; podinfo back to back exemptio…
TristanHoladay May 16, 2024
8a66cf2
add verify for podinfo in exempted-app test
TristanHoladay May 16, 2024
9e9055f
replacing console.log with Log
TristanHoladay May 17, 2024
c5a3da5
adding logging to isExempt() check
TristanHoladay May 17, 2024
5c64a9b
fix: tasks, schema [ci skip]
mjnagel May 17, 2024
033fdbf
wip: fix update issue
mjnagel May 17, 2024
3d01bf3
Merge branch 'main' into fix-exemptions
mjnagel May 17, 2024
c4d5cba
some more refactors
mjnagel May 17, 2024
f95f00e
use ramda for list equality/union
mjnagel May 17, 2024
53dad72
fix test
mjnagel May 18, 2024
065d171
Merge branch 'main' into fix-exemptions
TristanHoladay May 21, 2024
9aa1dba
Exemption store (#421)
rjferguson21 May 21, 2024
9e92245
Merge branch 'main' into fix-exemptions
mjnagel May 21, 2024
78a395c
rebase
mjnagel May 22, 2024
ae57973
Merge branch 'main' into fix-exemptions
mjnagel May 23, 2024
7dcc839
Merge branch 'main' into fix-exemptions
mjnagel May 23, 2024
890061b
Merge branch 'main' into fix-exemptions
mjnagel May 24, 2024
e25b971
Merge branch 'main' into fix-exemptions
mjnagel May 24, 2024
6e9085f
Merge branch 'main' into fix-exemptions
mjnagel May 24, 2024
b98ea0a
Merge branch 'main' into fix-exemptions
rjferguson21 May 28, 2024
b258e35
Merge branch 'main' into fix-exemptions
mjnagel May 28, 2024
d70fd03
Merge branch 'main' into fix-exemptions
mjnagel May 29, 2024
dda5de6
chore: move CRD apply to admission controller
mjnagel May 30, 2024
0c83b4d
Merge branch 'main' into fix-exemptions
mjnagel May 30, 2024
81e412e
chore: switch crd register to be more visible/timed well
mjnagel May 30, 2024
05176f0
chore: timing debug
mjnagel May 30, 2024
86a355e
pepr format
mjnagel May 30, 2024
7be93c8
Merge branch 'main' into fix-exemptions
mjnagel Jun 3, 2024
6d3adba
wip: iife CRD registration timing
mjnagel Jun 3, 2024
3f74d40
Merge branch 'main' into fix-exemptions
mjnagel Jun 4, 2024
b2c4e62
error handling
mjnagel Jun 4, 2024
c14e5ad
chore: peprstore cleanup
mjnagel Jun 5, 2024
5034cba
Merge branch 'main' into fix-exemptions
mjnagel Jun 5, 2024
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
34 changes: 22 additions & 12 deletions pepr.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import { PeprModule } from "pepr";
import { Log, PeprModule } from "pepr";

import cfg from "./package.json";

import { istio } from "./src/pepr/istio";
import { operator } from "./src/pepr/operator";
import { policies } from "./src/pepr/policies";
import { registerCRDs } from "./src/pepr/operator/crd/register";
import { policies, startExemptionWatch } from "./src/pepr/policies";
import { prometheus } from "./src/pepr/prometheus";

new PeprModule(cfg, [
// UDS Core Operator
operator,
(async () => {
// Apply the CRDs to the cluster
await registerCRDs();
// KFC watch for exemptions and update in-memory map
await startExemptionWatch();
new PeprModule(cfg, [
// UDS Core Operator
operator,

// UDS Core Policies
policies,
// UDS Core Policies
policies,

// Istio service mesh
istio,
// Istio service mesh
istio,

// Prometheus monitoring stack
prometheus,
]);
// Prometheus monitoring stack
prometheus,
]);
})().catch(err => {
Log.error(err);
process.exit(1);
});
3 changes: 0 additions & 3 deletions src/pepr/operator/common.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Capability } from "pepr";

// Register the CRD
import "./crd/register";

export const operator = new Capability({
name: "uds-core-operator",
description: "The UDS Operator is responsible for managing the lifecycle of UDS resources",
Expand Down
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