diff --git a/ee/app/assets/javascripts/threat_monitoring/components/constants.js b/ee/app/assets/javascripts/threat_monitoring/components/constants.js index b40d4f846f97ad45e85568f53c155a81b40a914b..f02e652bcdd90e85ae21e41f5d335a0934b41871 100644 --- a/ee/app/assets/javascripts/threat_monitoring/components/constants.js +++ b/ee/app/assets/javascripts/threat_monitoring/components/constants.js @@ -25,21 +25,22 @@ export { dateFormats as DATE_FORMATS } from '~/analytics/shared/constants'; export const POLICY_TYPE_COMPONENT_OPTIONS = { container: { component: 'network-policy-editor', + kind: { + cilium: 'CiliumNetworkPolicy', + network: 'NetworkPolicy', + }, shouldShowEnvironmentPicker: true, text: s__('SecurityOrchestration|Network'), + typeName: 'NetworkPolicy', urlParameter: 'container_policy', value: 'container', - yamlIndicator: { - cilium: 'CiliumNetworkPolicy', - network: 'NetworkPolicy', - }, }, scanExecution: { component: 'scan-execution-policy-editor', text: s__('SecurityOrchestration|Scan Execution'), + typeName: 'ScanExecutionPolicy', urlParameter: 'scan_execution_policy', value: 'scanExecution', - yamlIndicator: 'scanner_profile', }, }; diff --git a/ee/app/assets/javascripts/threat_monitoring/components/policies/policies_list.vue b/ee/app/assets/javascripts/threat_monitoring/components/policies/policies_list.vue index c14a5884bf731d6ab215e26f3b4c598105475833..01cc7a4e35fcd0161c3357382634ebab4f4072ec 100644 --- a/ee/app/assets/javascripts/threat_monitoring/components/policies/policies_list.vue +++ b/ee/app/assets/javascripts/threat_monitoring/components/policies/policies_list.vue @@ -167,7 +167,8 @@ export default { return ''; }, policyType() { - return this.selectedPolicy ? getPolicyType(this.selectedPolicy.yaml) : 'container'; + // eslint-disable-next-line no-underscore-dangle + return this.selectedPolicy ? getPolicyType(this.selectedPolicy.__typename) : 'container'; }, hasExistingPolicies() { return !(this.selectedPolicyType === POLICY_TYPE_OPTIONS.ALL.value && !this.policies.length); diff --git a/ee/app/assets/javascripts/threat_monitoring/components/policy_editor/network_policy/lib/to_yaml.js b/ee/app/assets/javascripts/threat_monitoring/components/policy_editor/network_policy/lib/to_yaml.js index 38a4e514ce6ed13a0bbbf40d71a95851146b14a7..d4c256671ca8a4581fa65ff54300d9c77d134c40 100644 --- a/ee/app/assets/javascripts/threat_monitoring/components/policy_editor/network_policy/lib/to_yaml.js +++ b/ee/app/assets/javascripts/threat_monitoring/components/policy_editor/network_policy/lib/to_yaml.js @@ -48,7 +48,7 @@ export default function toYaml(policy) { const policySpec = { apiVersion: 'cilium.io/v2', - kind: POLICY_TYPE_COMPONENT_OPTIONS.container.yamlIndicator.cilium, + kind: POLICY_TYPE_COMPONENT_OPTIONS.container.kind.cilium, }; if (description?.length > 0) { diff --git a/ee/app/assets/javascripts/threat_monitoring/components/policy_list.vue b/ee/app/assets/javascripts/threat_monitoring/components/policy_list.vue index ea9c63333b610f99701aaf2beaa6efaf19d4401f..ffbf5f679afc5e3a59d7e3e75e0a37c789b602cf 100644 --- a/ee/app/assets/javascripts/threat_monitoring/components/policy_list.vue +++ b/ee/app/assets/javascripts/threat_monitoring/components/policy_list.vue @@ -168,7 +168,8 @@ export default { : ''; }, policyType() { - return this.selectedPolicy ? getPolicyType(this.selectedPolicy.yaml) : ''; + // eslint-disable-next-line no-underscore-dangle + return this.selectedPolicy ? getPolicyType(this.selectedPolicy.__typename) : ''; }, fields() { const environments = { diff --git a/ee/app/assets/javascripts/threat_monitoring/utils.js b/ee/app/assets/javascripts/threat_monitoring/utils.js index 33fb1c701882bc4c3b2f101addcdfc4ebd21a860..035da4ed2e88e7aa82fc138da540af3695b03c12 100644 --- a/ee/app/assets/javascripts/threat_monitoring/utils.js +++ b/ee/app/assets/javascripts/threat_monitoring/utils.js @@ -1,18 +1,6 @@ import createGqClient from '~/lib/graphql'; import { POLICY_TYPE_COMPONENT_OPTIONS } from './components/constants'; -/** - * Determines if the yaml passed in is of the type `container` - * @param {String} yaml the policy in yaml form - * @returns {Boolean} - */ -const isContainerPolicyYaml = (yaml) => { - const containerYamlIndicator = Object.values( - POLICY_TYPE_COMPONENT_OPTIONS.container.yamlIndicator, - ); - return containerYamlIndicator.some((str) => yaml?.includes(str)); -}; - /** * Get the height of the wrapper page element * This height can be used to determine where the highest element goes in a page @@ -30,11 +18,11 @@ export const getContentWrapperHeight = (contentWrapperClass) => { * @param {String} yaml policy's YAML manifest * @returns {String|null} policy type if available */ -export const getPolicyType = (yaml = '') => { - if (isContainerPolicyYaml(yaml)) { +export const getPolicyType = (typeName = '') => { + if (typeName === POLICY_TYPE_COMPONENT_OPTIONS.container.typeName) { return POLICY_TYPE_COMPONENT_OPTIONS.container.value; } - if (yaml?.includes(POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.yamlIndicator)) { + if (typeName === POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.typeName) { return POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.value; } return null; diff --git a/ee/spec/frontend/threat_monitoring/mocks/mock_data.js b/ee/spec/frontend/threat_monitoring/mocks/mock_data.js index c82cdbc72f85d560a207b4fa0fa4889430c63601..b8835fe5a33efdab98ee8b7756ebb20827db6a32 100644 --- a/ee/spec/frontend/threat_monitoring/mocks/mock_data.js +++ b/ee/spec/frontend/threat_monitoring/mocks/mock_data.js @@ -114,6 +114,7 @@ spec: - cluster`; export const mockCiliumPolicy = { + __typename: 'NetworkPolicy', name: 'test-policy-03', updatedAt: new Date('2021-06-07T00:00:00.000Z'), yaml: mockCiliumManifest, @@ -121,6 +122,7 @@ export const mockCiliumPolicy = { export const mockNetworkPoliciesResponse = [ { + __typename: 'NetworkPolicy', name: 'policy', kind: 'NetworkPolicy', yaml: mockNetworkManifest, @@ -132,6 +134,7 @@ export const mockNetworkPoliciesResponse = [ }, }, { + __typename: 'NetworkPolicy', name: 'test-policy-02', kind: 'CiliumNetworkPolicy', yaml: mockL3Manifest, @@ -145,6 +148,7 @@ export const mockNetworkPoliciesResponse = [ ]; export const mockScanExecutionPolicy = { + __typename: 'ScanExecutionPolicy', name: 'Scheduled DAST scan', updatedAt: new Date('2021-06-07T00:00:00.000Z'), yaml: mockDastScanExecutionManifest, diff --git a/ee/spec/frontend/threat_monitoring/utils_spec.js b/ee/spec/frontend/threat_monitoring/utils_spec.js index 5e9dc25263cae657c020e7a9570740d659dc1854..c56fcdaa13435de987c95d4522b3ef79b7441664 100644 --- a/ee/spec/frontend/threat_monitoring/utils_spec.js +++ b/ee/spec/frontend/threat_monitoring/utils_spec.js @@ -1,3 +1,4 @@ +/* eslint-disable no-underscore-dangle */ import { POLICY_TYPE_COMPONENT_OPTIONS } from 'ee/threat_monitoring/components/constants'; import { getContentWrapperHeight, @@ -5,11 +6,7 @@ import { removeUnnecessaryDashes, } from 'ee/threat_monitoring/utils'; import { setHTMLFixture } from 'helpers/fixtures'; -import { - mockDastScanExecutionManifest, - mockCiliumManifest, - mockNetworkManifest, -} from './mocks/mock_data'; +import { mockScanExecutionPolicy, mockNetworkPoliciesResponse } from './mocks/mock_data'; describe('Threat Monitoring Utils', () => { describe('getContentWrapperHeight', () => { @@ -36,12 +33,12 @@ describe('Threat Monitoring Utils', () => { describe('getPolicyType', () => { it.each` - input | output - ${''} | ${null} - ${'random string'} | ${null} - ${mockNetworkManifest} | ${POLICY_TYPE_COMPONENT_OPTIONS.container.value} - ${mockCiliumManifest} | ${POLICY_TYPE_COMPONENT_OPTIONS.container.value} - ${mockDastScanExecutionManifest} | ${POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.value} + input | output + ${''} | ${null} + ${'UnknownPolicyType'} | ${null} + ${mockNetworkPoliciesResponse[0].__typename} | ${POLICY_TYPE_COMPONENT_OPTIONS.container.value} + ${mockNetworkPoliciesResponse[1].__typename} | ${POLICY_TYPE_COMPONENT_OPTIONS.container.value} + ${mockScanExecutionPolicy.__typename} | ${POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.value} `('returns $output when used on $input', ({ input, output }) => { expect(getPolicyType(input)).toBe(output); });