Skip to content

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

feat: quote meta in tenant template fixed part (#1282)

Cluasae2e05af608a
diff --git a/api/project.go b/api/project.go
index 5ed18613da..49c9ceb03f 100644
--- a/api/project.go
+++ b/api/project.go
@@ -171,7 +171,7 @@ var (
// ValidateRepositoryFilePathTemplate validates the repository file path template.
func ValidateRepositoryFilePathTemplate(filePathTemplate string, tenantMode ProjectTenantMode) error {
- tokens := getTemplateTokens(filePathTemplate)
+ tokens, _ := getTemplateTokens(filePathTemplate)
tokenMap := make(map[string]bool)
for _, token := range tokens {
tokenMap[token] = true
@@ -202,7 +202,7 @@ func ValidateRepositorySchemaPathTemplate(schemaPathTemplate string, tenantMode
if schemaPathTemplate == "" {
return nil
}
- tokens := getTemplateTokens(schemaPathTemplate)
+ tokens, _ := getTemplateTokens(schemaPathTemplate)
tokenMap := make(map[string]bool)
for _, token := range tokens {
tokenMap[token] = true
@@ -233,7 +233,7 @@ func ValidateProjectDBNameTemplate(template string) error {
if template == "" {
return nil
}
- tokens := getTemplateTokens(template)
+ tokens, _ := getTemplateTokens(template)
// Must contain {{DB_NAME}}
hasDBName := false
for _, token := range tokens {
@@ -252,13 +252,20 @@ func ValidateProjectDBNameTemplate(template string) error {
// FormatTemplate formats the template.
func FormatTemplate(template string, tokens map[string]string) (string, error) {
- keys := getTemplateTokens(template)
+ keys, fixed := getTemplateTokens(template)
for _, key := range keys {
if _, ok := tokens[key]; !ok {
return "", fmt.Errorf("token %q not found", key)
}
template = strings.ReplaceAll(template, key, tokens[key])
}
+ for _, key := range fixed {
+ quoteKey := regexp.QuoteMeta(key)
+ if quoteKey != key {
+ template = strings.Replace(template, key, quoteKey, 1)
+ }
+
+ }
return template, nil
}
@@ -299,7 +306,18 @@ func GetBaseDatabaseName(databaseName, dbNameTemplate, labelsJSON string) (strin
return names[1], nil
}
-func getTemplateTokens(template string) []string {
+func getTemplateTokens(template string) ([]string, []string) {
r := regexp.MustCompile(`{{[^{}]+}}`)
- return r.FindAllString(template, -1)
+ tokens := r.FindAllString(template, -1)
+ if len(tokens) > 0 {
+ split := r.Split(template, -1)
+ var fixed []string
+ for _, s := range split {
+ if s != "" {
+ fixed = append(fixed, s)
+ }
+ }
+ return tokens, fixed
+ }
+ return nil, nil
}
diff --git a/api/project_test.go b/api/project_test.go
index c1f74e2015..1e9f91d056 100644
--- a/api/project_test.go
+++ b/api/project_test.go
@@ -10,26 +10,31 @@ func TestGetTemplateTokens(t *testing.T) {
tests := []struct {
name string
template string
- want []string
+ tokens []string
+ fixed []string
}{
{
"NoToken",
"helloworld",
nil,
+ nil,
}, {
"TwoTokens",
"{{DB_NAME}}_{{LOCATION}}",
[]string{"{{DB_NAME}}", "{{LOCATION}}"},
+ []string{"_"},
}, {
"ExtraPrefix",
"hello_{{DB_NAME}}_{{LOCATION}}",
[]string{"{{DB_NAME}}", "{{LOCATION}}"},
+ []string{"hello_", "_"},
},
}
for _, test := range tests {
- tokens := getTemplateTokens(test.template)
- require.Equal(t, tokens, test.want)
+ tokens, fixed := getTemplateTokens(test.template)
+ require.Equal(t, test.tokens, tokens)
+ require.Equal(t, test.fixed, fixed)
}
}
@@ -175,7 +180,8 @@ func TestFormatTemplate(t *testing.T) {
},
"db1_hello_us-central1",
"",
- }, {
+ },
+ {
"tokenNotFound",
"{{DB_NAME}}_hello_{{LOCATION}}",
map[string]string{
@@ -184,6 +190,16 @@ func TestFormatTemplate(t *testing.T) {
"",
"not found",
},
+ {
+ "template with regex meta",
+ "{{DB_NAME}}_hello_${{LOCATION}}",
+ map[string]string{
+ "{{DB_NAME}}": "db1",
+ "{{LOCATION}}": "us-central1",
+ },
+ "db1_hello_\\$us-central1",
+ "",
+ },
}
for _, test := range tests {
@@ -238,6 +254,14 @@ func TestGetBaseDatabaseName(t *testing.T) {
"db1",
"",
},
+ {
+ "tenant_label_inculde_meta_success",
+ "db1$tenant123",
+ "{{DB_NAME}}${{TENANT}}",
+ "[{\"key\":\"bb.location\",\"value\":\"us-central1\"},{\"key\":\"bb.tenant\",\"value\":\"tenant123\"},{\"key\":\"bb.environment\",\"value\":\"Dev\"}]",
+ "db1",
+ "",
+ },
{
"tenant_location_label_success",
"us-central1...db你好_tenant123",
diff --git a/api/schema_system.go b/api/schema_system.go
index e63b93baf0..fdfd3bf690 100644
--- a/api/schema_system.go
+++ b/api/schema_system.go
@@ -173,7 +173,7 @@ func UnmarshalNamingRulePayloadAsTemplate(ruleType SchemaReviewRuleType, payload
}
template := nr.Format
- keys := getTemplateTokens(template)
+ keys, _ := getTemplateTokens(template)
for _, key := range keys {
if _, ok := TemplateNamingTokens[ruleType][key]; !ok {