feat: Notification providers config (#3212)

* feat: add login check lifetimes to login policy

* feat: org features test

* feat: debug notificatiaon events

* feat: debug notification file/log commands

* feat: add requests to proto

* feat: add api for debug notification providers file/log

* feat: add projection for debug notifiication providers

* feat: requests

* feat: merge v2

* feat: add settings proto to generate

* feat: notifiaction providers

* fix: remove unused code

* Update iam_converter.go

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
Fabi
2022-03-07 14:22:37 +01:00
committed by GitHub
parent 7d6c933485
commit 7899a0b851
47 changed files with 2489 additions and 200 deletions

View File

@@ -0,0 +1,112 @@
package query
import (
"context"
"database/sql"
errs "errors"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/query/projection"
)
type DebugNotificationProvider struct {
AggregateID string
CreationDate time.Time
ChangeDate time.Time
Sequence uint64
ResourceOwner string
State domain.NotificationProviderState
Type domain.NotificationProviderType
Compact bool
}
var (
notificationProviderTable = table{
name: projection.DebugNotificationProviderTable,
}
NotificationProviderColumnAggID = Column{
name: projection.DebugNotificationProviderAggIDCol,
table: notificationProviderTable,
}
NotificationProviderColumnCreationDate = Column{
name: projection.DebugNotificationProviderCreationDateCol,
table: notificationProviderTable,
}
NotificationProviderColumnChangeDate = Column{
name: projection.DebugNotificationProviderChangeDateCol,
table: notificationProviderTable,
}
NotificationProviderColumnSequence = Column{
name: projection.DebugNotificationProviderSequenceCol,
table: notificationProviderTable,
}
NotificationProviderColumnResourceOwner = Column{
name: projection.DebugNotificationProviderResourceOwnerCol,
table: notificationProviderTable,
}
NotificationProviderColumnState = Column{
name: projection.DebugNotificationProviderStateCol,
table: notificationProviderTable,
}
NotificationProviderColumnType = Column{
name: projection.DebugNotificationProviderTypeCol,
table: notificationProviderTable,
}
NotificationProviderColumnCompact = Column{
name: projection.DebugNotificationProviderCompactCol,
table: notificationProviderTable,
}
)
func (q *Queries) NotificationProviderByIDAndType(ctx context.Context, aggID string, providerType domain.NotificationProviderType) (*DebugNotificationProvider, error) {
query, scan := prepareDebugNotificationProviderQuery()
stmt, args, err := query.Where(
sq.Or{
sq.Eq{
LoginPolicyColumnOrgID.identifier(): aggID,
},
}).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-f9jSf", "Errors.Query.SQLStatement")
}
row := q.client.QueryRowContext(ctx, stmt, args...)
return scan(row)
}
func prepareDebugNotificationProviderQuery() (sq.SelectBuilder, func(*sql.Row) (*DebugNotificationProvider, error)) {
return sq.Select(
NotificationProviderColumnAggID.identifier(),
NotificationProviderColumnCreationDate.identifier(),
NotificationProviderColumnChangeDate.identifier(),
NotificationProviderColumnSequence.identifier(),
NotificationProviderColumnResourceOwner.identifier(),
NotificationProviderColumnState.identifier(),
NotificationProviderColumnType.identifier(),
NotificationProviderColumnCompact.identifier(),
).From(notificationProviderTable.identifier()).PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*DebugNotificationProvider, error) {
p := new(DebugNotificationProvider)
err := row.Scan(
&p.AggregateID,
&p.CreationDate,
&p.ChangeDate,
&p.Sequence,
&p.ResourceOwner,
&p.State,
&p.Type,
&p.Compact,
)
if err != nil {
if errs.Is(err, sql.ErrNoRows) {
return nil, errors.ThrowNotFound(err, "QUERY-s9ujf", "Errors.NotificationProvider.NotFound")
}
return nil, errors.ThrowInternal(err, "QUERY-2liu0", "Errors.Internal")
}
return p, nil
}
}

View File

@@ -0,0 +1,130 @@
package query
import (
"database/sql"
"database/sql/driver"
"errors"
"fmt"
"regexp"
"testing"
"github.com/caos/zitadel/internal/domain"
errs "github.com/caos/zitadel/internal/errors"
)
func Test_NotificationProviderPrepares(t *testing.T) {
type want struct {
sqlExpectations sqlExpectation
err checkErr
}
tests := []struct {
name string
prepare interface{}
want want
object interface{}
}{
{
name: "prepareNotificationProviderQuery no result",
prepare: prepareDebugNotificationProviderQuery,
want: want{
sqlExpectations: mockQueries(
regexp.QuoteMeta(`SELECT zitadel.projections.notification_providers.aggregate_id,`+
` zitadel.projections.notification_providers.creation_date,`+
` zitadel.projections.notification_providers.change_date,`+
` zitadel.projections.notification_providers.sequence,`+
` zitadel.projections.notification_providers.resource_owner,`+
` zitadel.projections.notification_providers.state,`+
` zitadel.projections.notification_providers.provider_type,`+
` zitadel.projections.notification_providers.compact`+
` FROM zitadel.projections.notification_providers`),
nil,
nil,
),
err: func(err error) (error, bool) {
if !errs.IsNotFound(err) {
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
}
return nil, true
},
},
object: (*DebugNotificationProvider)(nil),
},
{
name: "prepareNotificationProviderQuery found",
prepare: prepareDebugNotificationProviderQuery,
want: want{
sqlExpectations: mockQuery(
regexp.QuoteMeta(`SELECT zitadel.projections.notification_providers.aggregate_id,`+
` zitadel.projections.notification_providers.creation_date,`+
` zitadel.projections.notification_providers.change_date,`+
` zitadel.projections.notification_providers.sequence,`+
` zitadel.projections.notification_providers.resource_owner,`+
` zitadel.projections.notification_providers.state,`+
` zitadel.projections.notification_providers.provider_type,`+
` zitadel.projections.notification_providers.compact`+
` FROM zitadel.projections.notification_providers`),
[]string{
"aggregate_id",
"creation_date",
"change_date",
"sequence",
"resource_owner",
"state",
"provider_type",
"compact",
},
[]driver.Value{
"agg-id",
testNow,
testNow,
uint64(20211109),
"ro-id",
domain.NotificationProviderStateActive,
domain.NotificationProviderTypeFile,
true,
},
),
},
object: &DebugNotificationProvider{
AggregateID: "agg-id",
CreationDate: testNow,
ChangeDate: testNow,
Sequence: 20211109,
ResourceOwner: "ro-id",
State: domain.NotificationProviderStateActive,
Type: domain.NotificationProviderTypeFile,
Compact: true,
},
},
{
name: "prepareNotificationProviderQuery sql err",
prepare: prepareDebugNotificationProviderQuery,
want: want{
sqlExpectations: mockQueryErr(
regexp.QuoteMeta(`SELECT zitadel.projections.notification_providers.aggregate_id,`+
` zitadel.projections.notification_providers.creation_date,`+
` zitadel.projections.notification_providers.change_date,`+
` zitadel.projections.notification_providers.sequence,`+
` zitadel.projections.notification_providers.resource_owner,`+
` zitadel.projections.notification_providers.state,`+
` zitadel.projections.notification_providers.provider_type,`+
` zitadel.projections.notification_providers.compact`+
` FROM zitadel.projections.notification_providers`),
sql.ErrConnDone,
),
err: func(err error) (error, bool) {
if !errors.Is(err, sql.ErrConnDone) {
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
}
return nil, true
},
},
object: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err)
})
}
}

View File

@@ -0,0 +1,160 @@
package projection
import (
"context"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/repository/settings"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/handler"
"github.com/caos/zitadel/internal/eventstore/handler/crdb"
"github.com/caos/zitadel/internal/repository/iam"
)
type DebugNotificationProviderProjection struct {
crdb.StatementHandler
}
const (
DebugNotificationProviderTable = "zitadel.projections.notification_providers"
)
func NewDebugNotificationProviderProjection(ctx context.Context, config crdb.StatementHandlerConfig) *DebugNotificationProviderProjection {
p := &DebugNotificationProviderProjection{}
config.ProjectionName = DebugNotificationProviderTable
config.Reducers = p.reducers()
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *DebugNotificationProviderProjection) reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: iam.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: iam.DebugNotificationProviderFileAddedEventType,
Reduce: p.reduceDebugNotificationProviderAdded,
},
{
Event: iam.DebugNotificationProviderFileChangedEventType,
Reduce: p.reduceDebugNotificationProviderChanged,
},
{
Event: iam.DebugNotificationProviderFileRemovedEventType,
Reduce: p.reduceDebugNotificationProviderRemoved,
},
{
Event: iam.DebugNotificationProviderLogAddedEventType,
Reduce: p.reduceDebugNotificationProviderAdded,
},
{
Event: iam.DebugNotificationProviderLogChangedEventType,
Reduce: p.reduceDebugNotificationProviderChanged,
},
{
Event: iam.DebugNotificationProviderLogRemovedEventType,
Reduce: p.reduceDebugNotificationProviderRemoved,
},
},
},
}
}
const (
DebugNotificationProviderAggIDCol = "aggregate_id"
DebugNotificationProviderCreationDateCol = "creation_date"
DebugNotificationProviderChangeDateCol = "change_date"
DebugNotificationProviderSequenceCol = "sequence"
DebugNotificationProviderResourceOwnerCol = "resource_owner"
DebugNotificationProviderStateCol = "state"
DebugNotificationProviderTypeCol = "provider_type"
DebugNotificationProviderCompactCol = "compact"
)
func (p *DebugNotificationProviderProjection) reduceDebugNotificationProviderAdded(event eventstore.Event) (*handler.Statement, error) {
var providerEvent settings.DebugNotificationProviderAddedEvent
var providerType domain.NotificationProviderType
switch e := event.(type) {
case *iam.DebugNotificationProviderFileAddedEvent:
providerEvent = e.DebugNotificationProviderAddedEvent
providerType = domain.NotificationProviderTypeFile
case *iam.DebugNotificationProviderLogAddedEvent:
providerEvent = e.DebugNotificationProviderAddedEvent
providerType = domain.NotificationProviderTypeLog
default:
logging.WithFields("seq", event.Sequence(), "expectedTypes", []eventstore.EventType{iam.DebugNotificationProviderFileAddedEventType, iam.DebugNotificationProviderLogAddedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-pYPxS", "reduce.wrong.event.type")
}
return crdb.NewCreateStatement(&providerEvent, []handler.Column{
handler.NewCol(DebugNotificationProviderAggIDCol, providerEvent.Aggregate().ID),
handler.NewCol(DebugNotificationProviderCreationDateCol, providerEvent.CreationDate()),
handler.NewCol(DebugNotificationProviderChangeDateCol, providerEvent.CreationDate()),
handler.NewCol(DebugNotificationProviderSequenceCol, providerEvent.Sequence()),
handler.NewCol(DebugNotificationProviderResourceOwnerCol, providerEvent.Aggregate().ResourceOwner),
handler.NewCol(DebugNotificationProviderStateCol, domain.NotificationProviderStateActive),
handler.NewCol(DebugNotificationProviderTypeCol, providerType),
handler.NewCol(DebugNotificationProviderCompactCol, providerEvent.Compact),
}), nil
}
func (p *DebugNotificationProviderProjection) reduceDebugNotificationProviderChanged(event eventstore.Event) (*handler.Statement, error) {
var providerEvent settings.DebugNotificationProviderChangedEvent
var providerType domain.NotificationProviderType
switch e := event.(type) {
case *iam.DebugNotificationProviderFileChangedEvent:
providerEvent = e.DebugNotificationProviderChangedEvent
providerType = domain.NotificationProviderTypeFile
case *iam.DebugNotificationProviderLogChangedEvent:
providerEvent = e.DebugNotificationProviderChangedEvent
providerType = domain.NotificationProviderTypeLog
default:
logging.WithFields("seq", event.Sequence(), "expectedTypes", []eventstore.EventType{iam.DebugNotificationProviderFileChangedEventType, iam.DebugNotificationProviderLogChangedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-pYPxS", "reduce.wrong.event.type")
}
cols := []handler.Column{
handler.NewCol(DebugNotificationProviderChangeDateCol, providerEvent.CreationDate()),
handler.NewCol(DebugNotificationProviderSequenceCol, providerEvent.Sequence()),
}
if providerEvent.Compact != nil {
cols = append(cols, handler.NewCol(DebugNotificationProviderCompactCol, *providerEvent.Compact))
}
return crdb.NewUpdateStatement(
&providerEvent,
cols,
[]handler.Condition{
handler.NewCond(DebugNotificationProviderAggIDCol, providerEvent.Aggregate().ID),
handler.NewCond(DebugNotificationProviderTypeCol, providerType),
},
), nil
}
func (p *DebugNotificationProviderProjection) reduceDebugNotificationProviderRemoved(event eventstore.Event) (*handler.Statement, error) {
var providerEvent settings.DebugNotificationProviderRemovedEvent
var providerType domain.NotificationProviderType
switch e := event.(type) {
case *iam.DebugNotificationProviderFileRemovedEvent:
providerEvent = e.DebugNotificationProviderRemovedEvent
providerType = domain.NotificationProviderTypeFile
case *iam.DebugNotificationProviderLogRemovedEvent:
providerEvent = e.DebugNotificationProviderRemovedEvent
providerType = domain.NotificationProviderTypeLog
default:
logging.WithFields("seq", event.Sequence(), "expectedTypes", []eventstore.EventType{iam.DebugNotificationProviderFileRemovedEventType, iam.DebugNotificationProviderLogRemovedEventType}).Error("wrong event type")
return nil, errors.ThrowInvalidArgument(nil, "HANDL-dow9f", "reduce.wrong.event.type")
}
return crdb.NewDeleteStatement(
&providerEvent,
[]handler.Condition{
handler.NewCond(DebugNotificationProviderAggIDCol, providerEvent.Aggregate().ID),
handler.NewCond(DebugNotificationProviderTypeCol, providerType),
},
), nil
}

View File

@@ -0,0 +1,232 @@
package projection
import (
"testing"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/handler"
"github.com/caos/zitadel/internal/eventstore/repository"
"github.com/caos/zitadel/internal/repository/iam"
)
func TestDebugNotificationProviderProjection_reduces(t *testing.T) {
type args struct {
event func(t *testing.T) eventstore.Event
}
tests := []struct {
name string
args args
reduce func(event eventstore.Event) (*handler.Statement, error)
want wantReduce
}{
{
name: "iam.reduceNotificationProviderFileAdded",
reduce: (&DebugNotificationProviderProjection{}).reduceDebugNotificationProviderAdded,
args: args{
event: getEvent(testEvent(
repository.EventType(iam.DebugNotificationProviderFileAddedEventType),
iam.AggregateType,
[]byte(`{
"compact": true
}`),
), iam.DebugNotificationProviderFileAddedEventMapper),
},
want: wantReduce{
aggregateType: eventstore.AggregateType("iam"),
sequence: 15,
previousSequence: 10,
projection: DebugNotificationProviderTable,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "INSERT INTO zitadel.projections.notification_providers (aggregate_id, creation_date, change_date, sequence, resource_owner, state, provider_type, compact) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
expectedArgs: []interface{}{
"agg-id",
anyArg{},
anyArg{},
uint64(15),
"ro-id",
domain.NotificationProviderStateActive,
domain.NotificationProviderTypeFile,
true,
},
},
},
},
},
},
{
name: "iam.reduceNotificationProviderFileChanged",
reduce: (&DebugNotificationProviderProjection{}).reduceDebugNotificationProviderChanged,
args: args{
event: getEvent(testEvent(
repository.EventType(iam.DebugNotificationProviderFileChangedEventType),
iam.AggregateType,
[]byte(`{
"compact": true
}`),
), iam.DebugNotificationProviderFileChangedEventMapper),
},
want: wantReduce{
aggregateType: eventstore.AggregateType("iam"),
sequence: 15,
previousSequence: 10,
projection: DebugNotificationProviderTable,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.notification_providers SET (change_date, sequence, compact) = ($1, $2, $3) WHERE (aggregate_id = $4) AND (provider_type = $5)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
true,
"agg-id",
domain.NotificationProviderTypeFile,
},
},
},
},
},
},
{
name: "iam.reduceNotificationProviderFileRemoved",
reduce: (&DebugNotificationProviderProjection{}).reduceDebugNotificationProviderRemoved,
args: args{
event: getEvent(testEvent(
repository.EventType(iam.DebugNotificationProviderFileRemovedEventType),
iam.AggregateType,
nil,
), iam.DebugNotificationProviderFileRemovedEventMapper),
},
want: wantReduce{
aggregateType: eventstore.AggregateType("iam"),
sequence: 15,
previousSequence: 10,
projection: DebugNotificationProviderTable,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "DELETE FROM zitadel.projections.notification_providers WHERE (aggregate_id = $1) AND (provider_type = $2)",
expectedArgs: []interface{}{
"agg-id",
domain.NotificationProviderTypeFile,
},
},
},
},
},
},
{
name: "iam.reduceNotificationProviderLogAdded",
reduce: (&DebugNotificationProviderProjection{}).reduceDebugNotificationProviderAdded,
args: args{
event: getEvent(testEvent(
repository.EventType(iam.DebugNotificationProviderLogAddedEventType),
iam.AggregateType,
[]byte(`{
"compact": true
}`),
), iam.DebugNotificationProviderLogAddedEventMapper),
},
want: wantReduce{
aggregateType: eventstore.AggregateType("iam"),
sequence: 15,
previousSequence: 10,
projection: DebugNotificationProviderTable,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "INSERT INTO zitadel.projections.notification_providers (aggregate_id, creation_date, change_date, sequence, resource_owner, state, provider_type, compact) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)",
expectedArgs: []interface{}{
"agg-id",
anyArg{},
anyArg{},
uint64(15),
"ro-id",
domain.NotificationProviderStateActive,
domain.NotificationProviderTypeLog,
true,
},
},
},
},
},
},
{
name: "iam.reduceNotificationProviderLogChanged",
reduce: (&DebugNotificationProviderProjection{}).reduceDebugNotificationProviderChanged,
args: args{
event: getEvent(testEvent(
repository.EventType(iam.DebugNotificationProviderLogChangedEventType),
iam.AggregateType,
[]byte(`{
"compact": true
}`),
), iam.DebugNotificationProviderLogChangedEventMapper),
},
want: wantReduce{
aggregateType: eventstore.AggregateType("iam"),
sequence: 15,
previousSequence: 10,
projection: DebugNotificationProviderTable,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "UPDATE zitadel.projections.notification_providers SET (change_date, sequence, compact) = ($1, $2, $3) WHERE (aggregate_id = $4) AND (provider_type = $5)",
expectedArgs: []interface{}{
anyArg{},
uint64(15),
true,
"agg-id",
domain.NotificationProviderTypeLog,
},
},
},
},
},
},
{
name: "iam.reduceNotificationProviderLogRemoved",
reduce: (&DebugNotificationProviderProjection{}).reduceDebugNotificationProviderRemoved,
args: args{
event: getEvent(testEvent(
repository.EventType(iam.DebugNotificationProviderLogRemovedEventType),
iam.AggregateType,
nil,
), iam.DebugNotificationProviderLogRemovedEventMapper),
},
want: wantReduce{
aggregateType: eventstore.AggregateType("iam"),
sequence: 15,
previousSequence: 10,
projection: DebugNotificationProviderTable,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: "DELETE FROM zitadel.projections.notification_providers WHERE (aggregate_id = $1) AND (provider_type = $2)",
expectedArgs: []interface{}{
"agg-id",
domain.NotificationProviderTypeLog,
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
event := baseEvent(t)
got, err := tt.reduce(event)
if _, ok := err.(errors.InvalidArgument); !ok {
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
}
event = tt.args.event(t)
got, err = tt.reduce(event)
assertReduce(t, got, err, tt.want)
})
}
}

View File

@@ -74,6 +74,7 @@ func Start(ctx context.Context, sqlClient *sql.DB, es *eventstore.Eventstore, co
NewSMSConfigProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["sms_config"]))
NewOIDCSettingsProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["oidc_settings"]))
_, err := NewKeyProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["keys"]), keyConfig, keyChan)
NewDebugNotificationProviderProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["debug_notification_provider"]))
return err
}