@@ -18,32 +18,38 @@ public class MongoDbAwsAuth {
1818 // Placeholder functions
1919
2020 private static void encodeText () throws UnsupportedEncodingException {
21+ // Defines the encoding scheme used to translate a string
2122 // start urlEncode
2223 String encodedField = java .net .URLEncoder .encode ("<fieldValue>" .toString (), "ISO-8859-1" );
2324 // end urlEncode
2425 }
2526 private static void connectionString () {
27+ // Creates a MongoClient and provides AWS credentials to enable the MONGODB-AWS authentication mechanism
2628 // start connectionString
2729 MongoClient mongoClient = MongoClients .create ("mongodb://<awsKeyId>:<awsSecretKey>@<atlasUri>?authMechanism=MONGODB-AWS" );
2830 // end connectionString
2931 }
3032
3133 private static void mechOnlyConnectionString () {
34+ // Creates a MongoClient that MongoDB authenticates by using the MONGODB-AWS mechanism
3235 // start mechOnlyConnectionString
3336 MongoClient mongoClient = MongoClients .create ("mongodb://<atlasUri>?authMechanism=MONGODB-AWS" );
3437 // end mechOnlyConnectionString
3538 }
3639
3740 private static void connectionStringSessionToken () {
41+ // Creates a MongoClient and provides AWS credentials and a session token to enable the MONGODB-AWS authentication mechanism
3842 // start connectionStringSessionToken
3943 MongoClient mongoClient = MongoClients .create ("mongodb://<awsKeyId>:<awsSecretKey>@<atlasUri>?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>" );
4044 // end connectionStringSessionToken
4145 }
4246
4347 private static void mongoCredential () {
48+ // Creates a MongoCredential instance to specify the AWS credentials
4449 // start mongoCredential
4550 MongoCredential credential = MongoCredential .createAwsCredential ("<awsKeyId>" , "<awsSecretKey>" .toCharArray ());
4651
52+ // Creates a MongoClient that receives AWS credentials from the MongoCredential instance
4753 MongoClient mongoClient = MongoClients .create (
4854 MongoClientSettings .builder ()
4955 .applyToClusterSettings (builder ->
@@ -54,9 +60,11 @@ private static void mongoCredential() {
5460 }
5561
5662 private static void mechOnlyMongoCredential () {
63+ // Creates a MongoCredential instance to specify the authentication mechanism
5764 // start mechOnlyMongoCredential
5865 MongoCredential credential = MongoCredential .createAwsCredential (null , null );
5966
67+ // Creates a MongoClient that receives configuration information from a MongoCredential and environment variables
6068 MongoClient mongoClient = MongoClients .create (
6169 MongoClientSettings .builder ()
6270 .applyToClusterSettings (builder ->
@@ -67,10 +75,14 @@ private static void mechOnlyMongoCredential() {
6775 }
6876
6977 private static void mongoCredentialSessionTokenConnString () {
78+ // Creates a MongoCredential instance to specify the AWS credentials
7079 // start mongoCredentialSessionTokenConnString
7180 MongoCredential credential = MongoCredential .createAwsCredential ("<awsKeyId>" , "<awsSecretKey>" .toCharArray ());
81+
82+ // Specifies the authentication mechanism and session token in a connection string
7283 ConnectionString connectionString = new ConnectionString ("mongodb://<atlasUri>/?authMechanism=MONGODB-AWS&authMechanismProperties=AWS_SESSION_TOKEN:<awsSessionToken>" );
7384
85+ // Creates a MongoClient that receives configuration information from a MongoCrediential and connection string
7486 MongoClient mongoClient = MongoClients .create (
7587 MongoClientSettings .builder ()
7688 .applyConnectionString (connectionString )
@@ -80,9 +92,11 @@ private static void mongoCredentialSessionTokenConnString() {
8092 }
8193
8294 private static void mongoCredentialSessionTokenCredential () {
95+ // Creates a MongoCredential instance to specify the AWS credentials and a session token
8396 // start mongoCredentialSessionTokenCredential
8497 MongoCredential credential = MongoCredential .createAwsCredential ("<awsKeyId>" , "<awsSecretKey>" .toCharArray ()).withMechanismProperty ("AWS_SESSION_TOKEN" , "<awsSessionToken>" );
8598
99+ // Creates a MongoClient that receives configuration information from a MongoCredential instance
86100 MongoClient mongoClient = MongoClients .create (
87101 MongoClientSettings .builder ()
88102 .applyToClusterSettings (builder ->
@@ -93,23 +107,26 @@ private static void mongoCredentialSessionTokenCredential() {
93107 }
94108
95109 private static void mongoCredentialECSorEC2 () {
110+ // Creates a MongoCredential instance to specify the authentication mechanism
96111 // start mongoCredentialECSorEC2
97112 MongoCredential credential = MongoCredential .createAwsCredential (null , null );
98113 // end mongoCredentialECSorEC2
99114 }
100115
101116 private static void refreshCredentials () {
102-
117+ // Creates a lambda expression that returns new AWS credentials
103118 // start refreshCredentials
104119 Supplier <AwsCredential > awsFreshCredentialSupplier = () -> {
105- // Add your code here to fetch new credentials here
120+ // Add your code to fetch new credentials
106121
107- // Return the new credentials
108122 return new AwsCredential ("<awsKeyId>" , "<awsSecretKey>" , "<awsSessionToken>" );
109123 };
110124
125+ // Creates a MongoCredential instance to specify the new AWS credentials
111126 MongoCredential credential = MongoCredential .createAwsCredential (null , null )
112127 .withMechanismProperty (MongoCredential .AWS_CREDENTIAL_PROVIDER_KEY , awsFreshCredentialSupplier );
128+
129+ // Creates a MongoClient that receives new configuration information from a MongoCredential instance
113130 MongoClient mongoClient = MongoClients .create (
114131 MongoClientSettings .builder ()
115132 .applyToClusterSettings (builder ->
0 commit comments