File: explicit_encryption_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (176 lines) | stat: -rw-r--r-- 4,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Explicit Encryption' do
  require_libmongocrypt
  include_context 'define shared FLE helpers'

  let(:client) { ClientRegistry.instance.new_local_client(SpecConfig.instance.addresses) }

  let(:client_encryption_opts) do
    {
      kms_providers: kms_providers,
      kms_tls_options: kms_tls_options,
      key_vault_namespace: key_vault_namespace
    }
  end

  let(:client_encryption) do
    Mongo::ClientEncryption.new(
      client,
      client_encryption_opts
    )
  end

  before do
    client.use(key_vault_db)[key_vault_coll].drop
  end

  shared_examples 'an explicit encrypter' do
    it 'encrypts and decrypts the value using key_id' do
      data_key_id = client_encryption.create_data_key(
        kms_provider_name,
        data_key_options
      )

      encrypted = client_encryption.encrypt(
        value,
        {
          key_id: data_key_id,
          algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
        }
      )

      decrypted = client_encryption.decrypt(encrypted)
      expect(decrypted).to eq(value)
      expect(decrypted).to be_a_kind_of(value.class)
    end

    it 'encrypts and decrypts the value using key_alt_name' do
      data_key_id = client_encryption.create_data_key(
        kms_provider_name,
        data_key_options.merge(key_alt_names: [key_alt_name])
      )

      encrypted = client_encryption.encrypt(
        value,
        {
          key_alt_name: key_alt_name,
          algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
        }
      )

      decrypted = client_encryption.decrypt(encrypted)
      expect(decrypted).to eq(value)
      expect(decrypted).to be_a_kind_of(value.class)
    end
  end

  context 'value is a string' do
    let(:value) { 'Hello, world!' }

    context 'with AWS KMS provider' do
      include_context 'with AWS kms_providers'
      retry_test

      it_behaves_like 'an explicit encrypter'
    end

    context 'with Azure KMS provider' do
      include_context 'with Azure kms_providers'
      retry_test

      it_behaves_like 'an explicit encrypter'
    end

    context 'with GCP KMS provider' do
      include_context 'with GCP kms_providers'
      retry_test

      it_behaves_like 'an explicit encrypter'
    end

    context 'with KMIP KMS provider' do
      include_context 'with KMIP kms_providers'
      retry_test

      it_behaves_like 'an explicit encrypter'
    end

    context 'with local KMS provider' do
      include_context 'with local kms_providers'

      it_behaves_like 'an explicit encrypter'
    end
  end

  context 'value is an integer' do
    let(:value) { 42 }

    context 'with AWS KMS provider' do
      include_context 'with AWS kms_providers'

      it_behaves_like 'an explicit encrypter'
    end

    context 'with Azure KMS provider' do
      include_context 'with Azure kms_providers'

      it_behaves_like 'an explicit encrypter'
    end

    context 'with GCP KMS provider' do
      include_context 'with GCP kms_providers'

      it_behaves_like 'an explicit encrypter'
    end

    context 'with KMIP KMS provider' do
      include_context 'with KMIP kms_providers'

      it_behaves_like 'an explicit encrypter'
    end

    context 'with local KMS provider' do
      include_context 'with local kms_providers'

      it_behaves_like 'an explicit encrypter'
    end
  end

  context 'value is an symbol' do
    let(:value) { BSON::Symbol::Raw.new(:hello_world) }

    context 'with AWS KMS provider' do
      include_context 'with AWS kms_providers'

      it_behaves_like 'an explicit encrypter'
    end

    context 'with Azure KMS provider' do
      include_context 'with Azure kms_providers'

      it_behaves_like 'an explicit encrypter'
    end

    context 'with GCP KMS provider' do
      include_context 'with GCP kms_providers'

      it_behaves_like 'an explicit encrypter'
    end

    context 'with KMIP KMS provider' do
      include_context 'with KMIP kms_providers'

      it_behaves_like 'an explicit encrypter'
    end

    context 'with local KMS provider' do
      include_context 'with local kms_providers'

      it_behaves_like 'an explicit encrypter'
    end
  end
end