@@ -79,92 +79,8 @@ replace:
79
79
and placeholder value.
80
80
| *Optional*
81
81
82
- Custom Authentication Provider
83
- ------------------------------
82
+ .. tip:: Custom Authentication Provider
84
83
85
- You can add a custom authentication provider by implementing the
86
- ``com.mongodb.kafka.connect.util.custom.credentials.CustomCredentialProvider``
87
- interface. To enable this feature, set the following authentication
88
- properties:
89
-
90
- - ``mongo.custom.auth.mechanism.enable``: set to ``true``
91
- - ``mongo.custom.auth.mechanism.providerClass``: set to the qualified
92
- class name of the implementation class
93
-
94
- Depending on the design of your implementation class, you might also
95
- set the ``mongodbaws.auth.mechanism.roleArn`` property, which
96
- provides the Amazon Resource Name (ARN).
97
-
98
- Example
99
- ~~~~~~~
100
-
101
- This section provides a sample authentication provider implementation
102
- class and the corresponding configuration properties and values to
103
- implement the provider.
104
-
105
- The following code specifies the configuration properties to use the
106
- ``MONGODB-AWS`` authentication method and add a custom authentication
107
- provider:
108
-
109
- .. code-block:: ini
110
-
111
- connection.uri=<connection string>/?authMechanism=MONGODB-AWS
112
- mongo.custom.auth.mechanism.enable=true
113
- mongo.custom.auth.mechanism.providerClass=sample.AwsAssumeRoleCredentialProvider
114
- mongodbaws.auth.mechanism.roleArn=arn:aws:iam::<account ID>:role/<role name>
115
-
116
- The ``AwsAssumeRoleCredentialProvider`` class defines ``init()`` and
117
- ``validate()`` methods that are called when the connector initializes.
118
- The ``getCustomCredential()`` method returns an object of type
119
- ``com.mongodb.MongoCredential`` that is used by the ``MongoClient``
120
- constructed for the connector. The following code defines the custom
121
- authentication provider:
122
-
123
- .. code-block:: java
124
-
125
- public class AwsAssumeRoleCredentialProvider implements CustomCredentialProvider {
126
-
127
- public AwsAssumeRoleCredentialProvider() {}
128
- @Override
129
- public MongoCredential getCustomCredential(Map<?, ?> map) {
130
- AWSCredentialsProvider provider = new DefaultAWSCredentialsProviderChain();
131
- Supplier<AwsCredential> awsFreshCredentialSupplier = () -> {
132
- AWSSecurityTokenService stsClient = AWSSecurityTokenServiceAsyncClientBuilder.standard()
133
- .withCredentials(provider)
134
- .withRegion("us-east-1")
135
- .build();
136
- AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest().withDurationSeconds(3600)
137
- .withRoleArn((String)map.get("mongodbaws.auth.mechanism.roleArn"))
138
- .withRoleSessionName("Test_Session");
139
- AssumeRoleResult assumeRoleResult = stsClient.assumeRole(assumeRoleRequest);
140
- Credentials creds = assumeRoleResult.getCredentials();
141
- // Add your code to fetch new credentials
142
- return new AwsCredential(creds.getAccessKeyId(), creds.getSecretAccessKey(), creds.getSessionToken());
143
- };
144
- return MongoCredential.createAwsCredential(null, null)
145
- .withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier);
146
- }
147
-
148
- @Override
149
- public void validate(Map<?, ?> map) {
150
- String roleArn = (String) map.get("mongodbaws.auth.mechanism.roleArn");
151
- if (StringUtils.isNullOrEmpty(roleArn)) {
152
- throw new RuntimeException("Invalid value set for customProperty");
153
- }
154
- }
155
-
156
- @Override
157
- public void init(Map<?, ?> map) {
158
-
159
- }
160
- }
161
-
162
- In this example, the ``sample.AwsAssumeRoleCredentialProvider``
163
- implementation class must be available on the classpath. The
164
- authentication provider class reads the ARN you specify in the
165
- ``roleArn`` property.
166
-
167
- To view an example of a ``pom.xml`` file that can build the complete JAR containing
168
- the implementation class, see the `Kafka Connector GitHub repository
169
- README file
170
- <https://github.com/mongodb/mongo-kafka/blob/master/README.md#pom-file-to-build-the-sample-customroleprovider-into-a-jar>`__.
84
+ You can create and use a custom authentication provider to support
85
+ AWS IAM authentication. To learn more, see the
86
+ :ref:`kafka-custom-auth` guide.
0 commit comments