zitadel/internal/query/projection/user_metadata.go
Tim Möhlmann ba9b807854
perf(oidc): optimize the introspection endpoint (#6909)
* get key by id and cache them

* userinfo from events for v2 tokens

* improve keyset caching

* concurrent token and client checks

* client and project in single query

* logging and otel

* drop owner_removed column on apps and authN tables

* userinfo and project roles in go routines

* get  oidc user info from projections and add actions

* add avatar URL

* some cleanup

* pull oidc work branch

* remove storage from server

* add config flag for experimental introspection

* legacy introspection flag

* drop owner_removed column on user projections

* drop owner_removed column on useer_metadata

* query userinfo unit test

* query introspection client test

* add user_grants to the userinfo query

* handle PAT scopes

* bring triggers back

* test instance keys query

* add userinfo unit tests

* unit test keys

* go mod tidy

* solve some bugs

* fix missing preferred login name

* do not run triggers in go routines, they seem to deadlock

* initialize the trigger handlers late with a sync.OnceValue

* Revert "do not run triggers in go routines, they seem to deadlock"

This reverts commit 2a03da2127b7dc74552ec25d4772282a82cc1cba.

* add missing translations

* chore: update go version for linting

* pin oidc version

* parse a global time location for query test

* fix linter complains

* upgrade go lint

* fix more linting issues

---------

Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
2023-11-21 13:11:38 +01:00

171 lines
5.7 KiB
Go

package projection
import (
"context"
"github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/eventstore"
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/repository/instance"
"github.com/zitadel/zitadel/internal/repository/org"
"github.com/zitadel/zitadel/internal/repository/user"
)
const (
UserMetadataProjectionTable = "projections.user_metadata5"
UserMetadataColumnUserID = "user_id"
UserMetadataColumnCreationDate = "creation_date"
UserMetadataColumnChangeDate = "change_date"
UserMetadataColumnSequence = "sequence"
UserMetadataColumnResourceOwner = "resource_owner"
UserMetadataColumnInstanceID = "instance_id"
UserMetadataColumnKey = "key"
UserMetadataColumnValue = "value"
)
type userMetadataProjection struct{}
func newUserMetadataProjection(ctx context.Context, config handler.Config) *handler.Handler {
return handler.NewHandler(ctx, &config, new(userMetadataProjection))
}
func (*userMetadataProjection) Name() string {
return UserMetadataProjectionTable
}
func (*userMetadataProjection) Init() *old_handler.Check {
return handler.NewTableCheck(
handler.NewTable([]*handler.InitColumn{
handler.NewColumn(UserMetadataColumnUserID, handler.ColumnTypeText),
handler.NewColumn(UserMetadataColumnCreationDate, handler.ColumnTypeTimestamp),
handler.NewColumn(UserMetadataColumnChangeDate, handler.ColumnTypeTimestamp),
handler.NewColumn(UserMetadataColumnSequence, handler.ColumnTypeInt64),
handler.NewColumn(UserMetadataColumnResourceOwner, handler.ColumnTypeText),
handler.NewColumn(UserMetadataColumnInstanceID, handler.ColumnTypeText),
handler.NewColumn(UserMetadataColumnKey, handler.ColumnTypeText),
handler.NewColumn(UserMetadataColumnValue, handler.ColumnTypeBytes, handler.Nullable()),
},
handler.NewPrimaryKey(UserMetadataColumnInstanceID, UserMetadataColumnUserID, UserMetadataColumnKey),
handler.WithIndex(handler.NewIndex("resource_owner", []string{UserGrantResourceOwner})),
),
)
}
func (p *userMetadataProjection) Reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: user.AggregateType,
EventReducers: []handler.EventReducer{
{
Event: user.MetadataSetType,
Reduce: p.reduceMetadataSet,
},
{
Event: user.MetadataRemovedType,
Reduce: p.reduceMetadataRemoved,
},
{
Event: user.MetadataRemovedAllType,
Reduce: p.reduceMetadataRemovedAll,
},
{
Event: user.UserRemovedType,
Reduce: p.reduceMetadataRemovedAll,
},
},
},
{
Aggregate: org.AggregateType,
EventReducers: []handler.EventReducer{
{
Event: org.OrgRemovedEventType,
Reduce: p.reduceOwnerRemoved,
},
},
},
{
Aggregate: instance.AggregateType,
EventReducers: []handler.EventReducer{
{
Event: instance.InstanceRemovedEventType,
Reduce: reduceInstanceRemovedHelper(UserMetadataColumnInstanceID),
},
},
},
}
}
func (p *userMetadataProjection) reduceMetadataSet(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*user.MetadataSetEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Ghn52", "reduce.wrong.event.type %s", user.MetadataSetType)
}
return handler.NewUpsertStatement(
e,
[]handler.Column{
handler.NewCol(UserMetadataColumnInstanceID, nil),
handler.NewCol(UserMetadataColumnUserID, nil),
handler.NewCol(UserMetadataColumnKey, e.Key),
},
[]handler.Column{
handler.NewCol(UserMetadataColumnInstanceID, e.Aggregate().InstanceID),
handler.NewCol(UserMetadataColumnUserID, e.Aggregate().ID),
handler.NewCol(UserMetadataColumnKey, e.Key),
handler.NewCol(UserMetadataColumnResourceOwner, e.Aggregate().ResourceOwner),
handler.NewCol(UserMetadataColumnCreationDate, e.CreationDate()),
handler.NewCol(UserMetadataColumnChangeDate, e.CreationDate()),
handler.NewCol(UserMetadataColumnSequence, e.Sequence()),
handler.NewCol(UserMetadataColumnValue, e.Value),
},
), nil
}
func (p *userMetadataProjection) reduceMetadataRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*user.MetadataRemovedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Bm542", "reduce.wrong.event.type %s", user.MetadataRemovedType)
}
return handler.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(UserMetadataColumnUserID, e.Aggregate().ID),
handler.NewCond(UserMetadataColumnKey, e.Key),
handler.NewCond(UserAuthMethodInstanceIDCol, e.Aggregate().InstanceID),
},
), nil
}
func (p *userMetadataProjection) reduceMetadataRemovedAll(event eventstore.Event) (*handler.Statement, error) {
switch event.(type) {
case *user.MetadataRemovedAllEvent,
*user.UserRemovedEvent:
//ok
default:
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Bmnf2", "reduce.wrong.event.type %v", []eventstore.EventType{user.MetadataRemovedAllType, user.UserRemovedType})
}
return handler.NewDeleteStatement(
event,
[]handler.Condition{
handler.NewCond(UserMetadataColumnUserID, event.Aggregate().ID),
handler.NewCond(UserAuthMethodInstanceIDCol, event.Aggregate().InstanceID),
},
), nil
}
func (p *userMetadataProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*org.OrgRemovedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-oqwul", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
}
return handler.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(UserMetadataColumnInstanceID, e.Aggregate().InstanceID),
handler.NewCond(UserMetadataColumnResourceOwner, e.Aggregate().ID),
},
), nil
}