-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: address network policy generation inter-namespace bug (#564)
## Description A bug exists when combining `remoteNamespace` and `remoteSelector` in a single network.allow policy. The generated network policy results in two separate peers. ## Related Issue Fixes #528 ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [] Test, docs, adr added or updated as needed - [ ] [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 --------- Co-authored-by: Rob Ferguson <rjferguson21@gmail.com>
- Loading branch information
1 parent
235702e
commit 9b14c2c
Showing
2 changed files
with
166 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { describe, expect, it } from "@jest/globals"; | ||
import { kind } from "pepr"; | ||
import { Direction } from "../../crd"; | ||
import { generate } from "./generate"; | ||
|
||
describe("network policy generate", () => { | ||
it("should generate correct network policy", async () => { | ||
const policy = generate("test", { | ||
description: "test", | ||
direction: Direction.Ingress, | ||
selector: { app: "test" }, | ||
remoteNamespace: "foo", | ||
remoteSelector: { app: "foo" }, | ||
}); | ||
|
||
expect(policy.metadata?.name).toEqual("Ingress-test"); | ||
expect(policy.spec).toEqual({ | ||
ingress: [ | ||
{ | ||
from: [ | ||
{ | ||
namespaceSelector: { | ||
matchLabels: { | ||
"kubernetes.io/metadata.name": "foo", | ||
}, | ||
}, | ||
podSelector: { | ||
matchLabels: { | ||
app: "foo", | ||
}, | ||
}, | ||
}, | ||
], | ||
ports: [], | ||
}, | ||
], | ||
podSelector: { matchLabels: { app: "test" } }, | ||
policyTypes: ["Ingress"], | ||
} as kind.NetworkPolicy["spec"]); | ||
}); | ||
}); | ||
|
||
describe("network policy generate", () => { | ||
it("should generate correct network policy for just remoteNamespace", async () => { | ||
const policy = generate("test", { | ||
description: "test", | ||
direction: Direction.Ingress, | ||
selector: { app: "test" }, | ||
remoteNamespace: "foo", | ||
}); | ||
|
||
expect(policy.metadata?.name).toEqual("Ingress-test"); | ||
expect(policy.spec).toEqual({ | ||
ingress: [ | ||
{ | ||
from: [ | ||
{ | ||
namespaceSelector: { | ||
matchLabels: { | ||
"kubernetes.io/metadata.name": "foo", | ||
}, | ||
}, | ||
}, | ||
], | ||
ports: [], | ||
}, | ||
], | ||
podSelector: { matchLabels: { app: "test" } }, | ||
policyTypes: ["Ingress"], | ||
} as kind.NetworkPolicy["spec"]); | ||
}); | ||
}); | ||
|
||
describe("network policy generate", () => { | ||
it("should generate correct network policy for empty string and wildcard remoteNamespace", async () => { | ||
const policy = generate("test", { | ||
description: "test", | ||
direction: Direction.Egress, | ||
selector: { app: "test" }, | ||
remoteNamespace: "", | ||
}); | ||
|
||
expect(policy.metadata?.name).toEqual("Egress-test"); | ||
expect(policy.spec).toEqual({ | ||
egress: [ | ||
{ | ||
ports: [], | ||
to: [{ namespaceSelector: {} }], | ||
}, | ||
], | ||
podSelector: { matchLabels: { app: "test" } }, | ||
policyTypes: ["Egress"], | ||
} as kind.NetworkPolicy["spec"]); | ||
}); | ||
|
||
const policyWildcard = generate("test", { | ||
description: "test", | ||
direction: Direction.Egress, | ||
selector: { app: "test" }, | ||
remoteNamespace: "*", | ||
}); | ||
|
||
expect(policyWildcard.spec).toEqual({ | ||
egress: [ | ||
{ | ||
ports: [], | ||
to: [{ namespaceSelector: {} }], | ||
}, | ||
], | ||
podSelector: { matchLabels: { app: "test" } }, | ||
policyTypes: ["Egress"], | ||
} as kind.NetworkPolicy["spec"]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters