You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
488 lines
15 KiB
Go
488 lines
15 KiB
Go
package cloudinit
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"net/netip"
|
|
"regexp"
|
|
"strings"
|
|
"unicode/utf8"
|
|
|
|
"github.com/GehirnInc/crypt/sha512_crypt"
|
|
"github.com/bgrewell/iso-kit/pkg/iso9660"
|
|
)
|
|
|
|
const (
|
|
StartupScriptPath = "/var/lib/virt-cloud/startup.sh"
|
|
StartupRunnerPath = "/var/lib/virt-cloud/startup-runner.sh"
|
|
maxSeedBytes = 2 << 20
|
|
)
|
|
|
|
// StartupRunner deliberately records the attempt before starting the
|
|
// administrator-provided script. Settings changes may rotate NoCloud's
|
|
// instance identity, but success, failure, interruption, and reboot must all
|
|
// remain at-most-once.
|
|
const StartupRunner = `#!/bin/sh
|
|
umask 077
|
|
state=/var/lib/virt-cloud/startup
|
|
mkdir -p "$state" || exit 1
|
|
if ! mkdir "$state/started" 2>/dev/null; then
|
|
[ -d "$state/started" ] && exit 0
|
|
exit 1
|
|
fi
|
|
/bin/bash /var/lib/virt-cloud/startup.sh > "$state/output.log" 2>&1
|
|
result=$?
|
|
printf '%s\n' "$result" > "$state/exit-code"
|
|
exit "$result"
|
|
`
|
|
|
|
var (
|
|
vmNamePattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,62}$`)
|
|
hostLabel = regexp.MustCompile(`^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$`)
|
|
passwordHashRE = regexp.MustCompile(`^\$6\$rounds=100000\$[./0-9A-Za-z]{16}\$[./0-9A-Za-z]{86}$`)
|
|
reservedIPv4Net = mustPrefixes(
|
|
"0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8",
|
|
"169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24", "192.0.2.0/24",
|
|
"192.168.0.0/16", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24",
|
|
"224.0.0.0/4", "240.0.0.0/4",
|
|
)
|
|
)
|
|
|
|
type Network struct {
|
|
MAC string `json:"mac"`
|
|
PublicCIDR string `json:"public_ip_cidr"`
|
|
Gateway string `json:"gateway"`
|
|
DNSServers []string `json:"dns_servers"`
|
|
InterfaceKey string `json:"interface_key,omitempty"`
|
|
}
|
|
|
|
type Request struct {
|
|
UUID string
|
|
Name string
|
|
Hostname string
|
|
GuestSSHPublicKey string
|
|
RootPassword string
|
|
StartupScript string
|
|
Debian12NetworkFix bool
|
|
Network Network
|
|
}
|
|
|
|
type Files struct {
|
|
UserData []byte
|
|
MetaData []byte
|
|
NetworkConfig []byte
|
|
}
|
|
|
|
type writeFile struct {
|
|
Path string `json:"path"`
|
|
Content string `json:"content"`
|
|
Owner string `json:"owner,omitempty"`
|
|
Permissions string `json:"permissions"`
|
|
}
|
|
|
|
type rootUser struct {
|
|
Name string `json:"name"`
|
|
LockPasswd bool `json:"lock_passwd"`
|
|
Shell string `json:"shell"`
|
|
SSHAuthorizedKeys []string `json:"ssh_authorized_keys,omitempty"`
|
|
HashedPassword string `json:"hashed_passwd,omitempty"`
|
|
}
|
|
|
|
// BuildFiles returns the exact three NoCloud files. Sensitive inputs are
|
|
// consumed in memory: plaintext passwords are replaced with SHA-512 crypt
|
|
// hashes and callers must not persist Request.
|
|
func BuildFiles(request Request) (Files, error) {
|
|
if err := validateRequest(request); err != nil {
|
|
return Files{}, err
|
|
}
|
|
hostname := request.Hostname
|
|
if hostname == "" {
|
|
hostname = defaultHostname(request.Name)
|
|
}
|
|
user := rootUser{Name: "root", Shell: "/bin/bash", LockPasswd: request.RootPassword == ""}
|
|
if request.GuestSSHPublicKey != "" {
|
|
user.SSHAuthorizedKeys = []string{request.GuestSSHPublicKey}
|
|
}
|
|
if request.RootPassword != "" {
|
|
hash, err := hashPassword(request.RootPassword)
|
|
if err != nil {
|
|
return Files{}, err
|
|
}
|
|
user.HashedPassword = hash
|
|
}
|
|
passwordEnabled := request.RootPassword != ""
|
|
sshd := "PermitRootLogin prohibit-password\nPasswordAuthentication no\nKbdInteractiveAuthentication no\n"
|
|
if passwordEnabled {
|
|
sshd = "PermitRootLogin yes\nPasswordAuthentication yes\nKbdInteractiveAuthentication no\n"
|
|
}
|
|
files := []writeFile{{
|
|
Path: "/etc/ssh/sshd_config.d/00-virt-cloud-root.conf", Content: sshd, Permissions: "0644",
|
|
}}
|
|
userData := map[string]any{
|
|
"users": []rootUser{user},
|
|
"ssh_pwauth": passwordEnabled,
|
|
"disable_root": false,
|
|
"write_files": files,
|
|
"updates": map[string]any{
|
|
"network": map[string]any{"when": []string{"boot"}},
|
|
},
|
|
}
|
|
if passwordEnabled {
|
|
userData["chpasswd"] = map[string]any{
|
|
"expire": false,
|
|
"users": []map[string]string{{
|
|
"name": "root", "password": user.HashedPassword, "type": "hash",
|
|
}},
|
|
}
|
|
}
|
|
if request.StartupScript != "" {
|
|
files = append(files,
|
|
writeFile{Path: StartupScriptPath, Content: request.StartupScript, Owner: "root:root", Permissions: "0600"},
|
|
writeFile{Path: StartupRunnerPath, Content: StartupRunner, Owner: "root:root", Permissions: "0700"},
|
|
)
|
|
userData["write_files"] = files
|
|
userData["runcmd"] = [][]string{{"/bin/sh", StartupRunnerPath}}
|
|
}
|
|
if request.Debian12NetworkFix {
|
|
userData["bootcmd"] = [][]string{{"/bin/sh", "-ec", strings.Join([]string{
|
|
"systemd-machine-id-setup",
|
|
"netplan generate",
|
|
"systemctl unmask systemd-networkd.service",
|
|
"systemctl enable systemd-networkd.service",
|
|
"systemctl start --no-block systemd-networkd.service",
|
|
}, "\n")}}
|
|
}
|
|
|
|
metadata := map[string]any{
|
|
"instance-id": request.UUID, "local-hostname": hostname,
|
|
"virt-cloud-network-policy": "boot-v1",
|
|
}
|
|
if request.Debian12NetworkFix {
|
|
metadata["dsmode"] = "local"
|
|
}
|
|
interfaceKey := request.Network.InterfaceKey
|
|
if interfaceKey == "" {
|
|
interfaceKey = "public0"
|
|
}
|
|
networkConfig := map[string]any{
|
|
"version": 2,
|
|
"ethernets": map[string]any{
|
|
interfaceKey: map[string]any{
|
|
"match": map[string]string{"macaddress": strings.ToLower(request.Network.MAC)},
|
|
"dhcp4": false,
|
|
"dhcp6": false,
|
|
"accept-ra": false,
|
|
"addresses": []string{request.Network.PublicCIDR},
|
|
"routes": []map[string]string{{"to": "0.0.0.0/0", "via": request.Network.Gateway}},
|
|
"nameservers": map[string]any{"addresses": request.Network.DNSServers},
|
|
},
|
|
},
|
|
}
|
|
if request.Debian12NetworkFix {
|
|
networkConfig["renderer"] = "networkd"
|
|
}
|
|
encodedUser, err := json.Marshal(userData)
|
|
if err != nil {
|
|
return Files{}, fmt.Errorf("encode NoCloud user-data: %w", err)
|
|
}
|
|
encodedMeta, err := json.Marshal(metadata)
|
|
if err != nil {
|
|
return Files{}, fmt.Errorf("encode NoCloud meta-data: %w", err)
|
|
}
|
|
encodedNetwork, err := json.Marshal(networkConfig)
|
|
if err != nil {
|
|
return Files{}, fmt.Errorf("encode NoCloud network-config: %w", err)
|
|
}
|
|
return Files{
|
|
UserData: append(append([]byte("#cloud-config\n"), encodedUser...), '\n'),
|
|
MetaData: append(encodedMeta, '\n'),
|
|
NetworkConfig: append(encodedNetwork, '\n'),
|
|
}, nil
|
|
}
|
|
|
|
func BuildISO(request Request) ([]byte, error) {
|
|
files, err := BuildFiles(request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
image, err := iso9660.Create("CIDATA")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create NoCloud ISO: %w", err)
|
|
}
|
|
for name, content := range map[string][]byte{
|
|
"user-data": files.UserData, "meta-data": files.MetaData, "network-config": files.NetworkConfig,
|
|
} {
|
|
if err := image.AddFile(name, content); err != nil {
|
|
return nil, fmt.Errorf("add NoCloud %s: %w", name, err)
|
|
}
|
|
}
|
|
output := &boundedWriterAt{limit: maxSeedBytes}
|
|
if err := image.Save(output); err != nil {
|
|
return nil, fmt.Errorf("write NoCloud ISO: %w", err)
|
|
}
|
|
if len(output.data) == 0 || len(output.data) > maxSeedBytes {
|
|
return nil, errors.New("NoCloud ISO size is invalid")
|
|
}
|
|
return output.data, nil
|
|
}
|
|
|
|
func validateRequest(request Request) error {
|
|
if _, err := parseUUID(request.UUID); err != nil {
|
|
return err
|
|
}
|
|
if !vmNamePattern.MatchString(request.Name) {
|
|
return errors.New("VM name is invalid")
|
|
}
|
|
if request.Hostname != "" {
|
|
if err := validateHostname(request.Hostname); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := validateSSHKey(request.GuestSSHPublicKey); err != nil {
|
|
return err
|
|
}
|
|
if request.RootPassword == "" && request.GuestSSHPublicKey == "" {
|
|
return errors.New("root password or SSH public key is required")
|
|
}
|
|
if request.RootPassword != "" {
|
|
if err := validatePassword(request.RootPassword); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if !utf8.ValidString(request.StartupScript) || strings.ContainsRune(request.StartupScript, 0) || len(request.StartupScript) > 32<<10 {
|
|
return errors.New("startup script must be valid NUL-free UTF-8 up to 32768 bytes")
|
|
}
|
|
return validateNetwork(request.Network)
|
|
}
|
|
|
|
func validateNetwork(value Network) error {
|
|
hardware, err := net.ParseMAC(value.MAC)
|
|
if err != nil || len(hardware) != 6 || hardware[0]&1 != 0 || allZero(hardware) {
|
|
return errors.New("public interface MAC is invalid")
|
|
}
|
|
prefix, err := netip.ParsePrefix(value.PublicCIDR)
|
|
if err != nil || !prefix.Addr().Is4() || prefix.Bits() < 1 || prefix.Bits() > 30 {
|
|
return errors.New("public_ip_cidr must be an IPv4 host address with /1 through /30 prefix")
|
|
}
|
|
if !isPublicIPv4(prefix.Addr()) {
|
|
return errors.New("public_ip_cidr is not a public IPv4 address")
|
|
}
|
|
network := prefix.Masked()
|
|
broadcast := lastAddress(network)
|
|
if prefix.Addr() == network.Addr() || prefix.Addr() == broadcast {
|
|
return errors.New("public_ip_cidr cannot select the network or broadcast address")
|
|
}
|
|
gateway, err := netip.ParseAddr(value.Gateway)
|
|
if err != nil || !gateway.Is4() || !network.Contains(gateway) || gateway == prefix.Addr() || gateway == network.Addr() || gateway == broadcast {
|
|
return errors.New("gateway must be another usable address in the public subnet")
|
|
}
|
|
if len(value.DNSServers) < 1 || len(value.DNSServers) > 4 {
|
|
return errors.New("one to four DNS servers are required")
|
|
}
|
|
seen := make(map[netip.Addr]struct{}, len(value.DNSServers))
|
|
for _, raw := range value.DNSServers {
|
|
address, err := netip.ParseAddr(raw)
|
|
if err != nil || !address.Is4() || !isPublicIPv4(address) {
|
|
return errors.New("DNS servers must be public IPv4 addresses")
|
|
}
|
|
if _, exists := seen[address]; exists {
|
|
return errors.New("DNS servers must be unique")
|
|
}
|
|
seen[address] = struct{}{}
|
|
}
|
|
if value.InterfaceKey != "" && !regexp.MustCompile(`^[a-z][a-z0-9_-]{0,31}$`).MatchString(value.InterfaceKey) {
|
|
return errors.New("interface key is invalid")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateHostname(value string) error {
|
|
if value != strings.ToLower(value) || len(value) < 1 || len(value) > 253 || strings.HasSuffix(value, ".") {
|
|
return errors.New("hostname is invalid")
|
|
}
|
|
for _, label := range strings.Split(value, ".") {
|
|
if !hostLabel.MatchString(label) {
|
|
return errors.New("hostname is invalid")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func defaultHostname(name string) string {
|
|
value := strings.Trim(strings.Map(func(r rune) rune {
|
|
if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '-' {
|
|
return r
|
|
}
|
|
if r >= 'A' && r <= 'Z' {
|
|
return r + ('a' - 'A')
|
|
}
|
|
return '-'
|
|
}, name), "-")
|
|
if len(value) > 63 {
|
|
value = value[:63]
|
|
}
|
|
return value
|
|
}
|
|
|
|
func validatePassword(value string) error {
|
|
if len(value) < 12 || len(value) > 128 {
|
|
return errors.New("root password must contain 12-128 printable ASCII characters")
|
|
}
|
|
var upper, lower, digit, symbol bool
|
|
for _, char := range []byte(value) {
|
|
if char < 0x21 || char > 0x7e {
|
|
return errors.New("root password must contain 12-128 printable ASCII characters")
|
|
}
|
|
switch {
|
|
case char >= 'A' && char <= 'Z':
|
|
upper = true
|
|
case char >= 'a' && char <= 'z':
|
|
lower = true
|
|
case char >= '0' && char <= '9':
|
|
digit = true
|
|
default:
|
|
symbol = true
|
|
}
|
|
}
|
|
if !upper || !lower || !digit || !symbol {
|
|
return errors.New("root password must contain upper, lower, digit, and symbol characters")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func hashPassword(password string) (string, error) {
|
|
saltBytes := make([]byte, 8)
|
|
if _, err := rand.Read(saltBytes); err != nil {
|
|
return "", fmt.Errorf("generate password salt: %w", err)
|
|
}
|
|
// SHA-crypt accepts only the crypt base64 alphabet. Hex is a uniformly
|
|
// generated subset of that alphabet and always produces exactly 16 chars.
|
|
salt := hex.EncodeToString(saltBytes)
|
|
result, err := sha512_crypt.New().Generate([]byte(password), []byte("$6$rounds=100000$"+salt+"$"))
|
|
if err != nil {
|
|
return "", fmt.Errorf("hash root password: %w", err)
|
|
}
|
|
if !passwordHashRE.MatchString(result) {
|
|
return "", errors.New("generated root password hash is invalid")
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func validateSSHKey(value string) error {
|
|
if value == "" {
|
|
return nil
|
|
}
|
|
if value != strings.TrimSpace(value) || len(value) > 16384 || strings.ContainsAny(value, "\r\n\x00") {
|
|
return errors.New("SSH public key must be one bounded OpenSSH line")
|
|
}
|
|
fields := strings.Fields(value)
|
|
if len(fields) < 2 || len(fields) > 3 {
|
|
return errors.New("SSH public key is invalid")
|
|
}
|
|
allowed := map[string]bool{
|
|
"ssh-ed25519": true, "ssh-rsa": true,
|
|
"ecdsa-sha2-nistp256": true, "ecdsa-sha2-nistp384": true, "ecdsa-sha2-nistp521": true,
|
|
}
|
|
if !allowed[fields[0]] {
|
|
return errors.New("SSH public key type is unsupported")
|
|
}
|
|
blob, err := base64.StdEncoding.DecodeString(fields[1])
|
|
if err != nil || len(blob) < 8 || len(blob) > 8192 {
|
|
return errors.New("SSH public key payload is invalid")
|
|
}
|
|
embedded, rest, ok := sshString(blob)
|
|
if !ok || string(embedded) != fields[0] || len(rest) == 0 {
|
|
return errors.New("SSH public key type does not match its payload")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func sshString(value []byte) ([]byte, []byte, bool) {
|
|
if len(value) < 4 {
|
|
return nil, nil, false
|
|
}
|
|
size := int(binary.BigEndian.Uint32(value[:4]))
|
|
if size < 1 || size > 8192 || 4+size > len(value) {
|
|
return nil, nil, false
|
|
}
|
|
return value[4 : 4+size], value[4+size:], true
|
|
}
|
|
|
|
func parseUUID(value string) ([16]byte, error) {
|
|
var result [16]byte
|
|
if len(value) != 36 || value[8] != '-' || value[13] != '-' || value[18] != '-' || value[23] != '-' {
|
|
return result, errors.New("UUID is invalid")
|
|
}
|
|
compact := strings.ReplaceAll(value, "-", "")
|
|
decoded, err := hex.DecodeString(compact)
|
|
if err != nil || len(decoded) != len(result) {
|
|
return result, errors.New("UUID is invalid")
|
|
}
|
|
copy(result[:], decoded)
|
|
if result[6]>>4 < 1 || result[6]>>4 > 5 || result[8]>>6 != 2 {
|
|
return [16]byte{}, errors.New("UUID is invalid")
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func isPublicIPv4(address netip.Addr) bool {
|
|
if !address.Is4() || !address.IsGlobalUnicast() {
|
|
return false
|
|
}
|
|
for _, prefix := range reservedIPv4Net {
|
|
if prefix.Contains(address) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func mustPrefixes(values ...string) []netip.Prefix {
|
|
result := make([]netip.Prefix, 0, len(values))
|
|
for _, value := range values {
|
|
result = append(result, netip.MustParsePrefix(value))
|
|
}
|
|
return result
|
|
}
|
|
|
|
func lastAddress(prefix netip.Prefix) netip.Addr {
|
|
bytes := prefix.Addr().As4()
|
|
value := binary.BigEndian.Uint32(bytes[:])
|
|
value |= ^uint32(0) >> prefix.Bits()
|
|
var result [4]byte
|
|
binary.BigEndian.PutUint32(result[:], value)
|
|
return netip.AddrFrom4(result)
|
|
}
|
|
|
|
func allZero(value []byte) bool {
|
|
for _, item := range value {
|
|
if item != 0 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
type boundedWriterAt struct {
|
|
data []byte
|
|
limit int
|
|
}
|
|
|
|
func (w *boundedWriterAt) WriteAt(value []byte, offset int64) (int, error) {
|
|
if offset < 0 || offset > int64(w.limit) || len(value) > w.limit-int(offset) {
|
|
return 0, errors.New("NoCloud ISO exceeds size limit")
|
|
}
|
|
end := int(offset) + len(value)
|
|
if end > len(w.data) {
|
|
w.data = append(w.data, make([]byte, end-len(w.data))...)
|
|
}
|
|
copy(w.data[int(offset):end], value)
|
|
return len(value), nil
|
|
}
|