mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-17 19:05:13 +00:00
feat(settings): add secret generator type to settings RT implementation (#11152)
# Which Problems Are Solved The `SecretGenerators` setting is missing among the instance settings in the new relational model. # How the Problems Are Solved This PR adds: * `SettingTypeSecretGenerator` type to the settings model * `SecretGeneratorSettingsRepository` and `secretGeneratorSettingsJSONChanges` interfaces to set, retrieve, and delete `secret_generator` settings, and corresponding unit tests. * projection reducers to reduce the following events: * `SecretGeneratorAddedEvent` * `SecretGeneratorChangedEvent` * `SecretGeneratorRemovedEvent` * integration tests to test that the events get reduced correctly # Additional Changes N/A # Additional Context - Part of https://github.com/zitadel/zitadel/issues/10207 --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Marco A. <[email protected]>
This commit is contained in:
co-authored by
Copilot
Marco A.
parent
826039c620
commit
3903355d18
@@ -498,6 +498,11 @@ func TestSetLoginSettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -1167,6 +1172,11 @@ func TestSetBrandingSettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -1912,6 +1922,11 @@ func TestSetPasswordComplexitySettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -2300,6 +2315,11 @@ func TestSetPasswordExpirySettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -2695,6 +2715,11 @@ func TestSetLockoutSettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -3092,6 +3117,11 @@ func TestSetSecuritySettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -3489,6 +3519,11 @@ func TestSetDomainSettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -3860,6 +3895,11 @@ func TestSetOrganizationSettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -4227,6 +4267,11 @@ func TestSetNotificationSettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -4672,6 +4717,11 @@ func TestSetLegalAndSupportSettings(t *testing.T) {
|
||||
},
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -4774,3 +4824,325 @@ func TestDeleteLegalAndSupportSettings(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSecretGeneratorSettings(t *testing.T) {
|
||||
tx, rollback := transactionForRollback(t)
|
||||
defer rollback()
|
||||
|
||||
firstInstanceID := createInstance(t, tx)
|
||||
secondInstanceID := createInstance(t, tx)
|
||||
firstOrgID := createOrganization(t, tx, firstInstanceID)
|
||||
secondOrgID := createOrganization(t, tx, secondInstanceID)
|
||||
repo := repository.SecretGeneratorSettingsRepository()
|
||||
|
||||
settings := []*domain.SecretGeneratorSettings{
|
||||
createSecretGeneratorSettings(firstInstanceID, nil),
|
||||
createSecretGeneratorSettings(secondInstanceID, nil),
|
||||
createSecretGeneratorSettings(firstInstanceID, gu.Ptr(firstOrgID)),
|
||||
createSecretGeneratorSettings(secondInstanceID, gu.Ptr(secondOrgID)),
|
||||
}
|
||||
|
||||
for _, setting := range settings {
|
||||
err := repo.Set(t.Context(), tx, setting)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
condition database.Condition
|
||||
want *domain.SecretGeneratorSettings
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "incomplete condition",
|
||||
condition: repo.IDCondition(settings[0].ID),
|
||||
wantErr: database.NewMissingConditionError(repo.IDColumn()),
|
||||
},
|
||||
{
|
||||
name: "not found",
|
||||
condition: repo.PrimaryKeyCondition(firstInstanceID, "nix"),
|
||||
wantErr: database.NewNoRowFoundError(nil),
|
||||
},
|
||||
{
|
||||
name: "too many",
|
||||
condition: repo.InstanceIDCondition(firstInstanceID),
|
||||
wantErr: database.NewMultipleRowsFoundError(nil),
|
||||
},
|
||||
{
|
||||
name: "ok, instance",
|
||||
condition: repo.PrimaryKeyCondition(firstInstanceID, settings[0].ID),
|
||||
want: settings[0],
|
||||
},
|
||||
{
|
||||
name: "ok, organization",
|
||||
condition: repo.PrimaryKeyCondition(secondInstanceID, settings[3].ID),
|
||||
want: settings[3],
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := repo.Get(t.Context(), tx, database.WithCondition(tt.condition))
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
assert.EqualExportedValues(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestListSecretGeneratorSettings(t *testing.T) {
|
||||
tx, rollback := transactionForRollback(t)
|
||||
defer rollback()
|
||||
|
||||
firstInstanceID := createInstance(t, tx)
|
||||
secondInstanceID := createInstance(t, tx)
|
||||
firstOrgID := createOrganization(t, tx, firstInstanceID)
|
||||
secondOrgID := createOrganization(t, tx, secondInstanceID)
|
||||
repo := repository.SecretGeneratorSettingsRepository()
|
||||
|
||||
settings := []*domain.SecretGeneratorSettings{
|
||||
createSecretGeneratorSettings(firstInstanceID, nil),
|
||||
createSecretGeneratorSettings(secondInstanceID, nil),
|
||||
createSecretGeneratorSettings(firstInstanceID, gu.Ptr(firstOrgID)),
|
||||
createSecretGeneratorSettings(secondInstanceID, gu.Ptr(secondOrgID)),
|
||||
}
|
||||
|
||||
for _, setting := range settings {
|
||||
err := repo.Set(t.Context(), tx, setting)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
condition database.Condition
|
||||
want []*domain.SecretGeneratorSettings
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "incomplete condition",
|
||||
condition: repo.OrganizationIDCondition(gu.Ptr(firstOrgID)),
|
||||
wantErr: database.NewMissingConditionError(repo.IDColumn()),
|
||||
},
|
||||
{
|
||||
name: "no results, ok",
|
||||
condition: repo.PrimaryKeyCondition(firstInstanceID, "nix"),
|
||||
want: []*domain.SecretGeneratorSettings{},
|
||||
},
|
||||
{
|
||||
name: "all from instance",
|
||||
condition: repo.InstanceIDCondition(firstInstanceID),
|
||||
want: []*domain.SecretGeneratorSettings{settings[2], settings[0]},
|
||||
},
|
||||
{
|
||||
name: "only from instance",
|
||||
condition: database.And(
|
||||
repo.InstanceIDCondition(firstInstanceID),
|
||||
repo.OrganizationIDCondition(nil),
|
||||
),
|
||||
want: []*domain.SecretGeneratorSettings{settings[0]},
|
||||
},
|
||||
{
|
||||
name: "all from first org",
|
||||
condition: database.And(
|
||||
repo.InstanceIDCondition(firstInstanceID),
|
||||
repo.OrganizationIDCondition(gu.Ptr(firstOrgID)),
|
||||
),
|
||||
want: []*domain.SecretGeneratorSettings{settings[2]},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := repo.List(t.Context(), tx,
|
||||
database.WithCondition(tt.condition),
|
||||
database.WithOrderByAscending(repo.InstanceIDColumn(), repo.OrganizationIDColumn()),
|
||||
)
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
assert.EqualExportedValues(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetSecretGeneratorSettings(t *testing.T) {
|
||||
tx, rollback := transactionForRollback(t)
|
||||
defer rollback()
|
||||
|
||||
instanceID := createInstance(t, tx)
|
||||
orgID := createOrganization(t, tx, instanceID)
|
||||
repo := repository.SecretGeneratorSettingsRepository()
|
||||
|
||||
existingSettings := createSecretGeneratorSettings(instanceID, gu.Ptr(orgID))
|
||||
|
||||
err := repo.Set(t.Context(), tx, existingSettings)
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
settings *domain.SecretGeneratorSettings
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "create instance",
|
||||
settings: createSecretGeneratorSettings(instanceID, nil),
|
||||
},
|
||||
{
|
||||
name: "update organization",
|
||||
settings: createSecretGeneratorSettings(instanceID, gu.Ptr(orgID)),
|
||||
},
|
||||
{
|
||||
name: "non-existing instance",
|
||||
settings: createSecretGeneratorSettings("foo", nil),
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "non-existing org",
|
||||
settings: createSecretGeneratorSettings(instanceID, gu.Ptr("foo")),
|
||||
wantErr: new(database.ForeignKeyError),
|
||||
},
|
||||
{
|
||||
name: "nil settings",
|
||||
settings: nil,
|
||||
wantErr: database.ErrInvalidChangeTarget,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
savepoint, rollback := savepointForRollback(t, tx)
|
||||
defer rollback()
|
||||
err := repo.Set(t.Context(), savepoint, tt.settings)
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteSecretGeneratorSettings(t *testing.T) {
|
||||
tx, rollback := transactionForRollback(t)
|
||||
defer rollback()
|
||||
|
||||
instanceID := createInstance(t, tx)
|
||||
orgID := createOrganization(t, tx, instanceID)
|
||||
repo := repository.SecretGeneratorSettingsRepository()
|
||||
|
||||
existingInstanceSettings := createSecretGeneratorSettings(instanceID, nil)
|
||||
err := repo.Set(t.Context(), tx, existingInstanceSettings)
|
||||
require.NoError(t, err)
|
||||
|
||||
existingOrganizationSettings := createSecretGeneratorSettings(instanceID, gu.Ptr(orgID))
|
||||
err = repo.Set(t.Context(), tx, existingOrganizationSettings)
|
||||
require.NoError(t, err)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
condition database.Condition
|
||||
wantRowsAffected int64
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
name: "incomplete condition",
|
||||
condition: repo.InstanceIDCondition(instanceID),
|
||||
wantRowsAffected: 0,
|
||||
wantErr: database.NewMissingConditionError(repo.IDColumn()),
|
||||
},
|
||||
{
|
||||
name: "not found",
|
||||
condition: repo.UniqueCondition(existingInstanceSettings.InstanceID, gu.Ptr("foo"), domain.SettingTypeSecretGenerator, domain.SettingStateActive),
|
||||
wantRowsAffected: 0,
|
||||
},
|
||||
{
|
||||
name: "delete instance",
|
||||
condition: repo.UniqueCondition(existingInstanceSettings.InstanceID, existingInstanceSettings.OrganizationID, domain.SettingTypeSecretGenerator, domain.SettingStateActive),
|
||||
wantRowsAffected: 1,
|
||||
},
|
||||
{
|
||||
name: "delete instance twice",
|
||||
condition: repo.UniqueCondition(existingInstanceSettings.InstanceID, existingInstanceSettings.OrganizationID, domain.SettingTypeSecretGenerator, domain.SettingStateActive),
|
||||
wantRowsAffected: 0,
|
||||
},
|
||||
{
|
||||
name: "delete organization",
|
||||
condition: repo.UniqueCondition(existingOrganizationSettings.InstanceID, existingOrganizationSettings.OrganizationID, domain.SettingTypeSecretGenerator, domain.SettingStateActive),
|
||||
wantRowsAffected: 1,
|
||||
},
|
||||
{
|
||||
name: "delete organization twice",
|
||||
condition: repo.UniqueCondition(existingOrganizationSettings.InstanceID, existingOrganizationSettings.OrganizationID, domain.SettingTypeSecretGenerator, domain.SettingStateActive),
|
||||
wantRowsAffected: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rowsAffected, err := repo.Delete(t.Context(), tx, tt.condition)
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
assert.Equal(t, tt.wantRowsAffected, rowsAffected)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func createDefaultSecretGeneratorAttrs() domain.SecretGeneratorAttrs {
|
||||
return domain.SecretGeneratorAttrs{
|
||||
Length: gu.Ptr(uint(32)),
|
||||
IncludeUpperLetters: gu.Ptr(true),
|
||||
IncludeLowerLetters: gu.Ptr(true),
|
||||
IncludeDigits: gu.Ptr(true),
|
||||
IncludeSymbols: gu.Ptr(false),
|
||||
}
|
||||
}
|
||||
|
||||
func createDefaultSecretGeneratorAttrsWithExpiry(expiry time.Duration) domain.SecretGeneratorAttrsWithExpiry {
|
||||
return domain.SecretGeneratorAttrsWithExpiry{
|
||||
Expiry: gu.Ptr(expiry),
|
||||
SecretGeneratorAttrs: domain.SecretGeneratorAttrs{
|
||||
Length: gu.Ptr(uint(32)),
|
||||
IncludeUpperLetters: gu.Ptr(true),
|
||||
IncludeLowerLetters: gu.Ptr(true),
|
||||
IncludeDigits: gu.Ptr(true),
|
||||
IncludeSymbols: gu.Ptr(false),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func createDefaultSecretGeneratorSettingsAttributes() domain.SecretGeneratorSettingsAttributes {
|
||||
attrs := createDefaultSecretGeneratorAttrs()
|
||||
oneDay := 24 * time.Hour
|
||||
thirtyMinutes := 30 * time.Minute
|
||||
tenMinutes := 10 * time.Minute
|
||||
|
||||
return domain.SecretGeneratorSettingsAttributes{
|
||||
ClientSecret: &domain.ClientSecretAttributes{
|
||||
SecretGeneratorAttrs: attrs,
|
||||
},
|
||||
InitializeUserCode: &domain.InitializeUserCodeAttributes{
|
||||
SecretGeneratorAttrsWithExpiry: createDefaultSecretGeneratorAttrsWithExpiry(oneDay),
|
||||
},
|
||||
EmailVerificationCode: &domain.EmailVerificationCodeAttributes{
|
||||
SecretGeneratorAttrsWithExpiry: createDefaultSecretGeneratorAttrsWithExpiry(thirtyMinutes),
|
||||
},
|
||||
PhoneVerificationCode: &domain.PhoneVerificationCodeAttributes{
|
||||
SecretGeneratorAttrsWithExpiry: createDefaultSecretGeneratorAttrsWithExpiry(thirtyMinutes),
|
||||
},
|
||||
PasswordVerificationCode: &domain.PasswordVerificationCodeAttributes{
|
||||
SecretGeneratorAttrsWithExpiry: createDefaultSecretGeneratorAttrsWithExpiry(thirtyMinutes),
|
||||
},
|
||||
PasswordlessInitCode: &domain.PasswordlessInitCodeAttributes{
|
||||
SecretGeneratorAttrsWithExpiry: createDefaultSecretGeneratorAttrsWithExpiry(tenMinutes),
|
||||
},
|
||||
DomainVerification: &domain.DomainVerificationAttributes{
|
||||
SecretGeneratorAttrs: attrs,
|
||||
},
|
||||
OTPSMS: &domain.OTPSMSAttributes{
|
||||
SecretGeneratorAttrsWithExpiry: createDefaultSecretGeneratorAttrsWithExpiry(thirtyMinutes),
|
||||
},
|
||||
OTPEmail: &domain.OTPEmailAttributes{
|
||||
SecretGeneratorAttrsWithExpiry: createDefaultSecretGeneratorAttrsWithExpiry(thirtyMinutes),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func createSecretGeneratorSettings(instanceID string, organizationID *string) *domain.SecretGeneratorSettings {
|
||||
return &domain.SecretGeneratorSettings{
|
||||
Settings: domain.Settings{
|
||||
InstanceID: instanceID,
|
||||
OrganizationID: organizationID,
|
||||
},
|
||||
SecretGeneratorSettingsAttributes: createDefaultSecretGeneratorSettingsAttributes(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user