Skip to content

Commit

Permalink
fix: do not read outdated trust relations
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Jan 5, 2024
1 parent 8e94929 commit 8b0795d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
5 changes: 4 additions & 1 deletion persistence/sql/persister_grant_jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ func (p *Persister) GetPublicKeys(ctx context.Context, issuer string, subject st

grantsData := make([]trust.SQLData, 0)
query := p.QueryWithNetwork(ctx).
Select("key_set", "key_id").
Where("expires_at > NOW()").
Where("issuer = ?", issuer).
Where("(subject = ? OR allow_any_subject IS TRUE)", subject).
Where("nid = ?", p.NetworkID(ctx))
Order("created_at DESC").
Limit(100) // Load maximum of 100 keys

if err := query.All(&grantsData); err != nil {
return nil, sqlcon.HandleError(err)
Expand Down
45 changes: 31 additions & 14 deletions persistence/sql/persister_nid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"database/sql"
"encoding/json"
"github.com/ory/x/sqlcon"
"testing"
"time"

Expand Down Expand Up @@ -1341,22 +1342,38 @@ func (s *PersisterTestSuite) TestGetPublicKeys() {
t := s.T()
for k, r := range s.registries {
t.Run(k, func(t *testing.T) {
ks := newKeySet("ks-id", "use")
grant := trust.Grant{
ID: uuid.Must(uuid.NewV4()).String(),
ExpiresAt: time.Now().Add(time.Hour),
PublicKey: trust.PublicKey{Set: "ks-id", KeyID: ks.Keys[0].KeyID},
}
require.NoError(t, r.Persister().AddKeySet(s.t1, "ks-id", ks))
require.NoError(t, r.Persister().CreateGrant(s.t1, grant, ks.Keys[0]))
t.Run("get key", func(t *testing.T) {
ks := newKeySet("ks-id", "use")
grant := trust.Grant{
ID: uuid.Must(uuid.NewV4()).String(),
ExpiresAt: time.Now().Add(time.Hour),
PublicKey: trust.PublicKey{Set: "ks-id", KeyID: ks.Keys[0].KeyID},
}
require.NoError(t, r.Persister().AddKeySet(s.t1, "ks-id", ks))
require.NoError(t, r.Persister().CreateGrant(s.t1, grant, ks.Keys[0]))

actual, err := r.Persister().GetPublicKeys(s.t2, grant.Issuer, grant.Subject)
require.NoError(t, err)
require.Nil(t, actual.Keys)
actual, err := r.Persister().GetPublicKeys(s.t2, grant.Issuer, grant.Subject)
require.NoError(t, err)
require.Nil(t, actual.Keys)

actual, err = r.Persister().GetPublicKeys(s.t1, grant.Issuer, grant.Subject)
require.NoError(t, err)
require.NotNil(t, actual.Keys)
actual, err = r.Persister().GetPublicKeys(s.t1, grant.Issuer, grant.Subject)
require.NoError(t, err)
require.NotNil(t, actual.Keys)
})

t.Run("get expired key fails", func(t *testing.T) {
ks := newKeySet("ks-id", "use")
grant := trust.Grant{
ID: uuid.Must(uuid.NewV4()).String(),
ExpiresAt: time.Now().Add(-time.Hour),
PublicKey: trust.PublicKey{Set: "ks-id", KeyID: ks.Keys[0].KeyID},
}
require.NoError(t, r.Persister().AddKeySet(s.t1, "ks-id", ks))
require.NoError(t, r.Persister().CreateGrant(s.t1, grant, ks.Keys[0]))

_, err := r.Persister().GetPublicKeys(s.t2, grant.Issuer, grant.Subject)
require.ErrorIs(t, err, sqlcon.ErrNoRows)
})
})
}
}
Expand Down

0 comments on commit 8b0795d

Please sign in to comment.