| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "sync/atomic" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/appconfig" |
| 11 | "github.com/carlosframework/platform/internal/objstore" |
| 12 | "github.com/carlosframework/platform/internal/release" |
| 13 | ) |
| 14 | |
| 15 | type callbackConfigRaceStore struct { |
| 16 | release.Store |
| 17 | key string |
| 18 | before func() |
| 19 | loseReply bool |
| 20 | raced atomic.Bool |
| 21 | armed atomic.Bool |
| 22 | replyLost atomic.Bool |
| 23 | } |
| 24 | |
| 25 | var errCallbackReplyLost = errors.New("callback config response lost") |
| 26 | |
| 27 | func (s *callbackConfigRaceStore) Get(ctx context.Context, key string) ([]byte, string, error) { |
| 28 | if key == s.key && s.raced.CompareAndSwap(false, true) { |
| 29 | s.before() |
| 30 | s.armed.Store(true) |
| 31 | } |
| 32 | return s.Store.Get(ctx, key) |
| 33 | } |
| 34 | |
| 35 | func (s *callbackConfigRaceStore) Put(ctx context.Context, key string, body []byte, etag string) (string, error) { |
| 36 | got, err := s.Store.Put(ctx, key, body, etag) |
| 37 | if key == s.key && err == nil && s.loseReply && s.armed.Load() && s.replyLost.CompareAndSwap(false, true) { |
| 38 | return "", errCallbackReplyLost |
| 39 | } |
| 40 | return got, err |
| 41 | } |
| 42 | |
| 43 | func TestCallbackKeyConvergesAcrossConcurrentRotationsWithoutClaims(t *testing.T) { |
| 44 | for _, loseReply := range []bool{false, true} { |
| 45 | name := "completed writes" |
| 46 | if loseReply { |
| 47 | name = "interrupted delivery resumes despite current key ID" |
| 48 | } |
| 49 | t.Run(name, func(t *testing.T) { |
| 50 | fx, token, private := rotationRaceFixture(t, "key-sync-race") |
| 51 | if fx.srv.cfg.HostClaim { |
| 52 | t.Fatal("this regression must exercise independent writers without claims") |
| 53 | } |
| 54 | fx.srv.cfg.CallbackSigner = callbackTestSigner(t, "new-sync-master", testWebhookMaster) |
| 55 | store := &callbackConfigRaceStore{Store: fx.rel, key: appconfig.Key(fx.sqid, "titogo"), loseReply: loseReply} |
| 56 | store.before = func() { |
| 57 | status, body := postJSON(t, scanURL(fx, "titogo")+"/callback-key/rotate", map[string]any{}, token) |
| 58 | if status != http.StatusOK { |
| 59 | t.Fatalf("concurrent app rotation = %d %v", status, body) |
| 60 | } |
| 61 | } |
| 62 | fx.srv.cfg.Releases = store |
| 63 | pub, _ := appconfig.ParsePublicKey(fx.srv.cfg.SecretsPub, "test") |
| 64 | remaining, _, err := fx.srv.rotateWebhookBudget(context.Background(), pub) |
| 65 | if loseReply { |
| 66 | if !errors.Is(err, errCallbackReplyLost) || !store.replyLost.Load() { |
| 67 | t.Fatalf("interrupted delivery = %v, want lost-response error", err) |
| 68 | } |
| 69 | remaining, _, err = fx.srv.rotateWebhookBudget(context.Background(), pub) |
| 70 | } |
| 71 | if err != nil || remaining != 0 || !store.raced.Load() { |
| 72 | t.Fatalf("rotation remaining=%d err=%v raced=%v", remaining, err, store.raced.Load()) |
| 73 | } |
| 74 | rec := storedRecord(t, fx, "titogo") |
| 75 | want := string(fx.srv.cfg.CallbackSigner.DerivedKey(fx.sqid, "titogo", rec.Scanning.WebhookGen)) |
| 76 | if got := callbackSecret(t, fx, private, "titogo"); got != want { |
| 77 | t.Fatal("completed rotation left a secret for a different generation") |
| 78 | } |
| 79 | }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestCallbackKeyConvergenceHonorsRevokeDuringDelivery(t *testing.T) { |
| 84 | fx, token, _ := rotationRaceFixture(t, "key-sync-revoke") |
| 85 | fx.srv.cfg.CallbackSigner = callbackTestSigner(t, "new-sync-master", testWebhookMaster) |
| 86 | store := &callbackConfigRaceStore{Store: fx.rel, key: appconfig.Key(fx.sqid, "titogo")} |
| 87 | store.before = func() { |
| 88 | status, body := postJSON(t, scanURL(fx, "titogo")+"/revoke", map[string]any{}, token) |
| 89 | if status != http.StatusOK { |
| 90 | t.Fatalf("concurrent revoke = %d %v", status, body) |
| 91 | } |
| 92 | } |
| 93 | fx.srv.cfg.Releases = store |
| 94 | pub, _ := appconfig.ParsePublicKey(fx.srv.cfg.SecretsPub, "test") |
| 95 | remaining, changed, err := fx.srv.rotateWebhookBudget(context.Background(), pub) |
| 96 | if err != nil || remaining != 0 || changed != 0 || !store.raced.Load() { |
| 97 | t.Fatalf("rotation after revoke remaining=%d changed=%d err=%v", remaining, changed, err) |
| 98 | } |
| 99 | if storedRecord(t, fx, "titogo").Scanning.Enabled != nil { |
| 100 | t.Fatal("rotation recreated scanning consent") |
| 101 | } |
| 102 | if _, ok := appVars(t, fx, "titogo")[objstore.DefaultEnvPrefix+"_SCAN_CALLBACK_SECRET"]; ok { |
| 103 | t.Fatal("rotation left a callback secret after revoke") |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestScanGrantReplacesAnExistingCallbackKeyAfterMasterRotation(t *testing.T) { |
| 108 | fx, token, private := rotationRaceFixture(t, "key-sync-grant") |
| 109 | old := callbackSecret(t, fx, private, "titogo") |
| 110 | fx.srv.cfg.CallbackSigner = callbackTestSigner(t, "new-sync-master", testWebhookMaster) |
| 111 | status, body := postJSON(t, scanURL(fx, "titogo")+"/grant", map[string]any{}, token) |
| 112 | if status != http.StatusOK { |
| 113 | t.Fatalf("repeat grant = %d %v", status, body) |
| 114 | } |
| 115 | rec := storedRecord(t, fx, "titogo") |
| 116 | want := string(fx.srv.cfg.CallbackSigner.DerivedKey(fx.sqid, "titogo", rec.Scanning.WebhookGen)) |
| 117 | if got := callbackSecret(t, fx, private, "titogo"); got == old || got != want { |
| 118 | t.Fatal("grant stamped the new master while keeping the old delivered secret") |
| 119 | } |
| 120 | } |
| 121 | |