Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions sqlstore/backchannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
34 changes: 34 additions & 0 deletions sqlstore/backchannel_undecided_outage_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
8 changes: 8 additions & 0 deletions sqlstore/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading