Skip to content

Commit

Permalink
feat: change primary keys in tables for identity-associated data
Browse files Browse the repository at this point in the history
# Conflicts:
#	Makefile
#	driver/registry.go
#	go.mod
#	go.sum
#	identity/credentials.go
#	session/session.go
  • Loading branch information
alnr committed Sep 25, 2024
1 parent 358521a commit c46c90f
Show file tree
Hide file tree
Showing 43 changed files with 909 additions and 69 deletions.
59 changes: 30 additions & 29 deletions driver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,38 @@ import (
"context"
"io/fs"

"github.com/ory/kratos/selfservice/sessiontokenexchange"
"github.com/ory/x/contextx"
"github.com/ory/x/jsonnetsecure"
"github.com/ory/x/otelx"
prometheus "github.com/ory/x/prometheusx"

"github.com/gorilla/sessions"
"github.com/pkg/errors"

"github.com/ory/nosurf"

"github.com/ory/x/logrusx"

"github.com/ory/kratos/continuity"
"github.com/ory/kratos/courier"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/hash"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/persistence"
"github.com/ory/kratos/schema"
"github.com/ory/kratos/selfservice/errorx"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/flow/logout"
"github.com/ory/kratos/selfservice/flow/recovery"
"github.com/ory/kratos/selfservice/flow/registration"
"github.com/ory/kratos/selfservice/flow/settings"
"github.com/ory/kratos/selfservice/flow/verification"
"github.com/ory/kratos/selfservice/sessiontokenexchange"
"github.com/ory/kratos/selfservice/strategy/code"
"github.com/ory/kratos/selfservice/strategy/link"

"github.com/ory/x/healthx"

"github.com/ory/kratos/persistence"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/flow/logout"
"github.com/ory/kratos/selfservice/flow/registration"

"github.com/ory/kratos/x"

"github.com/ory/x/dbal"

"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/selfservice/errorx"
password2 "github.com/ory/kratos/selfservice/strategy/password"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
"github.com/ory/nosurf"
"github.com/ory/x/contextx"
"github.com/ory/x/dbal"
"github.com/ory/x/healthx"
"github.com/ory/x/jsonnetsecure"
"github.com/ory/x/logrusx"
"github.com/ory/x/otelx"
"github.com/ory/x/popx"
prometheus "github.com/ory/x/prometheusx"
)

type Registry interface {
Expand Down Expand Up @@ -186,10 +179,12 @@ type options struct {
replaceIdentitySchemaProvider func(Registry) schema.IdentitySchemaProvider
inspect func(Registry) error
extraMigrations []fs.FS
replacementStrategies []NewStrategy
extraHooks map[string]func(config.SelfServiceHook) any
disableMigrationLogging bool
jsonnetPool jsonnetsecure.Pool
extraGoMigrations popx.Migrations

replacementStrategies []NewStrategy
extraHooks map[string]func(config.SelfServiceHook) any
disableMigrationLogging bool
jsonnetPool jsonnetsecure.Pool
}

type RegistryOption func(*options)
Expand Down Expand Up @@ -251,6 +246,12 @@ func WithExtraMigrations(m ...fs.FS) RegistryOption {
}
}

func WithExtraGoMigrations(m ...popx.Migration) RegistryOption {
return func(o *options) {
o.extraGoMigrations = append(o.extraGoMigrations, m...)
}
}

func WithDisabledMigrationLogging() RegistryOption {
return func(o *options) {
o.disableMigrationLogging = true
Expand Down
5 changes: 4 additions & 1 deletion driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,10 @@ func (m *RegistryDefault) Init(ctx context.Context, ctxer contextx.Contextualize
m.Logger().WithError(err).Warnf("Unable to open database, retrying.")
return errors.WithStack(err)
}
p, err := sql.NewPersister(ctx, m, c, sql.WithExtraMigrations(o.extraMigrations...), sql.WithDisabledLogging(o.disableMigrationLogging))
p, err := sql.NewPersister(ctx, m, c,
sql.WithExtraMigrations(o.extraMigrations...),
sql.WithExtraGoMigrations(o.extraGoMigrations...),
sql.WithDisabledLogging(o.disableMigrationLogging))
if err != nil {
m.Logger().WithError(err).Warnf("Unable to initialize persister, retrying.")
return err
Expand Down
48 changes: 48 additions & 0 deletions identity/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import (
"context"
"database/sql"
"reflect"
"sync"
"time"

"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"github.com/wI2L/jsondiff"
"go.opentelemetry.io/otel/trace"

"github.com/ory/kratos/ui/node"
"github.com/ory/x/otelx"
"github.com/ory/x/sqlxx"
)

Expand Down Expand Up @@ -191,11 +196,54 @@ func (c Credentials) GetID() uuid.UUID {
return c.ID
}

func (c Credentials) GetNID() uuid.UUID {
return c.NID
}

var (
typeTable map[uuid.UUID]CredentialsType
typeErr error
typeOnce sync.Once
)

func (c *Credentials) AfterFind(con *pop.Connection) error {
typeOnce.Do(func() {
span := trace.SpanFromContext(con.Context())
ctx, span := span.TracerProvider().Tracer("").Start(con.Context(), "identity.Credentials.AfterFind")
con = con.WithContext(ctx)
defer otelx.End(span, &typeErr)

var table []CredentialsTypeTable
if typeErr = con.All(&table); typeErr != nil {
return
}
typeTable = make(map[uuid.UUID]CredentialsType, len(table))
for _, t := range table {
typeTable[t.ID] = t.Name
}
})
if typeErr != nil {
return typeErr
}

var ok bool
c.Type, ok = typeTable[c.IdentityCredentialTypeID]
if !ok {
return errors.New("could not find credentials type")
}

return nil
}

var _ pop.AfterFindable = (*Credentials)(nil)

type (
// swagger:ignore
CredentialIdentifier struct {
ID uuid.UUID `db:"id"`
Identifier string `db:"identifier"`
// Identity is a helper struct field for gobuffalo.pop.
IdentityID uuid.UUID `json:"-" db:"identity_id"`
// IdentityCredentialsID is a helper struct field for gobuffalo.pop.
IdentityCredentialsID uuid.UUID `json:"-" db:"identity_credential_id"`
// IdentityCredentialsTypeID is a helper struct field for gobuffalo.pop.
Expand Down
4 changes: 2 additions & 2 deletions identity/test/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -1347,8 +1347,8 @@ func TestPool(ctx context.Context, p persistence.Persister, m *identity.Manager,
require.NoError(t, p.GetConnection(ctx).RawQuery("INSERT INTO identity_credentials (id, identity_id, nid, identity_credential_type_id, created_at, updated_at, config) VALUES (?, ?, ?, ?, ?, ?, '{}')", cid2, iid, nid2, m[0].ID, time.Now(), time.Now()).Exec())

ici1, ici2 := x.NewUUID(), x.NewUUID()
require.NoError(t, p.GetConnection(ctx).RawQuery("INSERT INTO identity_credential_identifiers (id, identity_credential_id, nid, identifier, created_at, updated_at, identity_credential_type_id) VALUES (?, ?, ?, ?, ?, ?, ?)", ici1, cid1, nid1, "nid1", time.Now(), time.Now(), m[0].ID).Exec())
require.NoError(t, p.GetConnection(ctx).RawQuery("INSERT INTO identity_credential_identifiers (id, identity_credential_id, nid, identifier, created_at, updated_at, identity_credential_type_id) VALUES (?, ?, ?, ?, ?, ?, ?)", ici2, cid2, nid2, "nid2", time.Now(), time.Now(), m[0].ID).Exec())
require.NoError(t, p.GetConnection(ctx).RawQuery("INSERT INTO identity_credential_identifiers (id, identity_id, identity_credential_id, nid, identifier, created_at, updated_at, identity_credential_type_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", ici1, iid, cid1, nid1, "nid1", time.Now(), time.Now(), m[0].ID).Exec())
require.NoError(t, p.GetConnection(ctx).RawQuery("INSERT INTO identity_credential_identifiers (id, identity_id, identity_credential_id, nid, identifier, created_at, updated_at, identity_credential_type_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)", ici2, iid, cid2, nid2, "nid2", time.Now(), time.Now(), m[0].ID).Exec())

_, err := p.GetIdentity(ctx, nid1, identity.ExpandNothing)
require.ErrorIs(t, err, sqlcon.ErrNoRows)
Expand Down
7 changes: 5 additions & 2 deletions persistence/sql/identity/persister_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ func (p *IdentityPersister) createIdentityCredentials(ctx context.Context, conn

identifiers = append(identifiers, &identity.CredentialIdentifier{
Identifier: identifier,
IdentityID: cred.IdentityID,
IdentityCredentialsID: cred.ID,
IdentityCredentialsTypeID: ct.ID,
NID: p.NetworkID(ctx),
Expand Down Expand Up @@ -787,7 +788,7 @@ func QueryForCredentials(con *pop.Connection, where ...Where) (map[uuid.UUID](ma
ici := "identity_credential_identifiers"
switch con.Dialect.Name() {
case "cockroach":
ici += "@identity_credential_identifiers_nid_identity_credential_id_idx"
ici += "@primary"
case "sqlite3":
ici += " INDEXED BY identity_credential_identifiers_nid_identity_credential_id_idx"
case "mysql":
Expand All @@ -811,7 +812,9 @@ func QueryForCredentials(con *pop.Connection, where ...Where) (map[uuid.UUID](ma
"(identity_credentials.identity_credential_type_id = ict.id)",
).LeftJoin(
ici,
"identity_credential_identifiers.identity_credential_id = identity_credentials.id AND identity_credential_identifiers.nid = identity_credentials.nid",
`identity_credential_identifiers.identity_id = identity_credentials.identity_id
AND identity_credential_identifiers.identity_credential_id = identity_credentials.id
AND identity_credential_identifiers.nid = identity_credentials.nid`,
)
for _, w := range where {
q = q.Where("("+w.Condition+")", w.Args...)
Expand Down
26 changes: 13 additions & 13 deletions persistence/sql/migratest/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,21 @@ import (
"encoding/json"
"os"
"path/filepath"
"slices"
"sync"
"testing"
"time"

"github.com/ory/x/pagination/keysetpagination"
"github.com/ory/x/servicelocatorx"

"github.com/ory/kratos/identity"

"github.com/bradleyjkemp/cupaloy/v2"
"github.com/stretchr/testify/assert"

"github.com/ory/x/dbal"

"github.com/ory/kratos/x/xsql"

"github.com/ory/x/migratest"

"github.com/gobuffalo/pop/v6"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/kratos/driver"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/identity"
"github.com/ory/kratos/persistence/sql/migrations/gomigrations"
"github.com/ory/kratos/selfservice/flow/login"
"github.com/ory/kratos/selfservice/flow/recovery"
"github.com/ory/kratos/selfservice/flow/registration"
Expand All @@ -41,9 +32,14 @@ import (
"github.com/ory/kratos/selfservice/strategy/link"
"github.com/ory/kratos/session"
"github.com/ory/kratos/x"
"github.com/ory/kratos/x/xsql"
"github.com/ory/x/configx"
"github.com/ory/x/dbal"
"github.com/ory/x/logrusx"
"github.com/ory/x/migratest"
"github.com/ory/x/pagination/keysetpagination"
"github.com/ory/x/popx"
"github.com/ory/x/servicelocatorx"
"github.com/ory/x/sqlcon"
"github.com/ory/x/sqlcon/dockertest"
)
Expand Down Expand Up @@ -134,6 +130,10 @@ func testDatabase(t *testing.T, db string, c *pop.Connection) {
os.DirFS("../migrations/sql"),
popx.NewMigrator(c, l, nil, 1*time.Minute),
popx.WithTestdata(t, os.DirFS("./testdata")),
popx.WithGoMigrations(slices.Concat(
gomigrations.IdentityPrimaryKeysStep1,
gomigrations.IdentityPrimaryKeysStep2,
)),
)
require.NoError(t, err)
tm.DumpMigrations = true
Expand Down
Loading

0 comments on commit c46c90f

Please sign in to comment.