22 #include "mongodb_client_config.h" 24 #include <config/config.h> 25 #include <logging/logger.h> 49 logcomp_ =
"MongoDBClient|" + cfgname;
53 enabled_ = config->
get_bool((prefix +
"enabled").c_str());
57 std::string mode =
"connection";
59 mode = config->
get_string((prefix +
"mode").c_str());
62 "MongoDB config '%s' specifies no client " 63 "mode, assuming 'connection'.",
67 if (mode ==
"replica_set" || mode ==
"replicaset") {
69 replicaset_name_ = config->
get_string((prefix +
"name").c_str());
71 std::vector<std::string> hosts = config->
get_strings(prefix +
"hosts");
72 std::transform(hosts.begin(),
74 std::back_inserter(replicaset_hostports_),
75 [](
const std::string &s) -> mongo::HostAndPort {
76 return mongo::HostAndPort(s);
79 }
else if (mode ==
"sync_cluster" || mode ==
"synccluster") {
80 throw Exception(
"sync_cluster connections are no longer supported");
85 conn_hostport_ = mongo::HostAndPort(config->
get_string(prefix +
"hostport"));
104 std::set<std::string> authinfos;
107 std::string dbname = config->
get_string((prefix +
"auth_dbname").c_str());
108 std::string username = config->
get_string((prefix +
"auth_username").c_str());
109 std::string password = config->
get_string((prefix +
"auth_password").c_str());
110 auth_infos_.push_back(AuthInfo(dbname, username, password));
113 "No default authentication info for " 114 "MongoDB client '%s'",
118 std::unique_ptr<Configuration::ValueIterator> i(config->
search((prefix +
"auth/").c_str()));
120 std::string auth_name = std::string(i->path()).substr(prefix.length());
121 auth_name = auth_name.substr(0, auth_name.find(
"/"));
123 if (authinfos.find(auth_name) == authinfos.end()) {
125 std::string ap = prefix + auth_name +
"/";
126 std::string dbname = config->
get_string((ap +
"auth_dbname").c_str());
127 std::string username = config->
get_string((ap +
"auth_username").c_str());
128 std::string password = config->
get_string((ap +
"auth_password").c_str());
129 auth_infos_.push_back(AuthInfo(dbname, username, password));
132 "Incomplete extended auth info '%s' " 133 "for MongoDB client '%s'",
144 mongo::DBClientBase *
147 mongo::DBClientBase *client;
152 mongo::DBClientReplicaSet *repset =
153 new mongo::DBClientReplicaSet(replicaset_name_, replicaset_hostports_);
155 if (!repset->connect())
156 throw Exception(
"Cannot connect to replica set %s", replicaset_name_.c_str());
157 std::list<AuthInfo>::iterator ai;
158 for (ai = auth_infos_.begin(); ai != auth_infos_.end(); ++ai) {
159 if (!repset->auth(ai->dbname, ai->username, ai->clearpwd, errmsg,
false)) {
160 throw Exception(
"Authenticating for %s as %s failed: %s",
162 ai->username.c_str(),
169 mongo::DBClientConnection *clconn =
new mongo::DBClientConnection(
true);
172 if (!clconn->connect(conn_hostport_, errmsg)) {
173 throw Exception(
"Could not connect to MongoDB at %s: %s\n" 174 "You probably forgot to start/enable the mongod service",
175 conn_hostport_.toString().c_str(),
178 std::list<AuthInfo>::iterator ai;
179 for (ai = auth_infos_.begin(); ai != auth_infos_.end(); ++ai) {
180 if (!clconn->auth(ai->dbname, ai->username, ai->clearpwd, errmsg,
false)) {
181 throw Exception(
"Authenticating for %s as %s failed: %s",
183 ai->username.c_str(),
203 logger->
log_info(component,
"%smode: replica set", indent);
204 logger->
log_info(component,
"%shosts:", indent);
205 std::vector<mongo::HostAndPort>::iterator i;
206 for (i = replicaset_hostports_.begin(); i != replicaset_hostports_.end(); ++i) {
207 logger->
log_info(component,
"%s - %s:", indent, i->toString().c_str());
210 if (!auth_infos_.empty()) {
211 logger->
log_info(component,
"%sauth infos:", indent);
212 std::list<AuthInfo>::iterator a;
213 for (a = auth_infos_.begin(); a != auth_infos_.end(); ++a) {
215 component,
"%s - %s @ %s", indent, a->username.c_str(), a->dbname.c_str());
221 logger->
log_info(component,
"%smode: connection", indent);
222 logger->
log_info(component,
"%shost: %s", indent, conn_hostport_.toString().c_str());
223 if (!auth_infos_.empty()) {
224 logger->
log_info(component,
"%sauth infos:", indent);
225 std::list<AuthInfo>::iterator a;
226 for (a = auth_infos_.begin(); a != auth_infos_.end(); ++a) {
228 component,
"%s - %s @ %s", indent, a->username.c_str(), a->dbname.c_str());
241 return conn_hostport_.toString();
ConnectionMode mode() const
Get client configuration mode.
std::string hostport() const
Get host and port of configuration.
Fawkes library namespace.
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
mongo::DBClientBase * create_client()
Create MongoDB client for this configuration.
Base class for exceptions in Fawkes.
MongoDBClientConfig(fawkes::Configuration *config, fawkes::Logger *logger, std::string cfgname, std::string prefix)
Constructor.
virtual void log_info(const char *component, const char *format,...)
Log informational message.
void log(fawkes::Logger *logger, const char *component, const char *indent)
Write client configuration information to log.
virtual std::vector< std::string > get_strings(const char *path)=0
Get list of values from configuration which is of type string.
ConnectionMode
Connection mode enumeration.
Interface for configuration handling.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.