| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <proton/connect_config.hpp> |
| 21 |
#include "connect_config.hpp" |
| 22 |
|
| 23 |
#include "msg.hpp" |
| 24 |
|
| 25 |
#include <proton/connection_options.hpp> |
| 26 |
#include <proton/error.hpp> |
| 27 |
#include <proton/ssl.hpp> |
| 28 |
#include <proton/version.h> |
| 29 |
|
| 30 |
#include <json/value.h> |
| 31 |
#include <json/reader.h> |
| 32 |
#include <json/writer.h> |
| 33 |
|
| 34 |
#include <cstdlib> |
| 35 |
#include <fstream> |
| 36 |
#include <sstream> |
| 37 |
|
| 38 |
using namespace Json; |
| 39 |
using std::string; |
| 40 |
|
| 41 |
namespace { |
| 42 |
const char *type_name(ValueType t) { |
| 43 |
switch (t) { |
| 44 |
case nullValue: return "null"; |
| 45 |
case intValue: return "int"; |
| 46 |
case uintValue: return "uint"; |
| 47 |
case realValue: return "real"; |
| 48 |
case stringValue: return "string"; |
| 49 |
case booleanValue: return "boolean"; |
| 50 |
case arrayValue: return "array"; |
| 51 |
case objectValue: return "object"; |
| 52 |
default: return "unknown"; |
| 53 |
} |
| 54 |
} |
| 55 |
} // namespace |
| 56 |
|
| 57 |
namespace std { |
| 58 |
ostream& operator<<(ostream& o, ValueType t) { return o << type_name(t); } |
| 59 |
} |
| 60 |
|
| 61 |
namespace proton { |
| 62 |
|
| 63 |
namespace { |
| 64 |
|
| 65 |
proton::error err(const string& message) { |
| 66 |
return proton::error("connection configuration: " + message); |
| 67 |
} |
| 68 |
|
| 69 |
Value validate(ValueType t, const Value& v, const string& name) { |
| 70 |
if (v.type() != t) |
| 71 |
throw err(msg() << " '" << name << "' expected " << t << ", found " << v.type()); |
| 72 |
return v; |
| 73 |
} |
| 74 |
|
| 75 |
Value get(ValueType t, const Value& obj, const char *key, const Value& dflt=Value()) { |
| 76 |
Value v = obj.get(key, dflt); |
| 77 |
return v.isNull() ? dflt : validate(t, v, key); |
| 78 |
} |
| 79 |
|
| 80 |
bool get_bool(const Value& obj, const char *key, bool dflt) { |
| 81 |
return get(booleanValue, obj, key, dflt).asBool(); |
| 82 |
} |
| 83 |
|
| 84 |
string get_string(const Value& obj, const char *key, const string& dflt) { |
| 85 |
return get(stringValue, obj, key, dflt).asString(); |
| 86 |
} |
| 87 |
|
| 88 |
static const string HOME("HOME"); |
| 89 |
static const string ENV_VAR("MESSAGING_CONNECT_FILE"); |
| 90 |
static const string FILE_NAME("connect.json"); |
| 91 |
static const string HOME_FILE_NAME("/.config/messaging/" + FILE_NAME); |
| 92 |
static const string ETC_FILE_NAME("/etc/messaging/" + FILE_NAME); |
| 93 |
|
| 94 |
void parse_sasl(Value root, connection_options& opts) { |
| 95 |
Value sasl = get(objectValue, root, "sasl"); |
| 96 |
opts.sasl_enabled(get_bool(sasl, "enable", true)); |
| 97 |
opts.sasl_allow_insecure_mechs(get_bool(sasl, "allow_insecure", false)); |
| 98 |
if (!sasl.isNull()) { |
| 99 |
Value mechs = sasl.get("mechanisms", Value()); |
| 100 |
switch (mechs.type()) { |
| 101 |
case nullValue: |
| 102 |
break; |
| 103 |
case stringValue: |
| 104 |
opts.sasl_allowed_mechs(mechs.asString()); |
| 105 |
break; |
| 106 |
case arrayValue: { |
| 107 |
std::ostringstream s; |
| 108 |
for (ArrayIndex i= 0; i < mechs.size(); ++i) { |
| 109 |
Value v = mechs.get(i, Value()); |
| 110 |
validate(stringValue, v, "sasl/mechanisms"); |
| 111 |
if (i > 0) s << " "; |
| 112 |
s << v.asString(); |
| 113 |
} |
| 114 |
opts.sasl_allowed_mechs(s.str()); |
| 115 |
break; |
| 116 |
} |
| 117 |
default: |
| 118 |
throw err(msg() << "'mechanisms' expected string or array, found " << mechs.type()); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void parse_tls(const string& scheme, Value root, connection_options& opts) { |
| 124 |
Value tls = get(objectValue, root, "tls"); |
| 125 |
if (scheme == "amqps") { // TLS is enabled |
| 126 |
ssl::verify_mode mode = ssl::VERIFY_PEER_NAME; |
| 127 |
Value verifyValue = get(booleanValue, tls, "verify"); |
| 128 |
if (!verifyValue.empty()) { |
| 129 |
mode = verifyValue.asBool() ? ssl::VERIFY_PEER_NAME : ssl::ANONYMOUS_PEER; |
| 130 |
} |
| 131 |
string ca = get_string(tls, "ca", ""); |
| 132 |
string cert = get_string(tls, "cert", ""); |
| 133 |
string key = get_string(tls, "key", ""); |
| 134 |
if (!cert.empty()) { |
| 135 |
ssl_certificate sc = key.empty() ? ssl_certificate(cert) : ssl_certificate(cert, key); |
| 136 |
opts.ssl_client_options(ssl_client_options(sc, ca, mode)); |
| 137 |
} else if (!ca.empty()) { |
| 138 |
opts.ssl_client_options(ssl_client_options(ca, mode)); |
| 139 |
} else if (!verifyValue.empty()) { |
| 140 |
opts.ssl_client_options(ssl_client_options(mode)); |
| 141 |
} else { |
| 142 |
opts.ssl_client_options(ssl_client_options()); |
| 143 |
} |
| 144 |
} else if (!tls.empty()) { |
| 145 |
throw err(msg() << "'tls' object not allowed unless scheme is \"amqps\""); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
} // namespace |
| 150 |
|
| 151 |
std::string parse(Value root, connection_options& opts) { |
| 152 |
std::ostringstream addr; |
| 153 |
|
| 154 |
validate(objectValue, root, "configuration"); |
| 155 |
|
| 156 |
string scheme = get_string(root, "scheme", "amqps"); |
| 157 |
if (scheme != "amqp" && scheme != "amqps") { |
| 158 |
throw err(msg() << "'scheme' must be \"amqp\" or \"amqps\""); |
| 159 |
} |
| 160 |
|
| 161 |
string host = get_string(root, "host", "localhost"); |
| 162 |
opts.virtual_host(host.c_str()); |
| 163 |
addr << host << ":"; |
| 164 |
|
| 165 |
Value port = root.get("port", scheme); |
| 166 |
switch (port.type()) { |
| 167 |
case stringValue: |
| 168 |
addr << port.asString(); break; |
| 169 |
case intValue: |
| 170 |
case uintValue: |
| 171 |
addr << port.asUInt(); break; |
| 172 |
default: |
| 173 |
throw err(msg() << "'port' expected string or uint, found " << port.type()); |
| 174 |
} |
| 175 |
|
| 176 |
Value user = get(stringValue, root, "user"); |
| 177 |
if (!user.isNull()) opts.user(user.asString()); |
| 178 |
Value password = get(stringValue, root, "password"); |
| 179 |
if (!password.isNull()) opts.password(password.asString()); |
| 180 |
|
| 181 |
parse_sasl(root, opts); |
| 182 |
parse_tls(scheme, root, opts); |
| 183 |
|
| 184 |
return addr.str(); |
| 185 |
} |
| 186 |
|
| 187 |
bool config_file(std::ifstream& f, std::string& name) { |
| 188 |
const char *env_path = getenv(ENV_VAR.c_str()); |
| 189 |
const char *home = getenv(HOME.c_str()); |
| 190 |
|
| 191 |
// Try environment variable if set |
| 192 |
if (env_path) { |
| 193 |
name = env_path; |
| 194 |
f.open(name.c_str()); |
| 195 |
return f.good(); |
| 196 |
} |
| 197 |
|
| 198 |
std::vector<std::string> path; |
| 199 |
// current directory |
| 200 |
path.push_back(FILE_NAME); |
| 201 |
// $HOME/.config/messaging/FILE_NAME |
| 202 |
if (home) path.push_back(home + HOME_FILE_NAME); |
| 203 |
// INSTALL_PREFIX/etc/messaging/FILE_NAME |
| 204 |
if (PN_INSTALL_PREFIX && *PN_INSTALL_PREFIX) path.push_back(PN_INSTALL_PREFIX + ETC_FILE_NAME); |
| 205 |
|
| 206 |
for (unsigned i = 0; i < path.size(); ++i) { |
| 207 |
name = path[i]; |
| 208 |
f.open(name.c_str()); |
| 209 |
if (f.good()) return true; |
| 210 |
f.close(); |
| 211 |
} |
| 212 |
|
| 213 |
/* /etc/messaging/FILE_NAME */ |
| 214 |
name = ETC_FILE_NAME; |
| 215 |
f.open(name.c_str()); |
| 216 |
return f.good(); |
| 217 |
} |
| 218 |
|
| 219 |
string apply_config(connection_options& opts) { |
| 220 |
std::string name; |
| 221 |
std::ifstream f; |
| 222 |
bool good = config_file(f, name); |
| 223 |
try { |
| 224 |
Value root; |
| 225 |
if (good) { |
| 226 |
f >> root; |
| 227 |
f.close(); |
| 228 |
} else { |
| 229 |
std::istringstream is("{}"); |
| 230 |
is >> root; |
| 231 |
} |
| 232 |
return parse(root, opts); |
| 233 |
} catch (const std::ifstream::failure& e) { |
| 234 |
throw err(msg() << "io error parsing '" << name << "': " << e.what()); |
| 235 |
} catch (const std::exception& e) { |
| 236 |
throw err(msg() << "error parsing '" << name << "': " << e.what()); |
| 237 |
} catch (...) { |
| 238 |
throw err(msg() << "error parsing '" << name << "'"); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
// Legacy Unsettled API |
| 243 |
|
| 244 |
namespace connect_config { |
| 245 |
|
| 246 |
std::string parse(std::istream& is, connection_options& opts) { |
| 247 |
try { |
| 248 |
Value root; |
| 249 |
is >> root; |
| 250 |
return parse(root, opts); |
| 251 |
} catch (const std::exception& e) { |
| 252 |
throw err(e.what()); |
| 253 |
} catch (...) { |
| 254 |
throw err("unknown error"); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
string default_file() { |
| 259 |
std::string name; |
| 260 |
std::ifstream f; |
| 261 |
bool good = config_file(f, name); |
| 262 |
f.close(); |
| 263 |
if (good || name!=ETC_FILE_NAME) { |
| 264 |
return name; |
| 265 |
} |
| 266 |
throw err("no default configuration, last tried: " + name); |
| 267 |
} |
| 268 |
|
| 269 |
string parse_default(connection_options& opts) { |
| 270 |
std::string name; |
| 271 |
std::ifstream f; |
| 272 |
bool good = config_file(f, name); |
| 273 |
if (!good) { |
| 274 |
throw err("no default configuration, last tried: " + name); |
| 275 |
} |
| 276 |
try { |
| 277 |
Value root; |
| 278 |
f >> root; |
| 279 |
f.close(); |
| 280 |
return parse(root, opts); |
| 281 |
} catch (const std::ifstream::failure& e) { |
| 282 |
throw err(msg() << "io error parsing '" << name << "': " << e.what()); |
| 283 |
} catch (const std::exception& e) { |
| 284 |
throw err(msg() << "error parsing '" << name << "': " << e.what()); |
| 285 |
} catch (...) { |
| 286 |
throw err(msg() << "error parsing '" << name << "'"); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
}} // namespace proton::connect_config |