zitadel/internal/query/action_flow.go
Fabienne Bühler 07ce3b6905
chore!: Introduce ZITADEL v3 (#9645)
This PR summarizes multiple changes specifically only available with
ZITADEL v3:

- feat: Web Keys management
(https://github.com/zitadel/zitadel/pull/9526)
- fix(cmd): ensure proper working of mirror
(https://github.com/zitadel/zitadel/pull/9509)
- feat(Authz): system user support for permission check v2
(https://github.com/zitadel/zitadel/pull/9640)
- chore(license): change from Apache to AGPL
(https://github.com/zitadel/zitadel/pull/9597)
- feat(console): list v2 sessions
(https://github.com/zitadel/zitadel/pull/9539)
- fix(console): add loginV2 feature flag
(https://github.com/zitadel/zitadel/pull/9682)
- fix(feature flags): allow reading "own" flags
(https://github.com/zitadel/zitadel/pull/9649)
- feat(console): add Actions V2 UI
(https://github.com/zitadel/zitadel/pull/9591)

BREAKING CHANGE
- feat(webkey): migrate to v2beta API
(https://github.com/zitadel/zitadel/pull/9445)
- chore!: remove CockroachDB Support
(https://github.com/zitadel/zitadel/pull/9444)
- feat(actions): migrate to v2beta API
(https://github.com/zitadel/zitadel/pull/9489)

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
Co-authored-by: Stefan Benz <46600784+stebenz@users.noreply.github.com>
Co-authored-by: Silvan <27845747+adlerhurst@users.noreply.github.com>
Co-authored-by: Ramon <mail@conblem.me>
Co-authored-by: Elio Bischof <elio@zitadel.com>
Co-authored-by: Kenta Yamaguchi <56732734+KEY60228@users.noreply.github.com>
Co-authored-by: Harsha Reddy <harsha.reddy@klaviyo.com>
Co-authored-by: Livio Spring <livio@zitadel.com>
Co-authored-by: Max Peintner <max@caos.ch>
Co-authored-by: Iraq <66622793+kkrime@users.noreply.github.com>
Co-authored-by: Florian Forster <florian@zitadel.com>
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Max Peintner <peintnerm@gmail.com>
2025-04-02 16:53:06 +02:00

291 lines
8.4 KiB
Go

package query
import (
"context"
"database/sql"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
"github.com/zitadel/zitadel/internal/zerrors"
)
var (
flowsTriggersTable = table{
name: projection.FlowTriggerTable,
instanceIDCol: projection.FlowInstanceIDCol,
}
FlowsTriggersColumnFlowType = Column{
name: projection.FlowTypeCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnChangeDate = Column{
name: projection.FlowChangeDateCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnSequence = Column{
name: projection.FlowSequenceCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnTriggerType = Column{
name: projection.FlowTriggerTypeCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnResourceOwner = Column{
name: projection.FlowResourceOwnerCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnInstanceID = Column{
name: projection.FlowInstanceIDCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnTriggerSequence = Column{
name: projection.FlowActionTriggerSequenceCol,
table: flowsTriggersTable,
}
FlowsTriggersColumnActionID = Column{
name: projection.FlowActionIDCol,
table: flowsTriggersTable,
}
)
type Flow struct {
ChangeDate time.Time
ResourceOwner string
Sequence uint64
Type domain.FlowType
TriggerActions map[domain.TriggerType][]*Action
}
func (q *Queries) GetFlow(ctx context.Context, flowType domain.FlowType, orgID string) (flow *Flow, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
query, scan := prepareFlowQuery(flowType)
eq := sq.Eq{
FlowsTriggersColumnFlowType.identifier(): flowType,
FlowsTriggersColumnResourceOwner.identifier(): orgID,
FlowsTriggersColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
}
stmt, args, err := query.Where(eq).ToSql()
if err != nil {
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-HBRh3", "Errors.Query.InvalidRequest")
}
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
flow, err = scan(rows)
return err
}, stmt, args...)
return flow, err
}
func (q *Queries) GetActiveActionsByFlowAndTriggerType(ctx context.Context, flowType domain.FlowType, triggerType domain.TriggerType, orgID string) (actions []*Action, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
stmt, scan := prepareTriggerActionsQuery()
eq := sq.Eq{
FlowsTriggersColumnFlowType.identifier(): flowType,
FlowsTriggersColumnTriggerType.identifier(): triggerType,
FlowsTriggersColumnResourceOwner.identifier(): orgID,
FlowsTriggersColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
ActionColumnState.identifier(): domain.ActionStateActive,
}
query, args, err := stmt.Where(eq).ToSql()
if err != nil {
return nil, zerrors.ThrowInternal(err, "QUERY-Dgff3", "Errors.Query.SQLStatement")
}
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
actions, err = scan(rows)
return err
}, query, args...)
return actions, err
}
func (q *Queries) GetFlowTypesOfActionID(ctx context.Context, actionID string) (types []domain.FlowType, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
stmt, scan := prepareFlowTypesQuery()
eq := sq.Eq{
FlowsTriggersColumnActionID.identifier(): actionID,
FlowsTriggersColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
}
query, args, err := stmt.Where(eq).ToSql()
if err != nil {
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-Dh311", "Errors.Query.InvalidRequest")
}
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
types, err = scan(rows)
return err
}, query, args...)
return types, err
}
func prepareFlowTypesQuery() (sq.SelectBuilder, func(*sql.Rows) ([]domain.FlowType, error)) {
return sq.Select(
FlowsTriggersColumnFlowType.identifier(),
).
From(flowsTriggersTable.identifier()).
PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) ([]domain.FlowType, error) {
types := []domain.FlowType{}
for rows.Next() {
var flowType domain.FlowType
err := rows.Scan(
&flowType,
)
if err != nil {
return nil, err
}
types = append(types, flowType)
}
return types, nil
}
}
func prepareTriggerActionsQuery() (sq.SelectBuilder, func(*sql.Rows) ([]*Action, error)) {
return sq.Select(
ActionColumnID.identifier(),
ActionColumnCreationDate.identifier(),
ActionColumnChangeDate.identifier(),
ActionColumnResourceOwner.identifier(),
ActionColumnState.identifier(),
ActionColumnSequence.identifier(),
ActionColumnName.identifier(),
ActionColumnScript.identifier(),
ActionColumnAllowedToFail.identifier(),
ActionColumnTimeout.identifier(),
).
From(flowsTriggersTable.name).
LeftJoin(join(ActionColumnID, FlowsTriggersColumnActionID)).
OrderBy(FlowsTriggersColumnTriggerSequence.identifier()).
PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) ([]*Action, error) {
actions := make([]*Action, 0)
for rows.Next() {
action := new(Action)
err := rows.Scan(
&action.ID,
&action.CreationDate,
&action.ChangeDate,
&action.ResourceOwner,
&action.State,
&action.Sequence,
&action.Name,
&action.Script,
&action.AllowedToFail,
&action.timeout,
)
if err != nil {
return nil, err
}
actions = append(actions, action)
}
if err := rows.Close(); err != nil {
return nil, zerrors.ThrowInternal(err, "QUERY-Df42d", "Errors.Query.CloseRows")
}
return actions, nil
}
}
func prepareFlowQuery(flowType domain.FlowType) (sq.SelectBuilder, func(*sql.Rows) (*Flow, error)) {
return sq.Select(
ActionColumnID.identifier(),
ActionColumnCreationDate.identifier(),
ActionColumnChangeDate.identifier(),
ActionColumnResourceOwner.identifier(),
ActionColumnState.identifier(),
ActionColumnSequence.identifier(),
ActionColumnName.identifier(),
ActionColumnScript.identifier(),
ActionColumnAllowedToFail.identifier(),
ActionColumnTimeout.identifier(),
FlowsTriggersColumnTriggerType.identifier(),
FlowsTriggersColumnTriggerSequence.identifier(),
FlowsTriggersColumnFlowType.identifier(),
FlowsTriggersColumnChangeDate.identifier(),
FlowsTriggersColumnSequence.identifier(),
FlowsTriggersColumnResourceOwner.identifier(),
).
From(flowsTriggersTable.name).
LeftJoin(join(ActionColumnID, FlowsTriggersColumnActionID)).
OrderBy(FlowsTriggersColumnTriggerSequence.identifier()).
PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) (*Flow, error) {
flow := &Flow{
TriggerActions: make(map[domain.TriggerType][]*Action),
Type: flowType,
}
for rows.Next() {
var (
actionID sql.NullString
actionCreationDate sql.NullTime
actionChangeDate sql.NullTime
actionResourceOwner sql.NullString
actionState sql.NullInt32
actionSequence sql.NullInt64
actionName sql.NullString
actionScript sql.NullString
actionAllowedToFail sql.NullBool
actionTimeout sql.NullInt64
triggerType domain.TriggerType
triggerSequence int
)
err := rows.Scan(
&actionID,
&actionCreationDate,
&actionChangeDate,
&actionResourceOwner,
&actionState,
&actionSequence,
&actionName,
&actionScript,
&actionAllowedToFail,
&actionTimeout,
&triggerType,
&triggerSequence,
&flow.Type,
&flow.ChangeDate,
&flow.Sequence,
&flow.ResourceOwner,
)
if err != nil {
return nil, err
}
if !actionID.Valid {
continue
}
flow.TriggerActions[triggerType] = append(flow.TriggerActions[triggerType], &Action{
ID: actionID.String,
CreationDate: actionCreationDate.Time,
ChangeDate: actionChangeDate.Time,
ResourceOwner: actionResourceOwner.String,
State: domain.ActionState(actionState.Int32),
Sequence: uint64(actionSequence.Int64),
Name: actionName.String,
Script: actionScript.String,
AllowedToFail: actionAllowedToFail.Bool,
timeout: time.Duration(actionTimeout.Int64),
})
}
if err := rows.Close(); err != nil {
return nil, zerrors.ThrowInternal(err, "QUERY-Dfbe2", "Errors.Query.CloseRows")
}
return flow, nil
}
}