From 06f5c2e61e56a8d8460ea886ed8d46f97c14369d Mon Sep 17 00:00:00 2001 From: Linda Song Date: Tue, 22 Mar 2022 16:07:43 -0700 Subject: [PATCH 1/8] fixing policy parsing for comparative fields --- .../Policies/PatternFly/policies.utils.ts | 2 +- .../Policies/Wizard/Form/descriptors.tsx | 2 +- .../Details/ViolationDetailsPage.tsx | 24 +++++++------------ 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts index 171a7748f4257..6bd62f440ced4 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts @@ -405,7 +405,7 @@ export function formatValueStr(valueObj: ValueObj, fieldName: string): string { if (nonStandardNumberFields.includes(fieldName)) { // TODO: work with API to update contract for returning number comparison fields // until that improves, we short-circuit those fields here - valueStr = key !== '=' ? `${key}${value}` : `${value}`; + valueStr = key !== '=' ? `${key} ${value}` : `${value}`; } else if (source || fieldName === 'Environment Variable') { valueStr = `${source || ''}=${key}=${value}`; } else if (key) { diff --git a/ui/apps/platform/src/Containers/Policies/Wizard/Form/descriptors.tsx b/ui/apps/platform/src/Containers/Policies/Wizard/Form/descriptors.tsx index a491ae35ed196..dbf8950aae587 100644 --- a/ui/apps/platform/src/Containers/Policies/Wizard/Form/descriptors.tsx +++ b/ui/apps/platform/src/Containers/Policies/Wizard/Form/descriptors.tsx @@ -661,7 +661,7 @@ export const policyConfigurationDescriptor: Descriptor[] = [ shortName: 'Exposed node port', negatedName: `Exposed node port doesn't match`, category: policyCriteriaCategories.NETWORKING, - type: 'number', + type: 'text', placeholder: '22', canBooleanLogic: true, }, diff --git a/ui/apps/platform/src/Containers/Violations/Details/ViolationDetailsPage.tsx b/ui/apps/platform/src/Containers/Violations/Details/ViolationDetailsPage.tsx index 490a6856a630e..fd26a5c3903e6 100644 --- a/ui/apps/platform/src/Containers/Violations/Details/ViolationDetailsPage.tsx +++ b/ui/apps/platform/src/Containers/Violations/Details/ViolationDetailsPage.tsx @@ -12,12 +12,9 @@ import { } from '@patternfly/react-core'; import { fetchAlert } from 'services/AlertsService'; -import { preFormatPolicyFields } from 'Containers/Policies/Wizard/Form/utils'; -import useFeatureFlagEnabled from 'hooks/useFeatureFlagEnabled'; -import { knownBackendFlags } from 'utils/featureFlags'; import PolicyDetailContent from '../../Policies/PatternFly/Detail/PolicyDetailContent'; +import { getClientWizardPolicy } from '../../Policies/PatternFly/policies.utils'; import DeploymentDetails from './DeploymentDetails'; -import PolicyDetails from './PolicyDetails'; import EnforcementDetails from './EnforcementDetails'; import { Alert } from '../types/violationTypes'; import ViolationNotFoundPage from '../ViolationNotFoundPage'; @@ -30,7 +27,6 @@ function ViolationDetailsPage(): ReactElement { const [isFetchingSelectedAlert, setIsFetchingSelectedAlert] = useState(false); const { alertId } = useParams(); - const isPoliciesPFEnabled = useFeatureFlagEnabled(knownBackendFlags.ROX_POLICIES_PATTERNFLY); function handleTabClick(_, tabIndex) { setActiveTabKey(tabIndex); @@ -111,17 +107,13 @@ function ViolationDetailsPage(): ReactElement { )} Policy}> - {isPoliciesPFEnabled ? ( - <> - - Policy overview - - - - - ) : ( - - )} + <> + + Policy overview + + + + From 1948401f1c8b079fcaff68d2e4560bb0b3b9daad Mon Sep 17 00:00:00 2001 From: Linda Song Date: Tue, 22 Mar 2022 16:17:21 -0700 Subject: [PATCH 2/8] sending server policy sections if criteria are locked/default policy --- .../Policies/PatternFly/PolicyPage.tsx | 1 + .../Policies/PatternFly/policies.utils.ts | 40 +++++++++++-------- ui/apps/platform/src/types/policy.proto.ts | 1 + 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/PolicyPage.tsx b/ui/apps/platform/src/Containers/Policies/PatternFly/PolicyPage.tsx index 6a562a1611284..64353f3194161 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/PolicyPage.tsx +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/PolicyPage.tsx @@ -56,6 +56,7 @@ const initialPolicy: Policy = { SORT_lifecycleStage: '', // For internal use only. SORT_enforcement: false, // For internal use only. policyVersion: '', + serverPolicySections: [], policySections: [ { sectionName: 'Policy Section 1', diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts index 6bd62f440ced4..e2300c6da4bfd 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts @@ -381,6 +381,7 @@ function preFormatNestedPolicyFields(policy: Policy): Policy { } const clientPolicy = cloneDeep(policy); + clientPolicy.serverPolicySections = policy.policySections; // itreating through each value in a policy group in a policy section to parse value string policy.policySections.forEach((policySection, sectionIdx) => { const { policyGroups } = policySection; @@ -405,7 +406,7 @@ export function formatValueStr(valueObj: ValueObj, fieldName: string): string { if (nonStandardNumberFields.includes(fieldName)) { // TODO: work with API to update contract for returning number comparison fields // until that improves, we short-circuit those fields here - valueStr = key !== '=' ? `${key} ${value}` : `${value}`; + valueStr = key !== '=' ? `${key}${value}` : `${value}`; } else if (source || fieldName === 'Environment Variable') { valueStr = `${source || ''}=${key}=${value}`; } else if (key) { @@ -485,22 +486,29 @@ export function postFormatImageSigningPolicyGroup(policy: Policy): Policy { } const serverPolicy = cloneDeep(policy); - policy.policySections.forEach((policySection, sectionIdx) => { - const { policyGroups } = policySection; - policyGroups.forEach((policyGroup, groupIdx) => { - const { values } = policyGroup; - if (policyGroup.fieldName === imageSigningCriteriaName) { - const { arrayValue } = values[0]; - arrayValue?.forEach((value, valueIdx) => { - serverPolicy.policySections[sectionIdx].policyGroups[groupIdx].values[ - valueIdx - ] = { - value, - }; - }); - } + if (policy.criteriaLocked) { + serverPolicy.policySections = policy.serverPolicySections; + } else { + policy.policySections.forEach((policySection, sectionIdx) => { + const { policyGroups } = policySection; + policyGroups.forEach((policyGroup, groupIdx) => { + const { values } = policyGroup; + if (policyGroup.fieldName === imageSigningCriteriaName) { + const { arrayValue } = values[0]; + arrayValue?.forEach((value, valueIdx) => { + serverPolicy.policySections[sectionIdx].policyGroups[groupIdx].values[ + valueIdx + ] = { + value, + }; + }); + } + }); }); - }); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + delete serverPolicy.serverPolicySections; + } return serverPolicy; } diff --git a/ui/apps/platform/src/types/policy.proto.ts b/ui/apps/platform/src/types/policy.proto.ts index a4fa506a14318..79d3fe615d0d4 100644 --- a/ui/apps/platform/src/types/policy.proto.ts +++ b/ui/apps/platform/src/types/policy.proto.ts @@ -40,6 +40,7 @@ export type Policy = { SORT_lifecycleStage: string; // For internal use only. SORT_enforcement: boolean; // For internal use only. policyVersion: string; + serverPolicySections: PolicySection[]; // For internal use only. policySections: PolicySection[]; mitreAttackVectors: PolicyMitreAttackVector[]; readonly criteriaLocked: boolean; // If true, the policy's criteria fields are rendered read-only. From 8c40c6e1c44017bc2ef7f492613272f6649a4339 Mon Sep 17 00:00:00 2001 From: Linda Song Date: Tue, 22 Mar 2022 16:19:07 -0700 Subject: [PATCH 3/8] clean up --- .../src/Containers/Policies/PatternFly/policies.utils.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts index e2300c6da4bfd..9a10e9290753d 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts @@ -505,11 +505,10 @@ export function postFormatImageSigningPolicyGroup(policy: Policy): Policy { } }); }); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - delete serverPolicy.serverPolicySections; } - + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + delete serverPolicy.serverPolicySections; return serverPolicy; } From 753bc8513c31a7cfce811fbc6525e4d3a92739bb Mon Sep 17 00:00:00 2001 From: Linda Song Date: Tue, 22 Mar 2022 16:26:30 -0700 Subject: [PATCH 4/8] adding comments --- .../src/Containers/Policies/PatternFly/policies.utils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts index 9a10e9290753d..b469bbcfe9e33 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts @@ -538,6 +538,7 @@ export function preFormatImageSigningPolicyGroup(policy: Policy): Policy { export function getClientWizardPolicy(policy): Policy { let formattedPolicy = preFormatExclusionField(policy); + // preFormatNestedPolicyFields adds serverPolicySections unparsed formattedPolicy = preFormatNestedPolicyFields(formattedPolicy); formattedPolicy = preFormatImageSigningPolicyGroup(formattedPolicy); return formattedPolicy; @@ -546,6 +547,7 @@ export function getClientWizardPolicy(policy): Policy { export function getServerPolicy(policy): Policy { let serverPolicy = postFormatExclusionField(policy); serverPolicy = postFormatImageSigningPolicyGroup(serverPolicy); + // postFormatNestedPolicyFields rewrites policySections with serverPolicySections for default policies serverPolicy = postFormatNestedPolicyFields(serverPolicy); return serverPolicy; } From 0380da4b94dee0bc07482e10b0bb862a94a44a8d Mon Sep 17 00:00:00 2001 From: Linda Song Date: Tue, 22 Mar 2022 16:27:47 -0700 Subject: [PATCH 5/8] comments --- .../src/Containers/Policies/PatternFly/policies.utils.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts index b469bbcfe9e33..62e51cac4d2d2 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts @@ -538,7 +538,8 @@ export function preFormatImageSigningPolicyGroup(policy: Policy): Policy { export function getClientWizardPolicy(policy): Policy { let formattedPolicy = preFormatExclusionField(policy); - // preFormatNestedPolicyFields adds serverPolicySections unparsed + // order is important here since preFormatNestedPolicyFields adds + // serverPolicySections unparsed formattedPolicy = preFormatNestedPolicyFields(formattedPolicy); formattedPolicy = preFormatImageSigningPolicyGroup(formattedPolicy); return formattedPolicy; @@ -547,7 +548,8 @@ export function getClientWizardPolicy(policy): Policy { export function getServerPolicy(policy): Policy { let serverPolicy = postFormatExclusionField(policy); serverPolicy = postFormatImageSigningPolicyGroup(serverPolicy); - // postFormatNestedPolicyFields rewrites policySections with serverPolicySections for default policies + // order is important here since postFormatNestedPolicyFields rewrites + // policySections with serverPolicySections for default policies serverPolicy = postFormatNestedPolicyFields(serverPolicy); return serverPolicy; } From c805e28b92edc5c007a8bd45b7d6280659297343 Mon Sep 17 00:00:00 2001 From: Linda Song Date: Tue, 22 Mar 2022 16:59:03 -0700 Subject: [PATCH 6/8] oops --- .../Policies/PatternFly/policies.utils.ts | 77 +++++++++---------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts index 62e51cac4d2d2..ae3027806cd6e 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts @@ -421,21 +421,30 @@ function postFormatNestedPolicyFields(policy: Policy): Policy { } const serverPolicy = cloneDeep(policy); - // itereating through each value in a policy group in a policy section to format to a flat value string - policy.policySections.forEach((policySection, sectionIdx) => { - const { policyGroups } = policySection; - policyGroups.forEach((policyGroup, groupIdx) => { - const { values } = policyGroup; - values.forEach((value, valueIdx) => { - serverPolicy.policySections[sectionIdx].policyGroups[groupIdx].values[valueIdx] = { - value: formatValueStr(value as ValueObj, policyGroup.fieldName), - }; + if (policy.criteriaLocked) { + serverPolicy.policySections = policy.serverPolicySections; + } else { + // itereating through each value in a policy group in a policy section to format to a flat value string + policy.policySections.forEach((policySection, sectionIdx) => { + const { policyGroups } = policySection; + policyGroups.forEach((policyGroup, groupIdx) => { + const { values } = policyGroup; + values.forEach((value, valueIdx) => { + serverPolicy.policySections[sectionIdx].policyGroups[groupIdx].values[ + valueIdx + ] = { + value: formatValueStr(value as ValueObj, policyGroup.fieldName), + }; + }); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + delete serverPolicy.policySections[sectionIdx].policyGroups[groupIdx].fieldKey; }); - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - delete serverPolicy.policySections[sectionIdx].policyGroups[groupIdx].fieldKey; }); - }); + } + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + delete serverPolicy.serverPolicySections; return serverPolicy; } @@ -486,29 +495,23 @@ export function postFormatImageSigningPolicyGroup(policy: Policy): Policy { } const serverPolicy = cloneDeep(policy); - if (policy.criteriaLocked) { - serverPolicy.policySections = policy.serverPolicySections; - } else { - policy.policySections.forEach((policySection, sectionIdx) => { - const { policyGroups } = policySection; - policyGroups.forEach((policyGroup, groupIdx) => { - const { values } = policyGroup; - if (policyGroup.fieldName === imageSigningCriteriaName) { - const { arrayValue } = values[0]; - arrayValue?.forEach((value, valueIdx) => { - serverPolicy.policySections[sectionIdx].policyGroups[groupIdx].values[ - valueIdx - ] = { - value, - }; - }); - } - }); + + policy.policySections.forEach((policySection, sectionIdx) => { + const { policyGroups } = policySection; + policyGroups.forEach((policyGroup, groupIdx) => { + const { values } = policyGroup; + if (policyGroup.fieldName === imageSigningCriteriaName) { + const { arrayValue } = values[0]; + arrayValue?.forEach((value, valueIdx) => { + serverPolicy.policySections[sectionIdx].policyGroups[groupIdx].values[ + valueIdx + ] = { + value, + }; + }); + } }); - } - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - delete serverPolicy.serverPolicySections; + }); return serverPolicy; } @@ -538,8 +541,6 @@ export function preFormatImageSigningPolicyGroup(policy: Policy): Policy { export function getClientWizardPolicy(policy): Policy { let formattedPolicy = preFormatExclusionField(policy); - // order is important here since preFormatNestedPolicyFields adds - // serverPolicySections unparsed formattedPolicy = preFormatNestedPolicyFields(formattedPolicy); formattedPolicy = preFormatImageSigningPolicyGroup(formattedPolicy); return formattedPolicy; @@ -548,8 +549,6 @@ export function getClientWizardPolicy(policy): Policy { export function getServerPolicy(policy): Policy { let serverPolicy = postFormatExclusionField(policy); serverPolicy = postFormatImageSigningPolicyGroup(serverPolicy); - // order is important here since postFormatNestedPolicyFields rewrites - // policySections with serverPolicySections for default policies serverPolicy = postFormatNestedPolicyFields(serverPolicy); return serverPolicy; } From 63c571faa787a77b198dcb83f2bb5a39d27c3475 Mon Sep 17 00:00:00 2001 From: Linda Song Date: Tue, 22 Mar 2022 17:00:00 -0700 Subject: [PATCH 7/8] clean --- .../src/Containers/Policies/PatternFly/policies.utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts index ae3027806cd6e..9dcf9dcf5a3c8 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts @@ -495,7 +495,6 @@ export function postFormatImageSigningPolicyGroup(policy: Policy): Policy { } const serverPolicy = cloneDeep(policy); - policy.policySections.forEach((policySection, sectionIdx) => { const { policyGroups } = policySection; policyGroups.forEach((policyGroup, groupIdx) => { From 2099c023f3c5c9f2d6d9bf21bc8abe0c1c889049 Mon Sep 17 00:00:00 2001 From: Linda Song Date: Tue, 22 Mar 2022 17:01:38 -0700 Subject: [PATCH 8/8] space --- .../src/Containers/Policies/PatternFly/policies.utils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts index 9dcf9dcf5a3c8..619ba87460a48 100644 --- a/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts +++ b/ui/apps/platform/src/Containers/Policies/PatternFly/policies.utils.ts @@ -511,6 +511,7 @@ export function postFormatImageSigningPolicyGroup(policy: Policy): Policy { } }); }); + return serverPolicy; }