Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: check old policy path for bundle ownership #4847

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions server/server.go
Expand Up @@ -1982,7 +1982,7 @@ func (s *Server) v1PoliciesPut(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
vars := mux.Vars(r)

path, err := url.PathUnescape(vars["path"])
id, err := url.PathUnescape(vars["path"])
if err != nil {
writer.ErrorString(w, http.StatusBadRequest, types.CodeInvalidParameter, err)
return
Expand Down Expand Up @@ -2010,7 +2010,12 @@ func (s *Server) v1PoliciesPut(w http.ResponseWriter, r *http.Request) {
return
}

if bs, err := s.store.GetPolicy(ctx, txn, path); err != nil {
if err := s.checkPolicyIDScope(ctx, txn, id); err != nil && !storage.IsNotFound(err) {
s.abortAuto(ctx, txn, w, err)
return
}

if bs, err := s.store.GetPolicy(ctx, txn, id); err != nil {
if !storage.IsNotFound(err) {
s.abortAuto(ctx, txn, w, err)
return
Expand All @@ -2026,7 +2031,7 @@ func (s *Server) v1PoliciesPut(w http.ResponseWriter, r *http.Request) {
}

m.Timer(metrics.RegoModuleParse).Start()
parsedMod, err := ast.ParseModule(path, string(buf))
parsedMod, err := ast.ParseModule(id, string(buf))
m.Timer(metrics.RegoModuleParse).Stop()

if err != nil {
Expand Down Expand Up @@ -2057,7 +2062,7 @@ func (s *Server) v1PoliciesPut(w http.ResponseWriter, r *http.Request) {
return
}

modules[path] = parsedMod
modules[id] = parsedMod

c := ast.NewCompiler().
SetErrorLimit(s.errLimit).
Expand All @@ -2075,7 +2080,7 @@ func (s *Server) v1PoliciesPut(w http.ResponseWriter, r *http.Request) {

m.Timer(metrics.RegoModuleCompile).Stop()

if err := s.store.UpsertPolicy(ctx, txn, path, buf); err != nil {
if err := s.store.UpsertPolicy(ctx, txn, id, buf); err != nil {
s.abortAuto(ctx, txn, w, err)
return
}
Expand Down
7 changes: 7 additions & 0 deletions server/server_test.go
Expand Up @@ -1843,6 +1843,13 @@ func TestBundleScope(t *testing.T) {
code: http.StatusBadRequest,
resp: `{"code": "invalid_parameter", "message": "path a/b is owned by bundle \"test-bundle\""}`,
},
{
method: "PUT",
path: "/policies/someid",
body: `package other.path`,
code: http.StatusBadRequest,
resp: `{"code": "invalid_parameter", "message": "path x/y/z is owned by bundle \"test-bundle\""}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, there is a policy with id someid, it contains the package x.y.z, and we're attempting to replace this policy with one containing only the other.path package; effectively removing the x.y.z package. Am I understanding this correctly?
It might not be immediately apparent for the user that they're affecting packages not present in the pushed policy. Is there a sensible way of refining this error message? Like explicitly saying that the action would modify/remove the package.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the new policy was to replace an existing policy not owned by a bundle, so that the new package other.path is strictly additive, this action would be allowed? If so, then perhaps the error should state that the someid policy is protected. And this error would be reserved for when an update of an unprotected policy would infringe on a protected policy with e.g. a package overlap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revisit the error messages for the next release. I'd rather get this issue out of the way quick. 🌻

},
{
method: "DELETE",
path: "/policies/someid",
Expand Down