]>
git.proxmox.com Git - ceph.git/blob - 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
6 #include <boost/algorithm/string.hpp>
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>
13 #include "common/buffer_seastar.h"
14 #include "auth/KeyRing.h"
15 #include "include/denc.h"
16 #include "crimson/common/config_proxy.h"
18 namespace crimson::auth
{
20 seastar::future
<seastar::temporary_buffer
<char>> read_file(const std::string
& path
)
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
);
31 seastar::future
<KeyRing
*> load_from_keyring(KeyRing
* keyring
)
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
);
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
);
46 }).then([keyring
] (auto file_exists_and_path
) {
47 const auto& [exists
, path
] = file_exists_and_path
;
49 return read_file(path
).then([keyring
](auto buf
) {
51 bl
.append(buffer::create(std::move(buf
)));
54 return seastar::make_ready_future
<KeyRing
*>(keyring
);
57 return seastar::make_ready_future
<KeyRing
*>(keyring
);
62 seastar::future
<KeyRing
*> load_from_keyfile(KeyRing
* keyring
)
64 auto& path
= crimson::common::local_conf()->keyfile
;
66 return read_file(path
).then([keyring
](auto buf
) {
68 ea
.key
.decode_base64(std::string(buf
.begin(),
70 keyring
->add(crimson::common::local_conf()->name
, ea
);
71 return seastar::make_ready_future
<KeyRing
*>(keyring
);
74 return seastar::make_ready_future
<KeyRing
*>(keyring
);
78 seastar::future
<KeyRing
*> load_from_key(KeyRing
* keyring
)
80 auto& key
= crimson::common::local_conf()->key
;
83 ea
.key
.decode_base64(key
);
84 keyring
->add(crimson::common::local_conf()->name
, ea
);
86 return seastar::make_ready_future
<KeyRing
*>(keyring
);
89 } // namespace crimson::auth