Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor identity persister #3101

Merged
merged 24 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
802ae87
chore: factor out identity persister
alnr Feb 9, 2023
19589c2
fix: add limit clause in FindByCredentialsIdentifier
alnr Feb 9, 2023
f251cba
feat: refactor
alnr Feb 12, 2023
68c2c66
fix: nid injection
alnr Feb 14, 2023
dc60adb
feat: factor out device persister
alnr Feb 14, 2023
8028d94
fix: WithNetworkID
alnr Feb 15, 2023
c0b38a1
fix: don't test internals
alnr Feb 22, 2023
9f2361f
fix: verbose logging
alnr Feb 22, 2023
a9e8e79
feat: expose MigrationBox and pass through driver options to migrate …
alnr Feb 22, 2023
a0fb6bd
fix: use laher/mergefs instead of home-grown filesystem merging
alnr Feb 22, 2023
6360905
chore: bump laher/mergefs
alnr Feb 23, 2023
5b06143
feat: allow server termination through context cancelation
alnr Mar 1, 2023
0f187f6
chore: bump dependencies
alnr Mar 3, 2023
b015153
fix: consistently use otelx.End in IdentityPersister and inline priva…
alnr Mar 3, 2023
58d6461
Inline private (*Persister).delete function into persister_session.go
alnr Mar 3, 2023
476f5d9
chore: update #nosec lint directives
alnr Mar 3, 2023
bf12ceb
fix: lint
alnr Mar 3, 2023
cb16649
feat: factor out (*Persister).update()
alnr Mar 3, 2023
58c82c7
fix: hydrate session identity
alnr Mar 3, 2023
f85a033
fix: InjectTraitsSchemaURL description
alnr Mar 3, 2023
076fe90
fix: expose (*Identity).Validate()
alnr Mar 3, 2023
864c868
fix: delete (*IdentityPersister).update()
alnr Mar 3, 2023
d5ead1c
fix: hydrate session identity
alnr Mar 3, 2023
b8a7b72
Revert "chore: bump dependencies"
alnr Mar 3, 2023
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
4 changes: 2 additions & 2 deletions cmd/cliclient/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewMigrateHandler() *MigrateHandler {
return &MigrateHandler{}
}

func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) error {
func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string, opts ...driver.RegistryOption) error {
var d driver.Registry
var err error

Expand Down Expand Up @@ -78,7 +78,7 @@ func (h *MigrateHandler) MigrateSQL(cmd *cobra.Command, args []string) error {
}
}

err = d.Init(cmd.Context(), &contextx.Default{}, driver.SkipNetworkInit)
err = d.Init(cmd.Context(), &contextx.Default{}, append(opts, driver.SkipNetworkInit)...)
if err != nil {
return errors.Wrap(err, "an error occurred initializing migrations")
}
Expand Down
34 changes: 9 additions & 25 deletions cmd/courier/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package courier

import (
cx "context"
"context"
"net/http"

"github.com/spf13/cobra"
Expand All @@ -22,7 +22,7 @@ import (
)

func NewWatchCmd(slOpts []servicelocatorx.Option, dOpts []driver.RegistryOption) *cobra.Command {
var c = &cobra.Command{
c := &cobra.Command{
Use: "watch",
Short: "Starts the Ory Kratos message courier",
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -38,7 +38,7 @@ func NewWatchCmd(slOpts []servicelocatorx.Option, dOpts []driver.RegistryOption)
return c
}

func StartCourier(ctx cx.Context, r driver.Registry) error {
func StartCourier(ctx context.Context, r driver.Registry) error {
eg, ctx := errgroup.WithContext(ctx)

if r.Config().CourierExposeMetricsPort(ctx) != 0 {
Expand All @@ -54,7 +54,7 @@ func StartCourier(ctx cx.Context, r driver.Registry) error {
return eg.Wait()
}

func ServeMetrics(ctx cx.Context, r driver.Registry) error {
func ServeMetrics(ctx context.Context, r driver.Registry) error {
c := r.Config()
l := r.Logger()
n := negroni.New()
Expand All @@ -80,32 +80,16 @@ func ServeMetrics(ctx cx.Context, r driver.Registry) error {
})

l.Printf("Starting the metrics httpd on: %s", server.Addr)
if err := graceful.Graceful(func() error {
errChan := make(chan error, 1)
go func(errChan chan error) {
if err := server.ListenAndServe(); err != nil {
errChan <- err
}
}(errChan)
select {
case err := <-errChan:
l.Errorf("Failed to start the metrics httpd: %s\n", err)
return err
case <-ctx.Done():
l.Printf("Shutting down the metrics httpd: context closed: %s\n", ctx.Err())
return server.Shutdown(ctx)
}
}, server.Shutdown); err != nil {
if err := graceful.GracefulContext(ctx, server.ListenAndServe, server.Shutdown); err != nil {
l.Errorln("Failed to gracefully shutdown metrics httpd")
return err
} else {
l.Println("Metrics httpd was shutdown gracefully")
}
l.Println("Metrics httpd was shutdown gracefully")
return nil
}

func Watch(ctx cx.Context, r driver.Registry) error {
ctx, cancel := cx.WithCancel(ctx)
func Watch(ctx context.Context, r driver.Registry) error {
ctx, cancel := context.WithCancel(ctx)

r.Logger().Println("Courier worker started.")
if err := graceful.Graceful(func() error {
Expand All @@ -115,7 +99,7 @@ func Watch(ctx cx.Context, r driver.Registry) error {
}

return c.Work(ctx)
}, func(_ cx.Context) error {
}, func(_ context.Context) error {
cancel()
return nil
}); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/daemon/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func ServePublic(r driver.Registry, cmd *cobra.Command, args []string, slOpts *s
addr := c.PublicListenOn(ctx)

l.Printf("Starting the public httpd on: %s", addr)
if err := graceful.Graceful(func() error {
if err := graceful.GracefulContext(ctx, func() error {
listener, err := networkx.MakeListener(addr, c.PublicSocketPermission(ctx))
if err != nil {
return err
Expand Down Expand Up @@ -200,7 +200,7 @@ func ServeAdmin(r driver.Registry, cmd *cobra.Command, args []string, slOpts *se
addr := c.AdminListenOn(ctx)

l.Printf("Starting the admin httpd on: %s", addr)
if err := graceful.Graceful(func() error {
if err := graceful.GracefulContext(ctx, func() error {
listener, err := networkx.MakeListener(addr, c.AdminSocketPermission(ctx))
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions cmd/migrate/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"github.com/spf13/cobra"

"github.com/ory/kratos/cmd/cliclient"
"github.com/ory/kratos/driver"
"github.com/ory/x/configx"
)

// migrateSqlCmd represents the sql command
func NewMigrateSQLCmd() *cobra.Command {
func NewMigrateSQLCmd(opts ...driver.RegistryOption) *cobra.Command {
c := &cobra.Command{
Use: "sql <database-url>",
Short: "Create SQL schemas and apply migration plans",
Expand All @@ -29,7 +30,7 @@ You can read in the database URL using the -e flag, for example:
Before running this command on an existing database, create a back up!
`,
RunE: func(cmd *cobra.Command, args []string) error {
return cliclient.NewMigrateHandler().MigrateSQL(cmd, args)
return cliclient.NewMigrateHandler().MigrateSQL(cmd, args, opts...)
},
}

Expand Down
3 changes: 1 addition & 2 deletions driver/registry_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,7 @@ func (m *RegistryDefault) ContinuityCookieManager(ctx context.Context) sessions.

func (m *RegistryDefault) Tracer(ctx context.Context) *otelx.Tracer {
if m.trc == nil {
m.Logger().WithError(errors.WithStack(errors.New(""))).Warn("No tracer setup in RegistryDefault")
return otelx.NewNoop(m.l, m.Config().Tracing(ctx)) // should never happen
return otelx.NewNoop(m.l, m.Config().Tracing(ctx))
}
return m.trc
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ require (
github.com/jteeuwen/go-bindata v3.0.7+incompatible
github.com/julienschmidt/httprouter v1.3.0
github.com/knadh/koanf v1.4.4
github.com/laher/mergefs v0.1.2-0.20230223191438-d16611b2f4e7
github.com/luna-duclos/instrumentedsql v1.1.3
github.com/mattn/goveralls v0.0.7
github.com/mikefarah/yq/v4 v4.19.1
Expand All @@ -70,7 +71,7 @@ require (
github.com/ory/client-go v0.2.0-alpha.60
github.com/ory/dockertest/v3 v3.9.1
github.com/ory/go-acc v0.2.9-0.20230103102148-6b1c9a70dbbe
github.com/ory/graceful v0.1.3
github.com/ory/graceful v0.1.4-0.20230301144740-e222150c51d0
github.com/ory/herodot v0.9.13
github.com/ory/hydra-client-go/v2 v2.0.3
github.com/ory/jsonschema/v3 v3.0.7
Expand All @@ -87,7 +88,7 @@ require (
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/sqs/goreturns v0.0.0-20181028201513-538ac6014518
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.2
github.com/tidwall/gjson v1.14.3
github.com/tidwall/sjson v1.2.5
github.com/urfave/negroni v1.0.0
Expand Down
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/kylelemons/go-gypsy v1.0.0/go.mod h1:chkXM0zjdpXOiqkCW1XcCHDfjfk14PH2KKkQWxfJUcU=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/laher/mergefs v0.1.2-0.20230223191438-d16611b2f4e7 h1:PDeBswTUsSIT4QSrzLvlqKlGrANYa7TrXUwdBN9myU8=
github.com/laher/mergefs v0.1.2-0.20230223191438-d16611b2f4e7/go.mod h1:FSY1hYy94on4Tz60waRMGdO1awwS23BacqJlqf9lJ9Q=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/letsencrypt/pkcs11key/v4 v4.0.0/go.mod h1:EFUvBDay26dErnNb70Nd0/VW3tJiIbETBPTl9ATXQag=
Expand Down Expand Up @@ -945,6 +947,8 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/markbates/pkger v0.17.1 h1:/MKEtWqtc0mZvu9OinB9UzVN9iYCwLWuyUv4Bw+PCno=
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=
github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
Expand Down Expand Up @@ -1093,8 +1097,8 @@ github.com/ory/dockertest/v3 v3.9.1/go.mod h1:42Ir9hmvaAPm0Mgibk6mBPi7SFvTXxEcnz
github.com/ory/go-acc v0.2.6/go.mod h1:4Kb/UnPcT8qRAk3IAxta+hvVapdxTLWtrr7bFLlEgpw=
github.com/ory/go-acc v0.2.9-0.20230103102148-6b1c9a70dbbe h1:rvu4obdvqR0fkSIJ8IfgzKOWwZ5kOT2UNfLq81Qk7rc=
github.com/ory/go-acc v0.2.9-0.20230103102148-6b1c9a70dbbe/go.mod h1:z4n3u6as84LbV4YmgjHhnwtccQqzf4cZlSk9f1FhygI=
github.com/ory/graceful v0.1.3 h1:FaeXcHZh168WzS+bqruqWEw/HgXWLdNv2nJ+fbhxbhc=
github.com/ory/graceful v0.1.3/go.mod h1:4zFz687IAF7oNHHiB586U4iL+/4aV09o/PYLE34t2bA=
github.com/ory/graceful v0.1.4-0.20230301144740-e222150c51d0 h1:VMUeLRfQD14fOMvhpYZIIT4vtAqxYh+f3KnSqCeJ13o=
github.com/ory/graceful v0.1.4-0.20230301144740-e222150c51d0/go.mod h1:hg2iCy+LCWOXahBZ+NQa4dk8J2govyQD79rrqrgMyY8=
github.com/ory/herodot v0.9.13 h1:cN/Z4eOkErl/9W7hDIDLb79IO/bfsH+8yscBjRpB4IU=
github.com/ory/herodot v0.9.13/go.mod h1:IWDs9kSvFQqw/cQ8zi5ksyYvITiUU4dI7glUrhZcJYo=
github.com/ory/hydra-client-go/v2 v2.0.3 h1:jIx968J9RBnjRuaQ21QMLCwZoa28FPvzYWAQ+88XVLw=
Expand Down Expand Up @@ -1342,8 +1346,9 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
Expand Down
3 changes: 3 additions & 0 deletions identity/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,8 @@ type (

// HydrateIdentityAssociations hydrates the associations of an identity.
HydrateIdentityAssociations(ctx context.Context, i *Identity, expandables Expandables) error

// TODO: description
InjectTraitsSchemaURL(ctx context.Context, i *Identity) error
}
)
7 changes: 1 addition & 6 deletions identity/test/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ import (
"github.com/ory/kratos/x"
)

func TestPool(ctx context.Context, conf *config.Config, p interface {
persistence.Persister
}, m *identity.Manager) func(t *testing.T) {
func TestPool(ctx context.Context, conf *config.Config, p persistence.Persister, m *identity.Manager) func(t *testing.T) {
return func(t *testing.T) {
exampleServerURL := urlx.ParseOrPanic("http://example.com")
conf.MustSet(ctx, config.ViperKeyPublicBaseURL, exampleServerURL.String())
Expand Down Expand Up @@ -134,7 +132,6 @@ func TestPool(ctx context.Context, conf *config.Config, p interface {
assert.Empty(t, actual.RecoveryAddresses)
assert.Empty(t, actual.VerifiableAddresses)

require.Len(t, actual.InternalCredentials, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reason for removal?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Software that imports kratos as a package might choose to hydrate the credentials in a different way, bypassing InternalCredentials. This tests implementational details, which we shouldn't do IMO.

LMKWYT.

require.Len(t, actual.Credentials, 2)

assertx.EqualAsJSONExcept(t, expected.Credentials[identity.CredentialsTypePassword], actual.Credentials[identity.CredentialsTypePassword], []string{"updated_at", "created_at"})
Expand Down Expand Up @@ -181,7 +178,6 @@ func TestPool(ctx context.Context, conf *config.Config, p interface {
t.Run("expand=everything", func(t *testing.T) {
runner(t, identity.ExpandEverything, func(t *testing.T, actual *identity.Identity) {

require.Len(t, actual.InternalCredentials, 2)
require.Len(t, actual.Credentials, 2)

assertx.EqualAsJSONExcept(t, expected.Credentials[identity.CredentialsTypePassword], actual.Credentials[identity.CredentialsTypePassword], []string{"updated_at", "created_at"})
Expand All @@ -199,7 +195,6 @@ func TestPool(ctx context.Context, conf *config.Config, p interface {
runner(t, identity.ExpandNothing, func(t *testing.T, actual *identity.Identity) {
require.NoError(t, p.HydrateIdentityAssociations(ctx, actual, identity.ExpandEverything))

require.Len(t, actual.InternalCredentials, 2)
require.Len(t, actual.Credentials, 2)

assertx.EqualAsJSONExcept(t, expected.Credentials[identity.CredentialsTypePassword], actual.Credentials[identity.CredentialsTypePassword], []string{"updated_at", "created_at"})
Expand Down
1 change: 1 addition & 0 deletions persistence/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Persister interface {
MigrateDown(c context.Context, steps int) error
MigrateUp(c context.Context) error
Migrator() *popx.Migrator
MigrationBox() *popx.MigrationBox
GetConnection(ctx context.Context) *pop.Connection
Transaction(ctx context.Context, callback func(ctx context.Context, connection *pop.Connection) error) error
Networker
Expand Down
45 changes: 45 additions & 0 deletions persistence/sql/devices/persister_devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0

package devices

import (
"context"

"github.com/gobuffalo/pop/v6"
"github.com/gofrs/uuid"

"github.com/ory/kratos/session"
"github.com/ory/x/contextx"
"github.com/ory/x/popx"
"github.com/ory/x/sqlcon"
)

var _ session.DevicePersister = (*DevicePersister)(nil)

type DevicePersister struct {
ctxer contextx.Provider
c *pop.Connection
nid uuid.UUID
}

func NewPersister(r contextx.Provider, c *pop.Connection) *DevicePersister {
return &DevicePersister{
ctxer: r,
c: c,
}
}

func (p *DevicePersister) NetworkID(ctx context.Context) uuid.UUID {
return p.ctxer.Contextualizer().Network(ctx, p.nid)
}

func (p DevicePersister) WithNetworkID(nid uuid.UUID) session.DevicePersister {
p.nid = nid
return &p
}

func (p *DevicePersister) CreateDevice(ctx context.Context, d *session.Device) error {
d.NID = p.NetworkID(ctx)
return sqlcon.HandleError(popx.GetConnection(ctx, p.c.WithContext(ctx)).Create(d))
}
Loading