From 4c756e9a08dd4145b6e21dbcc59c4cca63a8c3bc Mon Sep 17 00:00:00 2001 From: Alexandre Alves Date: Thu, 26 Sep 2024 09:50:45 +0100 Subject: [PATCH] Fix Vue3: CIS Benchmark issues (#11991) * add missing translations in clusterscan edit view + fix broken links in clusterscan + update clusterscan model to apply defaults on edit/create view instead of model because of schema diet changes * change mode for realMode --- shell/assets/translations/en-us.yaml | 2 + shell/config/product/cis.js | 7 ++- shell/edit/cis.cattle.io.clusterscan.vue | 56 +++++++++++++------ shell/models/cis.cattle.io.clusterscan.js | 33 +++++------ .../cis.cattle.io.clusterscanprofile.js | 17 ++++++ 5 files changed, 78 insertions(+), 37 deletions(-) diff --git a/shell/assets/translations/en-us.yaml b/shell/assets/translations/en-us.yaml index 8c2aff48e96..57aa57d6a2d 100644 --- a/shell/assets/translations/en-us.yaml +++ b/shell/assets/translations/en-us.yaml @@ -1177,6 +1177,7 @@ cis: total: Total warn: Warn scheduling: + title: Scheduling disable: Run scan once enable: Run scan on a schedule scoreWarning: @@ -1185,6 +1186,7 @@ cis: testID: Test ID testsSkipped: Tests Skipped testsToSkip: Tests to Skip + alerting: Alerting cluster: jwtAuthentication: diff --git a/shell/config/product/cis.js b/shell/config/product/cis.js index d7126aa20f6..928db940343 100644 --- a/shell/config/product/cis.js +++ b/shell/config/product/cis.js @@ -37,9 +37,9 @@ export function init(store) { { name: 'clusterScanProfile', label: 'Profile', - value: 'status.lastRunScanProfileName', formatter: 'Link', - formatterOpts: { options: { internal: true }, to: { name: 'c-cluster-product-resource-id', params: { resource: CIS.CLUSTER_SCAN_PROFILE } } }, + formatterOpts: { options: { internal: true }, urlKey: 'scanProfileLink' }, + value: 'status.lastRunScanProfileName', sort: ['status.lastRunScanProfileName'], }, { @@ -115,7 +115,8 @@ export function init(store) { labelKey: 'cis.benchmarkVersion', value: 'spec.benchmarkVersion', formatter: 'Link', - formatterOpts: { options: { internal: true }, to: { name: 'c-cluster-product-resource-id', params: { resource: CIS.BENCHMARK } } }, + formatterOpts: { options: { internal: true }, urlKey: 'benchmarkVersionLink' }, + sort: ['spec.benchmarkVersion'], }, { name: 'skippedTests', diff --git a/shell/edit/cis.cattle.io.clusterscan.vue b/shell/edit/cis.cattle.io.clusterscan.vue index da128530880..9a75418e505 100644 --- a/shell/edit/cis.cattle.io.clusterscan.vue +++ b/shell/edit/cis.cattle.io.clusterscan.vue @@ -12,7 +12,7 @@ import { allHash } from '@shell/utils/promise'; import { Checkbox } from '@components/Form/Checkbox'; import { RadioGroup } from '@components/Form/Radio'; import { get } from '@shell/utils/object'; -import { _VIEW, _CREATE } from '@shell/config/query-params'; +import { _VIEW, _CREATE, _EDIT } from '@shell/config/query-params'; import { isValidCron } from 'cron-validator'; import { fetchSpecsScheduledScanConfig } from '@shell/models/cis.cattle.io.clusterscan'; @@ -39,6 +39,37 @@ export default { }, async fetch() { + // we need to force-fetch the resource fields, otherwise on page refresh + // in the clusterscan edit/create views the "canBeScheduled" won't run properly + await this.schema.fetchResourceFields(); + + if (this.realMode === _CREATE || this.realMode === _EDIT) { + const includeScheduling = this.value.canBeScheduled(); + const spec = this.value.spec || {}; + + spec.scanProfileName = null; + if (includeScheduling) { + spec.scoreWarning = 'pass'; + spec.scheduledScanConfig = { scanAlertRule: {}, retentionCount: 3 }; + } + + this.value.spec = spec; + } + + if (!this.value.metadata.name) { + this.value.metadata.generateName = 'scan-'; + } + if (!this.value.spec.scheduledScanConfig) { + this.value.spec['scheduledScanConfig'] = { scanAlertRule: {} }; + } + if (!this.value.spec.scheduledScanConfig.scanAlertRule) { + this.value.spec.scheduledScanConfig['scanAlertRule'] = { }; + } + + this.isScheduled = !!get(this.value, 'spec.scheduledScanConfig.cronSchedule'); + this.scheduledScanConfig = this.value.spec.scheduledScanConfig; + this.scanAlertRule = this.value.spec.scheduledScanConfig.scanAlertRule; + const hash = await allHash({ profiles: this.$store.dispatch('cluster/findAll', { type: CIS.CLUSTER_SCAN_PROFILE }), benchmarks: this.$store.dispatch('cluster/findAll', { type: CIS.BENCHMARK }), @@ -65,24 +96,13 @@ export default { }, data() { - if (!this.value.metadata.name) { - this.value.metadata.generateName = 'scan-'; - } - if (!this.value.spec.scheduledScanConfig) { - this.value.spec['scheduledScanConfig'] = { scanAlertRule: {} }; - } - if (!this.value.spec.scheduledScanConfig.scanAlertRule) { - this.value.spec.scheduledScanConfig['scanAlertRule'] = { }; - } - const isScheduled = !!get(this.value, 'spec.scheduledScanConfig.cronSchedule'); - return { allProfiles: [], defaultConfigMap: null, - scheduledScanConfig: this.value.spec.scheduledScanConfig, - scanAlertRule: this.value.spec.scheduledScanConfig.scanAlertRule, + scheduledScanConfig: null, + scanAlertRule: null, hasAlertManager: false, - isScheduled + isScheduled: null }; }, @@ -281,7 +301,7 @@ export default {