Skip to content

Commit

Permalink
lxd/util: Check for "#cloud-init"
Browse files Browse the repository at this point in the history
This helps to avoid parsing an invalid conifiguration, as the `#cloud-config` comment is mandatory, and also helps us check if the configuration is in a supported format.

Signed-off-by: hamistao <[email protected]>
  • Loading branch information
hamistao committed Feb 24, 2025
1 parent f2dd3a5 commit 22d8c5e
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lxd/cloudinit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ func GetResultingCloudConfig(instanceConfig map[string]string, vendorDataKey str
// parseCloudConfig attempts to unmarshal a string into a cloudConfig object. Returns an error if the
// provided string is not a valid YAML or lacks the needed "#cloud-config" comment.
func parseCloudConfig(rawCloudConfig string) (cloudConfig, error) {
// Check if rawCloudConfig is in a supported format.
// A YAML cloud config without #cloud-config is invalid.
// The "#cloud-config" tag can be either on the first or second lines.
if rawCloudConfig != "" && !shared.ValueInSlice("#cloud-config", shared.SplitNTrimSpace(rawCloudConfig, "\n", 3, false)) {
return nil, errors.New(`Parsing configuration is not supported as it is not "#cloud-config"`)
}

// Parse YAML cloud-config into map.
cloudConfigMap := make(map[any]any)
err := yaml.Unmarshal([]byte(rawCloudConfig), cloudConfigMap)
Expand Down

0 comments on commit 22d8c5e

Please sign in to comment.