|
| 1 | +# MongoDB Atlas Cloud Provider Access with Google Cloud Platform (GCP) |
| 2 | + |
| 3 | +This example demonstrates how to set up MongoDB Atlas Cloud Provider Access with Google Cloud Platform (GCP) to enable Customer-Managed Encryption Keys (CMEK) using Google Cloud KMS. |
| 4 | + |
| 5 | +## What This Example Does |
| 6 | + |
| 7 | +The following Terraform configurations: |
| 8 | + |
| 9 | +1. **Creates Cloud Provider Access Setup**: Establishes the initial setup for MongoDB Atlas to access GCP resources |
| 10 | +2. **Authorizes the Access Role**: Completes the authorization process, creating a GCP service account that MongoDB Atlas can use |
| 11 | +3. **Creates GCP KMS Resources**: Sets up a Key Ring and Crypto Key in Google Cloud KMS for encryption |
| 12 | +4. **Configures IAM Permissions**: Grants the MongoDB Atlas service account the necessary permissions to use the KMS key |
| 13 | +5. **Enables Encryption at Rest**: Configures MongoDB Atlas to use the GCP KMS key for encrypting data at rest |
| 14 | + |
| 15 | +## Architecture Overview |
| 16 | + |
| 17 | +``` |
| 18 | +┌─────────────────────┐ ┌─────────────────────────┐ |
| 19 | +│ MongoDB Atlas │ │ Google Cloud │ |
| 20 | +│ │ │ │ |
| 21 | +│ ┌───────────────┐ │ │ ┌─────────────────┐ │ |
| 22 | +│ │ Project │ │ │ │ KMS Key Ring │ │ |
| 23 | +│ │ │ │◄───┤ │ │ │ |
| 24 | +│ │ Cloud Provider│ │ │ │ ┌─────────────┐ │ │ |
| 25 | +│ │ Access │ │ │ │ │ Crypto Key │ │ │ |
| 26 | +│ │ │ │ │ │ └─────────────┘ │ │ |
| 27 | +│ └───────────────┘ │ │ └─────────────────┘ │ |
| 28 | +│ │ │ │ |
| 29 | +│ Service Account ───┼────┤► IAM Permissions │ |
| 30 | +│ (Created by Atlas) │ │ - cryptoKeyEncrypter │ |
| 31 | +│ │ │ Decrypter │ |
| 32 | +│ │ │ - viewer │ |
| 33 | +└─────────────────────┘ └─────────────────────────┘ |
| 34 | +``` |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +Before running this example, you need: |
| 39 | + |
| 40 | +1. **MongoDB Atlas Account**: With API keys that have project owner permissions |
| 41 | +2. **Google Cloud Platform Account**: With a project and appropriate permissions to: |
| 42 | + - Create KMS resources (Key Rings and Crypto Keys) |
| 43 | + - Manage IAM bindings on KMS resources |
| 44 | +3. **Terraform**: Version 0.13 or later |
| 45 | +4. **Google Cloud Terraform Provider**: The Google provider must be configured in your Terraform environment. See the [Google provider documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs) for authentication methods |
| 46 | + |
| 47 | +### Required GCP Permissions |
| 48 | + |
| 49 | +Your user or service account needs the following IAM roles: |
| 50 | +- `roles/cloudkms.admin` - To create and manage KMS resources |
| 51 | +- `roles/resourcemanager.projectIamAdmin` - To manage IAM bindings |
| 52 | + |
| 53 | +## Procedure |
| 54 | + |
| 55 | +### 1. Set Up Variables |
| 56 | + |
| 57 | +Create a `terraform.tfvars` file: |
| 58 | + |
| 59 | +```hcl |
| 60 | +atlas_public_key = <ATLAS_PUBLIC_KEY> |
| 61 | +atlas_private_key = <ATLAS_PRIVATE_KEY> |
| 62 | +atlas_project_id = <ATLAS_PROJECT_ID> |
| 63 | +gcp_project_id = <GCP_PROJECT_ID> |
| 64 | +
|
| 65 | +# Optional: Customize KMS resources |
| 66 | +key_ring_name = <KEY_RING_NAME> |
| 67 | +crypto_key_name = <CRYPTO_KEY_NAME> |
| 68 | +location = <GCP_LOCATION> |
| 69 | +``` |
| 70 | + |
| 71 | +### 2. Deploy the Infrastructure |
| 72 | + |
| 73 | +```bash |
| 74 | +terraform init |
| 75 | +terraform plan |
| 76 | +terraform apply |
| 77 | +``` |
| 78 | + |
| 79 | +### 3. Verify the Setup |
| 80 | + |
| 81 | +After having successfully deployed your infrastructure, you should get the following outputs: |
| 82 | +- Atlas role ID |
| 83 | +- GCP service account email created by Atlas |
| 84 | +- KMS key ring and crypto key IDs |
| 85 | +- Key version resource ID used for encryption |
| 86 | + |
| 87 | +### Resource Dependencies |
| 88 | + |
| 89 | +The configuration manages dependencies between resources: |
| 90 | +- KMS resources are created first |
| 91 | +- Atlas authorization completes before encryption configuration |
| 92 | +- IAM bindings are established before Atlas tries to use the key |
| 93 | + |
| 94 | +### Security Considerations |
| 95 | + |
| 96 | +- The Atlas service account gets minimal required permissions (`cryptoKeyEncrypterDecrypter` and `viewer`) |
| 97 | +- KMS keys are created with `ENCRYPT_DECRYPT` purpose only |
| 98 | + |
| 99 | +## Variables |
| 100 | + |
| 101 | +| Variable | Description | Type | Default | Required | |
| 102 | +|----------|-------------|------|---------|----------| |
| 103 | +| `atlas_public_key` | MongoDB Atlas public API key | string | - | Yes | |
| 104 | +| `atlas_private_key` | MongoDB Atlas private API key | string | - | Yes | |
| 105 | +| `atlas_project_id` | MongoDB Atlas project ID | string | - | Yes | |
| 106 | +| `gcp_project_id` | GCP project ID | string | - | Yes | |
| 107 | +| `key_ring_name` | Name of the KMS key ring | string | `"atlas-key-ring"` | No | |
| 108 | +| `crypto_key_name` | Name of the crypto key | string | `"atlas-crypto-key"` | No | |
| 109 | +| `location` | GCP region for KMS resources | string | `"us-central1"` | No | |
| 110 | + |
| 111 | +## Outputs |
| 112 | + |
| 113 | +| Output | Description | |
| 114 | +|--------|-------------| |
| 115 | +| `atlas_role_id` | The MongoDB Atlas cloud provider access role ID | |
| 116 | +| `gcp_service_account_email` | GCP service account email created by Atlas | |
| 117 | +| `kms_key_ring_id` | Full ID of the created KMS key ring | |
| 118 | +| `kms_crypto_key_id` | Full ID of the created crypto key | |
| 119 | +| `kms_key_version_resource_id` | Resource ID of the primary key version | |
| 120 | + |
| 121 | +## Related Documentation |
| 122 | + |
| 123 | +- [MongoDB Atlas Cloud Provider Access](https://www.mongodb.com/docs/atlas/security/customer-key-management/) |
| 124 | +- [Google Cloud KMS Documentation](https://cloud.google.com/kms/docs) |
| 125 | +- [Terraform MongoDB Atlas Provider](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs) |
0 commit comments