package console import ( "context" "errors" "net/http" "sync/atomic" "testing" "github.com/carlosframework/platform/internal/appconfig" "github.com/carlosframework/platform/internal/objstore" "github.com/carlosframework/platform/internal/release" ) type callbackConfigRaceStore struct { release.Store key string before func() loseReply bool raced atomic.Bool armed atomic.Bool replyLost atomic.Bool } var errCallbackReplyLost = errors.New("callback config response lost") func (s *callbackConfigRaceStore) Get(ctx context.Context, key string) ([]byte, string, error) { if key == s.key && s.raced.CompareAndSwap(false, true) { s.before() s.armed.Store(true) } return s.Store.Get(ctx, key) } func (s *callbackConfigRaceStore) Put(ctx context.Context, key string, body []byte, etag string) (string, error) { got, err := s.Store.Put(ctx, key, body, etag) if key == s.key && err == nil && s.loseReply && s.armed.Load() && s.replyLost.CompareAndSwap(false, true) { return "", errCallbackReplyLost } return got, err } func TestCallbackKeyConvergesAcrossConcurrentRotationsWithoutClaims(t *testing.T) { for _, loseReply := range []bool{false, true} { name := "completed writes" if loseReply { name = "interrupted delivery resumes despite current key ID" } t.Run(name, func(t *testing.T) { fx, token, private := rotationRaceFixture(t, "key-sync-race") if fx.srv.cfg.HostClaim { t.Fatal("this regression must exercise independent writers without claims") } fx.srv.cfg.CallbackSigner = callbackTestSigner(t, "new-sync-master", testWebhookMaster) store := &callbackConfigRaceStore{Store: fx.rel, key: appconfig.Key(fx.sqid, "titogo"), loseReply: loseReply} store.before = func() { status, body := postJSON(t, scanURL(fx, "titogo")+"/callback-key/rotate", map[string]any{}, token) if status != http.StatusOK { t.Fatalf("concurrent app rotation = %d %v", status, body) } } fx.srv.cfg.Releases = store pub, _ := appconfig.ParsePublicKey(fx.srv.cfg.SecretsPub, "test") remaining, _, err := fx.srv.rotateWebhookBudget(context.Background(), pub) if loseReply { if !errors.Is(err, errCallbackReplyLost) || !store.replyLost.Load() { t.Fatalf("interrupted delivery = %v, want lost-response error", err) } remaining, _, err = fx.srv.rotateWebhookBudget(context.Background(), pub) } if err != nil || remaining != 0 || !store.raced.Load() { t.Fatalf("rotation remaining=%d err=%v raced=%v", remaining, err, store.raced.Load()) } rec := storedRecord(t, fx, "titogo") want := string(fx.srv.cfg.CallbackSigner.DerivedKey(fx.sqid, "titogo", rec.Scanning.WebhookGen)) if got := callbackSecret(t, fx, private, "titogo"); got != want { t.Fatal("completed rotation left a secret for a different generation") } }) } } func TestCallbackKeyConvergenceHonorsRevokeDuringDelivery(t *testing.T) { fx, token, _ := rotationRaceFixture(t, "key-sync-revoke") fx.srv.cfg.CallbackSigner = callbackTestSigner(t, "new-sync-master", testWebhookMaster) store := &callbackConfigRaceStore{Store: fx.rel, key: appconfig.Key(fx.sqid, "titogo")} store.before = func() { status, body := postJSON(t, scanURL(fx, "titogo")+"/revoke", map[string]any{}, token) if status != http.StatusOK { t.Fatalf("concurrent revoke = %d %v", status, body) } } fx.srv.cfg.Releases = store pub, _ := appconfig.ParsePublicKey(fx.srv.cfg.SecretsPub, "test") remaining, changed, err := fx.srv.rotateWebhookBudget(context.Background(), pub) if err != nil || remaining != 0 || changed != 0 || !store.raced.Load() { t.Fatalf("rotation after revoke remaining=%d changed=%d err=%v", remaining, changed, err) } if storedRecord(t, fx, "titogo").Scanning.Enabled != nil { t.Fatal("rotation recreated scanning consent") } if _, ok := appVars(t, fx, "titogo")[objstore.DefaultEnvPrefix+"_SCAN_CALLBACK_SECRET"]; ok { t.Fatal("rotation left a callback secret after revoke") } } func TestScanGrantReplacesAnExistingCallbackKeyAfterMasterRotation(t *testing.T) { fx, token, private := rotationRaceFixture(t, "key-sync-grant") old := callbackSecret(t, fx, private, "titogo") fx.srv.cfg.CallbackSigner = callbackTestSigner(t, "new-sync-master", testWebhookMaster) status, body := postJSON(t, scanURL(fx, "titogo")+"/grant", map[string]any{}, token) if status != http.StatusOK { t.Fatalf("repeat grant = %d %v", status, body) } rec := storedRecord(t, fx, "titogo") want := string(fx.srv.cfg.CallbackSigner.DerivedKey(fx.sqid, "titogo", rec.Scanning.WebhookGen)) if got := callbackSecret(t, fx, private, "titogo"); got == old || got != want { t.Fatal("grant stamped the new master while keeping the old delivered secret") } }