feat: allow managing invite code in secret generators (#12109)

# Which Problems Are Solved

Zitadel exposes the secrets generator configuration through its admin
api. This allows instance admins to manage them on their own and they
can create overwrite the system / runtime defaults (incl. expiration).
This very much needed in multi-instance scenarios such as zitadel.cloud.
Currently the invite code configuration was not manageable through the
API, but only runtime config.

# How the Problems Are Solved

- added the `invite_code` type to the API allowing it to be set and
retrieved.
- added the type to console's management list
- added the type to be stored on instance setup
- change the `GetSecretGenerator` endpoint to fall back to the runtime
config if no config is stored on the instance itself
- ensure the `length` and at least one charset is enabled, return an
error otherwise
- expiry is not enforced, so 0 allows codes with no expiry (current
state)

# Additional Changes

None

# Additional Context

- closes https://github.com/zitadel/zitadel/issues/10474
This commit is contained in:
Livio Spring
2026-07-01 04:53:16 +00:00
committed by GitHub
parent 782133dbc7
commit 1e0b810dca
44 changed files with 351 additions and 62 deletions
@@ -1584,8 +1584,12 @@ func setSecretGeneratorSettingsAttrs(generatorType legacy_domain.SecretGenerator
secretGeneratorSettingsAttrs.OTPEmail = &domain.OTPEmailAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypeInviteCode, legacy_domain.SecretGeneratorTypeSigningKey:
// do nothing as these secret generators are not persisted in the settings
case legacy_domain.SecretGeneratorTypeInviteCode:
secretGeneratorSettingsAttrs.InviteCode = &domain.InviteCodeAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypeSigningKey:
// do nothing as this secret generator is not persisted in the settings
case legacy_domain.SecretGeneratorTypeUnspecified:
return domain.SecretGeneratorSettingsAttributes{}, zerrors.ThrowInvalidArgumentf(nil, "HANDL-2n3fK", "unspecified secret generator type")
default:
+3
View File
@@ -45,6 +45,7 @@ type Queries struct {
zitadelRoles []authz.RoleMapping
multifactors domain.MultifactorConfigs
defaultAuditLogRetention time.Duration
defaultSecretGenerators map[domain.SecretGeneratorType]*crypto.GeneratorConfig
}
func StartQueries(
@@ -62,6 +63,7 @@ func StartQueries(
defaultAuditLogRetention time.Duration,
systemAPIUsers map[string]*authz.SystemAPIUser,
startProjections bool,
secretGeneratorDefaults map[domain.SecretGeneratorType]*crypto.GeneratorConfig,
) (repo *Queries, err error) {
repo = &Queries{
eventstore: es,
@@ -84,6 +86,7 @@ func StartQueries(
},
},
defaultAuditLogRetention: defaultAuditLogRetention,
defaultSecretGenerators: secretGeneratorDefaults,
}
repo.checkPermission = permissionCheck(repo)
+107
View File
@@ -9,6 +9,12 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/zerrors"
)
@@ -70,6 +76,9 @@ var (
"include_symbols",
"count",
}
expectedSecretGeneratorByTypeQuery = regexp.QuoteMeta(prepareSecretGeneratorStmt +
` WHERE projections.secret_generators2.generator_type = $1 AND projections.secret_generators2.instance_id = $2`)
secretGeneratorByTypeInstanceID = "instanceID"
)
func Test_SecretGeneratorsPrepares(t *testing.T) {
@@ -314,3 +323,101 @@ func Test_SecretGeneratorsPrepares(t *testing.T) {
})
}
}
func TestQueries_SecretGeneratorByType(t *testing.T) {
ctx := authz.NewMockContext(secretGeneratorByTypeInstanceID, "orgID", "loginClient")
generatorType := domain.SecretGeneratorTypeInviteCode
queryArgs := []driver.Value{generatorType, secretGeneratorByTypeInstanceID}
inviteCodeDefault := &crypto.GeneratorConfig{
Length: 6,
Expiry: 72 * time.Hour,
IncludeUpperLetters: true,
IncludeDigits: true,
}
tests := []struct {
name string
mock sqlExpectation
defaultSecretGenerators map[domain.SecretGeneratorType]*crypto.GeneratorConfig
want *SecretGenerator
wantErr error
}{
{
name: "found in DB",
mock: mockQuery(expectedSecretGeneratorByTypeQuery, prepareSecretGeneratorCols, []driver.Value{
"agg-id",
domain.SecretGeneratorTypeInviteCode,
testNow,
testNow,
"ro",
uint64(20211108),
6,
72 * time.Hour,
false,
true,
true,
false,
}, queryArgs...),
want: &SecretGenerator{
AggregateID: "agg-id",
GeneratorType: domain.SecretGeneratorTypeInviteCode,
CreationDate: testNow,
ChangeDate: testNow,
ResourceOwner: "ro",
Sequence: 20211108,
Length: 6,
Expiry: 72 * time.Hour,
IncludeLowerLetters: false,
IncludeUpperLetters: true,
IncludeDigits: true,
IncludeSymbols: false,
},
},
{
name: "not found, default exists",
mock: mockQueryErr(expectedSecretGeneratorByTypeQuery, sql.ErrNoRows, queryArgs...),
defaultSecretGenerators: map[domain.SecretGeneratorType]*crypto.GeneratorConfig{
domain.SecretGeneratorTypeInviteCode: inviteCodeDefault,
},
want: &SecretGenerator{
AggregateID: secretGeneratorByTypeInstanceID,
ResourceOwner: secretGeneratorByTypeInstanceID,
GeneratorType: domain.SecretGeneratorTypeInviteCode,
Length: 6,
Expiry: 72 * time.Hour,
IncludeUpperLetters: true,
IncludeDigits: true,
},
},
{
name: "not found, no default",
mock: mockQueryErr(expectedSecretGeneratorByTypeQuery, sql.ErrNoRows, queryArgs...),
defaultSecretGenerators: map[domain.SecretGeneratorType]*crypto.GeneratorConfig{},
wantErr: sql.ErrNoRows,
},
{
name: "DB internal error",
mock: mockQueryErr(expectedSecretGeneratorByTypeQuery, sql.ErrConnDone, queryArgs...),
wantErr: sql.ErrConnDone,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
execMock(t, tt.mock, func(db *sql.DB) {
q := &Queries{
client: &database.DB{
DB: db,
},
defaultSecretGenerators: tt.defaultSecretGenerators,
}
got, err := q.SecretGeneratorByType(ctx, generatorType)
if tt.wantErr != nil {
require.ErrorIs(t, err, tt.wantErr)
} else {
require.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
})
}
}
+23 -2
View File
@@ -139,8 +139,29 @@ func (q *Queries) SecretGeneratorByType(ctx context.Context, generatorType domai
generator, err = scan(row)
return err
}, query, args...)
logging.OnError(err).WithField("type", generatorType).WithField("instance_id", instanceID).Error("secret generator by type")
return generator, err
if err == nil {
return generator, nil
}
if !errors.Is(err, sql.ErrNoRows) {
logging.OnError(err).WithField("type", generatorType).WithField("instance_id", instanceID).Error("secret generator by type")
return nil, err
}
// if the config was not (yet) stored on the database, fall back to the system / runtime config
defaultConfig, ok := q.defaultSecretGenerators[generatorType]
if !ok {
return nil, err // return the initial not found error
}
return &SecretGenerator{
AggregateID: instanceID,
ResourceOwner: instanceID,
GeneratorType: generatorType,
Length: defaultConfig.Length,
Expiry: defaultConfig.Expiry,
IncludeLowerLetters: defaultConfig.IncludeLowerLetters,
IncludeUpperLetters: defaultConfig.IncludeUpperLetters,
IncludeDigits: defaultConfig.IncludeDigits,
IncludeSymbols: defaultConfig.IncludeSymbols,
}, nil
}
func (q *Queries) SearchSecretGenerators(ctx context.Context, queries *SecretGeneratorSearchQueries) (secretGenerators *SecretGenerators, err error) {