Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix: add validation (#4008)
ecmadaofe771df8779f
diff --git a/plugin/db/pg/role.go b/plugin/db/pg/role.goindex 929f4ef90b..be043192d6 100644--- a/plugin/db/pg/role.go+++ b/plugin/db/pg/role.go@@ -17,19 +17,32 @@ type RoleAttribute stringconst (// SUPERUSER is the role attribute for rolsuper.SUPERUSER RoleAttribute = "SUPERUSER"+ // NOSUPERUSER is the role attribute for rolsuper.+ NOSUPERUSER RoleAttribute = "NOSUPERUSER"// LOGIN is the role attribute for rolcanlogin.LOGIN RoleAttribute = "LOGIN"+ // NOLOGIN is the role attribute for rolcanlogin.+ NOLOGIN RoleAttribute = "NOLOGIN"+ // INHERIT is the role attribute for rolinherit.+ INHERIT RoleAttribute = "INHERIT"// NOINHERIT is the role attribute for rolinherit.- // INHERIT is the default value for rolinherit, so we need to use the NOINHERIT.NOINHERIT RoleAttribute = "NOINHERIT"// CREATEDB is the role attribute for rolcreatedb.CREATEDB RoleAttribute = "CREATEDB"+ // NOCREATEDB is the role attribute for rolcreatedb.+ NOCREATEDB RoleAttribute = "NOCREATEDB"// CREATEROLE is the role attribute for rolcreaterole.CREATEROLE RoleAttribute = "CREATEROLE"+ // NOCREATEROLE is the role attribute for rolcreaterole.+ NOCREATEROLE RoleAttribute = "NOCREATEROLE"// REPLICATION is the role attribute for rolreplication.REPLICATION RoleAttribute = "REPLICATION"+ // NOREPLICATION is the role attribute for rolreplication.+ NOREPLICATION RoleAttribute = "NOREPLICATION"// BYPASSRLS is the role attribute for rolbypassrls.BYPASSRLS RoleAttribute = "BYPASSRLS"+ // NOBYPASSRLS is the role attribute for rolbypassrls.+ NOBYPASSRLS RoleAttribute = "BYPASSRLS")// ToString returns the string value for role attribute.@@ -254,24 +267,38 @@ func convertToAttributeStatement(r *v1pb.DatabaseRoleUpsert) string {if r.Attribute != nil {if r.Attribute.SuperUser {attributeList = append(attributeList, SUPERUSER.ToString())+ } else {+ attributeList = append(attributeList, NOSUPERUSER.ToString())}if r.Attribute.NoInherit {attributeList = append(attributeList, NOINHERIT.ToString())+ } else {+ attributeList = append(attributeList, INHERIT.ToString())}if r.Attribute.CanLogin {attributeList = append(attributeList, LOGIN.ToString())+ } else {+ attributeList = append(attributeList, NOLOGIN.ToString())}if r.Attribute.CreateRole {attributeList = append(attributeList, CREATEROLE.ToString())+ } else {+ attributeList = append(attributeList, NOCREATEROLE.ToString())}if r.Attribute.CreateDb {attributeList = append(attributeList, CREATEDB.ToString())+ } else {+ attributeList = append(attributeList, NOCREATEDB.ToString())}if r.Attribute.Replication {attributeList = append(attributeList, REPLICATION.ToString())+ } else {+ attributeList = append(attributeList, NOREPLICATION.ToString())}if r.Attribute.BypassRls {attributeList = append(attributeList, BYPASSRLS.ToString())+ } else {+ attributeList = append(attributeList, NOBYPASSRLS.ToString())}}diff --git a/server/openapi_instance.go b/server/openapi_instance.goindex fb90f7ef1d..77b32b7959 100644--- a/server/openapi_instance.go+++ b/server/openapi_instance.go@@ -15,6 +15,7 @@ import ("github.com/bytebase/bytebase/api"openAPIV1 "github.com/bytebase/bytebase/api/v1""github.com/bytebase/bytebase/common"+ "github.com/bytebase/bytebase/plugin/db"v1pb "github.com/bytebase/bytebase/proto/generated-go/v1""github.com/bytebase/bytebase/store"@@ -217,7 +218,7 @@ func (s *Server) listDatabaseRole(c echo.Context) error {return roleList, nil}()if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to query the role").SetInternal(err)+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to list the role").SetInternal(err)}c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)@@ -285,8 +286,11 @@ func (s *Server) createDatabaseRole(c echo.Context) error {return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)}- var upsert v1pb.DatabaseRoleUpsert- if err := protojson.Unmarshal(body, &upsert); err != nil {+ upsert := &v1pb.DatabaseRoleUpsert{}+ if err := protojson.Unmarshal(body, upsert); err != nil {+ return err+ }+ if err := validateRole(upsert); err != nil {return err}@@ -302,7 +306,7 @@ func (s *Server) createDatabaseRole(c echo.Context) error {}defer driver.Close(ctx)- role, err := driver.CreateRole(ctx, &upsert)+ role, err := driver.CreateRole(ctx, upsert)if err != nil {return nil, err}@@ -310,7 +314,7 @@ func (s *Server) createDatabaseRole(c echo.Context) error {return role, nil}()if err != nil {- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to query the role").SetInternal(err)+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create the role").SetInternal(err)}return marshalDatabaseRoleResponse(c, role, instance.ID)@@ -348,8 +352,11 @@ func (s *Server) updateDatabaseRole(c echo.Context) error {return echo.NewHTTPError(http.StatusBadRequest, "Failed to read request body").SetInternal(err)}- var upsert v1pb.DatabaseRoleUpsert- if err := protojson.Unmarshal(body, &upsert); err != nil {+ upsert := &v1pb.DatabaseRoleUpsert{}+ if err := protojson.Unmarshal(body, upsert); err != nil {+ return err+ }+ if err := validateRole(upsert); err != nil {return err}@@ -366,7 +373,7 @@ func (s *Server) updateDatabaseRole(c echo.Context) error {}defer driver.Close(ctx)- role, err := driver.UpdateRole(ctx, rawName, &upsert)+ role, err := driver.UpdateRole(ctx, rawName, upsert)if err != nil {return nil, err}@@ -377,12 +384,25 @@ func (s *Server) updateDatabaseRole(c echo.Context) error {if common.ErrorCode(err) == common.NotFound {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Cannot found the role %s in instance %d", rawName, instance.ID))}- return echo.NewHTTPError(http.StatusInternalServerError, "Failed to query the role").SetInternal(err)+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to update the role").SetInternal(err)}return marshalDatabaseRoleResponse(c, role, instance.ID)}+func validateRole(upsert *v1pb.DatabaseRoleUpsert) error {+ if v := upsert.ConnectionLimit; v != nil && *v < int32(-1) {+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid connection limit, it should greater than or equal to -1")+ }+ if v := upsert.ValidUntil; v != nil {+ if _, err := time.Parse(time.RFC3339, *v); err != nil {+ return echo.NewHTTPError(http.StatusBadRequest, "Invalid timestamp for valid_until").SetInternal(err)+ }+ }++ return nil+}+func (s *Server) validateInstance(ctx context.Context, c echo.Context) (*api.Instance, error) {instanceID, err := strconv.Atoi(c.Param("instanceID"))if err != nil {@@ -396,6 +416,9 @@ func (s *Server) validateInstance(ctx context.Context, c echo.Context) (*api.Insif instance == nil {return nil, echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", instanceID))}+ if instance.Engine != db.Postgres {+ return nil, echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Role management for %v is not support", instance.Engine))+ }return instance, nil}