File: external_key_vault_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 (144 lines) | stat: -rw-r--r-- 4,173 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
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

describe 'Client-Side Encryption' do
  describe 'Prose tests: External Key Vault Test' do
    require_libmongocrypt
    require_enterprise
    min_server_fcv '4.2'

    include_context 'define shared FLE helpers'

    let(:client) do
      new_local_client(
        SpecConfig.instance.addresses,
        SpecConfig.instance.test_options
      )
    end

    let(:test_schema_map) do
      {
        'db.coll' => BSON::ExtJSON.parse(File.read('spec/support/crypt/external/external-schema.json'))
      }
    end

    let(:external_key_vault_client) do
      new_local_client(
        SpecConfig.instance.addresses,
        SpecConfig.instance.test_options.merge(
          user: 'fake-user',
          password: 'fake-pwd'
        )
      )
    end

    let(:data_key_id) do
      BSON::Binary.new(Base64.decode64('LOCALAAAAAAAAAAAAAAAAA=='), :uuid)
    end

    before do
      client.use('keyvault')['datakeys'].drop
      client.use('db')['coll'].drop

      data_key = BSON::ExtJSON.parse(File.read('spec/support/crypt/external/external-key.json'))
      client.use('keyvault')['datakeys', write_concern: { w: :majority }].insert_one(data_key)
    end

    context 'with default key vault client' do
      let(:client_encrypted) do
        new_local_client(
          SpecConfig.instance.addresses,
          SpecConfig.instance.test_options.merge(
            auto_encryption_options: {
              kms_providers: local_kms_providers,
              key_vault_namespace: 'keyvault.datakeys',
              schema_map: test_schema_map,
              # Spawn mongocryptd on non-default port for sharded cluster tests
              extra_options: extra_options,
            },
            database: 'db',
          )
        )
      end

      let(:client_encryption) do
        Mongo::ClientEncryption.new(
          client,
          {
            kms_providers: local_kms_providers,
            key_vault_namespace: 'keyvault.datakeys',
          }
        )
      end

      it 'inserts an encrypted document with client' do
        result = client_encrypted['coll'].insert_one(encrypted: 'test')
        expect(result).to be_ok

        encrypted = client.use('db')['coll'].find.first['encrypted']
        expect(encrypted).to be_ciphertext
      end

      it 'encrypts a value with client encryption' do
        encrypted = client_encryption.encrypt(
          'test',
          {
            key_id: data_key_id,
            algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
          }
        )

        expect(encrypted).to be_ciphertext
      end
    end

    context 'with external key vault client' do
      let(:client_encrypted) do
        new_local_client(
          SpecConfig.instance.addresses,
          SpecConfig.instance.test_options.merge(
            auto_encryption_options: {
              kms_providers: local_kms_providers,
              key_vault_namespace: 'keyvault.datakeys',
              schema_map: test_schema_map,
              key_vault_client: external_key_vault_client,
              # Spawn mongocryptd on non-default port for sharded cluster tests
              extra_options: extra_options,
            },
            database: 'db',
          )
        )
      end

      let(:client_encryption) do
        Mongo::ClientEncryption.new(
          external_key_vault_client,
          {
            kms_providers: local_kms_providers,
            key_vault_namespace: 'keyvault.datakeys',
          }
        )
      end

       it 'raises an authentication exception when auto encrypting' do
        expect do
          client_encrypted['coll'].insert_one(encrypted: 'test')
        end.to raise_error(Mongo::Auth::Unauthorized, /fake-user/)
      end

      it 'raises an authentication exception when explicit encrypting' do
        expect do
          client_encryption.encrypt(
            'test',
            {
              key_id: data_key_id,
              algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
            }
          )
        end.to raise_error(Mongo::Auth::Unauthorized, /fake-user/)
      end
    end
  end
end