]> git.proxmox.com Git - ceph.git/blob - ceph/src/mount/conf.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / mount / conf.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <string>
5 #include <vector>
6 #include <cstring>
7 #include <map>
8
9 #include "common/async/context_pool.h"
10 #include "common/ceph_context.h"
11 #include "common/ceph_argparse.h"
12 #include "common/config.h"
13 #include "global/global_init.h"
14
15 #include "auth/KeyRing.h"
16 #include "mon/MonClient.h"
17
18 #include "mount.ceph.h"
19
20
21 extern "C" void mount_ceph_get_config_info(const char *config_file,
22 const char *name,
23 bool v2_addrs,
24 struct ceph_config_info *cci)
25 {
26 int err;
27 KeyRing keyring;
28 CryptoKey secret;
29 std::string secret_str;
30 std::string monaddrs;
31 vector<const char *> args = { "--name", name };
32 bool first = true;
33
34 if (config_file) {
35 args.push_back("--conf");
36 args.push_back(config_file);
37 }
38
39 /* Create CephContext */
40 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
41 CODE_ENVIRONMENT_UTILITY,
42 CINIT_FLAG_NO_DAEMON_ACTIONS|CINIT_FLAG_NO_MON_CONFIG);
43 auto& conf = cct->_conf;
44
45 conf.parse_env(cct->get_module_type()); // environment variables override
46 conf.apply_changes(nullptr);
47
48 ceph::async::io_context_pool ioc(1);
49 MonClient monc = MonClient(cct.get(), ioc);
50 err = monc.build_initial_monmap();
51 if (err)
52 goto scrape_keyring;
53
54 for (const auto& mon : monc.monmap.addr_mons) {
55 auto& eaddr = mon.first;
56
57 /*
58 * Filter v1 addrs if we're running in ms_mode=legacy. Filter
59 * v2 addrs for any other ms_mode.
60 */
61 if (v2_addrs) {
62 if (!eaddr.is_msgr2())
63 continue;
64 } else {
65 if (!eaddr.is_legacy())
66 continue;
67 }
68
69 std::string addr;
70 addr += eaddr.ip_only_to_str();
71 addr += ":";
72 addr += std::to_string(eaddr.get_port());
73 /* If this will overrun cci_mons, stop here */
74 if (monaddrs.length() + 1 + addr.length() + 1 > sizeof(cci->cci_mons))
75 break;
76
77 if (first)
78 first = false;
79 else
80 monaddrs += ",";
81
82 monaddrs += addr;
83 }
84
85 if (monaddrs.length())
86 strcpy(cci->cci_mons, monaddrs.c_str());
87 else
88 mount_ceph_debug("Could not discover monitor addresses\n");
89
90 scrape_keyring:
91 err = keyring.from_ceph_context(cct.get());
92 if (err) {
93 mount_ceph_debug("keyring.from_ceph_context failed: %d\n", err);
94 return;
95 }
96
97 if (!keyring.get_secret(conf->name, secret)) {
98 mount_ceph_debug("keyring.get_secret failed\n");
99 return;
100 }
101
102 secret.encode_base64(secret_str);
103
104 if (secret_str.length() + 1 > sizeof(cci->cci_secret)) {
105 mount_ceph_debug("secret is too long\n");
106 return;
107 }
108 strcpy(cci->cci_secret, secret_str.c_str());
109 }