Skip to content

Commit a48ff57

Browse files
author
Chris Cho
authored
DOCSP-19126: url encode admonition (#215)
* DOCSP-19126: url encode admonition
1 parent ca25ee4 commit a48ff57

File tree

4 files changed

+146
-56
lines changed

4 files changed

+146
-56
lines changed

source/fundamentals/auth.txt

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,24 @@ in the following order:
254254
The following code snippets show how to specify the authentication mechanism,
255255
using the following placeholders:
256256

257-
* ``username`` - value of your ``AWS_ACCESS_KEY_ID``.
258-
* ``password`` - value of your ``AWS_SECRET_ACCESS_KEY``.
257+
* ``awsKeyId`` - value of your ``AWS_ACCESS_KEY_ID``.
258+
* ``awsSecretKey`` - value of your ``AWS_SECRET_ACCESS_KEY``.
259259
* ``atlasUri`` - network address of your MongoDB Atlas instance.
260260
* ``awsSessionToken`` - value of your ``AWS_SESSION_TOKEN``. *(optional)*
261261

262+
.. important:: URL-encode Your Credentials
263+
264+
Make sure to URL-encode your credentials to prevent backslash or other
265+
characters from causing parsing errors. The following code example
266+
shows you how to URL-encode a sample string, represented by the placeholder
267+
``fieldValue``:
268+
269+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
270+
:language: java
271+
:dedent:
272+
:start-after: start urlEncode
273+
:end-before: end urlEncode
274+
262275
Select the :guilabel:`Connection String` or the :guilabel:`MongoCredential`
263276
tab below for instructions and sample code for specifying this authentication
264277
mechanism:
@@ -270,22 +283,25 @@ mechanism:
270283

271284
To specify the ``MONGODB-AWS`` authentication mechanism using a
272285
connection string, assign the ``authMechanism`` parameter the value
273-
``MONGODB-AWS`` in your connection string. Your code to instantiate
286+
``"MONGODB-AWS"`` in your connection string. Your code to instantiate
274287
a ``MongoClient`` should look something like this:
275288

276-
.. code-block:: java
277-
278-
MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<atlasUri>?authMechanism=MONGODB-AWS");
289+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
290+
:language: java
291+
:dedent:
292+
:start-after: start connectionString
293+
:end-before: end connectionString
279294

280295
If you need to specify an AWS session token, include it in the
281296
``authMechanismProperties`` parameter as follows using the format
282297
``AWS_SESSION_TOKEN:<awsSessionToken>``. Your code to instantiate
283298
a ``MongoClient`` with a session token should look something like this:
284299

285-
.. code-block:: java
286-
287-
MongoClient mongoClient = MongoClients.create("mongodb://<username>:<password>@<atlasUri>?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>");
288-
300+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
301+
:language: java
302+
:dedent:
303+
:start-after: start connectionStringSessionToken
304+
:end-before: end connectionStringSessionToken
289305

290306
.. tab::
291307
:tabid: MongoCredential
@@ -295,7 +311,11 @@ mechanism:
295311
`createAwsCredential() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#createAwsCredential(java.lang.String,char%5B%5D)>`__
296312
method. Your code to instantiate a ``MongoClient`` should look something like this:
297313

298-
.. include:: /includes/fundamentals/code-snippets/auth-credentials-aws.rst
314+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
315+
:language: java
316+
:dedent:
317+
:start-after: start mongoCredential
318+
:end-before: end mongoCredential
299319

300320
If you need to specify an AWS session token, you can add it using
301321
one of the following choices:
@@ -310,7 +330,11 @@ mechanism:
310330
`applyConnectionString() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#applyConnectionString(com.mongodb.ConnectionString)>`__
311331
method as follows:
312332

313-
.. include:: /includes/fundamentals/code-snippets/auth-credentials-aws-session.rst
333+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
334+
:language: java
335+
:dedent:
336+
:start-after: start mongoCredentialSessionTokenConnString
337+
:end-before: end mongoCredentialSessionTokenConnString
314338

315339
- **Specify your AWS session token in a MongoCredential.**
316340

@@ -319,15 +343,17 @@ mechanism:
319343
`withMechanismProperty() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCredential.html#withMechanismProperty(java.lang.String,T)>`__
320344
method as shown below:
321345

322-
.. code-block:: java
323-
324-
MongoCredential.createAwsCredential("<username>", "<password>".toCharArray()) .withMechanismProperty("AWS_SESSION_TOKEN", "<awsSessionToken>");
346+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
347+
:language: java
348+
:dedent:
349+
:start-after: start mongoCredentialSessionTokenCredential
350+
:end-before: end mongoCredentialSessionTokenCredential
325351

326352
- **Specify your AWS session token in an environment variable.**
327353

328354
In your client execution environment, set an environment variable
329355
called ``AWS_SESSION_TOKEN`` and assign your token to it. The value is
330-
automatically picked up by your MongoClient when you specify the
356+
automatically picked up by your ``MongoClient`` when you specify the
331357
``MONGODB-AWS`` authentication mechanism.
332358

333359
Refresh Credentials
@@ -336,24 +362,12 @@ Refresh Credentials
336362
The driver supports refreshing credentials for cases such as assuming roles
337363
or using `Elastic Kubernetes Service <https://docs.aws.amazon.com/eks/latest/userguide/what-is-eks.html>`__.
338364

339-
340-
.. code-block:: java
341-
:emphasize-lines: 3-4, 8
342-
343-
Supplier<AwsCredential> awsFreshCredentialSupplier = () -> {
344-
// Code to fetch fresh credentials, such as assuming a role using the AWS SDK.
345-
// Ensure you return the temporary credentials.
346-
return new AwsCredential("<accessKeyId>", "<secretAccessKey>", "<sessionToken>");
347-
};
348-
349-
MongoCredential credential = MongoCredential.createAwsCredential(null, null)
350-
.withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier);
351-
MongoClient mongoClient = MongoClients.create(
352-
MongoClientSettings.builder()
353-
.applyToClusterSettings(builder ->
354-
builder.hosts(Collections.singletonList(new ServerAddress("<hostname>", 27017))))
355-
.credential(credential)
356-
.build());
365+
.. literalinclude:: /includes/fundamentals/code-snippets/MongoDbAwsAuth.java
366+
:language: java
367+
:dedent:
368+
:start-after: start refreshCredentials
369+
:end-before: end refreshCredentials
370+
:emphasize-lines: 4-5, 9
357371

358372
.. _x509-auth-mechanism:
359373

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package other;
2+
3+
import java.io.UnsupportedEncodingException;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.function.Supplier;
7+
8+
import com.mongodb.AwsCredential;
9+
import com.mongodb.ConnectionString;
10+
import com.mongodb.MongoClientSettings;
11+
import com.mongodb.MongoCredential;
12+
import com.mongodb.ServerAddress;
13+
import com.mongodb.client.MongoClient;
14+
import com.mongodb.client.MongoClients;
15+
16+
public class MongoDbAwsAuth {
17+
18+
// Placeholder functions
19+
20+
private static void encodeText() throws UnsupportedEncodingException {
21+
// start urlEncode
22+
String encodedField = java.net.URLEncoder.encode("<fieldValue>".toString(), "ISO-8859-1");
23+
// end urlEncode
24+
}
25+
private static void connectionString() {
26+
// start connectionString
27+
MongoClient mongoClient = MongoClients.create("mongodb://<awsKeyId>:<awsSecretKey>@<atlasUri>?authMechanism=MONGODB-AWS");
28+
// end connectionString
29+
}
30+
31+
private static void connectionStringSessionToken() {
32+
// start connectionStringSessionToken
33+
MongoClient mongoClient = MongoClients.create("mongodb://<awsKeyId>:<awsSecretKey>@<atlasUri>?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>");
34+
// end connectionStringSessionToken
35+
}
36+
37+
private static void mongoCredential() {
38+
// start mongoCredential
39+
MongoCredential credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray());
40+
41+
MongoClient mongoClient = MongoClients.create(
42+
MongoClientSettings.builder()
43+
.applyToClusterSettings(builder ->
44+
builder.hosts(Arrays.asList(new ServerAddress("<atlasUri>"))))
45+
.credential(credential)
46+
.build());
47+
// end mongoCredential
48+
}
49+
50+
private static void mongoCredentialSessionTokenConnString() {
51+
// start mongoCredentialSessionTokenConnString
52+
MongoCredential credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray());
53+
ConnectionString connectionString = new ConnectionString("mongodb://<atlasUri>/?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>");
54+
55+
MongoClient mongoClient = MongoClients.create(
56+
MongoClientSettings.builder()
57+
.applyConnectionString(connectionString)
58+
.credential(credential)
59+
.build());
60+
// end mongoCredentialSessionTokenConnString
61+
}
62+
63+
private static void mongoCredentialSessionTokenCredential() {
64+
// start mongoCredentialSessionTokenCredential
65+
MongoCredential credential = MongoCredential.createAwsCredential("<awsKeyId>", "<awsSecretKey>".toCharArray()).withMechanismProperty("AWS_SESSION_TOKEN", "<awsSessionToken>");
66+
ConnectionString connectionString = new ConnectionString("mongodb://<atlasUri>/?authMechanism=MONGODB-AWS");
67+
68+
MongoClient mongoClient = MongoClients.create(
69+
MongoClientSettings.builder()
70+
.applyConnectionString(connectionString)
71+
.credential(credential)
72+
.build());
73+
// end mongoCredentialSessionTokenCredential
74+
}
75+
76+
private static void refreshCredentials() {
77+
78+
// start refreshCredentials
79+
Supplier<AwsCredential> awsFreshCredentialSupplier = () -> {
80+
// Add your code to fetch new credentials, such as assuming a role using the AWS SDK.
81+
82+
// Ensure you return the temporary credentials.
83+
return new AwsCredential("<awsKeyId>", "<awsSecretKey>", "<awsSessionToken>");
84+
};
85+
86+
MongoCredential credential = MongoCredential.createAwsCredential(null, null)
87+
.withMechanismProperty(MongoCredential.AWS_CREDENTIAL_PROVIDER_KEY, awsFreshCredentialSupplier);
88+
MongoClient mongoClient = MongoClients.create(
89+
MongoClientSettings.builder()
90+
.applyToClusterSettings(builder ->
91+
builder.hosts(Collections.singletonList(new ServerAddress("<hostname>", 27017))))
92+
.credential(credential)
93+
.build());
94+
// end refreshCredentials
95+
96+
}
97+
public static void main(String[] args) { }
98+
}

source/includes/fundamentals/code-snippets/auth-credentials-aws-session.rst

Lines changed: 0 additions & 11 deletions
This file was deleted.

source/includes/fundamentals/code-snippets/auth-credentials-aws.rst

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)