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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package cloudinit
import (
"encoding/json"
"fmt"
"path"
"slices"
"strconv"
"strings"
"time"
)
const (
RequestReleaseWait = 3 * time.Minute
RequestRevokeWait = 15 * time.Minute
GuestRequesterReleaseEach = RequestReleaseWait + RequestRevokeWait + 2*time.Minute
GuestRequesterReleaseUnitTimeout = GuestRequesterReleaseEach + time.Minute
GuestRequesterStopTimeout = 2 * time.Minute
GuestRequesterReleaseTimeout = GuestRequesterStopTimeout + GuestRequesterReleaseUnitTimeout + time.Minute
)
const (
DefaultAgentEnvFile = "/etc/atirc/atirc.env"
AssociationRefDir = "/etc/socialweb-computer"
DefaultAssociationRefFile = AssociationRefDir + "/association-ref"
DefaultAssociationRequestFile = AssociationRefDir + "/association-request.json"
GuestRequesterAtircEnvFile = AssociationRefDir + "/r2-atirc.env"
GuestRequesterGroup = "socialweb-r2"
GuestRequesterDir = "/run/socialweb-r2"
GuestRequesterUser = "r2"
GuestRequesterUnit = "socialweb-r2.service"
GuestRequesterReleaseUnit = "socialweb-r2-release.service"
GuestRequesterRunPath = "/usr/local/bin/socialweb-r2-run.sh"
GuestRequesterReleasePath = "/usr/local/bin/socialweb-r2-release.sh"
GuestRequesterStartEnv = GuestRequesterDir + "/r2-start.env"
GuestAgentAssociationPath = GuestAgentConfigDir + "/association.json"
GuestRequesterDIDPath = GuestAgentStateDir + "/requester-did"
SecretsFetchScriptPath = "/usr/local/bin/setup-secrets.sh"
SecretsRefetchEnv = "SECRETS_REFETCH"
AgentUnit = "atirc-agent.service"
AgentRunPath = "/usr/local/bin/atirc-agent-run.sh"
AgentEnvPathUnit = "atirc-agent-env.path"
AgentEnvUnit = "atirc-agent-env.service"
AgentEnvRestartPath = "/usr/local/bin/atirc-agent-env.sh"
AgentEnvStampDir = "/run/atirc-agent"
AgentEnvStampPath = AgentEnvStampDir + "/env.sha256"
AgentEnvSnapshotPath = AgentEnvStampDir + "/env"
AtircBinary = "/usr/local/bin/atirc"
AgentUserPath = "/usr/local/bin/atirc-agent-user.sh"
AgentExecPath = "/usr/local/bin/atirc-agent-exec.sh"
GuestAgentAgentPath = GuestAgentConfigDir + "/agent.json"
)
type AgentOptions struct {
EnvFile string
}
type guestAgentAgent struct {
Unit string `json:"unit"`
}
func (opts AgentOptions) envFile() string {
if opts.EnvFile == "" {
return DefaultAgentEnvFile
}
return opts.EnvFile
}
func (opts AgentOptions) Validate() error {
if !tunnelPathRE.MatchString(opts.envFile()) {
return fmt.Errorf("cloudinit agent: envFile %q is not an absolute path of [A-Za-z0-9._/-]", opts.EnvFile)
}
return nil
}
func AgentModule(opts AgentOptions) UserDataModule {
return func(Context) (UserDataPatch, error) {
return agentPatch(opts)
}
}
func agentPatch(opts AgentOptions) (UserDataPatch, error) {
if err := opts.Validate(); err != nil {
return UserDataPatch{}, err
}
raw, err := json.Marshal(guestAgentAgent{Unit: AgentUnit})
if err != nil {
return UserDataPatch{}, fmt.Errorf("cloudinit agent: encoding %s: %w", GuestAgentAgentPath, err)
}
return UserDataPatch{
WriteFiles: []WriteFileEntry{
{Path: "/etc/systemd/system/" + AgentUnit, Owner: "root:root", Permissions: "0644", Content: unitFile(
[]string{
"Description=atirc agent",
"After=network-online.target " + GuestBinariesUnit + " " + strings.Join(SecretsReadyUnits, " "),
"Wants=network-online.target " + GuestBinariesUnit + " " + GuestSecretsUnit,
"StartLimitIntervalSec=0",
},
[]string{
"Type=simple",
"ExecStart=" + AgentRunPath + " " + opts.envFile(),
"Restart=always",
"RestartSec=30",
"StandardOutput=journal",
"StandardError=journal",
})},
{Path: AgentRunPath, Owner: "root:root", Permissions: "0700", Content: agentRunScript},
{Path: AgentUserPath, Owner: "root:root", Permissions: "0700", Content: agentUserScript},
{Path: AgentExecPath, Owner: "root:root", Permissions: "0700", Content: agentExecScript},
{Path: AgentEnvRestartPath, Owner: "root:root", Permissions: "0700", Content: agentEnvRestartScript},
{Path: "/etc/systemd/system/" + AgentEnvUnit, Owner: "root:root", Permissions: "0644", Content: strings.Join([]string{
"[Unit]",
"Description=restart a running atirc agent when its env file changed",
"",
"[Service]",
"Type=oneshot",
"ExecStart=" + AgentEnvRestartPath + " " + opts.envFile(),
"StandardOutput=journal",
"StandardError=journal",
"",
}, "\n")},
{Path: "/etc/systemd/system/" + AgentEnvPathUnit, Owner: "root:root", Permissions: "0644", Content: strings.Join([]string{
"[Unit]",
"Description=watch the atirc agent env file",
"",
"[Path]",
"PathChanged=" + opts.envFile(),
"Unit=" + AgentEnvUnit,
"",
"[Install]",
"WantedBy=paths.target",
"",
}, "\n")},
{Path: GuestAgentAgentPath, Owner: "root:root", Permissions: "0644", Content: string(raw) + "\n"},
},
RuncmdPrepend: []any{"systemctl daemon-reload", "systemctl enable --now " + AgentEnvPathUnit},
}, nil
}
const agentEnvRestartScript = `#!/usr/bin/env bash
set -euo pipefail
ENV_FILE="$1"
now="$(sha256sum < "${ENV_FILE}" 2>/dev/null || sha256sum < /dev/null)"
if [ "${now}" = "$(cat "` + AgentEnvStampPath + `" 2>/dev/null || true)" ]; then
exit 0
fi
echo "atirc-agent-env: ${ENV_FILE} changed; restarting ` + AgentUnit + ` if it runs" >&2
exec systemctl try-restart --no-block ` + AgentUnit + `
`
const agentRunScript = `#!/usr/bin/env bash
set -euo pipefail
ENV_FILE="$1"
install -d -m 0700 -o root -g root "` + AgentEnvStampDir + `"
if [ -f "${ENV_FILE}" ]; then cp -f "${ENV_FILE}" "` + AgentEnvSnapshotPath + `"; else : > "` + AgentEnvSnapshotPath + `"; fi
chmod 0600 "` + AgentEnvSnapshotPath + `"
sha256sum < "` + AgentEnvSnapshotPath + `" > "` + AgentEnvStampPath + `"
while IFS= read -r line || [ -n "${line}" ]; do
if [[ "${line}" =~ ^[A-Za-z_][A-Za-z0-9_]*= ]]; then
if ! (export "${line}") 2>/dev/null; then
echo "atirc-agent: skipped ${line%%=*} from the env file (not exportable)" >&2
continue
fi
export "${line}"
fi
done < "` + AgentEnvSnapshotPath + `"
if [ ! -f "` + GuestSecretsReadyPath + `" ] && [ ! -f "` + SecretsDoneStamp + `" ]; then
echo "atirc-agent: the secrets bundle is not written yet (` + GuestSecretsReadyPath + `); the agent waits" >&2
exit 1
fi
if [ ! -x "` + AtircBinary + `" ]; then
echo "atirc-agent: ` + AtircBinary + ` is not installed yet (` + GuestBinariesUnit + `); the agent waits" >&2
exit 1
fi
if [ -z "${ATIRC_TOKEN:-}" ]; then
echo "atirc-agent: ATIRC_TOKEN from the secrets bundle is empty (mint failed or pending); the agent waits" >&2
exit 1
fi
export ATIRC_TOKEN
nick="$(` + AgentUserPath + `)"
uid="$(id -u "${nick}")"
exec runuser -u "${nick}" -- env XDG_RUNTIME_DIR="/run/user/${uid}" DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/${uid}/bus" ` + AtircBinary + ` agent run
`
const agentUserScript = `#!/usr/bin/env bash
set -euo pipefail
nick="$(` + AtircBinary + ` nick)"
if ! [[ "${nick}" =~ ^[a-z_][a-z0-9_-]{0,31}$ ]]; then
echo "atirc-agent: atirc nick printed ${nick@Q}, not a user name" >&2
exit 1
fi
getent group wheel >/dev/null || groupadd wheel
mkdir -p /etc/sudoers.d
printf '%%wheel ALL=(ALL) NOPASSWD: ALL\n' > /etc/sudoers.d/wheel
chmod 440 /etc/sudoers.d/wheel
id "${nick}" >/dev/null 2>&1 || useradd -m -G wheel -s /bin/bash "${nick}"
if getent group ` + GuestRequesterGroup + ` >/dev/null; then usermod -aG ` + GuestRequesterGroup + ` "${nick}"; fi
loginctl enable-linger "${nick}" >&2
uid="$(id -u "${nick}")"
for _ in $(seq 1 30); do
[ -S "/run/user/${uid}/bus" ] && break
sleep 1
done
printf '%s\n' "${nick}"
`
const agentExecScript = `#!/usr/bin/env bash
set -euo pipefail
if [ -z "${ATIRC_TOKEN:-}" ]; then
echo "atirc-agent-exec: ATIRC_TOKEN is empty" >&2
exit 1
fi
nick="$(` + AgentUserPath + `)"
uid="$(id -u "${nick}")"
home="$(getent passwd "${nick}" | cut -d: -f6)"
cd "${home}"
exec runuser -u "${nick}" -- env XDG_RUNTIME_DIR="/run/user/${uid}" DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/${uid}/bus" bash -c "$1"
`
func AssociationRefPath(raw string) (string, error) {
raw = strings.TrimSpace(raw)
if raw == "" {
return DefaultAssociationRefFile, nil
}
clean := path.Clean(raw)
if !strings.HasPrefix(raw, "/") || slices.Contains(strings.Split(raw, "/"), "..") || !strings.HasPrefix(clean, AssociationRefDir+"/") {
return "", fmt.Errorf("cloudinit association: refFile %q is not an absolute path under %s/ without a .. segment", raw, AssociationRefDir)
}
return clean, nil
}
type GuestAgentAssociation struct {
RefFile string `json:"refFile"`
}
func AssociationModule(refFile string) UserDataModule {
return func(Context) (UserDataPatch, error) {
clean, err := AssociationRefPath(refFile)
if err != nil {
return UserDataPatch{}, err
}
raw, err := json.Marshal(GuestAgentAssociation{RefFile: clean})
if err != nil {
return UserDataPatch{}, fmt.Errorf("cloudinit association: encoding %s: %w", GuestAgentAssociationPath, err)
}
after := []string{"After=network-online.target " + GuestBinariesUnit + " " + strings.Join(SecretsReadyUnits, " "),
"Wants=network-online.target " + GuestBinariesUnit + " " + GuestSecretsUnit}
return UserDataPatch{WriteFiles: []WriteFileEntry{
{Path: GuestAgentAssociationPath, Owner: "root:root", Permissions: "0644", Content: string(raw) + "\n"},
{Path: GuestRequesterRunPath, Owner: "root:root", Permissions: "0755", Content: guestRequesterRunScript},
{Path: GuestRequesterReleasePath, Owner: "root:root", Permissions: "0755", Content: guestRequesterReleaseScript},
{Path: "/etc/systemd/system/" + GuestRequesterUnit, Owner: "root:root", Permissions: "0644", Content: unitFile(
append([]string{"Description=the guest requester (plan 0036 A7), started by guest.r2Start"}, after...),
[]string{"Type=exec", "User=" + GuestRequesterUser, "ExecStart=" + GuestRequesterRunPath,
"TimeoutStopSec=" + seconds(GuestRequesterStopTimeout-30*time.Second), "StandardOutput=journal", "StandardError=journal"})},
{Path: "/etc/systemd/system/" + GuestRequesterReleaseUnit, Owner: "root:root", Permissions: "0644", Content: unitFile(
append([]string{"Description=release every contract of the guest requester (plan 0036 A8), started by guest.r2Release"}, after...),
[]string{"Type=oneshot", "User=" + GuestRequesterUser, "ExecStart=" + GuestRequesterReleasePath,
"TimeoutStartSec=" + seconds(GuestRequesterReleaseUnitTimeout), "StandardOutput=journal", "StandardError=journal"})},
}, RuncmdPrepend: []any{
"groupadd -f " + GuestRequesterGroup,
"id " + GuestRequesterUser + " >/dev/null 2>&1 || useradd -m -s /bin/bash -G " + GuestRequesterGroup + " " + GuestRequesterUser,
"loginctl enable-linger " + GuestRequesterUser,
"systemctl daemon-reload",
}}, nil
}
}
const guestRequesterEnv = `uid="$(id -u)"
export XDG_RUNTIME_DIR="/run/user/${uid}" DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/${uid}/bus"
for _ in $(seq 1 30); do
[ -S "${XDG_RUNTIME_DIR}/bus" ] && break
sleep 1
done
`
const guestRequesterRunScript = `#!/usr/bin/env bash
set -euo pipefail
. ` + GuestRequesterStartEnv + `
` + guestRequesterEnv + `args=(run request --name "${R2_NAME}" --guest-requester)
[ "${R2_AGENT:-}" = 1 ] && args+=(--agent)
[ "${R2_HOLD:-}" = 1 ] && args+=(--hold)
socialweb-computer "${args[@]}"
if [ "${R2_AGENT:-}" = 1 ] && [ "${R2_HOLD:-}" = 1 ]; then
exec socialweb-computer run request agent "${R2_NAME}"
fi
`
var guestRequesterReleaseScript = `#!/usr/bin/env bash
set -uo pipefail
command -v socialweb-computer >/dev/null || exit 0
` + guestRequesterEnv + `fail=0
pids=()
for name in $(socialweb-computer run request list --json | jq -r '.[] | select(.phase != "Released") | .name'); do
timeout --kill-after=30 ` + seconds(GuestRequesterReleaseEach) + ` socialweb-computer run request release --contract-timeout ` + seconds(RequestReleaseWait) + `s "${name}" &
pids+=("$!")
done
for pid in "${pids[@]}"; do
wait "${pid}" || fail=1
done
exit "${fail}"
`
func seconds(d time.Duration) string {
return strconv.Itoa(int(d / time.Second))
}