zitadel/internal/query/projection/secret_generator.go

165 lines
6.8 KiB
Go
Raw Normal View History

package projection
import (
"context"
"github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler"
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
"github.com/zitadel/zitadel/internal/repository/instance"
)
const (
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 09:52:43 +02:00
SecretGeneratorProjectionTable = "projections.secret_generators2"
SecretGeneratorColumnGeneratorType = "generator_type"
SecretGeneratorColumnAggregateID = "aggregate_id"
SecretGeneratorColumnCreationDate = "creation_date"
SecretGeneratorColumnChangeDate = "change_date"
SecretGeneratorColumnSequence = "sequence"
SecretGeneratorColumnResourceOwner = "resource_owner"
SecretGeneratorColumnInstanceID = "instance_id"
SecretGeneratorColumnLength = "length"
SecretGeneratorColumnExpiry = "expiry"
SecretGeneratorColumnIncludeLowerLetters = "include_lower_letters"
SecretGeneratorColumnIncludeUpperLetters = "include_upper_letters"
SecretGeneratorColumnIncludeDigits = "include_digits"
SecretGeneratorColumnIncludeSymbols = "include_symbols"
)
type secretGeneratorProjection struct {
crdb.StatementHandler
}
func newSecretGeneratorProjection(ctx context.Context, config crdb.StatementHandlerConfig) *secretGeneratorProjection {
p := new(secretGeneratorProjection)
config.ProjectionName = SecretGeneratorProjectionTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
feat(database): support for postgres (#3998) * beginning with postgres statements * try pgx * use pgx * database * init works for postgres * arrays working * init for cockroach * init * start tests * tests * TESTS * ch * ch * chore: use go 1.18 * read stmts * fix typo * tests * connection string * add missing error handler * cleanup * start all apis * go mod tidy * old update * switch back to minute * on conflict * replace string slice with `database.StringArray` in db models * fix tests and start * update go version in dockerfile * setup go * clean up * remove notification migration * update * docs: add deploy guide for postgres * fix: revert sonyflake * use `database.StringArray` for daos * use `database.StringArray` every where * new tables * index naming, metadata primary key, project grant role key type * docs(postgres): change to beta * chore: correct compose * fix(defaults): add empty postgres config * refactor: remove unused code * docs: add postgres to self hosted * fix broken link * so? * change title * add mdx to link * fix stmt * update goreleaser in test-code * docs: improve postgres example * update more projections * fix: add beta log for postgres * revert index name change * prerelease * fix: add sequence to v1 "reduce paniced" * log if nil * add logging * fix: log output * fix(import): check if org exists and user * refactor: imports * fix(user): ignore malformed events * refactor: method naming * fix: test * refactor: correct errors.Is call * ci: don't build dev binaries on main * fix(go releaser): update version to 1.11.0 * fix(user): projection should not break * fix(user): handle error properly * docs: correct config example * Update .releaserc.js * Update .releaserc.js Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 09:52:43 +02:00
crdb.NewColumn(SecretGeneratorColumnGeneratorType, crdb.ColumnTypeEnum),
crdb.NewColumn(SecretGeneratorColumnAggregateID, crdb.ColumnTypeText),
crdb.NewColumn(SecretGeneratorColumnCreationDate, crdb.ColumnTypeTimestamp),
crdb.NewColumn(SecretGeneratorColumnChangeDate, crdb.ColumnTypeTimestamp),
crdb.NewColumn(SecretGeneratorColumnSequence, crdb.ColumnTypeInt64),
crdb.NewColumn(SecretGeneratorColumnResourceOwner, crdb.ColumnTypeText),
crdb.NewColumn(SecretGeneratorColumnInstanceID, crdb.ColumnTypeText),
crdb.NewColumn(SecretGeneratorColumnLength, crdb.ColumnTypeInt64),
crdb.NewColumn(SecretGeneratorColumnExpiry, crdb.ColumnTypeInt64),
crdb.NewColumn(SecretGeneratorColumnIncludeLowerLetters, crdb.ColumnTypeBool),
crdb.NewColumn(SecretGeneratorColumnIncludeUpperLetters, crdb.ColumnTypeBool),
crdb.NewColumn(SecretGeneratorColumnIncludeDigits, crdb.ColumnTypeBool),
crdb.NewColumn(SecretGeneratorColumnIncludeSymbols, crdb.ColumnTypeBool),
},
crdb.NewPrimaryKey(SecretGeneratorColumnInstanceID, SecretGeneratorColumnGeneratorType, SecretGeneratorColumnAggregateID),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *secretGeneratorProjection) reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: instance.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: instance.SecretGeneratorAddedEventType,
Reduce: p.reduceSecretGeneratorAdded,
},
{
Event: instance.SecretGeneratorChangedEventType,
Reduce: p.reduceSecretGeneratorChanged,
},
{
Event: instance.SecretGeneratorRemovedEventType,
Reduce: p.reduceSecretGeneratorRemoved,
},
2022-10-26 15:06:48 +02:00
{
Event: instance.InstanceRemovedEventType,
Reduce: reduceInstanceRemovedHelper(SecretGeneratorColumnInstanceID),
},
},
},
}
}
func (p *secretGeneratorProjection) reduceSecretGeneratorAdded(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.SecretGeneratorAddedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-sk99F", "reduce.wrong.event.type %s", instance.SecretGeneratorAddedEventType)
}
return crdb.NewCreateStatement(
e,
[]handler.Column{
handler.NewCol(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
handler.NewCol(SecretGeneratorColumnGeneratorType, e.GeneratorType),
handler.NewCol(SecretGeneratorColumnCreationDate, e.CreationDate()),
handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
handler.NewCol(SecretGeneratorColumnResourceOwner, e.Aggregate().ResourceOwner),
handler.NewCol(SecretGeneratorColumnInstanceID, e.Aggregate().InstanceID),
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()),
handler.NewCol(SecretGeneratorColumnLength, e.Length),
handler.NewCol(SecretGeneratorColumnExpiry, e.Expiry),
handler.NewCol(SecretGeneratorColumnIncludeLowerLetters, e.IncludeLowerLetters),
handler.NewCol(SecretGeneratorColumnIncludeUpperLetters, e.IncludeUpperLetters),
handler.NewCol(SecretGeneratorColumnIncludeDigits, e.IncludeDigits),
handler.NewCol(SecretGeneratorColumnIncludeSymbols, e.IncludeSymbols),
},
), nil
}
func (p *secretGeneratorProjection) reduceSecretGeneratorChanged(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.SecretGeneratorChangedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-s00Fs", "reduce.wrong.event.type %s", instance.SecretGeneratorChangedEventType)
}
columns := make([]handler.Column, 0, 7)
columns = append(columns, handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()))
if e.Length != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnLength, *e.Length))
}
if e.Expiry != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnExpiry, *e.Expiry))
}
if e.IncludeLowerLetters != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeLowerLetters, *e.IncludeLowerLetters))
}
if e.IncludeUpperLetters != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeUpperLetters, *e.IncludeUpperLetters))
}
if e.IncludeDigits != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeDigits, *e.IncludeDigits))
}
if e.IncludeSymbols != nil {
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeSymbols, *e.IncludeSymbols))
}
return crdb.NewUpdateStatement(
e,
columns,
[]handler.Condition{
handler.NewCond(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
handler.NewCond(SecretGeneratorColumnGeneratorType, e.GeneratorType),
handler.NewCond(SecretGeneratorColumnInstanceID, e.Aggregate().InstanceID),
},
), nil
}
func (p *secretGeneratorProjection) reduceSecretGeneratorRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.SecretGeneratorRemovedEvent)
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-fmiIf", "reduce.wrong.event.type %s", instance.SecretGeneratorRemovedEventType)
}
return crdb.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
handler.NewCond(SecretGeneratorColumnGeneratorType, e.GeneratorType),
handler.NewCond(SecretGeneratorColumnInstanceID, e.Aggregate().InstanceID),
},
), nil
}