Skip to content

Commit 611b12c

Browse files
authored
DRIVERS-2601 OIDC: Automatic token acquisition for GCP Identity Provider (#1561)
* DRIVERS-2601 OIDC: Automatic token acquisition for GCP Identity Provider * Add changelog
1 parent cd08b72 commit 611b12c

File tree

3 files changed

+188
-5
lines changed

3 files changed

+188
-5
lines changed

source/auth/auth.md

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,14 +1214,15 @@ in the MONGODB-OIDC specification, including sections or blocks that specificall
12141214

12151215
- ENVIRONMENT\
12161216
Drivers MUST allow the user to specify the name of a built-in OIDC application environment integration
1217-
to use to obtain credentials. If provided, the value MUST be one of `["test", "azure"]`. If both `ENVIRONMENT` and
1218-
an [OIDC Callback](#oidc-callback) or [OIDC Human Callback](#oidc-human-callback) are provided for the same
1219-
`MongoClient`, the driver MUST raise an error.
1217+
to use to obtain credentials. If provided, the value MUST be one of `["test", "azure", "gcp"]`. If both
1218+
`ENVIRONMENT` and an [OIDC Callback](#oidc-callback) or [OIDC Human Callback](#oidc-human-callback) are provided for
1219+
the same `MongoClient`, the driver MUST raise an error.
12201220

12211221
- TOKEN_RESOURCE\
12221222
The URI of the target resource. This property is currently only used and required by the Azure
1223-
built-in OIDC provider integration. If `TOKEN_RESOURCE` is provided and `ENVIRONMENT` is not `azure` or
1224-
`TOKEN_RESOURCE` is not provided and `ENVIRONMENT` is `azure`, the driver MUST raise an error.
1223+
built-in OIDC provider integration. If `TOKEN_RESOURCE` is provided and `ENVIRONMENT` is not one of
1224+
`["azure", "gcp"]` or `TOKEN_RESOURCE` is not provided and `ENVIRONMENT` is one of `["azure", "gcp"]`, the driver
1225+
MUST raise an error.
12251226

12261227
- OIDC_CALLBACK\
12271228
An [OIDC Callback](#oidc-callback) that returns OIDC credentials. Drivers MAY allow the user to
@@ -1326,6 +1327,67 @@ For more details, see
13261327
The callback itself MUST not perform any caching, and the driver MUST cache its tokens in the same way as if a custom
13271328
callback had been provided by the user.
13281329

1330+
For details on test environment setup, see the README in
1331+
[Drivers-Evergreen-Tools](https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/auth_oidc/azure/README.md).
1332+
1333+
**GCP**
1334+
1335+
The GCP provider integration is enabled by setting auth mechanism property `ENVIRONMENT:gcp`.
1336+
1337+
If enabled, drivers MUST use an internal machine callback that calls the
1338+
[Google Cloud VM metadata](https://cloud.google.com/compute/docs/metadata/overview) endpoint and parse the JSON response
1339+
body, as follows:
1340+
1341+
Make an HTTP GET request to
1342+
1343+
```
1344+
http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=<resource>
1345+
```
1346+
1347+
with headers
1348+
1349+
```
1350+
Accept: application/json
1351+
Metadata-Flavor: Google
1352+
```
1353+
1354+
where `<resource>` is the value of the `TOKEN_RESOURCE` mechanism property. The timeout should equal the
1355+
`callbackTimeoutMS` parameter given to the callback.
1356+
1357+
Example code for the above using curl, where `$TOKEN_RESOURCE` is the value of the `TOKEN_RESOURCE` mechanism property.
1358+
1359+
```bash
1360+
curl -X GET \
1361+
-H "Accept: application/json" \
1362+
-H "Metadata-Flavor: Google" \
1363+
--max-time $CALLBACK_TIMEOUT_MS \
1364+
"http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=$TOKEN_RESOURCE"
1365+
```
1366+
1367+
The JSON response will be in this format:
1368+
1369+
```json
1370+
{
1371+
"aud": "https://example.com",
1372+
"azp": "118153013249117554930",
1373+
"exp": 1707488566,
1374+
"iat": 1707484966,
1375+
"iss": "https://accounts.google.com",
1376+
"sub": "118153013249117554930"
1377+
}
1378+
```
1379+
1380+
The driver MUST use the returned `"access_token"` value as the access token in a `JwtStepRequest`. If the response does
1381+
not return a status code of 200, the driver MUST raise an error including the HTTP response body.
1382+
1383+
For more details, see [View and query VM metadata](https://cloud.google.com/compute/docs/metadata/querying-metadata).
1384+
1385+
The callback itself MUST not perform any caching, and the driver MUST cache its tokens in the same way as if a custom
1386+
callback had been provided by the user.
1387+
1388+
For details on test environment setup, see the README in
1389+
[Drivers-Evergreen-Tools](https://github.com/mongodb-labs/drivers-evergreen-tools/blob/master/.evergreen/auth_oidc/gcp/README.md).
1390+
13291391
#### OIDC Callback
13301392

13311393
Drivers MUST allow users to provide a callback that returns an OIDC access token. The purpose of the callback is to
@@ -1988,6 +2050,8 @@ to EC2 instance metadata in ECS, for security reasons, Amazon states it's best p
19882050

19892051
## Changelog
19902052

2053+
- 2024-04-03: Added GCP built-in OIDC provider integration.
2054+
19912055
- 2024-03-29: Updated OIDC test setup and descriptions.
19922056

19932057
- 2024-03-21: Added Azure built-in OIDC provider integration.

source/auth/tests/legacy/connection-string.json

Lines changed: 69 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/auth/tests/legacy/connection-string.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,53 @@ tests:
390390
uri: mongodb://localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=UnsupportedProperty:unexisted
391391
valid: false
392392
credential:
393+
- description: should recognise the mechanism with azure provider (MONGODB-OIDC)
394+
uri: mongodb://localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:foo
395+
valid: true
396+
credential:
397+
username: null
398+
password: null
399+
source: $external
400+
mechanism: MONGODB-OIDC
401+
mechanism_properties:
402+
ENVIRONMENT: azure
403+
TOKEN_RESOURCE: foo
404+
- description: should accept a username with azure provider (MONGODB-OIDC)
405+
uri: mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:foo
406+
valid: true
407+
credential:
408+
username: user
409+
password: null
410+
source: $external
411+
mechanism: MONGODB-OIDC
412+
mechanism_properties:
413+
ENVIRONMENT: azure
414+
TOKEN_RESOURCE: foo
415+
- description: should accept a username and throw an error for a password with azure provider (MONGODB-OIDC)
416+
uri: mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:foo
417+
valid: false
418+
credential: null
419+
- description: should throw an exception if no token audience is given for azure provider (MONGODB-OIDC)
420+
uri: mongodb://username@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure
421+
valid: false
422+
credential: null
423+
- description: should recognise the mechanism with gcp provider (MONGODB-OIDC)
424+
uri: mongodb://localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:foo
425+
valid: true
426+
credential:
427+
username: null
428+
password: null
429+
source: $external
430+
mechanism: MONGODB-OIDC
431+
mechanism_properties:
432+
ENVIRONMENT: gcp
433+
TOKEN_RESOURCE: foo
434+
- description: should throw an error for a username and password with gcp provider
435+
(MONGODB-OIDC)
436+
uri: mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:foo
437+
valid: false
438+
credential: null
439+
- description: should throw an error if not TOKEN_RESOURCE with gcp provider (MONGODB-OIDC)
440+
uri: mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:gcp
441+
valid: false
442+
credential: null

0 commit comments

Comments
 (0)