Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: support access database control for sql editor (#3634)
rebelice6b1d4169a83b
diff --git a/api/policy.go b/api/policy.goindex f90b50c0bc..3c1917aa79 100644--- a/api/policy.go+++ b/api/policy.go@@ -289,7 +289,10 @@ func (p *SensitiveDataPolicy) String() (string, error) {// AccessControlPolicy is the policy configuration for database access control.// It is only applicable to database and environment resource type.+// For environment resource type, DisallowRuleList defines the access control rule.+// For database resource type, the AccessControlPolicy struct itself means allow to access.type AccessControlPolicy struct {+ // Environment resource type specific fields.DisallowRuleList []AccessControlRule `json:"disallowRuleList"`}diff --git a/server/sql.go b/server/sql.goindex c08b3586f0..5eaa46b97e 100644--- a/server/sql.go+++ b/server/sql.go@@ -180,23 +180,34 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {if instance == nil {return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", exec.InstanceID))}+ principalID := c.Get(getPrincipalIDContextKey()).(int)var database *api.Databaseif exec.DatabaseName != "" {database, err = s.getDatabase(ctx, instance.ID, exec.DatabaseName)if err != nil {return err}+ // Database Access Control+ hasAccessRights, err := s.hasDatabaseAccessRights(ctx, principalID, database)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to check access control for database: %q", exec.DatabaseName)).SetInternal(err)+ }+ if !hasAccessRights {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Malformed sql execute request, no permission to access database %q", exec.DatabaseName))+ }}- // Database Access Control+ // Database Access Control for MySQL dialect.+ // MySQL dialect can query cross the database.+ // We need special check.if instance.Engine == db.MySQL || instance.Engine == db.TiDB {databaseList, err := parser.ExtractDatabaseList(parser.MySQL, exec.Statement)if err != nil {return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to extract database list: %q", exec.Statement)).SetInternal(err)}- // Disallow cross-database query if specify database.if exec.DatabaseName != "" {+ // Disallow cross-database query if specify database.for _, databaseName := range databaseList {upperDatabaseName := strings.ToUpper(databaseName)// We allow querying information schema.@@ -207,6 +218,30 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Malformed sql execute request, specify database %q but access database %q", exec.DatabaseName, databaseName))}}+ } else {+ // Check database access rights.+ for _, databaseName := range databaseList {+ if databaseName == "" {+ // We have already checked the current database access rights.+ continue+ }+ accessDatabase, err := s.getDatabase(ctx, instance.ID, databaseName)+ if err != nil {+ if httpErr, ok := err.(*echo.HTTPError); ok && httpErr.Code == echo.ErrNotFound.Code {+ // If database not found, skip.+ continue+ }+ return err+ }++ hasAccessRights, err := s.hasDatabaseAccessRights(ctx, principalID, accessDatabase)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to check access control for database: %q", accessDatabase.Name)).SetInternal(err)+ }+ if !hasAccessRights {+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Malformed sql execute request, no permission to access database %q", accessDatabase.Name))+ }+ }}}@@ -992,3 +1027,42 @@ func isMySQLExcludeDatabase(database string) bool {}return true}++func (s *Server) hasDatabaseAccessRights(ctx context.Context, principalID int, database *api.Database) (bool, error) {+ // Only project member can access database.+ if !api.HasActiveProjectMembership(principalID, database.Project) {+ return false, nil+ }++ // calculate the effective policy.+ databasePolicy, inheritFromEnvironment, err := s.store.GetNormalAccessControlPolicy(ctx, api.PolicyResourceTypeDatabase, database.ID)+ if err != nil {+ return false, err+ }++ environmentPolicy, _, err := s.store.GetNormalAccessControlPolicy(ctx, api.PolicyResourceTypeEnvironment, database.Instance.EnvironmentID)+ if err != nil {+ return false, err+ }++ if !inheritFromEnvironment {+ // Use database policy.+ return databasePolicy != nil && len(databasePolicy.DisallowRuleList) == 0, nil+ }+ // Use both database policy and environment policy.+ hasAccessRights := true+ if environmentPolicy != nil {+ // Disallow by environment access policy.+ for _, rule := range environmentPolicy.DisallowRuleList {+ if rule.FullDatabase {+ hasAccessRights = false+ break+ }+ }+ }+ if databasePolicy != nil {+ // Allow by database access policy.+ hasAccessRights = true+ }+ return hasAccessRights, nil+}diff --git a/store/policy.go b/store/policy.goindex 3d021b1745..67d383c3e8 100644--- a/store/policy.go+++ b/store/policy.go@@ -265,6 +265,30 @@ func (s *Store) GetSensitiveDataPolicy(ctx context.Context, databaseID int) (*apreturn api.UnmarshalSensitiveDataPolicy(policy.Payload)}+// GetNormalAccessControlPolicy will get the normal access control polciy. Return nil if InheritFromParent is true.+func (s *Store) GetNormalAccessControlPolicy(ctx context.Context, resourceType api.PolicyResourceType, resourceID int) (*api.AccessControlPolicy, bool, error) {+ policy, err := s.getPolicyRaw(ctx, &api.PolicyFind{+ ResourceType: &resourceType,+ ResourceID: &resourceID,+ Type: api.PolicyTypeAccessControl,+ })+ if err != nil {+ // For access constrol policy, the default value for InheritFromParent is true.+ return nil, true, err+ }+ if policy == nil || policy.RowStatus != api.Normal {+ // For access constrol policy, the default value for InheritFromParent is true.+ return nil, true, nil+ }++ accessControlPolicy, err := api.UnmarshalAccessControlPolicy(policy.Payload)+ if err != nil {+ // For access constrol policy, the default value for InheritFromParent is true.+ return nil, true, err+ }+ return accessControlPolicy, policy.InheritFromParent, nil+}+//// private functions//@@ -320,6 +344,10 @@ func (s *Store) getPolicyRaw(ctx context.Context, find *api.PolicyFind) (*policyif find.ResourceID != nil {ret.ResourceID = *find.ResourceID}+ if find.Type == api.PolicyTypeAccessControl {+ // For access constrol policy, the default value for InheritFromParent is true.+ ret.InheritFromParent = true+ }} else if len(policyRawList) > 1 {return nil, &common.Error{Code: common.Conflict, Err: errors.Errorf("found %d policy with filter %+v, expect 1. ", len(policyRawList), find)}} else {