Skip to content

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

feat(tenancy): add validation for label values and database label size following the design (#256)

spinningbot2169c3c57e26
diff --git a/api/label.go b/api/label.go
index 57d400448d..89c853cd56 100644
--- a/api/label.go
+++ b/api/label.go
@@ -2,11 +2,16 @@ package api
import (
"context"
+ "fmt"
)
const (
// EnvironmentKeyName is the reserved key for environment.
EnvironmentKeyName string = "bb.environment"
+
+ // DatabaseLabelSizeMax is the maximium size of database labels.
+ DatabaseLabelSizeMax = 4
+ labelLengthMax = 63
)
// LabelKey is the available key for labels.
@@ -48,6 +53,16 @@ type LabelKeyPatch struct {
ValueList []string `jsonapi:"attr,valueList"`
}
+// Validate validates the sanity of patch values.
+func (patch *LabelKeyPatch) Validate() error {
+ for _, v := range patch.ValueList {
+ if len(v) <= 0 || len(v) > labelLengthMax {
+ return fmt.Errorf("label value has a maximum length of %v characters and cannot be empty", labelLengthMax)
+ }
+ }
+ return nil
+}
+
// DatabaseLabel is the label associated with a database.
type DatabaseLabel struct {
ID int `json:"-"`
diff --git a/docs/design/tenancy.md b/docs/design/tenancy.md
index bbf5420d15..af5cd3463c 100644
--- a/docs/design/tenancy.md
+++ b/docs/design/tenancy.md
@@ -322,7 +322,7 @@ We have some reserved/built-in keys to simplify the onboarding and be consistent
| -------------- | ------------------------------------------------------------------ |
| bb.location | The location of a database such as region, zone, datacenter names. |
| bb.tenant | The tenant name of a customer. |
-| bb.environment | The environment name mappped from instance environment customer. |
+| bb.environment | The environment name mappped from instance environment. |
Note, bb.environemnt labels are reserved labels mapped from database instances' environments. This label is immutable and always returned from database label listing. It must be present and its value must match exactly with instance environment on setting the database labels. Since tenants are identified by labels in the deployment config, we need an environment labels to identify tenants from different environment in a schema update deployment. If we expose the environment label concept in the deployment config, it should look consistent in the label API.
diff --git a/server/database.go b/server/database.go
index d541bd6bae..75e8beecfa 100644
--- a/server/database.go
+++ b/server/database.go
@@ -162,6 +162,12 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
var labels []*api.DatabaseLabel
json.Unmarshal([]byte(*databasePatch.Labels), &labels)
+ // For scalability, each database can have up to four labels for now.
+ if len(labels) > api.DatabaseLabelSizeMax {
+ err := fmt.Errorf("database labels are up to a maximum of %d", api.DatabaseLabelSizeMax)
+ return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
+ }
+
rowStatus := api.Normal
labelKeyList, err := s.LabelService.FindLabelKeyList(ctx, &api.LabelKeyFind{RowStatus: &rowStatus})
if err != nil {
diff --git a/server/label.go b/server/label.go
index 1a93ac3d63..a46a3efe49 100644
--- a/server/label.go
+++ b/server/label.go
@@ -58,6 +58,10 @@ func (s *Server) registerLabelRoutes(g *echo.Group) {
if err := jsonapi.UnmarshalPayload(c.Request().Body, patch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch label key request").SetInternal(err)
}
+
+ if err := patch.Validate(); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid patch label key request").SetInternal(err)
+ }
// We don't allow updating reserved environment label keys. Since its ID is zero, it cannot be updated by default.
labelKey, err := s.LabelService.PatchLabelKey(ctx, patch)