Skip to content

Commit 0802624

Browse files
authored
DOCSP-38523 - OIDC (mongodb#186)
1 parent 5c4516c commit 0802624

File tree

3 files changed

+130
-9
lines changed

3 files changed

+130
-9
lines changed

source/fundamentals/authentication.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,4 @@ guide, see the following API Documentation:
398398
- `MongoClient() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClient.html>`__
399399
- `MongoClientSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClientSettings.html>`__
400400
- `CreateCredential() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoCredential.CreateCredential.html>`__
401-
- `CreateMongoX509Credential() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoCredential.CreateMongoX509Credential.html>`__
401+
- `CreateMongoX509Credential() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoCredential.CreateMongoX509Credential.html>`__

source/fundamentals/enterprise-authentication.txt

+123
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ You can use the following authentication mechanisms with the latest version of
2323

2424
- :ref:`GSSAPI/Kerberos <csharp-kerberos>`
2525
- :ref:`LDAP (Plain) <csharp-LDAP>`
26+
- :ref:`MONGODB-OIDC <csharp-mongodb-oidc>`
2627

2728
To authenticate using another mechanism, see the
2829
:ref:`<csharp-authentication-mechanisms>` fundamentals page. For
@@ -255,6 +256,127 @@ mechanism:
255256
authenticates using the PLAIN Simple Authentication and Security Layer
256257
(SASL) defined in `RFC-4616 <https://tools.ietf.org/html/rfc4616>`__.
257258

259+
.. _csharp-mongodb-oidc:
260+
261+
MONGODB-OIDC
262+
------------
263+
264+
.. important::
265+
266+
The MONGODB-OIDC authentication mechanism requires MongoDB v7.0 or later running
267+
on a Linux platform.
268+
269+
The following sections describe how to use the MONGODB-OIDC authentication mechanism to
270+
authenticate from various platforms.
271+
272+
For more information about the MONGODB-OIDC authentication mechanism, see
273+
:manual:`OpenID Connect Authentication </core/security-oidc/>` and
274+
:manual:`MongoDB Server Parameters </reference/parameters/#mongodb-parameter-param.oidcIdentityProviders>`
275+
in the MongoDB Server manual.
276+
277+
.. _csharp-mongodb-oidc-azure-imds:
278+
279+
Azure IMDS
280+
~~~~~~~~~~
281+
282+
If your application runs on an Azure VM, or otherwise uses the
283+
`Azure Instance Metadata Service <https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service>`__
284+
(IMDS), you can authenticate to MongoDB by using the {+driver-short+}'s built-in Azure
285+
support.
286+
287+
You can specify Azure IMDS OIDC authentication on a ``MongoClientSettings`` object either by
288+
using a ``MongoCredential`` object or as part of the connection string. Select the
289+
:guilabel:`Connection String` or :guilabel:`MongoCredential` tab to
290+
see the corresponding syntax.
291+
292+
.. tabs::
293+
294+
.. tab:: Connection String
295+
:tabid: mongodb-azure-imds-connection-string
296+
297+
The following code example shows how to specify Azure IMDS OIDC authentication.
298+
Replace the ``<percent-encoded audience>`` placeholder with the percent-encoded
299+
value of the ``audience`` parameter configured on your MongoDB deployment.
300+
301+
.. code-block:: csharp
302+
303+
var connectionString = "mongodb://<username>@<hostname>[:<port>]/?" +
304+
"authMechanism=MONGODB-OIDC" +
305+
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>");
306+
var mongoClientSettings = MongoClientSettings.FromConnectionString(connectionString);
307+
var client = new MongoClient(mongoClientSettings);
308+
309+
.. tab:: MongoCredential
310+
:tabid: mongodb-azure-mongo-credential
311+
312+
The following code example shows how to specify Azure IMDS OIDC authentication.
313+
Replace the ``<username>`` placeholder with the client ID or application ID of the
314+
Azure managed identity or enterprise application. Replace the ``<audience>``
315+
placeholder with the value of the ``audience`` parameter configured on your MongoDB
316+
deployment.
317+
318+
.. code-block:: csharp
319+
320+
var mongoClientSettings = MongoClientSettings.FromConnectionString(
321+
"mongodb+srv://<hostname>[:<port>]");
322+
mongoClientSettings.Credential = MongoCredential.CreateOidcCredential("azure", "<username>")
323+
.WithMechanismProperty("TOKEN_RESOURCE", "<audience>");
324+
var client = new MongoClient(mongoClientSettings);
325+
326+
Custom Callback
327+
~~~~~~~~~~~~~~~
328+
329+
The {+driver-short+} doesn't offer built-in support for all platforms, including
330+
Azure Functions and Azure Kubernetes Service (AKS). Instead, you
331+
must define a custom callback to use OIDC to authenticate from these platforms.
332+
333+
First, define a class that implements the ``IOidcCallback`` interface. This interface
334+
contains two methods:
335+
336+
- ``GetOidcAccessToken()``: This method accepts the parameters to the callback method
337+
and returns the callback response.
338+
- ``GetOidcAccessTokenAsync()``: This method is an asynchronous version of the previous
339+
method.
340+
341+
The following code is an example implementation of the ``IOidcCallback`` interface.
342+
In this example, the methods retrieve an OIDC token from a file named ``"access-token.dat"``
343+
in the local file system.
344+
345+
.. code-block:: csharp
346+
347+
public class MyCallback : IOidcCallback
348+
{
349+
public OidcAccessToken GetOidcAccessToken(
350+
OidcCallbackParameters parameters,
351+
CancellationToken cancellationToken)
352+
{
353+
var accessToken = File.ReadAllText("access-token.dat");
354+
return new(accessToken, expiresIn: null);
355+
}
356+
357+
public async Task<OidcAccessToken> GetOidcAccessTokenAsync(
358+
OidcCallbackParameters parameters,
359+
CancellationToken cancellationToken)
360+
{
361+
var accessToken = await File.ReadAllTextAsync(
362+
"access-token.dat",
363+
cancellationToken)
364+
.ConfigureAwait(false);
365+
return new(accessToken, expiresIn: null);
366+
}
367+
}
368+
369+
After you define a class that contains your custom callback methods, call the
370+
``MongoCredential.CreateOidcCredential()`` method and pass in a new instance of your
371+
class. Store the result of this method call in the ``Credential`` property of your
372+
``MongoClientSettings`` object, as shown in the following code example:
373+
374+
.. code-block:: csharp
375+
376+
var mongoClientSettings = MongoClientSettings.FromConnectionString("mongodb://<hostname>[:port]");
377+
mongoClientSettings.Credential = MongoCredential.CreateOidcCredential(new MyCallback());
378+
var client = new MongoClient(mongoClientSettings);
379+
258380
API Documentation
259381
-----------------
260382

@@ -266,3 +388,4 @@ guide, see the following API Documentation:
266388
- `MongoClientSettings <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoClientSettings.html>`__
267389
- `CreateGssapiCredential() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoCredential.CreateGssapiCredential.html>`__
268390
- `CreatePlainCredential() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.MongoCredential.CreatePlainCredential.html>`__
391+
- `IOidcCallback <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.Core.Authentication.Oidc.IOidcCallback.html>`__

source/upgrade.txt

+6-8
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,9 @@ and MongoDB Server versions, visit the
130130
Version 2.14 Release Support Changes
131131
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132132

133-
- The v2.14 driver drops support for MongoDB Server v3.4 and earlier. To
134-
use any driver from v2.14 and later, your MongoDB Server must be v3.6 or
135-
later. To learn how to upgrade your MongoDB Server to v3.6, follow the
136-
link that corresponds to your MongoDB deployment configuration:
137-
138-
- :ref:`<3.6-upgrade-replica-set>`
139-
- :ref:`<3.6-upgrade-standalone>`
140-
- :ref:`<3.6-upgrade-sharded-cluster>`
133+
The v2.14 driver drops support for MongoDB Server v3.4 and earlier. To
134+
use any driver from v2.14 and later, your MongoDB Server must be v3.6 or
135+
later.
136+
137+
To learn how to upgrade your MongoDB Server deployment, see
138+
:manual:`Release Notes </release-notes/>` in the MongoDB Server manual.

0 commit comments

Comments
 (0)