diff --git a/sqlstore/backchannel.go b/sqlstore/backchannel.go index 069b37ce..1ae85c4e 100644 --- a/sqlstore/backchannel.go +++ b/sqlstore/backchannel.go @@ -76,6 +76,14 @@ var ( // errors. const opCreateBackchannel = "create backchannel authentication" +// opLookUpBackchannel and opDecideBackchannel name +// LookupBackchannelAuthentication and DecideBackchannelAuthentication +// in their errors. +const ( + opLookUpBackchannel = "look up backchannel authentication" + opDecideBackchannel = "decide backchannel authentication" +) + // CreateBackchannelAuthentication implements // storage.BackchannelAuthenticationStore, storing the request pending, // for Sweep to delete BackchannelExpiredRetention after it expires. @@ -131,9 +139,9 @@ func (s *BackchannelAuthenticationStore) LookupBackchannelAuthentication(ctx con handleHash[:]).Scan(&key, &clientID, &stored, &bound.deliveryMode, &bound.pollInterval, &bound.expires) switch { case errors.Is(err, sql.ErrNoRows): - return storage.LookedUpBackchannelAuthentication{}, unknownBackchannelHandle("look up backchannel authentication") + return storage.LookedUpBackchannelAuthentication{}, unknownBackchannelHandle(opLookUpBackchannel) case err != nil: - return storage.LookedUpBackchannelAuthentication{}, unavailable("look up backchannel authentication", err) + return storage.LookedUpBackchannelAuthentication{}, unavailable(opLookUpBackchannel, err) } record, err := s.openRecord(bound.ref(recordColumn, string(key), clientID), stored) if err != nil { @@ -189,7 +197,7 @@ func (s *BackchannelAuthenticationStore) DecideBackchannelAuthentication(ctx con case errors.Is(err, sql.ErrNoRows): return storage.DecidedBackchannelAuthentication{}, s.whyUndecided(ctx, decision.HandleHash) case err != nil: - return storage.DecidedBackchannelAuthentication{}, unavailable("decide backchannel authentication", err) + return storage.DecidedBackchannelAuthentication{}, unavailable(opDecideBackchannel, err) } return out, nil } @@ -207,10 +215,10 @@ func unknownBackchannelHandle(op string) error { func (s *BackchannelAuthenticationStore) whyUndecided(ctx context.Context, handleHash [32]byte) error { var n int if err := s.d.queryRow(ctx, "SELECT COUNT(*) FROM {backchannel_authentications} WHERE handle_hash = ?", handleHash[:]).Scan(&n); err != nil { - return unavailable("decide backchannel authentication", err) + return unavailable(opDecideBackchannel, err) } if n == 0 { - return unknownBackchannelHandle("decide backchannel authentication") + return unknownBackchannelHandle(opDecideBackchannel) } return errBackchannelDecided } diff --git a/sqlstore/backchannel_undecided_outage_test.go b/sqlstore/backchannel_undecided_outage_test.go new file mode 100644 index 00000000..f5c22de7 --- /dev/null +++ b/sqlstore/backchannel_undecided_outage_test.go @@ -0,0 +1,34 @@ +package sqlstore_test + +import ( + "context" + "crypto/sha256" + "errors" + "strings" + "testing" + + "github.com/idfoundry/fapigo/sqlstore" + "github.com/idfoundry/fapigo/storage" +) + +// TestDecideBackchannelReportsAnOutageWhileExplainingARefusal: when a +// decision's update decides nothing, the store asks whether the handle +// is unknown or already decided; if the database can't answer that, +// the decision fails as an outage, not as an unknown handle. +func TestDecideBackchannelReportsAnOutageWhileExplainingARefusal(t *testing.T) { + p := newPlace(t, sqlstore.SQLite) + p.migrated() + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + hook := func(query string, after bool) { + if !after && strings.Contains(query, "SELECT COUNT(*)") && strings.Contains(query, "backchannel_authentications") { + cancel() + } + } + b := p.handle(hookedDB(t, p, "", hook)).Backchannel() + decision := storage.DecideBackchannelAuthentication{HandleHash: sha256.Sum256([]byte("handle-never-stored")), Status: storage.BackchannelAuthenticationDenied} + _, err := b.DecideBackchannelAuthentication(ctx, decision) + if !errors.Is(err, storage.ErrStoreUnavailable) || errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) { + t.Fatalf("DecideBackchannelAuthentication with the follow-up query failing = %v, want ErrStoreUnavailable, not not-found", err) + } +} diff --git a/sqlstore/db_test.go b/sqlstore/db_test.go index 9275bb3c..fcc8eec5 100644 --- a/sqlstore/db_test.go +++ b/sqlstore/db_test.go @@ -553,6 +553,14 @@ func TestClosedDatabase(t *testing.T) { if _, err := d.Sweep(ctx, time.Now()); !errors.Is(err, storage.ErrStoreUnavailable) { t.Errorf("Sweep on a closed database = %v, want ErrStoreUnavailable", err) } + handle := sha256.Sum256([]byte("handle-on-a-closed-database")) + if _, err := d.Backchannel().LookupBackchannelAuthentication(ctx, handle); !errors.Is(err, storage.ErrStoreUnavailable) || errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) { + t.Errorf("LookupBackchannelAuthentication on a closed database = %v, want ErrStoreUnavailable, not not-found", err) + } + decision := storage.DecideBackchannelAuthentication{HandleHash: handle, Status: storage.BackchannelAuthenticationDenied} + if _, err := d.Backchannel().DecideBackchannelAuthentication(ctx, decision); !errors.Is(err, storage.ErrStoreUnavailable) || errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) { + t.Errorf("DecideBackchannelAuthentication on a closed database = %v, want ErrStoreUnavailable, not not-found", err) + } } // getOrCreateErr is GetOrCreate's error.