Skip to content

Commit

Permalink
Ensure that at least one character is logged in regex matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
samayer12 committed Oct 29, 2024
1 parent f1d94ff commit 048e707
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/lib/filter/adjudicators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const carriesDeletionTimestamp = pipe(
);
export const missingDeletionTimestamp = complement(carriesDeletionTimestamp);

export const carriedKind = pipe(kubernetesObject => kubernetesObject?.metadata?.kind, defaultTo("not set"));
export const carriedVersion = pipe(kubernetesObject => kubernetesObject?.metadata?.version, defaultTo("not set"));
export const carriedName = pipe(kubernetesObject => kubernetesObject?.metadata?.name, defaultTo(""));
export const carriesName = pipe(carriedName, equals(""), not);
export const missingName = complement(carriesName);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/filter/logMessages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { commonLogMessage } from "./logMessages";
it("should display the default log message when the subject is unsupported", () => {
const result = commonLogMessage("not-supported", ["some input"], ["some criteria"]);
expect(result).toMatch(
/Ignoring Admission Callback: An undefined logging condition occurred. Filter input was '.*' and Filter criteria was '.*'/,
/Ignoring Admission Callback: An undefined logging condition occurred. Filter input was '.+' and Filter criteria was '.+'/,
);
});
6 changes: 4 additions & 2 deletions src/lib/filter/logMessages.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { FilterInput } from "../types";
import {
carriedKind,
carriedName,
carriedNamespace,
carriedVersion,
definedAnnotations,
definedGroup,
definedKind,
Expand Down Expand Up @@ -45,9 +47,9 @@ const getBindingAdmissionRequestMessage = (subject: string, filterInput: FilterI
case "event":
return `${prefix} Binding defines ${subject} '${definedKind(filterInput)}' but Request does not declare it.`;
case "version":
return `${prefix} Binding defines ${subject} '${definedVersion(filterInput)}' but Request declares '${carriedName(filterCriteria)}'.`;
return `${prefix} Binding defines ${subject} '${definedVersion(filterInput)}' but Request declares '${carriedVersion(filterCriteria)}'.`;
case "kind":
return `${prefix} Binding defines ${subject} '${definedKind(filterInput)}' but Request declares '${carriedName(filterCriteria)}'.`;
return `${prefix} Binding defines ${subject} '${definedKind(filterInput)}' but Request declares '${carriedKind(filterCriteria)}'.`;
default:
return getUndefinedLoggingConditionMessage(subject, filterInput, filterCriteria);

Check warning on line 54 in src/lib/filter/logMessages.ts

View check run for this annotation

Codecov / codecov/patch

src/lib/filter/logMessages.ts#L53-L54

Added lines #L53 - L54 were not covered by tests
}
Expand Down
32 changes: 16 additions & 16 deletions src/lib/filter/shouldSkipRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe("when a pod is created", () => {
it("should reject when regex name does not match", () => {
const pod = CreatePod();
expect(shouldSkipRequest(defaultBinding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines name regex '.*' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines name regex '.+' but Object carries '.+'./,
);
});

Expand Down Expand Up @@ -154,7 +154,7 @@ describe("when a pod is created", () => {
const binding = { ...defaultBinding, filters };
const pod = CreatePod();
expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines namespace regexes '.*' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines namespace regexes '.+' but Object carries '.+'./,
);
});
it("should not reject when namespace is not ignored", () => {
Expand All @@ -168,7 +168,7 @@ describe("when a pod is created", () => {
const binding = { ...defaultBinding, filters };
const pod = CreatePod();
expect(shouldSkipRequest(binding, pod, [], ["helm-releasename"])).toMatch(
/Ignoring Admission Callback: Object carries namespace '.*' but ignored namespaces include '.*'./,
/Ignoring Admission Callback: Object carries namespace '.+' but ignored namespaces include '.+'./,
);
});
});
Expand All @@ -179,7 +179,7 @@ describe("when a pod is deleted", () => {
const binding = { ...defaultBinding, filters };
const pod = DeletePod();
expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines name regex '.*' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines name regex '.+' but Object carries '.+'./,
);
});

Expand All @@ -198,7 +198,7 @@ describe("when a pod is deleted", () => {
};
const pod = DeletePod();
expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines namespace regexes '.*' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines namespace regexes '.+' but Object carries '.+'./,
);
});

Expand Down Expand Up @@ -227,7 +227,7 @@ describe("when a pod is deleted", () => {
};
const pod = DeletePod();
expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines name '.*' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines name '.+' but Object carries '.+'./,
);
});

Expand All @@ -239,7 +239,7 @@ describe("when a pod is deleted", () => {
};
const pod = DeletePod();
expect(shouldSkipRequest(binding, pod, [], ["helm-releasename"])).toMatch(
/Ignoring Admission Callback: Object carries namespace '.*' but ignored namespaces include '.*'./,
/Ignoring Admission Callback: Object carries namespace '.+' but ignored namespaces include '.+'./,
);
});

Expand Down Expand Up @@ -270,7 +270,7 @@ it("should reject when kind does not match", () => {
const pod = CreatePod();

expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines kind '.*' but Request declares '.*'./,
/Ignoring Admission Callback: Binding defines kind '.+' but Request declares 'not set'./,
);
});

Expand All @@ -289,7 +289,7 @@ it("should reject when group does not match", () => {
const pod = CreatePod();

expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines group '.*' but Request declares '.*'./,
/Ignoring Admission Callback: Binding defines group '.+' but Request declares '.+'./,
);
});

Expand All @@ -308,7 +308,7 @@ it("should reject when version does not match", () => {
const pod = CreatePod();

expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines version '.*' but Request declares '.*'./,
/Ignoring Admission Callback: Binding defines version '.+' but Request declares '.+'./,
);
});

Expand Down Expand Up @@ -338,7 +338,7 @@ it("should reject when the capability namespace does not match", () => {
const pod = CreatePod();

expect(shouldSkipRequest(binding, pod, ["bleh", "bleh2"])).toMatch(
/Ignoring Admission Callback: Object carries namespace '.*' but namespaces allowed by Capability are '.*'./,
/Ignoring Admission Callback: Object carries namespace '.+' but namespaces allowed by Capability are '.+'./,
);
});

Expand All @@ -348,7 +348,7 @@ it("should reject when namespace does not match", () => {
const pod = CreatePod();

expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines namespaces '.*' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines namespaces '.+' but Object carries '.+'./,
);
});

Expand Down Expand Up @@ -385,7 +385,7 @@ it("should reject when label does not match", () => {
const pod = CreatePod();

expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines labels '.*' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines labels '.+' but Object carries '.+'./,
);
});

Expand Down Expand Up @@ -429,7 +429,7 @@ it("should reject when annotation does not match", () => {
const pod = CreatePod();

expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines annotations '.*' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines annotations '.+' but Object carries '.+'./,
);
});

Expand Down Expand Up @@ -549,13 +549,13 @@ describe("when multiple filters are triggered", () => {
it("should display the failure message for the first matching filter", () => {
const pod = CreatePod();
expect(shouldSkipRequest(binding, pod, [])).toMatch(
/Ignoring Admission Callback: Binding defines name 'not-a-match' but Object carries '.*'/,
/Ignoring Admission Callback: Binding defines name 'not-a-match' but Object carries '.+'./,
);
});
it("should NOT display the failure message for the second matching filter", () => {
const pod = CreatePod();
expect(shouldSkipRequest(binding, pod, [])).not.toMatch(
/Ignoring Admission Callback: Binding defines namespaces 'not-allowed,also-not-matching' but Object carries '.*'./,
/Ignoring Admission Callback: Binding defines namespaces 'not-allowed,also-not-matching' but Object carries '.+'./,
);
});
it("should NOT display the failure message for the third matching filter", () => {
Expand Down

0 comments on commit 048e707

Please sign in to comment.