Skip to content

Commit 994c959

Browse files
committed
feat(ingress): attest with dstack 0.6 /v1/Attest
GetQuote is TDX-only: it fails on other platforms, and on GCP it leaves out the vTPM quote that GCP's verification also binds. dstack 0.6 serves Attest under /v1, whose attestation carries the quote and event log on every platform and verifies with dstack-verifier. Evidence is published as attestation.json and the DNS webhook carries the Attest response when the agent offers /v1; an older agent answers 404 and gets GetQuote as before (quote.json). USE_GET_QUOTE=true keeps GetQuote for verifiers that need it. When evidence is republished, the file of the other kind is removed, so readers take quote.json and fall back to attestation.json. The webhook also stops forwarding an agent error body as the quote, and pads report_data to the 64 bytes Tappd.RawQuote requires. Signed-off-by: Kevin Wang <wy721@qq.com>
1 parent 4be5059 commit 994c959

5 files changed

Lines changed: 109 additions & 48 deletions

File tree

‎custom-domain/dstack-ingress/README.md‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ dstack-ingress is a HAProxy-based L4 (TCP) proxy that provides:
2222

2323
3. **Certificate Renewal**: A background daemon checks for renewal every 12 hours. On renewal, HAProxy is gracefully reloaded with zero downtime.
2424

25-
4. **Evidence Generation**: Generates cryptographically linked attestation evidence (ACME account, certificates, TDX quote) for TEE verification.
25+
4. **Evidence Generation**: Generates cryptographically linked attestation evidence (ACME account, certificates, dstack attestation) for TEE verification.
2626

2727
### Wildcard Domain Support
2828

@@ -196,6 +196,7 @@ otherwise arbitrary — the gateway answers every name under its domain.
196196
| `TIMEOUT_SERVER` | `86400s` | Server-side timeout |
197197
| `EVIDENCE_SERVER` | `true` | Serve evidence files at `/evidences/` on the TLS port |
198198
| `EVIDENCE_PORT` | `80` | Internal port for evidence HTTP server |
199+
| `USE_GET_QUOTE` | `false` | Attest evidence and webhook requests with a `GetQuote` TDX quote even when the dstack agent offers `/v1/Attest` |
199200
| `ALPN` | | TLS ALPN protocols (e.g. `h2,http/1.1`). Only set if backends support h2c |
200201
| `ACCEPT_PROXY_PROTOCOL` | `false` | Read the client's address from a PROXY protocol header (v1 or v2) on every connection to `PORT`, as the dstack gateway sends when the app's `port_policy` sets `pp` for it. A connection without one is refused. See [Client address](#client-address-proxy-protocol) |
201202
| `SEND_PROXY_PROTOCOL` | `false` | Pass the client's address to the backends (`TARGET_ENDPOINT`, `ROUTING_MAP`) with PROXY protocol v2. Only for backends that read it |
@@ -312,11 +313,14 @@ To disable the built-in evidence endpoint and serve evidence files only through
312313
| `acme-account.json` | ACME account used to request certificates |
313314
| `cert-{domain}.pem` | Let's Encrypt certificate for each domain |
314315
| `sha256sum.txt` | SHA-256 checksums of all evidence files |
315-
| `quote.json` | TDX quote with `sha256sum.txt` digest in report_data |
316+
| `attestation.json` | dstack attestation (`/v1/Attest`, dstack 0.6+) with `sha256sum.txt` digest in report_data |
317+
| `quote.json` | TDX quote with the same report_data, instead of `attestation.json` on older dstack or with `USE_GET_QUOTE=true` |
318+
319+
Exactly one of `quote.json` and `attestation.json` is published: read `quote.json`, and fall back to `attestation.json` if it is absent.
316320

317321
### Verification Chain
318322

319-
1. Verify the TDX quote in `quote.json`
323+
1. Verify the TDX quote in `quote.json`, or `attestation.json` by POSTing it to [dstack-verifier](https://github.com/Dstack-TEE/dstack/tree/master/dstack/verifier)'s `/verify`
320324
2. Extract `report_data` — it contains the SHA-256 of `sha256sum.txt`
321325
3. Verify checksums in `sha256sum.txt` against `acme-account.json` and `cert-*.pem`
322326
4. This proves the certificates were obtained within the TEE
@@ -496,14 +500,18 @@ Encrypt dropping expiry mail, and the container passes it for you.
496500
{
497501
"payload": "{\"app_id\":\"…\",\"challenge\":\"tls-alpn-01\",\"domain\":\"app.example.com\",\"instance_id\":\"…\",\"records\":[…],\"timestamp\":1234567890,\"version\":1}",
498502
"hmac_sha256": "…",
499-
"attestation": { "quote": "…", "report_data": "…" }
503+
"attestation": { "attestation": "…", "report_data": "…" }
500504
}
501505
```
502506

507+
`attestation` is the `/v1/Attest` response (dstack 0.6+). From an older agent,
508+
or with `USE_GET_QUOTE=true`, it is the `GetQuote` response instead, with
509+
`quote` in place of the inner `attestation`.
510+
503511
`payload` is a *string* so you sign and hash exactly the bytes you received.
504512
Verify `hmac_sha256` with `DNS_WEBHOOK_TOKEN`, and — since this request asks you
505-
to point a hostname at the instance it names — verify `attestation` too: the
506-
quote's `report_data` is `sha256(payload)`, so it proves which enclave produced
513+
to point a hostname at the instance it names — verify `attestation` too (dstack-verifier `/verify` takes either form): its
514+
`report_data` is `sha256(payload)`, so it proves which enclave produced
507515
those records. Check the app ID and measurements in it before changing DNS.
508516

509517
### Why lego and not certbot

‎custom-domain/dstack-ingress/TESTING.md‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ behaviour — test the observable effect (reload / no reload), not the prose.
166166
`/evidences/` is served on the TLS port. The chain to verify:
167167

168168
```
169-
quote.report_data == sha256(sha256sum.txt)
169+
attestation.report_data == sha256(sha256sum.txt)
170170
sha256sum.txt covers acme-account.json and every cert-<domain>.pem
171171
cert-<domain>.pem == the certificate actually served
172172
```
@@ -175,16 +175,22 @@ so:
175175

176176
```bash
177177
curl -sk https://<domain>/evidences/sha256sum.txt -o s.txt && sha256sum -c s.txt
178-
python3 -c "import json,hashlib;print(json.load(open('quote.json'))['report_data'][:64] \
179-
== hashlib.sha256(open('s.txt','rb').read()).hexdigest())"
178+
# quote.json, or attestation.json where there is none (dstack 0.6+);
179+
# dstack-verifier decodes either and reports report_data
180+
curl -skf https://<domain>/evidences/quote.json -o a.json ||
181+
curl -skf https://<domain>/evidences/attestation.json -o a.json
182+
curl -s -H 'Content-Type: application/json' --data-binary @a.json http://<verifier>/verify |
183+
jq -r .details.report_data | cut -c1-64
184+
sha256sum s.txt # must match
180185
```
181186

182187
Compare the evidence copy against a live `openssl s_client` fingerprint — the
183188
point of the chain is that the quote commits to what is *being served*, and only
184189
that comparison tests it.
185190

186-
To verify the quote itself rather than trusting it, run it through `dcap-qvl`
187-
against Intel collateral and expect `status = UpToDate`. Re-run the whole chain
191+
dstack-verifier also checks the attestation itself (`is_valid`). For a
192+
`quote.json`, run the quote through `dcap-qvl` against Intel collateral and
193+
expect `status = UpToDate`. Re-run the whole chain
188194
after a renewal: the evidence must track the new certificates, not go stale at
189195
first issuance.
190196

‎custom-domain/dstack-ingress/scripts/dnshook.py‎

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
request carries:
1212
1313
* an HMAC-SHA256 over the exact payload string, keyed by a shared secret; and
14-
* optionally a TDX quote whose report_data commits to that same payload.
14+
* optionally an attestation whose report_data commits to that same payload:
15+
a dstack attestation (`/v1/Attest`, dstack 0.6+) or, from older agents or
16+
with USE_GET_QUOTE=true, a TDX quote.
1517
1618
Verify both on the receiving end. The HMAC alone only proves the caller knows
17-
the secret; the quote proves the caller is the enclave you expect, so check the
18-
app_id / measurements in it before touching DNS.
19+
the secret; the attestation proves the caller is the enclave you expect, so
20+
check the app_id / measurements in it before touching DNS.
1921
2022
Payload is sent as a *string* field rather than a nested object on purpose: the
2123
receiver signs and hashes exactly the bytes it was given, with no JSON
@@ -37,10 +39,8 @@
3739

3840
DEFAULT_TIMEOUT = 15
3941
DEFAULT_RETRIES = 3
40-
DSTACK_SOCKETS = (
41-
("/var/run/dstack.sock", "http://localhost/GetQuote?report_data={rd}"),
42-
("/var/run/tappd.sock", "http://localhost/prpc/Tappd.RawQuote?report_data={rd}"),
43-
)
42+
DSTACK_SOCKET = "/var/run/dstack.sock"
43+
TAPPD_SOCKET = "/var/run/tappd.sock"
4444

4545

4646
def _canonical(payload: Dict[str, Any]) -> str:
@@ -51,30 +51,46 @@ def _sign(payload_str: str, secret: str) -> str:
5151
return hmac.new(secret.encode(), payload_str.encode(), hashlib.sha256).hexdigest()
5252

5353

54-
def get_quote(payload_str: str) -> Optional[Dict[str, Any]]:
55-
"""Fetch a TDX quote committing to payload_str, or None if unavailable."""
54+
def get_attestation(payload_str: str) -> Optional[Dict[str, Any]]:
55+
"""Fetch an attestation committing to payload_str, or None if unavailable.
56+
57+
dstack 0.6 serves Attest under /v1 for every platform; an older agent
58+
answers 404 there and is asked for a TDX quote instead, as is every agent
59+
with USE_GET_QUOTE=true.
60+
"""
5661
report_data = hashlib.sha256(payload_str.encode()).hexdigest()
57-
for sock, url_tpl in DSTACK_SOCKETS:
62+
# Tappd.RawQuote takes exactly 64 bytes; the others zero-pad to that.
63+
padded = report_data.ljust(128, "0")
64+
calls = [(DSTACK_SOCKET, f"/GetQuote?report_data={padded}"),
65+
(TAPPD_SOCKET, f"/prpc/Tappd.RawQuote?report_data={padded}")]
66+
if os.environ.get("USE_GET_QUOTE", "false") != "true":
67+
calls.insert(0, (DSTACK_SOCKET, f"/v1/Attest?report_data={padded}"))
68+
for sock, path in calls:
5869
if not os.path.exists(sock):
5970
continue
6071
try:
6172
out = subprocess.run(
62-
["curl", "-s", "--unix-socket", sock, url_tpl.format(rd=report_data)],
73+
["curl", "-sS", "-w", "\n%{http_code}", "--unix-socket", sock,
74+
f"http://localhost{path}"],
6375
capture_output=True,
6476
text=True,
6577
timeout=30,
6678
)
67-
if out.returncode != 0 or not out.stdout.strip():
79+
body, _, status = out.stdout.rpartition("\n")
80+
if out.returncode == 0 and status == "404" and path.startswith("/v1/"):
81+
continue
82+
if out.returncode != 0 or status != "200":
6883
print(
69-
f"Warning: quote fetch via {sock} failed (rc={out.returncode})",
84+
f"Warning: attestation via {sock}{path.split('?')[0]} failed "
85+
f"(rc={out.returncode}, HTTP {status or '-'})",
7086
file=sys.stderr,
7187
)
7288
continue
73-
quote = json.loads(out.stdout)
74-
quote["report_data"] = report_data
75-
return quote
89+
attestation = json.loads(body)
90+
attestation["report_data"] = report_data
91+
return attestation
7692
except (subprocess.SubprocessError, ValueError) as exc:
77-
print(f"Warning: quote fetch via {sock} failed: {exc}", file=sys.stderr)
93+
print(f"Warning: attestation via {sock} failed: {exc}", file=sys.stderr)
7894
return None
7995

8096

@@ -108,9 +124,9 @@ def build_envelope(
108124
)
109125

110126
if with_quote:
111-
quote = get_quote(payload_str)
112-
if quote is not None:
113-
envelope["attestation"] = quote
127+
attestation = get_attestation(payload_str)
128+
if attestation is not None:
129+
envelope["attestation"] = attestation
114130
return envelope
115131

116132

‎custom-domain/dstack-ingress/scripts/evidence-lib.sh‎

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ evidence_collect_cert() {
4040
chmod 644 "$EVIDENCE_STAGE/cert-${domain}.pem"
4141
}
4242

43-
# Hash whatever was collected, bind it into a TDX quote, and publish it.
43+
# Hash whatever was collected, bind it into an attestation, and publish it.
4444
evidence_finalize() {
45-
local stage="$EVIDENCE_STAGE" quoted_hash
45+
local stage="$EVIDENCE_STAGE" quoted_hash proof
4646
if ! (cd "$stage" && sha256sum acme-account.json cert-*.pem > sha256sum.txt) 2>/dev/null; then
4747
echo "Error: No certificate files found"
4848
rm -rf "$stage"
@@ -54,41 +54,64 @@ evidence_finalize() {
5454
quoted_hash="${quoted_hash}0"
5555
done
5656

57-
if ! evidence_get_quote "$quoted_hash" > "$stage/quote.json"; then
57+
if ! proof=$(evidence_attest "$quoted_hash" "$stage"); then
5858
echo "Error: Failed to generate evidences"
5959
rm -rf "$stage"
6060
return 1
6161
fi
62-
chmod 644 "$stage/sha256sum.txt" "$stage/quote.json"
63-
if ! evidence_publish "$stage"; then
62+
chmod 644 "$stage/sha256sum.txt" "$stage/$proof"
63+
if ! evidence_publish "$stage" "$proof"; then
6464
echo "Error: Failed to publish evidences"
6565
rm -rf "$stage"
6666
return 1
6767
fi
6868
echo "Generated evidences successfully"
6969
}
7070

71-
# -f: the guest agent reports errors as an HTTP status with a body, and that
72-
# body must not be published as the quote.
73-
evidence_get_quote() {
71+
# Write the attestation of report_data ($1) into the stage directory ($2) and
72+
# print the file name. dstack 0.6 serves Attest under /v1: one format for every
73+
# platform, carrying the quote and the event log, where GetQuote is TDX-only
74+
# and on GCP leaves out the vTPM quote. An agent without /v1 answers 404 and
75+
# gets GetQuote as before; USE_GET_QUOTE=true keeps GetQuote for verifiers
76+
# that read quote.json.
77+
evidence_attest() {
78+
local report_data="$1" stage="$2" status
79+
if [ "${USE_GET_QUOTE:-false}" != "true" ] && [[ -S /var/run/dstack.sock ]]; then
80+
status=$(curl -sS -o "$stage/attestation.json" -w '%{http_code}' \
81+
--unix-socket /var/run/dstack.sock \
82+
"http://localhost/v1/Attest?report_data=$report_data") || return 1
83+
if [ "$status" = 200 ]; then
84+
echo attestation.json
85+
return 0
86+
fi
87+
if [ "$status" != 404 ]; then
88+
echo "Error: Attest answered HTTP $status: $(head -c 300 "$stage/attestation.json")" >&2
89+
return 1
90+
fi
91+
rm -f "$stage/attestation.json"
92+
fi
93+
# -f: the guest agent reports errors as an HTTP status with a body, and
94+
# that body must not be published as the quote.
7495
if [[ -S /var/run/dstack.sock ]]; then
75-
curl -sSf --unix-socket /var/run/dstack.sock "http://localhost/GetQuote?report_data=$1"
96+
curl -sSf --unix-socket /var/run/dstack.sock "http://localhost/GetQuote?report_data=$report_data"
7697
else
77-
curl -sSf --unix-socket /var/run/tappd.sock "http://localhost/prpc/Tappd.RawQuote?report_data=$1"
78-
fi
98+
curl -sSf --unix-socket /var/run/tappd.sock "http://localhost/prpc/Tappd.RawQuote?report_data=$report_data"
99+
fi > "$stage/quote.json" || return 1
100+
echo quote.json
79101
}
80102

81-
# Each rename is atomic. The manifest and quote go last, so they never list a
82-
# file that is not yet in place, and certificates of removed domains go after.
103+
# Each rename is atomic. The manifest and attestation go last, so they never
104+
# list a file that is not yet in place; certificates of removed domains, and
105+
# the attestation file of the other kind, go after.
83106
evidence_publish() {
84-
local stage="$1" f stale=()
85-
for f in "$EVIDENCE_DIR"/cert-*.pem; do
107+
local stage="$1" proof="$2" f stale=()
108+
for f in "$EVIDENCE_DIR"/cert-*.pem "$EVIDENCE_DIR"/attestation.json "$EVIDENCE_DIR"/quote.json; do
86109
if [ -e "$f" ] && [ ! -e "$stage/${f##*/}" ]; then
87110
stale+=("$f")
88111
fi
89112
done
90113
mv -f "$stage"/acme-account.json "$stage"/cert-*.pem "$EVIDENCE_DIR"/ || return 1
91-
mv -f "$stage"/sha256sum.txt "$stage"/quote.json "$EVIDENCE_DIR"/ || return 1
114+
mv -f "$stage"/sha256sum.txt "$stage/$proof" "$EVIDENCE_DIR"/ || return 1
92115
rm -f "${stale[@]}"
93116
rm -rf "$stage"
94117
}

‎custom-domain/dstack-ingress/scripts/tests/e2e-test.sh‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ else
373373
fi
374374

375375
log "Test: Evidence files"
376-
for file in acme-account.json sha256sum.txt quote.json; do
376+
for file in acme-account.json sha256sum.txt; do
377377
FILE_STATUS=$(do_curl -s -o /dev/null -w '%{http_code}' --http1.1 "https://${DOMAIN}/evidences/${file}")
378378
if [ "$FILE_STATUS" = "200" ]; then
379379
pass "Evidence file /${file} exists"
@@ -382,6 +382,14 @@ for file in acme-account.json sha256sum.txt quote.json; do
382382
fi
383383
done
384384

385+
# dstack 0.6+ publishes attestation.json; older agents, or USE_GET_QUOTE=true, quote.json.
386+
if [ "$(do_curl -s -o /dev/null -w '%{http_code}' --http1.1 "https://${DOMAIN}/evidences/quote.json")" = "200" ] ||
387+
[ "$(do_curl -s -o /dev/null -w '%{http_code}' --http1.1 "https://${DOMAIN}/evidences/attestation.json")" = "200" ]; then
388+
pass "Evidence file /quote.json or /attestation.json exists"
389+
else
390+
fail "Neither /quote.json nor /attestation.json is served"
391+
fi
392+
385393
log "Test: Evidence integrity"
386394
SHA256_CONTENT=$(do_curl -sf --http1.1 "https://${DOMAIN}/evidences/sha256sum.txt" || echo "")
387395
if echo "$SHA256_CONTENT" | grep -q "acme-account.json"; then

0 commit comments

Comments
 (0)