fix(vm): block VM creation with invalid CloudInit YAML content

This commit is contained in:
copilot-swe-agent[bot] 2026-07-20 10:45:13 +00:00 committed by Andy Lee
parent fbd4a1108f
commit 1df5af32b8
3 changed files with 54 additions and 0 deletions

View File

@ -1,4 +1,5 @@
<script>
import jsyaml from 'js-yaml';
import { mapGetters } from 'vuex';
import Tabbed from '@shell/components/Tabbed';
import Tab from '@shell/components/Tabbed/Tab';
@ -177,8 +178,17 @@ export default {
},
async saveVMT(buttonCb) {
this.errors = [];
this.parseVM();
this.validateCloudInit();
if (this.errors.length) {
buttonCb(false);
return;
}
const templates = await this.$store.dispatch('harvester/findAll', { type: HCI.VM_TEMPLATE });
const template = templates.find( (O) => O.metadata.name === this.templateValue.metadata.name);
@ -237,6 +247,24 @@ export default {
this.$refs.yamlEditor?.refresh();
}
},
validateCloudInit() {
if (this.userScript) {
try {
jsyaml.load(this.userScript);
} catch (e) {
this.errors.push(this.t('harvester.virtualMachine.cloudConfig.user.invalidYaml'));
}
}
if (this.networkScript) {
try {
jsyaml.load(this.networkScript);
} catch (e) {
this.errors.push(this.t('harvester.virtualMachine.cloudConfig.network.invalidYaml'));
}
}
},
},
};
</script>

View File

@ -1,4 +1,5 @@
<script>
import jsyaml from 'js-yaml';
import { isEqual } from 'lodash';
import { mapGetters } from 'vuex';
import Tabbed from '@shell/components/Tabbed';
@ -379,6 +380,7 @@ export default {
this.validateCPUMemory();
this.validateWindowsSysprep();
this.validateCloudInit();
// block create VM flow if has validation errors
if (this.errors.length) {
@ -424,6 +426,28 @@ export default {
}
},
validateCloudInit() {
if (this.isWindows) {
return;
}
if (this.userScript) {
try {
jsyaml.load(this.userScript);
} catch (e) {
this.errors.push(this.t('harvester.virtualMachine.cloudConfig.user.invalidYaml'));
}
}
if (this.networkScript) {
try {
jsyaml.load(this.networkScript);
} catch (e) {
this.errors.push(this.t('harvester.virtualMachine.cloudConfig.network.invalidYaml'));
}
}
},
async saveSingle(buttonCb) {
this.parseVM();
this.value.spec.template.spec.hostname = this.hostname ? this.hostname : this.value.metadata.name;

View File

@ -830,10 +830,12 @@ harvester:
label: User Data Template
title: "User Data:"
tip: "You can specify user data to configure an instance or run a configuration script during launch. If you launch more than one instance at a time, the user data is available to all the instances in that reservation. <a href='https://cloudinit.readthedocs.io/en/latest/topics/examples.html' target='_blank'>Learn more</a>"
invalidYaml: 'Invalid User Data YAML syntax'
network:
label: Network Data Template
title: "Network Data:"
tip: "The network-data configuration allows you to customize the instance's networking interfaces by assigning subnet configuration, virtual device creation (bonds, bridges, VLANs) routes and DNS configuration. <a href='https://cloudinit.readthedocs.io/en/latest/reference/network-config-format-v1.html' target='_blank'>Learn more</a>"
invalidYaml: 'Invalid Network Data YAML syntax'
sysprep:
title: Windows Sysprep Configuration
description: "Configure Windows automated installation using autounattend.xml. The configuration will be stored in a Kubernetes Secret and mounted as a CD-ROM during installation. <a href='https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/automate-windows-setup' target='_blank'>Learn more</a>"