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: fix mapping of validation action for synchronized bindings #25

Merged
merged 2 commits into from
Jan 4, 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
16 changes: 15 additions & 1 deletion admission-controller/synchronizer/src/utils/policy-updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class PolicyUpdater {

return {
policyName: binding.policy.id,
validationActions: ['Warn'],
validationActions: [this.mapValidationAction(binding.action)],
matchResources: {
namespaceSelector: {
matchExpressions: [{
Expand All @@ -239,4 +239,18 @@ export class PolicyUpdater {

return _.isEqual(binding1Copy, binding2Copy);
}

protected mapValidationAction(action: string) {
const actionNormalized = (action || '').toLowerCase().trim();

switch (actionNormalized) {
case 'warn':
return 'Warn';
case 'deny':
return 'Deny';
default:
this._logger.error({ msg: 'Unknown validation action.', action });
return action;
}
}
}
2 changes: 2 additions & 0 deletions admission-controller/synchronizer/src/utils/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type ClusterQueryResponseBindingPolicy = {
export type ClusterQueryResponseBinding = {
id: string
mode: 'ALLOW_LIST' | 'BLOCK_LIST'
action: 'warn' | 'deny'
namespaces: string[]
policy: ClusterQueryResponseBindingPolicy
};
Expand Down Expand Up @@ -53,6 +54,7 @@ export const getClusterQuery = `
bindings {
id
mode
action
namespaces
policy {
id
Expand Down
15 changes: 15 additions & 0 deletions tests/src/cloud.e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ describe(`Cloud (dir: ${mainDir})`, () => {
assertResource(policy2, 'cluster-1-binding-2-policy');
}, 45 * 1000);

it('correctly maps deny action', async () => {
mockServer = await startMockServer('actionDeny');

// Wait for getCluster query to run.
await waitForRequests(mockServer, 2);
// Wait for CRDs propagation.
await sleep(500);

const policy1 = await run('kubectl get monoklepolicy.monokle.io/cluster-1-binding-1-policy -o yaml');
const binding1 = await run('kubectl get monoklepolicybinding.monokle.io/cluster-1-binding-1-deny -o yaml');

assertResource(binding1, 'cluster-1-binding-1-deny');
assertResource(policy1, 'cluster-1-binding-1-policy');
}, 45 * 1000);

// @TODO updates policy CRDs with new data
// @TODO deletes policy CRDs
// @TODO updates binding CRDs with new data
Expand Down
20 changes: 20 additions & 0 deletions tests/src/utils/expected-crds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ export const EXPECTED_CRDS: Record<string, any> = {
}
}
},
'cluster-1-binding-1-deny': {
apiVersion: 'monokle.io/v1alpha1',
kind: 'MonoklePolicyBinding',
metadata: {
name: 'cluster-1-binding-1-deny'
},
spec: {
policyName: 'cluster-1-binding-1-policy',
validationActions: ['Deny'],
matchResources: {
namespaceSelector: {
matchExpressions: [{
key: 'name',
operator: 'In',
values: ['my-namespace-0'],
}]
}
}
}
},
'cluster-1-binding-2': {
apiVersion: 'monokle.io/v1alpha1',
kind: 'MonoklePolicyBinding',
Expand Down
33 changes: 33 additions & 0 deletions tests/src/utils/response-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const RESPONSE_MOCK: Record<string, any> = {
{
id: "cluster-1-binding-1",
mode: "ALLOW_LIST",
action: "warn",
namespaces: ["ns-0","ns-1"],
policy: {
id: "cluster-1-binding-1-policy",
Expand All @@ -58,6 +59,7 @@ export const RESPONSE_MOCK: Record<string, any> = {
{
id: "cluster-1-binding-2",
mode: "ALLOW_LIST",
action: "warn",
namespaces: ["ns-2","ns-1"],
policy: {
id: "cluster-1-binding-2-policy",
Expand All @@ -71,5 +73,36 @@ export const RESPONSE_MOCK: Record<string, any> = {
]
}
}
},
actionDeny: {
data: {
getCluster: {
id: "cluster-1",
name: "Cluster 1",
namespaceSync: true,
namespaces: [
{
id: "ns-0",
name: "my-namespace-0"
}
],
bindings: [
{
id: "cluster-1-binding-1-deny",
mode: "ALLOW_LIST",
action: "deny",
namespaces: ["ns-0"],
policy: {
id: "cluster-1-binding-1-policy",
content: "plugins:\n open-policy-agent: true\n pod-security-standards: true\n",
project: {
id: "cluster-1-binding-1-policy-project",
name: "cluster-1-binding-1-policy-project"
}
}
}
]
}
}
}
}
2 changes: 1 addition & 1 deletion tests/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import cors from 'cors';
import _ from 'lodash';
import {RESPONSE_MOCK} from './response-mocks.js';

type ResponseMockName = 'empty' | 'emptySync' | 'dataAllow' | 'dataBlock';
type ResponseMockName = 'empty' | 'emptySync' | 'dataAllow' | 'actionDeny';

type MockServer = {
server: Server;
Expand Down
Loading