Skip to content

Commit 8db0c0b

Browse files
authored
feat: specify allowed symbols in password generation (#9480)
1 parent e1e88af commit 8db0c0b

12 files changed

Lines changed: 119 additions & 43 deletions

apis/apps/v1/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ type PasswordConfig struct {
441441
// +optional
442442
NumSymbols int32 `json:"numSymbols,omitempty"`
443443

444+
// The set of symbols allowed when generating password. If empty, kubeblocks will
445+
// use a default symbol set, which is "!@#&*".
446+
//
447+
// +optional
448+
SymbolCharacters string `json:"symbolCharacters,omitempty"`
449+
444450
// The case of the letters in the password.
445451
//
446452
// +kubebuilder:default=MixedCases

config/crd/bases/apps.kubeblocks.io_clusters.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,6 +4207,11 @@ spec:
42074207
Seed to generate the account's password.
42084208
Cannot be updated.
42094209
type: string
4210+
symbolCharacters:
4211+
description: |-
4212+
The set of symbols allowed when generating password. If empty, kubeblocks will
4213+
use a default symbol set, which is "!@#&*".
4214+
type: string
42104215
type: object
42114216
secretRef:
42124217
description: |-
@@ -11933,6 +11938,11 @@ spec:
1193311938
Seed to generate the account's password.
1193411939
Cannot be updated.
1193511940
type: string
11941+
symbolCharacters:
11942+
description: |-
11943+
The set of symbols allowed when generating password. If empty, kubeblocks will
11944+
use a default symbol set, which is "!@#&*".
11945+
type: string
1193611946
type: object
1193711947
secretRef:
1193811948
description: |-

config/crd/bases/apps.kubeblocks.io_componentdefinitions.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16658,6 +16658,11 @@ spec:
1665816658
Seed to generate the account's password.
1665916659
Cannot be updated.
1666016660
type: string
16661+
symbolCharacters:
16662+
description: |-
16663+
The set of symbols allowed when generating password. If empty, kubeblocks will
16664+
use a default symbol set, which is "!@#&*".
16665+
type: string
1666116666
type: object
1666216667
statement:
1666316668
description: |-

config/crd/bases/apps.kubeblocks.io_components.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4403,6 +4403,11 @@ spec:
44034403
Seed to generate the account's password.
44044404
Cannot be updated.
44054405
type: string
4406+
symbolCharacters:
4407+
description: |-
4408+
The set of symbols allowed when generating password. If empty, kubeblocks will
4409+
use a default symbol set, which is "!@#&*".
4410+
type: string
44064411
type: object
44074412
secretRef:
44084413
description: |-

controllers/apps/cluster/transformer_cluster_sharding_account.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package cluster
2121

2222
import (
2323
"fmt"
24-
"strings"
2524

2625
corev1 "k8s.io/api/core/v1"
2726
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -163,25 +162,12 @@ func (t *clusterShardingAccountTransformer) buildPassword(transCtx *clusterTrans
163162
return nil, fmt.Errorf("failed to restore password for system account %s of shard %s from annotation", account.Name, shardingName)
164163
}
165164
if len(password) == 0 {
166-
password = t.generatePassword(account)
165+
password, err := common.GeneratePasswordByConfig(account.PasswordGenerationPolicy)
166+
return []byte(password), err
167167
}
168168
return password, nil
169169
}
170170

171-
func (t *clusterShardingAccountTransformer) generatePassword(account appsv1.SystemAccount) []byte {
172-
config := account.PasswordGenerationPolicy
173-
passwd, _ := common.GeneratePassword((int)(config.Length), (int)(config.NumDigits), (int)(config.NumSymbols), config.Seed)
174-
switch config.LetterCase {
175-
case appsv1.UpperCases:
176-
passwd = strings.ToUpper(passwd)
177-
case appsv1.LowerCases:
178-
passwd = strings.ToLower(passwd)
179-
case appsv1.MixedCases:
180-
passwd, _ = common.EnsureMixedCase(passwd, config.Seed)
181-
}
182-
return []byte(passwd)
183-
}
184-
185171
func (t *clusterShardingAccountTransformer) newAccountSecretWithPassword(transCtx *clusterTransformContext,
186172
sharding *appsv1.ClusterSharding, accountName string, password []byte) (*corev1.Secret, error) {
187173
var (

controllers/apps/component/transformer_component_account.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ package component
2222
import (
2323
"fmt"
2424
"reflect"
25-
"strings"
2625

2726
"golang.org/x/crypto/bcrypt"
2827
"golang.org/x/exp/maps"
@@ -219,25 +218,12 @@ func (t *componentAccountTransformer) buildPassword(ctx *componentTransformConte
219218
password = []byte(factory.GetRestorePassword(ctx.SynthesizeComponent))
220219
}
221220
if len(password) == 0 {
222-
return t.generatePassword(account), nil
221+
password, err := common.GeneratePasswordByConfig(account.PasswordGenerationPolicy)
222+
return []byte(password), err
223223
}
224224
return password, nil
225225
}
226226

227-
func (t *componentAccountTransformer) generatePassword(account synthesizedSystemAccount) []byte {
228-
config := account.PasswordGenerationPolicy
229-
passwd, _ := common.GeneratePassword((int)(config.Length), (int)(config.NumDigits), (int)(config.NumSymbols), config.Seed)
230-
switch config.LetterCase {
231-
case appsv1.UpperCases:
232-
passwd = strings.ToUpper(passwd)
233-
case appsv1.LowerCases:
234-
passwd = strings.ToLower(passwd)
235-
case appsv1.MixedCases:
236-
passwd, _ = common.EnsureMixedCase(passwd, config.Seed)
237-
}
238-
return []byte(passwd)
239-
}
240-
241227
func (t *componentAccountTransformer) buildAccountSecretWithPassword(ctx *componentTransformContext,
242228
synthesizeComp *component.SynthesizedComponent, account synthesizedSystemAccount, password []byte) (*corev1.Secret, error) {
243229
secretName := constant.GenerateAccountSecretName(synthesizeComp.ClusterName, synthesizeComp.Name, account.Name)

deploy/helm/crds/apps.kubeblocks.io_clusters.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4207,6 +4207,11 @@ spec:
42074207
Seed to generate the account's password.
42084208
Cannot be updated.
42094209
type: string
4210+
symbolCharacters:
4211+
description: |-
4212+
The set of symbols allowed when generating password. If empty, kubeblocks will
4213+
use a default symbol set, which is "!@#&*".
4214+
type: string
42104215
type: object
42114216
secretRef:
42124217
description: |-
@@ -11933,6 +11938,11 @@ spec:
1193311938
Seed to generate the account's password.
1193411939
Cannot be updated.
1193511940
type: string
11941+
symbolCharacters:
11942+
description: |-
11943+
The set of symbols allowed when generating password. If empty, kubeblocks will
11944+
use a default symbol set, which is "!@#&*".
11945+
type: string
1193611946
type: object
1193711947
secretRef:
1193811948
description: |-

deploy/helm/crds/apps.kubeblocks.io_componentdefinitions.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16658,6 +16658,11 @@ spec:
1665816658
Seed to generate the account's password.
1665916659
Cannot be updated.
1666016660
type: string
16661+
symbolCharacters:
16662+
description: |-
16663+
The set of symbols allowed when generating password. If empty, kubeblocks will
16664+
use a default symbol set, which is "!@#&*".
16665+
type: string
1666116666
type: object
1666216667
statement:
1666316668
description: |-

deploy/helm/crds/apps.kubeblocks.io_components.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4403,6 +4403,11 @@ spec:
44034403
Seed to generate the account's password.
44044404
Cannot be updated.
44054405
type: string
4406+
symbolCharacters:
4407+
description: |-
4408+
The set of symbols allowed when generating password. If empty, kubeblocks will
4409+
use a default symbol set, which is "!@#&*".
4410+
type: string
44064411
type: object
44074412
secretRef:
44084413
description: |-

docs/developer_docs/api-reference/cluster.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8634,6 +8634,19 @@ int32
86348634
</tr>
86358635
<tr>
86368636
<td>
8637+
<code>symbolCharacters</code><br/>
8638+
<em>
8639+
string
8640+
</em>
8641+
</td>
8642+
<td>
8643+
<em>(Optional)</em>
8644+
<p>The set of symbols allowed when generating password. If empty, kubeblocks will
8645+
use a default symbol set, which is &ldquo;!@#&amp;*&rdquo;.</p>
8646+
</td>
8647+
</tr>
8648+
<tr>
8649+
<td>
86378650
<code>letterCase</code><br/>
86388651
<em>
86398652
<a href="#apps.kubeblocks.io/v1.LetterCase">

0 commit comments

Comments
 (0)