Skip to content

OIDC feature for oAuth flow - #11794

Closed
MathiasHaudgaard wants to merge 22 commits into
apache:masterfrom
MathiasHaudgaard:upstream/jwks-feature
Closed

MathiasHaudgaard wants to merge 22 commits into
apache:masterfrom
MathiasHaudgaard:upstream/jwks-feature

Conversation

@MathiasHaudgaard

Copy link
Copy Markdown

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 yes was chosen, please highlight the changes

  • Dependencies (does it add or upgrade a dependency): yes
  • The public API: no
  • The schema: no
  • The default values of configurations: yes
  • The wire protocol: no
  • The rest endpoints: no
  • The admin cli options: no
  • Anything that affects deployment: don't know

Documentation

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. 😄 :octocat:

@MathiasHaudgaard MathiasHaudgaard changed the title Upstream/jwks feature jwks feature for oAuth flow Aug 26, 2021
@MathiasHaudgaard
MathiasHaudgaard marked this pull request as draft August 26, 2021 10:15
Comment thread conf/standalone.conf Resolved
}

@SuppressWarnings("unchecked")
private DecodedJWT authenticateTokenJwk(final String token) throws AuthenticationException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sounds like a good idea 👍

I'll see what I can do 🔍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@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 eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment thread pulsar-broker-common/pom.xml Resolved

@michaeljmarshall michaeljmarshall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@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?

Comment on lines +146 to +149
@Override
public String getAuthMethodName() {
return TOKEN;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not quite sure what you mean here. Why would you ever want to store AuthenticationProviderTokenJWK and AuthenticationProviderToken on the same broker? :shipit:

I used the org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2 plugin to test the implementation 😄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Alright. I'll start documenting the setup I've used. Where should I document it?

@eolivelli

Copy link
Copy Markdown
Contributor

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 ?
This way we are not bundling the provider out of the box but we are adding it to the build.
Users will be able to pick the jar file and put it into "lib" in order to be able to use it
So zero impact to existing users and especially to other users who built something like this and need other versions of the same libraries

Comment on lines +216 to +218
if(!(jwk instanceof RSAPublicKey)){
throw new JwtException("key needs to be a RSA Publickey");
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@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 😄

@MathiasHaudgaard

Copy link
Copy Markdown
Author

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 ?
This way we are not bundling the provider out of the box but we are adding it to the build.
Users will be able to pick the jar file and put it into "lib" in order to be able to use it
So zero impact to existing users and especially to other users who built something like this and need other versions of the same libraries

I can try this out if it's necessary. @cckellogg @michaeljmarshall I would like your feedback on this on as well 😄

@michaeljmarshall

Copy link
Copy Markdown
Member

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.

@eolivelli

Copy link
Copy Markdown
Contributor

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.
Maybe it is just me, that I do not know JWKS and so I do not see the value in adding it to the core distribution, but I see more third party dependencies added to the core distribution.

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

@michaeljmarshall

Copy link
Copy Markdown
Member

Thanks for your response @eolivelli.

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.

Yes, I think we need more definition for the problem that @MathiasHaudgaard would like to solve with this PR.

Maybe it is just me, that I do not know JWKS and so I do not see the value in adding it to the core distribution, but I see more third party dependencies added to the core distribution.

The JWKs data structure is defined in this RFC: https://datatracker.ietf.org/doc/html/rfc7517. The data structure can hold several kinds of cryptographic information, like public keys or private keys. Here is a helpful quote from the introduction:

JWKs and JWK Sets are used in the JSON Web Signature [JWS] and JSON Web Encryption [JWE] specifications.

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 AuthenticationProviderToken class verifies a JWT in the same way that this new class verifies a token (although each uses different libraries). The AuthenticationProviderToken class relies on a public key that is available in to the broker's file system. This implementation is limited because it allows for only one active public key, which means that to rotate keys requires downtime: first to distribute new tokens signed by a new private key and then to update the paired public key in the broker.

By adding support to retrieve a JWKs from an Identity Provider, we will gain the ability to have multiple valid public keys at the same time. That means it'll be possible to rotate keys without downtime. Further, because the public keys are discovered, operators will be able to remove public keys in the event that some part of the chain were compromised.

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):

  1. Discover the issuer's jwks_uri by retrieving it at the issuer's /.well-known/openid-configuration endpoint (https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata)
  2. Implement configurable caching of public keys (https://openid.net/specs/openid-connect-core-1_0.html#RotateEncKeys)
  3. The trusted issuers should be required to use https, by default (it could be optional to override this requirement). This is technically required in the protocol: https://openid.net/specs/openid-connect-discovery-1_0.html.
  4. (Optional) Allow for multiple issuers to be considered "allowed" or "trusted". This is convenient if you're running your identity provider in the same kubernetes cluster due to the way that k8s DNS works. (This component could be debatable, but in the event that we implement number 1, it'd be pretty easy to allow for this implementation too.)
  5. (Optional) Allow for more algorithms than just RS256. Available algorithms are defined here: https://datatracker.ietf.org/doc/html/rfc7518#section-3.1. (This would be easy to add later, so it doesn't need to be a priority now.)

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 auth0 do not support retrieving the the provider metadata from the /.well-known/openid-configuration endpoint. Further, the auth0 library does not use nonblocking asynchronous IO. It'd be great if we could find a library that offers this. I'm not sure if one of these exists for OIDC.

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.

@MathiasHaudgaard

Copy link
Copy Markdown
Author

@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.

@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 eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

CONF_TOKEN_SETTING_PREFIX is an empty string. is there some bug here ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks. This is not needed for this plugin. I'll delete it 😄

Comment thread conf/standalone.conf Outdated
authorizationEnabled=false

# issuer url that points openid-configurations from a third party provider
issuerUrl=

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

maybe OIDCIssuerUrl?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what about tokenAuthenticationOIDCIssuerUrl ?

Comment thread pulsar-broker-common/pom.xml Resolved
@MathiasHaudgaard

Copy link
Copy Markdown
Author

I left some more comments.

Can you post a reference to Licenses for the new libraries ?

https://github.com/auth0/auth0-java/blob/master/LICENSE

@eolivelli

Copy link
Copy Markdown
Contributor

I believe that we are on our way.
Thank you for your patience, we are giving feedback in multiple steps
Good job

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this due to Oauth0 ? in this case please add a reference

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

whoops, that was a mistake. Sorry

Comment thread pulsar-broker-common/pom.xml Resolved
@MathiasHaudgaard

Copy link
Copy Markdown
Author

I see that we are missing tests.

@eolivelli I'm not quite sure how to test this plugin since it depends on a third-party provider. Any ideas? 😄

@eolivelli

Copy link
Copy Markdown
Contributor

I'm not quite sure how to test this plugin since it depends on a third-party provider. Any ideas? 😄
it is possible to create a mock server with WireMock ?
otherwise we could (as last resort) use PowerMock to override the behaviour of Auth0 library

@MathiasHaudgaard

Copy link
Copy Markdown
Author

@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 😄

@MathiasHaudgaard MathiasHaudgaard changed the title jwks feature for oAuth flow OIDC feature for oAuth flow Sep 22, 2021

if (audienceClaim != null && audience == null ) {
throw new IllegalArgumentException("Token Audience Claim [" + audienceClaim
+ "] configured, but Audience stands for this broker not.");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +98 to +101
private String confTokenAuthClaimSettingName;
private String confTokenAudienceClaimSettingName;
private String confTokenAudienceSettingName;
private String confJWkUrlSettingName;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These are constants, why stored as variables?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would suggest this configuration property be named tokenSubjectClaim and that its default value be sub.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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";


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not used - CONF_TOKEN_PUBLIC_ALG?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

missing clean-up again 😅

this.audience = getTokenAudience(config);
this.issuerUrl = getIssuerUrl(config);

URL url = new URL(this.issuerUrl+"/.well-known/openid-configuration");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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);

ghost Sep 24, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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);

ghost Sep 24, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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){

ghost Sep 24, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you use the io.jsonwebtoken library, you won't need to do expiration check because it will do it.

@codelipenghui

ghost commented Mar 4, 2022

Copy link
Copy Markdown
Contributor

The pr had no activity for 30 days, mark with Stale label.

@github-actions

ghost commented Mar 4, 2022

Copy link
Copy Markdown

@MathiasHaudgaard:Thanks for your contribution. For this PR, do we need to update docs?
(The PR template contains info about doc, which helps others know more about the changes. Can you provide doc-related info in this and future PR descriptions? Thanks)

@tisonkun

ghost commented Dec 8, 2022

Copy link
Copy Markdown
Member

Closed as stale and conflict. Please rebase and resubmit the patch if it's still relevant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants