|
|
|
|
@ -28,11 +28,17 @@ import (
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
maxSeedImageBytes = 2 << 20
|
|
|
|
|
maxTemplateBytes = int64(4) << 30
|
|
|
|
|
maxUserISOBytes = int64(32) << 30
|
|
|
|
|
maxRescueISOBytes = int64(8) << 30
|
|
|
|
|
minImportBytes = int64(1) << 20
|
|
|
|
|
maxSeedImageBytes = 2 << 20
|
|
|
|
|
maxTemplateBytes = int64(4) << 30
|
|
|
|
|
maxUserISOBytes = int64(32) << 30
|
|
|
|
|
maxRescueISOBytes = int64(8) << 30
|
|
|
|
|
minImportBytes = int64(1) << 20
|
|
|
|
|
backupRecoveryOperationMetadata = "virt-cloud-backup-recovery-operation"
|
|
|
|
|
diskGuestIdentityMetadata = "virt-cloud-guest-identity"
|
|
|
|
|
diskGuestIdentityValue = "disk"
|
|
|
|
|
diskSSHAuthorizedKeysPath = "/root/.ssh/authorized_keys"
|
|
|
|
|
diskSSHDirectoryCommand = "install -d -m 0700 -o root -g root /root/.ssh"
|
|
|
|
|
diskSSHRemoveCommand = "rm -f -- /root/.ssh/authorized_keys"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
@ -125,28 +131,67 @@ func mutateSeedSettings(contents []byte, request SettingsRequest, transitionID s
|
|
|
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.UserData[len(header):]), &userData); err != nil {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud user-data is invalid", err)
|
|
|
|
|
}
|
|
|
|
|
users, ok := userData["users"].([]any)
|
|
|
|
|
if !ok || len(users) != 1 {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud root account definition is invalid", nil)
|
|
|
|
|
var metadata map[string]any
|
|
|
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.MetaData), &metadata); err != nil {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud meta-data is invalid", err)
|
|
|
|
|
}
|
|
|
|
|
root, ok := users[0].(map[string]any)
|
|
|
|
|
if !ok || root["name"] != "root" {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud root account definition is invalid", nil)
|
|
|
|
|
instanceID, ok := metadata["instance-id"].(string)
|
|
|
|
|
if !ok || strings.TrimSpace(instanceID) == "" {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud instance identity is invalid", nil)
|
|
|
|
|
}
|
|
|
|
|
diskAuthoritative := metadata[diskGuestIdentityMetadata] == diskGuestIdentityValue
|
|
|
|
|
var root map[string]any
|
|
|
|
|
if diskAuthoritative {
|
|
|
|
|
// Recovery itself removes every pre-recovery identity instruction.
|
|
|
|
|
// From this point on, however, settings explicitly submitted by the
|
|
|
|
|
// user are new intent and must survive later settings transitions until
|
|
|
|
|
// that same field is changed again. This also prevents a quick second
|
|
|
|
|
// operation from deleting a password/key before cloud-init consumes it.
|
|
|
|
|
if users, exists := userData["users"]; exists {
|
|
|
|
|
parsed, valid := users.([]any)
|
|
|
|
|
if !valid || len(parsed) != 1 {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud root account definition is invalid", nil)
|
|
|
|
|
}
|
|
|
|
|
root, valid = parsed[0].(map[string]any)
|
|
|
|
|
if !valid || root["name"] != "root" {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud root account definition is invalid", nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if request.RootPassword != nil {
|
|
|
|
|
if root == nil {
|
|
|
|
|
root = map[string]any{"name": "root", "shell": "/bin/bash"}
|
|
|
|
|
userData["users"] = []any{root}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
users, valid := userData["users"].([]any)
|
|
|
|
|
if !valid || len(users) != 1 {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud root account definition is invalid", nil)
|
|
|
|
|
}
|
|
|
|
|
root, valid = users[0].(map[string]any)
|
|
|
|
|
if !valid || root["name"] != "root" {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud root account definition is invalid", nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if request.SSHPublicKey != nil {
|
|
|
|
|
key := *request.SSHPublicKey
|
|
|
|
|
if err := validateOptionalSSHKey(key); err != nil {
|
|
|
|
|
return nil, "", err
|
|
|
|
|
}
|
|
|
|
|
if key == "" {
|
|
|
|
|
if diskAuthoritative {
|
|
|
|
|
removeDiskAuthoritativeSSHKeyMutation(userData)
|
|
|
|
|
setDiskAuthoritativeSSHKeyMutation(userData, key)
|
|
|
|
|
} else if key == "" {
|
|
|
|
|
delete(root, "ssh_authorized_keys")
|
|
|
|
|
} else {
|
|
|
|
|
root["ssh_authorized_keys"] = []string{key}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
passwordEnabled := false
|
|
|
|
|
if hash, ok := root["hashed_passwd"].(string); ok && hash != "" {
|
|
|
|
|
passwordEnabled = true
|
|
|
|
|
if root != nil {
|
|
|
|
|
if hash, present := root["hashed_passwd"].(string); present && hash != "" {
|
|
|
|
|
passwordEnabled = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if request.RootPassword != nil {
|
|
|
|
|
password := *request.RootPassword
|
|
|
|
|
@ -172,22 +217,23 @@ func mutateSeedSettings(contents []byte, request SettingsRequest, transitionID s
|
|
|
|
|
passwordEnabled = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
keys, _ := root["ssh_authorized_keys"].([]any)
|
|
|
|
|
stringKeys, _ := root["ssh_authorized_keys"].([]string)
|
|
|
|
|
if !passwordEnabled && len(keys) == 0 && len(stringKeys) == 0 {
|
|
|
|
|
return nil, "", NewError(ErrInvalid, "guest_credentials_required", "at least one root login method must remain enabled", nil)
|
|
|
|
|
}
|
|
|
|
|
userData["ssh_pwauth"] = passwordEnabled
|
|
|
|
|
updateSSHDWriteFile(userData, passwordEnabled)
|
|
|
|
|
|
|
|
|
|
var metadata map[string]any
|
|
|
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.MetaData), &metadata); err != nil {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud meta-data is invalid", err)
|
|
|
|
|
if diskAuthoritative {
|
|
|
|
|
if request.RootPassword != nil && request.SSHPublicKey != nil &&
|
|
|
|
|
*request.RootPassword == "" && *request.SSHPublicKey == "" {
|
|
|
|
|
return nil, "", NewError(ErrInvalid, "guest_credentials_required", "at least one root login method must remain enabled", nil)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
keys, _ := root["ssh_authorized_keys"].([]any)
|
|
|
|
|
stringKeys, _ := root["ssh_authorized_keys"].([]string)
|
|
|
|
|
if root != nil && !passwordEnabled && len(keys) == 0 && len(stringKeys) == 0 {
|
|
|
|
|
return nil, "", NewError(ErrInvalid, "guest_credentials_required", "at least one root login method must remain enabled", nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
instanceID, ok := metadata["instance-id"].(string)
|
|
|
|
|
if !ok || strings.TrimSpace(instanceID) == "" {
|
|
|
|
|
return nil, "", NewError(ErrConflict, "invalid_seed", "NoCloud instance identity is invalid", nil)
|
|
|
|
|
if !diskAuthoritative || request.RootPassword != nil {
|
|
|
|
|
userData["ssh_pwauth"] = passwordEnabled
|
|
|
|
|
updateSSHDWriteFile(userData, passwordEnabled)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hostname := ""
|
|
|
|
|
if request.Hostname != nil {
|
|
|
|
|
hostname = *request.Hostname
|
|
|
|
|
@ -195,6 +241,9 @@ func mutateSeedSettings(contents []byte, request SettingsRequest, transitionID s
|
|
|
|
|
return nil, "", NewError(ErrInvalid, "invalid_hostname", "hostname must be a lower-case DNS name", nil)
|
|
|
|
|
}
|
|
|
|
|
metadata["local-hostname"] = hostname
|
|
|
|
|
if diskAuthoritative {
|
|
|
|
|
userData["preserve_hostname"] = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
generation, err := newRandomID()
|
|
|
|
|
if err != nil {
|
|
|
|
|
@ -215,6 +264,114 @@ func mutateSeedSettings(contents []byte, request SettingsRequest, transitionID s
|
|
|
|
|
return updated, hostname, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// setDiskAuthoritativeSSHKeyMutation updates only authorized_keys. In
|
|
|
|
|
// particular it does not use cloud-init's users module: lock_passwd defaults
|
|
|
|
|
// to true there, which would silently lock a password restored from disk when
|
|
|
|
|
// the user only asked to change an SSH key.
|
|
|
|
|
func setDiskAuthoritativeSSHKeyMutation(userData map[string]any, key string) {
|
|
|
|
|
if key == "" {
|
|
|
|
|
appendSeedCommand(userData, "runcmd", diskSSHRemoveCommand)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
appendSeedCommand(userData, "bootcmd", diskSSHDirectoryCommand)
|
|
|
|
|
files, _ := userData["write_files"].([]any)
|
|
|
|
|
files = append(files, map[string]any{
|
|
|
|
|
"path": diskSSHAuthorizedKeysPath, "content": key + "\n",
|
|
|
|
|
"owner": "root:root", "permissions": "0600",
|
|
|
|
|
})
|
|
|
|
|
userData["write_files"] = files
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeDiskAuthoritativeSSHKeyMutation(userData map[string]any) {
|
|
|
|
|
files, _ := userData["write_files"].([]any)
|
|
|
|
|
kept := make([]any, 0, len(files))
|
|
|
|
|
for _, raw := range files {
|
|
|
|
|
entry, ok := raw.(map[string]any)
|
|
|
|
|
if ok && entry["path"] == diskSSHAuthorizedKeysPath {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
kept = append(kept, raw)
|
|
|
|
|
}
|
|
|
|
|
if len(kept) == 0 {
|
|
|
|
|
delete(userData, "write_files")
|
|
|
|
|
} else {
|
|
|
|
|
userData["write_files"] = kept
|
|
|
|
|
}
|
|
|
|
|
removeSeedCommand(userData, "bootcmd", diskSSHDirectoryCommand)
|
|
|
|
|
removeSeedCommand(userData, "runcmd", diskSSHRemoveCommand)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func appendSeedCommand(userData map[string]any, field, script string) {
|
|
|
|
|
commands, _ := userData[field].([]any)
|
|
|
|
|
commands = append(commands, []string{"/bin/sh", "-ec", script})
|
|
|
|
|
userData[field] = commands
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func removeSeedCommand(userData map[string]any, field, script string) {
|
|
|
|
|
commands, _ := userData[field].([]any)
|
|
|
|
|
kept := make([]any, 0, len(commands))
|
|
|
|
|
for _, raw := range commands {
|
|
|
|
|
parts, ok := raw.([]any)
|
|
|
|
|
if ok && len(parts) == 3 && parts[0] == "/bin/sh" &&
|
|
|
|
|
parts[1] == "-ec" && parts[2] == script {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
kept = append(kept, raw)
|
|
|
|
|
}
|
|
|
|
|
if len(kept) == 0 {
|
|
|
|
|
delete(userData, field)
|
|
|
|
|
} else {
|
|
|
|
|
userData[field] = kept
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sanitizeSeedForDiskAuthoritativeRecovery(contents []byte,
|
|
|
|
|
transitionID string) ([]byte, error) {
|
|
|
|
|
if !operationIdentifierPattern.MatchString(transitionID) {
|
|
|
|
|
return nil, NewError(ErrInvalid, "invalid_operation_id", "backup recovery transition id is invalid", nil)
|
|
|
|
|
}
|
|
|
|
|
files, err := readSeedISO(contents)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
const header = "#cloud-config\n"
|
|
|
|
|
if !bytes.HasPrefix(files.UserData, []byte(header)) {
|
|
|
|
|
return nil, NewError(ErrConflict, "invalid_seed", "NoCloud user-data is not managed by Virt Cloud", nil)
|
|
|
|
|
}
|
|
|
|
|
var original map[string]any
|
|
|
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.UserData[len(header):]), &original); err != nil {
|
|
|
|
|
return nil, NewError(ErrConflict, "invalid_seed", "NoCloud user-data is invalid", err)
|
|
|
|
|
}
|
|
|
|
|
userData := map[string]any{
|
|
|
|
|
"preserve_hostname": true,
|
|
|
|
|
}
|
|
|
|
|
for _, key := range []string{"updates", "bootcmd"} {
|
|
|
|
|
if value, exists := original[key]; exists {
|
|
|
|
|
userData[key] = value
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var metadata map[string]any
|
|
|
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.MetaData), &metadata); err != nil {
|
|
|
|
|
return nil, NewError(ErrConflict, "invalid_seed", "NoCloud meta-data is invalid", err)
|
|
|
|
|
}
|
|
|
|
|
instanceID, ok := metadata["instance-id"].(string)
|
|
|
|
|
if !ok || strings.TrimSpace(instanceID) == "" {
|
|
|
|
|
return nil, NewError(ErrConflict, "invalid_seed", "NoCloud instance identity is invalid", nil)
|
|
|
|
|
}
|
|
|
|
|
delete(metadata, "local-hostname")
|
|
|
|
|
metadata[diskGuestIdentityMetadata] = diskGuestIdentityValue
|
|
|
|
|
metadata[backupRecoveryOperationMetadata] = transitionID
|
|
|
|
|
files.UserData, err = marshalCloudConfig(userData)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
files.MetaData, err = marshalJSONLine(metadata)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return buildSeedISO(files)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func updateSSHDWriteFile(userData map[string]any, passwordEnabled bool) {
|
|
|
|
|
content := "PermitRootLogin prohibit-password\nPasswordAuthentication no\nKbdInteractiveAuthentication no\n"
|
|
|
|
|
if passwordEnabled {
|
|
|
|
|
|