|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tutorials\QueryableEncryption; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use MongoDB\Client; |
| 7 | +use MongoDB\Driver\ClientEncryption; |
| 8 | +use MongoDB\Driver\Exception\Exception; |
| 9 | +use RuntimeException; |
| 10 | + |
| 11 | +function dropExistingCollection(Client $client, string $databaseName): void |
| 12 | +{ |
| 13 | + $database = $client->getDatabase($databaseName); |
| 14 | + $database->drop(); |
| 15 | +} |
| 16 | + |
| 17 | +function getKMSProviderCredentials(string $kmsProviderName): array |
| 18 | +{ |
| 19 | + switch ($kmsProviderName) { |
| 20 | + case 'aws': |
| 21 | + // start-aws-kms-credentials |
| 22 | + $kmsProviders = [ |
| 23 | + 'aws' => [ |
| 24 | + 'accessKeyId' => getenv('AWS_ACCESS_KEY_ID'), // Your AWS access key ID |
| 25 | + 'secretAccessKey' => getenv('AWS_SECRET_ACCESS_KEY'), // Your AWS secret access key |
| 26 | + ], |
| 27 | + ]; |
| 28 | + // end-aws-kms-credentials |
| 29 | + return $kmsProviders; |
| 30 | + case 'azure': |
| 31 | + // start-azure-kms-credentials |
| 32 | + $kmsProviders = [ |
| 33 | + 'azure' => [ |
| 34 | + 'tenantId' => getenv('AZURE_TENANT_ID'), // Your Azure tenant ID |
| 35 | + 'clientId' => getenv('AZURE_CLIENT_ID'), // Your Azure client ID |
| 36 | + 'clientSecret' => getenv('AZURE_CLIENT_SECRET'), // Your Azure client secret |
| 37 | + ], |
| 38 | + ]; |
| 39 | + // end-azure-kms-credentials |
| 40 | + return $kmsProviders; |
| 41 | + case 'gcp': |
| 42 | + // start-gcp-kms-credentials |
| 43 | + $kmsProviders = [ |
| 44 | + 'gcp' => [ |
| 45 | + 'email' => getenv('GCP_EMAIL'), // Your GCP email |
| 46 | + 'privateKey' => getenv('GCP_PRIVATE_KEY'), // Your GCP private key |
| 47 | + ], |
| 48 | + ]; |
| 49 | + // end-gcp-kms-credentials |
| 50 | + return $kmsProviders; |
| 51 | + case 'kmip': |
| 52 | + // start-kmip-kms-credentials |
| 53 | + $kmsProviders = [ |
| 54 | + 'kmip' => [ |
| 55 | + 'endpoint' => getenv('KMIP_ENDPOINT'), // Your KMIP endpoint |
| 56 | + ], |
| 57 | + ]; |
| 58 | + // end-kmip-kms-credentials |
| 59 | + return $kmsProviders; |
| 60 | + case 'local': |
| 61 | + // start-generate-local-key |
| 62 | + if (!file_exists('./customer-master-key.txt')) { |
| 63 | + file_put_contents('./customer-master-key.txt', base64_encode(random_bytes(96))); |
| 64 | + } |
| 65 | + // end-generate-local-key |
| 66 | + |
| 67 | + // start-get-local-key |
| 68 | + $localMasterKey = file_get_contents('./customer-master-key.txt'); |
| 69 | + $kmsProviders = [ |
| 70 | + 'local' => [ |
| 71 | + 'key' => $localMasterKey, |
| 72 | + ], |
| 73 | + ]; |
| 74 | + // end-get-local-key |
| 75 | + return $kmsProviders; |
| 76 | + default: |
| 77 | + throw new InvalidArgumentException(sprintf('Unrecognized value for KMS provider name. Must be one of: aws, gcp, azure, kmip, or local. Got "%s".', $kmsProviderName)); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function getCustomerMasterKeyCredentials(string $kmsProviderName): array |
| 82 | +{ |
| 83 | + switch ($kmsProviderName) { |
| 84 | + case 'aws': |
| 85 | + // start-aws-cmk-credentials |
| 86 | + $customerMasterKeyCredentials = [ |
| 87 | + 'key' => getenv('AWS_KEY_ARN'), // Your AWS key ID |
| 88 | + 'region' => getenv('AWS_REGION'), // Your AWS region |
| 89 | + ]; |
| 90 | + // end-aws-cmk-credentials |
| 91 | + return $customerMasterKeyCredentials; |
| 92 | + case "azure": |
| 93 | + // start-azure-cmk-credentials |
| 94 | + $customerMasterKeyCredentials = [ |
| 95 | + 'keyVaultEndpoint' => getenv('AZURE_KEY_VAULT_ENDPOINT'), // Your Azure Key Vault Endpoint |
| 96 | + 'keyName' => getenv('AZURE_KEY_NAME'), // Your Azure Key Name |
| 97 | + ]; |
| 98 | + // end-azure-cmk-credentials |
| 99 | + return $customerMasterKeyCredentials; |
| 100 | + case "gcp": |
| 101 | + // start-gcp-cmk-credentials |
| 102 | + $customerMasterKeyCredentials = [ |
| 103 | + 'projectId' => getenv('GCP_PROJECT_ID'), // Your GCP Project ID |
| 104 | + 'location' => getenv('GCP_LOCATION'), // Your GCP Key Location |
| 105 | + 'keyRing' => getenv('GCP_KEY_RING'), // Your GCP Key Ring |
| 106 | + 'keyName' => getenv('GCP_KEY_NAME'), // Your GCP Key Name |
| 107 | + ]; |
| 108 | + // end-gcp-cmk-credentials |
| 109 | + return $customerMasterKeyCredentials; |
| 110 | + case "kmip": |
| 111 | + case "local": |
| 112 | + // start-kmip-local-cmk-credentials |
| 113 | + $customerMasterKeyCredentials = []; |
| 114 | + // end-kmip-local-cmk-credentials |
| 115 | + return $customerMasterKeyCredentials; |
| 116 | + default: |
| 117 | + throw new InvalidArgumentException(sprintf('Unrecognized value for KMS provider name. Must be one of: aws, gcp, azure, kmip, or local. Got "%s".', $kmsProviderName)); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function getAutoEncryptionOptions( |
| 122 | + string $kmsProviderName, |
| 123 | + string $keyVaultNamespace, |
| 124 | + array $kmsProviders, |
| 125 | +): array |
| 126 | +{ |
| 127 | + if ($kmsProviderName === 'kmip') { |
| 128 | + $tlsOptions = getKmipTlsOptions(); |
| 129 | + |
| 130 | + // start-kmip-encryption-options |
| 131 | + $sharedLibraryPathOptions = [ |
| 132 | + 'cryptSharedLibPath' => getenv('SHARED_LIB_PATH'), // Path to your Automatic Encryption Shared Library |
| 133 | + ]; |
| 134 | + |
| 135 | + $autoEncryptionOptions = [ |
| 136 | + 'keyVaultNamespace' => $keyVaultNamespace, |
| 137 | + 'kmsProviders' => $kmsProviders, |
| 138 | + 'sharedLibraryPathOptions' => $sharedLibraryPathOptions, |
| 139 | + 'tlsOptions' => $tlsOptions, |
| 140 | + ]; |
| 141 | + // end-kmip-encryption-options |
| 142 | + return $autoEncryptionOptions; |
| 143 | + } else { |
| 144 | + // start-auto-encryption-options |
| 145 | + $sharedLibraryPathOptions = [ |
| 146 | + 'cryptSharedLibPath' => getenv('SHARED_LIB_PATH'), // Path to your Automatic Encryption Shared Library |
| 147 | + ]; |
| 148 | + |
| 149 | + $autoEncryptionOptions = [ |
| 150 | + 'keyVaultNamespace' => $keyVaultNamespace, |
| 151 | + 'kmsProviders' => $kmsProviders, |
| 152 | + 'sharedLibraryPathOptions' => $sharedLibraryPathOptions, |
| 153 | + ]; |
| 154 | + // end-auto-encryption-options |
| 155 | + |
| 156 | + return $autoEncryptionOptions; |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function getKmipTlsOptions(): array |
| 161 | +{ |
| 162 | + // start-tls-options |
| 163 | + $tlsOptions = [ |
| 164 | + 'kmip' => [ |
| 165 | + 'tlsCAFile' => getenv('KMIP_TLS_CA_FILE'), // Path to your TLS CA file |
| 166 | + 'tlsCertificateKeyFile' => getenv('KMIP_TLS_CERT_FILE'), // Path to your TLS certificate key file |
| 167 | + ], |
| 168 | + ]; |
| 169 | + // end-tls-options |
| 170 | + return $tlsOptions; |
| 171 | +} |
| 172 | + |
| 173 | +function getClientEncryption(Client $encryptedClient, array $autoEncryptionOptions): ClientEncryption |
| 174 | +{ |
| 175 | + // start-create-client-encryption |
| 176 | + $clientEncryption = $encryptedClient->createClientEncryption($autoEncryptionOptions); |
| 177 | + // end-create-client-encryption |
| 178 | + |
| 179 | + return $clientEncryption; |
| 180 | +} |
| 181 | + |
| 182 | +function createEncryptedCollection( |
| 183 | + Client $client, |
| 184 | + ClientEncryption $clientEncryption, |
| 185 | + string $encryptedDatabase, |
| 186 | + string $encryptedCollectionName, |
| 187 | + string $kmsProviderName, |
| 188 | + array $encryptedFieldsMap, |
| 189 | + ?array $customerMasterKeyCredentials, |
| 190 | +): void |
| 191 | +{ |
| 192 | + try { |
| 193 | + // start-create-encrypted-collection |
| 194 | + $client->getDatabase($encryptedDatabase)->createEncryptedCollection( |
| 195 | + $encryptedCollectionName, |
| 196 | + $clientEncryption, |
| 197 | + $kmsProviderName, |
| 198 | + $customerMasterKeyCredentials, |
| 199 | + $encryptedFieldsMap, |
| 200 | + ); |
| 201 | + // end-create-encrypted-collection |
| 202 | + } catch (Exception $e) { |
| 203 | + throw new RuntimeException(sprintf('Unable to create encrypted collection: %s', $e->getMessage()), 0, $e); |
| 204 | + } |
| 205 | +} |
0 commit comments