Skip to content

Commit

Permalink
DOCSP-42514: kotlin user/pass placeholders (mongodb#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustagir authored and stephmarie17 committed Aug 20, 2024
1 parent 4324743 commit 31cb0e9
Show file tree
Hide file tree
Showing 27 changed files with 51 additions and 50 deletions.
12 changes: 6 additions & 6 deletions examples/src/test/kotlin/AuthTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class AuthTest {
fun defaultConnectionStringTest() = runBlocking {
// :replace-start: {
// "terms": {
// "CONNECTION_URI_PLACEHOLDER": "\"mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>\""
// "CONNECTION_URI_PLACEHOLDER": "\"mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>\""
// }
// }
// :snippet-start: default-cred-string
Expand All @@ -70,7 +70,7 @@ internal class AuthTest {
fun scramSha256ConnectionStringTest() = runBlocking {
// :replace-start: {
// "terms": {
// "CONNECTION_URI_PLACEHOLDER": "\"mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-256\""
// "CONNECTION_URI_PLACEHOLDER": "\"mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-256\""
// }
// }
// :snippet-start: scram-sha-256-string
Expand All @@ -93,7 +93,7 @@ internal class AuthTest {
fun scramSha1ConnectionStringTest() = runBlocking {
// :replace-start: {
// "terms": {
// "CONNECTION_URI_PLACEHOLDER": "\"mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-1\""
// "CONNECTION_URI_PLACEHOLDER": "\"mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-1\""
// }
// }
// :snippet-start: scram-sha-1-string
Expand All @@ -116,7 +116,7 @@ internal class AuthTest {
fun x509ConnectionStringTest() = runBlocking {
// :replace-start: {
// "terms": {
// "CONNECTION_URI_PLACEHOLDER": "\"mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=MONGODB-X509&tls=true\""
// "CONNECTION_URI_PLACEHOLDER": "\"mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=MONGODB-X509&tls=true\""
// }
// }
// :snippet-start: x-509-string
Expand All @@ -137,8 +137,8 @@ internal class AuthTest {

// :replace-start: {
// "terms": {
// "USERNAME": "\"<username>\"",
// "PASSWORD": "\"<password>\"",
// "USERNAME": "\"<db_username>\"",
// "PASSWORD": "\"<db_password>\"",
// "AUTH_DB": "\"<authenticationDb>\"",
// "HOSTNAME": "\"<hostname>\"",
// "PORT": "\"<port>\""
Expand Down
25 changes: 13 additions & 12 deletions examples/src/test/kotlin/EnterpriseAuthTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import javax.naming.Context
import javax.security.auth.Subject
import javax.security.auth.login.LoginContext
import kotlin.test.Ignore

// :replace-start: {
// "terms": {
// "PORT": "<port>",
Expand All @@ -36,7 +37,7 @@ internal class EnterpriseAuthTest {

fun createGSSAPICred() = runBlocking {
// :snippet-start: auth-creds-gssapi
val credential = MongoCredential.createGSSAPICredential("<username>")
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")

val settings = MongoClientSettings.builder()
.applyToClusterSettings { builder ->
Expand All @@ -51,7 +52,7 @@ internal class EnterpriseAuthTest {

fun serviceNameKey() = runBlocking {
// :snippet-start: service-name-key
val credential = MongoCredential.createGSSAPICredential("<username>")
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
.withMechanismProperty(MongoCredential.SERVICE_NAME_KEY, "myService")
// :snippet-end:
}
Expand All @@ -62,7 +63,7 @@ internal class EnterpriseAuthTest {
loginContext.login()
val subject: Subject = loginContext.subject

val credential = MongoCredential.createGSSAPICredential("<username>")
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
.withMechanismProperty(MongoCredential.JAVA_SUBJECT_KEY, subject)
// :snippet-end:
}
Expand All @@ -74,7 +75,7 @@ internal class EnterpriseAuthTest {
val myLoginContext = "myContext"
/* Login context defaults to "com.sun.security.jgss.krb5.initiate"
if unspecified in KerberosSubjectProvider */
val credential = MongoCredential.createGSSAPICredential("<username>")
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
.withMechanismProperty(
MongoCredential.JAVA_SUBJECT_PROVIDER_KEY,
KerberosSubjectProvider(myLoginContext)
Expand All @@ -84,7 +85,7 @@ internal class EnterpriseAuthTest {

fun ldapCredential() = runBlocking {
// :snippet-start: ldap-mongo-credential
val credential = MongoCredential.createPlainCredential("<username>", "$external", "<password>".toCharArray())
val credential = MongoCredential.createPlainCredential("<LDAP username>", "$external", "<password>".toCharArray())

val settings = MongoClientSettings.builder()
.applyToClusterSettings { builder ->
Expand All @@ -99,29 +100,29 @@ internal class EnterpriseAuthTest {

fun gssapiConnectionString() = runBlocking {
// :snippet-start: gssapi-connection-string
val connectionString = ConnectionString("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI")
val connectionString = ConnectionString("<Kerberos principal>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI")
val mongoClient = MongoClient.create(connectionString)
// :snippet-end:
}

fun gssapiPropertiesConnectionString() = runBlocking {
// :snippet-start: gssapi-properties-connection-string
val connectionString = ConnectionString("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService")
val connectionString = ConnectionString("<Kerberos principal>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService")
val mongoClient = MongoClient.create(connectionString)
// :snippet-end:
}

fun ldapConnectionString() = runBlocking {
// :snippet-start: ldap-connection-string
val connectionString = ConnectionString("<username>:<password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN")
val connectionString = ConnectionString("<LDAP username>:<password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN")
val mongoClient = MongoClient.create(connectionString)
// :snippet-end:
}

fun oidcAzureConnectionString() = runBlocking {
// :snippet-start: oidc-azure-connection-string
val connectionString = ConnectionString(
"mongodb://<username>@<hostname>:<port>/?" +
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
"?authMechanism=MONGODB-OIDC" +
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>")
val mongoClient = MongoClient.create(connectionString)
Expand All @@ -130,7 +131,7 @@ internal class EnterpriseAuthTest {

fun oidcAzureCredential() = runBlocking {
// :snippet-start: oidc-azure-credential
val credential = MongoCredential.createOidcCredential("<username>")
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
.withMechanismProperty("ENVIRONMENT", "azure")
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")

Expand All @@ -147,7 +148,7 @@ internal class EnterpriseAuthTest {
fun oidcGCPConnectionString() = runBlocking {
// :snippet-start: oidc-gcp-connection-string
val connectionString = ConnectionString(
"mongodb://<hostname>:<port>/?" +
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
"authMechanism=MONGODB-OIDC" +
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>")
val mongoClient = MongoClient.create(connectionString)
Expand All @@ -156,7 +157,7 @@ internal class EnterpriseAuthTest {

fun oidcGCPCredential() = runBlocking {
// :snippet-start: oidc-gcp-credential
val credential = MongoCredential.createOidcCredential("<username>")
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
.withMechanismProperty("ENVIRONMENT", "gcp")
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")

Expand Down
2 changes: 1 addition & 1 deletion examples/src/test/kotlin/MongoClientSettingsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import kotlin.test.Ignore
// :replace-start: {
// "terms": {
// "uri": "\"<your connection string>\"",
// "uriString": "\"mongodb+srv:/<username>:<password>@<hostname>:<port>?connectTimeoutMS(2000)\"",
// "uriString": "\"mongodb+srv:/<db_username>:<db_password>@<hostname>:<port>?connectTimeoutMS(2000)\"",
// "uriAcmeString": "\"mongodb+srv://host1.acme.com\""
// }
// }
Expand Down
2 changes: 1 addition & 1 deletion source/connection-troubleshooting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ database:
:copyable: false

val mongoClient =
MongoClient.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=users")
MongoClient.create("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=users")

.. _kotlin-error-sending-message:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val mongoClient =
MongoClient.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>")
MongoClient.create("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val credential = MongoCredential.createCredential(
"<username>", "<authenticationDb>", "<password>".toCharArray()
"<db_username>", "<authenticationDb>", "<db_password>".toCharArray()
)
val settings = MongoClientSettings.builder()
.applyToClusterSettings { builder: ClusterSettings.Builder ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val credential = MongoCredential.createScramSha1Credential(
"<username>", "<authenticationDb>", "<password>".toCharArray()
"<db_username>", "<authenticationDb>", "<db_password>".toCharArray()
)
val settings = MongoClientSettings.builder()
.applyToClusterSettings { builder: ClusterSettings.Builder ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val mongoClient =
MongoClient.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-1")
MongoClient.create("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-1")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val credential = MongoCredential.createScramSha256Credential(
"<username>", "<authenticationDb>", "<password>".toCharArray()
"<db_username>", "<authenticationDb>", "<db_password>".toCharArray()
)
val settings = MongoClientSettings.builder()
.applyToClusterSettings { builder: ClusterSettings.Builder ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val mongoClient =
MongoClient.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-256")
MongoClient.create("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=SCRAM-SHA-256")
2 changes: 1 addition & 1 deletion source/examples/generated/AuthTest.snippet.x-509-string.kt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val mongoClient =
MongoClient.create("mongodb://<username>:<password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=MONGODB-X509&tls=true")
MongoClient.create("mongodb://<db_username>:<db_password>@<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=MONGODB-X509&tls=true")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val credential = MongoCredential.createGSSAPICredential("<username>")
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")

val settings = MongoClientSettings.builder()
.applyToClusterSettings { builder ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val connectionString = ConnectionString("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI")
val connectionString = ConnectionString("<Kerberos principal>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI")
val mongoClient = MongoClient.create(connectionString)
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val connectionString = ConnectionString("<username>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService")
val connectionString = ConnectionString("<Kerberos principal>@<hostname>:<port>/?authSource=$external&authMechanism=GSSAPI&authMechanismProperties=SERVICE_NAME:myService")
val mongoClient = MongoClient.create(connectionString)
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ val loginContext = LoginContext("<LoginModule implementation from JAAS config>")
loginContext.login()
val subject: Subject = loginContext.subject

val credential = MongoCredential.createGSSAPICredential("<username>")
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
.withMechanismProperty(MongoCredential.JAVA_SUBJECT_KEY, subject)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ will share a Kerberos ticket cache */
val myLoginContext = "myContext"
/* Login context defaults to "com.sun.security.jgss.krb5.initiate"
if unspecified in KerberosSubjectProvider */
val credential = MongoCredential.createGSSAPICredential("<username>")
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
.withMechanismProperty(
MongoCredential.JAVA_SUBJECT_PROVIDER_KEY,
KerberosSubjectProvider(myLoginContext)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val connectionString = ConnectionString("<username>:<password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN")
val connectionString = ConnectionString("<LDAP username>:<password>@<hostname>:<port>/?authSource=$external&authMechanism=PLAIN")
val mongoClient = MongoClient.create(connectionString)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val credential = MongoCredential.createPlainCredential("<username>", "$external", "<password>".toCharArray())
val credential = MongoCredential.createPlainCredential("<LDAP username>", "$external", "<password>".toCharArray())

val settings = MongoClientSettings.builder()
.applyToClusterSettings { builder ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val connectionString = ConnectionString(
"mongodb://<username>@<hostname>:<port>/?" +
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
"?authMechanism=MONGODB-OIDC" +
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>")
val mongoClient = MongoClient.create(connectionString)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val credential = MongoCredential.createOidcCredential("<username>")
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
.withMechanismProperty("ENVIRONMENT", "azure")
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val connectionString = ConnectionString(
"mongodb://<hostname>:<port>/?" +
"mongodb://<OIDC principal>@<hostname>:<port>/?" +
"authMechanism=MONGODB-OIDC" +
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>")
val mongoClient = MongoClient.create(connectionString)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val credential = MongoCredential.createOidcCredential("<username>")
val credential = MongoCredential.createOidcCredential("<OIDC principal>")
.withMechanismProperty("ENVIRONMENT", "gcp")
.withMechanismProperty("TOKEN_RESOURCE", "<audience>")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
val credential = MongoCredential.createGSSAPICredential("<username>")
val credential = MongoCredential.createGSSAPICredential("<Kerberos principal>")
.withMechanismProperty(MongoCredential.SERVICE_NAME_KEY, "myService")
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
val mongoClient = MongoClient.create(
MongoClientSettings.builder()
.applyConnectionString(ConnectionString("mongodb+srv:/<username>:<password>@<hostname>:<port>?connectTimeoutMS(2000)"))
.applyConnectionString(ConnectionString("mongodb+srv:/<db_username>:<db_password>@<hostname>:<port>?connectTimeoutMS(2000)"))
.applyToSocketSettings{ builder ->
builder.connectTimeout(5, TimeUnit.SECONDS)
}
Expand Down
12 changes: 6 additions & 6 deletions source/fundamentals/auth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ which they advertise support.
The following code snippets show how to specify the authentication mechanism,
using the following placeholders:

* ``username`` - your MongoDB username
* ``password`` - your MongoDB user's password
* ``db_username`` - your MongoDB database username
* ``db_password`` - your MongoDB database user's password
* ``hostname`` - network address of your MongoDB server, accessible by your client
* ``port`` - port number of your MongoDB server
* ``authenticationDb`` - MongoDB database that contains your user's
Expand Down Expand Up @@ -120,8 +120,8 @@ algorithm, to authenticate your user.
The following code snippets show how to specify the authentication mechanism,
using the following placeholders:

* ``username`` - your MongoDB username.
* ``password`` - your MongoDB user's password.
* ``db_username`` - your MongoDB database username.
* ``db_password`` - your MongoDB database user's password.
* ``hostname`` - network address of your MongoDB server, accessible by your client.
* ``port`` - port number of your MongoDB server.
* ``authenticationDb`` - MongoDB database that contains your user's
Expand Down Expand Up @@ -172,8 +172,8 @@ your user.
The following code snippets show how to specify the authentication mechanism,
using the following placeholders:

* ``username`` - your MongoDB username.
* ``password`` - your MongoDB user's password.
* ``db_username`` - your MongoDB database username.
* ``db_password`` - your MongoDB database user's password.
* ``hostname`` - network address of your MongoDB server, accessible by your client.
* ``port`` - port number of your MongoDB server.
* ``authenticationDb`` - MongoDB database that contains your user's
Expand Down
2 changes: 1 addition & 1 deletion source/fundamentals/connection/connect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ to change the servers in rotation without reconfiguring clients.

The next part of the connection URI contains your credentials if you are
using a password-based authentication mechanism. Replace the value of ``user``
with your username and ``pass`` with your password. If your
with your database username and ``pass`` with your database user's password. If your
authentication mechanism does not require credentials, omit this part of
the connection URI.

Expand Down
6 changes: 3 additions & 3 deletions source/fundamentals/enterprise-auth.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ principal name.
The following code snippets show how to specify the authentication mechanism,
using the following placeholders:

* ``username`` - your URL-encoded principal name, e.g. "username%40REALM.ME"
* ``Kerberos principal`` - your URL-encoded principal name, e.g. "username%40REALM.ME"
* ``hostname`` - network address of your MongoDB server, accessible by your client
* ``port`` - port number of your MongoDB server

Expand Down Expand Up @@ -248,7 +248,7 @@ parameter to ``PLAIN`` and including your LDAP username and password in the
The following code snippets show how to specify the authentication mechanism,
using the following placeholders:

* ``username`` - your LDAP username
* ``LDAP username`` - your LDAP username
* ``password`` - your LDAP user's password
* ``hostname`` - network address of your MongoDB server, accessible by your client
* ``port`` - port number of your MongoDB server
Expand Down Expand Up @@ -344,7 +344,7 @@ see the corresponding syntax.
.. tab:: MongoCredential
:tabid: mongodb-azure-mongo-credential

Replace the ``<username>`` placeholder with the client ID or application ID of the
Replace the ``<OIDC principal>`` placeholder with the client ID or application ID of the
Azure managed identity or enterprise application. Replace the ``<audience>``
placeholder with the value of the
``audience`` server parameter configured on your MongoDB deployment.
Expand Down

0 comments on commit 31cb0e9

Please sign in to comment.