Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/test-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- 'docs/operator-public-documentation/**'
- '.github/scripts/check_doc_links*.py'
- '.github/workflows/*docs.yml'
- 'documentdb-playground/contrib/telemetry-and-cost-optimized-eks/scripts/**'
push:
branches: [main]
paths:
Expand All @@ -16,6 +17,7 @@ on:
- 'docs/operator-public-documentation/**'
- '.github/scripts/check_doc_links*.py'
- '.github/workflows/*docs.yml'
- 'documentdb-playground/contrib/telemetry-and-cost-optimized-eks/scripts/**'
workflow_dispatch:

permissions:
Expand All @@ -32,10 +34,15 @@ jobs:
- uses: actions/setup-python@v5
with:
python-version: 3.x
- uses: actions/setup-node@v4
with:
node-version: '22'
- name: Install documentation dependencies
run: pip install "mkdocs-material==9.5.19" "mike==2.1.3" "mkdocs-redirects==1.2.2"
- name: Test link checker
run: python -m unittest discover -s .github/scripts -p '*_test.py'
- name: Test deployment diagnostic credentials
run: python -m unittest discover -s documentdb-playground/contrib/telemetry-and-cost-optimized-eks/scripts -p 'test_*.py'
- name: Build and check candidate entrypoints
run: |
mkdocs build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,17 @@ documentdb-service-my-documentdb.documentdb-ns.svc.cluster.local:10260
## Connect with mongosh

```bash
mongosh "mongodb://dev_user:DevPassword123@127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0"
# Enter the password from your credentials Secret when prompted.
mongosh "mongodb://127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0" \
--username dev_user --password
```

Or use individual flags:

```bash
mongosh 127.0.0.1:10260 \
-u dev_user \
-p DevPassword123 \
--password \
--authenticationMechanism SCRAM-SHA-256 \
--tls --tlsAllowInvalidCertificates
```
Expand All @@ -156,9 +158,11 @@ The [DocumentDB for VS Code](https://marketplace.visualstudio.com/items?itemName
3. Select **Add Connection** and enter your connection string:

```text
mongodb://dev_user:DevPassword123@127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0
mongodb://<username>:<password>@127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0
```

Replace the placeholders with credentials from your Secret, percent-encoding any reserved URI characters.

4. Browse databases, collections, and documents. You can also use the built-in playground to run queries interactively.

!!! tip
Expand All @@ -169,11 +173,18 @@ The [DocumentDB for VS Code](https://marketplace.visualstudio.com/items?itemName
All examples below assume:

- DocumentDB is accessible at `127.0.0.1:10260` (via port-forward or LoadBalancer)
- Credentials: username `dev_user`, password `DevPassword123`
- Credentials supplied through `DOCUMENTDB_USERNAME` and `DOCUMENTDB_PASSWORD`
- Self-signed TLS (certificate validation skipped)

For production deployments with trusted certificates, see [TLS configuration](../configuration/tls.md).

Read credentials from the same Secret used by your cluster rather than embedding them in the examples:

```bash
export DOCUMENTDB_USERNAME="$(kubectl get secret documentdb-credentials -n documentdb-ns -o jsonpath='{.data.username}' | base64 -d)"
export DOCUMENTDB_PASSWORD="$(kubectl get secret documentdb-credentials -n documentdb-ns -o jsonpath='{.data.password}' | base64 -d)"
```

### Python (PyMongo)

Install the driver:
Expand All @@ -183,13 +194,15 @@ pip install pymongo
```

```python title="connect_documentdb.py"
import os

from pymongo import MongoClient

client = MongoClient(
host="127.0.0.1",
port=10260,
username="dev_user",
password="DevPassword123",
username=os.environ["DOCUMENTDB_USERNAME"],
password=os.environ["DOCUMENTDB_PASSWORD"],
authSource="admin",
authMechanism="SCRAM-SHA-256",
tls=True,
Expand Down Expand Up @@ -224,12 +237,20 @@ npm install mongodb
```javascript title="connect_documentdb.js"
const { MongoClient } = require("mongodb");

const username = process.env.DOCUMENTDB_USERNAME;
const password = process.env.DOCUMENTDB_PASSWORD;
if (!username || !password) {
throw new Error("DOCUMENTDB_USERNAME and DOCUMENTDB_PASSWORD are required");
}

const uri =
"mongodb://dev_user:DevPassword123@127.0.0.1:10260/" +
"?directConnection=true&authMechanism=SCRAM-SHA-256" +
"mongodb://127.0.0.1:10260/" +
"?directConnection=true" +
"&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0";

const client = new MongoClient(uri, {
auth: { username, password },
authMechanism: "SCRAM-SHA-256",
// Connection pool settings
maxPoolSize: 50,
minPoolSize: 5,
Expand Down Expand Up @@ -273,6 +294,7 @@ import (
"crypto/tls"
"fmt"
"log"
"os"
"time"

"go.mongodb.org/mongo-driver/bson"
Expand All @@ -284,15 +306,21 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

uri := "mongodb://dev_user:DevPassword123@127.0.0.1:10260/" +
"?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true"
username, password := os.Getenv("DOCUMENTDB_USERNAME"), os.Getenv("DOCUMENTDB_PASSWORD")
if username == "" || password == "" {
log.Fatal("DOCUMENTDB_USERNAME and DOCUMENTDB_PASSWORD are required")
}

uri := "mongodb://127.0.0.1:10260/" +
"?directConnection=true&tls=true"

// For self-signed certificates, skip verification.
// For trusted CAs, load the CA cert into a tls.Config instead.
tlsConfig := &tls.Config{InsecureSkipVerify: true}

clientOpts := options.Client().
ApplyURI(uri).
SetAuth(options.Credential{AuthMechanism: "SCRAM-SHA-256", Username: username, Password: password, AuthSource: "admin"}).
SetTLSConfig(tlsConfig).
SetMaxPoolSize(50). // Maximum connections in the pool
SetMinPoolSize(5). // Minimum idle connections
Expand Down Expand Up @@ -345,6 +373,7 @@ Add the dependency to your `pom.xml`:
```java title="ConnectDocumentDB.java"
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
Expand All @@ -359,8 +388,14 @@ import java.util.concurrent.TimeUnit;

public class ConnectDocumentDB {
public static void main(String[] args) throws Exception {
String uri = "mongodb://dev_user:DevPassword123@127.0.0.1:10260/"
+ "?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true";
String username = System.getenv("DOCUMENTDB_USERNAME");
String password = System.getenv("DOCUMENTDB_PASSWORD");
if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
throw new IllegalArgumentException("DOCUMENTDB_USERNAME and DOCUMENTDB_PASSWORD are required");
}

String uri = "mongodb://127.0.0.1:10260/"
+ "?directConnection=true&tls=true";

// For self-signed certificates, create a trust-all SSLContext.
// For trusted CAs, configure a proper TrustStore instead.
Expand All @@ -376,6 +411,7 @@ public class ConnectDocumentDB {

MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(uri))
.credential(MongoCredential.createScramSha256Credential(username, "admin", password.toCharArray()))
.applyToSslSettings(builder -> builder
.enabled(true)
.context(sslContext))
Expand Down Expand Up @@ -438,7 +474,8 @@ When using `CertManager` or `Provided` mode with a trusted CA, remove `tlsAllowI
=== "mongosh"

```bash
mongosh "mongodb://dev_user:DevPassword123@<host>:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&replicaSet=rs0" \
mongosh "mongodb://<host>:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&replicaSet=rs0" \
--username "$DOCUMENTDB_USERNAME" --password \
--tlsCAFile /path/to/ca.crt
```

Expand All @@ -448,8 +485,8 @@ When using `CertManager` or `Provided` mode with a trusted CA, remove `tlsAllowI
client = MongoClient(
host="<host>",
port=10260,
username="dev_user",
password="DevPassword123",
username=os.environ["DOCUMENTDB_USERNAME"],
password=os.environ["DOCUMENTDB_PASSWORD"],
authSource="admin",
authMechanism="SCRAM-SHA-256",
tls=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ In another terminal, get the connection string and connect:
# View the connection string from the DocumentDB cluster status
kubectl get documentdb my-documentdb -n documentdb-ns -o jsonpath='{.status.connectionString}'

# Connect with mongosh (substitute your credentials)
mongosh "mongodb://dev_user:DevPassword123@127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0"
# Enter the password from your credentials Secret when prompted.
mongosh "mongodb://127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0" \
--username dev_user --password
```

### Option 2: K3s built-in load balancer
Expand Down Expand Up @@ -238,7 +239,8 @@ my-documentdb Cluster in healthy state mongodb://...@192.168.1.100:10260/...
Connect with `mongosh` using the external IP:

```bash
mongosh "mongodb://dev_user:DevPassword123@192.168.1.100:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0"
mongosh "mongodb://192.168.1.100:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0" \
--username dev_user --password
```

For more connection options including application drivers, see [Connecting to DocumentDB](connecting-to-documentdb.md).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,15 @@ In a new terminal, retrieve the connection string from the DocumentDB cluster st
kubectl get documentdb my-documentdb -n documentdb-ns -o jsonpath='{.status.connectionString}'
```

Use `mongosh` with the resolved credentials:
Use `mongosh` with your username, and enter the password from the credentials Secret when prompted:

```bash
mongosh "mongodb://dev_user:DevPassword123@127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0"
mongosh "mongodb://127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0" \
--username dev_user --password
```

!!! tip
The `CONNECTION STRING` column in `kubectl get documentdb` output contains embedded `kubectl` commands that extract the username and password from the credentials Secret at runtime. You can copy and `eval` the full string, or substitute your known credentials directly as shown above.
The `CONNECTION STRING` column in `kubectl get documentdb` output contains embedded `kubectl` commands that extract the username and password from the credentials Secret at runtime. The command above keeps the password out of the URI and shell history.

Try inserting and querying data:

Expand Down
7 changes: 5 additions & 2 deletions docs/operator-public-documentation/v1/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,20 @@ kubectl port-forward svc/documentdb-service-documentdb-preview 10260:10260 -n do
```bash
mongosh 127.0.0.1:10260 \
-u k8s_secret_user \
-p K8sSecret100 \
--password \
--authenticationMechanism SCRAM-SHA-256 \
--tls --tlsAllowInvalidCertificates
```

Or use a connection string:

```bash
mongosh "mongodb://k8s_secret_user:K8sSecret100@127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0"
mongosh "mongodb://127.0.0.1:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0" \
--username k8s_secret_user --password
```

Enter the password from your credentials Secret when prompted.

#### Option 2: LoadBalancer service (for cloud deployments)

For direct external access in cloud environments (AKS, EKS, GKE), deploy with a `LoadBalancer` service type:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ Confirm the new primary accepts writes:
kubectl --context new-primary port-forward \
-n documentdb-preview-ns svc/documentdb-preview 10260:10260 &

# Connect with mongosh
mongosh "mongodb://admin:password@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true"
# Connect with your username and enter its password when prompted.
mongosh "mongodb://localhost:10260/?tls=true&tlsAllowInvalidCertificates=true" \
--username "<username>" --password

# Test write operation
db.testCollection.insertOne({
Expand Down Expand Up @@ -328,7 +329,8 @@ kubectl --context new-primary get documentdb documentdb-preview \
kubectl --context new-primary port-forward \
-n documentdb-preview-ns svc/documentdb-preview 10260:10260 &

mongosh "mongodb://admin:password@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true"
mongosh "mongodb://localhost:10260/?tls=true&tlsAllowInvalidCertificates=true" \
--username "<username>" --password
db.testCollection.insertOne({message: "DR failover test"})
```

Expand Down
2 changes: 1 addition & 1 deletion documentdb-playground/aks-setup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ kubectl get secret documentdb-credentials -n documentdb-instance-ns -o jsonpath=
kubectl get secret documentdb-credentials -n documentdb-instance-ns -o jsonpath='{.data.password}' | base64 -d

# Connection string format
mongodb://username:password@EXTERNAL-IP:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0
mongodb://<username>:<password>@<external-ip>:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true&replicaSet=rs0
```

### Common Issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -818,10 +818,33 @@ diagnose_documentdb() {

# 5. Ping test via in-cluster mongosh (validate endpoint independently of app code)
log "Testing DocumentDB connectivity via in-cluster mongosh..."
if kubectl run mongosh-diag-$RANDOM --rm -i --restart=Never --quiet \
--image=mongo:7 -n documentdb-instance-ns -- \
mongosh "mongodb://docdbadmin:SecurePassword123!@documentdb-service-sample-documentdb:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true" \
--quiet --eval "db.runCommand({ping: 1})" 2>/dev/null | grep -q "ok.*1"; then
local diagnostic_pod="mongosh-diag-$RANDOM"
local diagnostic_overrides
diagnostic_overrides=$(cat <<EOF
{
"spec": {
"containers": [{
"name": "$diagnostic_pod",
"image": "mongo:7",
"env": [
{"name": "DOCUMENTDB_USERNAME", "valueFrom": {"secretKeyRef": {"name": "documentdb-credentials", "key": "username"}}},
{"name": "DOCUMENTDB_PASSWORD", "valueFrom": {"secretKeyRef": {"name": "documentdb-credentials", "key": "password"}}}
],
"command": ["mongosh"],
"args": [
"mongodb://documentdb-service-sample-documentdb:10260/?directConnection=true&tls=true&tlsAllowInvalidCertificates=true",
"--quiet",
"--eval",
"const admin = db.getSiblingDB('admin'); if (admin.auth({user: process.env.DOCUMENTDB_USERNAME, pwd: process.env.DOCUMENTDB_PASSWORD, mechanism: 'SCRAM-SHA-256'}).ok !== 1) { throw new Error('Authentication failed'); } admin.runCommand({ping: 1})"
]
}]
}
}
EOF
)
if kubectl run "$diagnostic_pod" --rm -i --restart=Never --quiet \
--image=mongo:7 -n documentdb-instance-ns \
--overrides="$diagnostic_overrides" 2>/dev/null | grep -q "ok.*1"; then
success "DocumentDB ping succeeded"
else
warn "DocumentDB ping failed -- see troubleshooting guide in summary"
Expand Down Expand Up @@ -880,7 +903,7 @@ print_summary() {
echo " - Check DocumentDB: kubectl get documentdb -n documentdb-instance-ns"
echo " - Check service status: kubectl get svc -n documentdb-instance-ns"
echo " - Wait for LoadBalancer IP: kubectl get svc documentdb-service-sample-documentdb -n documentdb-instance-ns -w"
echo " - Once IP is assigned, connect: mongodb://docdbadmin:SecurePassword123!@<EXTERNAL-IP>:10260/"
echo " - Once IP is assigned, connect using credentials from the documentdb-credentials Secret"
echo ""
echo "🔎 Troubleshooting (adapted from DocumentDB troubleshooting best practices):"
echo ""
Expand Down Expand Up @@ -909,7 +932,7 @@ print_summary() {
echo ""
echo " 6. Validate the endpoint independently of your application code:"
echo " kubectl port-forward -n documentdb-instance-ns svc/documentdb-service-sample-documentdb 10260:10260 &"
echo " mongosh \"mongodb://docdbadmin:SecurePassword123!@localhost:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true\""
echo " mongosh \"mongodb://localhost:10260/?directConnection=true&authMechanism=SCRAM-SHA-256&tls=true&tlsAllowInvalidCertificates=true\" --username '<username>' --password"
echo ""
echo " 7. TLS / certificate errors:"
echo " - For self-signed certs (default): keep tlsAllowInvalidCertificates=true in the connection string"
Expand Down Expand Up @@ -962,4 +985,6 @@ main() {
}

# Run main function
main "$@"
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi
Loading
Loading