|
| 1 | +name: v2 API |
| 2 | + |
| 3 | +# Exercises the Tailscale-compatible v2 API end to end: a nix-built headscale, |
| 4 | +# the official tailscale GitHub Action joining over OAuth and pre-auth, and the |
| 5 | +# v2 API driven under every credential and scope. SQLite only. |
| 6 | + |
| 7 | +on: |
| 8 | + workflow_dispatch: |
| 9 | + pull_request: |
| 10 | + paths: |
| 11 | + - "hscontrol/api/v2/**" |
| 12 | + - "hscontrol/scope/**" |
| 13 | + - "hscontrol/db/oauth*.go" |
| 14 | + - "hscontrol/types/oauth.go" |
| 15 | + - "cmd/headscale/cli/oauth_client.go" |
| 16 | + - ".github/workflows/test-v2-api.yaml" |
| 17 | + - ".github/actions/headscale-up/**" |
| 18 | + |
| 19 | +concurrency: |
| 20 | + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} |
| 21 | + cancel-in-progress: true |
| 22 | + |
| 23 | +permissions: |
| 24 | + contents: read |
| 25 | + |
| 26 | +jobs: |
| 27 | + # Unit/contract tests for the OAuth credential model and scope enforcement. |
| 28 | + go-tests: |
| 29 | + runs-on: ubuntu-latest |
| 30 | + defaults: |
| 31 | + run: |
| 32 | + shell: nix develop --fallback --command bash -e {0} |
| 33 | + steps: |
| 34 | + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 35 | + - uses: NixOS/nix-installer-action@6b8548fe06acfb0155a50ab5d561accb215764cc # main |
| 36 | + - uses: Mic92/hestia/action@ff07bb902a9968ac0c3d0e51d90a606662a375d8 # main |
| 37 | + - name: go test |
| 38 | + env: |
| 39 | + CGO_ENABLED: "0" |
| 40 | + run: go test ./hscontrol/db/... ./hscontrol/api/v2/... ./hscontrol/scope/... |
| 41 | + |
| 42 | + # Headline E2E: the official action joins a node and its built-in ping to the |
| 43 | + # pre-joined regular node is the pass/fail gate (real connectivity, not just |
| 44 | + # registration). One row joins via the OAuth client-credentials flow (the |
| 45 | + # tskey-client- secret the client resolves against headscale), one via pre-auth. |
| 46 | + join: |
| 47 | + runs-on: ubuntu-latest |
| 48 | + strategy: |
| 49 | + fail-fast: false |
| 50 | + matrix: |
| 51 | + join: [oauth-tskey, preauth] |
| 52 | + steps: |
| 53 | + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 54 | + - id: hs |
| 55 | + uses: ./.github/actions/headscale-up |
| 56 | + |
| 57 | + - name: Prepare auth (${{ matrix.join }}) |
| 58 | + id: prep |
| 59 | + env: |
| 60 | + URL: ${{ steps.hs.outputs.url }} |
| 61 | + PREAUTH: ${{ steps.hs.outputs.preauth }} |
| 62 | + run: | |
| 63 | + set -euo pipefail |
| 64 | + if [ "${{ matrix.join }}" = "oauth-tskey" ]; then |
| 65 | + FULL=$(./result/bin/headscale oauth-clients create -s auth_keys -t tag:ci -o json | jq -r .key) |
| 66 | + # The upstream client only runs its OAuth exchange for tskey-client- |
| 67 | + # secrets, and reads the control URL from a baseURL= attribute on the |
| 68 | + # secret itself (--login-server governs registration, not the exchange). |
| 69 | + TS="tskey-${FULL#hskey-}?baseURL=${URL}&ephemeral=true&preauthorized=true" |
| 70 | + echo "::add-mask::$TS" |
| 71 | + echo "authkey=$TS" >> "$GITHUB_OUTPUT" |
| 72 | + echo "args=--login-server=${URL} --advertise-tags=tag:ci" >> "$GITHUB_OUTPUT" |
| 73 | + else |
| 74 | + echo "authkey=${PREAUTH}" >> "$GITHUB_OUTPUT" |
| 75 | + echo "args=--login-server=${URL}" >> "$GITHUB_OUTPUT" |
| 76 | + fi |
| 77 | +
|
| 78 | + - name: Join via official tailscale action + ping |
| 79 | + uses: tailscale/github-action@v4 |
| 80 | + with: |
| 81 | + authkey: ${{ steps.prep.outputs.authkey }} |
| 82 | + args: ${{ steps.prep.outputs.args }} |
| 83 | + ping: ${{ steps.hs.outputs.pre_ip }} |
| 84 | + |
| 85 | + - name: Assert node registered |
| 86 | + run: | |
| 87 | + set -euo pipefail |
| 88 | + ./result/bin/headscale nodes list -o json > nodes.json |
| 89 | + test "$(jq length nodes.json)" -ge 2 |
| 90 | + if [ "${{ matrix.join }}" = "oauth-tskey" ]; then |
| 91 | + jq -e '[.[] | select((.tags // []) | index("tag:ci"))] | length >= 1' nodes.json |
| 92 | + fi |
| 93 | +
|
| 94 | + # Auth model: an admin API key (all-access), a full OAuth token, and a |
| 95 | + # read-only OAuth token must all read devices, over both Bearer and Basic. |
| 96 | + credential: |
| 97 | + runs-on: ubuntu-latest |
| 98 | + strategy: |
| 99 | + fail-fast: false |
| 100 | + matrix: |
| 101 | + credential: [admin-apikey, oauth-full, oauth-readonly] |
| 102 | + transport: [bearer, basic] |
| 103 | + steps: |
| 104 | + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 105 | + - id: hs |
| 106 | + uses: ./.github/actions/headscale-up |
| 107 | + |
| 108 | + - name: Resolve credential + assert device read |
| 109 | + env: |
| 110 | + URL: ${{ steps.hs.outputs.url }} |
| 111 | + API_KEY: ${{ steps.hs.outputs.api_key }} |
| 112 | + run: | |
| 113 | + set -euo pipefail |
| 114 | + mint() { curl -fsS "${URL}/api/v2/oauth/token" -d "client_secret=$1" | jq -r .access_token; } |
| 115 | +
|
| 116 | + case "${{ matrix.credential }}" in |
| 117 | + admin-apikey) CRED="$API_KEY" ;; |
| 118 | + oauth-full) |
| 119 | + S=$(./result/bin/headscale oauth-clients create -s devices:core -t tag:ci -o json | jq -r .key) |
| 120 | + CRED=$(mint "$S") ;; |
| 121 | + oauth-readonly) |
| 122 | + S=$(./result/bin/headscale oauth-clients create -s devices:core:read -t tag:ci -o json | jq -r .key) |
| 123 | + CRED=$(mint "$S") ;; |
| 124 | + esac |
| 125 | + echo "::add-mask::$CRED" |
| 126 | +
|
| 127 | + if [ "${{ matrix.transport }}" = "bearer" ]; then |
| 128 | + AUTH=(-H "Authorization: Bearer ${CRED}") |
| 129 | + else |
| 130 | + AUTH=(-u "${CRED}:") |
| 131 | + fi |
| 132 | +
|
| 133 | + CODE=$(curl -s -o body.json -w '%{http_code}' "${AUTH[@]}" "${URL}/api/v2/tailnet/-/devices") |
| 134 | + echo "status=$CODE"; cat body.json |
| 135 | + test "$CODE" = "200" |
| 136 | + # The pre-joined node means the tailnet is not empty. |
| 137 | + jq -e '.devices | length >= 1' body.json |
| 138 | + # Scope enforcement: a token minted with exactly one scope may run that |
| 139 | + # scope's operation but is 403'd on an operation requiring another scope. |
| 140 | + scope: |
| 141 | + runs-on: ubuntu-latest |
| 142 | + strategy: |
| 143 | + fail-fast: false |
| 144 | + matrix: |
| 145 | + include: |
| 146 | + - { scope: "devices:core:read", allow: "GET /api/v2/tailnet/-/devices", deny: "DELETE /api/v2/device/1" } |
| 147 | + - { scope: "devices:core", allow: "GET /api/v2/tailnet/-/devices", deny: "POST /api/v2/tailnet/-/acl" } |
| 148 | + - { scope: "auth_keys:read", allow: "GET /api/v2/tailnet/-/keys", deny: "POST /api/v2/tailnet/-/acl" } |
| 149 | + - { scope: "auth_keys", allow: "GET /api/v2/tailnet/-/keys", deny: "POST /api/v2/tailnet/-/acl" } |
| 150 | + - { scope: "policy_file:read", allow: "GET /api/v2/tailnet/-/acl", deny: "DELETE /api/v2/device/1" } |
| 151 | + - { scope: "policy_file", allow: "GET /api/v2/tailnet/-/acl", deny: "DELETE /api/v2/device/1" } |
| 152 | + - { scope: "users:read", allow: "GET /api/v2/tailnet/-/users", deny: "DELETE /api/v2/device/1" } |
| 153 | + - { scope: "all:read", allow: "GET /api/v2/tailnet/-/devices", deny: "DELETE /api/v2/device/1" } |
| 154 | + - { scope: "all", allow: "GET /api/v2/tailnet/-/devices", deny: "" } |
| 155 | + steps: |
| 156 | + - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 |
| 157 | + - id: hs |
| 158 | + uses: ./.github/actions/headscale-up |
| 159 | + |
| 160 | + - name: Assert scope ${{ matrix.scope }} |
| 161 | + env: |
| 162 | + URL: ${{ steps.hs.outputs.url }} |
| 163 | + run: | |
| 164 | + set -euo pipefail |
| 165 | + S=$(./result/bin/headscale oauth-clients create -s '${{ matrix.scope }}' -t tag:ci -o json | jq -r .key) |
| 166 | + TOKEN=$(curl -fsS "${URL}/api/v2/oauth/token" -d "client_secret=$S" | jq -r .access_token) |
| 167 | + echo "::add-mask::$TOKEN" |
| 168 | +
|
| 169 | + code() { |
| 170 | + curl -s -o /dev/null -w '%{http_code}' -X "$1" \ |
| 171 | + -H "Authorization: Bearer ${TOKEN}" "${URL}${2}" |
| 172 | + } |
| 173 | +
|
| 174 | + read -r AM AP <<<"${{ matrix.allow }}" |
| 175 | + ALLOW=$(code "$AM" "$AP") |
| 176 | + echo "allow ${{ matrix.allow }} -> $ALLOW" |
| 177 | + case "$ALLOW" in 2*) ;; *) echo "expected 2xx"; exit 1 ;; esac |
| 178 | +
|
| 179 | + if [ -n "${{ matrix.deny }}" ]; then |
| 180 | + read -r DM DP <<<"${{ matrix.deny }}" |
| 181 | + DENY=$(code "$DM" "$DP") |
| 182 | + echo "deny ${{ matrix.deny }} -> $DENY" |
| 183 | + test "$DENY" = "403" |
| 184 | + fi |
0 commit comments