Skip to content

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

Add policy routes for getting and upserting policy key'ed by policy type.

spinningbot93881aac9ec5
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index e906dae98d..69f6c5207c 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -22,6 +22,7 @@ p, DBA, /project/{projectId}/webhook/{webhookId}/test, GET
p, DBA, /environment, POST
p, DBA, /environment, GET
p, DBA, /environment/{id}, PATCH
+p, DBA, /policy/{type}, GET
p, DBA, /instance, POST
p, DBA, /instance, GET
p, DBA, /instance/{id}, GET
diff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csv
index f9ac68c30e..67b1bc28db 100644
--- a/server/acl_casbin_policy_developer.csv
+++ b/server/acl_casbin_policy_developer.csv
@@ -20,6 +20,7 @@ p, DEVELOPER, /project/{projectId}/webhook/{webhookId}, PATCH
p, DEVELOPER, /project/{projectId}/webhook/{webhookId}, DELETE
p, DEVELOPER, /project/{projectId}/webhook/{webhookId}/test, GET
p, DEVELOPER, /environment, GET
+p, DEVELOPER, /policy/{type}, GET
p, DEVELOPER, /instance, GET
p, DEVELOPER, /instance/{id}, GET
p, DEVELOPER, /instance/{id}/user, GET
diff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csv
index b8ed2a9430..65292f131c 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -26,6 +26,8 @@ p, OWNER, /project/{projectId}/webhook/{webhookId}/test, GET
p, OWNER, /environment, POST
p, OWNER, /environment, GET
p, OWNER, /environment/{id}, PATCH
+p, OWNER, /policy/{type}, GET
+p, OWNER, /policy/{type}, PATCH
p, OWNER, /instance, POST
p, OWNER, /instance, GET
p, OWNER, /instance/{id}, GET
diff --git a/server/policy.go b/server/policy.go
new file mode 100644
index 0000000000..3661faac6b
--- /dev/null
+++ b/server/policy.go
@@ -0,0 +1,54 @@
+package server
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/google/jsonapi"
+ "github.com/labstack/echo/v4"
+)
+
+func (s *Server) registerPolicyRoutes(g *echo.Group) {
+ g.PATCH("/policy/:type", func(c echo.Context) error {
+ policyUpsert := &api.PolicyUpsert{}
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, policyUpsert); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted set policy request").SetInternal(err)
+ }
+ pType := c.Param("type")
+ policyUpsert.Type = pType
+ policyUpsert.UpdaterId = c.Get(GetPrincipalIdContextKey()).(int)
+
+ policy, err := s.PolicyService.UpsertPolicy(context.Background(), policyUpsert)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to set policy for type %q", pType)).SetInternal(err)
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, policy); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal create set policy response").SetInternal(err)
+ }
+ return nil
+ })
+
+ g.GET("/policy/:type", func(c echo.Context) error {
+ policyFind := &api.PolicyFind{}
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, policyFind); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformatted get policy request").SetInternal(err)
+ }
+ pType := c.Param("type")
+ policyFind.Type = &pType
+
+ policy, err := s.PolicyService.FindPolicy(context.Background(), policyFind)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get policy for type %q", pType)).SetInternal(err)
+ }
+
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, policy); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal get policy response: %v", pType)).SetInternal(err)
+ }
+ return nil
+ })
+}
diff --git a/server/server.go b/server/server.go
index b362232574..ee9e7af06f 100644
--- a/server/server.go
+++ b/server/server.go
@@ -191,6 +191,7 @@ func NewServer(logger *zap.Logger, version string, host string, port int, fronte
s.registerAuthRoutes(apiGroup)
s.registerPrincipalRoutes(apiGroup)
s.registerMemberRoutes(apiGroup)
+ s.registerPolicyRoutes(apiGroup)
s.registerProjectRoutes(apiGroup)
s.registerProjectWebhookRoutes(apiGroup)
s.registerProjectMemberRoutes(apiGroup)
diff --git a/store/policy.go b/store/policy.go
index 6b19e047c4..60e977165d 100644
--- a/store/policy.go
+++ b/store/policy.go
@@ -38,6 +38,7 @@ func (s *PolicyService) FindPolicy(ctx context.Context, find *api.PolicyFind) (*
defer tx.Rollback()
list, err := s.findPolicy(ctx, tx, find)
+ // TODO(spinningbot): handle the default.
if err != nil {
return nil, err
} else if len(list) == 0 {