Skip to content

Commit

Permalink
rpc/migration: limit email migration to conf'd projects (#65)
Browse files Browse the repository at this point in the history
* rpc/migration: limit email migration to conf'd projects

* rpc/migration: limit project in one-time migration too
  • Loading branch information
patrislav authored Aug 23, 2024
1 parent 94e2543 commit f47e436
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions config/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type OIDCToStytchConfig struct {
}

type EmailMigrationConfig struct {
Enabled bool `toml:"enabled"`
IssuerPrefix string `toml:"issuer_prefix"`
Enabled bool `toml:"enabled"`
IssuerPrefix string `toml:"issuer_prefix"`
Projects []uint64 `toml:"projects"`
}
1 change: 1 addition & 0 deletions etc/waas-auth.dev.conf
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ QwIDAQAB
[migrations.oidc_to_email]
enabled = true
issuer_prefix = "https://cognito-idp.ca-central-1.amazonaws.com/"
projects = [694]

12 changes: 12 additions & 0 deletions rpc/migration/oidc_to_email.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"strings"

"github.com/0xsequence/waas-authenticator/config"
Expand All @@ -28,6 +29,9 @@ func (m *OIDCToEmail) OnRegisterSession(ctx context.Context, originalAccount *da
if originalAccount.ProjectID != tntData.ProjectID {
return errors.New("project id does not match")
}
if !slices.Contains(m.config.Projects, originalAccount.ProjectID) {
return nil
}
if originalAccount.Identity.Type != proto.IdentityType_OIDC {
return nil
}
Expand Down Expand Up @@ -79,6 +83,10 @@ func (m *OIDCToEmail) OnRegisterSession(ctx context.Context, originalAccount *da
}

func (m *OIDCToEmail) NextBatch(ctx context.Context, projectID uint64, page data.Page) ([]string, data.Page, error) {
if !slices.Contains(m.config.Projects, projectID) {
return nil, data.Page{}, fmt.Errorf("project id does not match")
}

items := make([]string, 0, page.Limit)
for {
accounts, page, err := m.accounts.ListByProjectAndIdentity(ctx, page, projectID, proto.IdentityType_OIDC, m.config.IssuerPrefix)
Expand Down Expand Up @@ -109,6 +117,10 @@ func (m *OIDCToEmail) NextBatch(ctx context.Context, projectID uint64, page data
}

func (m *OIDCToEmail) ProcessItems(ctx context.Context, tenant *proto.TenantData, items []string) (*Result, error) {
if !slices.Contains(m.config.Projects, tenant.ProjectID) {
return nil, fmt.Errorf("project id does not match")
}

if len(items) > 100 {
return nil, fmt.Errorf("can only process 100 items at a time")
}
Expand Down
3 changes: 3 additions & 0 deletions rpc/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,12 @@ func TestMigrationEmail(t *testing.T) {
issuer, tok, closeJWKS := issueAccessTokenAndRunJwksServer(t, tokBuilderFn)
defer closeJWKS()

projectID := currentProjectID.Load() + 1
svc := initRPC(t, func(cfg *config.Config) {
cfg.Migrations.Email = config.EmailMigrationConfig{
Enabled: true,
IssuerPrefix: issuer,
Projects: []uint64{projectID},
}
})
tenant, _ := newTenant(t, svc.Enclave, issuer)
Expand Down Expand Up @@ -316,6 +318,7 @@ func TestMigrationEmail(t *testing.T) {
cfg.Migrations.Email = config.EmailMigrationConfig{
Enabled: true,
IssuerPrefix: issuer,
Projects: []uint64{projectID},
}
})
tenant, _ := newTenant(t, svc.Enclave, issuer)
Expand Down

0 comments on commit f47e436

Please sign in to comment.