@@ -18,32 +18,38 @@ public class MongoDbAwsAuth {
18
18
// Placeholder functions
19
19
20
20
private static void encodeText () throws UnsupportedEncodingException {
21
+ // Defines the encoding scheme used to translate a string
21
22
// start urlEncode
22
23
String encodedField = java .net .URLEncoder .encode ("<fieldValue>" .toString (), "ISO-8859-1" );
23
24
// end urlEncode
24
25
}
25
26
private static void connectionString () {
27
+ // Creates a MongoClient and provides AWS credentials to enable the MONGODB-AWS authentication mechanism
26
28
// start connectionString
27
29
MongoClient mongoClient = MongoClients .create ("mongodb://<awsKeyId>:<awsSecretKey>@<atlasUri>?authMechanism=MONGODB-AWS" );
28
30
// end connectionString
29
31
}
30
32
31
33
private static void mechOnlyConnectionString () {
34
+ // Creates a MongoClient that MongoDB authenticates by using the MONGODB-AWS mechanism
32
35
// start mechOnlyConnectionString
33
36
MongoClient mongoClient = MongoClients .create ("mongodb://<atlasUri>?authMechanism=MONGODB-AWS" );
34
37
// end mechOnlyConnectionString
35
38
}
36
39
37
40
private static void connectionStringSessionToken () {
41
+ // Creates a MongoClient and provides AWS credentials and a session token to enable the MONGODB-AWS authentication mechanism
38
42
// start connectionStringSessionToken
39
43
MongoClient mongoClient = MongoClients .create ("mongodb://<awsKeyId>:<awsSecretKey>@<atlasUri>?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>" );
40
44
// end connectionStringSessionToken
41
45
}
42
46
43
47
private static void mongoCredential () {
48
+ // Creates a MongoCredential instance to specify the AWS credentials
44
49
// start mongoCredential
45
50
MongoCredential credential = MongoCredential .createAwsCredential ("<awsKeyId>" , "<awsSecretKey>" .toCharArray ());
46
51
52
+ // Creates a MongoClient that receives AWS credentials from the MongoCredential instance
47
53
MongoClient mongoClient = MongoClients .create (
48
54
MongoClientSettings .builder ()
49
55
.applyToClusterSettings (builder ->
@@ -54,9 +60,11 @@ private static void mongoCredential() {
54
60
}
55
61
56
62
private static void mechOnlyMongoCredential () {
63
+ // Creates a MongoCredential instance to specify the authentication mechanism
57
64
// start mechOnlyMongoCredential
58
65
MongoCredential credential = MongoCredential .createAwsCredential (null , null );
59
66
67
+ // Creates a MongoClient that receives configuration information from a MongoCredential and environment variables
60
68
MongoClient mongoClient = MongoClients .create (
61
69
MongoClientSettings .builder ()
62
70
.applyToClusterSettings (builder ->
@@ -67,10 +75,14 @@ private static void mechOnlyMongoCredential() {
67
75
}
68
76
69
77
private static void mongoCredentialSessionTokenConnString () {
78
+ // Creates a MongoCredential instance to specify the AWS credentials
70
79
// start mongoCredentialSessionTokenConnString
71
80
MongoCredential credential = MongoCredential .createAwsCredential ("<awsKeyId>" , "<awsSecretKey>" .toCharArray ());
81
+
82
+ // Specifies the authentication mechanism and session token in a connection string
72
83
ConnectionString connectionString = new ConnectionString ("mongodb://<atlasUri>/?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>" );
73
84
85
+ // Creates a MongoClient that receives configuration information from a MongoCrediential and connection string
74
86
MongoClient mongoClient = MongoClients .create (
75
87
MongoClientSettings .builder ()
76
88
.applyConnectionString (connectionString )
@@ -80,9 +92,11 @@ private static void mongoCredentialSessionTokenConnString() {
80
92
}
81
93
82
94
private static void mongoCredentialSessionTokenCredential () {
95
+ // Creates a MongoCredential instance to specify the AWS credentials and a session token
83
96
// start mongoCredentialSessionTokenCredential
84
97
MongoCredential credential = MongoCredential .createAwsCredential ("<awsKeyId>" , "<awsSecretKey>" .toCharArray ()).withMechanismProperty ("AWS_SESSION_TOKEN" , "<awsSessionToken>" );
85
98
99
+ // Creates a MongoClient that receives configuration information from a MongoCredential instance
86
100
MongoClient mongoClient = MongoClients .create (
87
101
MongoClientSettings .builder ()
88
102
.applyToClusterSettings (builder ->
@@ -93,23 +107,26 @@ private static void mongoCredentialSessionTokenCredential() {
93
107
}
94
108
95
109
private static void mongoCredentialECSorEC2 () {
110
+ // Creates a MongoCredential instance to specify the authentication mechanism
96
111
// start mongoCredentialECSorEC2
97
112
MongoCredential credential = MongoCredential .createAwsCredential (null , null );
98
113
// end mongoCredentialECSorEC2
99
114
}
100
115
101
116
private static void refreshCredentials () {
102
-
117
+ // Creates a lambda expression that returns new AWS credentials
103
118
// start refreshCredentials
104
119
Supplier <AwsCredential > awsFreshCredentialSupplier = () -> {
105
- // Add your code here to fetch new credentials here
120
+ // Add your code to fetch new credentials
106
121
107
- // Return the new credentials
108
122
return new AwsCredential ("<awsKeyId>" , "<awsSecretKey>" , "<awsSessionToken>" );
109
123
};
110
124
125
+ // Creates a MongoCredential instance to specify the new AWS credentials
111
126
MongoCredential credential = MongoCredential .createAwsCredential (null , null )
112
127
.withMechanismProperty (MongoCredential .AWS_CREDENTIAL_PROVIDER_KEY , awsFreshCredentialSupplier );
128
+
129
+ // Creates a MongoClient that receives new configuration information from a MongoCredential instance
113
130
MongoClient mongoClient = MongoClients .create (
114
131
MongoClientSettings .builder ()
115
132
.applyToClusterSettings (builder ->
0 commit comments