Skip to content

Commit

Permalink
[Cloud Posture] update CSP rules configuration template
Browse files Browse the repository at this point in the history
  • Loading branch information
CohenIdo authored May 12, 2022
1 parent 6af6863 commit a10d698
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import { schema as rt, TypeOf } from '@kbn/config-schema';

export const cspRulesConfigSchema = rt.object({
activated_rules: rt.object({
cis_k8s: rt.arrayOf(rt.string()),
data_yaml: rt.object({
activated_rules: rt.object({
cis_k8s: rt.arrayOf(rt.string()),
}),
}),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
httpServerMock,
} from '@kbn/core/server/mocks';
import {
convertRulesConfigToYaml,
createRulesConfig,
defineUpdateRulesConfigRoute,
getCspRules,
Expand Down Expand Up @@ -144,7 +143,9 @@ describe('Update rules configuration API', () => {
],
} as unknown as SavedObjectsFindResponse<CspRuleSchema>;
const cspConfig = await createRulesConfig(cspRules);
expect(cspConfig).toMatchObject({ activated_rules: { cis_k8s: ['cis_1_1_1', 'cis_1_1_3'] } });
expect(cspConfig).toMatchObject({
data_yaml: { activated_rules: { cis_k8s: ['cis_1_1_1', 'cis_1_1_3'] } },
});
});

it('create empty csp rules config when all rules are disabled', async () => {
Expand All @@ -169,21 +170,13 @@ describe('Update rules configuration API', () => {
],
} as unknown as SavedObjectsFindResponse<CspRuleSchema>;
const cspConfig = await createRulesConfig(cspRules);
expect(cspConfig).toMatchObject({ activated_rules: { cis_k8s: [] } });
});

it('validate converting rules config object to Yaml', async () => {
const cspRuleConfig = { activated_rules: { cis_k8s: ['1.1.1', '1.1.2'] } };

const dataYaml = convertRulesConfigToYaml(cspRuleConfig);

expect(dataYaml).toEqual('activated_rules:\n cis_k8s:\n - 1.1.1\n - 1.1.2\n');
expect(cspConfig).toMatchObject({ data_yaml: { activated_rules: { cis_k8s: [] } } });
});

it('validate adding new data.yaml to package policy instance', async () => {
const packagePolicy = createPackagePolicyMock();

const dataYaml = 'activated_rules:\n cis_k8s:\n - 1.1.1\n - 1.1.2\n';
const dataYaml = 'data_yaml:\n activated_rules:\n cis_k8s:\n - 1.1.1\n - 1.1.2\n';
const updatedPackagePolicy = setVarToPackagePolicy(packagePolicy, dataYaml);

expect(updatedPackagePolicy.vars).toEqual({ dataYaml: { type: 'config', value: dataYaml } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ export const createRulesConfig = (
): CspRulesConfigSchema => {
const activatedRules = cspRules.saved_objects.filter((cspRule) => cspRule.attributes.enabled);
const config = {
activated_rules: {
cis_k8s: activatedRules.map((activatedRule) => activatedRule.attributes.rego_rule_id),
data_yaml: {
activated_rules: {
cis_k8s: activatedRules.map((activatedRule) => activatedRule.attributes.rego_rule_id),
},
},
};
return config;
Expand Down

0 comments on commit a10d698

Please sign in to comment.