Skip to content

Commit 1f7dad7

Browse files
committed
Switch to cronjob on host as no systemd running in enclave
1 parent 97c6e98 commit 1f7dad7

4 files changed

Lines changed: 70 additions & 36 deletions

File tree

scripts/aws/entrypoint.sh

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ echo "Starting vsock proxy..."
2424

2525
TIME_SYNC_URL="http://127.0.0.1:27015/getCurrentTime"
2626
TIME_SYNC_PROXY="socks5h://127.0.0.1:3305"
27-
TIME_SYNC_INTERVAL_SECONDS="300"
28-
27+
TIME_SYNC_TRIGGER_PORT="${TIME_SYNC_TRIGGER_PORT:-27100}"
2928
TIME_SYNC_OFFSET_SECONDS="${TIME_SYNC_OFFSET_SECONDS:-30}"
3029

3130
sync_enclave_time_with_offset_once() {
@@ -50,43 +49,52 @@ sync_enclave_time_with_offset_once() {
5049
sync_enclave_time_with_offset_once || true
5150

5251

53-
enable_time_sync_timer() {
54-
if ! command -v systemctl >/dev/null 2>&1 || [[ ! -d /run/systemd/system ]]; then
55-
echo "Time sync: systemd not available; skipping timer setup" >&2
56-
return 0
57-
fi
5852

59-
cat <<EOF >/etc/systemd/system/uid2-time-sync.service
60-
[Unit]
61-
Description=UID2 enclave time sync
62-
63-
[Service]
64-
Type=oneshot
65-
Environment=TIME_SYNC_URL=${TIME_SYNC_URL}
66-
Environment=TIME_SYNC_PROXY=${TIME_SYNC_PROXY}
67-
ExecStart=/bin/bash -c 'set -euo pipefail; current_time="$(curl -sSf -x "$TIME_SYNC_PROXY" "$TIME_SYNC_URL")"; date -u -s "$current_time"; echo "Time sync: updated enclave time to $current_time"'
68-
EOF
69-
70-
cat <<EOF >/etc/systemd/system/uid2-time-sync.timer
71-
[Unit]
72-
Description=UID2 enclave time sync timer
73-
74-
[Timer]
75-
OnBootSec=300s
76-
OnUnitActiveSec=${TIME_SYNC_INTERVAL_SECONDS}s
77-
Unit=uid2-time-sync.service
78-
Persistent=true
79-
AccuracySec=1s
80-
81-
[Install]
82-
WantedBy=timers.target
83-
EOF
84-
85-
systemctl daemon-reload
86-
systemctl enable --now uid2-time-sync.timer
53+
start_time_sync_server() {
54+
python3 - <<'PY' &
55+
import os
56+
import subprocess
57+
from http.server import BaseHTTPRequestHandler, HTTPServer
58+
59+
TIME_SYNC_URL = os.environ.get("TIME_SYNC_URL", "http://127.0.0.1:27015/getCurrentTime")
60+
TIME_SYNC_PROXY = os.environ.get("TIME_SYNC_PROXY", "socks5h://127.0.0.1:3305")
61+
TIME_SYNC_TRIGGER_PORT = int(os.environ.get("TIME_SYNC_TRIGGER_PORT", "27100"))
62+
63+
def sync_time() -> str:
64+
current_time = subprocess.check_output(
65+
["curl", "-sSf", "-x", TIME_SYNC_PROXY, TIME_SYNC_URL],
66+
text=True,
67+
).strip()
68+
subprocess.check_call(["date", "-u", "-s", current_time])
69+
return current_time
70+
71+
class Handler(BaseHTTPRequestHandler):
72+
def do_GET(self) -> None:
73+
if self.path not in ("/", "/sync"):
74+
self.send_response(404)
75+
self.end_headers()
76+
return
77+
try:
78+
result = sync_time()
79+
print(f"Time sync: updated enclave time to {result}")
80+
self.send_response(200)
81+
self.end_headers()
82+
self.wfile.write(f"OK {result}\n".encode())
83+
except Exception as exc: # pragma: no cover - best effort logging
84+
print(f"Time sync error: {exc}")
85+
self.send_response(500)
86+
self.end_headers()
87+
self.wfile.write(f"ERROR {exc}\n".encode())
88+
89+
def log_message(self, format, *args): # noqa: N802 - match base class
90+
return
91+
92+
server = HTTPServer(("127.0.0.1", TIME_SYNC_TRIGGER_PORT), Handler)
93+
server.serve_forever()
94+
PY
8795
}
8896

89-
enable_time_sync_timer
97+
start_time_sync_server
9098

9199
build_parameterized_config() {
92100
curl -s -f -o "${PARAMETERIZED_CONFIG}" -x socks5h://127.0.0.1:3305 http://127.0.0.1:27015/getConfig

scripts/aws/proxies.host.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ syslogng:
1919
service: direct
2020
listen: vsock://-1:2011
2121
connect: tcp://127.0.0.1:2011
22+
time-sync:
23+
service: direct
24+
listen: tcp://127.0.0.1:27100
25+
connect: vsock://42:27100
26+

scripts/aws/proxies.nitro.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ syslogng:
1919
service: direct
2020
listen: tcp://127.0.0.1:2011
2121
connect: vsock://3:2011
22+
time-sync:
23+
service: direct
24+
listen: vsock://-1:27100
25+
connect: tcp://127.0.0.1:27100
26+

scripts/aws/uid2-operator-ami/ansible/playbook.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@
167167
dest: /etc/systemd/system/uid2operator.service
168168
remote_src: yes
169169

170+
- name: Install time sync trigger script
171+
ansible.builtin.copy:
172+
dest: /usr/local/bin/uid2-time-sync
173+
mode: "0755"
174+
content: |
175+
#!/usr/bin/env bash
176+
set -euo pipefail
177+
curl -sSf http://127.0.0.1:27100/sync > /dev/null
178+
170179
- name: Install AWS Nitro Enclaves CLI
171180
ansible.builtin.dnf:
172181
name: aws-nitro-enclaves-cli
@@ -240,6 +249,13 @@
240249
ansible.builtin.systemd:
241250
name: uid2operator.service
242251
enabled: yes
252+
253+
- name: Install time sync cron job
254+
ansible.builtin.copy:
255+
dest: /etc/cron.d/uid2-time-sync
256+
mode: "0644"
257+
content: |
258+
*/5 * * * * root /usr/local/bin/uid2-time-sync
243259
244260
- name: Clean up tmp files
245261
file:

0 commit comments

Comments
 (0)