Skip to content
quarry Artifact [d2d0d718a4]
Login

Artifact [d2d0d718a4]

Artifact d2d0d718a4ed2c3b16990c751acfe143a1a2b84a8f97475450bd3d1a7c0e410a:


package cibridge

import (
	"encoding/json"
	"io"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

// A live worker token scoped to another repository's group is the
// engine's 403; the bridge names that repository, to its holder. A token
// the engine does not know is its 401, which passes through unchanged
// and names nothing.
func TestAWorkerTokenOfAnotherRepositoryIsNamedAndAnUnknownOneIsNot(t *testing.T) {
	engine := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.Header.Get("Authorization") {
		case "Bearer BETA":
			http.Error(w, `this token is scoped to worker groups ["g-beta"]; the requested group "g-alpha" is not one of them -- worker-group scopes match exactly`, http.StatusForbidden)
		case "Bearer FLEET":
			http.Error(w, `this token is scoped to task types ["x"]`, http.StatusForbidden)
		case "Bearer GONE":
			http.Error(w, `this token is scoped to worker groups ["g-deleted"]; no`, http.StatusForbidden)
		default:
			http.Error(w, "unauthorized", http.StatusUnauthorized)
		}
	}))
	defer engine.Close()
	repoOf := func(g string) (string, bool) { return strings.TrimPrefix(g, "g-"), g == "g-beta" || g == "g-alpha" }
	build := func(opts ...Option) *httptest.Server {
		h, err := Handler(engine.URL, "ADMIN", func(repo string) string { return "g-" + repo },
			func(repo string) bool { return repo == "alpha" }, nil, nil, opts...)
		if err != nil {
			t.Fatal(err)
		}
		return httptest.NewServer(h)
	}
	front, plain := build(WithRepoOfGroup(repoOf)), build()
	defer front.Close()
	defer plain.Close()
	post := func(srv *httptest.Server, path, tok string) (*http.Response, string) {
		req, _ := http.NewRequest("POST", srv.URL+"/r/alpha/ci/bridge/v1/"+path, strings.NewReader(`{"task_types":["t"]}`))
		req.Header.Set("Authorization", "Bearer "+tok)
		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			t.Fatal(err)
		}
		defer resp.Body.Close()
		b, _ := io.ReadAll(resp.Body)
		return resp, string(b)
	}
	for _, path := range []string{"tasks/poll", "workers/connect"} {
		resp, body := post(front, path, "BETA")
		var got map[string]string
		json.Unmarshal([]byte(body), &got)
		if resp.StatusCode != http.StatusForbidden || resp.Header.Get(ReasonHeader) != "other-repo" ||
			got["repository"] != "beta" || got["reason"] != "other-repo" {
			t.Errorf("FAIL: %s: a beta token got %d %q %q", path, resp.StatusCode, resp.Header.Get(ReasonHeader), body)
		}
		// Unknown token: byte-identical with and without the option.
		a, ab := post(front, path, "NOPE")
		b, bb := post(plain, path, "NOPE")
		if a.StatusCode != 401 || ab != bb || a.Header.Get(ReasonHeader) != "" || a.Header.Get("Content-Type") != b.Header.Get("Content-Type") {
			t.Errorf("FAIL: %s: an unknown token got %d %q, plain bridge %d %q", path, a.StatusCode, ab, b.StatusCode, bb)
		}
		// Not a group refusal, or a group no repository owns: as the engine said.
		for _, tok := range []string{"FLEET", "GONE"} {
			r, body := post(front, path, tok)
			if r.StatusCode != 403 || r.Header.Get(ReasonHeader) != "" || !strings.Contains(body, "scoped to") {
				t.Errorf("FAIL: %s: %s got %d %q %q", path, tok, r.StatusCode, r.Header.Get(ReasonHeader), body)
			}
		}
	}
	if !t.Failed() {
		t.Log("ok")
	}
}