Skip to content

Commit c33965e

Browse files
authored
feat: remove duplicate queries during settings flow and use better index hint for credentials lookup (#4193)
This patch reduces duplicate GetIdentity queries as part of submitting the settings flow, and improves an index to significantly reduce credential lookup. For better debugging, more tracing ha been added to the settings module.
1 parent 2fcc786 commit c33965e

38 files changed

+334
-259
lines changed

cmd/clidoc/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ func validateAllMessages(path string) error {
309309
info := &types.Info{
310310
Defs: make(map[*ast.Ident]types.Object),
311311
}
312+
313+
//nolint:staticcheck
312314
var pack *ast.Package
313315
for _, p := range packs {
314316
if p.Name == "text" {

cmd/hashers/argon2/calibrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ Please note that the values depend on the machine you run the hashing on. If you
246246
case res.MaxMem > conf.localConfig.DedicatedMemory:
247247
_, _ = progressPrinter.Printf("The required memory was %s more than the maximum allowed of %s.\n", res.MaxMem-maxMemory, conf.localConfig.DedicatedMemory)
248248

249+
//nolint:gosec // disable G115
249250
conf.localConfig.Memory -= (res.MaxMem - conf.localConfig.DedicatedMemory) / bytesize.ByteSize(reqPerMin)
250251
_, _ = progressPrinter.Printf("Decreasing memory to %s\n", conf.localConfig.Memory)
251252
// too slow

courier/courier_dispatcher.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (c *courier) DispatchQueue(ctx context.Context) error {
7979
maxRetries := c.deps.CourierConfig().CourierMessageRetries(ctx)
8080
pullCount := c.deps.CourierConfig().CourierWorkerPullCount(ctx)
8181

82+
//nolint:gosec // disable G115
8283
messages, err := c.deps.CourierPersister().NextMessages(ctx, uint8(pullCount))
8384
if err != nil {
8485
if errors.Is(err, ErrQueueEmpty) {

driver/config/config.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,14 @@ func (p *Config) HasherArgon2(ctx context.Context) *Argon2 {
550550
// warn about usage of default values and point to the docs
551551
// warning will require https://github.com/ory/viper/issues/19
552552
return &Argon2{
553-
Memory: p.GetProvider(ctx).ByteSizeF(ViperKeyHasherArgon2ConfigMemory, Argon2DefaultMemory),
554-
Iterations: uint32(p.GetProvider(ctx).IntF(ViperKeyHasherArgon2ConfigIterations, int(Argon2DefaultIterations))),
555-
Parallelism: uint8(p.GetProvider(ctx).IntF(ViperKeyHasherArgon2ConfigParallelism, int(Argon2DefaultParallelism))),
556-
SaltLength: uint32(p.GetProvider(ctx).IntF(ViperKeyHasherArgon2ConfigSaltLength, int(Argon2DefaultSaltLength))),
553+
Memory: p.GetProvider(ctx).ByteSizeF(ViperKeyHasherArgon2ConfigMemory, Argon2DefaultMemory),
554+
//nolint:gosec // disable G115
555+
Iterations: uint32(p.GetProvider(ctx).IntF(ViperKeyHasherArgon2ConfigIterations, int(Argon2DefaultIterations))),
556+
//nolint:gosec // disable G115
557+
Parallelism: uint8(p.GetProvider(ctx).IntF(ViperKeyHasherArgon2ConfigParallelism, int(Argon2DefaultParallelism))),
558+
//nolint:gosec // disable G115
559+
SaltLength: uint32(p.GetProvider(ctx).IntF(ViperKeyHasherArgon2ConfigSaltLength, int(Argon2DefaultSaltLength))),
560+
//nolint:gosec // disable G115
557561
KeyLength: uint32(p.GetProvider(ctx).IntF(ViperKeyHasherArgon2ConfigKeyLength, int(Argon2DefaultKeyLength))),
558562
ExpectedDuration: p.GetProvider(ctx).DurationF(ViperKeyHasherArgon2ConfigExpectedDuration, Argon2DefaultDuration),
559563
ExpectedDeviation: p.GetProvider(ctx).DurationF(ViperKeyHasherArgon2ConfigExpectedDeviation, Argon2DefaultDeviation),

hash/hash_comparator.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929

3030
//nolint:staticcheck
3131
//lint:ignore SA1019 compatibility for imported passwords
32-
"golang.org/x/crypto/md4" //#nosec G501 -- compatibility for imported passwords
32+
"golang.org/x/crypto/md4" //nolint:gosec // disable G115 G501 -- compatibility for imported passwords
3333
"golang.org/x/crypto/pbkdf2"
3434
"golang.org/x/crypto/scrypt"
3535

@@ -159,6 +159,7 @@ func CompareArgon2id(_ context.Context, password []byte, hash []byte) error {
159159
}
160160

161161
// Derive the key from the other password using the same parameters.
162+
//nolint:gosec // disable G115
162163
otherHash := argon2.IDKey(password, salt, p.Iterations, uint32(p.Memory), p.Parallelism, p.KeyLength)
163164

164165
return comparePasswordHashConstantTime(hash, otherHash)

hash/hasher_argon2.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func NewHasherArgon2(c Argon2Configuration) *Argon2 {
4141
}
4242

4343
func toKB(mem bytesize.ByteSize) uint32 {
44+
//nolint:gosec // disable G115
4445
return uint32(mem / bytesize.KB)
4546
}
4647

identity/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ func (e *CreateIdentitiesError) Find(ident *Identity) *FailedIdentity {
370370
return nil
371371
}
372372
func (e *CreateIdentitiesError) ErrOrNil() error {
373-
if e.failedIdentities == nil || len(e.failedIdentities) == 0 {
373+
if len(e.failedIdentities) == 0 {
374374
return nil
375375
}
376376
return e

internal/client-go/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
44
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
55
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
66
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
7+
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
78
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
89
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
910
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

persistence/sql/identity/persister_identity.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ import (
1313
"sync"
1414
"time"
1515

16-
"github.com/ory/kratos/x/events"
17-
18-
"github.com/ory/x/crdbx"
19-
2016
"github.com/gobuffalo/pop/v6"
2117
"github.com/gofrs/uuid"
2218
"github.com/pkg/errors"
@@ -33,7 +29,9 @@ import (
3329
"github.com/ory/kratos/persistence/sql/update"
3430
"github.com/ory/kratos/schema"
3531
"github.com/ory/kratos/x"
32+
"github.com/ory/kratos/x/events"
3633
"github.com/ory/x/contextx"
34+
"github.com/ory/x/crdbx"
3735
"github.com/ory/x/errorsx"
3836
"github.com/ory/x/otelx"
3937
"github.com/ory/x/pagination/keysetpagination"
@@ -806,11 +804,11 @@ func identifiersTableNameWithIndexHint(con *pop.Connection) string {
806804
ici := "identity_credential_identifiers"
807805
switch con.Dialect.Name() {
808806
case "cockroach":
809-
ici += "@identity_credential_identifiers_nid_i_ici_idx"
807+
ici += "@identity_credential_identifiers_ici_nid_i_idx"
810808
case "sqlite3":
811-
ici += " INDEXED BY identity_credential_identifiers_nid_i_ici_idx"
809+
ici += " INDEXED BY identity_credential_identifiers_ici_nid_i_idx"
812810
case "mysql":
813-
ici += " USE INDEX(identity_credential_identifiers_nid_i_ici_idx)"
811+
ici += " USE INDEX(identity_credential_identifiers_ici_nid_i_idx)"
814812
default:
815813
// good luck 🤷‍♂️
816814
}
@@ -932,7 +930,7 @@ func (p *IdentityPersister) ListIdentities(ctx context.Context, params identity.
932930
)
933931
}
934932

935-
if params.IdsFilter != nil && len(params.IdsFilter) != 0 {
933+
if len(params.IdsFilter) > 0 {
936934
wheres += `
937935
AND identities.id in (?)
938936
`
@@ -987,15 +985,15 @@ func (p *IdentityPersister) ListIdentities(ctx context.Context, params identity.
987985
}
988986
case identity.ExpandFieldVerifiableAddresses:
989987
addrs := make([]identity.VerifiableAddress, 0)
990-
if err := con.Where("nid = ?", nid).Where("identity_id IN (?)", identityIDs).Order("id").All(&addrs); err != nil {
988+
if err := con.Where("identity_id IN (?)", identityIDs).Where("nid = ?", nid).Order("id").All(&addrs); err != nil {
991989
return sqlcon.HandleError(err)
992990
}
993991
for _, addr := range addrs {
994992
identitiesByID[addr.IdentityID].VerifiableAddresses = append(identitiesByID[addr.IdentityID].VerifiableAddresses, addr)
995993
}
996994
case identity.ExpandFieldRecoveryAddresses:
997995
addrs := make([]identity.RecoveryAddress, 0)
998-
if err := con.Where("nid = ?", nid).Where("identity_id IN (?)", identityIDs).Order("id").All(&addrs); err != nil {
996+
if err := con.Where("identity_id IN (?)", identityIDs).Where("nid = ?", nid).Order("id").All(&addrs); err != nil {
999997
return sqlcon.HandleError(err)
1000998
}
1001999
for _, addr := range addrs {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP INDEX IF EXISTS identity_credential_identifiers_nid_ici_i_idx;

0 commit comments

Comments
 (0)