feat: implement instance-level UpdateZitadelProvider (#12378)

# Which Problems Are Solved

This PR implements the instance-level `UpdateZitadelProvider` endpoint,
enabling updates to existing instance Zitadel IdP templates.

# How the Problems Are Solved

- Added `AdminService.UpdateZitadelProvider` handler, converter
- Added the command-layer implementation to update a ZitadelProvider and
push `instance.idp.zitadel.changed` event
- Added projection-layer implementation to persist Zitadel provider
updates into `projections.idp_templates6_zitadel`
- Added unit and integration tests


# Additional Changes
N/A

# Additional Context
Related to https://github.com/zitadel/zitadel/issues/11922
Follow-up for PRs:
- https://github.com/zitadel/zitadel/pull/12018
- https://github.com/zitadel/zitadel/pull/12020
- https://github.com/zitadel/zitadel/pull/12055
- https://github.com/zitadel/zitadel/pull/12056
- https://github.com/zitadel/zitadel/pull/12371

---------

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
This commit is contained in:
Gayathri Vijayan
2026-07-06 10:18:35 +02:00
committed by GitHub
co-authored by Copilot Autofix powered by AI
parent 1beabed8cf
commit 653a968d91
13 changed files with 1327 additions and 16 deletions
+63
View File
@@ -551,6 +551,10 @@ func (p *idpTemplateProjection) Reducers() []handler.AggregateReducer {
Event: instance.ZitadelIDPAddedEventType,
Reduce: p.reduceZitadelIDPAdded,
},
{
Event: instance.ZitadelIDPChangedEventType,
Reduce: p.reduceZitadelIDPChanged,
},
{
Event: instance.IDPConfigRemovedEventType,
Reduce: p.reduceIDPConfigRemoved,
@@ -2630,3 +2634,62 @@ func (p *idpTemplateProjection) reduceZitadelIDPAdded(event eventstore.Event) (*
),
), nil
}
func (p *idpTemplateProjection) reduceZitadelIDPChanged(event eventstore.Event) (*handler.Statement, error) {
var idpEvent idp.ZitadelIDPChangedEvent
switch e := event.(type) {
case *instance.ZitadelIDPChangedEvent:
idpEvent = e.ZitadelIDPChangedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-qjMUfi", "reduce.wrong.event.type %v", []eventstore.EventType{instance.ZitadelIDPChangedEventType})
}
ops := make([]func(eventstore.Event) handler.Exec, 0, 2)
ops = append(ops,
handler.AddUpdateStatement(
reduceIDPChangedTemplateColumns(idpEvent.Name, idpEvent.CreationDate(), idpEvent.Sequence(), idpEvent.OptionChanges),
[]handler.Condition{
handler.NewCond(IDPTemplateIDCol, idpEvent.ID),
handler.NewCond(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID),
},
),
)
zitadelIDPChangedColumns := reduceZitadelIDPChangedColumns(idpEvent)
if len(zitadelIDPChangedColumns) > 0 {
ops = append(ops,
handler.AddUpdateStatement(
zitadelIDPChangedColumns,
[]handler.Condition{
handler.NewCond(ZitadelIDCol, idpEvent.ID),
handler.NewCond(ZitadelInstanceIDCol, idpEvent.Aggregate().InstanceID),
},
handler.WithTableSuffix(IDPTemplateZitadelSuffix),
),
)
}
return handler.NewMultiStatement(
&idpEvent,
ops...,
), nil
}
func reduceZitadelIDPChangedColumns(idpEvent idp.ZitadelIDPChangedEvent) []handler.Column {
zitadelCols := make([]handler.Column, 0, 5)
if idpEvent.ClientID != nil {
zitadelCols = append(zitadelCols, handler.NewCol(ZitadelClientIDCol, *idpEvent.ClientID))
}
if idpEvent.ClientSecret != nil {
zitadelCols = append(zitadelCols, handler.NewCol(ZitadelClientSecretCol, *idpEvent.ClientSecret))
}
if idpEvent.Issuer != nil {
zitadelCols = append(zitadelCols, handler.NewCol(ZitadelIssuerCol, *idpEvent.Issuer))
}
if idpEvent.Scopes != nil {
zitadelCols = append(zitadelCols, handler.NewCol(ZitadelScopesCol, database.TextArray[string](*idpEvent.Scopes)))
}
if idpEvent.InstanceRolesInfo != nil {
zitadelCols = append(zitadelCols, handler.NewJSONCol(ZitadelInstanceRolesInfoCol, *idpEvent.InstanceRolesInfo))
}
return zitadelCols
}
@@ -4808,6 +4808,179 @@ func TestIDPTemplateProjection_reducesZitadel(t *testing.T) {
},
},
},
{
name: "instance reduceZitadelIDPChanged minimal",
args: args{
event: getEvent(
testEvent(
instance.ZitadelIDPChangedEventType,
instance.AggregateType,
[]byte(`{
"id": "idp-id",
"isCreationAllowed": true,
"clientId": "id"
}`),
), eventstore.GenericEventMapper[instance.ZitadelIDPChangedEvent]),
},
reduce: (&idpTemplateProjection{}).reduceZitadelIDPChanged,
want: wantReduce{
aggregateType: eventstore.AggregateType("instance"),
sequence: 15,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: idpTemplateUpdateMinimalStmt,
expectedArgs: []interface{}{
true,
anyArg{},
uint64(15),
"idp-id",
"instance-id",
},
},
{
expectedStmt: "UPDATE projections.idp_templates6_zitadel SET client_id = $1 WHERE (idp_id = $2) AND (instance_id = $3)",
expectedArgs: []interface{}{
"id",
"idp-id",
"instance-id",
},
},
},
},
},
},
{
name: "instance reduceZitadelIDPChanged",
args: args{
event: getEvent(
testEvent(
instance.ZitadelIDPChangedEventType,
instance.AggregateType,
[]byte(`{
"id": "idp-id",
"name": "name",
"issuer": "issuer",
"clientId": "client_id",
"clientSecret": {
"cryptoType": 0,
"algorithm": "RSA-265",
"keyId": "key-id"
},
"scopes": ["profile"],
"instanceRolesInfo": [{
"organizationId": "org3",
"organizationDomain": "org3.com"
}],
"isCreationAllowed": true,
"isLinkingAllowed": true,
"isAutoCreation": true,
"isAutoUpdate": true,
"autoLinkingOption": 1
}`),
), eventstore.GenericEventMapper[instance.ZitadelIDPChangedEvent]),
},
reduce: (&idpTemplateProjection{}).reduceZitadelIDPChanged,
want: wantReduce{
aggregateType: eventstore.AggregateType("instance"),
sequence: 15,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: idpTemplateUpdateStmt,
expectedArgs: []interface{}{
"name",
true,
true,
true,
true,
domain.AutoLinkingOptionUsername,
anyArg{},
uint64(15),
"idp-id",
"instance-id",
},
},
{
expectedStmt: "UPDATE projections.idp_templates6_zitadel SET (client_id, client_secret, issuer, scopes, instance_roles_info) = ($1, $2, $3, $4, $5) WHERE (idp_id = $6) AND (instance_id = $7)",
expectedArgs: []interface{}{
"client_id",
anyArg{},
"issuer",
database.TextArray[string]{"profile"},
[]byte(`[{"organizationId":"org3","organizationDomain":"org3.com"}]`),
"idp-id",
"instance-id",
},
},
},
},
},
},
{
name: "instance reduceZitadelIDPChanged - unset scopes and instance roles info",
args: args{
event: getEvent(
testEvent(
instance.ZitadelIDPChangedEventType,
instance.AggregateType,
[]byte(`{
"id": "idp-id",
"name": "name",
"issuer": "issuer",
"clientId": "client_id",
"clientSecret": {
"cryptoType": 0,
"algorithm": "RSA-265",
"keyId": "key-id"
},
"scopes": [],
"instanceRolesInfo": [],
"isCreationAllowed": true,
"isLinkingAllowed": true,
"isAutoCreation": true,
"isAutoUpdate": true,
"autoLinkingOption": 1
}`),
), eventstore.GenericEventMapper[instance.ZitadelIDPChangedEvent]),
},
reduce: (&idpTemplateProjection{}).reduceZitadelIDPChanged,
want: wantReduce{
aggregateType: eventstore.AggregateType("instance"),
sequence: 15,
executer: &testExecuter{
executions: []execution{
{
expectedStmt: idpTemplateUpdateStmt,
expectedArgs: []interface{}{
"name",
true,
true,
true,
true,
domain.AutoLinkingOptionUsername,
anyArg{},
uint64(15),
"idp-id",
"instance-id",
},
},
{
expectedStmt: "UPDATE projections.idp_templates6_zitadel SET (client_id, client_secret, issuer, scopes, instance_roles_info) = ($1, $2, $3, $4, $5) WHERE (idp_id = $6) AND (instance_id = $7)",
expectedArgs: []interface{}{
"client_id",
anyArg{},
"issuer",
database.TextArray[string]{},
[]byte(`[]`),
"idp-id",
"instance-id",
},
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {