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/ConfigKeyService.h"
21 #include "mon/OSDMonitor.h"
23 #include "messages/MMonCommand.h"
24 #include "messages/MAuth.h"
25 #include "messages/MAuthReply.h"
26 #include "messages/MMonGlobalID.h"
27 #include "msg/Messenger.h"
29 #include "auth/AuthServiceHandler.h"
30 #include "auth/KeyRing.h"
31 #include "include/stringify.h"
32 #include "include/assert.h"
34 #define dout_subsys ceph_subsys_mon
36 #define dout_prefix _prefix(_dout, mon, get_last_committed())
37 static ostream
& _prefix(std::ostream
*_dout
, Monitor
*mon
, version_t v
) {
38 return *_dout
<< "mon." << mon
->name
<< "@" << mon
->rank
39 << "(" << mon
->get_state_name()
40 << ").auth v" << v
<< " ";
43 ostream
& operator<<(ostream
&out
, const AuthMonitor
&pm
)
48 bool AuthMonitor::check_rotate()
50 KeyServerData::Incremental rot_inc
;
51 rot_inc
.op
= KeyServerData::AUTH_INC_SET_ROTATING
;
52 if (!mon
->key_server
.updated_rotating(rot_inc
.rotating_bl
, last_rotating_ver
))
54 dout(10) << __func__
<< " updated rotating" << dendl
;
55 push_cephx_inc(rot_inc
);
60 Tick function to update the map based on performance every N seconds
63 void AuthMonitor::tick()
65 if (!is_active()) return;
67 dout(10) << *this << dendl
;
69 if (!mon
->is_leader()) return;
75 void AuthMonitor::on_active()
77 dout(10) << "AuthMonitor::on_active()" << dendl
;
79 if (!mon
->is_leader())
81 mon
->key_server
.start_server();
84 void AuthMonitor::create_initial()
86 dout(10) << "create_initial -- creating initial map" << dendl
;
88 // initialize rotating keys
89 last_rotating_ver
= 0;
91 assert(pending_auth
.size() == 1);
93 if (mon
->is_keyring_required()) {
96 int ret
= mon
->store
->get("mkfs", "keyring", bl
);
97 // fail hard only if there's an error we're not expecting to see
98 assert((ret
== 0) || (ret
== -ENOENT
));
100 // try importing only if there's a key
103 bufferlist::iterator p
= bl
.begin();
105 ::decode(keyring
, p
);
106 import_keyring(keyring
);
110 max_global_id
= MIN_GLOBAL_ID
;
113 inc
.inc_type
= GLOBAL_ID
;
114 inc
.max_global_id
= max_global_id
;
115 pending_auth
.push_back(inc
);
120 void AuthMonitor::update_from_paxos(bool *need_bootstrap
)
122 dout(10) << __func__
<< dendl
;
123 version_t version
= get_last_committed();
124 version_t keys_ver
= mon
->key_server
.get_ver();
125 if (version
== keys_ver
)
127 assert(version
> keys_ver
);
129 version_t latest_full
= get_version_latest_full();
131 dout(10) << __func__
<< " version " << version
<< " keys ver " << keys_ver
132 << " latest " << latest_full
<< dendl
;
134 if ((latest_full
> 0) && (latest_full
> keys_ver
)) {
135 bufferlist latest_bl
;
136 int err
= get_version_full(latest_full
, latest_bl
);
138 assert(latest_bl
.length() != 0);
139 dout(7) << __func__
<< " loading summary e " << latest_full
<< dendl
;
140 dout(7) << __func__
<< " latest length " << latest_bl
.length() << dendl
;
141 bufferlist::iterator p
= latest_bl
.begin();
143 ::decode(struct_v
, p
);
144 ::decode(max_global_id
, p
);
145 ::decode(mon
->key_server
, p
);
146 mon
->key_server
.set_ver(latest_full
);
147 keys_ver
= latest_full
;
150 dout(10) << __func__
<< " key server version " << mon
->key_server
.get_ver() << dendl
;
152 // walk through incrementals
153 while (version
> keys_ver
) {
155 int ret
= get_version(keys_ver
+1, bl
);
159 // reset if we are moving to initial state. we will normally have
160 // keys in here temporarily for bootstrapping that we need to
163 mon
->key_server
.clear_secrets();
165 dout(20) << __func__
<< " walking through version " << (keys_ver
+1)
166 << " len " << bl
.length() << dendl
;
168 bufferlist::iterator p
= bl
.begin();
174 switch (inc
.inc_type
) {
176 max_global_id
= inc
.max_global_id
;
181 KeyServerData::Incremental auth_inc
;
182 bufferlist::iterator iter
= inc
.auth_data
.begin();
183 ::decode(auth_inc
, iter
);
184 mon
->key_server
.apply_data_incremental(auth_inc
);
191 mon
->key_server
.set_ver(keys_ver
);
193 if (keys_ver
== 1 && mon
->is_keyring_required()) {
194 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
195 t
->erase("mkfs", "keyring");
196 mon
->store
->apply_transaction(t
);
200 if (last_allocated_id
== 0)
201 last_allocated_id
= max_global_id
;
203 dout(10) << "update_from_paxos() last_allocated_id=" << last_allocated_id
204 << " max_global_id=" << max_global_id
205 << " format_version " << format_version
209 void AuthMonitor::increase_max_global_id()
211 assert(mon
->is_leader());
213 max_global_id
+= g_conf
->mon_globalid_prealloc
;
214 dout(10) << "increasing max_global_id to " << max_global_id
<< dendl
;
216 inc
.inc_type
= GLOBAL_ID
;
217 inc
.max_global_id
= max_global_id
;
218 pending_auth
.push_back(inc
);
221 bool AuthMonitor::should_propose(double& delay
)
223 return (!pending_auth
.empty());
226 void AuthMonitor::create_pending()
228 pending_auth
.clear();
229 dout(10) << "create_pending v " << (get_last_committed() + 1) << dendl
;
232 void AuthMonitor::encode_pending(MonitorDBStore::TransactionRef t
)
234 dout(10) << __func__
<< " v " << (get_last_committed() + 1) << dendl
;
240 vector
<Incremental
>::iterator p
;
241 for (p
= pending_auth
.begin(); p
!= pending_auth
.end(); ++p
)
242 p
->encode(bl
, mon
->get_quorum_con_features());
244 version_t version
= get_last_committed() + 1;
245 put_version(t
, version
, bl
);
246 put_last_committed(t
, version
);
249 void AuthMonitor::encode_full(MonitorDBStore::TransactionRef t
)
251 version_t version
= mon
->key_server
.get_ver();
252 // do not stash full version 0 as it will never be removed nor read
256 dout(10) << __func__
<< " auth v " << version
<< dendl
;
257 assert(get_last_committed() == version
);
260 Mutex::Locker
l(mon
->key_server
.get_lock());
261 dout(20) << __func__
<< " key server has "
262 << (mon
->key_server
.has_secrets() ? "" : "no ")
263 << "secrets!" << dendl
;
265 ::encode(v
, full_bl
);
266 ::encode(max_global_id
, full_bl
);
267 ::encode(mon
->key_server
, full_bl
);
269 put_version_full(t
, version
, full_bl
);
270 put_version_latest_full(t
, version
);
273 version_t
AuthMonitor::get_trim_to()
275 unsigned max
= g_conf
->paxos_max_join_drift
* 2;
276 version_t version
= get_last_committed();
277 if (mon
->is_leader() && (version
> max
))
278 return version
- max
;
282 bool AuthMonitor::preprocess_query(MonOpRequestRef op
)
284 PaxosServiceMessage
*m
= static_cast<PaxosServiceMessage
*>(op
->get_req());
285 dout(10) << "preprocess_query " << *m
<< " from " << m
->get_orig_source_inst() << dendl
;
286 switch (m
->get_type()) {
287 case MSG_MON_COMMAND
:
288 return preprocess_command(op
);
291 return prep_auth(op
, false);
293 case MSG_MON_GLOBAL_ID
:
302 bool AuthMonitor::prepare_update(MonOpRequestRef op
)
304 PaxosServiceMessage
*m
= static_cast<PaxosServiceMessage
*>(op
->get_req());
305 dout(10) << "prepare_update " << *m
<< " from " << m
->get_orig_source_inst() << dendl
;
306 switch (m
->get_type()) {
307 case MSG_MON_COMMAND
:
308 return prepare_command(op
);
309 case MSG_MON_GLOBAL_ID
:
310 return prepare_global_id(op
);
312 return prep_auth(op
, true);
319 uint64_t AuthMonitor::assign_global_id(MonOpRequestRef op
, bool should_increase_max
)
321 MAuth
*m
= static_cast<MAuth
*>(op
->get_req());
322 int total_mon
= mon
->monmap
->size();
323 dout(10) << "AuthMonitor::assign_global_id m=" << *m
<< " mon=" << mon
->rank
<< "/" << total_mon
324 << " last_allocated=" << last_allocated_id
<< " max_global_id=" << max_global_id
<< dendl
;
326 uint64_t next_global_id
= last_allocated_id
+ 1;
327 int remainder
= next_global_id
% total_mon
;
329 remainder
= total_mon
- remainder
;
330 next_global_id
+= remainder
+ mon
->rank
;
331 dout(10) << "next_global_id should be " << next_global_id
<< dendl
;
333 // if we can't bump the max, bail out now on an out-of-bounds gid
334 if (next_global_id
> max_global_id
&&
335 (!mon
->is_leader() || !should_increase_max
)) {
339 // can we return a gid?
340 bool return_next
= (next_global_id
<= max_global_id
);
343 while (mon
->is_leader() &&
344 (max_global_id
< g_conf
->mon_globalid_prealloc
||
345 next_global_id
>= max_global_id
- g_conf
->mon_globalid_prealloc
/ 2)) {
346 increase_max_global_id();
350 last_allocated_id
= next_global_id
;
351 return next_global_id
;
358 bool AuthMonitor::prep_auth(MonOpRequestRef op
, bool paxos_writable
)
360 MAuth
*m
= static_cast<MAuth
*>(op
->get_req());
361 dout(10) << "prep_auth() blob_size=" << m
->get_auth_payload().length() << dendl
;
363 MonSession
*s
= op
->get_session();
365 dout(10) << "no session, dropping" << dendl
;
370 AuthCapsInfo caps_info
;
372 bufferlist response_bl
;
373 bufferlist::iterator indata
= m
->auth_payload
.begin();
374 __u32 proto
= m
->protocol
;
376 EntityName entity_name
;
379 if (m
->protocol
== 0 && !s
->auth_handler
) {
380 set
<__u32
> supported
;
384 ::decode(struct_v
, indata
);
385 ::decode(supported
, indata
);
386 ::decode(entity_name
, indata
);
387 ::decode(s
->global_id
, indata
);
388 } catch (const buffer::error
&e
) {
389 dout(10) << "failed to decode initial auth message" << dendl
;
394 // do we require cephx signatures?
396 if (!m
->get_connection()->has_feature(CEPH_FEATURE_MSG_AUTH
)) {
397 if (entity_name
.get_type() == CEPH_ENTITY_TYPE_MON
||
398 entity_name
.get_type() == CEPH_ENTITY_TYPE_OSD
||
399 entity_name
.get_type() == CEPH_ENTITY_TYPE_MDS
||
400 entity_name
.get_type() == CEPH_ENTITY_TYPE_MGR
) {
401 if (g_conf
->cephx_cluster_require_signatures
||
402 g_conf
->cephx_require_signatures
) {
403 dout(1) << m
->get_source_inst()
404 << " supports cephx but not signatures and"
405 << " 'cephx [cluster] require signatures = true';"
406 << " disallowing cephx" << dendl
;
407 supported
.erase(CEPH_AUTH_CEPHX
);
410 if (g_conf
->cephx_service_require_signatures
||
411 g_conf
->cephx_require_signatures
) {
412 dout(1) << m
->get_source_inst()
413 << " supports cephx but not signatures and"
414 << " 'cephx [service] require signatures = true';"
415 << " disallowing cephx" << dendl
;
416 supported
.erase(CEPH_AUTH_CEPHX
);
422 if (entity_name
.get_type() == CEPH_ENTITY_TYPE_MON
||
423 entity_name
.get_type() == CEPH_ENTITY_TYPE_OSD
||
424 entity_name
.get_type() == CEPH_ENTITY_TYPE_MDS
||
425 entity_name
.get_type() == CEPH_ENTITY_TYPE_MGR
)
426 type
= mon
->auth_cluster_required
.pick(supported
);
428 type
= mon
->auth_service_required
.pick(supported
);
430 s
->auth_handler
= get_auth_service_handler(type
, g_ceph_context
, &mon
->key_server
);
431 if (!s
->auth_handler
) {
432 dout(1) << "client did not provide supported auth type" << dendl
;
437 } else if (!s
->auth_handler
) {
438 dout(10) << "protocol specified but no s->auth_handler" << dendl
;
443 /* assign a new global_id? we assume this should only happen on the first
444 request. If a client tries to send it later, it'll screw up its auth
447 s
->global_id
= assign_global_id(op
, paxos_writable
);
450 delete s
->auth_handler
;
451 s
->auth_handler
= NULL
;
453 if (mon
->is_leader() && paxos_writable
) {
454 dout(10) << "increasing global id, waitlisting message" << dendl
;
455 wait_for_active(op
, new C_RetryMessage(this, op
));
459 if (!mon
->is_leader()) {
460 dout(10) << "not the leader, requesting more ids from leader" << dendl
;
461 int leader
= mon
->get_leader();
462 MMonGlobalID
*req
= new MMonGlobalID();
463 req
->old_max_id
= max_global_id
;
464 mon
->messenger
->send_message(req
, mon
->monmap
->get_inst(leader
));
465 wait_for_finished_proposal(op
, new C_RetryMessage(this, op
));
469 assert(!paxos_writable
);
479 // always send the latest monmap.
480 if (m
->monmap_epoch
< mon
->monmap
->get_epoch())
481 mon
->send_latest_monmap(m
->get_connection().get());
483 proto
= s
->auth_handler
->start_session(entity_name
, indata
, response_bl
, caps_info
);
485 if (caps_info
.allow_all
)
486 s
->caps
.set_allow_all();
489 ret
= s
->auth_handler
->handle_request(indata
, response_bl
, s
->global_id
, caps_info
, &auid
);
492 wait_for_active(op
, new C_RetryMessage(this,op
));
495 if (caps_info
.caps
.length()) {
496 bufferlist::iterator p
= caps_info
.caps
.begin();
500 } catch (const buffer::error
&err
) {
501 derr
<< "corrupt cap data for " << entity_name
<< " in auth db" << dendl
;
504 s
->caps
.parse(str
, NULL
);
507 } catch (const buffer::error
&err
) {
509 dout(0) << "caught error when trying to handle auth request, probably malformed request" << dendl
;
513 reply
= new MAuthReply(proto
, &response_bl
, ret
, s
->global_id
);
514 mon
->send_reply(op
, reply
);
519 bool AuthMonitor::preprocess_command(MonOpRequestRef op
)
521 MMonCommand
*m
= static_cast<MMonCommand
*>(op
->get_req());
526 map
<string
, cmd_vartype
> cmdmap
;
527 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
528 // ss has reason for failure
529 string rs
= ss
.str();
530 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
535 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
536 if (prefix
== "auth add" ||
537 prefix
== "auth del" ||
538 prefix
== "auth rm" ||
539 prefix
== "auth get-or-create" ||
540 prefix
== "auth get-or-create-key" ||
541 prefix
== "auth import" ||
542 prefix
== "auth caps") {
546 MonSession
*session
= m
->get_session();
548 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
552 // entity might not be supplied, but if it is, it should be valid
554 cmd_getval(g_ceph_context
, cmdmap
, "entity", entity_name
);
556 if (!entity_name
.empty() && !entity
.from_str(entity_name
)) {
557 ss
<< "invalid entity_auth " << entity_name
;
558 mon
->reply_command(op
, -EINVAL
, ss
.str(), get_last_committed());
563 cmd_getval(g_ceph_context
, cmdmap
, "format", format
, string("plain"));
564 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
566 if (prefix
== "auth export") {
568 export_keyring(keyring
);
569 if (!entity_name
.empty()) {
571 if (keyring
.get_auth(entity
, eauth
)) {
573 kr
.add(entity
, eauth
);
575 kr
.encode_formatted("auth", f
.get(), rdata
);
577 kr
.encode_plaintext(rdata
);
578 ss
<< "export " << eauth
;
581 ss
<< "no key for " << eauth
;
586 keyring
.encode_formatted("auth", f
.get(), rdata
);
588 keyring
.encode_plaintext(rdata
);
590 ss
<< "exported master keyring";
593 } else if (prefix
== "auth get" && !entity_name
.empty()) {
595 EntityAuth entity_auth
;
596 if(!mon
->key_server
.get_auth(entity
, entity_auth
)) {
597 ss
<< "failed to find " << entity_name
<< " in keyring";
600 keyring
.add(entity
, entity_auth
);
602 keyring
.encode_formatted("auth", f
.get(), rdata
);
604 keyring
.encode_plaintext(rdata
);
605 ss
<< "exported keyring for " << entity_name
;
608 } else if (prefix
== "auth print-key" ||
609 prefix
== "auth print_key" ||
610 prefix
== "auth get-key") {
612 if (!mon
->key_server
.get_auth(entity
, auth
)) {
613 ss
<< "don't have " << entity
;
618 auth
.key
.encode_formatted("auth", f
.get(), rdata
);
620 auth
.key
.encode_plaintext(rdata
);
623 } else if (prefix
== "auth list") {
625 mon
->key_server
.encode_formatted("auth", f
.get(), rdata
);
627 mon
->key_server
.encode_plaintext(rdata
);
628 if (rdata
.length() > 0)
629 ss
<< "installed auth entries:" << std::endl
;
631 ss
<< "no installed auth entries!" << std::endl
;
636 ss
<< "invalid command";
643 getline(ss
, rs
, '\0');
644 mon
->reply_command(op
, r
, rs
, rdata
, get_last_committed());
648 void AuthMonitor::export_keyring(KeyRing
& keyring
)
650 mon
->key_server
.export_keyring(keyring
);
653 int AuthMonitor::import_keyring(KeyRing
& keyring
)
655 for (map
<EntityName
, EntityAuth
>::iterator p
= keyring
.get_keys().begin();
656 p
!= keyring
.get_keys().end();
658 if (p
->second
.caps
.empty()) {
659 dout(0) << "import: no caps supplied" << dendl
;
662 int err
= add_entity(p
->first
, p
->second
);
668 int AuthMonitor::remove_entity(const EntityName
&entity
)
670 dout(10) << __func__
<< " " << entity
<< dendl
;
671 if (!mon
->key_server
.contains(entity
))
674 KeyServerData::Incremental auth_inc
;
675 auth_inc
.name
= entity
;
676 auth_inc
.op
= KeyServerData::AUTH_INC_DEL
;
677 push_cephx_inc(auth_inc
);
682 bool AuthMonitor::entity_is_pending(EntityName
& entity
)
684 // are we about to have it?
685 for (auto& p
: pending_auth
) {
686 if (p
.inc_type
== AUTH_DATA
) {
687 KeyServerData::Incremental inc
;
688 bufferlist::iterator q
= p
.auth_data
.begin();
690 if (inc
.op
== KeyServerData::AUTH_INC_ADD
&&
691 inc
.name
== entity
) {
699 int AuthMonitor::exists_and_matches_entity(
700 const auth_entity_t
& entity
,
704 return exists_and_matches_entity(entity
.name
, entity
.auth
,
705 entity
.auth
.caps
, has_secret
, ss
);
708 int AuthMonitor::exists_and_matches_entity(
709 const EntityName
& name
,
710 const EntityAuth
& auth
,
711 const map
<string
,bufferlist
>& caps
,
716 dout(20) << __func__
<< " entity " << name
<< " auth " << auth
717 << " caps " << caps
<< " has_secret " << has_secret
<< dendl
;
719 EntityAuth existing_auth
;
720 // does entry already exist?
721 if (mon
->key_server
.get_auth(name
, existing_auth
)) {
724 if (existing_auth
.key
.get_secret().cmp(auth
.key
.get_secret())) {
725 ss
<< "entity " << name
<< " exists but key does not match";
731 if (caps
.size() != existing_auth
.caps
.size()) {
732 ss
<< "entity " << name
<< " exists but caps do not match";
735 for (auto& it
: caps
) {
736 if (existing_auth
.caps
.count(it
.first
) == 0 ||
737 !existing_auth
.caps
[it
.first
].contents_equal(it
.second
)) {
738 ss
<< "entity " << name
<< " exists but cap "
739 << it
.first
<< " does not match";
750 int AuthMonitor::add_entity(
751 const EntityName
& name
,
752 const EntityAuth
& auth
)
756 KeyServerData::Incremental auth_inc
;
757 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
758 auth_inc
.name
= name
;
759 auth_inc
.auth
= auth
;
761 dout(10) << " importing " << auth_inc
.name
<< dendl
;
762 dout(30) << " " << auth_inc
.auth
<< dendl
;
763 push_cephx_inc(auth_inc
);
767 int AuthMonitor::validate_osd_destroy(
770 EntityName
& cephx_entity
,
771 EntityName
& lockbox_entity
,
774 assert(paxos
->is_plugged());
776 dout(10) << __func__
<< " id " << id
<< " uuid " << uuid
<< dendl
;
778 string cephx_str
= "osd." + stringify(id
);
779 string lockbox_str
= "client.osd-lockbox." + stringify(uuid
);
781 if (!cephx_entity
.from_str(cephx_str
)) {
782 dout(10) << __func__
<< " invalid cephx entity '"
783 << cephx_str
<< "'" << dendl
;
784 ss
<< "invalid cephx key entity '" << cephx_str
<< "'";
788 if (!lockbox_entity
.from_str(lockbox_str
)) {
789 dout(10) << __func__
<< " invalid lockbox entity '"
790 << lockbox_str
<< "'" << dendl
;
791 ss
<< "invalid lockbox key entity '" << lockbox_str
<< "'";
795 if (!mon
->key_server
.contains(cephx_entity
) &&
796 !mon
->key_server
.contains(lockbox_entity
)) {
803 int AuthMonitor::do_osd_destroy(
804 const EntityName
& cephx_entity
,
805 const EntityName
& lockbox_entity
)
807 assert(paxos
->is_plugged());
809 dout(10) << __func__
<< " cephx " << cephx_entity
810 << " lockbox " << lockbox_entity
<< dendl
;
812 bool removed
= false;
814 int err
= remove_entity(cephx_entity
);
815 if (err
== -ENOENT
) {
816 dout(10) << __func__
<< " " << cephx_entity
<< " does not exist" << dendl
;
821 err
= remove_entity(lockbox_entity
);
822 if (err
== -ENOENT
) {
823 dout(10) << __func__
<< " " << lockbox_entity
<< " does not exist" << dendl
;
829 dout(10) << __func__
<< " entities do not exist -- no-op." << dendl
;
833 // given we have paxos plugged, this will not result in a proposal
834 // being triggered, but it will still be needed so that we get our
835 // pending state encoded into the paxos' pending transaction.
840 bufferlist
_encode_cap(const string
& cap
)
850 const map
<string
,bufferlist
>& caps
)
855 auth
.key
.decode_base64(key
);
856 } catch (buffer::error
& e
) {
863 int AuthMonitor::validate_osd_new(
866 const string
& cephx_secret
,
867 const string
& lockbox_secret
,
868 auth_entity_t
& cephx_entity
,
869 auth_entity_t
& lockbox_entity
,
873 dout(10) << __func__
<< " osd." << id
<< " uuid " << uuid
<< dendl
;
875 map
<string
,bufferlist
> cephx_caps
= {
876 { "osd", _encode_cap("allow *") },
877 { "mon", _encode_cap("allow profile osd") },
878 { "mgr", _encode_cap("allow profile osd") }
880 map
<string
,bufferlist
> lockbox_caps
= {
881 { "mon", _encode_cap("allow command \"config-key get\" "
882 "with key=\"dm-crypt/osd/" +
887 bool has_lockbox
= !lockbox_secret
.empty();
889 string cephx_name
= "osd." + stringify(id
);
890 string lockbox_name
= "client.osd-lockbox." + stringify(uuid
);
892 if (!cephx_entity
.name
.from_str(cephx_name
)) {
893 dout(10) << __func__
<< " invalid cephx entity '"
894 << cephx_name
<< "'" << dendl
;
895 ss
<< "invalid cephx key entity '" << cephx_name
<< "'";
900 if (!lockbox_entity
.name
.from_str(lockbox_name
)) {
901 dout(10) << __func__
<< " invalid cephx lockbox entity '"
902 << lockbox_name
<< "'" << dendl
;
903 ss
<< "invalid cephx lockbox entity '" << lockbox_name
<< "'";
908 if (entity_is_pending(cephx_entity
.name
) ||
909 (has_lockbox
&& entity_is_pending(lockbox_entity
.name
))) {
910 // If we have pending entities for either the cephx secret or the
911 // lockbox secret, then our safest bet is to retry the command at
912 // a later time. These entities may be pending because an `osd new`
913 // command has been run (which is unlikely, due to the nature of
914 // the operation, which will force a paxos proposal), or (more likely)
915 // because a competing client created those entities before we handled
916 // the `osd new` command. Regardless, let's wait and see.
920 if (!is_valid_cephx_key(cephx_secret
)) {
921 ss
<< "invalid cephx secret.";
925 if (has_lockbox
&& !is_valid_cephx_key(lockbox_secret
)) {
926 ss
<< "invalid cephx lockbox secret.";
930 int err
= _create_auth(cephx_entity
.auth
, cephx_secret
, cephx_caps
);
933 bool cephx_is_idempotent
= false, lockbox_is_idempotent
= false;
934 err
= exists_and_matches_entity(cephx_entity
, true, ss
);
936 if (err
!= -ENOENT
) {
941 cephx_is_idempotent
= true;
945 err
= _create_auth(lockbox_entity
.auth
, lockbox_secret
, lockbox_caps
);
947 err
= exists_and_matches_entity(lockbox_entity
, true, ss
);
948 if (err
!= -ENOENT
) {
953 lockbox_is_idempotent
= true;
957 if (cephx_is_idempotent
&& (!has_lockbox
|| lockbox_is_idempotent
)) {
964 int AuthMonitor::do_osd_new(
965 const auth_entity_t
& cephx_entity
,
966 const auth_entity_t
& lockbox_entity
,
969 assert(paxos
->is_plugged());
971 dout(10) << __func__
<< " cephx " << cephx_entity
.name
974 *_dout
<< lockbox_entity
.name
;
980 // we must have validated before reaching this point.
981 // if keys exist, then this means they also match; otherwise we would
982 // have failed before calling this function.
983 bool cephx_exists
= mon
->key_server
.contains(cephx_entity
.name
);
986 int err
= add_entity(cephx_entity
.name
, cephx_entity
.auth
);
991 !mon
->key_server
.contains(lockbox_entity
.name
)) {
992 int err
= add_entity(lockbox_entity
.name
, lockbox_entity
.auth
);
996 // given we have paxos plugged, this will not result in a proposal
997 // being triggered, but it will still be needed so that we get our
998 // pending state encoded into the paxos' pending transaction.
1003 bool AuthMonitor::prepare_command(MonOpRequestRef op
)
1005 MMonCommand
*m
= static_cast<MMonCommand
*>(op
->get_req());
1006 stringstream ss
, ds
;
1011 map
<string
, cmd_vartype
> cmdmap
;
1012 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
1013 // ss has reason for failure
1014 string rs
= ss
.str();
1015 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
1020 vector
<string
>caps_vec
;
1024 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
1027 cmd_getval(g_ceph_context
, cmdmap
, "format", format
, string("plain"));
1028 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
1030 MonSession
*session
= m
->get_session();
1032 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
1036 cmd_getval(g_ceph_context
, cmdmap
, "caps", caps_vec
);
1037 if ((caps_vec
.size() % 2) != 0) {
1038 ss
<< "bad capabilities request; odd number of arguments";
1043 cmd_getval(g_ceph_context
, cmdmap
, "entity", entity_name
);
1044 if (!entity_name
.empty() && !entity
.from_str(entity_name
)) {
1045 ss
<< "bad entity name";
1050 if (prefix
== "auth import") {
1051 bufferlist bl
= m
->get_data();
1052 if (bl
.length() == 0) {
1053 ss
<< "auth import: no data supplied";
1055 mon
->reply_command(op
, -EINVAL
, rs
, get_last_committed());
1058 bufferlist::iterator iter
= bl
.begin();
1061 ::decode(keyring
, iter
);
1062 } catch (const buffer::error
&ex
) {
1063 ss
<< "error decoding keyring" << " " << ex
.what();
1067 err
= import_keyring(keyring
);
1069 ss
<< "auth import: no caps supplied";
1071 mon
->reply_command(op
, -EINVAL
, rs
, get_last_committed());
1074 ss
<< "imported keyring";
1077 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1078 get_last_committed() + 1));
1080 } else if (prefix
== "auth add" && !entity_name
.empty()) {
1081 /* expected behavior:
1082 * - if command reproduces current state, return 0.
1083 * - if command adds brand new entity, handle it.
1084 * - if command adds new state to existing entity, return error.
1086 KeyServerData::Incremental auth_inc
;
1087 auth_inc
.name
= entity
;
1088 bufferlist bl
= m
->get_data();
1089 bool has_keyring
= (bl
.length() > 0);
1090 map
<string
,bufferlist
> new_caps
;
1092 KeyRing new_keyring
;
1094 bufferlist::iterator iter
= bl
.begin();
1096 ::decode(new_keyring
, iter
);
1097 } catch (const buffer::error
&ex
) {
1098 ss
<< "error decoding keyring";
1104 // are we about to have it?
1105 if (entity_is_pending(entity
)) {
1106 wait_for_finished_proposal(op
,
1107 new Monitor::C_Command(mon
, op
, 0, rs
, get_last_committed() + 1));
1111 // build new caps from provided arguments (if available)
1112 for (vector
<string
>::iterator it
= caps_vec
.begin();
1113 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1117 ::encode(*(it
+1), cap
);
1118 new_caps
[sys
] = cap
;
1121 // pull info out of provided keyring
1124 if (!new_keyring
.get_auth(auth_inc
.name
, new_inc
)) {
1125 ss
<< "key for " << auth_inc
.name
1126 << " not found in provided keyring";
1130 if (!new_caps
.empty() && !new_inc
.caps
.empty()) {
1131 ss
<< "caps cannot be specified both in keyring and in command";
1135 if (new_caps
.empty()) {
1136 new_caps
= new_inc
.caps
;
1140 err
= exists_and_matches_entity(auth_inc
.name
, new_inc
,
1141 new_caps
, has_keyring
, ss
);
1142 // if entity/key/caps do not exist in the keyring, just fall through
1143 // and add the entity; otherwise, make sure everything matches (in
1144 // which case it's a no-op), because if not we must fail.
1145 if (err
!= -ENOENT
) {
1157 dout(10) << "AuthMonitor::prepare_command generating random key for "
1158 << auth_inc
.name
<< dendl
;
1159 new_inc
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1161 new_inc
.caps
= new_caps
;
1163 err
= add_entity(auth_inc
.name
, new_inc
);
1166 ss
<< "added key for " << auth_inc
.name
;
1168 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1169 get_last_committed() + 1));
1171 } else if ((prefix
== "auth get-or-create-key" ||
1172 prefix
== "auth get-or-create") &&
1173 !entity_name
.empty()) {
1174 // auth get-or-create <name> [mon osdcapa osd osdcapb ...]
1176 if (!valid_caps(caps_vec
, &ss
)) {
1181 // Parse the list of caps into a map
1182 std::map
<std::string
, bufferlist
> wanted_caps
;
1183 for (vector
<string
>::const_iterator it
= caps_vec
.begin();
1184 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1186 const std::string
&sys
= *it
;
1188 ::encode(*(it
+1), cap
);
1189 wanted_caps
[sys
] = cap
;
1193 EntityAuth entity_auth
;
1194 if (mon
->key_server
.get_auth(entity
, entity_auth
)) {
1195 for (const auto &sys_cap
: wanted_caps
) {
1196 if (entity_auth
.caps
.count(sys_cap
.first
) == 0 ||
1197 !entity_auth
.caps
[sys_cap
.first
].contents_equal(sys_cap
.second
)) {
1198 ss
<< "key for " << entity
<< " exists but cap " << sys_cap
.first
1199 << " does not match";
1205 if (prefix
== "auth get-or-create-key") {
1207 entity_auth
.key
.encode_formatted("auth", f
.get(), rdata
);
1209 ds
<< entity_auth
.key
;
1213 kr
.add(entity
, entity_auth
.key
);
1215 kr
.set_caps(entity
, entity_auth
.caps
);
1216 kr
.encode_formatted("auth", f
.get(), rdata
);
1218 kr
.encode_plaintext(rdata
);
1225 // ...or are we about to?
1226 for (vector
<Incremental
>::iterator p
= pending_auth
.begin();
1227 p
!= pending_auth
.end();
1229 if (p
->inc_type
== AUTH_DATA
) {
1230 KeyServerData::Incremental auth_inc
;
1231 bufferlist::iterator q
= p
->auth_data
.begin();
1232 ::decode(auth_inc
, q
);
1233 if (auth_inc
.op
== KeyServerData::AUTH_INC_ADD
&&
1234 auth_inc
.name
== entity
) {
1235 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1236 get_last_committed() + 1));
1243 KeyServerData::Incremental auth_inc
;
1244 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1245 auth_inc
.name
= entity
;
1246 auth_inc
.auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1247 auth_inc
.auth
.caps
= wanted_caps
;
1249 push_cephx_inc(auth_inc
);
1251 if (prefix
== "auth get-or-create-key") {
1253 auth_inc
.auth
.key
.encode_formatted("auth", f
.get(), rdata
);
1255 ds
<< auth_inc
.auth
.key
;
1259 kr
.add(entity
, auth_inc
.auth
.key
);
1261 kr
.set_caps(entity
, wanted_caps
);
1262 kr
.encode_formatted("auth", f
.get(), rdata
);
1264 kr
.encode_plaintext(rdata
);
1270 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
, rdata
,
1271 get_last_committed() + 1));
1273 } else if (prefix
== "auth caps" && !entity_name
.empty()) {
1274 KeyServerData::Incremental auth_inc
;
1275 auth_inc
.name
= entity
;
1276 if (!mon
->key_server
.get_auth(auth_inc
.name
, auth_inc
.auth
)) {
1277 ss
<< "couldn't find entry " << auth_inc
.name
;
1282 if (!valid_caps(caps_vec
, &ss
)) {
1287 map
<string
,bufferlist
> newcaps
;
1288 for (vector
<string
>::iterator it
= caps_vec
.begin();
1289 it
!= caps_vec
.end(); it
+= 2)
1290 ::encode(*(it
+1), newcaps
[*it
]);
1292 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1293 auth_inc
.auth
.caps
= newcaps
;
1294 push_cephx_inc(auth_inc
);
1296 ss
<< "updated caps for " << auth_inc
.name
;
1298 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1299 get_last_committed() + 1));
1301 } else if ((prefix
== "auth del" || prefix
== "auth rm") &&
1302 !entity_name
.empty()) {
1303 KeyServerData::Incremental auth_inc
;
1304 auth_inc
.name
= entity
;
1305 if (!mon
->key_server
.contains(auth_inc
.name
)) {
1306 ss
<< "entity " << entity
<< " does not exist";
1310 auth_inc
.op
= KeyServerData::AUTH_INC_DEL
;
1311 push_cephx_inc(auth_inc
);
1315 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1316 get_last_committed() + 1));
1321 getline(ss
, rs
, '\0');
1322 mon
->reply_command(op
, err
, rs
, rdata
, get_last_committed());
1326 bool AuthMonitor::prepare_global_id(MonOpRequestRef op
)
1328 dout(10) << "AuthMonitor::prepare_global_id" << dendl
;
1329 increase_max_global_id();
1334 void AuthMonitor::upgrade_format()
1336 unsigned int current
= 2;
1337 if (!mon
->get_quorum_mon_features().contains_all(
1338 ceph::features::mon::FEATURE_LUMINOUS
)) {
1341 if (format_version
>= current
) {
1342 dout(20) << __func__
<< " format " << format_version
<< " is current" << dendl
;
1346 bool changed
= false;
1347 if (format_version
== 0) {
1348 dout(1) << __func__
<< " upgrading from format 0 to 1" << dendl
;
1349 map
<EntityName
, EntityAuth
>::iterator p
;
1350 for (p
= mon
->key_server
.secrets_begin();
1351 p
!= mon
->key_server
.secrets_end();
1353 // grab mon caps, if any
1355 if (p
->second
.caps
.count("mon") == 0)
1358 bufferlist::iterator it
= p
->second
.caps
["mon"].begin();
1359 ::decode(mon_caps
, it
);
1361 catch (buffer::error
) {
1362 dout(10) << __func__
<< " unable to parse mon cap for "
1363 << p
->first
<< dendl
;
1367 string n
= p
->first
.to_str();
1370 // set daemon profiles
1371 if ((p
->first
.is_osd() || p
->first
.is_mds()) &&
1372 mon_caps
== "allow rwx") {
1373 new_caps
= string("allow profile ") + string(p
->first
.get_type_name());
1376 // update bootstrap keys
1377 if (n
== "client.bootstrap-osd") {
1378 new_caps
= "allow profile bootstrap-osd";
1380 if (n
== "client.bootstrap-mds") {
1381 new_caps
= "allow profile bootstrap-mds";
1384 if (new_caps
.length() > 0) {
1385 dout(5) << __func__
<< " updating " << p
->first
<< " mon cap from "
1386 << mon_caps
<< " to " << new_caps
<< dendl
;
1389 ::encode(new_caps
, bl
);
1391 KeyServerData::Incremental auth_inc
;
1392 auth_inc
.name
= p
->first
;
1393 auth_inc
.auth
= p
->second
;
1394 auth_inc
.auth
.caps
["mon"] = bl
;
1395 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1396 push_cephx_inc(auth_inc
);
1402 if (format_version
== 1) {
1403 dout(1) << __func__
<< " upgrading from format 1 to 2" << dendl
;
1404 map
<EntityName
, EntityAuth
>::iterator p
;
1405 for (p
= mon
->key_server
.secrets_begin();
1406 p
!= mon
->key_server
.secrets_end();
1408 string n
= p
->first
.to_str();
1411 if (n
== "client.admin") {
1412 // admin gets it all
1414 } else if (n
.find("osd.") == 0 ||
1415 n
.find("mds.") == 0 ||
1416 n
.find("mon.") == 0) {
1417 // daemons follow their profile
1418 string type
= n
.substr(0, 3);
1419 newcap
= "allow profile " + type
;
1420 } else if (p
->second
.caps
.count("mon")) {
1421 // if there are any mon caps, give them 'r' mgr caps
1425 if (newcap
.length() > 0) {
1426 dout(5) << " giving " << n
<< " mgr '" << newcap
<< "'" << dendl
;
1428 ::encode(newcap
, bl
);
1430 KeyServerData::Incremental auth_inc
;
1431 auth_inc
.name
= p
->first
;
1432 auth_inc
.auth
= p
->second
;
1433 auth_inc
.auth
.caps
["mgr"] = bl
;
1434 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1435 push_cephx_inc(auth_inc
);
1438 if (n
.find("mgr.") == 0 &&
1439 p
->second
.caps
.count("mon")) {
1440 // the kraken ceph-mgr@.service set the mon cap to 'allow *'.
1441 auto blp
= p
->second
.caps
["mon"].begin();
1443 ::decode(oldcaps
, blp
);
1444 if (oldcaps
== "allow *") {
1445 dout(5) << " fixing " << n
<< " mon cap to 'allow profile mgr'"
1448 ::encode("allow profile mgr", bl
);
1449 KeyServerData::Incremental auth_inc
;
1450 auth_inc
.name
= p
->first
;
1451 auth_inc
.auth
= p
->second
;
1452 auth_inc
.auth
.caps
["mon"] = bl
;
1453 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1454 push_cephx_inc(auth_inc
);
1459 // add bootstrap key
1461 KeyServerData::Incremental auth_inc
;
1462 bool r
= auth_inc
.name
.from_str("client.bootstrap-mgr");
1464 ::encode("allow profile bootstrap-mgr", auth_inc
.auth
.caps
["mon"]);
1465 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1466 push_cephx_inc(auth_inc
);
1473 dout(10) << __func__
<< " proposing update from format " << format_version
1474 << " -> " << current
<< dendl
;
1475 format_version
= current
;
1480 void AuthMonitor::dump_info(Formatter
*f
)
1482 /*** WARNING: do not include any privileged information here! ***/
1483 f
->open_object_section("auth");
1484 f
->dump_unsigned("first_committed", get_first_committed());
1485 f
->dump_unsigned("last_committed", get_last_committed());
1486 f
->dump_unsigned("num_secrets", mon
->key_server
.get_num_secrets());