Skip to content

Commit 6b47855

Browse files
committed
Add locking to credentials
1 parent efddfed commit 6b47855

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

core/origin.cxx

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,37 @@ origin::to_json() const -> std::string
315315

316316
} // namespace couchbase::core
317317

318+
couchbase::core::origin::origin(origin&& other) noexcept
319+
: options_(std::move(other.options_))
320+
, nodes_(std::move(other.nodes_))
321+
, next_node_(std::move(other.next_node_))
322+
, exhausted_(other.exhausted_)
323+
, connection_string_(std::move(other.connection_string_))
324+
{
325+
update_credentials(other.credentials());
326+
}
327+
328+
auto
329+
couchbase::core::origin::operator=(origin&& other) -> origin&
330+
{
331+
if (this != &other) {
332+
options_ = std::move(other.options_);
333+
nodes_ = std::move(other.nodes_);
334+
next_node_ = std::move(other.next_node_);
335+
exhausted_ = other.exhausted_;
336+
connection_string_ = std::move(other.connection_string_);
337+
update_credentials(other.credentials());
338+
}
339+
return *this;
340+
}
341+
318342
couchbase::core::origin::origin(const couchbase::core::origin& other)
319343
: options_(other.options_)
320-
, credentials_(other.credentials_)
321344
, nodes_(other.nodes_)
322345
, next_node_(nodes_.begin())
323346
{
347+
std::shared_lock lock(other.credentials_mutex_);
348+
credentials_ = other.credentials_;
324349
}
325350

326351
couchbase::core::origin::origin(origin other, const topology::configuration& config)
@@ -334,25 +359,26 @@ couchbase::core::origin::origin(couchbase::core::cluster_credentials auth,
334359
std::uint16_t port,
335360
couchbase::core::cluster_options options)
336361
: options_(std::move(options))
337-
, credentials_(std::move(auth))
338362
, nodes_{ { hostname, std::to_string(port) } }
339363
, next_node_(nodes_.begin())
340364
{
365+
std::shared_lock lock(credentials_mutex_);
366+
credentials_ = std::move(auth);
341367
}
342368
couchbase::core::origin::origin(couchbase::core::cluster_credentials auth,
343369
const std::string& hostname,
344370
const std::string& port,
345371
couchbase::core::cluster_options options)
346372
: options_(std::move(options))
347-
, credentials_(std::move(auth))
348373
, nodes_{ { hostname, port } }
349374
, next_node_(nodes_.begin())
350375
{
376+
std::shared_lock lock(credentials_mutex_);
377+
credentials_ = std::move(auth);
351378
}
352379
couchbase::core::origin::origin(couchbase::core::cluster_credentials auth,
353380
const couchbase::core::utils::connection_string& connstr)
354381
: options_(connstr.options)
355-
, credentials_(std::move(auth))
356382
, connection_string_(connstr.input)
357383
{
358384
nodes_.reserve(connstr.bootstrap_nodes.size());
@@ -365,13 +391,16 @@ couchbase::core::origin::origin(couchbase::core::cluster_credentials auth,
365391
shuffle_nodes();
366392
}
367393
next_node_ = nodes_.begin();
394+
395+
std::unique_lock lock(credentials_mutex_);
396+
credentials_ = std::move(auth);
368397
}
369398
auto
370399
couchbase::core::origin::operator=(const couchbase::core::origin& other) -> couchbase::core::origin&
371400
{
372401
if (this != &other) {
373402
options_ = other.options_;
374-
credentials_ = other.credentials_;
403+
update_credentials(other.credentials());
375404
nodes_ = other.nodes_;
376405
next_node_ = nodes_.begin();
377406
exhausted_ = false;
@@ -384,23 +413,27 @@ couchbase::core::origin::connection_string() const -> const std::string&
384413
return connection_string_;
385414
}
386415
auto
387-
couchbase::core::origin::username() const -> const std::string&
416+
couchbase::core::origin::username() const -> std::string
388417
{
418+
const std::shared_lock lock(credentials_mutex_);
389419
return credentials_.username;
390420
}
391421
auto
392-
couchbase::core::origin::password() const -> const std::string&
422+
couchbase::core::origin::password() const -> std::string
393423
{
424+
const std::shared_lock lock(credentials_mutex_);
394425
return credentials_.password;
395426
}
396427
auto
397-
couchbase::core::origin::certificate_path() const -> const std::string&
428+
couchbase::core::origin::certificate_path() const -> std::string
398429
{
430+
const std::shared_lock lock(credentials_mutex_);
399431
return credentials_.certificate_path;
400432
}
401433
auto
402-
couchbase::core::origin::key_path() const -> const std::string&
434+
couchbase::core::origin::key_path() const -> std::string
403435
{
436+
const std::shared_lock lock(credentials_mutex_);
404437
return credentials_.key_path;
405438
}
406439
auto
@@ -471,6 +504,7 @@ couchbase::core::origin::set_nodes_from_config(const topology::configuration& co
471504
void
472505
couchbase::core::origin::update_credentials(cluster_credentials auth)
473506
{
507+
const std::unique_lock lock(credentials_mutex_);
474508
credentials_ = std::move(auth);
475509
}
476510
auto
@@ -508,7 +542,8 @@ couchbase::core::origin::options() -> couchbase::core::cluster_options&
508542
return options_;
509543
}
510544
auto
511-
couchbase::core::origin::credentials() const -> const couchbase::core::cluster_credentials&
545+
couchbase::core::origin::credentials() const -> couchbase::core::cluster_credentials
512546
{
547+
const std::shared_lock lock(credentials_mutex_);
513548
return credentials_;
514549
}

core/origin.hxx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "cluster_credentials.hxx"
2121
#include "cluster_options.hxx"
2222

23+
#include <shared_mutex>
2324
#include <string>
2425
#include <utility>
2526
#include <vector>
@@ -43,7 +44,7 @@ struct origin {
4344
origin() = default;
4445
~origin() = default;
4546

46-
origin(origin&& other) = default;
47+
origin(origin&& other) noexcept;
4748
origin(const origin& other);
4849
origin(origin other, const topology::configuration& config);
4950
origin(cluster_credentials auth,
@@ -55,14 +56,14 @@ struct origin {
5556
const std::string& port,
5657
cluster_options options);
5758
origin(cluster_credentials auth, const utils::connection_string& connstr);
58-
auto operator=(origin&& other) -> origin& = default;
59+
auto operator=(origin&& other) -> origin&;
5960
auto operator=(const origin& other) -> origin&;
6061

6162
[[nodiscard]] auto connection_string() const -> const std::string&;
62-
[[nodiscard]] auto username() const -> const std::string&;
63-
[[nodiscard]] auto password() const -> const std::string&;
64-
[[nodiscard]] auto certificate_path() const -> const std::string&;
65-
[[nodiscard]] auto key_path() const -> const std::string&;
63+
[[nodiscard]] auto username() const -> std::string;
64+
[[nodiscard]] auto password() const -> std::string;
65+
[[nodiscard]] auto certificate_path() const -> std::string;
66+
[[nodiscard]] auto key_path() const -> std::string;
6667

6768
[[nodiscard]] auto get_hostnames() const -> std::vector<std::string>;
6869
[[nodiscard]] auto get_nodes() const -> std::vector<std::string>;
@@ -81,16 +82,17 @@ struct origin {
8182

8283
[[nodiscard]] auto options() const -> const couchbase::core::cluster_options&;
8384
[[nodiscard]] auto options() -> couchbase::core::cluster_options&;
84-
[[nodiscard]] auto credentials() const -> const couchbase::core::cluster_credentials&;
85+
[[nodiscard]] auto credentials() const -> couchbase::core::cluster_credentials;
8586
[[nodiscard]] auto to_json() const -> std::string;
8687

8788
private:
8889
couchbase::core::cluster_options options_{};
89-
cluster_credentials credentials_{};
9090
node_list nodes_{};
9191
node_list::iterator next_node_{};
9292
bool exhausted_{ false };
9393
std::string connection_string_{};
94+
cluster_credentials credentials_{};
95+
mutable std::shared_mutex credentials_mutex_{};
9496
};
9597

9698
} // namespace couchbase::core

0 commit comments

Comments
 (0)