]> git.proxmox.com Git - ceph.git/blob - ceph/src/mount/conf.cc
import ceph 14.2.5
[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/ceph_context.h"
10 #include "common/ceph_argparse.h"
11 #include "global/global_init.h"
12 #include "common/config.h"
13 #include "auth/KeyRing.h"
14 #include "mount.ceph.h"
15 #include "mon/MonClient.h"
16
17 extern "C" void mount_ceph_get_config_info(const char *config_file,
18 const char *name,
19 struct ceph_config_info *cci)
20 {
21 int err;
22 KeyRing keyring;
23 CryptoKey secret;
24 std::string secret_str;
25 std::string monaddrs;
26 vector<const char *> args = { "--name", name };
27 bool first = true;
28
29 if (config_file) {
30 args.push_back("--conf");
31 args.push_back(config_file);
32 }
33
34 /* Create CephContext */
35 auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
36 CODE_ENVIRONMENT_UTILITY,
37 CINIT_FLAG_NO_DAEMON_ACTIONS|CINIT_FLAG_NO_MON_CONFIG);
38 auto& conf = cct->_conf;
39
40 conf.parse_env(cct->get_module_type()); // environment variables override
41 conf.apply_changes(nullptr);
42
43 MonClient monc = MonClient(cct.get());
44 err = monc.build_initial_monmap();
45 if (err)
46 goto scrape_keyring;
47
48 for (const auto& mon : monc.monmap.addr_mons) {
49 auto& eaddr = mon.first;
50
51 // For now, kernel client only accepts legacy addrs
52 if (!eaddr.is_legacy())
53 continue;
54
55 std::string addr;
56 addr += eaddr.ip_only_to_str();
57 addr += ":";
58 addr += std::to_string(eaddr.get_port());
59 /* If this will overrun cci_mons, stop here */
60 if (monaddrs.length() + 1 + addr.length() + 1 > sizeof(cci->cci_mons))
61 break;
62
63 if (first)
64 first = false;
65 else
66 monaddrs += ",";
67
68 monaddrs += addr;
69 }
70
71 if (monaddrs.length())
72 strcpy(cci->cci_mons, monaddrs.c_str());
73 else
74 mount_ceph_debug("Could not discover monitor addresses");
75
76 scrape_keyring:
77 err = keyring.from_ceph_context(cct.get());
78 if (err) {
79 mount_ceph_debug("keyring.from_ceph_context failed: %d\n", err);
80 return;
81 }
82
83 if (!keyring.get_secret(conf->name, secret)) {
84 mount_ceph_debug("keyring.get_secret failed\n");
85 return;
86 }
87
88 secret.encode_base64(secret_str);
89
90 if (secret_str.length() + 1 > sizeof(cci->cci_secret)) {
91 mount_ceph_debug("secret is too long\n");
92 return;
93 }
94 strcpy(cci->cci_secret, secret_str.c_str());
95 }