π§© Introduction
Setting up MongoDB with OIDC authentication in a production-grade Spring Boot app sounds simple — until reality strikes. π
This post covers two specific issues I faced when integrating MongoDB v8 with Spring Boot 3.3.6, and deploying the app via Terraform to Cloud Run. If you’re planning the same, ame, save yourself some debugging time and read on. π
π Issue #1: MongoDB Driver Compatibility – “Unsupported authMechanism: MONGODB-OIDC”
π₯ The Problem:
After upgrading to MongoDB v8, my app started throwing this error:
Turns out, the default MongoDB driver version (5.0.1) pulled in by Spring Boot 3.3.6 isn’t compatible with MongoDB v8, especially when using OIDC authentication.
Turns out, the default MongoDB driver version (5.0.1) pulled in by Spring Boot 3.3.6 isn’t compatible with MongoDB v8, especially when using OIDC authentication.
✅ The Fix:
Manually specify compatible driver versions (5.2.1
or above). Here's the working setup in build.gradle
:
// For MongoDB v8 + OIDC support
implementation 'org.mongodb:mongodb-driver-sync:5.2.1'
implementation 'org.mongodb:bson:5.2.1'
implementation 'org.mongodb:mongodb-driver-core:5.2.1'
implementation 'org.mongodb:bson-record-codec:5.2.1'
π Tip: Always check the official MongoDB compatibility matrix (see screenshot below) before upgrading your database or Spring Boot version.
πΈ Reference image:
πΌ️ (Attach the screenshot you provided for visual clarity.)
π ️ Issue #2: Terraform + Cloud Run – Service Account Confusion
π₯ The Problem:
While deploying via Terraform, the app in Cloud Run couldn’t connect to MongoDB, even after the driver was fixed.
The culprit? I had mistakenly used the service account ID of the deploying identity instead of the Cloud Run-bound service account in my Terraform resource configuration.
✅ The Fix:
Update the Terraform config to use the correct SA — the one mapped to Cloud Run, not your CI/CD user.
π‘ Lessons Learned
-
MongoDB driver versions matter — especially with advanced auth like OIDC
-
Spring Boot’s dependency management may not always pull the latest compatible drivers
-
Cloud IAM misconfigurations are sneaky, but consistent π
-
Compatibility matrices are your best friend (even though they’re hidden in docs)
π Wrapping Up
This post hopefully saves you from chasing vague errors across layers. Feel free to reuse the driver config above, and double-check your Terraform SAs before deploying!
Have you dealt with MongoDB OIDC or Cloud Run surprises lately? Drop your stories in the comments or reach out!
Until next time,
– Anand ☕ @ Java Bean Bag
Comments
Post a Comment