@@ -22,6 +22,10 @@ GATEWAY_PROXIES="gateway-1:9014 gateway-2:9014 gateway-3:9014"
2222GATEWAY_DEBUG_URLS=" http://gateway-1:9015 http://gateway-2:9015 http://gateway-3:9015"
2323GATEWAY_ADMIN=" http://gateway-1:9016"
2424
25+ # Must match `admin_token` in configs/gateway-*.toml
26+ ADMIN_TOKEN=" e2e-admin-token"
27+ ADMIN_AUTH_HEADER=" Authorization: Bearer ${ADMIN_TOKEN} "
28+
2529# External services
2630MOCK_CF_API=" http://mock-cf-dns-api:8080"
2731PEBBLE_DIR=" http://pebble:14000/dir"
@@ -183,6 +187,7 @@ setup_certbot_config() {
183187 # Set ACME URL
184188 log_info " Setting ACME URL: ${ACME_URL} "
185189 if ! curl -sf -X POST " ${GATEWAY_ADMIN} /prpc/Admin.SetCertbotConfig" \
190+ -H " ${ADMIN_AUTH_HEADER} " \
186191 -H " Content-Type: application/json" \
187192 -d ' {"acme_url": "' " ${ACME_URL} " ' "}' > /dev/null; then
188193 log_error " Failed to set certbot config"
@@ -192,6 +197,7 @@ setup_certbot_config() {
192197 # Create DNS credential
193198 log_info " Creating DNS credential..."
194199 if ! curl -sf -X POST " ${GATEWAY_ADMIN} /prpc/Admin.CreateDnsCredential" \
200+ -H " ${ADMIN_AUTH_HEADER} " \
195201 -H " Content-Type: application/json" \
196202 -d ' {
197203 "name": "test-cloudflare",
@@ -210,11 +216,13 @@ setup_certbot_config() {
210216 for domain in $CERT_DOMAINS ; do
211217 log_info " Adding domain: $domain "
212218 curl -sf -X POST " ${GATEWAY_ADMIN} /prpc/Admin.AddZtDomain" \
219+ -H " ${ADMIN_AUTH_HEADER} " \
213220 -H " Content-Type: application/json" \
214221 -d ' {"domain": "' " ${domain} " ' "}' > /dev/null || true
215222
216223 log_info " Triggering renewal for: $domain "
217224 curl -sf -X POST " ${GATEWAY_ADMIN} /prpc/Admin.RenewZtDomainCert" \
225+ -H " ${ADMIN_AUTH_HEADER} " \
218226 -H " Content-Type: application/json" \
219227 -d ' {"domain": "' " ${domain} " ' ", "force": true}' > /dev/null || \
220228 log_warn " Renewal request failed for $domain (may retry)"
@@ -223,6 +231,31 @@ setup_certbot_config() {
223231 return 0
224232}
225233
234+ # Returns 0 if HTTP status code from $1 args equals $2.
235+ http_status_eq () {
236+ local expected=" $1 "
237+ shift
238+ local actual
239+ actual=$( curl -s -o /dev/null -w ' %{http_code}' " $@ " )
240+ [ " $actual " = " $expected " ]
241+ }
242+
243+ # Returns 0 if all three admin auth checks pass: missing 401, wrong 401, right 200.
244+ test_admin_auth () {
245+ log_info " checking admin auth on ${GATEWAY_ADMIN} "
246+ # Missing token → 401
247+ http_status_eq 401 " ${GATEWAY_ADMIN} /prpc/Admin.Status" \
248+ || { log_error " no-token request did not return 401" ; return 1; }
249+ # Wrong token → 401
250+ http_status_eq 401 " ${GATEWAY_ADMIN} /prpc/Admin.Status" \
251+ -H " Authorization: Bearer wrong-token" \
252+ || { log_error " wrong-token request did not return 401" ; return 1; }
253+ # Correct token → 200
254+ http_status_eq 200 " ${GATEWAY_ADMIN} /prpc/Admin.Status" \
255+ -H " ${ADMIN_AUTH_HEADER} " \
256+ || { log_error " valid-token request did not return 200" ; return 1; }
257+ }
258+
226259# ==================== Main ====================
227260
228261main () {
@@ -241,14 +274,19 @@ main() {
241274 i=$(( i + 1 ))
242275 done
243276
244- # Phase 3: Configure certbot
245- log_phase 3 " Configure certbot"
277+ # Phase 3: Admin auth gating
278+ log_phase 3 " Admin token auth"
279+ run_test " Admin endpoint accepts valid token and rejects missing/wrong" \
280+ " $( test_admin_auth; echo $? ) "
281+
282+ # Phase 4: Configure certbot
283+ log_phase 4 " Configure certbot"
246284 if ! setup_certbot_config; then
247285 log_error " Failed to setup certbot configuration"
248286 fi
249287
250- # Phase 4 : Certificate issuance
251- log_phase 4 " Certificate issuance"
288+ # Phase 5 : Certificate issuance
289+ log_phase 5 " Certificate issuance"
252290 local first_domain=$( echo " $CERT_DOMAINS " | cut -d' ' -f1)
253291 local first_sni=$( get_test_sni " $first_domain " )
254292 local first_proxy=$( echo " $GATEWAY_PROXIES " | cut -d' ' -f1)
@@ -274,8 +312,8 @@ main() {
274312 log_info " Waiting 20s for cluster sync..."
275313 sleep 20
276314
277- # Phase 5 : Certificate consistency
278- log_phase 5 " Certificate consistency"
315+ # Phase 6 : Certificate consistency
316+ log_phase 6 " Certificate consistency"
279317 for domain in $CERT_DOMAINS ; do
280318 local sni=$( get_test_sni " $domain " )
281319 run_test " All gateways have same cert for $domain " \
@@ -284,17 +322,17 @@ main() {
284322 " $( test_certificate_from_pebble " $sni " ; echo $? ) "
285323 done
286324
287- # Phase 6 : SNI-based selection
288- log_phase 6 " SNI-based certificate selection"
325+ # Phase 7 : SNI-based selection
326+ log_phase 7 " SNI-based certificate selection"
289327 for domain in $CERT_DOMAINS ; do
290328 local sni=$( get_test_sni " $domain " )
291329 local wildcard=$( get_wildcard_domain " $domain " )
292330 run_test " SNI $sni returns $wildcard cert" \
293331 " $( test_sni_cert_selection " $first_proxy " " $sni " " $wildcard " ; echo $? ) "
294332 done
295333
296- # Phase 7 : Proxy TLS health
297- log_phase 7 " Proxy TLS health endpoint"
334+ # Phase 8 : Proxy TLS health
335+ log_phase 8 " Proxy TLS health endpoint"
298336 for domain in $CERT_DOMAINS ; do
299337 local sni=$( get_test_sni " $domain " )
300338 local i=1
@@ -305,8 +343,8 @@ main() {
305343 done
306344 done
307345
308- # Phase 8 : DNS records (informational)
309- log_phase 8 " DNS-01 challenge records"
346+ # Phase 9 : DNS records (informational)
347+ log_phase 9 " DNS-01 challenge records"
310348 local records=$( curl -sf " ${MOCK_CF_API} /api/records" 2> /dev/null || echo " " )
311349 if echo " $records " | grep -q " TXT" ; then
312350 log_success " DNS TXT records found"
0 commit comments