Skip to content

Commit 6433dac

Browse files
authored
v1: data_key_options (CXX-3237, CXX-3238) (#1507)
1 parent 7281666 commit 6433dac

File tree

10 files changed

+558
-94
lines changed

10 files changed

+558
-94
lines changed

src/mongocxx/include/mongocxx/v1/data_key_options.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ class data_key_options {
127127
///
128128
/// Return the current "keyMaterial" field.
129129
///
130-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v1::stdx::optional<key_material_type>) key_material();
130+
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v1::stdx::optional<key_material_type>) key_material() const;
131+
132+
class internal;
131133
};
132134

133135
} // namespace v1

src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/data_key-fwd.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#pragma once
1616

17+
#include <mongocxx/v1/data_key_options-fwd.hpp>
18+
1719
#include <mongocxx/config/prelude.hpp>
1820

1921
namespace mongocxx {
@@ -29,7 +31,7 @@ class data_key;
2931
namespace mongocxx {
3032
namespace options {
3133

32-
using ::mongocxx::v_noabi::options::data_key;
34+
using v_noabi::options::data_key;
3335

3436
} // namespace options
3537
} // namespace mongocxx
@@ -40,3 +42,6 @@ using ::mongocxx::v_noabi::options::data_key;
4042
/// @file
4143
/// Declares @ref mongocxx::v_noabi::options::data_key.
4244
///
45+
/// @par Includes
46+
/// - @ref mongocxx/v1/data_key_options-fwd.hpp
47+
///

src/mongocxx/include/mongocxx/v_noabi/mongocxx/options/data_key.hpp

Lines changed: 87 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,22 @@
1414

1515
#pragma once
1616

17+
#include <mongocxx/options/data_key-fwd.hpp> // IWYU pragma: export
18+
19+
//
20+
21+
#include <bsoncxx/v1/document/value.hpp>
22+
23+
#include <mongocxx/v1/data_key_options.hpp> // IWYU pragma: export
24+
25+
#include <cstdint>
1726
#include <string>
27+
#include <utility>
1828
#include <vector>
1929

20-
#include <mongocxx/client_encryption-fwd.hpp>
21-
#include <mongocxx/options/data_key-fwd.hpp> // IWYU pragma: export
30+
#include <mongocxx/client_encryption-fwd.hpp> // IWYU pragma: keep: backward compatibility, to be removed.
2231

32+
#include <bsoncxx/document/view.hpp>
2333
#include <bsoncxx/document/view_or_value.hpp>
2434
#include <bsoncxx/stdx/optional.hpp>
2535

@@ -34,6 +44,35 @@ namespace options {
3444
///
3545
class data_key {
3646
public:
47+
///
48+
/// Default initialization.
49+
///
50+
data_key() = default;
51+
52+
///
53+
/// Construct with the @ref mongocxx::v1 equivalent.
54+
///
55+
/* explicit(false) */ MONGOCXX_ABI_EXPORT_CDECL() data_key(v1::data_key_options key);
56+
57+
///
58+
/// Convert to the @ref mongocxx::v1 equivalent.
59+
///
60+
explicit operator v1::data_key_options() const {
61+
v1::data_key_options ret;
62+
63+
if (_master_key) {
64+
ret.master_key(bsoncxx::v1::document::value{bsoncxx::v_noabi::to_v1(_master_key->view())});
65+
}
66+
67+
ret.key_alt_names(_key_alt_names);
68+
69+
if (_key_material) {
70+
ret.key_material(*_key_material);
71+
}
72+
73+
return ret;
74+
}
75+
3776
///
3877
/// Sets a KMS-specific key used to encrypt the new data key.
3978
///
@@ -99,17 +138,20 @@ class data_key {
99138
/// @see
100139
/// - https://www.mongodb.com/docs/manual/core/security-client-side-encryption-key-management/
101140
///
102-
MONGOCXX_ABI_EXPORT_CDECL(data_key&)
103-
master_key(bsoncxx::v_noabi::document::view_or_value master_key);
141+
data_key& master_key(bsoncxx::v_noabi::document::view_or_value master_key) {
142+
_master_key = std::move(master_key);
143+
return *this;
144+
}
104145

105146
///
106147
/// Gets the master key.
107148
///
108149
/// @return
109150
/// An optional document containing the master key.
110151
///
111-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view_or_value> const&)
112-
master_key() const;
152+
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view_or_value> const& master_key() const {
153+
return _master_key;
154+
}
113155

114156
///
115157
/// Sets an optional list of string alternate names used to reference the key.
@@ -125,20 +167,25 @@ class data_key {
125167
/// @see
126168
/// - https://www.mongodb.com/docs/manual/reference/method/getClientEncryption/
127169
///
128-
MONGOCXX_ABI_EXPORT_CDECL(data_key&) key_alt_names(std::vector<std::string> key_alt_names);
170+
data_key& key_alt_names(std::vector<std::string> key_alt_names) {
171+
_key_alt_names = std::move(key_alt_names);
172+
return *this;
173+
}
129174

130175
///
131176
/// Gets the alternate names for the data key.
132177
///
133178
/// @return
134179
/// The alternate names for the data key.
135180
///
136-
MONGOCXX_ABI_EXPORT_CDECL(std::vector<std::string> const&) key_alt_names() const;
181+
std::vector<std::string> const& key_alt_names() const {
182+
return _key_alt_names;
183+
}
137184

138185
///
139186
/// Represents binary data used to represent key material.
140187
///
141-
using key_material_type = std::vector<uint8_t>;
188+
using key_material_type = std::vector<std::uint8_t>;
142189

143190
///
144191
/// Sets the binary data for the key material
@@ -159,7 +206,10 @@ class data_key {
159206
/// @see
160207
/// - https://www.mongodb.com/docs/v6.0/reference/method/KeyVault.createKey/
161208
///
162-
MONGOCXX_ABI_EXPORT_CDECL(data_key&) key_material(key_material_type key_material);
209+
data_key& key_material(key_material_type key_material) {
210+
_key_material = std::move(key_material);
211+
return *this;
212+
}
163213

164214
///
165215
/// Gets the keyMaterial as binary data
@@ -170,14 +220,13 @@ class data_key {
170220
/// @see
171221
/// - https://www.mongodb.com/docs/v6.0/reference/method/KeyVault.createKey/
172222
///
173-
MONGOCXX_ABI_EXPORT_CDECL(bsoncxx::v_noabi::stdx::optional<key_material_type> const&)
174-
key_material();
223+
bsoncxx::v_noabi::stdx::optional<key_material_type> const& key_material() {
224+
return _key_material;
225+
}
175226

176-
private:
177-
friend ::mongocxx::v_noabi::client_encryption;
178-
179-
void* convert() const;
227+
class internal;
180228

229+
private:
181230
bsoncxx::v_noabi::stdx::optional<bsoncxx::v_noabi::document::view_or_value> _master_key;
182231
std::vector<std::string> _key_alt_names;
183232
bsoncxx::v_noabi::stdx::optional<key_material_type> _key_material;
@@ -187,9 +236,31 @@ class data_key {
187236
} // namespace v_noabi
188237
} // namespace mongocxx
189238

239+
namespace mongocxx {
240+
namespace v_noabi {
241+
242+
///
243+
/// Convert to the @ref mongocxx::v_noabi equivalent of `v`.
244+
///
245+
inline v_noabi::options::data_key from_v1(v1::data_key_options v) {
246+
return {std::move(v)};
247+
}
248+
249+
/// Convert to the @ref mongocxx::v1 equivalent of `v`.
250+
///
251+
inline v1::data_key_options to_v1(v_noabi::options::data_key v) {
252+
return v1::data_key_options{std::move(v)};
253+
}
254+
255+
} // namespace v_noabi
256+
} // namespace mongocxx
257+
190258
#include <mongocxx/config/postlude.hpp>
191259

192260
///
193261
/// @file
194262
/// Provides @ref mongocxx::v_noabi::options::data_key.
195263
///
264+
/// @par Includes
265+
/// - @ref mongocxx/v1/data_key_options.hpp
266+
///

src/mongocxx/lib/mongocxx/v1/data_key_options.cpp

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,156 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
#include <mongocxx/v1/data_key_options.hpp>
15+
#include <mongocxx/v1/data_key_options.hh>
16+
17+
//
18+
19+
#include <bsoncxx/v1/document/value.hpp>
20+
#include <bsoncxx/v1/stdx/optional.hpp>
21+
22+
#include <algorithm>
23+
#include <cstdint>
24+
#include <iterator>
25+
#include <memory>
26+
#include <string>
27+
#include <vector>
28+
29+
#include <mongocxx/private/mongoc.hh>
30+
#include <mongocxx/private/scoped_bson.hh>
31+
#include <mongocxx/private/utility.hh>
32+
33+
namespace mongocxx {
34+
namespace v1 {
35+
36+
class data_key_options::impl {
37+
public:
38+
bsoncxx::v1::stdx::optional<bsoncxx::v1::document::value> _master_key;
39+
std::vector<std::string> _key_alt_names;
40+
bsoncxx::v1::stdx::optional<key_material_type> _key_material;
41+
42+
static impl const& with(data_key_options const& self) {
43+
return *static_cast<impl const*>(self._impl);
44+
}
45+
46+
static impl const* with(data_key_options const* self) {
47+
return static_cast<impl const*>(self->_impl);
48+
}
49+
50+
static impl& with(data_key_options& self) {
51+
return *static_cast<impl*>(self._impl);
52+
}
53+
54+
static impl* with(data_key_options* self) {
55+
return static_cast<impl*>(self->_impl);
56+
}
57+
58+
static impl* with(void* ptr) {
59+
return static_cast<impl*>(ptr);
60+
}
61+
};
62+
63+
data_key_options::~data_key_options() {
64+
delete impl::with(this);
65+
}
66+
67+
data_key_options::data_key_options(data_key_options&& other) noexcept : _impl{exchange(other._impl, nullptr)} {}
68+
69+
data_key_options& data_key_options::operator=(data_key_options&& other) noexcept {
70+
if (this != &other) {
71+
delete impl::with(exchange(_impl, exchange(other._impl, nullptr)));
72+
}
73+
74+
return *this;
75+
}
76+
77+
data_key_options::data_key_options(data_key_options const& other) : _impl{new impl{impl::with(other)}} {}
78+
79+
data_key_options& data_key_options::operator=(data_key_options const& other) {
80+
if (this != &other) {
81+
delete impl::with(exchange(_impl, new impl{impl::with(other)}));
82+
}
83+
84+
return *this;
85+
}
86+
87+
data_key_options::data_key_options() : _impl{new impl{}} {}
88+
89+
data_key_options& data_key_options::master_key(bsoncxx::v1::document::value master_key) {
90+
impl::with(this)->_master_key = std::move(master_key);
91+
return *this;
92+
}
93+
94+
bsoncxx::v1::stdx::optional<bsoncxx::v1::document::view> data_key_options::master_key() const {
95+
return impl::with(this)->_master_key;
96+
}
97+
98+
data_key_options& data_key_options::key_alt_names(std::vector<std::string> key_alt_names) {
99+
impl::with(this)->_key_alt_names = std::move(key_alt_names);
100+
return *this;
101+
}
102+
103+
std::vector<std::string> data_key_options::key_alt_names() const {
104+
return impl::with(this)->_key_alt_names;
105+
}
106+
107+
data_key_options& data_key_options::key_material(key_material_type key_material) {
108+
impl::with(this)->_key_material = std::move(key_material);
109+
return *this;
110+
}
111+
112+
bsoncxx::v1::stdx::optional<data_key_options::key_material_type> data_key_options::key_material() const {
113+
return impl::with(this)->_key_material;
114+
}
115+
116+
std::unique_ptr<mongoc_client_encryption_datakey_opts_t, data_key_options::internal::deleter_type>
117+
data_key_options::internal::to_mongoc(data_key_options const& self) {
118+
std::unique_ptr<mongoc_client_encryption_datakey_opts_t, data_key_options::internal::deleter_type> ret{
119+
libmongoc::client_encryption_datakey_opts_new()};
120+
121+
auto const opts = ret.get();
122+
auto& _impl = impl::with(self);
123+
124+
if (auto const& opt = _impl._master_key) {
125+
libmongoc::client_encryption_datakey_opts_set_masterkey(opts, scoped_bson_view{opt->view()}.bson());
126+
}
127+
128+
if (!_impl._key_alt_names.empty()) {
129+
std::vector<char*> names;
130+
131+
names.reserve(_impl._key_alt_names.size());
132+
std::transform(
133+
_impl._key_alt_names.begin(),
134+
_impl._key_alt_names.end(),
135+
std::back_inserter(names),
136+
[](std::string const& name) {
137+
return const_cast<char*>(name.c_str()); // For copy only.
138+
});
139+
140+
libmongoc::client_encryption_datakey_opts_set_keyaltnames(
141+
opts, names.data(), static_cast<std::uint32_t>(_impl._key_alt_names.size()));
142+
}
143+
144+
if (auto const& opt = _impl._key_material) {
145+
libmongoc::client_encryption_datakey_opts_set_keymaterial(
146+
opts, opt->data(), static_cast<std::uint32_t>(opt->size()));
147+
}
148+
149+
return ret;
150+
}
151+
152+
bsoncxx::v1::stdx::optional<bsoncxx::v1::document::value>& data_key_options::internal::master_key(
153+
data_key_options& self) {
154+
return impl::with(self)._master_key;
155+
}
156+
157+
std::vector<std::string>& data_key_options::internal::key_alt_names(data_key_options& self) {
158+
return impl::with(self)._key_alt_names;
159+
}
160+
161+
bsoncxx::v1::stdx::optional<data_key_options::key_material_type>& data_key_options::internal::key_material(
162+
data_key_options& self) {
163+
return impl::with(self)._key_material;
164+
}
165+
166+
} // namespace v1
167+
} // namespace mongocxx

0 commit comments

Comments
 (0)