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.
164 lines
5.4 KiB
Go
164 lines
5.4 KiB
Go
package cloudinit
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/bgrewell/iso-kit/pkg/iso9660"
|
|
)
|
|
|
|
func validSeedRequest() Request {
|
|
keyBlob := make([]byte, 4+len("ssh-ed25519")+4+32)
|
|
binary.BigEndian.PutUint32(keyBlob[:4], uint32(len("ssh-ed25519")))
|
|
copy(keyBlob[4:], "ssh-ed25519")
|
|
offset := 4 + len("ssh-ed25519")
|
|
binary.BigEndian.PutUint32(keyBlob[offset:offset+4], 32)
|
|
for index := offset + 4; index < len(keyBlob); index++ {
|
|
keyBlob[index] = byte(index)
|
|
}
|
|
return Request{
|
|
UUID: "123e4567-e89b-42d3-a456-426614174000",
|
|
Name: "vm-test",
|
|
Hostname: "vm-test.example.com",
|
|
GuestSSHPublicKey: "ssh-ed25519 " + base64.StdEncoding.EncodeToString(keyBlob) + " user@test",
|
|
RootPassword: "StrongPassword!123",
|
|
StartupScript: "#!/bin/bash\necho '中文'\n",
|
|
Network: Network{
|
|
MAC: "52:54:00:12:34:56", PublicCIDR: "8.8.8.10/24", Gateway: "8.8.8.1",
|
|
DNSServers: []string{"1.1.1.1", "9.9.9.9"},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestBuildFilesEmbedsAtMostOnceStartupRunnerWithoutPlaintextPassword(t *testing.T) {
|
|
request := validSeedRequest()
|
|
files, err := BuildFiles(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if bytes.Contains(files.UserData, []byte(request.RootPassword)) {
|
|
t.Fatal("NoCloud user-data contains the plaintext root password")
|
|
}
|
|
if !bytes.HasPrefix(files.UserData, []byte("#cloud-config\n")) {
|
|
t.Fatalf("unexpected user-data prefix: %q", files.UserData[:20])
|
|
}
|
|
var document map[string]any
|
|
if err := json.Unmarshal(bytes.TrimPrefix(files.UserData, []byte("#cloud-config\n")), &document); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
writeFiles, ok := document["write_files"].([]any)
|
|
if !ok || len(writeFiles) != 3 {
|
|
t.Fatalf("write_files = %#v", document["write_files"])
|
|
}
|
|
entries := make(map[string]map[string]any)
|
|
for _, raw := range writeFiles {
|
|
entry := raw.(map[string]any)
|
|
entries[entry["path"].(string)] = entry
|
|
}
|
|
if entries[StartupScriptPath]["content"] != request.StartupScript || entries[StartupScriptPath]["permissions"] != "0600" {
|
|
t.Fatalf("startup script entry = %#v", entries[StartupScriptPath])
|
|
}
|
|
if entries[StartupRunnerPath]["content"] != StartupRunner || entries[StartupRunnerPath]["permissions"] != "0700" {
|
|
t.Fatalf("startup runner entry = %#v", entries[StartupRunnerPath])
|
|
}
|
|
if !strings.Contains(StartupRunner, `mkdir "$state/started"`) || !strings.Contains(StartupRunner, `printf '%s\n' "$result"`) {
|
|
t.Fatalf("startup runner lost durable at-most-once markers: %q", StartupRunner)
|
|
}
|
|
runcmd := document["runcmd"].([]any)
|
|
command := runcmd[0].([]any)
|
|
if command[0] != "/bin/sh" || command[1] != StartupRunnerPath {
|
|
t.Fatalf("runcmd = %#v", runcmd)
|
|
}
|
|
users := document["users"].([]any)
|
|
hash := users[0].(map[string]any)["hashed_passwd"].(string)
|
|
if !passwordHashRE.MatchString(hash) {
|
|
t.Fatalf("root password hash has unexpected format: %q", hash)
|
|
}
|
|
}
|
|
|
|
func TestBuildISOHasExactNoCloudRockRidgeNames(t *testing.T) {
|
|
imageBytes, err := BuildISO(validSeedRequest())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(imageBytes) == 0 || len(imageBytes) > maxSeedBytes {
|
|
t.Fatalf("ISO length = %d", len(imageBytes))
|
|
}
|
|
image, err := iso9660.Open(bytes.NewReader(imageBytes))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"user-data", "meta-data", "network-config"} {
|
|
value, err := image.ReadFile(name)
|
|
if err != nil {
|
|
t.Fatalf("read %s: %v", name, err)
|
|
}
|
|
if len(value) == 0 {
|
|
t.Fatalf("%s is empty", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBuildFilesRejectsStartupOverflowAndInvalidPublicNetwork(t *testing.T) {
|
|
request := validSeedRequest()
|
|
request.StartupScript = strings.Repeat("x", (32<<10)+1)
|
|
if _, err := BuildFiles(request); err == nil {
|
|
t.Fatal("startup script over 32 KiB was accepted")
|
|
}
|
|
request = validSeedRequest()
|
|
request.Network.PublicCIDR = "192.168.1.10/24"
|
|
request.Network.Gateway = "192.168.1.1"
|
|
if _, err := BuildFiles(request); err == nil {
|
|
t.Fatal("private address was accepted as public network")
|
|
}
|
|
}
|
|
|
|
func TestDebian12NetworkFixRunsInInitLocalWithNetworkd(t *testing.T) {
|
|
request := validSeedRequest()
|
|
request.Debian12NetworkFix = true
|
|
files, err := BuildFiles(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var metadata map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.MetaData), &metadata); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if metadata["dsmode"] != "local" {
|
|
t.Fatalf("dsmode = %#v", metadata["dsmode"])
|
|
}
|
|
var network map[string]any
|
|
if err := json.Unmarshal(bytes.TrimSpace(files.NetworkConfig), &network); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if network["renderer"] != "networkd" {
|
|
t.Fatalf("renderer = %#v", network["renderer"])
|
|
}
|
|
var userData map[string]any
|
|
if err := json.Unmarshal(bytes.TrimPrefix(files.UserData, []byte("#cloud-config\n")), &userData); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
encoded, err := json.Marshal(userData["bootcmd"])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, command := range []string{"systemd-machine-id-setup", "netplan generate", "systemctl start --no-block systemd-networkd.service"} {
|
|
if !bytes.Contains(encoded, []byte(command)) {
|
|
t.Fatalf("bootcmd missing %q: %s", command, encoded)
|
|
}
|
|
}
|
|
|
|
request.Debian12NetworkFix = false
|
|
ordinary, err := BuildFiles(request)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if bytes.Contains(ordinary.MetaData, []byte(`"dsmode"`)) || bytes.Contains(ordinary.NetworkConfig, []byte(`"renderer"`)) || bytes.Contains(ordinary.UserData, []byte(`"bootcmd"`)) {
|
|
t.Fatal("Debian 12 workaround leaked into ordinary templates")
|
|
}
|
|
}
|