Chapter 23 · Encryption, CMEK, Network Access, Private Connectivity, and Data Governance

Encryption at Rest / In Transit, Google-Managed Keys, CMEK Concepts, Key Availability, Rotation, and Failure Impact

Protect AtlasMart data with explicit encryption ownership and key-availability runbooks while distinguishing Google-managed encryption, CMEK, client-side encryption, IAM, and recovery dependencies.

Advanced · 160–220 minutesencryption · CMEK · KMS · TLS · availabilityNode 22+ · Firebase CLI 15.30.0 · Firestore emulator 127.0.0.1:8080Mandatory governance lab local/no-cost · CMEK/VPC/managed evidence optional cloud-onlyLast reviewed: 17 September 2026

1. AtlasMart problem: encryption can become an availability dependency

AtlasMart stores customer contact details, order history, support notes, and seller settlement metadata. The team correctly knows that Firestore encrypts stored data by default, but a compliance review now requires explicit key ownership and a tested response if a customer-managed key is disabled. The dangerous shortcut is to say “enable CMEK” and stop there. CMEK changes who controls an encryption dependency; it does not make authorization, logging, backup, deletion, or network design disappear.

Learning outcomes
  • Separate encryption at rest, encryption in transit, CMEK, and client-side encryption.
  • Explain the Firestore/KMS trust path, service-agent permission, key-location constraint, and creation-time choice.
  • Predict what happens when a CMEK version or permission becomes unavailable.
  • Plan rotation without disabling a key version still used by Firestore.
  • Test governance logic locally without pretending the emulator reproduces Cloud KMS.
Execution and safety note

Use the Emulator Suite, a Firebase demo project, or an isolated test project for destructive, security-sensitive, billing-sensitive, migration, backup/restore, or write-heavy exercises unless the lesson explicitly marks managed verification as required. Treat shown output as expected evidence unless it is explicitly identified as captured output, and re-check current Firebase/Google Cloud edition, mode, quota, pricing, and security documentation before production execution.

Chapter 23 reproducibility baseline · reviewed 17 September 2026

AtlasMart keeps project ID demo-atlasmart-firestore, Standard-edition Native mode, database (default), Node.js 22+, Firebase CLI 15.30.0, Firebase Admin Node SDK 14.4.0, Firestore emulator 127.0.0.1:8080, Auth emulator 127.0.0.1:9099, and Emulator UI 127.0.0.1:4000. Mandatory work is deterministic and local. Cloud KMS/CMEK, VPC Service Controls, Private Google Access, regional endpoints, Cloud Audit Logs, managed backup/export evidence, and real residency validation require a real Google Cloud project and are optional controlled verification steps. The emulator does not emulate KMS key availability, Google network routing, service perimeters, regional processing guarantees, or managed audit logs. For optional CMEK verification, use a disposable database and never disable or destroy a production key as a classroom experiment.

Term Meaning in this chapter
Client SDK Mobile/web Firestore path normally authorized with Firebase Authentication and Security Rules; App Check can reduce abuse but does not replace authorization.
Server/Admin SDK Trusted workload path authenticated with Google credentials/IAM; it bypasses Firestore Security Rules and therefore requires application-layer authorization.
Standard / Enterprise Firestore editions with different query/index/pricing capabilities. Governance controls must be checked against the actual edition and interface.
Native Core / Pipeline Enterprise Native can expose familiar Core operations and advanced Pipeline operations. Network/encryption controls protect the database resource; query semantics still differ.
MongoDB compatibility Enterprise mode exposing a MongoDB-compatible protocol and firestore.goog endpoint. It is not MongoDB server software and has distinct private-connectivity instructions.
CMEK Customer-managed encryption key in Cloud KMS used by Firestore for server-side encryption at rest. It is not client-side encryption.
VPC Service Controls A Google Cloud data-exfiltration control that places supported managed services/resources behind a service perimeter. It complements rather than replaces IAM/Rules.

2. Encryption layers: the same word hides different control planes

Layer Who owns the key/control What it protects What it does not prove
Google-managed server-side encryption Google Firestore data before it is written to disk Who may read it; whether logs/exports are governed correctly
CMEK Your organization in Cloud KMS Firestore data at rest including indexes and backups, subject to documented exceptions Client-side confidentiality; request authorization; network isolation
TLS Managed transport security Data in transit between client/workload and service Whether an authenticated caller is entitled to the data
Client-side encryption Your application/key system Selected field/application payload before Firestore receives it Searchability, key recovery, rotation simplicity, or Firestore-level authorization
Wrong mental model

“CMEK means Firestore never sees plaintext.” Incorrect. CMEK is server-side encryption at rest. Firestore decrypts authorized reads transparently. If the requirement is that the service itself only stores opaque application ciphertext, that is a client-side/application-layer design with different query, indexing, key-recovery, and incident-response consequences.

3. CMEK creation contract and trust path

CMEK must be selected when the database is created; an existing Google-default-encrypted database cannot simply be switched in place. The Cloud KMS key must be in the same location as the Firestore database. The Firestore service agent needs roles/cloudkms.cryptoKeyEncrypterDecrypter on the key. The KMS project may be different from the Firestore project, but cross-project ownership must be intentional and audited.

optional-cmek-create.sh
# OPTIONAL: real isolated Google Cloud project only# Placeholders intentionally prevent accidental execution.gcloud firestore databases create \  --database=atlasmart-governed \  --location=FIRESTORE_LOCATION \  --kms-key-name=projects/KMS_PROJECT/locations/KMS_LOCATION/keyRings/RING/cryptoKeys/KEY# Verify metadata after creationgcloud firestore databases describe --database=atlasmart-governed

The database request path does not carry the KMS key on every read/write. Firestore checks key availability asynchronously and performs cryptographic operations through its service integration. Your application should therefore treat KMS availability as a managed dependency, not as a per-request library call.

4. Key unavailability is a database incident, not just a KMS incident

Current Native-mode documentation says Firestore polls Cloud KMS roughly every five minutes. After it detects that the key is unavailable, subsequent reads, writes, and queries can begin failing within about ten minutes with FAILED_PRECONDITION. Import/export operations can fail; index builds and new TTL-policy operations can pause until the key returns. A key can become unavailable because a key version is disabled/destroyed or because Firestore loses permission to use it.

Do not test this on production

The local lab simulates the dependency with a guard file and deterministic failure state. Real key revocation can interrupt the database and, if left inaccessible for an extended period, can lead to severe recovery consequences. Destroying an unrecoverable key can make data permanently inaccessible.

cmek-availability-simulator.mjs
import fs from "node:fs";const policy = JSON.parse(fs.readFileSync("governance/key-state.json", "utf8"));function requireKeyForDatabase() {  if (policy.state !== "AVAILABLE") {    const e = new Error("SIMULATED_CMEK_UNAVAILABLE");    e.code = "FAILED_PRECONDITION";    throw e;  }}for (const op of ["read", "write", "query"]) {  try { requireKeyForDatabase(); console.log(op, "ALLOWED"); }  catch (e) { console.log(op, "DENIED", e.code); }}

5. Rotation: new primary does not mean old version can disappear immediately

When the CMEK primary key version rotates, Firestore re-encrypts the database with the latest primary version. During that process, both old and new versions must remain available. Only after Firestore no longer reports the old version as active should the old version be disabled under your KMS lifecycle policy. Rotation runbooks therefore need a database evidence gate, not merely a KMS “new primary created” event.

Unsafe step Failure mechanism Safer gate
Disable old key version immediately after rotation Firestore may still depend on it during re-encryption Observe active key versions; retire only after old version is absent
Remove service-agent role during IAM cleanup Firestore loses decrypt/encrypt permission Policy-as-code test that service agent keeps only required KMS role
Destroy key version to “prove control” Potential permanent data loss Use local failure simulation or a disposable database/key with explicit recovery plan

6. Backups, clone, restore, and export remain part of key governance

A backup from a CMEK database is encrypted using the database’s encryption mechanism and the primary key version at backup creation. Key rotation does not rewrite the key of an already-created backup. Restore/clone can keep source encryption or, with authorized configuration, select a different encryption type/key. This is why a governance review must include who may restore a CMEK database into a Google-default-encrypted database and who may export data to Cloud Storage.

Encryption controls reduce exposure; they do not enforce purpose limitation. IAM on backup/restore/export operations, bucket governance, retention, deletion policy, and audit evidence remain separate controls.

7. Mandatory local lab: key ownership and failure evidence

governance/key-state.json
{  "database": "(default)",  "mode": "Standard Native",  "encryption": "SIMULATED_CMEK",  "keyOwner": "security-platform",  "servicePrincipal": "firestore-service-agent",  "state": "AVAILABLE",  "rotationGate": "old-version-not-active",  "breakGlassOwner": "incident-commander"}
  1. Run the simulator with AVAILABLE; all operations must print ALLOWED.
  2. Change state to UNAVAILABLE; all operations must fail with simulated FAILED_PRECONDITION.
  3. Restore AVAILABLE and record recovery time in governance/evidence/key-drill.json.
  4. Verify no production KMS resource was changed.

This proves the application/runbook reacts to a database-wide encryption dependency. It does not prove Google Cloud propagation timing, actual KMS audit records, or Firestore service behavior; those require a separately approved real-project drill.

Production judgment

Choose CMEK because the threat/compliance model requires customer-controlled key policy, auditability, organization constraints, or external-key ownership—not because “more encryption” sounds safer. Add a key SLO, alerting, break-glass owner, backup policy, rotation evidence, and tested failure response. Client-side encryption is a separate choice for specific sensitive fields and can reduce Firestore query/index capability.

Knowledge check

  1. Does CMEK provide client-side encryption?
  2. Can you enable CMEK on an existing database in place?
  3. What permission does the Firestore service agent need on the KMS key?
  4. Why can disabling an old key version immediately after rotation be unsafe?
  5. Do IAM/Rules become unnecessary when CMEK is enabled?
Review the answers

1. No; CMEK is server-side encryption at rest.

2. No; create/restore/clone into a CMEK-protected database instead.

3. Cloud KMS CryptoKey Encrypter/Decrypter.

4. Firestore may still be re-encrypting data and using the old version.

5. No. Encryption, authorization, and network governance solve different problems.

Summary and next step

This lesson established the working contract for Encryption at Rest/In Transit, Google-Managed Keys, CMEK Concepts, Key Availability, Rotation, and Failure Impact. Keep its edition/mode assumptions, trust boundary, verification evidence, and operational constraints explicit when reusing the pattern.

Next, continue to VPC/Private Google Access and Service Perimeter Concepts Where Applicable, Egress Paths, and Trusted Backends.

Authoritative references

Keep knowledge open

Help the academy stay free and grow.

If these tutorials save you time, a small donation supports new lessons, technical review, diagrams, examples, and long-term maintenance.

ETHEthereum / ERC-20 only
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0

Send only Ethereum or ERC-20 compatible assets to this address.