]> git.proxmox.com Git - ceph.git/blob - ceph/src/crimson/auth/KeyRing.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / crimson / auth / KeyRing.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 "KeyRing.h"
5
6 #include <boost/algorithm/string.hpp>
7
8 #include <seastar/core/do_with.hh>
9 #include <seastar/core/fstream.hh>
10 #include <seastar/core/future-util.hh>
11 #include <seastar/core/reactor.hh>
12
13 #include "common/buffer_seastar.h"
14 #include "auth/KeyRing.h"
15 #include "include/denc.h"
16 #include "crimson/common/config_proxy.h"
17
18 namespace crimson::auth {
19
20 seastar::future<seastar::temporary_buffer<char>> read_file(const std::string& path)
21 {
22 return seastar::open_file_dma(path, seastar::open_flags::ro).then([] (seastar::file f) {
23 return f.size().then([f = std::move(f)](size_t s) {
24 return seastar::do_with(seastar::make_file_input_stream(f), [s](seastar::input_stream<char>& in) {
25 return in.read_exactly(s);
26 });
27 });
28 });
29 }
30
31 seastar::future<KeyRing*> load_from_keyring(KeyRing* keyring)
32 {
33 std::vector<std::string> paths;
34 boost::split(paths, crimson::common::local_conf()->keyring,
35 boost::is_any_of(",;"));
36 std::pair<bool, std::string> found;
37 return seastar::map_reduce(paths, [](auto path) {
38 return seastar::engine().file_exists(path).then([path](bool file_exists) {
39 return std::make_pair(file_exists, path);
40 });
41 }, std::move(found), [](auto found, auto file_exists_and_path) {
42 if (!found.first && file_exists_and_path.first) {
43 found = std::move(file_exists_and_path);
44 }
45 return found;
46 }).then([keyring] (auto file_exists_and_path) {
47 const auto& [exists, path] = file_exists_and_path;
48 if (exists) {
49 return read_file(path).then([keyring](auto buf) {
50 bufferlist bl;
51 bl.append(buffer::create(std::move(buf)));
52 auto i = bl.cbegin();
53 keyring->decode(i);
54 return seastar::make_ready_future<KeyRing*>(keyring);
55 });
56 } else {
57 return seastar::make_ready_future<KeyRing*>(keyring);
58 }
59 });
60 }
61
62 seastar::future<KeyRing*> load_from_keyfile(KeyRing* keyring)
63 {
64 auto& path = crimson::common::local_conf()->keyfile;
65 if (!path.empty()) {
66 return read_file(path).then([keyring](auto buf) {
67 EntityAuth ea;
68 ea.key.decode_base64(std::string(buf.begin(),
69 buf.end()));
70 keyring->add(crimson::common::local_conf()->name, ea);
71 return seastar::make_ready_future<KeyRing*>(keyring);
72 });
73 } else {
74 return seastar::make_ready_future<KeyRing*>(keyring);
75 }
76 }
77
78 seastar::future<KeyRing*> load_from_key(KeyRing* keyring)
79 {
80 auto& key = crimson::common::local_conf()->key;
81 if (!key.empty()) {
82 EntityAuth ea;
83 ea.key.decode_base64(key);
84 keyring->add(crimson::common::local_conf()->name, ea);
85 }
86 return seastar::make_ready_future<KeyRing*>(keyring);
87 }
88
89 } // namespace crimson::auth