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

Improve permission handling #86

Merged
merged 7 commits into from
Aug 28, 2024
Merged

Improve permission handling #86

merged 7 commits into from
Aug 28, 2024

Conversation

b-rowan
Copy link
Contributor

@b-rowan b-rowan commented Aug 26, 2024

Improve permissions handling with string parsing.

Rules are as follows -

  • * - Matches all at all levels.
  • ! - Prefix, inverts the permission, and takes priority.
  • foo - The name of a permission, just foo on its own grants all sub permissions for that value.
  • foo.* - Same as foo.
  • foo.bar - Grants bar permissions for foo endpoint.

For example, if an endpoint requires foo.bar.baz, valid user permissions are

  • *
  • foo
  • foo.*
  • foo.bar
  • foo.bar.*
  • foo.bar.baz

User permissions that will always be invalid are the inverted versions of this, so

  • !*
  • !foo
  • !foo.*
  • !foo.bar
  • !foo.bar.*
  • !foo.bar.baz

@b-rowan b-rowan marked this pull request as draft August 26, 2024 16:20
@b-rowan
Copy link
Contributor Author

b-rowan commented Aug 26, 2024

On second thought, leaving as a draft until #85 gets merged, so I can add docs on this.

main.py Outdated Show resolved Hide resolved
from goosebit.updater.manager import UpdateManager, get_update_manager

router = APIRouter(prefix="/{dev_id}")


@router.get(
"",
dependencies=[Security(validate_user_permissions, scopes=[Permissions.HOME.READ])],
dependencies=[Security(validate_user_permissions, scopes=["home.read"])],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Bit sad to switch from a "type-safe" approach to free form strings that can have typos. But ok for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah it's not my favorite either, but I think this makes more sense for allowing plugins.

I could try to make this more type safe, maybe by having the nav register permissions, then checking if all user permissions match a valid pattern, but that could be a problem with plugins.

goosebit/auth/__init__.py Outdated Show resolved Hide resolved
goosebit/auth/__init__.py Outdated Show resolved Hide resolved
Copy link

filepath function $$\textcolor{#f14c4c}{\tt{failed}}$$ SUBTOTAL
$$\textcolor{#f14c4c}{\tt{tests/updater/controller/v1/test\_routes.py}}$$ $$\textcolor{#f14c4c}{\tt{test\_register\_device}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$
$$\textcolor{#f14c4c}{\tt{tests/updater/controller/v1/test\_routes.py}}$$ $$\textcolor{#f14c4c}{\tt{test\_rollout\_full}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$
$$\textcolor{#f14c4c}{\tt{tests/updater/controller/v1/test\_routes.py}}$$ $$\textcolor{#f14c4c}{\tt{test\_rollout\_signalling\_download\_failure}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$
$$\textcolor{#f14c4c}{\tt{tests/updater/controller/v1/test\_routes.py}}$$ $$\textcolor{#f14c4c}{\tt{test\_rollout\_selection}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$
$$\textcolor{#f14c4c}{\tt{tests/updater/controller/v1/test\_routes.py}}$$ $$\textcolor{#f14c4c}{\tt{test\_latest}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$
$$\textcolor{#f14c4c}{\tt{tests/updater/controller/v1/test\_routes.py}}$$ $$\textcolor{#f14c4c}{\tt{test\_latest\_with\_no\_software\_available}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$
$$\textcolor{#f14c4c}{\tt{tests/updater/controller/v1/test\_routes.py}}$$ $$\textcolor{#f14c4c}{\tt{test\_pinned}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$
$$\textcolor{#f14c4c}{\tt{tests/updater/controller/v1/test\_routes.py}}$$ $$\textcolor{#f14c4c}{\tt{test\_up\_to\_date}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$ $$\textcolor{#f14c4c}{\tt{1}}$$
$$\textcolor{#f14c4c}{\tt{TOTAL}}$$ $$\textcolor{#f14c4c}{\tt{8}}$$ $$\textcolor{#f14c4c}{\tt{13}}$$

@UpstreamData UpstreamData force-pushed the dev_permissions branch 2 times, most recently from 18a5dd5 to 58a436d Compare August 27, 2024 16:21
@b-rowan b-rowan marked this pull request as ready for review August 27, 2024 16:31
@b-rowan b-rowan added the v0.2.0 label Aug 27, 2024
Use strings for permissions as they are more extensible than enums.  Less granular permissions are also available, `*` is all permissions, `software` or `software.*` gives access to all software endpoints, and `software.read` gives access to read-only software information.  `*.read` can also be used to give read access to all endpoints.
Prevent users from writing when they don't have the correct permissions.
Using the `!` prefix will deny permissions for a route, and takes precedence over allowing permissions.  If you wanted to allow reading but deny software access, you could use `["*.read", "!software"]`.
Update pytest-asyncio because it was unable to find the event loop for new tests, see pytest-dev/pytest-asyncio#862
@b-rowan b-rowan merged commit c72559f into master Aug 28, 2024
2 checks passed
@b-rowan b-rowan deleted the dev_permissions branch August 28, 2024 04:55
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