Skip to content

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

Fix policy API URL path.

spinningbotad4cf2a2dabf
diff --git a/api/policy.go b/api/policy.go
index c56ee3f4b7..a8b7876b84 100644
--- a/api/policy.go
+++ b/api/policy.go
@@ -73,7 +73,7 @@ type PolicyUpsert struct {
UpdaterId int
// Related fields
- EnvironmentId int `jsonapi:"attr,environmentId"`
+ EnvironmentId int
// Domain specific fields
Type PolicyType `jsonapi:"attr,type"`
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index 69f6c5207c..3a763e067d 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -22,7 +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, /policy/{type}/environment/{environmentId}, 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 67b1bc28db..fbe243a58c 100644
--- a/server/acl_casbin_policy_developer.csv
+++ b/server/acl_casbin_policy_developer.csv
@@ -20,7 +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, /policy/{type}/environment/{environmentId}, 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 65292f131c..6e8077e6ac 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -26,8 +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, /policy/{type}/environment/{environmentId}, GET
+p, OWNER, /policy/{type}/environment/{environmentId}, PATCH
p, OWNER, /instance, POST
p, OWNER, /instance, GET
p, OWNER, /instance/{id}, GET
diff --git a/server/policy.go b/server/policy.go
index 41f8a564b0..f5d4623069 100644
--- a/server/policy.go
+++ b/server/policy.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
+ "strconv"
"github.com/bytebase/bytebase/api"
"github.com/google/jsonapi"
@@ -11,12 +12,18 @@ import (
)
func (s *Server) registerPolicyRoutes(g *echo.Group) {
- g.PATCH("/policy/:type", func(c echo.Context) error {
+ g.PATCH("/policy/:type/environment/:environmentId", func(c echo.Context) error {
+ environmentID, err := strconv.Atoi(c.Param("environmentId"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("environmentID is not a number: %s", c.Param("id"))).SetInternal(err)
+ }
+
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 := api.PolicyType(c.Param("type"))
+ policyUpsert.EnvironmentId = environmentID
policyUpsert.Type = pType
policyUpsert.UpdaterId = c.Get(GetPrincipalIdContextKey()).(int)
@@ -32,13 +39,15 @@ func (s *Server) registerPolicyRoutes(g *echo.Group) {
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)
+ g.GET("/policy/:type/environment/:environmentId", func(c echo.Context) error {
+ environmentID, err := strconv.Atoi(c.Param("environmentId"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("environmentID is not a number: %s", c.Param("id"))).SetInternal(err)
}
+ policyFind := &api.PolicyFind{}
pType := api.PolicyType(c.Param("type"))
policyFind.Type = &pType
+ policyFind.EnvironmentId = &environmentID
policy, err := s.PolicyService.FindPolicy(context.Background(), policyFind)
if err != nil {