| new file mode 100644 |
| index 000000000..a997397ca |
| --- /dev/null |
| +++ b/internal/console/apiconfig_conditional_test.go |
| @@ -0,0 +1,633 @@ |
| +package console |
| + |
| +// Conditional config writes and receipts on the PUT door (#522 plan 1, spec |
| +// §4.5-4.6): if_version and its If-Match synonym, the 412, the refusals that |
| +// answer before anything is read, and the receipt every write now carries. |
| + |
| +import ( |
| + "bytes" |
| + "context" |
| + "encoding/json" |
| + "fmt" |
| + "net/http" |
| + "reflect" |
| + "sort" |
| + "strings" |
| + "sync" |
| + "testing" |
| + |
| + "github.com/carlosframework/platform/internal/appconfig" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// cfgCountStore counts the Gets made under one app's prefix, so a refusal can |
| +// be shown to have read nothing, and runs beforePut once ahead of the first |
| +// Put to putKey: another writer landing between the door's read and its |
| +// write. It makes no promise about conditional writes. |
| +type cfgCountStore struct { |
| + release.Store |
| + prefix string |
| + |
| + mu sync.Mutex |
| + gets int |
| + putKey string |
| + beforePut func(ctx context.Context) |
| +} |
| + |
| +func (c *cfgCountStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + if strings.HasPrefix(key, c.prefix) { |
| + c.mu.Lock() |
| + c.gets++ |
| + c.mu.Unlock() |
| + } |
| + return c.Store.Get(ctx, key) |
| +} |
| + |
| +func (c *cfgCountStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| + c.mu.Lock() |
| + hook := c.beforePut |
| + if key != c.putKey { |
| + hook = nil |
| + } |
| + if hook != nil { |
| + c.beforePut = nil |
| + } |
| + c.mu.Unlock() |
| + if hook != nil { |
| + hook(ctx) |
| + } |
| + return c.Store.Put(ctx, key, body, ifMatch) |
| +} |
| + |
| +func (c *cfgCountStore) getCount() int { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + return c.gets |
| +} |
| + |
| +// atomicDirStore is cfgCountStore claiming an atomic conditional Put. In one |
| +// test process DirStore's mutex makes its check and replace one step, which |
| +// is the promise; production consoles get it from S3. |
| +type atomicDirStore struct{ *cfgCountStore } |
| + |
| +func (atomicDirStore) AtomicConditionalPut() {} |
| + |
| +// useCfgCountStore puts the console on a cfgCountStore over the fixture's bucket, |
| +// atomic or not. fx.rel stays the plain store, so the test's own reads are |
| +// not counted. |
| +func useCfgCountStore(fx *consoleFixture, atomic bool) *cfgCountStore { |
| + c := &cfgCountStore{Store: fx.rel, prefix: release.AppPrefix(release.OpsAccount, "titogo")} |
| + if atomic { |
| + fx.srv.cfg.Releases = atomicDirStore{c} |
| + } else { |
| + fx.srv.cfg.Releases = c |
| + } |
| + return c |
| +} |
| + |
| +// rawAtKey is the stored bytes at key, nil when absent. |
| +func rawAtKey(t *testing.T, fx *consoleFixture, key string) []byte { |
| + t.Helper() |
| + raw, _, err := fx.rel.Get(context.Background(), key) |
| + if err != nil { |
| + return nil |
| + } |
| + return raw |
| +} |
| + |
| +// receiptOf is the receipt object of a PUT's answer. |
| +func receiptOf(t *testing.T, out map[string]any) map[string]any { |
| + t.Helper() |
| + r, ok := out["receipt"].(map[string]any) |
| + if !ok { |
| + t.Fatalf("answer has no receipt: %v", out) |
| + } |
| + return r |
| +} |
| + |
| +// receiptList decodes a JSON array of strings, failing on null: the receipt's |
| +// lists are always present. |
| +func receiptList(t *testing.T, m map[string]any, k string) []string { |
| + t.Helper() |
| + raw, ok := m[k].([]any) |
| + if !ok { |
| + t.Fatalf("%s = %#v, want an array", k, m[k]) |
| + } |
| + out := make([]string, 0, len(raw)) |
| + for _, v := range raw { |
| + out = append(out, v.(string)) |
| + } |
| + return out |
| +} |
| + |
| +func putBody(t *testing.T, body map[string]any) string { |
| + t.Helper() |
| + raw, err := json.Marshal(body) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return string(raw) |
| +} |
| + |
| +func seedDefault(t *testing.T, fx *consoleFixture, token string, set map[string]any) { |
| + t.Helper() |
| + if status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{"set": set}, token); status != http.StatusOK { |
| + t.Fatalf("seed PUT = %d %v", status, out) |
| + } |
| +} |
| + |
| +func envSet(kv ...string) map[string]any { |
| + out := map[string]any{} |
| + for i := 0; i+1 < len(kv); i += 2 { |
| + out[kv[i]] = map[string]any{"kind": "env", "value": kv[i+1]} |
| + } |
| + return out |
| +} |
| + |
| +func configEventCount(t *testing.T, fx *consoleFixture) int { |
| + t.Helper() |
| + events, err := fx.st.Events(OpsAccountID, "titogo", 200) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + n := 0 |
| + for _, e := range events { |
| + if e.Action == "config" { |
| + n++ |
| + } |
| + } |
| + return n |
| +} |
| + |
| +// TestAPIConfigPutReceipt: an unconditional write answers with a receipt |
| +// whose versions name the stored bytes either side and whose lists are |
| +// exact, and keeps today's removed. |
| +func TestAPIConfigPutReceipt(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-receipt") |
| + seedDefault(t, fx, token, envSet("OLD", "1", "KEEP", "2")) |
| + key := appconfig.Key(release.OpsAccount, "titogo") |
| + before := storedVersion(t, fx, key) |
| + _, _, envelope := sealedFor(t, "receipt-secret") |
| + |
| + status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "set": map[string]any{ |
| + "PORT": map[string]any{"kind": "env", "value": "8080"}, |
| + "BANK_KEY": map[string]any{"kind": "secret", "envelope": envelope}, |
| + }, |
| + "unset": []string{"OLD", "NEVER_THERE", "OLD"}, |
| + }, token) |
| + if status != http.StatusOK { |
| + t.Fatalf("PUT = %d %v", status, out) |
| + } |
| + if out["removed"] != 1.0 { |
| + t.Errorf("removed = %v, want 1", out["removed"]) |
| + } |
| + r := receiptOf(t, out) |
| + if str(t, r, "bundle") != appconfig.DefaultBundle { |
| + t.Errorf("bundle = %v", r["bundle"]) |
| + } |
| + if got := str(t, r, "version_before"); got != before { |
| + t.Errorf("version_before = %q, want %q", got, before) |
| + } |
| + if got, want := str(t, r, "version_after"), storedVersion(t, fx, key); got != want { |
| + t.Errorf("version_after = %q, want %q (the stored bytes')", got, want) |
| + } |
| + if r["changed"] != true { |
| + t.Errorf("changed = %v, want true", r["changed"]) |
| + } |
| + if got := receiptList(t, r, "set"); !reflect.DeepEqual(got, []string{"BANK_KEY", "PORT"}) { |
| + t.Errorf("set = %v", got) |
| + } |
| + if got := receiptList(t, r, "unset"); !reflect.DeepEqual(got, []string{"OLD"}) { |
| + t.Errorf("unset = %v", got) |
| + } |
| + if got := receiptList(t, r, "unset_absent"); !reflect.DeepEqual(got, []string{"NEVER_THERE"}) { |
| + t.Errorf("unset_absent = %v", got) |
| + } |
| + stored, _ := storedManifest(t, fx, "titogo") |
| + digests, _ := r["digests"].(map[string]any) |
| + if len(digests) != 2 { |
| + t.Errorf("digests = %v, want the two keys just set", digests) |
| + } |
| + for _, k := range []string{"PORT", "BANK_KEY"} { |
| + if digests[k] != appconfig.Digest(stored.Vars[k]) { |
| + t.Errorf("digest %s = %v, want %q", k, digests[k], appconfig.Digest(stored.Vars[k])) |
| + } |
| + } |
| + if _, ok := r["others"]; ok { |
| + t.Errorf("a default-bundle write carries others: %v", r["others"]) |
| + } |
| + if strings.Contains(fmt.Sprint(out), envelope) { |
| + t.Fatal("the answer carries the envelope") |
| + } |
| +} |
| + |
| +// TestAPIConfigPutIfVersionStale: a stale if_version is 412 naming the |
| +// current version, with nothing written and no generation bump. |
| +func TestAPIConfigPutIfVersionStale(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-stale") |
| + useCfgCountStore(fx, true) |
| + key := appconfig.Key(release.OpsAccount, "titogo") |
| + seedDefault(t, fx, token, envSet("A", "1")) |
| + old := storedVersion(t, fx, key) |
| + seedDefault(t, fx, token, envSet("A", "2")) |
| + cur := storedVersion(t, fx, key) |
| + stored := rawAtKey(t, fx, key) |
| + gen := genEtag(t, fx, "titogo") |
| + events := configEventCount(t, fx) |
| + |
| + status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "if_version": old, "set": envSet("B", "3"), |
| + }, token) |
| + if status != http.StatusPreconditionFailed { |
| + t.Fatalf("stale PUT = %d %v, want 412", status, out) |
| + } |
| + if str(t, out, "version") != cur { |
| + t.Errorf("412 version = %v, want %q", out["version"], cur) |
| + } |
| + if str(t, out, "error") == "" { |
| + t.Error("412 carries no error sentence") |
| + } |
| + if !bytes.Equal(rawAtKey(t, fx, key), stored) { |
| + t.Fatal("a stale write changed the stored bytes") |
| + } |
| + if genEtag(t, fx, "titogo") != gen { |
| + t.Fatal("a stale write bumped the generation") |
| + } |
| + if configEventCount(t, fx) != events { |
| + t.Fatal("a stale write left an audit row") |
| + } |
| + |
| + // At the current version it lands, and the receipt starts there. |
| + status, out = putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "if_version": cur, "set": envSet("B", "3"), |
| + }, token) |
| + if status != http.StatusOK { |
| + t.Fatalf("PUT at the current version = %d %v", status, out) |
| + } |
| + if r := receiptOf(t, out); str(t, r, "version_before") != cur || str(t, r, "version_after") != storedVersion(t, fx, key) { |
| + t.Errorf("receipt versions = %v -> %v", r["version_before"], r["version_after"]) |
| + } |
| + |
| + // "none" creates a named environment once, and is 412 after that. |
| + body := map[string]any{"environment": "staging", "if_version": appconfig.VersionNone, "set": envSet("S", "1")} |
| + if status, out := putJSON(t, configURL(fx, "titogo"), body, token); status != http.StatusOK { |
| + t.Fatalf("create with none = %d %v", status, out) |
| + } |
| + status, out = putJSON(t, configURL(fx, "titogo"), body, token) |
| + envKey := appconfig.EnvironmentKey(release.OpsAccount, "titogo", "staging") |
| + if status != http.StatusPreconditionFailed || str(t, out, "version") != storedVersion(t, fx, envKey) { |
| + t.Fatalf("second create with none = %d %v, want 412 naming the stored version", status, out) |
| + } |
| +} |
| + |
| +// TestAPIConfigPutIfVersionRace: another writer landing between the door's |
| +// read and its write is a 412 too, and its bytes stay (R2-4). |
| +func TestAPIConfigPutIfVersionRace(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-race") |
| + cs := useCfgCountStore(fx, true) |
| + key := appconfig.Key(release.OpsAccount, "titogo") |
| + seedDefault(t, fx, token, envSet("A", "1")) |
| + cur := storedVersion(t, fx, key) |
| + competing := []byte(`{"schema":1,"app":"titogo","vars":{"A":{"kind":"env","value":"theirs"}}}`) |
| + cs.putKey = key |
| + cs.beforePut = func(ctx context.Context) { |
| + _, etag, err := fx.rel.Get(ctx, key) |
| + if err != nil { |
| + t.Error(err) |
| + return |
| + } |
| + if _, err := fx.rel.Put(ctx, key, competing, etag); err != nil { |
| + t.Error(err) |
| + } |
| + } |
| + |
| + status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "if_version": cur, "set": envSet("B", "2"), |
| + }, token) |
| + if status != http.StatusPreconditionFailed { |
| + t.Fatalf("raced PUT = %d %v, want 412", status, out) |
| + } |
| + if !bytes.Equal(rawAtKey(t, fx, key), competing) { |
| + t.Fatal("the competing write was overwritten") |
| + } |
| + if got, want := str(t, out, "version"), appconfig.Version(competing, true); got != want { |
| + t.Errorf("412 version = %q, want the competing bytes' %q", got, want) |
| + } |
| +} |
| + |
| +// TestAPIConfigPutIfVersionWithExecDelivery: one precondition cannot cover |
| +// two writes, so the pair is refused before anything is read. |
| +func TestAPIConfigPutIfVersionWithExecDelivery(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-exec-if") |
| + seedDefault(t, fx, token, envSet("A", "1")) |
| + key := appconfig.Key(release.OpsAccount, "titogo") |
| + cur := storedVersion(t, fx, key) |
| + stored := rawAtKey(t, fx, key) |
| + cs := useCfgCountStore(fx, true) |
| + |
| + status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "if_version": cur, "set": envSet("B", "2"), "exec_delivery": "file", |
| + }, token) |
| + if status != http.StatusBadRequest { |
| + t.Fatalf("if_version with exec_delivery = %d %v, want 400", status, out) |
| + } |
| + if n := cs.getCount(); n != 0 { |
| + t.Errorf("the refusal read the store %d times, want 0", n) |
| + } |
| + if !bytes.Equal(rawAtKey(t, fx, key), stored) { |
| + t.Fatal("the refusal changed the stored bytes") |
| + } |
| +} |
| + |
| +// TestAPIConfigPutIfMatchHeaderConflict: If-Match is the body field's |
| +// synonym; the two disagreeing is a 400 before any read. |
| +func TestAPIConfigPutIfMatchHeaderConflict(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-ifmatch") |
| + seedDefault(t, fx, token, envSet("A", "1")) |
| + key := appconfig.Key(release.OpsAccount, "titogo") |
| + cur := storedVersion(t, fx, key) |
| + cs := useCfgCountStore(fx, true) |
| + u := configURL(fx, "titogo") |
| + |
| + body := putBody(t, map[string]any{"if_version": cur, "set": envSet("B", "2")}) |
| + status, _, out := rawReqHeader(t, http.MethodPut, u, token, body, map[string]string{"If-Match": `"cv1:00"`}) |
| + if status != http.StatusBadRequest { |
| + t.Fatalf("header and body differ = %d %s, want 400", status, out) |
| + } |
| + if n := cs.getCount(); n != 0 { |
| + t.Errorf("the refusal read the store %d times, want 0", n) |
| + } |
| + |
| + // The header alone, stale: 412, as the body field would be. |
| + body = putBody(t, map[string]any{"set": envSet("B", "2")}) |
| + if status, _, out := rawReqHeader(t, http.MethodPut, u, token, body, map[string]string{"If-Match": "cv1:00"}); status != http.StatusPreconditionFailed { |
| + t.Fatalf("stale header = %d %s, want 412", status, out) |
| + } |
| + // The header alone, current and quoted as an HTTP entity tag: it lands. |
| + status, _, out = rawReqHeader(t, http.MethodPut, u, token, body, map[string]string{"If-Match": `"` + cur + `"`}) |
| + if status != http.StatusOK { |
| + t.Fatalf("current header = %d %s, want 200", status, out) |
| + } |
| + var res map[string]any |
| + if err := json.Unmarshal([]byte(out), &res); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := receiptOf(t, res); str(t, r, "version_before") != cur { |
| + t.Errorf("version_before = %v, want %q", r["version_before"], cur) |
| + } |
| + // Header and body agreeing is the same as either. |
| + now := storedVersion(t, fx, key) |
| + body = putBody(t, map[string]any{"if_version": now, "set": envSet("C", "3")}) |
| + if status, _, out := rawReqHeader(t, http.MethodPut, u, token, body, map[string]string{"If-Match": now}); status != http.StatusOK { |
| + t.Fatalf("agreeing header and body = %d %s, want 200", status, out) |
| + } |
| +} |
| + |
| +// TestAPIConfigPutIfVersionOnDirStore: a store that cannot promise an |
| +// atomic conditional write refuses if_version before any read (R2-2), and |
| +// keeps unconditional writes as they were. |
| +func TestAPIConfigPutIfVersionOnDirStore(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-dirstore") |
| + seedDefault(t, fx, token, envSet("A", "1")) |
| + key := appconfig.Key(release.OpsAccount, "titogo") |
| + cur := storedVersion(t, fx, key) |
| + stored := rawAtKey(t, fx, key) |
| + cs := useCfgCountStore(fx, false) |
| + |
| + status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "if_version": cur, "set": envSet("B", "2"), |
| + }, token) |
| + if status != http.StatusBadRequest { |
| + t.Fatalf("if_version on a DirStore = %d %v, want 400", status, out) |
| + } |
| + if n := cs.getCount(); n != 0 { |
| + t.Errorf("the refusal read the store %d times, want 0", n) |
| + } |
| + if !bytes.Equal(rawAtKey(t, fx, key), stored) { |
| + t.Fatal("the refusal changed the stored bytes") |
| + } |
| + if status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{"set": envSet("B", "2")}, token); status != http.StatusOK { |
| + t.Fatalf("unconditional PUT on a DirStore = %d %v", status, out) |
| + } |
| +} |
| + |
| +// TestAPIConfigPutIfVersionSetAndUnsetSameName: with a precondition, a name |
| +// both set and unset is refused before any read; without one it is today's |
| +// write. |
| +func TestAPIConfigPutIfVersionSetAndUnsetSameName(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-both") |
| + seedDefault(t, fx, token, envSet("A", "1")) |
| + key := appconfig.Key(release.OpsAccount, "titogo") |
| + cur := storedVersion(t, fx, key) |
| + stored := rawAtKey(t, fx, key) |
| + cs := useCfgCountStore(fx, true) |
| + |
| + status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "if_version": cur, "set": envSet("A", "2"), "unset": []string{"A"}, |
| + }, token) |
| + if status != http.StatusBadRequest { |
| + t.Fatalf("set and unset of one name with if_version = %d %v, want 400", status, out) |
| + } |
| + if n := cs.getCount(); n != 0 { |
| + t.Errorf("the refusal read the store %d times, want 0", n) |
| + } |
| + if !bytes.Equal(rawAtKey(t, fx, key), stored) { |
| + t.Fatal("the refusal changed the stored bytes") |
| + } |
| + if status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "set": envSet("A", "2"), "unset": []string{"A"}, |
| + }, token); status != http.StatusOK { |
| + t.Fatalf("the same write without if_version = %d %v, want today's 200", status, out) |
| + } |
| +} |
| + |
| +// TestAPIConfigPutOthersForNamedEnv: a write to a named environment that is |
| +// not owned names the default bundle's version either side of it; a write |
| +// to an owned one, whose merge never reads the default, does not. |
| +func TestAPIConfigPutOthersForNamedEnv(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-others") |
| + seedDefault(t, fx, token, envSet("BASE", "1")) |
| + defKey := appconfig.Key(release.OpsAccount, "titogo") |
| + def := storedVersion(t, fx, defKey) |
| + |
| + status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "environment": "staging", "set": envSet("S", "1"), |
| + }, token) |
| + if status != http.StatusOK { |
| + t.Fatalf("PUT staging = %d %v", status, out) |
| + } |
| + r := receiptOf(t, out) |
| + if str(t, r, "bundle") != "staging" { |
| + t.Errorf("bundle = %v", r["bundle"]) |
| + } |
| + envKey := appconfig.EnvironmentKey(release.OpsAccount, "titogo", "staging") |
| + if str(t, r, "version_before") != appconfig.VersionNone || str(t, r, "version_after") != storedVersion(t, fx, envKey) { |
| + t.Errorf("versions = %v -> %v", r["version_before"], r["version_after"]) |
| + } |
| + others, _ := r["others"].(map[string]any) |
| + pair, _ := others["default"].(map[string]any) |
| + if pair == nil || str(t, pair, "before") != def || str(t, pair, "after") != def { |
| + t.Fatalf("others = %v, want default before and after both %q", r["others"], def) |
| + } |
| + |
| + // The default bundle written during the request: before and after differ. |
| + cs := useCfgCountStore(fx, false) |
| + cs.putKey = envKey |
| + cs.beforePut = func(ctx context.Context) { |
| + if err := appconfig.SetEnv(ctx, fx.rel, release.OpsAccount, "titogo", map[string]string{"BASE": "2"}); err != nil { |
| + t.Error(err) |
| + } |
| + } |
| + status, out = putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "environment": "staging", "set": envSet("S", "2"), |
| + }, token) |
| + if status != http.StatusOK { |
| + t.Fatalf("second PUT staging = %d %v", status, out) |
| + } |
| + pair, _ = receiptOf(t, out)["others"].(map[string]any)["default"].(map[string]any) |
| + if str(t, pair, "before") != def || str(t, pair, "after") != storedVersion(t, fx, defKey) || str(t, pair, "after") == def { |
| + t.Fatalf("others.default = %v, want %q then the moved version", pair, def) |
| + } |
| + |
| + // An owned environment, written by the account itself: no others. |
| + if _, err := appconfig.ApplyEnvironmentOwned(context.Background(), fx.rel, release.OpsAccount, "titogo", "team", "alta", |
| + map[string]appconfig.Var{"T": {Kind: "env", Value: "1"}}, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + status, out = putJSON(t, configURL(fx, "titogo"), map[string]any{ |
| + "environment": "team", "set": envSet("T", "2"), |
| + }, token) |
| + if status != http.StatusOK { |
| + t.Fatalf("PUT owned = %d %v", status, out) |
| + } |
| + if o, ok := receiptOf(t, out)["others"]; ok { |
| + t.Errorf("an owned environment's receipt carries others: %v", o) |
| + } |
| +} |
| + |
| +// TestAPIConfigTeamPutIfVersion: a team's conditional write is checked on |
| +// the owned path, ownership first; "none" (create) is refused before any |
| +// read (R1-3). |
| +func TestAPIConfigTeamPutIfVersion(t *testing.T) { |
| + w := newTeamWorld(t, "code-team-ifversion") |
| + cs := useCfgCountStore(w.fx, true) |
| + ctx := context.Background() |
| + key := appconfig.EnvironmentKey(release.OpsAccount, "titogo", "team") |
| + old := storedVersion(t, w.fx, key) |
| + if _, err := appconfig.ApplyEnvironmentOwned(ctx, w.fx.rel, "bab", "titogo", "team", "alta", |
| + map[string]appconfig.Var{"MOVED": {Kind: "env", Value: "1"}}, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + cur := storedVersion(t, w.fx, key) |
| + stored := rawAtKey(t, w.fx, key) |
| + |
| + status, out := putJSON(t, w.url("/config"), map[string]any{ |
| + "environment": "team", "if_version": old, "set": envSet("K", "v"), |
| + }, w.team) |
| + if status != http.StatusPreconditionFailed || str(t, out, "version") != cur { |
| + t.Fatalf("stale team PUT = %d %v, want 412 naming %q", status, out, cur) |
| + } |
| + if !bytes.Equal(rawAtKey(t, w.fx, key), stored) { |
| + t.Fatal("a stale team write changed the stored bytes") |
| + } |
| + |
| + // Another scope's bundle is the not-found it always was, whatever the |
| + // version: a 412 would say it exists. |
| + other := storedVersion(t, w.fx, appconfig.EnvironmentKey(release.OpsAccount, "titogo", "other")) |
| + w.notFound(t, "conditional PUT other", http.MethodPut, "/config", |
| + putBody(t, map[string]any{"environment": "other", "if_version": other, "set": envSet("K", "v")})) |
| + |
| + // The scope gate reads the named bundle's owner before any team |
| + // handler runs. A request refused by the handler's own body checks |
| + // costs exactly that read and no more, so it is the yardstick. |
| + before := cs.getCount() |
| + if status, out := putJSON(t, w.url("/config"), map[string]any{ |
| + "environment": "fresh", "set": envSet("bad-name", "v"), |
| + }, w.team); status != http.StatusBadRequest { |
| + t.Fatalf("yardstick PUT = %d %v, want 400", status, out) |
| + } |
| + gate := cs.getCount() - before |
| + before = cs.getCount() |
| + status, out = putJSON(t, w.url("/config"), map[string]any{ |
| + "environment": "fresh", "if_version": appconfig.VersionNone, "set": envSet("K", "v"), |
| + }, w.team) |
| + if status != http.StatusBadRequest { |
| + t.Fatalf("team PUT with none = %d %v, want 400", status, out) |
| + } |
| + if n := cs.getCount() - before; n != gate { |
| + t.Errorf("the refusal read the store %d times, want the scope gate's %d alone", n, gate) |
| + } |
| + if _, _, found, _ := appconfig.GetEnvironment(ctx, w.fx.rel, "bab", "titogo", "fresh"); found { |
| + t.Fatal("a refused create landed") |
| + } |
| + |
| + status, out = putJSON(t, w.url("/config"), map[string]any{ |
| + "environment": "team", "if_version": cur, "set": envSet("K", "v"), "unset": []string{"TEAM_ONLY"}, |
| + }, w.team) |
| + if status != http.StatusOK || out["removed"] != 1.0 { |
| + t.Fatalf("team PUT at the current version = %d %v", status, out) |
| + } |
| + r := receiptOf(t, out) |
| + if str(t, r, "version_before") != cur || str(t, r, "version_after") != storedVersion(t, w.fx, key) { |
| + t.Errorf("receipt versions = %v -> %v", r["version_before"], r["version_after"]) |
| + } |
| + if _, ok := r["others"]; ok { |
| + t.Errorf("a team receipt names the default bundle: %v", r["others"]) |
| + } |
| +} |
| + |
| +// TestAPIConfigPutNoContentInLogsOrAudit: versions and digests go to the |
| +// caller only; the log and the audit row carry what they carried before. |
| +func TestAPIConfigPutNoContentInLogsOrAudit(t *testing.T) { |
| + fx, token := configFixture(t, "code-config-nocontent") |
| + useCfgCountStore(fx, true) |
| + buf := captureLog(t) |
| + _, _, envelope := sealedFor(t, "audit-secret") |
| + const plain = "plain-value-canary" |
| + set := map[string]any{ |
| + "PORT": map[string]any{"kind": "env", "value": plain}, |
| + "BANK_KEY": map[string]any{"kind": "secret", "envelope": envelope}, |
| + } |
| + status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{"set": set}, token) |
| + if status != http.StatusOK { |
| + t.Fatalf("PUT = %d %v", status, out) |
| + } |
| + stale := str(t, receiptOf(t, out), "version_before") |
| + // A refused conditional write logs too; it must say no more. |
| + if status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{"if_version": stale, "set": set}, token); status != http.StatusPreconditionFailed { |
| + t.Fatalf("stale PUT = %d %v", status, out) |
| + } |
| + cur := storedVersion(t, fx, appconfig.Key(release.OpsAccount, "titogo")) |
| + if status, out := putJSON(t, configURL(fx, "titogo"), map[string]any{"if_version": cur, "set": set}, token); status != http.StatusOK { |
| + t.Fatalf("conditional PUT = %d %v", status, out) |
| + } |
| + |
| + logged := buf.String() |
| + for _, bad := range []string{envelope, plain, "cv1:", "vd1:"} { |
| + if strings.Contains(logged, bad) { |
| + t.Errorf("the log carries %q:\n%s", bad, logged) |
| + } |
| + } |
| + events, err := fx.st.Events(OpsAccountID, "titogo", 50) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var details []string |
| + for _, e := range events { |
| + row := fmt.Sprintf("%+v", e) |
| + for _, bad := range []string{envelope, plain, "cv1:", "vd1:"} { |
| + if strings.Contains(row, bad) { |
| + t.Errorf("audit row carries %q: %s", bad, row) |
| + } |
| + } |
| + if e.Action == "config" { |
| + details = append(details, e.Detail) |
| + } |
| + } |
| + sort.Strings(details) |
| + want := []string{"config: 2 set, 0 unset (default) (cli)", "config: 2 set, 0 unset (default) (cli)"} |
| + if !reflect.DeepEqual(details, want) { |
| + t.Errorf("config audit details = %q, want %q", details, want) |
| + } |
| +} |