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

Add support for per route authorization #2011

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ibizaman
Copy link


Description

Add the possibility to associate a group - and/or role for
Keycloak-OIDC - with a path prefix. This means we can have fine
grained control over which group and/or role to allow per request
path.

The following configuration:

allowed_roles = [ "user", "admin", "superadmin", "/admin|admin", "/admin|superadmin" ]

Gets mapped internally to:

path: /
  - user
  - admin
  - superadmin
path: /admin
  - admin
  - superadmin

So only those having the role admin or superadmin can access
anything under the /admin path.

Same thing when substituting "role" for "group".

A few caveats:

  • I chose to extend the existing allowed_groups and allowed_roles
    syntax to [<path>|]<name>. This choice is completely arbitrary and
    can be changed if something else makes more sense.
  • If we choose to keep the extended syntax, I should probably add a
    group_path_separator argument to let the user choose another
    separator and default it to |.
  • I chose to only match on path prefix. No Regexp, path suffix, method
    matching, etc. Why? It was simpler to get a PoC out of the door. I'm
    happy to extend this to other use cases I didn't think about, of
    course.

Motivation and Context

This feature allows more fine-grained control on what path is
accessible to what users.

It allows oauth2-proxy to become an even more indispensable building
block to protect apps because we can rely on oauth2-proxy to implement
security that was only possible in-app beforehand, freeing the
developers to work on their business use case.

But more importantly, at least for my use case, we can add protection
over apps that we cannot update. For example, here I wanted to protect
a Vaultwarden instance that does not handle SSO
yet
. Alos,
Vaultwarden exposes an admin UI under /admin path. I wanted to add
SSO in front of the whole Vaultwarden UI but also to protect the
/admin path with stricter role. This PR makes this possible.

How Has This Been Tested?

First, I added some unit tests.

Also, I deployed oauth2-proxy in front of a Vaultwarden service. oauth2-proxy's config included in particular:

allowed_roles = [ "user", "/admin|admin" ]

The Keycloak instance has two user:

  • "lowpriviledge" having a realm role of user
  • and "highpriviledge" having a realm role of admin which is a
    composite role including the user role.

When logging with "lowpriviledge", I can access the normal Vaultwarden
UI under / path while I get a 403 if I try to access anything under
/admin. While with "highpriviledge" I can access the whole UI.

Checklist:

I purposely didn't add documentation yet as I wanted to gather
feedback first. But I'll of course add some when we settle on what
arguments to have and such.

  • My change requires a change to the documentation or CHANGELOG.
  • I have updated the documentation/CHANGELOG accordingly.
  • I have created a feature (non-master) branch for my PR.

@github-actions
Copy link
Contributor

This pull request has been inactive for 60 days. If the pull request is still relevant please comment to re-activate the pull request. If no action is taken within 7 days, the pull request will be marked closed.

@github-actions github-actions bot added the Stale label Apr 18, 2023
@ibizaman
Copy link
Author

Relevant! Already 60 days… time flies.

expectedAuthZ: true,
},
{
name: "MoreSpecificPathMatches",

Choose a reason for hiding this comment

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

Equating "more specific" to "longer prefix match" is ambiguous and potentially misleading?

Which is "more specific" - /administration or /admin/a/b? I think most people would say the latter, because it is 'deeper'. But given a rule for /administration and a rule for /admin/a/b, your code will pick the former as being "more specific" and hence the rule that is used.

An alternative to consider would be to use the first match in the list?

(I'm not associated with the project, this is a drive-by comment!)

Copy link
Author

Choose a reason for hiding this comment

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

This makes sense indeed. Maybe we should allow globing or regex. It’s more cognitive load on the user but at the same time the contract is clear.

Choose a reason for hiding this comment

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

I can imagine some use cases might benefit from regex/glob matching. But I think it's an orthogonal question - you could still have multiple matches and need a way to pick one, i.e. first wins.

Copy link
Author

Choose a reason for hiding this comment

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

I see what you mean. Probably first wins is more intuitive.

@sebwills
Copy link

This feature would be super useful. What needs to be done to attract some feedback from the maintainers?

@github-actions
Copy link
Contributor

This pull request has been inactive for 60 days. If the pull request is still relevant please comment to re-activate the pull request. If no action is taken within 7 days, the pull request will be marked closed.

@github-actions github-actions bot added the Stale label Jul 12, 2023
@ibizaman
Copy link
Author

still relevant!

@ghmer
Copy link

ghmer commented Jul 15, 2023

Exactly what I was looking for. Please get this merged into oauth2-proxy!

@github-actions github-actions bot removed the Stale label Jul 16, 2023
@henriduflot
Copy link

That is exactly what we need. We have a lot a tools which don't support authentication but we need oauth2_proxy to protected it based on users groups.

Copy link
Contributor

This pull request has been inactive for 60 days. If the pull request is still relevant please comment to re-activate the pull request. If no action is taken within 7 days, the pull request will be marked closed.

@github-actions github-actions bot added the Stale label Nov 13, 2023
@ibizaman
Copy link
Author

ibizaman commented Nov 13, 2023

Would love to get this merged although TBH I since switched from Keycloak to Authelia which does support this out of the box :/

@github-actions github-actions bot removed the Stale label Nov 15, 2023
@igops
Copy link

igops commented Dec 1, 2023

@ibizaman great stuff! +1 for merging into the main branch from me!

I was going to make a similar fork by myself. However, my idea was to keep a configuration untouched and manage per-route roles on the reverse-proxy like:

location /my-location {
    set $roles "admin,viewer,commentor";
    # auth_request block with some additional params ...
    # proxying logic ...
}

location = /oauth2/auth {
    # pass the roles to the oauth2-proxy somehow (in headers?) ...
    # auth logic ...
}

I haven't tried my version yet 😄

Copy link
Contributor

This pull request has been inactive for 60 days. If the pull request is still relevant please comment to re-activate the pull request. If no action is taken within 7 days, the pull request will be marked closed.

@github-actions github-actions bot added the Stale label Jan 31, 2024
@haeraeus
Copy link

Would love to get this merged too ❤️

@github-actions github-actions bot removed the Stale label Feb 2, 2024
Copy link
Contributor

github-actions bot commented Apr 3, 2024

This pull request has been inactive for 60 days. If the pull request is still relevant please comment to re-activate the pull request. If no action is taken within 7 days, the pull request will be marked closed.

@github-actions github-actions bot added the Stale label Apr 3, 2024
@sebwills
Copy link

sebwills commented Apr 5, 2024

@ibizaman does your 👍 on the bot's post mean you don't think this is worth pursuing any more? Personally I don't need it any more but seems like a useful feature still?

@ibizaman
Copy link
Author

ibizaman commented Apr 5, 2024

@sebwills there was a bit of regnisation indeed. I’m not using this project anymore but I’d be happy to get to merging it if there is some interest for it.

@github-actions github-actions bot removed the Stale label Apr 6, 2024
Copy link
Contributor

github-actions bot commented Jun 5, 2024

This pull request has been inactive for 60 days. If the pull request is still relevant please comment to re-activate the pull request. If no action is taken within 7 days, the pull request will be marked closed.

@pushc6
Copy link

pushc6 commented Jun 25, 2024

I would love to see this feature added, it would be extremely useful.

Copy link
Contributor

This pull request has been inactive for 60 days. If the pull request is still relevant please comment to re-activate the pull request. If no action is taken within 7 days, the pull request will be marked closed.

@github-actions github-actions bot added the Stale label Aug 28, 2024
@github-actions github-actions bot closed this Sep 4, 2024
@tuunit tuunit reopened this Sep 4, 2024
@tuunit tuunit removed the Stale label Sep 4, 2024
@faresismail96
Copy link

would love to have this feature as well!

@ctschubel
Copy link

Coming from louketo-proxy/keycloak-proxy/keycloak-gatekeeper, this feature would help a great deal setting up oauth2-proxy for my apps!

@tonydnguyen7
Copy link

Would love for this to be added in! Hopefully this isn't stale. @JoelSpeed

@dextercai
Copy link

hope to have this feature.

@tuunit tuunit removed this from the v8.0.0 milestone Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.