summaryrefslogtreecommitdiffstats
path: root/src/mount/conf.cc
blob: 05dc09c04804b35e9a59da4824f5a2ab1f453d33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include <string>
#include <vector>
#include <cstring>
#include <map>

#include "common/async/context_pool.h"
#include "common/ceph_context.h"
#include "common/ceph_argparse.h"
#include "common/config.h"
#include "global/global_init.h"

#include "auth/KeyRing.h"
#include "mon/MonClient.h"

#include "mount.ceph.h"


extern "C" void mount_ceph_get_config_info(const char *config_file,
					   const char *name,
					   bool v2_addrs,
					   struct ceph_config_info *cci)
{
  int err;
  KeyRing keyring;
  CryptoKey secret;
  std::string secret_str;
  std::string monaddrs;
  vector<const char *> args = { "--name", name };
  bool first = true;

  if (config_file) {
    args.push_back("--conf");
    args.push_back(config_file);
  }

  /* Create CephContext */
  auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
			 CODE_ENVIRONMENT_UTILITY,
			 CINIT_FLAG_NO_DAEMON_ACTIONS|CINIT_FLAG_NO_MON_CONFIG);
  auto& conf = cct->_conf;

  conf.parse_env(cct->get_module_type()); // environment variables override
  conf.apply_changes(nullptr);

  ceph::async::io_context_pool ioc(1);
  MonClient monc = MonClient(cct.get(), ioc);
  err = monc.build_initial_monmap();
  if (err)
    goto scrape_keyring;

  for (const auto& mon : monc.monmap.addr_mons) {
    auto& eaddr = mon.first;

    /*
     * Filter v1 addrs if we're running in ms_mode=legacy. Filter
     * v2 addrs for any other ms_mode.
     */
    if (v2_addrs) {
      if (!eaddr.is_msgr2())
	continue;
    } else {
      if (!eaddr.is_legacy())
	continue;
    }

    std::string addr;
    addr += eaddr.ip_n_port_to_str();
    /* If this will overrun cci_mons, stop here */
    if (monaddrs.length() + 1 + addr.length() + 1 > sizeof(cci->cci_mons))
      break;

    if (first)
      first = false;
    else
      monaddrs += ",";

    monaddrs += addr;
  }

  if (monaddrs.length())
    strcpy(cci->cci_mons, monaddrs.c_str());
  else
    mount_ceph_debug("Could not discover monitor addresses\n");

scrape_keyring:
  err = keyring.from_ceph_context(cct.get());
  if (err) {
    mount_ceph_debug("keyring.from_ceph_context failed: %d\n", err);
    return;
  }

  if (!keyring.get_secret(conf->name, secret)) {
    mount_ceph_debug("keyring.get_secret failed\n");
    return;
  }

  secret.encode_base64(secret_str);

  if (secret_str.length() + 1 > sizeof(cci->cci_secret)) {
    mount_ceph_debug("secret is too long\n");
    return;
  }
  strcpy(cci->cci_secret, secret_str.c_str());
}