forked from streamnative/kop
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multi-tenant support for OAuth authentication (streamnative#1728)
### Motivation Currently, KoP stores all group and offset metadata in one topic, `public/__kafka/__consumer_offsets`. It's not easy to extend and might encounter performance issues for large amount of consumers. > in the long run as consumers keep increasing as all these consumers share the same topic would there be slowness in committing the offsets etc. To solve this issue, we can specify the tenant in `PLAIN` authentication's username, for example: ``` required username="public/default" password="token:xxx"; ``` But when using `OAuth` authentication, there is no way to specify the tenant. In this PR, we will introduce a way to specify tenants on OAuth authentication, and we will add a new property in `credentials_file.json`. For example: ```json { "client_id":"Xd23RHsUnvUlP7wchjNYOaIfazgeHd9x", "client_secret":"rT7ps7WY8uhdVuBTKWZkttwLdQotmdEliaM5rLfmgNibvqziZ-g07ZH52N_poGAb", "tenant": "my_tenant" } ``` **Internal design** The tenant will be encoded to a token sent by the client, the token format will be `{tenant} __with_tenant__{token}`, since the token only allows to `(?<token>[-_\.a-zA-Z0-9]+)`, so here used `__with_tenant__ ` as the delimiter. On the KoP server side, it will try to extract the tenant and token, the tenant will be used as KoP metadata tenant. ### Modifications Add multi-tenant support for OAuth authentication.
- Loading branch information
1 parent
6fe20fb
commit 3d29dff
Showing
18 changed files
with
467 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...l/src/main/java/io/streamnative/pulsar/handlers/kop/security/oauth/OAuthTokenDecoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.streamnative.pulsar.handlers.kop.security.oauth; | ||
|
||
import lombok.NonNull; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
public class OAuthTokenDecoder { | ||
|
||
protected static final String DELIMITER = "__with_tenant_"; | ||
|
||
/** | ||
* Decode the raw token to token and tenant. | ||
* | ||
* @param rawToken Raw token, it contains token and tenant. Format: Tenant + "__with_tenant_" + Token. | ||
* @return Token and tenant pair. Left is token, right is tenant. | ||
*/ | ||
public static Pair<String, String> decode(@NonNull String rawToken) { | ||
final String token; | ||
final String tenant; | ||
// Extract real token. | ||
int idx = rawToken.indexOf(DELIMITER); | ||
if (idx != -1) { | ||
token = rawToken.substring(idx + DELIMITER.length()); | ||
tenant = rawToken.substring(0, idx); | ||
} else { | ||
token = rawToken; | ||
tenant = null; | ||
} | ||
return Pair.of(token, tenant); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...c/test/java/io/streamnative/pulsar/handlers/kop/security/oauth/OAuthTokenDecoderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.streamnative.pulsar.handlers.kop.security.oauth; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertNull; | ||
|
||
import org.apache.commons.lang3.tuple.Pair; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Test {@link OAuthTokenDecoder}. | ||
*/ | ||
public class OAuthTokenDecoderTest { | ||
|
||
@Test | ||
public void testDecode() { | ||
Pair<String, String> tokenAndTenant = OAuthTokenDecoder.decode("my-token"); | ||
assertEquals(tokenAndTenant.getLeft(), "my-token"); | ||
assertNull(tokenAndTenant.getRight()); | ||
tokenAndTenant = OAuthTokenDecoder.decode("my-tenant" + OAuthTokenDecoder.DELIMITER + "my-token"); | ||
assertEquals(tokenAndTenant.getLeft(), "my-token"); | ||
assertEquals(tokenAndTenant.getRight(), "my-tenant"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"client_id": "my-id", | ||
"client_secret": "my-secret" | ||
"client_secret": "my-secret", | ||
"tenant": "my-tenant" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.