Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(backend): forbid lower case letter in secret name (#5742)

h3n4l16b750511942
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index 73c1029243..cac8027ed1 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -553,7 +553,7 @@ func (s *DatabaseService) UpdateSecret(ctx context.Context, request *v1pb.Update
return nil, status.Errorf(codes.InvalidArgument, "secret is required")
}
- environmentID, instanceID, databaseName, secretName, err := getEnvironmentInstanceDatabaseIDSecretName(request.Secret.Name)
+ environmentID, instanceID, databaseName, updateSecretName, err := getEnvironmentInstanceDatabaseIDSecretName(request.Secret.Name)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
@@ -599,16 +599,16 @@ func (s *DatabaseService) UpdateSecret(ctx context.Context, request *v1pb.Update
}
var newSecret storepb.SecretItem
- if _, ok := secretsMap[secretName]; !ok {
+ if _, ok := secretsMap[updateSecretName]; !ok {
// If the secret is not existed and allow_missing is false, we will not create it.
if !request.AllowMissing {
- return nil, status.Errorf(codes.NotFound, "secret %q not found", secretName)
+ return nil, status.Errorf(codes.NotFound, "secret %q not found", updateSecretName)
}
- newSecret.Name = secretName
+ newSecret.Name = updateSecretName
newSecret.Value = request.Secret.Value
newSecret.Description = request.Secret.Description
} else {
- oldSecret := secretsMap[secretName]
+ oldSecret := secretsMap[updateSecretName]
newSecret.Name = oldSecret.Name
newSecret.Value = oldSecret.Value
newSecret.Description = oldSecret.Description
@@ -628,7 +628,7 @@ func (s *DatabaseService) UpdateSecret(ctx context.Context, request *v1pb.Update
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
- secretsMap[secretName] = &newSecret
+ secretsMap[updateSecretName] = &newSecret
// Flatten the map to a slice.
var secretItems []*storepb.SecretItem
for _, secret := range secretsMap {
@@ -649,7 +649,7 @@ func (s *DatabaseService) UpdateSecret(ctx context.Context, request *v1pb.Update
// Get the secret from the updated database.
for _, secret := range updatedDatabase.Secrets.Items {
- if secret.Name == secretName {
+ if secret.Name == updateSecretName {
return stripeAndConvertToServiceSecret(secret, updatedDatabase.EnvironmentID, updatedDatabase.InstanceID, updatedDatabase.DatabaseName), nil
}
}
@@ -657,11 +657,12 @@ func (s *DatabaseService) UpdateSecret(ctx context.Context, request *v1pb.Update
}
// DeleteSecret deletes a secret of a database.
-func (s DatabaseService) DeleteSecret(ctx context.Context, request *v1pb.DeleteSecretRequest) (*emptypb.Empty, error) {
+func (s *DatabaseService) DeleteSecret(ctx context.Context, request *v1pb.DeleteSecretRequest) (*emptypb.Empty, error) {
environmentID, instanceID, databaseName, secretName, err := getEnvironmentInstanceDatabaseIDSecretName(request.Name)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
+
environment, err := s.store.GetEnvironmentV2(ctx, &store.FindEnvironmentMessage{
ResourceID: &environmentID,
})
@@ -1360,15 +1361,19 @@ func isSecretValid(secret *storepb.SecretItem) error {
return errors.Errorf("invalid secret name: %s, name must not start with the 'BYTEBASE_' prefix", secret.Name)
}
// Names must not start with a number.
- if unicode.IsNumber(rune(secret.Name[0])) {
+ if unicode.IsDigit(rune(secret.Name[0])) {
return errors.Errorf("invalid secret name: %s, name must not start with a number", secret.Name)
}
- // Names can only contain alphanumeric characters ([a-z], [A-Z], [0-9]) or underscores (_). Spaces are not allowed.
+ // Names can only contain alphanumeric characters ([A-Z], [0-9]) or underscores (_). Spaces are not allowed.
for _, c := range secret.Name {
- if !unicode.IsLetter(c) && !unicode.IsNumber(c) && c != '_' {
+ if !isUpperCaseLetter(c) && !unicode.IsDigit(c) && c != '_' {
return errors.Errorf("invalid secret name: %s, expect [a-z], [A-Z], [0-9], '_', but meet: %v", secret.Name, c)
}
}
return nil
}
+
+func isUpperCaseLetter(c rune) bool {
+ return 'A' <= c && c <= 'Z'
+}
diff --git a/backend/api/v1/database_service_test.go b/backend/api/v1/database_service_test.go
index 73f462b24f..13093b77bd 100644
--- a/backend/api/v1/database_service_test.go
+++ b/backend/api/v1/database_service_test.go
@@ -75,7 +75,7 @@ func TestIsSecretValid(t *testing.T) {
// Disallow empty value.
{
item: &storepb.SecretItem{
- Name: "name",
+ Name: "NAME",
Value: "",
Description: "",
},
@@ -84,7 +84,7 @@ func TestIsSecretValid(t *testing.T) {
// Name cannot start with a number.
{
item: &storepb.SecretItem{
- Name: "1name",
+ Name: "1NAME",
Value: "pwd",
Description: "",
},
@@ -99,10 +99,10 @@ func TestIsSecretValid(t *testing.T) {
},
wantErr: true,
},
- // Names can only contain alphanumeric characters ([a-z], [A-Z], [0-9]) or underscores (_). Spaces are not allowed.
+ // Names can only contain alphanumeric characters ([A-Z], [0-9]) or underscores (_). Spaces are not allowed.
{
item: &storepb.SecretItem{
- Name: "name with space",
+ Name: "NAME WITH SPACE",
Value: "pwd",
Description: "",
},
@@ -110,7 +110,7 @@ func TestIsSecretValid(t *testing.T) {
},
{
item: &storepb.SecretItem{
- Name: "name-with-hyphen",
+ Name: "NAME-WITH-DASH",
Value: "pwd",
Description: "",
},
@@ -118,7 +118,7 @@ func TestIsSecretValid(t *testing.T) {
},
{
item: &storepb.SecretItem{
- Name: "name_with_special_characters_©",
+ Name: "NAME_WITH_SPECIAL_CHARACTER_©",
Value: "pwd",
Description: "",
},
@@ -126,7 +126,31 @@ func TestIsSecretValid(t *testing.T) {
},
{
item: &storepb.SecretItem{
- Name: "name_with_special_characters_normal",
+ Name: "NAME_WITH_SPECIAL_CHARACTER_ç",
+ Value: "pwd",
+ Description: "",
+ },
+ wantErr: true,
+ },
+ {
+ item: &storepb.SecretItem{
+ Name: "NAME_WITH_SPECIAL_CHARACTER_Ⅷ",
+ Value: "pwd",
+ Description: "",
+ },
+ wantErr: true,
+ },
+ {
+ item: &storepb.SecretItem{
+ Name: "NAME_WITH_LOWER_CASE_a",
+ Value: "pwd",
+ Description: "",
+ },
+ wantErr: true,
+ },
+ {
+ item: &storepb.SecretItem{
+ Name: "NORMAL_NAME",
Value: "pwd",
Description: "",
},