-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
118 additions
and
91 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
source/includes/security/AwsAssumeRoleCredentialProvider.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,38 @@ | ||
public class AwsAssumeRoleCredentialProvider implements CustomCredentialProvider { | ||
|
||
public AwsAssumeRoleCredentialProvider() {} | ||
@Override | ||
public MongoCredential getCustomCredential(Map<?, ?> map) { | ||
AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain(); | ||
Supplier<AwsCredential> awsFreshCredentialSupplier = () -> { | ||
AWSSecurityTokenService stsClient = AWSSecurityTokenServiceAsyncClientBuilder.standard() | ||
.withCredentials(provider) | ||
.withRegion("us-east-1") | ||
.build(); | ||
AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest().withDurationSeconds(3600) | ||
.withRoleArn((String)map.get("mongodbaws.auth.mechanism.roleArn")) | ||
.withRoleSessionName("Test_Session"); | ||
AssumeRoleResult assumeRoleResult = stsClient.assumeRole(assumeRoleRequest); | ||
Credentials creds = assumeRoleResult.getCredentials(); | ||
// Add your code to fetch new credentials | ||
return new AwsCredential(creds.getAccessKeyId(), creds.getSecretAccessKey(), creds.getSessionToken()); | ||
}; | ||
return MongoCredential.createAwsCredential(null, null) | ||
.withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier); | ||
} | ||
|
||
// Validates presence of an ARN | ||
@Override | ||
public void validate(Map<?, ?> map) { | ||
String roleArn = (String) map.get("mongodbaws.auth.mechanism.roleArn"); | ||
if (StringUtils.isNullOrEmpty(roleArn)) { | ||
throw new RuntimeException("Invalid value set for customProperty"); | ||
} | ||
} | ||
|
||
// Initializes the custom provider | ||
@Override | ||
public void init(Map<?, ?> map) { | ||
|
||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
.. _kafka-custom-auth: | ||
|
||
============================== | ||
Custom Authentication Provider | ||
============================== | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: credentials, implementation class, custom class | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 2 | ||
:class: singlecol | ||
|
||
Overview | ||
-------- | ||
|
||
You can add a custom authentication provider by implementing the | ||
``com.mongodb.kafka.connect.util.custom.credentials.CustomCredentialProvider`` | ||
interface. You can use a custom authentication provider with any of the | ||
supported authentication mechanisms. | ||
|
||
To enable this feature, set the following authentication | ||
properties: | ||
|
||
- ``mongo.custom.auth.mechanism.enable``: set to ``true`` | ||
- ``mongo.custom.auth.mechanism.providerClass``: set to the qualified | ||
class name of the implementation class | ||
- *(Optional)* ``mongodbaws.auth.mechanism.roleArn``: set to an Amazon Resource Name (ARN) | ||
|
||
Example | ||
------- | ||
|
||
This section provides a sample authentication provider implementation | ||
class and the corresponding configuration properties and values to | ||
implement the provider. | ||
|
||
The following sample configuration file specifies the | ||
``MONGODB-AWS`` authentication method, adds a custom authentication | ||
provider, and provides an ARN: | ||
|
||
.. code-block:: ini | ||
|
||
connection.uri=<connection string>/?authMechanism=MONGODB-AWS | ||
mongo.custom.auth.mechanism.enable=true | ||
mongo.custom.auth.mechanism.providerClass=sample.AwsAssumeRoleCredentialProvider | ||
mongodbaws.auth.mechanism.roleArn=arn:aws:iam::<account ID>:role/<role name> | ||
|
||
The ``AwsAssumeRoleCredentialProvider`` class defines ``init()`` and | ||
``validate()`` methods that are called when the connector initializes. | ||
The ``getCustomCredential()`` method returns an object of type | ||
``com.mongodb.MongoCredential`` that is used by the ``MongoClient`` | ||
constructed for the connector. The following code defines the custom | ||
authentication provider: | ||
|
||
.. literalinclude:: /includes/security/AwsAssumeRoleCredentialProvider.java | ||
:language: java | ||
|
||
In this example, the ``sample.AwsAssumeRoleCredentialProvider`` | ||
implementation class must be available on the classpath. The | ||
authentication provider class reads the ARN you specify in the | ||
``roleArn`` property. | ||
|
||
To view an example of a ``pom.xml`` file that can build the complete JAR containing | ||
the implementation class, see the `Kafka Connector GitHub repository | ||
README file | ||
<https://github.com/mongodb/mongo-kafka/blob/master/README.md#pom-file-to-build-the-sample-customroleprovider-into-a-jar>`__. |
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