Repository navigation
OIDC feature for oAuth flow - #11794
MathiasHaudgaard wants to merge 22 commits into
Conversation
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private DecodedJWT authenticateTokenJwk(final String token) throws AuthenticationException { |
There was a problem hiding this comment.
Instead of adding this code to the AuthenticationProvider could we refactor some of this code out into separate classes so there is not a lot of if else checks (i think that makes the code more difficult to follow test and maintain) within the code?
Maybe there can be a TokenDecoder interface and TokenInfo or Token class. Then there can be multiple implementations of the TokenDecoder and the code can be simplified and not care how it works underneath. The TokenInfo or Token class can add more attributes over time but at this point I think we only care about the principal and expiration.
Thoughts?
There was a problem hiding this comment.
Sounds like a good idea 👍
I'll see what I can do 🔍
There was a problem hiding this comment.
@cckellogg I added the feature as a separate plugin instead. This gets rid of most of the if else statements and the jwkEnable flag which makes it a bit cleaner 😄
eolivelli
left a comment
There was a problem hiding this comment.
Thank you very much for this work.
I believe it will be very useful
I have thought more about this new plugin.
I am not sure it is good to add so many third party libraries to the core Pulsar distribution.
I haven't verified if those 3 new Maven artifacts bring in more third party deps
michaeljmarshall
left a comment
There was a problem hiding this comment.
@MathiasHaudgaard - thank you for this draft contribution. Support for dynamically retrieving Public Keys will be a great addition to Pulsar's authentication framework.
I wonder if it'd be worth drafting a Pulsar Improvement Proposal (PIP) to make this new addition to Pulsar's public API. The first step to create a PIP is to start a discussion on the Pulsar Mailing List ([email protected]) describing the problem, your proposed solution, and possibly alternative solutions. Note that the AuthenticationProviderToken class was added in PIP-25. Perhaps @cckellogg and @eolivelli can let me know if it's right to push this change to the mailing list?
| @Override | ||
| public String getAuthMethodName() { | ||
| return TOKEN; | ||
| } |
There was a problem hiding this comment.
To use token as the auth method name is extremely helpful for integrating with Pulsar's OAuth2.0 Client Authentication plugin. However, because the AuthenticationService stores AuthenticationProviders in a map from AuthMethodName to AuthenticationProvider, that means that a broker wouldn't be able to load AuthenticationProviderTokenJWK and AuthenticationProviderToken on the same broker. This nuance would need to be clearly documented.
There was a problem hiding this comment.
Not quite sure what you mean here. Why would you ever want to store AuthenticationProviderTokenJWK and AuthenticationProviderToken on the same broker? ![]()
I used the org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2 plugin to test the implementation 😄
There was a problem hiding this comment.
I could see a use case in the short term (the official helm chart already includes support for properly mounting super user tokens into pulsar components using the basic AuthenticationToken client class), but we can ignore that case, as it'd be possible to add support for mounting in a client id and client secret to enable OAuth2.0 verification from these pulsar components.
Ultimately, my main point is that the nuance here should be documented to prevent users from unexpected behavior and to help them understand which clients and providers are compatible.
There was a problem hiding this comment.
Alright. I'll start documenting the setup I've used. Where should I document it?
|
What about creating a separate Maven Module and use the Maven shade plugin to include all the third party deps in the .jar file for this Provider ? |
| if(!(jwk instanceof RSAPublicKey)){ | ||
| throw new JwtException("key needs to be a RSA Publickey"); | ||
| } |
There was a problem hiding this comment.
@michaeljmarshall I'm expecting the key to be an instance of RSAPublicKey so I want to throw an exception if it's not. I'm not sure if you had something else in mind 😄
I can try this out if it's necessary. @cckellogg @michaeljmarshall I would like your feedback on this on as well 😄 |
|
I don't think I agree with making this its own module. If we did, I'd wonder why it wasn't in its own repository, or some kind of Pulsar Contrib repository. I don't have a fully formed opinion, though. I can provider more feedback tomorrow. |
@michaeljmarshall we have other components that are in the repo and that are not released in the "lib" directory, for instance the offloaders. If this is meant to be a core feature, available out of the box, then it is fine to put it in "lib", otherwise it is better to create a separate module and let users add it to their system if they want. My point here is about not adding "optional" modules to the core distribution. Probably having some high level description of this feature, would help in understanding why this feature is so useful and why we should add it to Pulsar core. Please note that I am not against this feature, I am sure it is useful. I am concerned only about the distribution of this code and the dependencies to the end users |
|
Thanks for your response @eolivelli.
Yes, I think we need more definition for the problem that @MathiasHaudgaard would like to solve with this PR.
The
I don't know all of the use cases of the JWK and JWKs data structure. I know it is used in the OpenID Connect protocol, an extension of the OAuth2.0 protocol, to store public keys that can be used to verify the signature on a JWT. An Identity Provider implementing the OIDC protocol will issue JWTs for an authenticated entity. These tokens will be signed by the Identity Provider using a private key. The Identity Provider will serve the paired Public Keys at an endpoint on the server to make it easy to verify token signatures. The PR currently uses the JWKs endpoint to retrieve a single Identity Provider's public keys to verify the signature of the JWT. For reference, the current By adding support to retrieve a Ultimately, this PR would make it much easier for end users to integrate with an independent Identity Provider service, which is a great feature. However, from my perspective, I think this PR does not go far enough to implement a specific authentication protocol. I know that the JWKS endpoint is used in OpenID Connect. I'm not sure if it is used in any other authentication protocols. I think I would prefer to see a complete implementation of the OpenID Connect protocol (or some other protocol) instead of a partial implementation. The main features that I currently see missing in this implementation (assuming we want to implement the OpenID Connect protocol):
If we do implement the OpenID Connect protocol (or even some other protocol), I think it would certainly deserve to be in the core distribution. @eolivelli - I think the question of which dependencies to use is a valid one that needs to be answered. I am not sure of the right answer, but I do think it'd be worth adding dependencies to get full support of the OpenID Connect protocol. The current 3rd party libraries from Finally, I think we should resolve the asynchronous authentication provider issue that I raised on the dev mailing list before we can merge this PR. Otherwise, any network calls made by this provider will block netty event loop threads. @MathiasHaudgaard - what do you think about my comments? Do you agree that this is meant to implement the OpenID Connect protocol or were you seeking to implement something different? I am not an expert on uses of the JWKS, so your insight will be helpful. @eolivelli - I hope my comment has helped explain some of the context for this PR and its significance. |
@michaeljmarshall, I think you're absolutely right. My knowledge of OIDC is a bit deficient, so I'll look into how I can implement it. Thanks for the feedback! 😃 |
eolivelli
left a comment
There was a problem hiding this comment.
I left some more comments.
Can you post a reference to Licenses for the new libraries ?
You will also have to update the LICENSE files in distribution/server.
We should also verify is there is something to add in the NOTICE file
|
|
||
| @Override | ||
| public void initialize(ServiceConfiguration config) throws IOException, IllegalArgumentException { | ||
| String prefix = (String) config.getProperty(CONF_TOKEN_SETTING_PREFIX); |
There was a problem hiding this comment.
CONF_TOKEN_SETTING_PREFIX is an empty string. is there some bug here ?
There was a problem hiding this comment.
Thanks. This is not needed for this plugin. I'll delete it 😄
| authorizationEnabled=false | ||
|
|
||
| # issuer url that points openid-configurations from a third party provider | ||
| issuerUrl= |
There was a problem hiding this comment.
What about adding some prefix here or some better name that gives a context ?
otherwise this name is not much meaningful if you pick it separated from the context.
This entry is useful only for the new Auth provider
There was a problem hiding this comment.
what about tokenAuthenticationOIDCIssuerUrl ?
|
|
This is very good. |
|
I believe that we are on our way. |
eolivelli
left a comment
There was a problem hiding this comment.
I see that we are missing tests.
I added a couple of comments regarding deps again.
I cannot find any discussions on [email protected] about this new feature.
We should definitely discuss this is the mailing list before accepting the patch
| * HOMEPAGE: | ||
| * https://github.com/google/protobuf | ||
|
|
||
| Contains a modified portion of and optionally depends on 'Protocol Buffers', Google's data |
There was a problem hiding this comment.
is this due to Oauth0 ? in this case please add a reference
There was a problem hiding this comment.
whoops, that was a mistake. Sorry
@eolivelli I'm not quite sure how to test this plugin since it depends on a third-party provider. Any ideas? 😄 |
|
|
@eolivelli I've started on the tests for the plugin. I'm not done yet, but I would like your feedback so I know that I'm heading in the right direction 😄 |
|
|
||
| if (audienceClaim != null && audience == null ) { | ||
| throw new IllegalArgumentException("Token Audience Claim [" + audienceClaim | ||
| + "] configured, but Audience stands for this broker not."); |
There was a problem hiding this comment.
Please rephrase, e.g. ", please also configure Audience".
Even better, don't perform this check and allow an audience claim to be configured without a corresponding audience. Simply do audience validation only if an audience is configured. In that way, the audience claim may have a default configuration of aud without implying that audience validation is enabled.
| private String confTokenAuthClaimSettingName; | ||
| private String confTokenAudienceClaimSettingName; | ||
| private String confTokenAudienceSettingName; | ||
| private String confJWkUrlSettingName; |
There was a problem hiding this comment.
These are constants, why stored as variables?
There was a problem hiding this comment.
My bad. This is basically some clean-up I forgot to do 😅 . Thanks!
| static final String HTTP_HEADER_VALUE_PREFIX = "Bearer "; | ||
|
|
||
| // The token's claim that corresponds to the "role" string | ||
| static final String CONF_TOKEN_AUTH_CLAIM = "tokenAuthClaim"; |
There was a problem hiding this comment.
I would suggest this configuration property be named tokenSubjectClaim and that its default value be sub.
There was a problem hiding this comment.
I would suggest this configuration property be named
tokenSubjectClaim
If I change the name, I need to add another configuration parameter in the conf files, which I'm not a big fan of. tokenAuthClaim is already being used by the AuthenticationProviderToken class so instead of adding a new parameter, we can just use the same. I think it makes more sense imo 😄
and that its default value be
sub.
it already is: L271
There was a problem hiding this comment.
My preference would be to not reuse the configuration parameters that are designed for a different auth provider, unless this provider is designed to emulate or subsume it.
| // The token's claim that corresponds to the "role" string | ||
| static final String CONF_TOKEN_AUTH_CLAIM = "tokenAuthClaim"; | ||
|
|
||
|
|
There was a problem hiding this comment.
Should we add strongly-typed fields to the ServiceConfiguration for these configuration properties? I assume that would improve the categorization, documentation etc.
| static final String CONF_ISSUER_URL = "tokenAuthenticationOIDCIssuerUrl"; | ||
|
|
||
| // When using public key's, the algorithm of the key | ||
| static final String CONF_TOKEN_PUBLIC_ALG = "tokenPublicKeytokenPublicKey"; |
There was a problem hiding this comment.
Not used - CONF_TOKEN_PUBLIC_ALG?
There was a problem hiding this comment.
missing clean-up again 😅
| this.audience = getTokenAudience(config); | ||
| this.issuerUrl = getIssuerUrl(config); | ||
|
|
||
| URL url = new URL(this.issuerUrl+"/.well-known/openid-configuration"); |
There was a problem hiding this comment.
Please don't formulate the URL in this way. The issuer URL is likely to have a trailing slash. Notice how the issuer property in a typical OpenID Configuration document has a trailing slash (example). Some libraries even validate that the issuer property is equal to the configured issuer URL.
Please use an approach like:
URL issuerUrl = getIssuerUrl(config);
URL configUrl = new URL(issuerUrl, "/.well-known/openid-configuration");
| // Parse Token by validating | ||
|
|
||
|
|
||
| role = getPrincipal(authenticateToken(token)); |
There was a problem hiding this comment.
Consider storing the result of authenticateToken into a local variable before invoking getPrincipal. Anticipate that other claims might be extracted from the token in future.
| throw new JwtException("key needs to be a RSA Publickey"); | ||
| } | ||
|
|
||
| algorithm = Algorithm.RSA256((RSAPublicKey) jwk.getPublicKey(), null); |
There was a problem hiding this comment.
May I suggest that the io.jsonwebtoken library be used for token validation. Pulsar already has the dependency. This will take care of applying the correct algorithm based on the key.
It is easy to use, if you implement io.jsonwebtoken.SigningKeyResolver as a wrapper over the UrlJwkProvider. Then you can instantiate a io.jsonwebtoken.JwtParser that correctly validates the token.
this.jwtParser = Jwts.parserBuilder()
.setSigningKeyResolver(signingKeyResolver)
.requireIssuer(issuerUrl)
.build();
...
// parse and validate the token
Jws<Claims> jwt = this.jwtParser.parseClaimsJws(tokenString);
// perform app-specific validations, e.g audience
| } else if (object instanceof String) { | ||
| if (!object.equals(audience)) { | ||
| throw new AuthenticationException("Audiences in token: [" + object | ||
| + "] not contains this broker: " + audience); |
There was a problem hiding this comment.
Regarding the error message, not contains this broker, don't assume that the audience represents the broker because that's an oauth2 thing. It might simply represent the allowed user(s).
To be honest, if you want to support audience validation in an OIDC scenario, the broker configuration should accept a list of allowed audiences.
| } | ||
| } | ||
|
|
||
| if(jwt.getExpiresAt() != null){ |
There was a problem hiding this comment.
If you use the io.jsonwebtoken library, you won't need to do expiration check because it will do it.
commented
Mar 4, 2022
|
The pr had no activity for 30 days, mark with Stale label. |
commented
Mar 4, 2022
|
@MathiasHaudgaard:Thanks for your contribution. For this PR, do we need to update docs? |
commented
Dec 8, 2022
|
Closed as stale and conflict. Please rebase and resubmit the patch if it's still relevant. |
Motivation
This PR adds support for JWKS in the AuthenticationProviderToken class. The reason why I didn't create a new plugin for this, is because I think it's a standard and it's really common to use.
Modifications
I've changed the AuthenticationProviderToken class so you're able to enable JWKS through the conf files.
Does this pull request potentially affect one of the following parts:
If
yeswas chosen, please highlight the changesDocumentation
You can try it out by setting jwkEnabled=true in standalone.conf and then also set the jwkUrl=[the URL for the JWKS]
HELP NEEDED
I can get this to work for the standalone version but when I try to use it in a cluster, it doesn't quite work. I hope someone can help me with this PR so we can merge the JWKS functionality. 😄