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

Fix JSON Merge on removing permissions (fixes #1322) #1353

Merged
merged 4 commits into from
Nov 16, 2017

Conversation

gabisurita
Copy link
Member

@gabisurita gabisurita commented Oct 21, 2017

Fixes #1322

Allow null on permissions to support removing lists of principals on record permissions. As a side effect this allows passing null to regular patch operations with no effect, but we can change this if needed.

  • Add tests.
  • Add a changelog entry.

Allow null on permissions to support removing lists of principals
on record permissions. As a side effect this allows passing null
to regular patch operations with no effect.
@leplatrem
Copy link
Contributor

Gabi \o/ Welcome back :)

Thanks for your «patch» (huhu)

To be honest, I could not take time to review it thoroughly yet. But the tests look good!

I think we might have to be a bit more talkative about the changes in the changelog (and API changelog?) with regards to null values. (as well as in the docs on patch endpoints)

@gabisurita
Copy link
Member Author

Hey @leplatrem ! Good to see you!

I can update the documentation as soon as I have some time or keep the current behavior by not allowing null values using custom validation. What do you think makes more sense?

Copy link
Contributor

@glasserc glasserc left a comment

Choose a reason for hiding this comment

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

Gabi! Sorry for taking so long to review this -- it looks great to me. I didn't understand your comment about not allowing null values using custom validation. However, when trying to understand the current behavior, I felt the documentation on PATCH was a little thin -- in particular, the section for Permissions is above the section for JSON Patch, even though permissions are modified using the same plain update/merge patch/JSON patch mechanism. So I'd vote for refreshing the docs -- not much is really needed IMHO, just a comment that permissions are modified according to the same mechanism as the rest of the request (attributes merge, merge patch, or JSON patch) and that nulls are ignored when using attributes merge (or whatever strange behavior you're trying to describe).

@@ -1184,18 +1184,29 @@ def process_record(self, new, old=None):
"""
new = super().process_record(new, old)

# patch is specified as a list of of operations (RFC 6902)
# patch is specified as a list of of operations (RFC i6902)
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo here?

else:
permissions = self.request.validated['body'].get('permissions', {})
permissions = {k: v for k, v in payload.get('permissions', {}).items()
if v is not None}

annotated = {**new}

if permissions:
Copy link
Contributor

Choose a reason for hiding this comment

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

This is outside the scope of this PR, but I guess this means we can never set the permissions to the empty object.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this change is meaningful to the fix. Without it a null value on a regular patch would remove permissions as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but IMHO passing an empty object should be different from not passing anything.

@glasserc
Copy link
Contributor

glasserc commented Nov 2, 2017

Also in the documentation is this example, which I think is incorrect: {"a": {"b":"c"}} + {"a":{"d":"e"}} → {"a":{"b:c", "d":"e"}} (because "b:c" isn' valid JSON). That might be outside the scope of this PR, but if you're in there anyhow... 😇

@gabisurita
Copy link
Member Author

@glasserc Thanks for the review! I've tried to sketch some minor upgrades on the documentation for this section. What do you think?

@gabisurita
Copy link
Member Author

Flake8 seems to be failing because of this new error "E722 do not use bare except". May I just ignore this error and open an issue for it?
https://travis-ci.org/Kinto/kinto/jobs/297283472

Copy link
Contributor

@glasserc glasserc left a comment

Choose a reason for hiding this comment

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

Yes, I don't think a fix for "do not use bare except" belongs in this PR. As long as the tests still run I think that's enough.

.. must be provided.

When permissions are modified, the same behaviour used for the body is expected.
But notice thought that there are some permissions specific behaviours, such as:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think but and though (N.B. typo) are redundant -- either one is fine. I'd probably write note instead of notice. permissions-specific.

else:
permissions = self.request.validated['body'].get('permissions', {})
permissions = {k: v for k, v in payload.get('permissions', {}).items()
if v is not None}

annotated = {**new}

if permissions:
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, but IMHO passing an empty object should be different from not passing anything.


3. On JSON Merge operations, ``null`` values can be used to remove all principals
from a specific permission level. As a consequence, using ``null`` for permissions
on other types of patch operations is considered a no-op.
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, maybe I misunderstood, I didn't think that was a consequence. I would have written something like However, using null instead of a set of principals when using other patch types is not valid and will result in an error.

Copy link
Member Author

Choose a reason for hiding this comment

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

From my understanding what I've described is the implemented behavior. This is what I've tried to explain in the PR by saying "As a side effect this allows passing null to regular patch operations with no effect".

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, yes, then I misunderstood. I thought we had logic to only do the preprocess/postprocess null step if we were in a merge patch. In that case, why do we have this preprocess/postprocess step, versus just allowing permissions to be nullable? I'm guessing it's because we want to only allow nulls in certain kinds of operations, but it seems like we added it to the PermissionSchema, so it seems like it's global anyhow?

@Natim
Copy link
Member

Natim commented Nov 16, 2017

Shall I merge and release this?

@Natim Natim merged commit 2c3a5c5 into master Nov 16, 2017
@Natim Natim deleted the fix-json-merge-permissions branch November 16, 2017 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants