Files
floci/docs/services/docdb.md
T
Michal Schreiber 7b83bd5283 feat(rds,docdb): tag DocumentDB clusters and parameter groups (#2408)
* fix(rds): tag DB and DB cluster parameter groups

The tag resolver handled db, cluster, subgrp, db-proxy and og, so tagging a
parameter group answered that Floci does not implement the type — a live account
tags both, and the terraform provider reads a group's tags as part of its read.
DocumentDB's cluster parameter groups are these records too, so the same read
fails for aws_docdb_cluster_parameter_group.

Neither model carried tags, and neither carried the ARN a caller tags by: the
create response omitted it, leaving nothing to address the group with. Both are
stored and reported now, and tags given at create are kept, as a live account
keeps them.

* feat(docdb): tag clusters and instances

aws_docdb_cluster reads a cluster's tags as part of every read, so a cluster
that created fine failed its apply on DBClusterNotFoundFault — created, but not
readable.

DocumentDB signs with the rds credential scope, and on that scope it is told
apart by Engine or a cluster/instance identifier. The tag actions carry neither,
only the ARN of the resource they address, so they reached the RDS handler,
which does not hold DocumentDB's records. The router reads that ARN now, and an
ARN naming something DocumentDB does not have still goes to RDS.

Clusters and instances store tags, keep the ones given at create, and answer the
three tag actions. Behaviour follows a live account: an existing key is
overwritten, removing a key that is not present is not an error, and an ARN in
another region or account is rejected with the one message AWS gives for both —
storage is keyed by identifier alone, so without that check an ARN naming
another region would be answered with this caller's cluster.

A tag update is a read-modify-write, so it takes the record's monitor and the
other writers take the same one; otherwise tagging a cluster while it is being
deleted stores it again. Paths that hold both take the instance's monitor before
the cluster's.

Filters on ListTagsForResource are accepted without validation: a live account
rejects an unrecognised name, but Floci carries no list of the ones it accepts,
and the tags of one named resource are the same answer either way.

* fix(rds,docdb): store an empty value for a tag given without one

A tag whose Key arrived without a Value was stored with a null, and the read
hands the tags back as an immutable copy, which rejects nulls — so one such tag
made every subsequent read of that resource fail with an internal error, not
just the tag that caused it. A live account stores an empty value and reads it
back.

Both handlers parse tags the same way and both had it.

* fix(rds,docdb): match the whole ARN when routing tags, and give legacy groups one

Two things reported on review.

Routing read the trailing name of the ResourceName ARN and looked it up in
DocumentDB storage, but RDS and DocumentDB share one ARN space: an RDS resource
whose name a DocumentDB record happens to share was answered — and mutated —
out of the wrong store, and an RDS parameter group or option group could be
resolved as a cluster. The whole stored ARN is matched now, which settles
region, account, type and name at once, as the db-cluster-id filter already
does.

And only creation assigned a parameter group's ARN, so a group persisted by an
earlier version read back without one, leaving describe with no ARN to report
and the caller nothing to tag by. The read that backfills a missing region
backfills the ARN too.

The service keeps rejecting an ARN from another region or account: the router
no longer sends it one, but the check guards its own contract, and it is
covered directly rather than through the endpoint.

* fix(rds,docdb): keep one identifier space across the RDS family

Matching the whole ARN still left one ambiguity: an RDS cluster and a
DocumentDB cluster of the same name in the same account and region produce the
same ARN, and a tag call naming it could only reach one of them.

A live account settles it — creating an Aurora cluster named like an existing
DocumentDB one is refused with DBClusterAlreadyExistsFault, because the RDS
family shares one identifier space. Floci already refused that direction, since
a create naming an identifier DocumentDB holds is routed to DocumentDB, which
answers AlreadyExists; the other direction was open, so a docdb create could
take a name RDS already had.

A create naming an identifier RDS holds now goes to RDS whatever engine it
names, so it answers AlreadyExists too and the two stores cannot both hold one
identifier — which is what makes an ARN unambiguous.

* fix(rds,docdb): report a persisted identifier held by both services

Creates can no longer put one identifier in both stores, but state written
before that can, and a restart loads it. For such a record the identifier
checks in the router already send every action to DocumentDB — describe, modify
and delete included — and have long before tags existed, so tags answer from
there too: one identifier keeps one answer, rather than a tag call reaching a
record no describe can show.

The collision is logged once per ARN, since the RDS record behind it cannot be
reached on this scope at all and only deleting one of the two records fixes it.

* fix(docdb): build cluster and instance ARNs from the caller's region

The ARN was built from the configured default region, so a caller signing for
anywhere else got back an ARN naming somewhere they were not — and since a tag
call is checked against the region it is made from, that caller could not tag
the cluster the create had just answered with.

It also reopened the identifier collision from the other side: the create-time
check looked for an RDS record in the request region while the ARN landed in
the default one, so signing the create for another region got past the check and
produced two records under one ARN. DocumentDB keys records by bare identifier,
so the check now covers every region — narrower than AWS, which allows one name
per region, but Floci cannot hold both and answer either correctly.

The check also asked about the wrong identifier for instances: CreateDBInstance
names its parent cluster, and that cluster existing in RDS is the normal case,
not a clash. Only the identifier being created is checked, and the clash is
answered with AlreadyExists directly rather than by handing the create to a
service that rejects the engine first.

* fix(rds): scope the create-time identifier check to the request's region

Widening it to every region broke what RDS actually does: names are scoped by
region, so the same instance name in two regions is two resources, and
RdsControlPlaneTest.sdkScopesTheSameDbInstanceNameBySignedRegion pins exactly
that. The second create answered DBInstanceAlreadyExists.

The cross-region case that widening was meant to cover is closed by the ARNs
instead: each carries the region it was created in, so two records of one name
in different regions never share an ARN and no tag call has to choose between
them. What must not exist is one identifier in both services in one region, and
that is what the check now asks about.

* fix(docdb): decide ownership of an identifier per region

The routing checks asked whether DocumentDB holds an identifier at all, and its
records are keyed by identifier alone, so a name DocumentDB held in one region
was taken away from RDS in every other one: the create was refused as already
existing, and a describe, modify or delete answered from the wrong region's
record.

The region now comes from the one place a record carries it — the ARN — so
DocumentDB claims an identifier only in the region it was created in. Records
written before ARNs carried the caller's region read as the configured default,
which is the region they were in fact created under.

This was true before tags existed; it becomes reachable in a way worth fixing
now that a create in another region records that region rather than the default.

* fix(docdb): check the identifier on every route into DocumentDB

The create-time check lived in the rds credential scope's branch, but
DocumentDB is reachable under its own scope too, and that one dispatches
straight to its handler — which knows only its own store. A create signed that
way could take an identifier RDS already held in the region, which is the state
the ARN matching relies on being impossible.

The check is one method now, called from both routes.

Neptune shares the same identifier space and its scope is not checked either;
that is untouched here, since this PR's invariant is about the ARN a tag call
resolves and Neptune holds no tags.

* fix(docdb): resolve a tag ARN to the record it names, not to the identifier

resolveTagTarget checked the ARN's region against the caller's and then loaded
the record by identifier, and records are keyed by identifier alone — so an ARN
whose region matched the request but named a record stored in another region
resolved to that other record, and tagged it.

Reachable on the docdb credential scope, which dispatches to this service
directly rather than through the routing that matches the whole ARN. The stored
ARN is now compared with the one asked for, and anything else is not found.
2026-08-19 20:23:32 -04:00

5.7 KiB

DocumentDB

Protocol: Query (XML) for the management API Management Endpoint: POST http://localhost:4566/ with Action= param Data Endpoint: the Endpoint and Port returned by DescribeDBClusters (MongoDB wire protocol)

Floci emulates Amazon DocumentDB by managing real MongoDB Docker containers behind an RDS-shaped control plane. DocumentDB is MongoDB-compatible, so the cluster endpoint returned by DescribeDBClusters speaks the MongoDB wire protocol and works with any standard MongoDB driver.

Always read the host and port from DescribeDBClusters rather than assuming a fixed port. MongoDB listens on 27017 inside the container, but the port you connect to depends on how Floci runs:

  • Real mode, Floci on the host (default): the container's 27017 is published on a dynamically assigned host port. DescribeDBClusters.Port returns that mapped port.
  • Real mode, Floci itself in a container (shared Docker network): the endpoint is the container host on 27017.
  • Mock mode (FLOCI_SERVICES_DOCDB_MOCK=true): no container is started; the cluster reports localhost:27017.

The management API shares the RDS Query endpoint (POST / with an Action= parameter). Requests are routed to DocumentDB when Engine=docdb is supplied, or when the referenced cluster/instance is a known DocumentDB resource.

Supported Actions

Action Description
CreateDBCluster Create a DocumentDB cluster and start a MongoDB container
DescribeDBClusters List clusters and their connection details
DescribeDBClusterSnapshots -
DescribeGlobalClusters List global clusters — always empty, as none are modeled
DeleteDBCluster Stop and remove a cluster (must have no instances)
ModifyDBCluster Update engine version or IAM auth setting
CreateDBInstance Add an instance to a cluster
DescribeDBInstances List instances
DeleteDBInstance Remove an instance from a cluster
ModifyDBInstance Update instance class or IAM auth setting
ListTagsForResource List a cluster's or instance's tags
AddTagsToResource Add or overwrite tags on a cluster or instance
RemoveTagsFromResource Remove tags by key from a cluster or instance

Configuration

Variable Default Description
FLOCI_SERVICES_DOCDB_ENABLED true Enable or disable DocumentDB
FLOCI_SERVICES_DOCDB_MOCK false Mock mode: skip the container and return a placeholder endpoint
FLOCI_SERVICES_DOCDB_DEFAULT_IMAGE mongo:7.0 MongoDB Docker image
FLOCI_SERVICES_DOCDB_DOCKER_NETWORK (host default) Docker network for container connectivity

Mock mode is useful for control-plane tests that do not need a live database; the cluster reports localhost:27017 and no container is started.

Docker Compose

DocumentDB needs the Docker socket so it can launch MongoDB containers. Each cluster's container is published on a dynamically assigned host port, returned by DescribeDBClusters.

services:
  floci:
    image: floci/floci:latest
    ports:
      - "4566:4566"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      FLOCI_SERVICES_DOCDB_DOCKER_NETWORK: my-project_default

For private registry authentication and other Docker settings see Docker Configuration.

Examples

Management API (AWS CLI)

export AWS_ENDPOINT_URL=http://localhost:4566
export AWS_DEFAULT_REGION=us-east-1
export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test

# Create a DocumentDB cluster (starts a MongoDB container)
aws docdb create-db-cluster \
  --db-cluster-identifier my-docdb \
  --engine docdb \
  --master-username admin \
  --master-user-password secret99

# Get the cluster endpoint and port
aws docdb describe-db-clusters \
  --db-cluster-identifier my-docdb \
  --query 'DBClusters[0].{Endpoint:Endpoint,Port:Port}'

# Add an instance to the cluster
aws docdb create-db-instance \
  --db-instance-identifier my-docdb-instance \
  --db-cluster-identifier my-docdb \
  --db-instance-class db.r5.large \
  --engine docdb

# Delete instance and cluster
aws docdb delete-db-instance \
  --db-instance-identifier my-docdb-instance
aws docdb delete-db-cluster \
  --db-cluster-identifier my-docdb \
  --skip-final-snapshot

Data plane (Python + pymongo)

from pymongo import MongoClient

# Read the host and port from DescribeDBClusters — the port is dynamic
# in real mode and is NOT guaranteed to be 27017.
host, port = "localhost", 32768  # e.g. DBClusters[0].Endpoint / .Port
client = MongoClient(f"mongodb://admin:secret99@{host}:{port}/")

db = client["app"]
db["people"].insert_one({"name": "Alice"})

for doc in db["people"].find():
    print(doc)

client.close()

Management API (Python / boto3)

import boto3

docdb = boto3.client(
    "docdb",
    endpoint_url="http://localhost:4566",
    region_name="us-east-1",
)

cluster = docdb.create_db_cluster(
    DBClusterIdentifier="my-docdb",
    Engine="docdb",
    MasterUsername="admin",
    MasterUserPassword="secret99",
)
print(cluster["DBCluster"]["Endpoint"])

Out of Scope

  • IAM database authentication for MongoDB connections (the flag is stored and echoed back, but connections are not SigV4-proxied).
  • TLS / --tls enforced connections.
  • Snapshot creation and restore (DescribeDBClusterSnapshots returns an empty stub result; snapshots are not modeled).
  • Global clusters, replicas, and read-scaling beyond a single MongoDB container per cluster.
  • Parameter groups, subnet groups, and maintenance windows.