Skip to content

Commit

Permalink
Fix Vue3: CIS Benchmark issues (#11991)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
aalves08 authored Sep 26, 2024
1 parent 1e32c3e commit 4c756e9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 37 deletions.
2 changes: 2 additions & 0 deletions shell/assets/translations/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ cis:
total: Total
warn: Warn
scheduling:
title: Scheduling
disable: Run scan once
enable: Run scan on a schedule
scoreWarning:
Expand All @@ -1185,6 +1186,7 @@ cis:
testID: Test ID
testsSkipped: Tests Skipped
testsToSkip: Tests to Skip
alerting: Alerting

cluster:
jwtAuthentication:
Expand Down
7 changes: 4 additions & 3 deletions shell/config/product/cis.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
},
{
Expand Down Expand Up @@ -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',
Expand Down
56 changes: 38 additions & 18 deletions shell/edit/cis.cattle.io.clusterscan.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 }),
Expand All @@ -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
};
},
Expand Down Expand Up @@ -281,7 +301,7 @@ export default {
</div>
</div>
<template v-if="canBeScheduled">
<h3>Scheduling</h3>
<h3>{{ t('cis.scheduling.title') }}</h3>
<div class="row mb-20">
<div class="col">
<RadioGroup
Expand Down Expand Up @@ -314,8 +334,8 @@ export default {
/>
</div>
</div>
<h3>
Alerting
<h3 class="mt-20">
{{ t('cis.alerting') }}
</h3>
<div class="row mb-20">
<div class="col span-12">
Expand Down
33 changes: 17 additions & 16 deletions shell/models/cis.cattle.io.clusterscan.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { _CREATE, _EDIT } from '@shell/config/query-params';
import { NAME as PRODUCT_NAME } from '@shell/config/product/cis';
import { CIS } from '@shell/config/types';
import { findBy } from '@shell/utils/array';
import { downloadFile, generateZip } from '@shell/utils/download';
import { get, isEmpty, set } from '@shell/utils/object';
import { get, isEmpty } from '@shell/utils/object';
import { sortBy } from '@shell/utils/sort';
import day from 'dayjs';
import SteveModel from '@shell/plugins/steve/steve-class';
Expand Down Expand Up @@ -77,20 +77,6 @@ export default class ClusterScan extends SteveModel {
return out;
}

applyDefaults(vm, mode) {
if (mode === _CREATE || mode === _EDIT) {
const includeScheduling = this.canBeScheduled();
const spec = this.spec || {};

spec.scanProfileName = null;
if (includeScheduling) {
spec.scoreWarning = 'pass';
spec.scheduledScanConfig = { scanAlertRule: {}, retentionCount: 3 };
}
set(this, 'spec', spec);
}
}

canBeScheduled() {
return hasSpecsScheduledScanConfig(this.$getters['schemaFor'](this.type));
}
Expand Down Expand Up @@ -166,6 +152,21 @@ export default class ClusterScan extends SteveModel {
});
}
}

get scanProfileLink() {
if (this.status?.lastRunScanProfileName) {
return {
name: 'c-cluster-product-resource-id',
params: {
resource: CIS.CLUSTER_SCAN_PROFILE,
product: PRODUCT_NAME,
id: this.status?.lastRunScanProfileName
}
};
}

return {};
}
}

const labelFor = (report) => {
Expand Down
17 changes: 17 additions & 0 deletions shell/models/cis.cattle.io.clusterscanprofile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

import SteveModel from '@shell/plugins/steve/steve-class';
import { NAME as PRODUCT_NAME } from '@shell/config/product/cis';
import { CIS } from '@shell/config/types';

export default class CISProfile extends SteveModel {
warnDeletionMessage(toRemove = []) {
Expand All @@ -11,4 +13,19 @@ export default class CISProfile extends SteveModel {

return skipTests.length;
}

get benchmarkVersionLink() {
if (this.spec?.benchmarkVersion) {
return {
name: 'c-cluster-product-resource-id',
params: {
resource: CIS.BENCHMARK,
product: PRODUCT_NAME,
id: this.spec?.benchmarkVersion
}
};
}

return {};
}
}

0 comments on commit 4c756e9

Please sign in to comment.