diff --git a/backend/v3/domain/id_provider.go b/backend/v3/domain/id_provider.go index 79d39a5a21..7941246702 100644 --- a/backend/v3/domain/id_provider.go +++ b/backend/v3/domain/id_provider.go @@ -19,9 +19,9 @@ const ( IDPTypeLDAP IDPTypeAzure IDPTypeGithub - IDPTypeGitHubEnterprise - IDPTypeGitLab - IDPTypeGitLabSelfHosted + IDPTypeGithubEnterprise + IDPTypeGitlab + IDPTypeGitlabSelfHosted IDPTypeGoogle IDPTypeApple IDPTypeSAML @@ -164,6 +164,20 @@ type IDPGithub struct { Github } +type GithubEnterprise struct { + ClientID string `json:"clientId,omitempty"` + ClientSecret *crypto.CryptoValue `json:"clientSecret,omitempty"` + AuthorizationEndpoint string `json:"authorizationEndpoint,omitempty"` + TokenEndpoint string `json:"tokenEndpoint,omitempty"` + UserEndpoint string `json:"userEndpoint,omitempty"` + Scopes []string `json:"scopes,omitempty"` +} + +type IDPGithubEnterprise struct { + *IdentityProvider + GithubEnterprise +} + // IDPIdentifierCondition is used to help specify a single identity_provider, // it will either be used as the identity_provider ID or identity_provider name, // as identity_provider can be identified either using (instanceID + OrgID + ID) OR (instanceID + OrgID + name) @@ -240,4 +254,5 @@ type IDProviderRepository interface { GetOAzureAD(ctx context.Context, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPOAzureAD, error) GetGoogle(ctx context.Context, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPGoogle, error) GetGithub(ctx context.Context, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPGithub, error) + GetGithubEnterprise(ctx context.Context, id IDPIdentifierCondition, instanceID string, orgID *string) (*IDPGithubEnterprise, error) } diff --git a/backend/v3/domain/idptype_enumer.go b/backend/v3/domain/idptype_enumer.go index 4f08382173..de2d9d42ed 100644 --- a/backend/v3/domain/idptype_enumer.go +++ b/backend/v3/domain/idptype_enumer.go @@ -31,15 +31,15 @@ func _IDPTypeNoOp() { _ = x[IDPTypeLDAP-(4)] _ = x[IDPTypeAzure-(5)] _ = x[IDPTypeGithub-(6)] - _ = x[IDPTypeGitHubEnterprise-(7)] - _ = x[IDPTypeGitLab-(8)] - _ = x[IDPTypeGitLabSelfHosted-(9)] + _ = x[IDPTypeGithubEnterprise-(7)] + _ = x[IDPTypeGitlab-(8)] + _ = x[IDPTypeGitlabSelfHosted-(9)] _ = x[IDPTypeGoogle-(10)] _ = x[IDPTypeApple-(11)] _ = x[IDPTypeSAML-(12)] } -var _IDPTypeValues = []IDPType{IDPTypeUnspecified, IDPTypeOIDC, IDPTypeJWT, IDPTypeOAuth, IDPTypeLDAP, IDPTypeAzure, IDPTypeGithub, IDPTypeGitHubEnterprise, IDPTypeGitLab, IDPTypeGitLabSelfHosted, IDPTypeGoogle, IDPTypeApple, IDPTypeSAML} +var _IDPTypeValues = []IDPType{IDPTypeUnspecified, IDPTypeOIDC, IDPTypeJWT, IDPTypeOAuth, IDPTypeLDAP, IDPTypeAzure, IDPTypeGithub, IDPTypeGithubEnterprise, IDPTypeGitlab, IDPTypeGitlabSelfHosted, IDPTypeGoogle, IDPTypeApple, IDPTypeSAML} var _IDPTypeNameToValueMap = map[string]IDPType{ _IDPTypeName[0:11]: IDPTypeUnspecified, @@ -56,12 +56,12 @@ var _IDPTypeNameToValueMap = map[string]IDPType{ _IDPTypeLowerName[27:32]: IDPTypeAzure, _IDPTypeName[32:38]: IDPTypeGithub, _IDPTypeLowerName[32:38]: IDPTypeGithub, - _IDPTypeName[38:54]: IDPTypeGitHubEnterprise, - _IDPTypeLowerName[38:54]: IDPTypeGitHubEnterprise, - _IDPTypeName[54:60]: IDPTypeGitLab, - _IDPTypeLowerName[54:60]: IDPTypeGitLab, - _IDPTypeName[60:76]: IDPTypeGitLabSelfHosted, - _IDPTypeLowerName[60:76]: IDPTypeGitLabSelfHosted, + _IDPTypeName[38:54]: IDPTypeGithubEnterprise, + _IDPTypeLowerName[38:54]: IDPTypeGithubEnterprise, + _IDPTypeName[54:60]: IDPTypeGitlab, + _IDPTypeLowerName[54:60]: IDPTypeGitlab, + _IDPTypeName[60:76]: IDPTypeGitlabSelfHosted, + _IDPTypeLowerName[60:76]: IDPTypeGitlabSelfHosted, _IDPTypeName[76:82]: IDPTypeGoogle, _IDPTypeLowerName[76:82]: IDPTypeGoogle, _IDPTypeName[82:87]: IDPTypeApple, diff --git a/backend/v3/storage/database/dialect/postgres/migration/003_identity_providers_table/up.sql b/backend/v3/storage/database/dialect/postgres/migration/003_identity_providers_table/up.sql index bdcd033604..c4fd52c886 100644 --- a/backend/v3/storage/database/dialect/postgres/migration/003_identity_providers_table/up.sql +++ b/backend/v3/storage/database/dialect/postgres/migration/003_identity_providers_table/up.sql @@ -10,6 +10,7 @@ CREATE TYPE zitadel.idp_type AS ENUM ( 'saml', 'ldap', 'github', + 'githubenterprise', 'azure', 'google', 'microsoft', diff --git a/backend/v3/storage/database/events_testing/id_provider_test.go b/backend/v3/storage/database/events_testing/id_provider_test.go index 9e3f822599..eb6db3e4ec 100644 --- a/backend/v3/storage/database/events_testing/id_provider_test.go +++ b/backend/v3/storage/database/events_testing/id_provider_test.go @@ -528,7 +528,6 @@ func TestServer_TestIDProviderReduces(t *testing.T) { assert.Equal(t, "authoizationEndpoint", oauth.AuthorizationEndpoint) assert.Equal(t, "tokenEndpoint", oauth.TokenEndpoint) assert.Equal(t, "userEndpoint", oauth.UserEndpoint) - assert.Equal(t, "userEndpoint", oauth.UserEndpoint) assert.Equal(t, []string{"scope"}, oauth.Scopes) assert.Equal(t, false, oauth.AllowLinking) assert.Equal(t, false, oauth.AllowCreation) @@ -673,7 +672,6 @@ func TestServer_TestIDProviderReduces(t *testing.T) { // assert.Equal(t, "authoizationEndpoint", oidc.AuthorizationEndpoint) // assert.Equal(t, "tokenEndpoint", oidc.TokenEndpoint) // assert.Equal(t, "userEndpoint", oidc.UserEndpoint) - // assert.Equal(t, "userEndpoint", oidc.UserEndpoint) assert.Equal(t, []string{"scope"}, oidc.Scopes) assert.Equal(t, "issuer", oidc.Issuer) assert.Equal(t, false, oidc.IsIDTokenMapping) @@ -1312,4 +1310,138 @@ func TestServer_TestIDProviderReduces(t *testing.T) { assert.WithinRange(t, updateGithub.UpdatedAt, beforeCreate, afterCreate) }, retryDuration, tick) }) + + t.Run("test instance idp github enterprise added reduces", func(t *testing.T) { + name := gofakeit.Name() + + // add github enterprise + beforeCreate := time.Now() + addGithubEnterprise, err := AdminClient.AddGitHubEnterpriseServerProvider(CTX, &admin.AddGitHubEnterpriseServerProviderRequest{ + Name: name, + ClientId: "clientId", + ClientSecret: "clientSecret", + AuthorizationEndpoint: "authoizationEndpoint", + TokenEndpoint: "tokenEndpoint", + UserEndpoint: "userEndpoint", + Scopes: []string{"scope"}, + ProviderOptions: &idp_grpc.Options{ + IsLinkingAllowed: false, + IsCreationAllowed: false, + IsAutoCreation: false, + IsAutoUpdate: false, + AutoLinking: idp.AutoLinkingOption_AUTO_LINKING_OPTION_EMAIL, + }, + }) + afterCreate := time.Now() + require.NoError(t, err) + + idpRepo := repository.IDProviderRepository(pool) + + // check values for github enterprise + retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Second*5) + assert.EventuallyWithT(t, func(t *assert.CollectT) { + githubEnterprise, err := idpRepo.GetGithubEnterprise(CTX, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, nil) + require.NoError(t, err) + + // event instance.idp.github_enterprise.added + // idp + assert.Equal(t, addGithubEnterprise.Id, githubEnterprise.ID) + assert.Equal(t, name, githubEnterprise.Name) + + assert.Equal(t, domain.IDPTypeGithubEnterprise.String(), githubEnterprise.Type) + assert.Equal(t, "clientId", githubEnterprise.ClientID) + assert.NotNil(t, githubEnterprise.ClientSecret) + assert.Equal(t, "authoizationEndpoint", githubEnterprise.AuthorizationEndpoint) + assert.Equal(t, "tokenEndpoint", githubEnterprise.TokenEndpoint) + assert.Equal(t, "userEndpoint", githubEnterprise.UserEndpoint) + assert.Equal(t, []string{"scope"}, githubEnterprise.Scopes) + assert.Equal(t, false, githubEnterprise.AllowLinking) + assert.Equal(t, false, githubEnterprise.AllowCreation) + assert.Equal(t, false, githubEnterprise.AllowAutoUpdate) + assert.Equal(t, domain.IDPAutoLinkingOptionEmail.String(), githubEnterprise.AllowAutoLinking) + assert.WithinRange(t, githubEnterprise.CreatedAt, beforeCreate, afterCreate) + assert.WithinRange(t, githubEnterprise.UpdatedAt, beforeCreate, afterCreate) + }, retryDuration, tick) + }) + + t.Run("test instance idp github enterprise changed reduces", func(t *testing.T) { + name := gofakeit.Name() + + // add github enterprise + addGithubEnterprise, err := AdminClient.AddGitHubEnterpriseServerProvider(CTX, &admin.AddGitHubEnterpriseServerProviderRequest{ + Name: name, + ClientId: "clientId", + ClientSecret: "clientSecret", + AuthorizationEndpoint: "authoizationEndpoint", + TokenEndpoint: "tokenEndpoint", + UserEndpoint: "userEndpoint", + Scopes: []string{"scope"}, + ProviderOptions: &idp_grpc.Options{ + IsLinkingAllowed: true, + IsCreationAllowed: true, + IsAutoCreation: true, + IsAutoUpdate: true, + AutoLinking: idp.AutoLinkingOption_AUTO_LINKING_OPTION_USERNAME, + }, + }) + require.NoError(t, err) + + idpRepo := repository.IDProviderRepository(pool) + + var githubEnterprise *domain.IDPGithubEnterprise + retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Second*5) + assert.EventuallyWithT(t, func(t *assert.CollectT) { + githubEnterprise, err = idpRepo.GetGithubEnterprise(CTX, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, nil) + require.NoError(t, err) + assert.Equal(t, addGithubEnterprise.Id, githubEnterprise.ID) + }, retryDuration, tick) + + name = "new_" + name + // change github enterprise + beforeCreate := time.Now() + _, err = AdminClient.UpdateGitHubEnterpriseServerProvider(CTX, &admin.UpdateGitHubEnterpriseServerProviderRequest{ + Id: addGithubEnterprise.Id, + Name: name, + ClientId: "new_clientId", + ClientSecret: "new_clientSecret", + AuthorizationEndpoint: "new_authoizationEndpoint", + TokenEndpoint: "new_tokenEndpoint", + UserEndpoint: "new_userEndpoint", + Scopes: []string{"new_scope"}, + ProviderOptions: &idp_grpc.Options{ + IsLinkingAllowed: false, + IsCreationAllowed: false, + IsAutoCreation: false, + IsAutoUpdate: false, + AutoLinking: idp.AutoLinkingOption_AUTO_LINKING_OPTION_EMAIL, + }, + }) + afterCreate := time.Now() + require.NoError(t, err) + + // check values for azure + retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Second*5) + assert.EventuallyWithT(t, func(t *assert.CollectT) { + updateGithubEnterprise, err := idpRepo.GetGithubEnterprise(CTX, idpRepo.IDCondition(addGithubEnterprise.Id), instanceID, nil) + require.NoError(t, err) + + // event instance.idp.github_enterprise.changed + // idp + assert.Equal(t, addGithubEnterprise.Id, updateGithubEnterprise.ID) + assert.Equal(t, name, updateGithubEnterprise.Name) + + assert.Equal(t, domain.IDPTypeGithubEnterprise.String(), updateGithubEnterprise.Type) + assert.Equal(t, "new_clientId", updateGithubEnterprise.ClientID) + assert.NotNil(t, updateGithubEnterprise.ClientSecret) + assert.Equal(t, "new_authoizationEndpoint", updateGithubEnterprise.AuthorizationEndpoint) + assert.Equal(t, "new_tokenEndpoint", updateGithubEnterprise.TokenEndpoint) + assert.Equal(t, "new_userEndpoint", updateGithubEnterprise.UserEndpoint) + assert.Equal(t, []string{"new_scope"}, updateGithubEnterprise.Scopes) + assert.Equal(t, false, updateGithubEnterprise.AllowLinking) + assert.Equal(t, false, updateGithubEnterprise.AllowCreation) + assert.Equal(t, false, updateGithubEnterprise.AllowAutoUpdate) + assert.Equal(t, domain.IDPAutoLinkingOptionEmail.String(), updateGithubEnterprise.AllowAutoLinking) + assert.WithinRange(t, updateGithubEnterprise.UpdatedAt, beforeCreate, afterCreate) + }, retryDuration, tick) + }) } diff --git a/backend/v3/storage/database/repository/id_provider.go b/backend/v3/storage/database/repository/id_provider.go index c2090f5156..1b0bf33373 100644 --- a/backend/v3/storage/database/repository/id_provider.go +++ b/backend/v3/storage/database/repository/id_provider.go @@ -251,6 +251,28 @@ func (i *idProvider) GetGithub(ctx context.Context, id domain.IDPIdentifierCondi return idpGithub, nil } +func (i *idProvider) GetGithubEnterprise(ctx context.Context, id domain.IDPIdentifierCondition, instnaceID string, orgID *string) (*domain.IDPGithubEnterprise, error) { + idpGithubEnterprise := &domain.IDPGithubEnterprise{} + var err error + + idpGithubEnterprise.IdentityProvider, err = i.Get(ctx, id, instnaceID, orgID) + if err != nil { + return nil, err + } + + if idpGithubEnterprise.Type != domain.IDPTypeGithubEnterprise.String() { + // TODO + return nil, errors.New("WRONG TYPE") + } + + err = json.Unmarshal([]byte(*idpGithubEnterprise.Payload), idpGithubEnterprise) + if err != nil { + return nil, err + } + + return idpGithubEnterprise, nil +} + // ------------------------------------------------------------- // columns // ------------------------------------------------------------- diff --git a/internal/query/projection/idp_template_relational.go b/internal/query/projection/idp_template_relational.go index cc73dc0912..ea279ff0e0 100644 --- a/internal/query/projection/idp_template_relational.go +++ b/internal/query/projection/idp_template_relational.go @@ -3,7 +3,6 @@ package projection import ( "context" "encoding/json" - "fmt" "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/postgres" "github.com/zitadel/zitadel/backend/v3/storage/database/repository" @@ -124,14 +123,14 @@ func (p *idpTemplateRelationalProjection) Reducers() []handler.AggregateReducer Event: instance.GitHubIDPChangedEventType, Reduce: p.reduceGitHubIDPRelationalChanged, }, - // { - // Event: instance.GitHubEnterpriseIDPAddedEventType, - // Reduce: p.reduceGitHubEnterpriseIDPAdded, - // }, - // { - // Event: instance.GitHubEnterpriseIDPChangedEventType, - // Reduce: p.reduceGitHubEnterpriseIDPChanged, - // }, + { + Event: instance.GitHubEnterpriseIDPAddedEventType, + Reduce: p.reduceGitHubEnterpriseIDPRelationalAdded, + }, + { + Event: instance.GitHubEnterpriseIDPChangedEventType, + Reduce: p.reduceGitHubEnterpriseIDPRelationalChanged, + }, // { // Event: instance.GitLabIDPAddedEventType, // Reduce: p.reduceGitLabIDPAdded, @@ -1190,57 +1189,6 @@ func (p *idpTemplateRelationalProjection) reduceGitHubIDPRelationalAdded(event e // ), nil } -// func (p *idpTemplateProjection) reduceGitHubEnterpriseIDPAdded(event eventstore.Event) (*handler.Statement, error) { -// var idpEvent idp.GitHubEnterpriseIDPAddedEvent -// var idpOwnerType domain.IdentityProviderType -// switch e := event.(type) { -// case *org.GitHubEnterpriseIDPAddedEvent: -// idpEvent = e.GitHubEnterpriseIDPAddedEvent -// idpOwnerType = domain.IdentityProviderTypeOrg -// case *instance.GitHubEnterpriseIDPAddedEvent: -// idpEvent = e.GitHubEnterpriseIDPAddedEvent -// idpOwnerType = domain.IdentityProviderTypeSystem -// default: -// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Sf3g2a", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitHubEnterpriseIDPAddedEventType, instance.GitHubEnterpriseIDPAddedEventType}) -// } - -// 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.IDPTypeGitHubEnterprise), -// 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( -// []handler.Column{ -// handler.NewCol(GitHubEnterpriseIDCol, idpEvent.ID), -// handler.NewCol(GitHubEnterpriseInstanceIDCol, idpEvent.Aggregate().InstanceID), -// handler.NewCol(GitHubEnterpriseClientIDCol, idpEvent.ClientID), -// handler.NewCol(GitHubEnterpriseClientSecretCol, idpEvent.ClientSecret), -// handler.NewCol(GitHubEnterpriseAuthorizationEndpointCol, idpEvent.AuthorizationEndpoint), -// handler.NewCol(GitHubEnterpriseTokenEndpointCol, idpEvent.TokenEndpoint), -// handler.NewCol(GitHubEnterpriseUserEndpointCol, idpEvent.UserEndpoint), -// handler.NewCol(GitHubEnterpriseScopesCol, database.TextArray[string](idpEvent.Scopes)), -// }, -// handler.WithTableSuffix(IDPTemplateGitHubEnterpriseSuffix), -// ), -// ), nil -// } - func (p *idpTemplateRelationalProjection) reduceGitHubIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { // var idpEvent idp.GitHubIDPChangedEvent // switch e := event.(type) { @@ -1275,8 +1223,105 @@ func (p *idpTemplateRelationalProjection) reduceGitHubIDPRelationalChanged(event columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payload)) } - fmt.Printf("@@ >>>>>>>>>>>>>>>>>>>>>>>>>>>> *e.Name = %+v\n", *e.Name) - fmt.Println("@@ >>>>>>>>>>>>>>>>>>>>>>>>>>>> UPDATE GITHUB") + return handler.NewMultiStatement( + e, + handler.AddUpdateStatement( + columns, + []handler.Condition{ + handler.NewCond(IDPTemplateIDCol, e.ID), + handler.NewCond(IDPTemplateInstanceIDCol, e.Aggregate().InstanceID), + }, + ), + ), nil +} + +func (p *idpTemplateRelationalProjection) reduceGitHubEnterpriseIDPRelationalAdded(event eventstore.Event) (*handler.Statement, error) { + // var idpEvent idp.GitHubEnterpriseIDPAddedEvent + // var idpOwnerType domain.IdentityProviderType + // switch e := event.(type) { + // case *org.GitHubEnterpriseIDPAddedEvent: + // idpEvent = e.GitHubEnterpriseIDPAddedEvent + // idpOwnerType = domain.IdentityProviderTypeOrg + // case *instance.GitHubEnterpriseIDPAddedEvent: + // idpEvent = e.GitHubEnterpriseIDPAddedEvent + // idpOwnerType = domain.IdentityProviderTypeSystem + // default: + // return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Sf3g2a", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitHubEnterpriseIDPAddedEventType, instance.GitHubEnterpriseIDPAddedEventType}) + // } + + e, ok := event.(*instance.GitHubEnterpriseIDPAddedEvent) + if !ok { + return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Sf3g2a", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitHubEnterpriseIDPAddedEventType, instance.GitHubEnterpriseIDPAddedEventType}) + } + + githubEnterprise := domain.GithubEnterprise{ + ClientID: e.ClientID, + ClientSecret: e.ClientSecret, + AuthorizationEndpoint: e.AuthorizationEndpoint, + TokenEndpoint: e.TokenEndpoint, + UserEndpoint: e.UserEndpoint, + Scopes: e.Scopes, + } + + payload, err := json.Marshal(githubEnterprise) + if err != nil { + return nil, err + } + + return handler.NewMultiStatement( + e, + handler.AddCreateStatement( + []handler.Column{ + handler.NewCol(IDPTemplateIDCol, e.ID), + handler.NewCol(IDPTemplateInstanceIDCol, e.Aggregate().InstanceID), + handler.NewCol(IDPTemplateStateCol, domain.IDPStateActive.String()), + handler.NewCol(IDPTemplateNameCol, e.Name), + handler.NewCol(IDPTemplateTypeCol, domain.IDPTypeGithubEnterprise.String()), + handler.NewCol(IDPRelationalAllowCreationCol, e.IsCreationAllowed), + handler.NewCol(IDPRelationalAllowLinkingCol, e.IsLinkingAllowed), + handler.NewCol(IDPRelationalAllowAutoCreationCol, e.IsAutoCreation), + handler.NewCol(IDPRelationalAllowAutoUpdateCol, e.IsAutoUpdate), + handler.NewCol(IDPRelationalAllowAutoLinkingCol, domain.IDPAutoLinkingOption(e.AutoLinkingOption).String()), + handler.NewCol(IDPRelationalPayloadCol, payload), + handler.NewCol(CreatedAt, e.CreationDate()), + }, + ), + ), nil +} + +func (p *idpTemplateRelationalProjection) reduceGitHubEnterpriseIDPRelationalChanged(event eventstore.Event) (*handler.Statement, error) { + // var idpEvent idp.GitHubEnterpriseIDPChangedEvent + // switch e := event.(type) { + // case *org.GitHubEnterpriseIDPChangedEvent: + // idpEvent = e.GitHubEnterpriseIDPChangedEvent + // case *instance.GitHubEnterpriseIDPChangedEvent: + // idpEvent = e.GitHubEnterpriseIDPChangedEvent + // default: + // return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-SDg3g", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitHubEnterpriseIDPChangedEventType, instance.GitHubEnterpriseIDPChangedEventType}) + // } + + e, ok := event.(*instance.GitHubEnterpriseIDPChangedEvent) + if !ok { + return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-SDg3g", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitHubEnterpriseIDPChangedEventType, instance.GitHubEnterpriseIDPChangedEventType}) + } + + githubEnterprise, err := p.idpRepo.GetGithubEnterprise(context.Background(), p.idpRepo.IDCondition(e.ID), e.Agg.InstanceID, nil) + if err != nil { + return nil, err + } + + columns := make([]handler.Column, 0, 7) + reduceIDPRelationalChangedTemplateColumns(e.Name, e.OptionChanges, &columns) + + payload := &githubEnterprise.GithubEnterprise + payloadChanged := reduceGitHubEnterpriseIDPRelationalChangedColumns(payload, &e.GitHubEnterpriseIDPChangedEvent) + if payloadChanged { + payload, err := json.Marshal(payload) + if err != nil { + return nil, err + } + columns = append(columns, handler.NewCol(IDPRelationalPayloadCol, payload)) + } return handler.NewMultiStatement( e, @@ -1299,16 +1344,15 @@ func (p *idpTemplateRelationalProjection) reduceGitHubIDPRelationalChanged(event // }, // ), // ) - // githubCols := reduceGitHubIDPChangedColumns(idpEvent) // if len(githubCols) > 0 { // ops = append(ops, // handler.AddUpdateStatement( // githubCols, // []handler.Condition{ - // handler.NewCond(GitHubIDCol, idpEvent.ID), - // handler.NewCond(GitHubInstanceIDCol, idpEvent.Aggregate().InstanceID), + // handler.NewCond(GitHubEnterpriseIDCol, idpEvent.ID), + // handler.NewCond(GitHubEnterpriseInstanceIDCol, idpEvent.Aggregate().InstanceID), // }, - // handler.WithTableSuffix(IDPTemplateGitHubSuffix), + // handler.WithTableSuffix(IDPTemplateGitHubEnterpriseSuffix), // ), // ) // } @@ -1319,47 +1363,6 @@ func (p *idpTemplateRelationalProjection) reduceGitHubIDPRelationalChanged(event // ), nil } -// func (p *idpTemplateProjection) reduceGitHubEnterpriseIDPChanged(event eventstore.Event) (*handler.Statement, error) { -// var idpEvent idp.GitHubEnterpriseIDPChangedEvent -// switch e := event.(type) { -// case *org.GitHubEnterpriseIDPChangedEvent: -// idpEvent = e.GitHubEnterpriseIDPChangedEvent -// case *instance.GitHubEnterpriseIDPChangedEvent: -// idpEvent = e.GitHubEnterpriseIDPChangedEvent -// default: -// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-SDg3g", "reduce.wrong.event.type %v", []eventstore.EventType{org.GitHubEnterpriseIDPChangedEventType, instance.GitHubEnterpriseIDPChangedEventType}) -// } - -// 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), -// }, -// ), -// ) -// githubCols := reduceGitHubEnterpriseIDPChangedColumns(idpEvent) -// if len(githubCols) > 0 { -// ops = append(ops, -// handler.AddUpdateStatement( -// githubCols, -// []handler.Condition{ -// handler.NewCond(GitHubEnterpriseIDCol, idpEvent.ID), -// handler.NewCond(GitHubEnterpriseInstanceIDCol, idpEvent.Aggregate().InstanceID), -// }, -// handler.WithTableSuffix(IDPTemplateGitHubEnterpriseSuffix), -// ), -// ) -// } - -// return handler.NewMultiStatement( -// &idpEvent, -// ops..., -// ), nil -// } - // func (p *idpTemplateProjection) reduceGitLabIDPAdded(event eventstore.Event) (*handler.Statement, error) { // var idpEvent idp.GitLabIDPAddedEvent // var idpOwnerType domain.IdentityProviderType @@ -2452,3 +2455,32 @@ func reduceGitHubIDPRelationalChangedColumns(payload *domain.Github, idpEvent *i } return payloadChange } + +func reduceGitHubEnterpriseIDPRelationalChangedColumns(payload *domain.GithubEnterprise, idpEvent *idp.GitHubEnterpriseIDPChangedEvent) bool { + payloadChange := false + if idpEvent.ClientID != nil { + payloadChange = true + payload.ClientID = *idpEvent.ClientID + } + if idpEvent.ClientSecret != nil { + payloadChange = true + payload.ClientSecret = idpEvent.ClientSecret + } + if idpEvent.AuthorizationEndpoint != nil { + payloadChange = true + payload.AuthorizationEndpoint = *idpEvent.AuthorizationEndpoint + } + if idpEvent.TokenEndpoint != nil { + payloadChange = true + payload.TokenEndpoint = *idpEvent.TokenEndpoint + } + if idpEvent.UserEndpoint != nil { + payloadChange = true + payload.UserEndpoint = *idpEvent.UserEndpoint + } + if idpEvent.Scopes != nil { + payloadChange = true + payload.Scopes = idpEvent.Scopes + } + return payloadChange +}