forked from rancher-sandbox/rancher-desktop-opensuse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserdata.go
More file actions
225 lines (211 loc) · 7.72 KB
/
userdata.go
File metadata and controls
225 lines (211 loc) · 7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"github.com/goccy/go-yaml"
)
// Escape the name of a mount point systemd-style; see systemd.unit(5)
func escapeSystemdMountName(input string) string {
input = filepath.Clean(input)
input = strings.Trim(input, "/")
if input == "" {
return "-" // Special case for the root directory
}
var builder strings.Builder
for _, r := range input {
switch {
case r == '/':
builder.WriteRune('-')
case 'A' <= r && r <= 'Z', 'a' <= r && r <= 'z', '0' <= r && r <= '9':
builder.WriteRune(r)
case r == ':', r == '_', r == '.':
builder.WriteRune(r)
default:
builder.WriteString(fmt.Sprintf("\\x%02x", r))
}
}
result := builder.String()
if strings.HasPrefix(result, ".") {
return "\\x2e" + result[1:]
}
return result
}
// Load /mnt/lima-cidata/user-data; returns a list of systemd units that must
// be started after.
func LoadUserData(ctx context.Context) ([]string, error) {
var units []string
var userData struct {
Users []struct {
Name string `yaml:"name"`
UID string `yaml:"uid"`
GECOS string `yaml:"gecos"`
HomeDir string `yaml:"homedir"`
Shell string `yaml:"shell"`
Sudo string `yaml:"sudo"`
LockPasswd bool `yaml:"lock_passwd"`
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"`
SSHAuthorizedKeysFallback []string `yaml:"ssh-authorized-keys"`
} `yaml:"users"`
Mounts [][]string `yaml:"mounts"`
WriteFiles []struct {
Content string `yaml:"content"`
Owner string `yaml:"owner"`
Path string `yaml:"path"`
Permissions string `yaml:"permissions"`
} `yaml:"write_files"`
ManageResolveConf bool `yaml:"manage_resolv_conf"`
ResolveConf struct {
NameServers []string `yaml:"nameservers"`
} `yaml:"resolv_conf"`
}
file, err := os.Open("/mnt/lima-cidata/user-data")
if err != nil {
return nil, fmt.Errorf("failed to read user-data: %w", err)
}
defer file.Close()
if err := yaml.NewDecoder(file).DecodeContext(ctx, &userData); err != nil {
return nil, fmt.Errorf("failed to unmarshal user-data: %w", err)
}
// Process users
for _, userEntry := range userData.Users {
userInfo, err := user.LookupId(userEntry.UID)
if err != nil {
// User does not exist yet; create it.
slog.InfoContext(ctx, "creating user", "user", userEntry.Name)
err = runCommand(ctx, "/usr/sbin/useradd",
"--home-dir", userEntry.HomeDir,
"--create-home",
"--comment", userEntry.GECOS,
"--uid", userEntry.UID,
"--shell", userEntry.Shell,
userEntry.Name)
if err != nil {
return nil, fmt.Errorf("failed to create user %q: %w", userEntry.Name, err)
}
userInfo, err = user.LookupId(userEntry.UID)
if err != nil {
return nil, fmt.Errorf("failed to look up newly created user %q: %w", userEntry.Name, err)
}
} else {
slog.InfoContext(ctx, "user already exists", "user", userEntry.Name)
}
uid, err := strconv.ParseInt(userInfo.Uid, 10, 32)
if err != nil {
return nil, fmt.Errorf("failed to parse user %q uid %q: %w", userEntry.Name, userInfo.Uid, err)
}
gid, err := strconv.ParseInt(userInfo.Gid, 10, 32)
if err != nil {
return nil, fmt.Errorf("failed to parse user %q gid %q: %w", userEntry.Name, userInfo.Gid, err)
}
// Add user to sudoers
if userEntry.Sudo != "" {
slog.InfoContext(ctx, "adding user to sudoers", "user", userEntry.Name)
err := os.WriteFile(
fmt.Sprintf("/etc/sudoers.d/90-lima-user-%s", userEntry.Name),
[]byte(userEntry.Name + " " + userEntry.Sudo),
0o400)
if err != nil {
return nil, fmt.Errorf("failed to create sudoers file for %q: %w", userEntry.Name, err)
}
}
// Create authorized_keys
slog.InfoContext(ctx, "creating authorized_keys", "user", userEntry.Name)
sshDir := filepath.Join(userEntry.HomeDir, ".ssh")
if err := os.MkdirAll(sshDir, 0o700); err != nil {
return nil, fmt.Errorf("failed to create %q .ssh: %w", userEntry.Name, err)
}
if err := os.Chown(sshDir, int(uid), int(gid)); err != nil {
return nil, err
}
sshAuthorizedKeys := append(userEntry.SSHAuthorizedKeys, userEntry.SSHAuthorizedKeysFallback...)
err = os.WriteFile(
filepath.Join(sshDir, "authorized_keys"),
[]byte(strings.Join(sshAuthorizedKeys, "\n")),
0o600)
if err != nil {
return nil, fmt.Errorf("failed to write %q authorized_keys: %w", userEntry.Name, err)
}
if err := os.Chown(filepath.Join(sshDir, "authorized_keys"), int(uid), int(gid)); err != nil {
return nil, err
}
}
// Process mounts
for i, mount := range userData.Mounts {
if len(mount) < 2 {
return nil, fmt.Errorf("mount #%d is too short: %+v", i, mount)
}
slog.InfoContext(ctx, "creating mount", "where", mount[1], "what", mount[0])
lines := []string{
"[Unit]",
"After=local-fs.target",
"[Install]",
"WantedBy=default.target",
"[Mount]",
fmt.Sprintf("What=%s", mount[0]),
fmt.Sprintf("Where=%s", mount[1]),
}
if len(mount) > 2 {
lines = append(lines, fmt.Sprintf("Type=%s", mount[2]))
}
if len(mount) > 3 {
lines = append(lines, fmt.Sprintf("Options=%s", mount[3]))
}
output := []byte(strings.Join(append(lines, ""), "\n"))
filename := filepath.Join("/run/systemd/system", escapeSystemdMountName(mount[1])+".mount")
if err := os.WriteFile(filename, output, 0o644); err != nil {
return nil, fmt.Errorf("failed to create mount unit %q: %w", filename, err)
}
units = append(units, escapeSystemdMountName(mount[1]) + ".mount")
}
// Process files
for _, writeFile := range userData.WriteFiles {
slog.InfoContext(ctx, "writing file", "path", writeFile.Path)
fileMode, err := strconv.ParseUint(writeFile.Permissions, 8, 32)
if err != nil {
return nil, fmt.Errorf("failed to parse permissions for %s: %w", writeFile.Path, err)
}
if err := os.MkdirAll(filepath.Dir(writeFile.Path), 0o755); err != nil {
return nil, fmt.Errorf("failed to create directory for %s: %w", writeFile.Path, err)
}
if err := os.WriteFile(writeFile.Path, []byte(writeFile.Content), os.FileMode(fileMode)); err != nil {
return nil, fmt.Errorf("failed to write file %s: %w", writeFile.Path, err)
}
var uid int64
gid := int64(-1)
userName, groupName, _ := strings.Cut(writeFile.Owner, ":")
if u, err := user.Lookup(userName); err != nil {
return nil, fmt.Errorf("failed to write file %s: failed to lookup user %s: %w", writeFile.Path, userName, err)
} else if uid, err = strconv.ParseInt(u.Uid, 10, 32); err != nil {
return nil, fmt.Errorf("failed to write file %s: user %s has non-numeric uid %s", writeFile.Path, userName, u.Uid)
}
if groupName != "" {
if g, err := user.LookupGroup(groupName); err != nil {
return nil, fmt.Errorf("failed to write file %s: failed to lookup group %s: %w", writeFile.Path, groupName, err)
} else if gid, err = strconv.ParseInt(g.Gid, 10, 32); err != nil {
return nil, fmt.Errorf("failed to write file %s: group %s has non-numeric gid %s", writeFile.Path, groupName, g.Gid)
}
}
if err := os.Chown(writeFile.Path, int(uid), int(gid)); err != nil {
return nil, fmt.Errorf("failed to change file %s owner: %w", writeFile.Path, err)
}
}
// Process name server overrides
if userData.ManageResolveConf {
slog.InfoContext(ctx, "updating name servers", "name servers", userData.ResolveConf.NameServers)
contents := fmt.Sprintf("[Resolve]\nDNS=%s\n", strings.Join(userData.ResolveConf.NameServers, " "))
filePath := "/run/systemd/resolved.conf.d/10-rd-init.conf"
if err := os.MkdirAll(filepath.Dir(filePath), 0o755); err != nil {
return nil, err
}
if err := os.WriteFile(filePath, []byte(contents), 0o644); err != nil {
return nil, err
}
}
return units, nil
}