2022-12-14 06:17:36 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-12-14 06:17:36 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
SecurityPolicyProjectionTable = "projections.security_policies"
|
|
|
|
SecurityPolicyColumnInstanceID = "instance_id"
|
|
|
|
SecurityPolicyColumnCreationDate = "creation_date"
|
|
|
|
SecurityPolicyColumnChangeDate = "change_date"
|
|
|
|
SecurityPolicyColumnSequence = "sequence"
|
|
|
|
SecurityPolicyColumnEnabled = "enabled"
|
|
|
|
SecurityPolicyColumnAllowedOrigins = "origins"
|
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type securityPolicyProjection struct{}
|
|
|
|
|
|
|
|
func newSecurityPolicyProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(securityPolicyProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*securityPolicyProjection) Name() string {
|
|
|
|
return SecurityPolicyProjectionTable
|
2022-12-14 06:17:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (*securityPolicyProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(SecurityPolicyColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(SecurityPolicyColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(SecurityPolicyColumnInstanceID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SecurityPolicyColumnSequence, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(SecurityPolicyColumnEnabled, handler.ColumnTypeBool, handler.Default(false)),
|
|
|
|
handler.NewColumn(SecurityPolicyColumnAllowedOrigins, handler.ColumnTypeTextArray, handler.Nullable()),
|
2022-12-14 06:17:36 +00:00
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewPrimaryKey(SecurityPolicyColumnInstanceID),
|
2022-12-14 06:17:36 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *securityPolicyProjection) Reducers() []handler.AggregateReducer {
|
2022-12-14 06:17:36 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-12-14 06:17:36 +00:00
|
|
|
{
|
|
|
|
Event: instance.SecurityPolicySetEventType,
|
|
|
|
Reduce: p.reduceSecurityPolicySet,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(SecurityPolicyColumnInstanceID),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *securityPolicyProjection) reduceSecurityPolicySet(event eventstore.Event) (*handler.Statement, error) {
|
|
|
|
e, ok := event.(*instance.SecurityPolicySetEvent)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-D3g87", "reduce.wrong.event.type %s", instance.SecurityPolicySetEventType)
|
|
|
|
}
|
|
|
|
changes := []handler.Column{
|
|
|
|
handler.NewCol(SecurityPolicyColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SecurityPolicyColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SecurityPolicyColumnInstanceID, e.Aggregate().InstanceID),
|
|
|
|
handler.NewCol(SecurityPolicyColumnSequence, e.Sequence()),
|
|
|
|
}
|
|
|
|
if e.Enabled != nil {
|
|
|
|
changes = append(changes, handler.NewCol(SecurityPolicyColumnEnabled, *e.Enabled))
|
|
|
|
}
|
|
|
|
if e.AllowedOrigins != nil {
|
|
|
|
changes = append(changes, handler.NewCol(SecurityPolicyColumnAllowedOrigins, e.AllowedOrigins))
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpsertStatement(
|
2022-12-14 06:17:36 +00:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SecurityPolicyColumnInstanceID, ""),
|
|
|
|
},
|
|
|
|
changes,
|
|
|
|
), nil
|
|
|
|
}
|