-
Notifications
You must be signed in to change notification settings - Fork 338
Add azure_federated authentication to Kafka source #7011
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
bagmarnikhil
wants to merge
2
commits into
opensearch-project:main
Choose a base branch
from
bagmarnikhil:feature/kafka-azure-federated-oauthbearer
base: main
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 all commits
Commits
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
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
35 changes: 35 additions & 0 deletions
35
...rg/opensearch/dataprepper/plugins/kafka/authenticator/AwsCredentialsSupplierProvider.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,35 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.plugins.kafka.authenticator; | ||
|
|
||
| import org.opensearch.dataprepper.aws.api.AwsCredentialsSupplier; | ||
|
|
||
| public class AwsCredentialsSupplierProvider { | ||
| private static final AwsCredentialsSupplierProvider singleton = new AwsCredentialsSupplierProvider(); | ||
|
|
||
| public static AwsCredentialsSupplierProvider getInstance() { | ||
| return singleton; | ||
| } | ||
|
|
||
| // set once before Kafka client construction; volatile suffices for cross-thread visibility | ||
| private volatile AwsCredentialsSupplier awsCredentialsSupplier; | ||
|
|
||
| // Used for testing only | ||
| protected AwsCredentialsSupplierProvider() {} | ||
|
|
||
| public AwsCredentialsSupplier getAwsCredentialsSupplier() { | ||
| return awsCredentialsSupplier; | ||
| } | ||
|
|
||
| public void set(final AwsCredentialsSupplier awsCredentialsSupplier) { | ||
| this.awsCredentialsSupplier = awsCredentialsSupplier; | ||
| } | ||
| } |
97 changes: 97 additions & 0 deletions
97
...org/opensearch/dataprepper/plugins/kafka/authenticator/AzureFederatedCallbackHandler.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,97 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.plugins.kafka.authenticator; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import org.apache.kafka.common.security.auth.AuthenticateCallbackHandler; | ||
| import org.apache.kafka.common.security.oauthbearer.OAuthBearerLoginModule; | ||
| import org.apache.kafka.common.security.oauthbearer.OAuthBearerTokenCallback; | ||
| import org.opensearch.dataprepper.aws.api.AwsCredentialsSupplier; | ||
|
|
||
| import javax.security.auth.callback.Callback; | ||
| import javax.security.auth.callback.UnsupportedCallbackException; | ||
| import javax.security.auth.login.AppConfigurationEntry; | ||
| import java.io.IOException; | ||
| import java.util.Base64; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class AzureFederatedCallbackHandler implements AuthenticateCallbackHandler { | ||
| public static final String OPT_REGION = "azureFederatedRegion"; | ||
| public static final String OPT_STS_ROLE_ARN = "azureFederatedStsRoleArn"; | ||
| public static final String OPT_TOKEN_ENDPOINT = "azureFederatedTokenEndpoint"; | ||
| public static final String OPT_CLIENT_ID = "azureFederatedClientId"; | ||
| public static final String OPT_SCOPE = "azureFederatedScope"; | ||
| public static final String OPT_STS_HEADER_OVERRIDES = "azureFederatedStsHeaderOverrides"; | ||
|
|
||
| private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
|
||
| private AzureFederatedTokenProvider tokenProvider; | ||
|
|
||
| public AzureFederatedCallbackHandler() { | ||
| } | ||
|
|
||
| AzureFederatedCallbackHandler(final AzureFederatedTokenProvider tokenProvider) { | ||
| this.tokenProvider = tokenProvider; | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public void configure(final Map<String, ?> configs, final String saslMechanism, | ||
| final List<AppConfigurationEntry> jaasConfigEntries) { | ||
| if (!OAuthBearerLoginModule.OAUTHBEARER_MECHANISM.equals(saslMechanism)) { | ||
| throw new IllegalArgumentException("Unexpected SASL mechanism: " + saslMechanism); | ||
| } | ||
| final Map<String, String> options = (Map<String, String>) jaasConfigEntries.get(0).getOptions(); | ||
| final Map<String, String> stsHeaderOverrides = decodeStsHeaderOverrides(options.get(OPT_STS_HEADER_OVERRIDES)); | ||
| final AwsCredentialsSupplier awsCredentialsSupplier = | ||
| AwsCredentialsSupplierProvider.getInstance().getAwsCredentialsSupplier(); | ||
| this.tokenProvider = new AzureFederatedTokenProvider( | ||
| options.get(OPT_REGION), options.get(OPT_STS_ROLE_ARN), options.get(OPT_TOKEN_ENDPOINT), | ||
| options.get(OPT_CLIENT_ID), options.get(OPT_SCOPE), stsHeaderOverrides, awsCredentialsSupplier); | ||
| } | ||
|
|
||
| private static Map<String, String> decodeStsHeaderOverrides(final String encoded) { | ||
| if (encoded == null || encoded.isEmpty()) { | ||
| return Collections.emptyMap(); | ||
| } | ||
| try { | ||
| final byte[] json = Base64.getDecoder().decode(encoded); | ||
| return OBJECT_MAPPER.readValue(json, new TypeReference<Map<String, String>>() {}); | ||
| } catch (final IOException e) { | ||
| throw new RuntimeException("Failed to decode azure_federated STS header overrides", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(final Callback[] callbacks) throws UnsupportedCallbackException { | ||
| for (final Callback callback : callbacks) { | ||
| if (callback instanceof OAuthBearerTokenCallback) { | ||
| ((OAuthBearerTokenCallback) callback).token(tokenProvider.getToken()); | ||
| } else { | ||
| throw new UnsupportedCallbackException(callback); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
|
bagmarnikhil marked this conversation as resolved.
bagmarnikhil marked this conversation as resolved.
|
||
| if (tokenProvider != null) { | ||
| tokenProvider.close(); | ||
| } | ||
| } | ||
|
|
||
| AzureFederatedTokenProvider getTokenProvider() { | ||
| return tokenProvider; | ||
| } | ||
| } | ||
57 changes: 57 additions & 0 deletions
57
...rg/opensearch/dataprepper/plugins/kafka/authenticator/AzureFederatedOAuthBearerToken.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,57 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| * | ||
| */ | ||
|
|
||
| package org.opensearch.dataprepper.plugins.kafka.authenticator; | ||
|
|
||
| import org.apache.kafka.common.security.oauthbearer.OAuthBearerToken; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| public class AzureFederatedOAuthBearerToken implements OAuthBearerToken { | ||
| private final String value; | ||
| private final long lifetimeMs; | ||
| private final long startTimeMs; | ||
| private final String scope; | ||
| private final String principalName; | ||
|
|
||
| public AzureFederatedOAuthBearerToken(final String value, final long expiresInSeconds, | ||
| final String scope, final String principalName) { | ||
| this.value = value; | ||
| this.startTimeMs = System.currentTimeMillis(); | ||
| this.lifetimeMs = this.startTimeMs + (expiresInSeconds * 1000L); | ||
| this.scope = scope; | ||
| this.principalName = principalName; | ||
| } | ||
|
|
||
| @Override | ||
| public String value() { | ||
| return value; | ||
| } | ||
|
|
||
| @Override | ||
| public long lifetimeMs() { | ||
| return lifetimeMs; | ||
| } | ||
|
|
||
| @Override | ||
| public Long startTimeMs() { | ||
| return startTimeMs; | ||
| } | ||
|
|
||
| @Override | ||
| public String principalName() { | ||
| return principalName; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> scope() { | ||
| return scope == null ? Set.of() : Set.of(scope); | ||
| } | ||
| } |
Oops, something went wrong.
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.