Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Support OAUTHBEARER mechanism for KoP #400

Conversation

BewareMyPower
Copy link
Collaborator

@BewareMyPower BewareMyPower commented Mar 11, 2021

This PR introduces OAUTHBEARER mechanism to KoP.

Two config items are added. One is the server callback handler to validate OAUTHBEARER token, the other is the properties configuration file that contains the server callback handler's configs. The default server callback handler is the same as Kafka, which validates unsecured JSON Web Tokens.

In addition, the authenticator is refactored that now it uses different SaslServer for different mechanisms to perform authentication.

Related tests are added to verify these configs and the default server callback handler:

  • KafkaServerConfiguration#testGetKopOauth2Configs: unit test for getKopOauth2Properties method.
  • SaslOAuthBearerTest: test OAUTHBEARER SASL mechanism with Kafka's default server callback handler.
  • CustomOAuthBearerCallbackHandlerTest: test custom AuthenticateCallbackHandler.

@BewareMyPower BewareMyPower changed the title Support OAUTHBEARER mechanism for KoP [WIP] Support OAUTHBEARER mechanism for KoP Mar 11, 2021
@BewareMyPower
Copy link
Collaborator Author

Mark this PR as WIP first. The failed spotbugs check and Codacy check need to fix. And I'll add a mocked server callback handler to verify if the config kopOauth2AuthenticateCallbackHandler works.

@BewareMyPower BewareMyPower changed the title [WIP] Support OAUTHBEARER mechanism for KoP Support OAUTHBEARER mechanism for KoP Mar 11, 2021
@BewareMyPower
Copy link
Collaborator Author

@jiazhai PTAL.

BTW, I found Codacy Static Code Analysis has a bug. It check the code from the diff file not the whole code. See the change of SaslAuthenticator.java and The Codacy report. I removed the fields authRole, authState, and authDataSource but they were reported as error. These fields were added when Codacy was not enabled, so the previous PRs passed the Codacy check but this PR doesn't.

@BewareMyPower BewareMyPower self-assigned this Mar 11, 2021
@BewareMyPower BewareMyPower added the type/feature Indicates new functionality label Mar 11, 2021
@BewareMyPower BewareMyPower merged commit 0484bc5 into streamnative:master Mar 12, 2021
@BewareMyPower BewareMyPower deleted the bewaremypower/add-oauthbearer-mechanism branch March 12, 2021 05:15
BewareMyPower added a commit that referenced this pull request Mar 19, 2021
Based on the framework from #400, this PR adds two callback handlers and their associated configs:
- `OauthLoginCallbackHandler` and `ClientConfig`: They are in an independent module and for Kafka client to get access token from a third-party OAuth 2.0 authorization server. It requires issuer URI, credential file's URI and andience, just like what Pulsar client does.
- `OauthValidatorCallbackHandler` and `ServerConfig`: It validates the client's access token using Pulsar's `AuthenticationProvider` whose `authMethod` is determined by a config of `ServerConfig`.

Since the validate callback handler is created by `SaslServer`, we cannot pass construct params to it. So this PR makes `AuthenticationService` static so that the callback handler could access it.

Also this PR exposed the role (or authorization id) for the implementation of authorization in future.

Unit tests are added for `ServerConfig` and `ClientConfig` and an integration test is added for OAuth 2.0 authentication with the new added callback handlers in this PR.
jiazhai pushed a commit that referenced this pull request Mar 24, 2021
#400 makes KoP support OAUTHBEARER mechanism. Also it introduced two new configs `kopOauth2AuthenticateCallbackHandler` and `kopOauth2ConfigFile`, which are the OAuth 2.0 server callback handler and its config file.

However, after that, if KoP enables PLAIN mechanism, `kopOauth2ConfigFile` must be configured too, because `SaslAuthenticator` tries to load the callback handler and its config file no matter what the configured mechanism are. If user didn't configure them, NPE would be thrown like 

```
java.lang.NullPointerException: null
	at java.util.Properties$LineReader.readLine(Properties.java:434) ~[?:1.8.0_261]
	at java.util.Properties.load0(Properties.java:353) ~[?:1.8.0_261]
	at java.util.Properties.load(Properties.java:341) ~[?:1.8.0_261]
	at io.streamnative.pulsar.handlers.kop.KafkaServiceConfiguration.getKopOauth2Properties(KafkaServiceConfiguration.java:353) ~[?:?]
	at io.streamnative.pulsar.handlers.kop.security.SaslAuthenticator.createOauth2CallbackHandler(SaslAuthenticator.java:174) ~[?:?]
	at io.streamnative.pulsar.handlers.kop.security.SaslAuthenticator.<init>(SaslAuthenticator.java:108) ~[?:?]
```

The existed tests didn't expose this bug because if `kopOauth2ConfigFile` was not configured, it would try to read `kop-oauth2.properties` from the resource directory and the file exists here.

So this PR only loads the OAuth 2.0 server callback handler if the mechanism contain `OAUTHBEARER`. In addition, this PR removes the default file path of `kopOauth2ConfigFile`.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type/feature Indicates new functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants