1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
17 #include "mon/AuthMonitor.h"
18 #include "mon/Monitor.h"
19 #include "mon/MonitorDBStore.h"
20 #include "mon/OSDMonitor.h"
21 #include "mon/MDSMonitor.h"
22 #include "mon/ConfigMonitor.h"
24 #include "messages/MMonCommand.h"
25 #include "messages/MAuth.h"
26 #include "messages/MAuthReply.h"
27 #include "messages/MMonGlobalID.h"
28 #include "msg/Messenger.h"
30 #include "auth/AuthServiceHandler.h"
31 #include "auth/KeyRing.h"
32 #include "include/stringify.h"
33 #include "include/ceph_assert.h"
35 #include "mds/MDSAuthCaps.h"
36 #include "mgr/MgrCap.h"
37 #include "osd/OSDCap.h"
39 #define dout_subsys ceph_subsys_mon
41 #define dout_prefix _prefix(_dout, mon, get_last_committed())
42 using namespace TOPNSPC::common
;
52 using std::ostringstream
;
57 using std::stringstream
;
60 using std::unique_ptr
;
62 using ceph::bufferlist
;
65 using ceph::Formatter
;
66 using ceph::JSONFormatter
;
67 using ceph::make_message
;
68 using ceph::mono_clock
;
69 using ceph::mono_time
;
70 using ceph::timespan_str
;
71 static ostream
& _prefix(std::ostream
*_dout
, Monitor
&mon
, version_t v
) {
72 return *_dout
<< "mon." << mon
.name
<< "@" << mon
.rank
73 << "(" << mon
.get_state_name()
74 << ").auth v" << v
<< " ";
77 ostream
& operator<<(ostream
&out
, const AuthMonitor
&pm
)
82 bool AuthMonitor::check_rotate()
84 KeyServerData::Incremental rot_inc
;
85 rot_inc
.op
= KeyServerData::AUTH_INC_SET_ROTATING
;
86 if (mon
.key_server
.prepare_rotating_update(rot_inc
.rotating_bl
)) {
87 dout(10) << __func__
<< " updating rotating" << dendl
;
88 push_cephx_inc(rot_inc
);
95 Tick function to update the map based on performance every N seconds
98 void AuthMonitor::tick()
100 if (!is_active()) return;
102 dout(10) << *this << dendl
;
104 // increase global_id?
105 bool propose
= false;
108 std::lock_guard
l(mon
.auth_lock
);
109 increase
= _should_increase_max_global_id();
112 if (mon
.is_leader()) {
113 increase_max_global_id();
116 dout(10) << __func__
<< "requesting more ids from leader" << dendl
;
117 int leader
= mon
.get_leader();
118 MMonGlobalID
*req
= new MMonGlobalID();
119 req
->old_max_id
= max_global_id
;
120 mon
.send_mon_message(req
, leader
);
124 if (!mon
.is_leader()) {
128 if (check_rotate()) {
137 void AuthMonitor::on_active()
139 dout(10) << "AuthMonitor::on_active()" << dendl
;
141 if (!mon
.is_leader())
144 mon
.key_server
.start_server();
146 if (is_writeable()) {
147 bool propose
= false;
148 if (check_rotate()) {
153 std::lock_guard
l(mon
.auth_lock
);
154 increase
= _should_increase_max_global_id();
157 increase_max_global_id();
166 bufferlist
_encode_cap(const string
& cap
)
173 void AuthMonitor::get_initial_keyring(KeyRing
*keyring
)
175 dout(10) << __func__
<< dendl
;
176 ceph_assert(keyring
!= nullptr);
179 int ret
= mon
.store
->get("mkfs", "keyring", bl
);
180 if (ret
== -ENOENT
) {
183 // fail hard only if there's an error we're not expecting to see
184 ceph_assert(ret
== 0);
186 auto p
= bl
.cbegin();
190 void _generate_bootstrap_keys(
191 list
<pair
<EntityName
,EntityAuth
> >* auth_lst
)
193 ceph_assert(auth_lst
!= nullptr);
195 map
<string
,map
<string
,bufferlist
> > bootstrap
= {
197 { "mon", _encode_cap("allow *") },
198 { "osd", _encode_cap("allow *") },
199 { "mds", _encode_cap("allow *") },
200 { "mgr", _encode_cap("allow *") }
203 { "mon", _encode_cap("allow profile bootstrap-osd") }
206 { "mon", _encode_cap("allow profile bootstrap-rgw") }
209 { "mon", _encode_cap("allow profile bootstrap-mds") }
212 { "mon", _encode_cap("allow profile bootstrap-mgr") }
215 { "mon", _encode_cap("allow profile bootstrap-rbd") }
217 { "bootstrap-rbd-mirror", {
218 { "mon", _encode_cap("allow profile bootstrap-rbd-mirror") }
222 for (auto &p
: bootstrap
) {
224 name
.from_str("client." + p
.first
);
226 auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
227 auth
.caps
= p
.second
;
229 auth_lst
->push_back(make_pair(name
, auth
));
233 void AuthMonitor::create_initial_keys(KeyRing
*keyring
)
235 dout(10) << __func__
<< " with keyring" << dendl
;
236 ceph_assert(keyring
!= nullptr);
238 list
<pair
<EntityName
,EntityAuth
> > auth_lst
;
239 _generate_bootstrap_keys(&auth_lst
);
241 for (auto &p
: auth_lst
) {
242 if (keyring
->exists(p
.first
)) {
245 keyring
->add(p
.first
, p
.second
);
249 void AuthMonitor::create_initial()
251 dout(10) << "create_initial -- creating initial map" << dendl
;
253 // initialize rotating keys
254 mon
.key_server
.clear_secrets();
256 ceph_assert(pending_auth
.size() == 1);
258 if (mon
.is_keyring_required()) {
260 // attempt to obtain an existing mkfs-time keyring
261 get_initial_keyring(&keyring
);
262 // create missing keys in the keyring
263 create_initial_keys(&keyring
);
264 // import the resulting keyring
265 import_keyring(keyring
);
268 max_global_id
= MIN_GLOBAL_ID
;
271 inc
.inc_type
= GLOBAL_ID
;
272 inc
.max_global_id
= max_global_id
;
273 pending_auth
.push_back(inc
);
278 void AuthMonitor::update_from_paxos(bool *need_bootstrap
)
280 dout(10) << __func__
<< dendl
;
283 version_t version
= get_last_committed();
284 version_t keys_ver
= mon
.key_server
.get_ver();
285 if (version
== keys_ver
)
287 ceph_assert(version
> keys_ver
);
289 version_t latest_full
= get_version_latest_full();
291 dout(10) << __func__
<< " version " << version
<< " keys ver " << keys_ver
292 << " latest " << latest_full
<< dendl
;
294 if ((latest_full
> 0) && (latest_full
> keys_ver
)) {
295 bufferlist latest_bl
;
296 int err
= get_version_full(latest_full
, latest_bl
);
297 ceph_assert(err
== 0);
298 ceph_assert(latest_bl
.length() != 0);
299 dout(7) << __func__
<< " loading summary e " << latest_full
<< dendl
;
300 dout(7) << __func__
<< " latest length " << latest_bl
.length() << dendl
;
301 auto p
= latest_bl
.cbegin();
304 decode(max_global_id
, p
);
305 decode(mon
.key_server
, p
);
306 mon
.key_server
.set_ver(latest_full
);
307 keys_ver
= latest_full
;
310 dout(10) << __func__
<< " key server version " << mon
.key_server
.get_ver() << dendl
;
312 // walk through incrementals
313 while (version
> keys_ver
) {
315 int ret
= get_version(keys_ver
+1, bl
);
316 ceph_assert(ret
== 0);
317 ceph_assert(bl
.length());
319 // reset if we are moving to initial state. we will normally have
320 // keys in here temporarily for bootstrapping that we need to
323 mon
.key_server
.clear_secrets();
325 dout(20) << __func__
<< " walking through version " << (keys_ver
+1)
326 << " len " << bl
.length() << dendl
;
328 auto p
= bl
.cbegin();
334 switch (inc
.inc_type
) {
336 max_global_id
= inc
.max_global_id
;
341 KeyServerData::Incremental auth_inc
;
342 auto iter
= inc
.auth_data
.cbegin();
343 decode(auth_inc
, iter
);
344 mon
.key_server
.apply_data_incremental(auth_inc
);
351 mon
.key_server
.set_ver(keys_ver
);
353 if (keys_ver
== 1 && mon
.is_keyring_required()) {
354 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
355 t
->erase("mkfs", "keyring");
356 mon
.store
->apply_transaction(t
);
361 std::lock_guard
l(mon
.auth_lock
);
362 if (last_allocated_id
== 0) {
363 last_allocated_id
= max_global_id
;
364 dout(10) << __func__
<< " last_allocated_id initialized to "
365 << max_global_id
<< dendl
;
369 dout(10) << __func__
<< " max_global_id=" << max_global_id
370 << " format_version " << format_version
373 mon
.key_server
.dump();
376 bool AuthMonitor::_should_increase_max_global_id()
378 ceph_assert(ceph_mutex_is_locked(mon
.auth_lock
));
379 auto num_prealloc
= g_conf()->mon_globalid_prealloc
;
380 if (max_global_id
< num_prealloc
||
381 (last_allocated_id
+ 1) >= max_global_id
- num_prealloc
/ 2) {
387 void AuthMonitor::increase_max_global_id()
389 ceph_assert(mon
.is_leader());
392 inc
.inc_type
= GLOBAL_ID
;
393 inc
.max_global_id
= max_global_id
+ g_conf()->mon_globalid_prealloc
;
394 dout(10) << "increasing max_global_id to " << inc
.max_global_id
<< dendl
;
395 pending_auth
.push_back(inc
);
398 bool AuthMonitor::should_propose(double& delay
)
400 return (!pending_auth
.empty());
403 void AuthMonitor::create_pending()
405 pending_auth
.clear();
406 dout(10) << "create_pending v " << (get_last_committed() + 1) << dendl
;
409 void AuthMonitor::encode_pending(MonitorDBStore::TransactionRef t
)
411 dout(10) << __func__
<< " v " << (get_last_committed() + 1) << dendl
;
417 vector
<Incremental
>::iterator p
;
418 for (p
= pending_auth
.begin(); p
!= pending_auth
.end(); ++p
)
419 p
->encode(bl
, mon
.get_quorum_con_features());
421 version_t version
= get_last_committed() + 1;
422 put_version(t
, version
, bl
);
423 put_last_committed(t
, version
);
426 health_check_map_t next
;
427 map
<string
,list
<string
>> bad_detail
; // entity -> details
428 for (auto i
= mon
.key_server
.secrets_begin();
429 i
!= mon
.key_server
.secrets_end();
431 for (auto& p
: i
->second
.caps
) {
433 if (!valid_caps(p
.first
, p
.second
, &ss
)) {
435 ss2
<< i
->first
<< " " << ss
.str();
436 bad_detail
[i
->first
.to_str()].push_back(ss2
.str());
440 for (auto& inc
: pending_auth
) {
441 if (inc
.inc_type
== AUTH_DATA
) {
442 KeyServerData::Incremental auth_inc
;
443 auto iter
= inc
.auth_data
.cbegin();
444 decode(auth_inc
, iter
);
445 if (auth_inc
.op
== KeyServerData::AUTH_INC_DEL
) {
446 bad_detail
.erase(auth_inc
.name
.to_str());
447 } else if (auth_inc
.op
== KeyServerData::AUTH_INC_ADD
) {
448 for (auto& p
: auth_inc
.auth
.caps
) {
450 if (!valid_caps(p
.first
, p
.second
, &ss
)) {
452 ss2
<< auth_inc
.name
<< " " << ss
.str();
453 bad_detail
[auth_inc
.name
.to_str()].push_back(ss2
.str());
459 if (bad_detail
.size()) {
461 ss
<< bad_detail
.size() << " auth entities have invalid capabilities";
462 health_check_t
*check
= &next
.add("AUTH_BAD_CAPS", HEALTH_ERR
, ss
.str(),
464 for (auto& i
: bad_detail
) {
465 for (auto& j
: i
.second
) {
466 check
->detail
.push_back(j
);
470 encode_health(next
, t
);
473 void AuthMonitor::encode_full(MonitorDBStore::TransactionRef t
)
475 version_t version
= mon
.key_server
.get_ver();
476 // do not stash full version 0 as it will never be removed nor read
480 dout(10) << __func__
<< " auth v " << version
<< dendl
;
481 ceph_assert(get_last_committed() == version
);
484 std::scoped_lock l
{mon
.key_server
.get_lock()};
485 dout(20) << __func__
<< " key server has "
486 << (mon
.key_server
.has_secrets() ? "" : "no ")
487 << "secrets!" << dendl
;
490 encode(max_global_id
, full_bl
);
491 encode(mon
.key_server
, full_bl
);
493 put_version_full(t
, version
, full_bl
);
494 put_version_latest_full(t
, version
);
497 version_t
AuthMonitor::get_trim_to() const
499 unsigned max
= g_conf()->paxos_max_join_drift
* 2;
500 version_t version
= get_last_committed();
501 if (mon
.is_leader() && (version
> max
))
502 return version
- max
;
506 bool AuthMonitor::preprocess_query(MonOpRequestRef op
)
508 auto m
= op
->get_req
<PaxosServiceMessage
>();
509 dout(10) << "preprocess_query " << *m
<< " from " << m
->get_orig_source_inst() << dendl
;
510 switch (m
->get_type()) {
511 case MSG_MON_COMMAND
:
513 return preprocess_command(op
);
514 } catch (const bad_cmd_get
& e
) {
516 mon
.reply_command(op
, -EINVAL
, e
.what(), bl
, get_last_committed());
521 return prep_auth(op
, false);
523 case MSG_MON_GLOBAL_ID
:
532 bool AuthMonitor::prepare_update(MonOpRequestRef op
)
534 auto m
= op
->get_req
<PaxosServiceMessage
>();
535 dout(10) << "prepare_update " << *m
<< " from " << m
->get_orig_source_inst() << dendl
;
536 switch (m
->get_type()) {
537 case MSG_MON_COMMAND
:
539 return prepare_command(op
);
540 } catch (const bad_cmd_get
& e
) {
542 mon
.reply_command(op
, -EINVAL
, e
.what(), bl
, get_last_committed());
545 case MSG_MON_GLOBAL_ID
:
546 return prepare_global_id(op
);
548 return prep_auth(op
, true);
555 void AuthMonitor::_set_mon_num_rank(int num
, int rank
)
557 dout(10) << __func__
<< " num " << num
<< " rank " << rank
<< dendl
;
558 ceph_assert(ceph_mutex_is_locked(mon
.auth_lock
));
563 uint64_t AuthMonitor::_assign_global_id()
565 ceph_assert(ceph_mutex_is_locked(mon
.auth_lock
));
566 if (mon_num
< 1 || mon_rank
< 0) {
567 dout(10) << __func__
<< " inactive (num_mon " << mon_num
568 << " rank " << mon_rank
<< ")" << dendl
;
571 if (!last_allocated_id
) {
572 dout(10) << __func__
<< " last_allocated_id == 0" << dendl
;
576 uint64_t id
= last_allocated_id
+ 1;
577 int remainder
= id
% mon_num
;
579 remainder
= mon_num
- remainder
;
581 id
+= remainder
+ mon_rank
;
583 if (id
>= max_global_id
) {
584 dout(10) << __func__
<< " failed (max " << max_global_id
<< ")" << dendl
;
588 last_allocated_id
= id
;
589 dout(10) << __func__
<< " " << id
<< " (max " << max_global_id
<< ")"
594 uint64_t AuthMonitor::assign_global_id(bool should_increase_max
)
598 std::lock_guard
l(mon
.auth_lock
);
599 id
=_assign_global_id();
600 if (should_increase_max
) {
601 should_increase_max
= _should_increase_max_global_id();
604 if (mon
.is_leader() &&
605 should_increase_max
) {
606 increase_max_global_id();
611 bool AuthMonitor::prep_auth(MonOpRequestRef op
, bool paxos_writable
)
613 auto m
= op
->get_req
<MAuth
>();
614 dout(10) << "prep_auth() blob_size=" << m
->get_auth_payload().length() << dendl
;
616 MonSession
*s
= op
->get_session();
618 dout(10) << "no session, dropping" << dendl
;
624 bufferlist response_bl
;
625 auto indata
= m
->auth_payload
.cbegin();
626 __u32 proto
= m
->protocol
;
628 bool finished
= false;
629 EntityName entity_name
;
630 bool is_new_global_id
= false;
633 if (m
->protocol
== 0 && !s
->auth_handler
) {
634 set
<__u32
> supported
;
638 decode(struct_v
, indata
);
639 decode(supported
, indata
);
640 decode(entity_name
, indata
);
641 decode(s
->con
->peer_global_id
, indata
);
642 } catch (const ceph::buffer::error
&e
) {
643 dout(10) << "failed to decode initial auth message" << dendl
;
648 // do we require cephx signatures?
650 if (!m
->get_connection()->has_feature(CEPH_FEATURE_MSG_AUTH
)) {
651 if (entity_name
.get_type() == CEPH_ENTITY_TYPE_MON
||
652 entity_name
.get_type() == CEPH_ENTITY_TYPE_OSD
||
653 entity_name
.get_type() == CEPH_ENTITY_TYPE_MDS
||
654 entity_name
.get_type() == CEPH_ENTITY_TYPE_MGR
) {
655 if (g_conf()->cephx_cluster_require_signatures
||
656 g_conf()->cephx_require_signatures
) {
657 dout(1) << m
->get_source_inst()
658 << " supports cephx but not signatures and"
659 << " 'cephx [cluster] require signatures = true';"
660 << " disallowing cephx" << dendl
;
661 supported
.erase(CEPH_AUTH_CEPHX
);
664 if (g_conf()->cephx_service_require_signatures
||
665 g_conf()->cephx_require_signatures
) {
666 dout(1) << m
->get_source_inst()
667 << " supports cephx but not signatures and"
668 << " 'cephx [service] require signatures = true';"
669 << " disallowing cephx" << dendl
;
670 supported
.erase(CEPH_AUTH_CEPHX
);
673 } else if (!m
->get_connection()->has_feature(CEPH_FEATURE_CEPHX_V2
)) {
674 if (entity_name
.get_type() == CEPH_ENTITY_TYPE_MON
||
675 entity_name
.get_type() == CEPH_ENTITY_TYPE_OSD
||
676 entity_name
.get_type() == CEPH_ENTITY_TYPE_MDS
||
677 entity_name
.get_type() == CEPH_ENTITY_TYPE_MGR
) {
678 if (g_conf()->cephx_cluster_require_version
>= 2 ||
679 g_conf()->cephx_require_version
>= 2) {
680 dout(1) << m
->get_source_inst()
681 << " supports cephx but not v2 and"
682 << " 'cephx [cluster] require version >= 2';"
683 << " disallowing cephx" << dendl
;
684 supported
.erase(CEPH_AUTH_CEPHX
);
687 if (g_conf()->cephx_service_require_version
>= 2 ||
688 g_conf()->cephx_require_version
>= 2) {
689 dout(1) << m
->get_source_inst()
690 << " supports cephx but not v2 and"
691 << " 'cephx [service] require version >= 2';"
692 << " disallowing cephx" << dendl
;
693 supported
.erase(CEPH_AUTH_CEPHX
);
699 if (entity_name
.get_type() == CEPH_ENTITY_TYPE_MON
||
700 entity_name
.get_type() == CEPH_ENTITY_TYPE_OSD
||
701 entity_name
.get_type() == CEPH_ENTITY_TYPE_MDS
||
702 entity_name
.get_type() == CEPH_ENTITY_TYPE_MGR
)
703 type
= mon
.auth_cluster_required
.pick(supported
);
705 type
= mon
.auth_service_required
.pick(supported
);
707 s
->auth_handler
= get_auth_service_handler(type
, g_ceph_context
, &mon
.key_server
);
708 if (!s
->auth_handler
) {
709 dout(1) << "client did not provide supported auth type" << dendl
;
715 } else if (!s
->auth_handler
) {
716 dout(10) << "protocol specified but no s->auth_handler" << dendl
;
721 /* assign a new global_id? we assume this should only happen on the first
722 request. If a client tries to send it later, it'll screw up its auth
724 if (!s
->con
->peer_global_id
) {
725 s
->con
->peer_global_id
= assign_global_id(paxos_writable
);
726 if (!s
->con
->peer_global_id
) {
728 delete s
->auth_handler
;
729 s
->auth_handler
= NULL
;
731 if (mon
.is_leader() && paxos_writable
) {
732 dout(10) << "increasing global id, waitlisting message" << dendl
;
733 wait_for_active(op
, new C_RetryMessage(this, op
));
737 if (!mon
.is_leader()) {
738 dout(10) << "not the leader, requesting more ids from leader" << dendl
;
739 int leader
= mon
.get_leader();
740 MMonGlobalID
*req
= new MMonGlobalID();
741 req
->old_max_id
= max_global_id
;
742 mon
.send_mon_message(req
, leader
);
743 wait_for_finished_proposal(op
, new C_RetryMessage(this, op
));
747 ceph_assert(!paxos_writable
);
750 is_new_global_id
= true;
756 ret
= s
->auth_handler
->start_session(entity_name
,
757 s
->con
->peer_global_id
,
760 &s
->con
->peer_caps_info
);
763 ret
= s
->auth_handler
->handle_request(
765 0, // no connection_secret needed
767 &s
->con
->peer_caps_info
,
771 wait_for_active(op
, new C_RetryMessage(this,op
));
775 if (!s
->authenticated
&&
776 mon
.ms_handle_authentication(s
->con
.get()) > 0) {
781 } catch (const ceph::buffer::error
&err
) {
783 dout(0) << "caught error when trying to handle auth request, probably malformed request" << dendl
;
787 reply
= new MAuthReply(proto
, &response_bl
, ret
, s
->con
->peer_global_id
);
788 mon
.send_reply(op
, reply
);
790 // always send the latest monmap.
791 if (m
->monmap_epoch
< mon
.monmap
->get_epoch())
792 mon
.send_latest_monmap(m
->get_connection().get());
794 mon
.configmon()->check_sub(s
);
800 bool AuthMonitor::preprocess_command(MonOpRequestRef op
)
802 auto m
= op
->get_req
<MMonCommand
>();
808 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
809 // ss has reason for failure
810 string rs
= ss
.str();
811 mon
.reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
816 cmd_getval(cmdmap
, "prefix", prefix
);
817 if (prefix
== "auth add" ||
818 prefix
== "auth del" ||
819 prefix
== "auth rm" ||
820 prefix
== "auth get-or-create" ||
821 prefix
== "auth get-or-create-key" ||
822 prefix
== "fs authorize" ||
823 prefix
== "auth import" ||
824 prefix
== "auth caps") {
828 MonSession
*session
= op
->get_session();
830 mon
.reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
834 // entity might not be supplied, but if it is, it should be valid
836 cmd_getval(cmdmap
, "entity", entity_name
);
838 if (!entity_name
.empty() && !entity
.from_str(entity_name
)) {
839 ss
<< "invalid entity_auth " << entity_name
;
840 mon
.reply_command(op
, -EINVAL
, ss
.str(), get_last_committed());
845 cmd_getval(cmdmap
, "format", format
, string("plain"));
846 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
848 if (prefix
== "auth export") {
850 export_keyring(keyring
);
851 if (!entity_name
.empty()) {
853 if (keyring
.get_auth(entity
, eauth
)) {
855 kr
.add(entity
, eauth
);
857 kr
.encode_formatted("auth", f
.get(), rdata
);
859 kr
.encode_plaintext(rdata
);
860 ss
<< "export " << eauth
;
863 ss
<< "no key for " << eauth
;
868 keyring
.encode_formatted("auth", f
.get(), rdata
);
870 keyring
.encode_plaintext(rdata
);
872 ss
<< "exported master keyring";
875 } else if (prefix
== "auth get" && !entity_name
.empty()) {
877 EntityAuth entity_auth
;
878 if(!mon
.key_server
.get_auth(entity
, entity_auth
)) {
879 ss
<< "failed to find " << entity_name
<< " in keyring";
882 keyring
.add(entity
, entity_auth
);
884 keyring
.encode_formatted("auth", f
.get(), rdata
);
886 keyring
.encode_plaintext(rdata
);
887 ss
<< "exported keyring for " << entity_name
;
890 } else if (prefix
== "auth print-key" ||
891 prefix
== "auth print_key" ||
892 prefix
== "auth get-key") {
894 if (!mon
.key_server
.get_auth(entity
, auth
)) {
895 ss
<< "don't have " << entity
;
900 auth
.key
.encode_formatted("auth", f
.get(), rdata
);
902 auth
.key
.encode_plaintext(rdata
);
905 } else if (prefix
== "auth list" ||
906 prefix
== "auth ls") {
908 mon
.key_server
.encode_formatted("auth", f
.get(), rdata
);
910 mon
.key_server
.encode_plaintext(rdata
);
911 if (rdata
.length() > 0)
912 ss
<< "installed auth entries:" << std::endl
;
914 ss
<< "no installed auth entries!" << std::endl
;
919 ss
<< "invalid command";
926 getline(ss
, rs
, '\0');
927 mon
.reply_command(op
, r
, rs
, rdata
, get_last_committed());
931 void AuthMonitor::export_keyring(KeyRing
& keyring
)
933 mon
.key_server
.export_keyring(keyring
);
936 int AuthMonitor::import_keyring(KeyRing
& keyring
)
938 dout(10) << __func__
<< " " << keyring
.size() << " keys" << dendl
;
940 for (map
<EntityName
, EntityAuth
>::iterator p
= keyring
.get_keys().begin();
941 p
!= keyring
.get_keys().end();
943 if (p
->second
.caps
.empty()) {
944 dout(0) << "import: no caps supplied" << dendl
;
947 int err
= add_entity(p
->first
, p
->second
);
948 ceph_assert(err
== 0);
953 int AuthMonitor::remove_entity(const EntityName
&entity
)
955 dout(10) << __func__
<< " " << entity
<< dendl
;
956 if (!mon
.key_server
.contains(entity
))
959 KeyServerData::Incremental auth_inc
;
960 auth_inc
.name
= entity
;
961 auth_inc
.op
= KeyServerData::AUTH_INC_DEL
;
962 push_cephx_inc(auth_inc
);
967 bool AuthMonitor::entity_is_pending(EntityName
& entity
)
969 // are we about to have it?
970 for (auto& p
: pending_auth
) {
971 if (p
.inc_type
== AUTH_DATA
) {
972 KeyServerData::Incremental inc
;
973 auto q
= p
.auth_data
.cbegin();
975 if (inc
.op
== KeyServerData::AUTH_INC_ADD
&&
976 inc
.name
== entity
) {
984 int AuthMonitor::exists_and_matches_entity(
985 const auth_entity_t
& entity
,
989 return exists_and_matches_entity(entity
.name
, entity
.auth
,
990 entity
.auth
.caps
, has_secret
, ss
);
993 int AuthMonitor::exists_and_matches_entity(
994 const EntityName
& name
,
995 const EntityAuth
& auth
,
996 const map
<string
,bufferlist
>& caps
,
1001 dout(20) << __func__
<< " entity " << name
<< " auth " << auth
1002 << " caps " << caps
<< " has_secret " << has_secret
<< dendl
;
1004 EntityAuth existing_auth
;
1005 // does entry already exist?
1006 if (mon
.key_server
.get_auth(name
, existing_auth
)) {
1009 if (existing_auth
.key
.get_secret().cmp(auth
.key
.get_secret())) {
1010 ss
<< "entity " << name
<< " exists but key does not match";
1016 if (caps
.size() != existing_auth
.caps
.size()) {
1017 ss
<< "entity " << name
<< " exists but caps do not match";
1020 for (auto& it
: caps
) {
1021 if (existing_auth
.caps
.count(it
.first
) == 0 ||
1022 !existing_auth
.caps
[it
.first
].contents_equal(it
.second
)) {
1023 ss
<< "entity " << name
<< " exists but cap "
1024 << it
.first
<< " does not match";
1029 // they match, no-op
1035 int AuthMonitor::add_entity(
1036 const EntityName
& name
,
1037 const EntityAuth
& auth
)
1041 KeyServerData::Incremental auth_inc
;
1042 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1043 auth_inc
.name
= name
;
1044 auth_inc
.auth
= auth
;
1046 dout(10) << " add auth entity " << auth_inc
.name
<< dendl
;
1047 dout(30) << " " << auth_inc
.auth
<< dendl
;
1048 push_cephx_inc(auth_inc
);
1052 int AuthMonitor::validate_osd_destroy(
1055 EntityName
& cephx_entity
,
1056 EntityName
& lockbox_entity
,
1059 ceph_assert(paxos
.is_plugged());
1061 dout(10) << __func__
<< " id " << id
<< " uuid " << uuid
<< dendl
;
1063 string cephx_str
= "osd." + stringify(id
);
1064 string lockbox_str
= "client.osd-lockbox." + stringify(uuid
);
1066 if (!cephx_entity
.from_str(cephx_str
)) {
1067 dout(10) << __func__
<< " invalid cephx entity '"
1068 << cephx_str
<< "'" << dendl
;
1069 ss
<< "invalid cephx key entity '" << cephx_str
<< "'";
1073 if (!lockbox_entity
.from_str(lockbox_str
)) {
1074 dout(10) << __func__
<< " invalid lockbox entity '"
1075 << lockbox_str
<< "'" << dendl
;
1076 ss
<< "invalid lockbox key entity '" << lockbox_str
<< "'";
1080 if (!mon
.key_server
.contains(cephx_entity
) &&
1081 !mon
.key_server
.contains(lockbox_entity
)) {
1088 int AuthMonitor::do_osd_destroy(
1089 const EntityName
& cephx_entity
,
1090 const EntityName
& lockbox_entity
)
1092 ceph_assert(paxos
.is_plugged());
1094 dout(10) << __func__
<< " cephx " << cephx_entity
1095 << " lockbox " << lockbox_entity
<< dendl
;
1097 bool removed
= false;
1099 int err
= remove_entity(cephx_entity
);
1100 if (err
== -ENOENT
) {
1101 dout(10) << __func__
<< " " << cephx_entity
<< " does not exist" << dendl
;
1106 err
= remove_entity(lockbox_entity
);
1107 if (err
== -ENOENT
) {
1108 dout(10) << __func__
<< " " << lockbox_entity
<< " does not exist" << dendl
;
1114 dout(10) << __func__
<< " entities do not exist -- no-op." << dendl
;
1118 // given we have paxos plugged, this will not result in a proposal
1119 // being triggered, but it will still be needed so that we get our
1120 // pending state encoded into the paxos' pending transaction.
1128 const map
<string
,bufferlist
>& caps
)
1133 auth
.key
.decode_base64(key
);
1134 } catch (ceph::buffer::error
& e
) {
1141 int AuthMonitor::validate_osd_new(
1144 const string
& cephx_secret
,
1145 const string
& lockbox_secret
,
1146 auth_entity_t
& cephx_entity
,
1147 auth_entity_t
& lockbox_entity
,
1151 dout(10) << __func__
<< " osd." << id
<< " uuid " << uuid
<< dendl
;
1153 map
<string
,bufferlist
> cephx_caps
= {
1154 { "osd", _encode_cap("allow *") },
1155 { "mon", _encode_cap("allow profile osd") },
1156 { "mgr", _encode_cap("allow profile osd") }
1158 map
<string
,bufferlist
> lockbox_caps
= {
1159 { "mon", _encode_cap("allow command \"config-key get\" "
1160 "with key=\"dm-crypt/osd/" +
1165 bool has_lockbox
= !lockbox_secret
.empty();
1167 string cephx_name
= "osd." + stringify(id
);
1168 string lockbox_name
= "client.osd-lockbox." + stringify(uuid
);
1170 if (!cephx_entity
.name
.from_str(cephx_name
)) {
1171 dout(10) << __func__
<< " invalid cephx entity '"
1172 << cephx_name
<< "'" << dendl
;
1173 ss
<< "invalid cephx key entity '" << cephx_name
<< "'";
1178 if (!lockbox_entity
.name
.from_str(lockbox_name
)) {
1179 dout(10) << __func__
<< " invalid cephx lockbox entity '"
1180 << lockbox_name
<< "'" << dendl
;
1181 ss
<< "invalid cephx lockbox entity '" << lockbox_name
<< "'";
1186 if (entity_is_pending(cephx_entity
.name
) ||
1187 (has_lockbox
&& entity_is_pending(lockbox_entity
.name
))) {
1188 // If we have pending entities for either the cephx secret or the
1189 // lockbox secret, then our safest bet is to retry the command at
1190 // a later time. These entities may be pending because an `osd new`
1191 // command has been run (which is unlikely, due to the nature of
1192 // the operation, which will force a paxos proposal), or (more likely)
1193 // because a competing client created those entities before we handled
1194 // the `osd new` command. Regardless, let's wait and see.
1198 if (!is_valid_cephx_key(cephx_secret
)) {
1199 ss
<< "invalid cephx secret.";
1203 if (has_lockbox
&& !is_valid_cephx_key(lockbox_secret
)) {
1204 ss
<< "invalid cephx lockbox secret.";
1208 int err
= _create_auth(cephx_entity
.auth
, cephx_secret
, cephx_caps
);
1209 ceph_assert(0 == err
);
1211 bool cephx_is_idempotent
= false, lockbox_is_idempotent
= false;
1212 err
= exists_and_matches_entity(cephx_entity
, true, ss
);
1214 if (err
!= -ENOENT
) {
1218 ceph_assert(0 == err
);
1219 cephx_is_idempotent
= true;
1223 err
= _create_auth(lockbox_entity
.auth
, lockbox_secret
, lockbox_caps
);
1224 ceph_assert(err
== 0);
1225 err
= exists_and_matches_entity(lockbox_entity
, true, ss
);
1226 if (err
!= -ENOENT
) {
1230 ceph_assert(0 == err
);
1231 lockbox_is_idempotent
= true;
1235 if (cephx_is_idempotent
&& (!has_lockbox
|| lockbox_is_idempotent
)) {
1242 int AuthMonitor::do_osd_new(
1243 const auth_entity_t
& cephx_entity
,
1244 const auth_entity_t
& lockbox_entity
,
1247 ceph_assert(paxos
.is_plugged());
1249 dout(10) << __func__
<< " cephx " << cephx_entity
.name
1252 *_dout
<< lockbox_entity
.name
;
1258 // we must have validated before reaching this point.
1259 // if keys exist, then this means they also match; otherwise we would
1260 // have failed before calling this function.
1261 bool cephx_exists
= mon
.key_server
.contains(cephx_entity
.name
);
1263 if (!cephx_exists
) {
1264 int err
= add_entity(cephx_entity
.name
, cephx_entity
.auth
);
1265 ceph_assert(0 == err
);
1269 !mon
.key_server
.contains(lockbox_entity
.name
)) {
1270 int err
= add_entity(lockbox_entity
.name
, lockbox_entity
.auth
);
1271 ceph_assert(0 == err
);
1274 // given we have paxos plugged, this will not result in a proposal
1275 // being triggered, but it will still be needed so that we get our
1276 // pending state encoded into the paxos' pending transaction.
1281 bool AuthMonitor::valid_caps(
1286 if (type
== "mon") {
1288 if (!moncap
.parse(caps
, out
)) {
1294 if (!g_conf().get_val
<bool>("mon_auth_validate_all_caps")) {
1298 if (type
== "mgr") {
1300 if (!mgrcap
.parse(caps
, out
)) {
1303 } else if (type
== "osd") {
1305 if (!ocap
.parse(caps
, out
)) {
1308 } else if (type
== "mds") {
1310 if (!mdscap
.parse(g_ceph_context
, caps
, out
)) {
1315 *out
<< "unknown cap type '" << type
<< "'";
1322 bool AuthMonitor::valid_caps(const vector
<string
>& caps
, ostream
*out
)
1324 for (vector
<string
>::const_iterator p
= caps
.begin();
1325 p
!= caps
.end(); p
+= 2) {
1326 if ((p
+1) == caps
.end()) {
1327 *out
<< "cap '" << *p
<< "' has no value";
1330 if (!valid_caps(*p
, *(p
+1), out
)) {
1337 bool AuthMonitor::prepare_command(MonOpRequestRef op
)
1339 auto m
= op
->get_req
<MMonCommand
>();
1340 stringstream ss
, ds
;
1346 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
1347 // ss has reason for failure
1348 string rs
= ss
.str();
1349 mon
.reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
1354 vector
<string
>caps_vec
;
1358 cmd_getval(cmdmap
, "prefix", prefix
);
1361 cmd_getval(cmdmap
, "format", format
, string("plain"));
1362 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
1364 MonSession
*session
= op
->get_session();
1366 mon
.reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
1370 cmd_getval(cmdmap
, "caps", caps_vec
);
1371 // fs authorize command's can have odd number of caps arguments
1372 if ((prefix
!= "fs authorize") && (caps_vec
.size() % 2) != 0) {
1373 ss
<< "bad capabilities request; odd number of arguments";
1378 cmd_getval(cmdmap
, "entity", entity_name
);
1379 if (!entity_name
.empty() && !entity
.from_str(entity_name
)) {
1380 ss
<< "bad entity name";
1385 if (prefix
== "auth import") {
1386 bufferlist bl
= m
->get_data();
1387 if (bl
.length() == 0) {
1388 ss
<< "auth import: no data supplied";
1390 mon
.reply_command(op
, -EINVAL
, rs
, get_last_committed());
1393 auto iter
= bl
.cbegin();
1396 decode(keyring
, iter
);
1397 } catch (const ceph::buffer::error
&ex
) {
1398 ss
<< "error decoding keyring" << " " << ex
.what();
1402 err
= import_keyring(keyring
);
1404 ss
<< "auth import: no caps supplied";
1406 mon
.reply_command(op
, -EINVAL
, rs
, get_last_committed());
1409 ss
<< "imported keyring";
1412 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1413 get_last_committed() + 1));
1415 } else if (prefix
== "auth add" && !entity_name
.empty()) {
1416 /* expected behavior:
1417 * - if command reproduces current state, return 0.
1418 * - if command adds brand new entity, handle it.
1419 * - if command adds new state to existing entity, return error.
1421 KeyServerData::Incremental auth_inc
;
1422 auth_inc
.name
= entity
;
1423 bufferlist bl
= m
->get_data();
1424 bool has_keyring
= (bl
.length() > 0);
1425 map
<string
,bufferlist
> new_caps
;
1427 KeyRing new_keyring
;
1429 auto iter
= bl
.cbegin();
1431 decode(new_keyring
, iter
);
1432 } catch (const ceph::buffer::error
&ex
) {
1433 ss
<< "error decoding keyring";
1439 if (!valid_caps(caps_vec
, &ss
)) {
1444 // are we about to have it?
1445 if (entity_is_pending(entity
)) {
1446 wait_for_finished_proposal(op
,
1447 new Monitor::C_Command(mon
, op
, 0, rs
, get_last_committed() + 1));
1451 // build new caps from provided arguments (if available)
1452 for (vector
<string
>::iterator it
= caps_vec
.begin();
1453 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1457 encode(*(it
+1), cap
);
1458 new_caps
[sys
] = cap
;
1461 // pull info out of provided keyring
1464 if (!new_keyring
.get_auth(auth_inc
.name
, new_inc
)) {
1465 ss
<< "key for " << auth_inc
.name
1466 << " not found in provided keyring";
1470 if (!new_caps
.empty() && !new_inc
.caps
.empty()) {
1471 ss
<< "caps cannot be specified both in keyring and in command";
1475 if (new_caps
.empty()) {
1476 new_caps
= new_inc
.caps
;
1480 err
= exists_and_matches_entity(auth_inc
.name
, new_inc
,
1481 new_caps
, has_keyring
, ss
);
1482 // if entity/key/caps do not exist in the keyring, just fall through
1483 // and add the entity; otherwise, make sure everything matches (in
1484 // which case it's a no-op), because if not we must fail.
1485 if (err
!= -ENOENT
) {
1490 ceph_assert(err
== 0);
1497 dout(10) << "AuthMonitor::prepare_command generating random key for "
1498 << auth_inc
.name
<< dendl
;
1499 new_inc
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1501 new_inc
.caps
= new_caps
;
1503 err
= add_entity(auth_inc
.name
, new_inc
);
1504 ceph_assert(err
== 0);
1506 ss
<< "added key for " << auth_inc
.name
;
1508 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1509 get_last_committed() + 1));
1511 } else if ((prefix
== "auth get-or-create-key" ||
1512 prefix
== "auth get-or-create") &&
1513 !entity_name
.empty()) {
1514 // auth get-or-create <name> [mon osdcapa osd osdcapb ...]
1516 if (!valid_caps(caps_vec
, &ss
)) {
1521 // Parse the list of caps into a map
1522 std::map
<std::string
, bufferlist
> wanted_caps
;
1523 for (vector
<string
>::const_iterator it
= caps_vec
.begin();
1524 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1526 const std::string
&sys
= *it
;
1528 encode(*(it
+1), cap
);
1529 wanted_caps
[sys
] = cap
;
1533 EntityAuth entity_auth
;
1534 if (mon
.key_server
.get_auth(entity
, entity_auth
)) {
1535 for (const auto &sys_cap
: wanted_caps
) {
1536 if (entity_auth
.caps
.count(sys_cap
.first
) == 0 ||
1537 !entity_auth
.caps
[sys_cap
.first
].contents_equal(sys_cap
.second
)) {
1538 ss
<< "key for " << entity
<< " exists but cap " << sys_cap
.first
1539 << " does not match";
1545 if (prefix
== "auth get-or-create-key") {
1547 entity_auth
.key
.encode_formatted("auth", f
.get(), rdata
);
1549 ds
<< entity_auth
.key
;
1553 kr
.add(entity
, entity_auth
.key
);
1555 kr
.set_caps(entity
, entity_auth
.caps
);
1556 kr
.encode_formatted("auth", f
.get(), rdata
);
1558 kr
.encode_plaintext(rdata
);
1565 // ...or are we about to?
1566 for (vector
<Incremental
>::iterator p
= pending_auth
.begin();
1567 p
!= pending_auth
.end();
1569 if (p
->inc_type
== AUTH_DATA
) {
1570 KeyServerData::Incremental auth_inc
;
1571 auto q
= p
->auth_data
.cbegin();
1572 decode(auth_inc
, q
);
1573 if (auth_inc
.op
== KeyServerData::AUTH_INC_ADD
&&
1574 auth_inc
.name
== entity
) {
1575 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1576 get_last_committed() + 1));
1583 KeyServerData::Incremental auth_inc
;
1584 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1585 auth_inc
.name
= entity
;
1586 auth_inc
.auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1587 auth_inc
.auth
.caps
= wanted_caps
;
1589 push_cephx_inc(auth_inc
);
1591 if (prefix
== "auth get-or-create-key") {
1593 auth_inc
.auth
.key
.encode_formatted("auth", f
.get(), rdata
);
1595 ds
<< auth_inc
.auth
.key
;
1599 kr
.add(entity
, auth_inc
.auth
.key
);
1601 kr
.set_caps(entity
, wanted_caps
);
1602 kr
.encode_formatted("auth", f
.get(), rdata
);
1604 kr
.encode_plaintext(rdata
);
1610 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
, rdata
,
1611 get_last_committed() + 1));
1613 } else if (prefix
== "fs authorize") {
1615 cmd_getval(cmdmap
, "filesystem", filesystem
);
1616 string mon_cap_string
= "allow r";
1617 string mds_cap_string
, osd_cap_string
;
1618 string osd_cap_wanted
= "r";
1620 std::shared_ptr
<const Filesystem
> fs
;
1621 if (filesystem
!= "*" && filesystem
!= "all") {
1622 fs
= mon
.mdsmon()->get_fsmap().get_filesystem(filesystem
);
1623 if (fs
== nullptr) {
1624 ss
<< "filesystem " << filesystem
<< " does not exist.";
1628 mon_cap_string
+= " fsname=" + std::string(fs
->mds_map
.get_fs_name());
1632 for (auto it
= caps_vec
.begin();
1633 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1635 const string
&path
= *it
;
1636 const string
&cap
= *(it
+1);
1637 bool root_squash
= false;
1638 if ((it
+ 2) != caps_vec
.end() && *(it
+2) == "root_squash") {
1643 if (cap
!= "r" && cap
.compare(0, 2, "rw")) {
1644 ss
<< "Permission flags must start with 'r' or 'rw'.";
1648 if (cap
.compare(0, 2, "rw") == 0)
1649 osd_cap_wanted
= "rw";
1652 for (size_t i
= 2; i
< cap
.size(); ++i
) {
1655 ss
<< "Permission flags (except 'rw') must be specified in alphabetical order.";
1665 ss
<< "Unknown permission flag '" << c
<< "'.";
1671 mds_cap_string
+= mds_cap_string
.empty() ? "" : ", ";
1672 mds_cap_string
+= "allow " + cap
;
1674 if (filesystem
!= "*" && filesystem
!= "all" && fs
!= nullptr) {
1675 mds_cap_string
+= " fsname=" + std::string(fs
->mds_map
.get_fs_name());
1679 mds_cap_string
+= " path=" + path
;
1683 mds_cap_string
+= " root_squash";
1687 osd_cap_string
+= osd_cap_string
.empty() ? "" : ", ";
1688 osd_cap_string
+= "allow " + osd_cap_wanted
1689 + " tag " + pg_pool_t::APPLICATION_NAME_CEPHFS
1690 + " data=" + filesystem
;
1692 std::map
<string
, bufferlist
> wanted_caps
= {
1693 { "mon", _encode_cap(mon_cap_string
) },
1694 { "osd", _encode_cap(osd_cap_string
) },
1695 { "mds", _encode_cap(mds_cap_string
) }
1698 if (!valid_caps("mon", mon_cap_string
, &ss
) ||
1699 !valid_caps("osd", osd_cap_string
, &ss
) ||
1700 !valid_caps("mds", mds_cap_string
, &ss
)) {
1705 EntityAuth entity_auth
;
1706 if (mon
.key_server
.get_auth(entity
, entity_auth
)) {
1707 for (const auto &sys_cap
: wanted_caps
) {
1708 if (entity_auth
.caps
.count(sys_cap
.first
) == 0 ||
1709 !entity_auth
.caps
[sys_cap
.first
].contents_equal(sys_cap
.second
)) {
1710 ss
<< entity
<< " already has fs capabilities that differ from "
1711 << "those supplied. To generate a new auth key for " << entity
1712 << ", first remove " << entity
<< " from configuration files, "
1713 << "execute 'ceph auth rm " << entity
<< "', then execute this "
1714 << "command again.";
1721 kr
.add(entity
, entity_auth
.key
);
1723 kr
.set_caps(entity
, entity_auth
.caps
);
1724 kr
.encode_formatted("auth", f
.get(), rdata
);
1726 kr
.encode_plaintext(rdata
);
1732 KeyServerData::Incremental auth_inc
;
1733 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1734 auth_inc
.name
= entity
;
1735 auth_inc
.auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1736 auth_inc
.auth
.caps
= wanted_caps
;
1738 push_cephx_inc(auth_inc
);
1740 kr
.add(entity
, auth_inc
.auth
.key
);
1742 kr
.set_caps(entity
, wanted_caps
);
1743 kr
.encode_formatted("auth", f
.get(), rdata
);
1745 kr
.encode_plaintext(rdata
);
1750 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
, rdata
,
1751 get_last_committed() + 1));
1753 } else if (prefix
== "auth caps" && !entity_name
.empty()) {
1754 KeyServerData::Incremental auth_inc
;
1755 auth_inc
.name
= entity
;
1756 if (!mon
.key_server
.get_auth(auth_inc
.name
, auth_inc
.auth
)) {
1757 ss
<< "couldn't find entry " << auth_inc
.name
;
1762 if (!valid_caps(caps_vec
, &ss
)) {
1767 map
<string
,bufferlist
> newcaps
;
1768 for (vector
<string
>::iterator it
= caps_vec
.begin();
1769 it
!= caps_vec
.end(); it
+= 2)
1770 encode(*(it
+1), newcaps
[*it
]);
1772 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1773 auth_inc
.auth
.caps
= newcaps
;
1774 push_cephx_inc(auth_inc
);
1776 ss
<< "updated caps for " << auth_inc
.name
;
1778 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1779 get_last_committed() + 1));
1781 } else if ((prefix
== "auth del" || prefix
== "auth rm") &&
1782 !entity_name
.empty()) {
1783 KeyServerData::Incremental auth_inc
;
1784 auth_inc
.name
= entity
;
1785 if (!mon
.key_server
.contains(auth_inc
.name
)) {
1786 ss
<< "entity " << entity
<< " does not exist";
1790 auth_inc
.op
= KeyServerData::AUTH_INC_DEL
;
1791 push_cephx_inc(auth_inc
);
1795 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1796 get_last_committed() + 1));
1801 getline(ss
, rs
, '\0');
1802 mon
.reply_command(op
, err
, rs
, rdata
, get_last_committed());
1806 bool AuthMonitor::prepare_global_id(MonOpRequestRef op
)
1808 dout(10) << "AuthMonitor::prepare_global_id" << dendl
;
1809 increase_max_global_id();
1814 bool AuthMonitor::_upgrade_format_to_dumpling()
1816 dout(1) << __func__
<< " upgrading from format 0 to 1" << dendl
;
1817 ceph_assert(format_version
== 0);
1819 bool changed
= false;
1820 map
<EntityName
, EntityAuth
>::iterator p
;
1821 for (p
= mon
.key_server
.secrets_begin();
1822 p
!= mon
.key_server
.secrets_end();
1824 // grab mon caps, if any
1826 if (p
->second
.caps
.count("mon") == 0)
1829 auto it
= p
->second
.caps
["mon"].cbegin();
1830 decode(mon_caps
, it
);
1832 catch (const ceph::buffer::error
&) {
1833 dout(10) << __func__
<< " unable to parse mon cap for "
1834 << p
->first
<< dendl
;
1838 string n
= p
->first
.to_str();
1841 // set daemon profiles
1842 if ((p
->first
.is_osd() || p
->first
.is_mds()) &&
1843 mon_caps
== "allow rwx") {
1844 new_caps
= string("allow profile ") + std::string(p
->first
.get_type_name());
1847 // update bootstrap keys
1848 if (n
== "client.bootstrap-osd") {
1849 new_caps
= "allow profile bootstrap-osd";
1851 if (n
== "client.bootstrap-mds") {
1852 new_caps
= "allow profile bootstrap-mds";
1855 if (new_caps
.length() > 0) {
1856 dout(5) << __func__
<< " updating " << p
->first
<< " mon cap from "
1857 << mon_caps
<< " to " << new_caps
<< dendl
;
1860 encode(new_caps
, bl
);
1862 KeyServerData::Incremental auth_inc
;
1863 auth_inc
.name
= p
->first
;
1864 auth_inc
.auth
= p
->second
;
1865 auth_inc
.auth
.caps
["mon"] = bl
;
1866 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1867 push_cephx_inc(auth_inc
);
1874 bool AuthMonitor::_upgrade_format_to_luminous()
1876 dout(1) << __func__
<< " upgrading from format 1 to 2" << dendl
;
1877 ceph_assert(format_version
== 1);
1879 bool changed
= false;
1880 map
<EntityName
, EntityAuth
>::iterator p
;
1881 for (p
= mon
.key_server
.secrets_begin();
1882 p
!= mon
.key_server
.secrets_end();
1884 string n
= p
->first
.to_str();
1887 if (n
== "client.admin") {
1888 // admin gets it all
1890 } else if (n
.find("osd.") == 0 ||
1891 n
.find("mds.") == 0 ||
1892 n
.find("mon.") == 0) {
1893 // daemons follow their profile
1894 string type
= n
.substr(0, 3);
1895 newcap
= "allow profile " + type
;
1896 } else if (p
->second
.caps
.count("mon")) {
1897 // if there are any mon caps, give them 'r' mgr caps
1901 if (newcap
.length() > 0) {
1902 dout(5) << " giving " << n
<< " mgr '" << newcap
<< "'" << dendl
;
1906 EntityAuth auth
= p
->second
;
1907 auth
.caps
["mgr"] = bl
;
1909 add_entity(p
->first
, auth
);
1913 if (n
.find("mgr.") == 0 &&
1914 p
->second
.caps
.count("mon")) {
1915 // the kraken ceph-mgr@.service set the mon cap to 'allow *'.
1916 auto blp
= p
->second
.caps
["mon"].cbegin();
1918 decode(oldcaps
, blp
);
1919 if (oldcaps
== "allow *") {
1920 dout(5) << " fixing " << n
<< " mon cap to 'allow profile mgr'"
1923 encode("allow profile mgr", bl
);
1925 EntityAuth auth
= p
->second
;
1926 auth
.caps
["mon"] = bl
;
1927 add_entity(p
->first
, p
->second
);
1933 // add bootstrap key if it does not already exist
1934 // (might have already been get-or-create'd by
1935 // ceph-create-keys)
1936 EntityName bootstrap_mgr_name
;
1937 int r
= bootstrap_mgr_name
.from_str("client.bootstrap-mgr");
1939 if (!mon
.key_server
.contains(bootstrap_mgr_name
)) {
1941 EntityName name
= bootstrap_mgr_name
;
1943 encode("allow profile bootstrap-mgr", auth
.caps
["mon"]);
1944 auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1945 add_entity(name
, auth
);
1951 bool AuthMonitor::_upgrade_format_to_mimic()
1953 dout(1) << __func__
<< " upgrading from format 2 to 3" << dendl
;
1954 ceph_assert(format_version
== 2);
1956 list
<pair
<EntityName
,EntityAuth
> > auth_lst
;
1957 _generate_bootstrap_keys(&auth_lst
);
1959 bool changed
= false;
1960 for (auto &p
: auth_lst
) {
1961 if (mon
.key_server
.contains(p
.first
)) {
1964 int err
= add_entity(p
.first
, p
.second
);
1965 ceph_assert(err
== 0);
1972 void AuthMonitor::upgrade_format()
1974 constexpr unsigned int FORMAT_NONE
= 0;
1975 constexpr unsigned int FORMAT_DUMPLING
= 1;
1976 constexpr unsigned int FORMAT_LUMINOUS
= 2;
1977 constexpr unsigned int FORMAT_MIMIC
= 3;
1979 // when upgrading from the current format to a new format, ensure that
1980 // the new format doesn't break the older format. I.e., if a given format N
1981 // changes or adds something, ensure that when upgrading from N-1 to N+1, we
1982 // still observe the changes for format N if those have not been superseded
1985 unsigned int current
= FORMAT_MIMIC
;
1986 if (!mon
.get_quorum_mon_features().contains_all(
1987 ceph::features::mon::FEATURE_LUMINOUS
)) {
1988 // pre-luminous quorum
1989 current
= FORMAT_DUMPLING
;
1990 } else if (!mon
.get_quorum_mon_features().contains_all(
1991 ceph::features::mon::FEATURE_MIMIC
)) {
1993 current
= FORMAT_LUMINOUS
;
1995 if (format_version
>= current
) {
1996 dout(20) << __func__
<< " format " << format_version
1997 << " is current" << dendl
;
2001 // perform a rolling upgrade of the new format, if necessary.
2002 // i.e., if we are moving from format NONE to MIMIC, we will first upgrade
2003 // to DUMPLING, then to LUMINOUS, and finally to MIMIC, in several different
2006 bool changed
= false;
2007 if (format_version
== FORMAT_NONE
) {
2008 changed
= _upgrade_format_to_dumpling();
2010 } else if (format_version
== FORMAT_DUMPLING
) {
2011 changed
= _upgrade_format_to_luminous();
2012 } else if (format_version
== FORMAT_LUMINOUS
) {
2013 changed
= _upgrade_format_to_mimic();
2018 dout(10) << __func__
<< " proposing update from format " << format_version
2019 << " -> " << current
<< dendl
;
2020 format_version
= current
;
2025 void AuthMonitor::dump_info(Formatter
*f
)
2027 /*** WARNING: do not include any privileged information here! ***/
2028 f
->open_object_section("auth");
2029 f
->dump_unsigned("first_committed", get_first_committed());
2030 f
->dump_unsigned("last_committed", get_last_committed());
2031 f
->dump_unsigned("num_secrets", mon
.key_server
.get_num_secrets());