-
Notifications
You must be signed in to change notification settings - Fork 33
Add KoP OAuth example #93
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
Open
Demogorgon314
wants to merge
8
commits into
streamnative:master
Choose a base branch
from
Demogorgon314:Demogorgon314/kop-oauth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1d5694c
Add KoP OAuth example
Demogorgon314 66e779d
Fix stop hydra server command
Demogorgon314 a8bfa36
Fix json format
Demogorgon314 cc3ecfd
Update kop/java/README.md
Demogorgon314 a273561
Change oauth-client version
Demogorgon314 009b452
Use online oauth server in example
Demogorgon314 a9d8417
Use an online OAuth server as in the example
Demogorgon314 bc72559
Fix format
Demogorgon314 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
74 changes: 74 additions & 0 deletions
74
kop/java/src/main/java/io/streamnative/examples/kafka/OAuthConsumer.java
This file contains hidden or 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,74 @@ | ||
| /** | ||
| * 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.examples.kafka; | ||
|
|
||
| import io.streamnative.pulsar.handlers.kop.security.oauth.OauthLoginCallbackHandler; | ||
| import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
| import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
| import org.apache.kafka.common.serialization.StringDeserializer; | ||
| import java.io.IOException; | ||
| import java.nio.file.Paths; | ||
| import java.time.Duration; | ||
| import java.util.Collections; | ||
| import java.util.Properties; | ||
|
|
||
| public class OAuthConsumer { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| // 1. Get the configured parameters from oauth.properties | ||
| final Properties properties = new Properties(); | ||
| properties.load(TokenProducer.class.getClassLoader().getResourceAsStream("oauth.properties")); | ||
| String bootstrapServers = properties.getProperty("bootstrap.servers"); | ||
| String topic = properties.getProperty("topic"); | ||
| String group = properties.getProperty("group"); | ||
| String issuerUrl = properties.getProperty("issuerUrl"); | ||
| String credentialsUrl = properties.getProperty("credentialsUrl"); | ||
| String audience = properties.getProperty("audience"); | ||
|
|
||
| // 2. Create a consumer with OAuth authentication. | ||
| final Properties props = new Properties(); | ||
| props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
| props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
| props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); | ||
| props.put(ConsumerConfig.GROUP_ID_CONFIG, group); | ||
| props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); | ||
| props.setProperty("sasl.login.callback.handler.class", OauthLoginCallbackHandler.class.getName()); | ||
| props.setProperty("security.protocol", "SASL_PLAINTEXT"); | ||
| props.setProperty("sasl.mechanism", "OAUTHBEARER"); | ||
| final String jaasTemplate = "org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required" | ||
| + " oauth.issuer.url=\"%s\"" | ||
| + " oauth.credentials.url=\"%s\"" | ||
| + " oauth.audience=\"%s\";"; | ||
| props.setProperty("sasl.jaas.config", String.format(jaasTemplate, | ||
| issuerUrl, | ||
| "file://" + Paths.get(credentialsUrl).toAbsolutePath(), | ||
| audience | ||
| )); | ||
| final KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); | ||
| consumer.subscribe(Collections.singleton(topic)); | ||
|
|
||
| // 3. Consume some messages and quit immediately | ||
| boolean running = true; | ||
| while (running) { | ||
| final ConsumerRecords<String, String> records = consumer.poll(Duration.ofSeconds(1)); | ||
| if (!records.isEmpty()) { | ||
| records.forEach(record -> System.out.println("Receive record: " + record.value() + " from " | ||
| + record.topic() + "-" + record.partition() + "@" + record.offset())); | ||
| running = false; | ||
| } | ||
| } | ||
| consumer.close(); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
kop/java/src/main/java/io/streamnative/examples/kafka/OAuthProducer.java
This file contains hidden or 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,65 @@ | ||
| /** | ||
| * 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.examples.kafka; | ||
|
|
||
| import io.streamnative.pulsar.handlers.kop.security.oauth.OauthLoginCallbackHandler; | ||
| import org.apache.kafka.clients.producer.KafkaProducer; | ||
| import org.apache.kafka.clients.producer.ProducerConfig; | ||
| import org.apache.kafka.clients.producer.ProducerRecord; | ||
| import org.apache.kafka.clients.producer.RecordMetadata; | ||
| import org.apache.kafka.common.serialization.StringSerializer; | ||
| import java.io.IOException; | ||
| import java.nio.file.Paths; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.Future; | ||
|
|
||
| public class OAuthProducer { | ||
|
|
||
| public static void main(String[] args) throws IOException, ExecutionException, InterruptedException { | ||
| // 1. Get the configured parameters from oauth.properties | ||
| final Properties properties = new Properties(); | ||
| properties.load(OAuthProducer.class.getClassLoader().getResourceAsStream("oauth.properties")); | ||
| String bootstrapServers = properties.getProperty("bootstrap.servers"); | ||
| String topic = properties.getProperty("topic"); | ||
| String issuerUrl = properties.getProperty("issuerUrl"); | ||
| String credentialsUrl = properties.getProperty("credentialsUrl"); | ||
| String audience = properties.getProperty("audience"); | ||
|
|
||
| // 2. Create a producer with OAuth authentication. | ||
| final Properties props = new Properties(); | ||
| props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
| props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
| props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); | ||
| props.setProperty("sasl.login.callback.handler.class", OauthLoginCallbackHandler.class.getName()); | ||
| props.setProperty("security.protocol", "SASL_PLAINTEXT"); | ||
| props.setProperty("sasl.mechanism", "OAUTHBEARER"); | ||
| final String jaasTemplate = "org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule required" | ||
| + " oauth.issuer.url=\"%s\"" | ||
| + " oauth.credentials.url=\"%s\"" | ||
| + " oauth.audience=\"%s\";"; | ||
| props.setProperty("sasl.jaas.config", String.format(jaasTemplate, | ||
| issuerUrl, | ||
| "file://" + Paths.get(credentialsUrl).toAbsolutePath(), | ||
| audience | ||
| )); | ||
| final KafkaProducer<String, String> producer = new KafkaProducer<>(props); | ||
|
|
||
| // 3. Produce one message | ||
| final Future<RecordMetadata> recordMetadataFuture = producer.send(new ProducerRecord<>(topic, "hello")); | ||
| final RecordMetadata recordMetadata = recordMetadataFuture.get(); | ||
| System.out.println("Send hello to " + recordMetadata); | ||
| producer.close(); | ||
| } | ||
| } |
This file contains hidden or 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,35 @@ | ||
| # | ||
| # 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. | ||
| # | ||
|
|
||
| version: '3' | ||
|
|
||
| services: | ||
|
|
||
| hydra: | ||
| image: oryd/hydra:v1.11.7 | ||
| container_name: hydra | ||
| ports: | ||
| - 4444:4444 | ||
| - 4445:4445 | ||
| - 9020:9020 | ||
| command: serve all --dangerous-force-http | ||
| environment: | ||
| - DSN=memory | ||
| - URLS_SELF_ISSUER=http://127.0.0.1:4444/ | ||
| - URLS_CONSENT=http://127.0.0.1:9020/consent | ||
| - URLS_LOGIN=http://127.0.0.1:9020/login | ||
| - SERVE_PUBLIC_PORT=4444 | ||
| - SERVE_ADMIN_PORT=4445 | ||
| - STRATEGIES_ACCESS_TOKEN=jwt | ||
| - OIDC_SUBJECT_IDENTIFIERS_SUPPORTED_TYPES=public |
This file contains hidden or 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,12 @@ | ||
| { | ||
| "alg": "RS256", | ||
| "d": "eojGh8GMfF-g3eloMhHCRsqn01T1YRjbiyLNOfZO6xelUo8hlEtGyZR9oMVNml4k-cVxtO-3PuTstMbhU95Z3XcbhxMCQNZcWxKhVHK436A9wpGoyY1p7EKO1qXkNmSpBD_sxMbsS7qz3YpAUp9WmdsxhuuOnzp6TB3PQW8jIOwODZpblYbO8M8PYloH2nq85CDlzBvO_0YaSNE_7CK6Uevt4edUQyXfjzGCs-vbJd5Hrbb_p1B4z5BJzMBxTOS36H1B_w4boycEoCJabEiaGQojQSqpxBbwHYmJGGu-ycaruRahdMKeosQyV3_tTPJqm2OFGRrqNvR3k1zIHkU_1PUbyFQouhp36n87uPUDGqeTJ-fzK7XkcDy27XFwoDmCDdsBHiLl4Z-608jc8DePmtPd4DrYr4nxQxLzSO8LWZOmvxnkLPXd4qO-_XSBx7kQQpbdHYFXysZ_A_YVFtJPD8_2qXux-o_hbJxDUiNLUr6SPiIVTgkYbzShLyh1uyVWu-rXUE1lQl9JEVAiNOq4S_ZjRRyG_GEJeozIAfeX_rKn1bMOPdDSifc70tcY9ZDmJmYsMp0obNtk7x4VMbxS--wiwQiTASaE5tt1DTGzp0BZR-eo3WwXXCE-1Wd1-rUSjNt08kSksha8rBQgV6bqUIsMtZ6NrBTLY2pI_qGPjUE", | ||
| "e": "AQAB", | ||
| "kid": "private:7e9cbc3a-20f4-44aa-831c-75471946a7dc", | ||
| "kty": "RSA", | ||
| "n": "4g8rgGslfLNGdfh94KbfsMPjgX17nnEHnCLhrlVyA-jxSThiQyQVQCkZfav9k4cLCiKdoqxKtLV0RA3hWXGHE0qUNUJWVN3vz3NOI7ccEHBJHzbDk24NYxsW7M6zNfBfTc6ZrJr5XENy7emscODn8HJ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veXgK3CB-lX2BzQEyexh2xgf6NAZorFruohc-KeD2YGqH4XMYPOAgjRgaGgEvV8RNdNOA_qUedFQkIz_XWoB93TmYmxr7ezIkDGjKcJ0Cmb0kpGY_gD5tm7xec6WcQAkMaasV7cCvV9bE19wd1_4TXYW-4UWBvy0tTNmmNMbQu7tKbw", | ||
| "use": "sig", | ||
| "x5c": null | ||
| } |
This file contains hidden or 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,9 @@ | ||
| { | ||
| "alg": "RS256", | ||
| "e": "AQAB", | ||
| "kid": "public:7e9cbc3a-20f4-44aa-831c-75471946a7dc", | ||
| "kty": "RSA", | ||
| "n": "4g8rgGslfLNGdfh94KbfsMPjgX17nnEHnCLhrlVyA-jxSThiQyQVQCkZfav9k4cLCiKdoqxKtLV0RA3hWXGHE0qUNUJWVN3vz3NOI7ccEHBJHzbDk24NYxsW7M6zNfBfTc6ZrJr5XENy7emscODn8HJ2Qf1UkMUeze5EirJ2lsB9Zzo1GIw9ZU65W9HWWcgS5sL9eHlDRbVLmgph7jRzkQJGm2hOeyiE-ufUOWkBQH49BhKaNGfjZ8BOJ1WRsbIIVtwhS7m-HSIKmglboG-onNd5LYAmngbkCuhwjJajBQayxkeBeumvRQACC1-mKC5KaW40JmVRKFFHDcf892t6GX6c7PaVWPqvf2l6nYRbYT9nl4fQK1aUTiCqrPf2-WjEH1JIEwTfFZKTwpTtlr3ejGJMT7wH2L4uFbpguKawTo4lYHWN3IsryDfUVvNbb7l8KMqiuDIy-5R6WezajsCYI_GzvLGCYO1EnRTDFdEmipfbNT2_D91OPKNGmZLVUkVVlL0z-1iQtwfRamn2oRNHzMYMAplGikxrQld_IPUIbKjgtLWPDnfskoWvuCIDQdRzMpxAXa3O_cq5uQRpu2o8xZ8RYWixxrIGc1_8m-QQLy7DwcmVd0dGU29S-fnfOzWr43KWlyWfGsBLFxUkltjY6gx6oB6tsQVC3Cy5Eku8Fdc", | ||
| "use": "sig", | ||
| "x5c": null | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.