feat: projection layer implementation to add Zitadel provider (#12055)

# Which Problems Are Solved

Persisting instance-level Zitadel provider in the projections.

# How the Problems Are Solved
- Defining a new projection table `projections.idp_templates6_zitadel`
and columns for the Zitadel provider
- Adding a reducer `reduceZitadelIDPAdded` to process instance-level
`ZitadelIDPAddedEvent`
- Unit tests 


# Additional Changes
N/A


# Additional Context
- Related to https://github.com/zitadel/zitadel/issues/11823
- Follow-up for PRs https://github.com/zitadel/zitadel/pull/12018,
https://github.com/zitadel/zitadel/pull/12020
This commit is contained in:
Gayathri Vijayan
2026-04-15 07:59:52 +02:00
committed by GitHub
parent 7b9d2b7d17
commit 55a9acf3bf
3 changed files with 249 additions and 0 deletions
+79
View File
@@ -30,6 +30,7 @@ const (
IDPTemplateLDAPTable = IDPTemplateTable + "_" + IDPTemplateLDAPSuffix
IDPTemplateAppleTable = IDPTemplateTable + "_" + IDPTemplateAppleSuffix
IDPTemplateSAMLTable = IDPTemplateTable + "_" + IDPTemplateSAMLSuffix
IDPTemplateZitadelTable = IDPTemplateTable + "_" + IDPTemplateZitadelSuffix
IDPTemplateOAuthSuffix = "oauth2"
IDPTemplateOIDCSuffix = "oidc"
@@ -43,6 +44,7 @@ const (
IDPTemplateLDAPSuffix = "ldap2"
IDPTemplateAppleSuffix = "apple"
IDPTemplateSAMLSuffix = "saml"
IDPTemplateZitadelSuffix = "zitadel"
IDPTemplateIDCol = "id"
IDPTemplateCreationDateCol = "creation_date"
@@ -175,6 +177,14 @@ const (
SAMLNameIDFormatCol = "name_id_format"
SAMLTransientMappingAttributeName = "transient_mapping_attribute_name"
SAMLFederatedLogoutEnabled = "federated_logout_enabled"
ZitadelIDCol = "idp_id"
ZitadelInstanceIDCol = "instance_id"
ZitadelIssuerCol = "issuer"
ZitadelClientIDCol = "client_id"
ZitadelClientSecretCol = "client_secret"
ZitadelScopesCol = "scopes"
ZitadelInstanceRolesInfoCol = "instance_roles_info"
)
type idpTemplateProjection struct{}
@@ -386,6 +396,19 @@ func (*idpTemplateProjection) Init() *old_handler.Check {
IDPTemplateSAMLSuffix,
handler.WithForeignKey(handler.NewForeignKeyOfPublicKeys()),
),
handler.NewSuffixedTable([]*handler.InitColumn{
handler.NewColumn(ZitadelIDCol, handler.ColumnTypeText),
handler.NewColumn(ZitadelInstanceIDCol, handler.ColumnTypeText),
handler.NewColumn(ZitadelIssuerCol, handler.ColumnTypeText),
handler.NewColumn(ZitadelClientIDCol, handler.ColumnTypeText),
handler.NewColumn(ZitadelClientSecretCol, handler.ColumnTypeJSONB),
handler.NewColumn(ZitadelScopesCol, handler.ColumnTypeTextArray, handler.Nullable()),
handler.NewColumn(ZitadelInstanceRolesInfoCol, handler.ColumnTypeJSONB, handler.Nullable()),
},
handler.NewPrimaryKey(ZitadelInstanceIDCol, ZitadelIDCol),
IDPTemplateZitadelSuffix,
handler.WithForeignKey(handler.NewForeignKeyOfPublicKeys()),
),
)
}
@@ -522,6 +545,10 @@ func (p *idpTemplateProjection) Reducers() []handler.AggregateReducer {
Event: instance.SAMLIDPChangedEventType,
Reduce: p.reduceSAMLIDPChanged,
},
{
Event: instance.ZitadelIDPAddedEventType,
Reduce: p.reduceZitadelIDPAdded,
},
{
Event: instance.IDPConfigRemovedEventType,
Reduce: p.reduceIDPConfigRemoved,
@@ -2539,3 +2566,55 @@ func reduceSAMLIDPChangedColumns(idpEvent idp.SAMLIDPChangedEvent) []handler.Col
}
return SAMLCols
}
func (p *idpTemplateProjection) reduceZitadelIDPAdded(event eventstore.Event) (*handler.Statement, error) {
var idpEvent idp.ZitadelIDPAddedEvent
var idpOwnerType domain.IdentityProviderType
switch e := event.(type) {
case *instance.ZitadelIDPAddedEvent:
idpEvent = e.ZitadelIDPAddedEvent
idpOwnerType = domain.IdentityProviderTypeSystem
// todo (@grvijayan): org-level ZitadelIDPAddedEvent in the next PRs
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-ZCpH41", "reduce.wrong.event.type %v", []eventstore.EventType{instance.ZitadelIDPAddedEventType})
}
zitadelIDPCols := []handler.Column{
handler.NewCol(ZitadelIDCol, idpEvent.ID),
handler.NewCol(ZitadelInstanceIDCol, idpEvent.Aggregate().InstanceID),
handler.NewCol(ZitadelIssuerCol, idpEvent.Issuer),
handler.NewCol(ZitadelClientIDCol, idpEvent.ClientID),
handler.NewCol(ZitadelClientSecretCol, idpEvent.ClientSecret),
handler.NewCol(ZitadelScopesCol, database.TextArray[string](idpEvent.Scopes)),
}
if len(idpEvent.InstanceRolesInfo) > 0 {
zitadelIDPCols = append(zitadelIDPCols, handler.NewJSONCol(ZitadelInstanceRolesInfoCol, idpEvent.InstanceRolesInfo))
}
return handler.NewMultiStatement(
&idpEvent,
handler.AddCreateStatement(
[]handler.Column{
handler.NewCol(IDPTemplateIDCol, idpEvent.ID),
handler.NewCol(IDPTemplateCreationDateCol, idpEvent.CreationDate()),
handler.NewCol(IDPTemplateChangeDateCol, idpEvent.CreationDate()),
handler.NewCol(IDPTemplateSequenceCol, idpEvent.Sequence()),
handler.NewCol(IDPTemplateResourceOwnerCol, idpEvent.Aggregate().ResourceOwner),
handler.NewCol(IDPTemplateInstanceIDCol, idpEvent.Aggregate().InstanceID),
handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive),
handler.NewCol(IDPTemplateNameCol, idpEvent.Name),
handler.NewCol(IDPTemplateOwnerTypeCol, idpOwnerType),
handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeZitadel),
handler.NewCol(IDPTemplateIsCreationAllowedCol, idpEvent.IsCreationAllowed),
handler.NewCol(IDPTemplateIsLinkingAllowedCol, idpEvent.IsLinkingAllowed),
handler.NewCol(IDPTemplateIsAutoCreationCol, idpEvent.IsAutoCreation),
handler.NewCol(IDPTemplateIsAutoUpdateCol, idpEvent.IsAutoUpdate),
handler.NewCol(IDPTemplateAutoLinkingCol, idpEvent.AutoLinkingOption),
},
),
handler.AddCreateStatement(
zitadelIDPCols,
handler.WithTableSuffix(IDPTemplateZitadelSuffix),
),
), nil
}