-
Notifications
You must be signed in to change notification settings - Fork 78
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
[breaking] Variabilized security roles #223
Conversation
@@ -38,6 +36,8 @@ | |||
}) | |||
@SupportedOptions({ "debug" }) | |||
public class RestxAnnotationProcessor extends RestxAbstractProcessor { | |||
private static final Pattern ROLE_PARAM_INTERPOLATOR_REGEX = Pattern.compile("\\{([^}]+)\\}"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't "\\{(.+?)\\}"
more easy to read ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, added 4ce9ac1
I'm ok with the introduction of this new feature. I have some few remarks though.
That been said I didn't used the security API very often, so maybe there is already enough existing anchors to change the security checking mechanism :) |
You're right, updated the PR title to reflect this
I think I don't get what you mean. In what it is more linked to
In that case, currently, you just have to provide your own We may extract this behaviour into a dedicated
That's what I expected initially when I started implementing the feature. However after having played a bit with permissions API, I don't find it relevant for following reasons :
|
So has you explained, everything is tied together, the annotation processor, the So with this PR, for my POV you add a new link between
And as you said, if we want to override the mechanism, we will have to do either a subclass of Again from my POV, I would like to be able to provide a So I know that was not the goal of this PR, but by changing the That being said, I just read the whole implementation one more time, and there is a problem in my proposal, it can not be the Anyway, I hope this message will be clearer than the previous one ;) And again, I never had the need to change the default role management (yet, maybe in the next few month), so I will totally trust your judgement, if you think there is no need to keep the original API. |
mmm I'm not sure I'm getting it, let's bring some code on the table, I think it will be clearer :-) Currently, annotation securityManager.check(request, /* match, <- added in this PR */ anyOf(hasRole("foo"), hasRole("bar_{oid}") /*, hasRole("bar_*") <- added in this PR */)); If I understand what you're meaning, you would like something like : securityManager.check(request, permissionFactory.createPermissionsFor(request, match, "foo", "bar_{oid}")); And it would be Is that what you mean ? |
Almost ;) securityManager.check(request, permissionFactory.createPermissionsFor("foo", "bar")); For a endpoint annotated with And with your PR, for a endpoint annotated with securityManager.check(request, match, permissionFactory.createPermissionsFor("foo", "bar_{oid}", "bar_*")); I would let the cartesian product being made by the annotation processor (like you said, its better to calculate all the roles during compile time), but the interpolation map base on the |
Ok, so maybe something like :
Things I'm not a big fan here, is the fact that it is less "readable" than before : Here, we're adding the I need to think about it a bit more |
For me the permission factory will not interpolate anything, it just creates a public static Permission hasRole(final ImmutableList<String> roles) {
return new Permission() {
public final String TO_STRING = "HAS_ROLE[" + roles + "]";
@Override
public Optional<? extends Permission> has(RestxPrincipal principal, RestxRequest request, RestxRequestMatch match) {
Map<String, String> roleMap = createRoleInterpolationMapFrom(request, match);
for (String role : roles) {
// ... eventually return the permission...
}
return Optional.absent();
}
private Map<String, String> createRoleInterpolationMapFrom(RestxRequest request, RestxRequestMatch match) {
// ...
}
@Override
public String toString() {
return TO_STRING;
}
};
} |
And also I agree to say that it's less readable than before, in the generated router. |
ok got it, so we'd end with something like :
(we may improve readability by using a short var name like I understand now the problem about interpolation map being calculated on every I don't like the solution to make this calculation during What about keeping interpolation map generation in |
As discussed, just updated the PR by refactoring However, I kept map interpolation at the Thus, line
is now looking like :
Later I added some alias method in
It allows to bring back the initial
generated code. However, it brings some potential open doors to NPEs if developper calls by itself some constructors with null- |
Sorry for the late response :/ I'm ok with your modifications. So feel free to merge. |
9b014ba
to
3084b46
Compare
This is intended to be able to use path parameters to interpolate 'dynamic' roles in the future If you implemented your own RestxSecurityManager, you will have to update your check() prototype to take the new RestxRequestMatcher parameter into consideration. If you implemented your own routes relying on the securityManager.check() call, you will have to pass the current restx request matcher to the secrutyManager.check() call.
…reaking]. If you provided your own Permission implementation(s), you will have to update your has() prototype method to include the RestxRequestMatcher
…tcher with an interpolation map. This interpolation map will be made of every query & path parameters
…ole() implementation [breaking] If you were using brackets ({}) in your roles, those will now be interpolated
…th potential wildcard for every variable in role
…interpolation map
…nterpolator regex
…tory component, thus allowing to potentially override hasRole() implementation if needed
…e allowing to have a more readable generated code in generated routes Note that it implies StdEntityRoute requires PermissionFactory instance to be passed to the constructor. A PermissionFactory-less has been kept for backward compatibility and Builder constructor, however it is discouraged to not provide it as calling any of the method aliases will throw a NPE if permissionFactory is not set
…ssionFactory at construction time Deprecated RestxRouter.Builder.addRoute() with no permissionFactory in order to avoid NPEs when checking for security permissions
b63a59a
to
4f61fd2
Compare
This PR allows to define "variabilized" security roles based on what we have in
query params
orpath param
for a dedicated endpoint.It will allow to typically have such endpoint checks :
Changes have been made to
StdRestxSecurityManager
in order to generate arole interpolation map
, basically withquery
&path
parameters (but which may be overwritten with specific stuff if wanted on every project .. you'll only need to@Provide
anotherStdRestxSecurityManager
)By introducing variabilized roles, I added a support for wildcards on roles located in
RestxPrincipal
That is to say, if current user has role named
EDIT_COMPANY_1234_*
he will be able to call every urls like/security/1234/*
without having any403 Forbidden
response.The
wildcard roles
checks are generated at compilation time throughRestxAnnotationProcessor
(because variable cartesian product may take some time to generate, which we would like to avoid at runtime).That is to say, corresponding generated code for endpoint above will be :