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

chore: refactor complex validation processing logic #1382

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"build": "tsc && node build.mjs && npm pack",
"build:image": "npm run build && docker buildx build --output type=docker --tag pepr:dev .",
"test": "npm run test:unit && npm run test:journey",
"test:unit": "npm run gen-data-json && jest src --coverage --detectOpenHandles --coverageDirectory=./coverage",
"test:unit": "npm run gen-data-json && jest src --coverage --detectOpenHandles --coverageDirectory=./coverage --testPathIgnorePatterns='cosign.e2e.test.ts'",
"test:journey": "npm run test:journey:k3d && npm run build && npm run test:journey:image && npm run test:journey:run",
"test:journey:prep": "if [ ! -d ./pepr-upgrade-test ]; then git clone https://github.com/defenseunicorns/pepr-upgrade-test.git ; fi",
"test:journey-wasm": "npm run test:journey:k3d && npm run build && npm run test:journey:image && npm run test:journey:run-wasm",
Expand Down
74 changes: 74 additions & 0 deletions src/lib/validate-processor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, expect, it } from "@jest/globals";
import { validateProcessor } from "./validate-processor";
import { Capability } from "./capability";
import { KubernetesObject } from "kubernetes-fluent-client";
import { AdmissionRequest, CapabilityCfg } from "./types";
import { Operation } from "./enums";

describe("validate-processor tests", () => {
const defaultCapabilityConfig: CapabilityCfg = {
name: "test-capability",
description: "Test capability description",
namespaces: ["default"],
};

const defaultModuleConfig = { uuid: "some-uuid", alwaysIgnore: { namespaces: [] } };
const defaultCapabilities: Capability[] = [new Capability(defaultCapabilityConfig)];
const defaultRequestMetadata = {};
const defaultKind = {
group: "",
version: "v1",
kind: "Pod",
};
const defaultRequest: AdmissionRequest<KubernetesObject> = {
operation: Operation.CREATE,
uid: "test-uid",
kind: defaultKind,
resource: {
group: "",
version: "v1",
resource: "pods",
},
name: "test-pod",
userInfo: {
username: "test-user",
groups: ["test-group"],
},
object: {
apiVersion: "v1",
kind: "Pod",
metadata: {
name: "test-pod",
labels: {
"test-label": "true",
},
annotations: {
"test-annotation": "true",
},
},
},
};

it("should return an empty validate response", async () => {
const result = await validateProcessor(
defaultModuleConfig,
defaultCapabilities,
defaultRequest,
defaultRequestMetadata,
);
expect(result).toStrictEqual([]);
});

it("TODO: should do something when secret", async () => {
const request = { ...defaultRequest, kind: { group: "", kind: "Secret", version: "v1" } };
const result = await validateProcessor(defaultModuleConfig, defaultCapabilities, request, defaultRequestMetadata);
expect(result).toStrictEqual([]);
});

it("TODO should do something with bindings", async () => {
const capabilities: Capability[] = [new Capability({ ...defaultCapabilityConfig })];
const request = { ...defaultRequest, kind: { group: "", kind: "Secret", version: "v1" } };
const result = await validateProcessor(defaultModuleConfig, capabilities, request, defaultRequestMetadata);
expect(result).toStrictEqual([]);
});
});
10 changes: 5 additions & 5 deletions src/lib/validate-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
for (const { name, bindings, namespaces } of capabilities) {
const actionMetadata = { ...reqMetadata, name };

for (const action of bindings) {
for (const binding of bindings) {
// Skip this action if it's not a validation action
if (!action.validateCallback) {
if (!binding.validateCallback) {
continue;
}

Expand All @@ -44,18 +44,18 @@
};

// Continue to the next action without doing anything if this one should be skipped
const shouldSkip = shouldSkipRequest(action, req, namespaces, config?.alwaysIgnore?.namespaces);
const shouldSkip = shouldSkipRequest(binding, req, namespaces, config?.alwaysIgnore?.namespaces);

Check warning on line 47 in src/lib/validate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/validate-processor.ts#L47

Added line #L47 was not covered by tests
if (shouldSkip !== "") {
Log.debug(shouldSkip);
continue;
}

const label = action.validateCallback.name;
const label = binding.validateCallback.name;

Check warning on line 53 in src/lib/validate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/validate-processor.ts#L53

Added line #L53 was not covered by tests
Log.info(actionMetadata, `Processing validation action (${label})`);

try {
// Run the validation callback, if it fails set allowed to false
const resp = await action.validateCallback(wrapped);
const resp = await binding.validateCallback(wrapped);

Check warning on line 58 in src/lib/validate-processor.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/validate-processor.ts#L58

Added line #L58 was not covered by tests
localResponse.allowed = resp.allowed;

// If the validation callback returned a status code or message, set it in the Response
Expand Down
File renamed without changes.
Loading