package main import ( "context" "crypto/sha256" "encoding/hex" "encoding/json" "os" "sync" "testing" "git.novox.be/novox/mesh-sdk/go/catalogue" "git.novox.be/novox/mesh-sdk/go/provider" "git.novox.be/novox/mesh-sdk/go/provisions" ) // bucketBus is the helper's bus over in-memory buckets. type bucketBus struct { mu sync.Mutex buckets map[string]map[string][]byte } func (b *bucketBus) Watch(_ context.Context, bucket string, handle func(string, []byte)) error { b.mu.Lock() defer b.mu.Unlock() for k, v := range b.buckets[bucket] { handle(k, v) } return nil } func (b *bucketBus) Put(_ context.Context, bucket, key string, value []byte) error { b.mu.Lock() defer b.mu.Unlock() if b.buckets[bucket] == nil { b.buckets[bucket] = map[string][]byte{} } b.buckets[bucket][key] = append([]byte(nil), value...) return nil } func (b *bucketBus) Delete(_ context.Context, bucket, key string) error { b.mu.Lock() defer b.mu.Unlock() delete(b.buckets[bucket], key) return nil } // A consumer of a route is answered with the names its plan's entry carries, and nothing of a credential (issue 792), // from the seat mesh-provisioner's plans and keys (novox/hq ADR 0570 clause 2), never the controller's assignments: a // module not handed whose route the seat keys (grafana, issue 774) has a plan of provisions alone, and a helper that // read only the assignments left its key asked for good, so the assigner withheld every new build of 38 such modules. func TestAConsumerOfARouteIsToldItsNamesFromItsPlanEntry(t *testing.T) { bus := &bucketBus{buckets: map[string]map[string][]byte{}} for module, names := range map[string]map[string]string{ "gitea": {"name": "git.example.org", "internal-name": "git.novox.internal", "name-web": "git.example.org"}, "grafana": {"name": "grafana.example.org"}, } { plan, err := provisions.EncodePlan(provisions.Plan{Node: "g14", Module: module, Plan: catalogue.Plan{ Provisions: []catalogue.PlanProvision{{Provision: ProvisionName, Provider: "novox", ProviderModule: ModuleName, At: "10.7.0.1", From: "novox", As: "mesh_g14_" + module, Names: names}}}}) if err != nil { t.Fatal(err) } key := "g14." + module if err := bus.Put(context.Background(), provisions.PlansBucket, key, plan); err != nil { t.Fatal(err) } asked, err := json.Marshal(provisions.Provision{V: provisions.Version, Provision: ProvisionName, Consumer: provisions.End{Node: "g14", Module: module}, Provider: provisions.End{Node: "novox", Module: ModuleName}, State: provisions.Asked}) if err != nil { t.Fatal(err) } if err := bus.Put(context.Background(), provisions.ProvisionsBucket, key+"."+ProvisionName, asked); err != nil { t.Fatal(err) } } p := newProvider("novox", bus, t.Logf) out, err := p.Pass(context.Background()) if err != nil { t.Fatal(err) } if len(out) != 2 || out[0].Status != provider.Answered || out[1].Status != provider.Answered { t.Fatalf("the pass did %+v", out) } for key, want := range map[string]map[string]string{ "g14.gitea.route": {"name-web": "git.example.org", "name": "git.example.org", "internal-name": "git.novox.internal"}, "g14.grafana.route": {"name": "grafana.example.org"}, } { var e provider.Entry if err := json.Unmarshal(bus.buckets[provider.BucketOf(ModuleName)][key], &e); err != nil { t.Fatalf("%s: %v", key, err) } for k, v := range want { if e.Values[k] != v { t.Errorf("%s says %s %v, want %s", key, k, e.Values[k], v) } } if e.State != provisions.Provided || e.VaultName != "" || e.User != "" || e.Refused != "" { t.Errorf("the entry %s is %+v", key, e) } } } // route-proxy's own files and resources stay as they were (issue 792): the module answers its route through the // helper but is not marked moved, so its acme.env, its broker secret and its containers are composed as today and // the public ingress does not change. Everything of the manifest but its build, what it provides and the state it // keeps is pinned by this digest; changing the ingress means changing it here, on purpose. Re-pinned in R4d (novox/hq // ADR 0480): the program is an archive mounted into the trust image, the container and its files are otherwise as before. // Re-pinned for issue 774 (research 072 row 34): the received routes file, its directory, mount and ROUTES went, since // the proxy serves from the seat's plans alone; the ingress, its certificates and its files are otherwise as before. func TestRouteProxyIsNotMovedAndKeepsItsOwnFiles(t *testing.T) { raw, err := os.ReadFile("../../module.json") if err != nil { t.Fatal(err) } var m map[string]any if err := json.Unmarshal(raw, &m); err != nil { t.Fatal(err) } if _, moved := m["moved"]; moved { t.Fatal("route-proxy is marked moved: its own files would be handed") } provides, _ := m["provides"].([]any) if len(provides) != 1 || provides[0].(map[string]any)["answered-by"] != "helper" { t.Fatalf("route is not said to be answered by the helper: %v", provides) } // Its states and its tools are what it says on the bus, not what it runs or writes (issue 774). for _, k := range []string{"build", "provides", "state", "reads", "tools", "replaces"} { delete(m, k) } pinned, err := json.Marshal(m) // sorted keys, compact if err != nil { t.Fatal(err) } sum := sha256.Sum256(pinned) if got := hex.EncodeToString(sum[:]); got != "e96702560003ae7f27490d27fd2353f8c6680d8e16864e15fdc0a833108bc2e5" { t.Fatalf("route-proxy's own manifest changed: digest %s", got) } // The two acme templates are pinned the same way (issue 849): they are what the proxy's certificates are asked // with, so a change to either is a change to the public ingress. for file, want := range map[string]string{ "acme.env": "d66eba145c893080e30e013329fc00e51395f0c31fc8621a20c7b26abc11b52f", "internal-acme.env": "baf4da75379183758c8ae505be4dd8a79d5b089ae1e7c8ded4d232df40251df5", } { text, err := os.ReadFile("../../files/" + file) if err != nil { t.Fatal(err) } sum := sha256.Sum256(text) if got := hex.EncodeToString(sum[:]); got != want { t.Fatalf("route-proxy's files/%s changed: digest %s, pinned %s; if the change is meant, check that the "+ "public and internal ingress still ask for the certificates they should, then pin the new digest here", file, got, want) } } }