|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +def simulate_failure(type, times = 1) |
| 6 | + url = URI.parse("https://localhost:9003/set_failpoint/#{type}") |
| 7 | + data = { count: times }.to_json |
| 8 | + http = Net::HTTP.new(url.host, url.port) |
| 9 | + http.use_ssl = true |
| 10 | + http.verify_mode = OpenSSL::SSL::VERIFY_NONE |
| 11 | + http.ca_file = '.evergreen/x509gen/ca.pem' |
| 12 | + request = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' }) |
| 13 | + request.body = data |
| 14 | + http.request(request) |
| 15 | +end |
| 16 | + |
| 17 | +describe 'KMS Retry Prose Spec' do |
| 18 | + require_libmongocrypt |
| 19 | + require_enterprise |
| 20 | + min_server_version '4.2' |
| 21 | + |
| 22 | + include_context 'define shared FLE helpers' |
| 23 | + |
| 24 | + let(:key_vault_client) do |
| 25 | + ClientRegistry.instance.new_local_client(SpecConfig.instance.addresses) |
| 26 | + end |
| 27 | + |
| 28 | + let(:client_encryption) do |
| 29 | + Mongo::ClientEncryption.new( |
| 30 | + key_vault_client, |
| 31 | + kms_tls_options: { |
| 32 | + aws: default_kms_tls_options_for_provider, |
| 33 | + gcp: default_kms_tls_options_for_provider, |
| 34 | + azure: default_kms_tls_options_for_provider, |
| 35 | + }, |
| 36 | + key_vault_namespace: key_vault_namespace, |
| 37 | + # For some reason libmongocrypt ignores custom endpoints for Azure and CGP |
| 38 | + # kms_providers: aws_kms_providers.merge(azure_kms_providers).merge(gcp_kms_providers) |
| 39 | + kms_providers: aws_kms_providers |
| 40 | + ) |
| 41 | + end |
| 42 | + |
| 43 | + shared_examples 'kms_retry prose spec' do |
| 44 | + it 'createDataKey and encrypt with TCP retry' do |
| 45 | + simulate_failure('network') |
| 46 | + data_key_id = client_encryption.create_data_key(kms_provider, master_key: master_key) |
| 47 | + simulate_failure('network') |
| 48 | + expect do |
| 49 | + client_encryption.encrypt(123, key_id: data_key_id, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic') |
| 50 | + end.not_to raise_error |
| 51 | + end |
| 52 | + |
| 53 | + it 'createDataKey and encrypt with HTTP retry' do |
| 54 | + simulate_failure('http') |
| 55 | + data_key_id = client_encryption.create_data_key(kms_provider, master_key: master_key) |
| 56 | + simulate_failure('http') |
| 57 | + expect do |
| 58 | + client_encryption.encrypt(123, key_id: data_key_id, algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic') |
| 59 | + end.not_to raise_error |
| 60 | + end |
| 61 | + |
| 62 | + it 'createDataKey fails after too many retries' do |
| 63 | + simulate_failure('network', 4) |
| 64 | + expect do |
| 65 | + client_encryption.create_data_key(kms_provider, master_key: master_key) |
| 66 | + end.to raise_error(Mongo::Error::KmsError) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context 'with AWS KMS provider' do |
| 71 | + let(:kms_provider) { 'aws' } |
| 72 | + |
| 73 | + let(:master_key) do |
| 74 | + { |
| 75 | + region: 'foo', |
| 76 | + key: 'bar', |
| 77 | + endpoint: '127.0.0.1:9003', |
| 78 | + } |
| 79 | + end |
| 80 | + |
| 81 | + include_examples 'kms_retry prose spec' |
| 82 | + end |
| 83 | + |
| 84 | + context 'with GCP KMS provider', skip: 'For some reason libmongocrypt ignores custom endpoints for Azure and CGP' do |
| 85 | + let(:kms_provider) { 'gcp' } |
| 86 | + |
| 87 | + let(:master_key) do |
| 88 | + { |
| 89 | + project_id: 'foo', |
| 90 | + location: 'bar', |
| 91 | + key_ring: 'baz', |
| 92 | + key_name: 'qux', |
| 93 | + endpoint: '127.0.0.1:9003' |
| 94 | + } |
| 95 | + end |
| 96 | + |
| 97 | + include_examples 'kms_retry prose spec' |
| 98 | + end |
| 99 | + |
| 100 | + context 'with Azure KMS provider', skip: 'For some reason libmongocrypt ignores custom endpoints for Azure and CGP' do |
| 101 | + let(:kms_provider) { 'azure' } |
| 102 | + |
| 103 | + let(:master_key) do |
| 104 | + { |
| 105 | + key_vault_endpoint: '127.0.0.1:9003', |
| 106 | + key_name: 'foo', |
| 107 | + } |
| 108 | + end |
| 109 | + |
| 110 | + include_examples 'kms_retry prose spec' |
| 111 | + end |
| 112 | +end |
0 commit comments