diff --git a/modules/gitea/internal/seatgit/whole_integration_test.go b/modules/gitea/internal/seatgit/whole_integration_test.go index eaa20face..f8bc98ae0 100644 --- a/modules/gitea/internal/seatgit/whole_integration_test.go +++ b/modules/gitea/internal/seatgit/whole_integration_test.go @@ -16,10 +16,16 @@ package seatgit // - With the forge's store stopped a merge call answers away, and called again once it is back it lands (H2 §776). // - A forge that refuses the second merge gives one merge-called with the first member and `refused`, and merge-partial // raised; a later call landing the refused member clears it (§767, test 115). +// - No token is configured: the bundle mints the tools' account's token at the fixture forge with the admin's login, +// writes it through the vault seat's two-step sealed own-put and reads it through its sealed get, both answered +// here, and the forge holds one token for the account after, the old fixed-name token deleted (H2 §808, H-A-422, +// H-A-430 (1), H-A-453 (3)). import ( "bufio" "context" + "crypto/rand" + "encoding/base64" "encoding/json" "errors" "fmt" @@ -35,9 +41,11 @@ import ( "testing" "time" + "git.novox.be/novox/mesh-sdk/go/seal" "git.novox.be/novox/mesh-sdk/go/testbus" "github.com/nats-io/nats.go" "github.com/nats-io/nats.go/jetstream" + "golang.org/x/crypto/nacl/box" ) const within = 30 * time.Second @@ -58,8 +66,107 @@ type forge struct { paths []string // away is the forge's store stopped (§776): the API answers 503 and the version read names the store unavailable. away bool + // admin is the admin's login, which mints (§808); tokens what each account holds; every other call is refused 401 + // unless it carries one of them. + admin string + tokens map[string][]forgeToken + nextID int } +// forgeToken is one access token the fixture forge minted. +type forgeToken struct { + ID int `json:"id"` + Name string `json:"name"` + sha1 string +} + +// authorized is the admin's basic login or a token the forge holds. +func (f *forge) authorized(r *http.Request) bool { + if u, p, ok := r.BasicAuth(); ok { + return u == f.admin && p == adminPassword + } + given := strings.TrimPrefix(r.Header.Get("Authorization"), "token ") + for _, held := range f.tokens { + for _, t := range held { + if given != "" && t.sha1 == given { + return true + } + } + } + return false +} + +// held answers the names of the tokens an account holds and their values. +func (f *forge) held(account string) (names, values []string) { + f.mu.Lock() + defer f.mu.Unlock() + for _, t := range f.tokens[account] { + names, values = append(names, t.Name), append(values, t.sha1) + } + return names, values +} + +// serveAdmin answers the admin API the mint uses: the admin's own read, the account and its team, and the account's +// tokens (mint, list, delete). False when the path is not one of them. +func (f *forge) serveAdmin(w http.ResponseWriter, r *http.Request) bool { + switch p := r.URL.Path; { + case p == "/api/v1/user": + _ = json.NewEncoder(w).Encode(map[string]any{"login": f.admin}) + case p == "/api/v1/admin/users" && r.Method == http.MethodPost: + w.WriteHeader(http.StatusCreated) + case p == "/api/v1/orgs/novox/teams" && r.Method == http.MethodGet: + _, _ = w.Write([]byte("[]")) + case p == "/api/v1/orgs/novox/teams" && r.Method == http.MethodPost: + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(`{"id":1}`)) + case strings.HasPrefix(p, "/api/v1/teams/1/members/"): + w.WriteHeader(http.StatusNoContent) + default: + m := regexp.MustCompile(`^/api/v1/users/([^/]+)/tokens(?:/(\d+))?$`).FindStringSubmatch(p) + if m == nil { + return false + } + account := m[1] + switch { + case r.Method == http.MethodPost: + var asked struct { + Name string `json:"name"` + } + _ = json.NewDecoder(r.Body).Decode(&asked) + for _, t := range f.tokens[account] { + if t.Name == asked.Name { + http.Error(w, `{"message":"access token name has been used already"}`, http.StatusUnprocessableEntity) + return true + } + } + f.nextID++ + t := forgeToken{ID: f.nextID, Name: asked.Name, sha1: fmt.Sprintf("%040x", 0xa0000+f.nextID)} + f.tokens[account] = append(f.tokens[account], t) + w.WriteHeader(http.StatusCreated) + _ = json.NewEncoder(w).Encode(map[string]any{"id": t.ID, "name": t.Name, "sha1": t.sha1}) + case r.Method == http.MethodGet: + if r.URL.Query().Get("page") != "1" { + _, _ = w.Write([]byte("[]")) + return true + } + _ = json.NewEncoder(w).Encode(f.tokens[account]) + case r.Method == http.MethodDelete: + kept := f.tokens[account][:0] + for _, t := range f.tokens[account] { + if fmt.Sprint(t.ID) != m[2] { + kept = append(kept, t) + } + } + f.tokens[account] = kept + w.WriteHeader(http.StatusNoContent) + } + } + return true +} + +// adminPassword is the admin's password the fixture forge takes, in the file the host would unseal it into. +const adminPassword = "admin-password" + func (f *forge) serve(w http.ResponseWriter, r *http.Request) { f.mu.Lock() defer f.mu.Unlock() @@ -76,6 +183,13 @@ func (f *forge) serve(w http.ResponseWriter, r *http.Request) { http.Error(w, "Service Unavailable", http.StatusServiceUnavailable) return } + if !f.authorized(r) { + http.Error(w, `{"message":"token is required"}`, http.StatusUnauthorized) + return + } + if f.serveAdmin(w, r) { + return + } m := regexp.MustCompile(`^/api/v1/repos/([^/]+/[^/]+)/pulls/(\d+)(/merge)?$`).FindStringSubmatch(r.URL.Path) if m != nil { p := f.pulls[m[1]+"#"+m[2]] @@ -136,6 +250,76 @@ type runtime struct { // regs are what the seat mesh-catalog's registrations answers; subs the patterns the bundle subscribed. regs []map[string]any subs []string + // vault is the seat mesh-vault as this test answers it (§808, §822): the module's own secrets by name, and the + // own-put calls opened, each with the key its value is sealed to. + vault map[string]vaulted + puts map[string]openPut +} + +// openPut is an own-put call its first step opened: the name it puts and the key its value is sealed to. +type openPut struct { + name string + pub, priv *[32]byte +} + +// vaulted is one secret the test's vault keeps: its newest value and generation. +type vaulted struct { + value string + generation int +} + +// vaultAsk answers the seat mesh-vault's get, sealed to the asking process's key, and its two-step own-put, whose value +// comes sealed to the key the first step made (§822). +func (r *runtime) vaultAsk(key string, body json.RawMessage) (any, error) { + var b struct { + Name string `json:"name"` + PublicKey string `json:"public_key"` + Call string `json:"call"` + Sealed string `json:"sealed"` + } + _ = json.Unmarshal(body, &b) + r.mu.Lock() + defer r.mu.Unlock() + switch { + case key == "seat:mesh-vault.get": + v, ok := r.vault[b.Name] + if !ok { + return nil, fmt.Errorf("the vault holds no secret named %s", b.Name) + } + sealed, err := seal.Seal(v.value, b.PublicKey) + if err != nil { + return nil, err + } + return map[string]any{"generation": v.generation, "changed-at": time.Now().UTC().Format(time.RFC3339), "sealed": sealed}, nil + case b.Call == "": + pub, priv, err := box.GenerateKey(rand.Reader) + if err != nil { + return nil, err + } + r.next++ + call := fmt.Sprintf("put-%d", r.next) + r.puts[call] = openPut{name: b.Name, pub: pub, priv: priv} + return map[string]any{"call": call, "seal_to": base64.StdEncoding.EncodeToString(pub[:])}, nil + default: + put, ok := r.puts[b.Call] + if !ok { + return nil, fmt.Errorf("no own-put call %s is open", b.Call) + } + raw, err := base64.StdEncoding.DecodeString(b.Sealed) + if err != nil { + return nil, err + } + value, ok := box.OpenAnonymous(nil, raw, put.pub, put.priv) + if !ok { + return nil, fmt.Errorf("the own-put value of %s is not sealed to its call's key", b.Call) + } + // The module named by the caller's account, never by an argument: here always the forge module (§822). + name := "module/gitea/" + put.name + delete(r.puts, b.Call) + v := vaulted{value: string(value), generation: r.vault[name].generation + 1} + r.vault[name] = v + return map[string]any{"generation": v.generation}, nil + } } func (r *runtime) subscribed(pattern string) bool { @@ -208,7 +392,15 @@ func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessa r.subs = append(r.subs, p.Pattern) r.mu.Unlock() return map[string]any{}, nil - case "mesh/publish", "mesh/state.watch", "mesh/seat.record": + case "mesh/state.watch": + // The mint's lease is watched for its release (§808): its changes are delivered as the runtime delivers them. + if p.State == "token-lease" { + var watch string + _ = json.Unmarshal(id, &watch) + return map[string]any{}, r.watchState(kv, p.Key, watch) + } + return map[string]any{}, nil + case "mesh/publish", "mesh/seat.record": return map[string]any{}, nil case "mesh/capabilities": return map[string]any{"words": []string{}}, nil @@ -226,6 +418,8 @@ func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessa return map[string]any{"registrations": r.regs}, nil case "seat:mesh-hand-acts.hand-act": return map[string]any{}, nil + case "seat:mesh-vault.get", "seat:mesh-vault.own-put": + return r.vaultAsk(a.Key, p.Body) } return nil, fmt.Errorf("the runtime answers no ask %s in this test", a.Key) case "mesh/state.get": @@ -240,6 +434,15 @@ func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessa return nil, err } return map[string]any{"key": p.Key, "value": json.RawMessage(e.Value()), "revision": e.Revision()}, nil + case "mesh/state.create": + if kv == nil { + return nil, fmt.Errorf("the state %q is not declared", p.State) + } + rev, err := kv.Create(ctx, p.Key, p.Value) + if errors.Is(err, jetstream.ErrKeyExists) { + return nil, changed{err} + } + return map[string]any{"revision": rev}, err case "mesh/state.put": if kv == nil { return nil, fmt.Errorf("the state %q is not declared", p.State) @@ -269,12 +472,46 @@ func (r *runtime) answer(id json.RawMessage, method string, params json.RawMessa return } if err != nil { - r.say(map[string]any{"id": id, "error": map[string]any{"code": -32000, "message": err.Error()}}) + code := -32000 + if errors.As(err, new(changed)) { + code = -32010 + } + r.say(map[string]any{"id": id, "error": map[string]any{"code": code, "message": err.Error()}}) return } r.say(map[string]any{"id": id, "result": result}) } +// watchState delivers each later change of one key of a state to the bundle's watch, as `mesh/state` requests naming +// the watch, a delete without its current value. +func (r *runtime) watchState(kv jetstream.KeyValue, key, watch string) error { + w, err := kv.Watch(context.Background(), key, jetstream.UpdatesOnly()) + if err != nil { + return err + } + r.t.Cleanup(func() { _ = w.Stop() }) + go func() { + for e := range w.Updates() { + if e == nil { + continue + } + change := map[string]any{"key": e.Key()} + if e.Operation() == jetstream.KeyValuePut { + change["current"] = map[string]any{"value": json.RawMessage(e.Value()), "revision": e.Revision()} + } + r.mu.Lock() + r.next++ + n := r.next + r.mu.Unlock() + r.say(map[string]any{"id": fmt.Sprintf("rt-watch-%d", n), "method": "mesh/state", "params": map[string]any{"watch": watch, "change": change}}) + } + }() + return nil +} + +// changed is the runtime's refusal of a create whose key has a value (mesh-sdk's CODE_CHANGED, -32010). +type changed struct{ error } + // ask sends the bundle one request and answers its result. func (r *runtime) ask(method string, params map[string]any) json.RawMessage { r.t.Helper() @@ -399,7 +636,7 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) { t.Fatal(err) } kvs := map[string]jetstream.KeyValue{} - for _, name := range []string{"bound", "announced", "findings", "first-views", "transfers", "idle", "failed-steps", "calls"} { + for _, name := range []string{"bound", "announced", "findings", "first-views", "transfers", "idle", "failed-steps", "calls", "token-lease"} { kv, err := js.CreateKeyValue(ctx, jetstream.KeyValueConfig{Bucket: "gitea_" + name}) if err != nil { t.Fatal(err) @@ -425,15 +662,22 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) { "novox/lib#10": {repo: "novox/lib", base: "main", head: B, number: 10}, "novox/hq#11": {repo: "novox/hq", base: "main", head: H, number: 11}, }} + // The forge holds the tools' account's token under its old fixed name, which the first mint replaces (H-A-430 (1)). + f.admin, f.nextID = "mesh-admin", 1 + f.tokens = map[string][]forgeToken{"mesh-tools": {{ID: 1, Name: "mesh-tools", sha1: strings.Repeat("0", 40)}}} srv := httptest.NewServer(http.HandlerFunc(f.serve)) t.Cleanup(srv.Close) state := t.TempDir() + passwordFile := filepath.Join(state, "admin.secret") + if err := os.WriteFile(passwordFile, []byte(adminPassword+"\n"), 0o600); err != nil { + t.Fatal(err) + } launch := `const sdk = await import("@novox/mesh-sdk/stdio"); await import("./dist/index.js"); await import("./dist/tools/index.js"); await sdk.serveRegisteredOverStdio("gitea");` cmd := exec.Command("node", "--input-type=module", "-e", launch) cmd.Dir = dir cmd.Env = append(os.Environ(), "MESH_SERVED_MODULE=gitea", "MESH_MODULE=gitea", "MESH_NODE=test", "MESH_GITEA_URL="+srv.URL, - "GITEA_TOKEN=t", "MESH_GITEA_STATE_DIR="+state, "MESH_GITEA_HOOK_PORT=0", "MESH_GITEA_CONFIG_FILE="+filepath.Join(state, "config.json")) + "MESH_GITEA_ADMIN_USER=mesh-admin", "MESH_GITEA_ADMIN_PASSWORD_FILE="+passwordFile, "MESH_GITEA_STATE_DIR="+state, "MESH_GITEA_HOOK_PORT=0", "MESH_GITEA_CONFIG_FILE="+filepath.Join(state, "config.json")) in, _ := cmd.StdinPipe() out, _ := cmd.StdoutPipe() cmd.Stderr = testLog{t} @@ -447,6 +691,7 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) { // A source-only registration (H2 §815): no module, one watched branch, its check a build seat's step. {"origin": "novox/hq", "branches": []string{"main"}, "policy": map[string]any{"steps": map[string]any{"unit": "required"}}, "check": map[string]any{"seat": "node-build-go", "step": "unit"}}}} + rt.vault, rt.puts = map[string]vaulted{}, map[string]openPut{} go rt.read(out) // At the start the stopped call says one merge-called naming the two that landed (§766). @@ -531,6 +776,27 @@ func TestTheSeatGitWholeOverARealBus(t *testing.T) { _, err := kvs["findings"].Get(ctx, key) return errors.Is(err, jetstream.ErrKeyNotFound) }) + + // No token was configured: the calls above ran on the token the bundle minted with the admin's login, wrote through + // the two-step sealed own-put and read through the sealed get; the forge holds one token for the account after, the + // one the vault keeps, and the old fixed-name token is gone (§808, H-A-430 (1)). + until(t, "the forge holds one token for the tools' account", func() bool { + names, _ := f.held("mesh-tools") + return len(names) == 1 + }) + names, values := f.held("mesh-tools") + rt.mu.Lock() + kept := rt.vault["module/gitea/mesh-tools"] + rt.mu.Unlock() + var opened struct { + Token string `json:"token"` + Scopes []string `json:"scopes"` + } + _ = json.Unmarshal([]byte(kept.value), &opened) + if fmt.Sprint(names) != "[mesh-tools-1]" || opened.Token != values[0] || kept.generation != 1 || len(opened.Scopes) == 0 { + t.Fatalf("the forge holds %v for the tools' account; the vault keeps generation %d of a token %t to it, scopes %v", + names, kept.generation, opened.Token == values[0], opened.Scopes) + } } type testLog struct{ t *testing.T }