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" ||
624 prefix
== "auth ls") {
626 mon
->key_server
.encode_formatted("auth", f
.get(), rdata
);
628 mon
->key_server
.encode_plaintext(rdata
);
629 if (rdata
.length() > 0)
630 ss
<< "installed auth entries:" << std::endl
;
632 ss
<< "no installed auth entries!" << std::endl
;
637 ss
<< "invalid command";
644 getline(ss
, rs
, '\0');
645 mon
->reply_command(op
, r
, rs
, rdata
, get_last_committed());
649 void AuthMonitor::export_keyring(KeyRing
& keyring
)
651 mon
->key_server
.export_keyring(keyring
);
654 int AuthMonitor::import_keyring(KeyRing
& keyring
)
656 for (map
<EntityName
, EntityAuth
>::iterator p
= keyring
.get_keys().begin();
657 p
!= keyring
.get_keys().end();
659 if (p
->second
.caps
.empty()) {
660 dout(0) << "import: no caps supplied" << dendl
;
663 int err
= add_entity(p
->first
, p
->second
);
669 int AuthMonitor::remove_entity(const EntityName
&entity
)
671 dout(10) << __func__
<< " " << entity
<< dendl
;
672 if (!mon
->key_server
.contains(entity
))
675 KeyServerData::Incremental auth_inc
;
676 auth_inc
.name
= entity
;
677 auth_inc
.op
= KeyServerData::AUTH_INC_DEL
;
678 push_cephx_inc(auth_inc
);
683 bool AuthMonitor::entity_is_pending(EntityName
& entity
)
685 // are we about to have it?
686 for (auto& p
: pending_auth
) {
687 if (p
.inc_type
== AUTH_DATA
) {
688 KeyServerData::Incremental inc
;
689 bufferlist::iterator q
= p
.auth_data
.begin();
691 if (inc
.op
== KeyServerData::AUTH_INC_ADD
&&
692 inc
.name
== entity
) {
700 int AuthMonitor::exists_and_matches_entity(
701 const auth_entity_t
& entity
,
705 return exists_and_matches_entity(entity
.name
, entity
.auth
,
706 entity
.auth
.caps
, has_secret
, ss
);
709 int AuthMonitor::exists_and_matches_entity(
710 const EntityName
& name
,
711 const EntityAuth
& auth
,
712 const map
<string
,bufferlist
>& caps
,
717 dout(20) << __func__
<< " entity " << name
<< " auth " << auth
718 << " caps " << caps
<< " has_secret " << has_secret
<< dendl
;
720 EntityAuth existing_auth
;
721 // does entry already exist?
722 if (mon
->key_server
.get_auth(name
, existing_auth
)) {
725 if (existing_auth
.key
.get_secret().cmp(auth
.key
.get_secret())) {
726 ss
<< "entity " << name
<< " exists but key does not match";
732 if (caps
.size() != existing_auth
.caps
.size()) {
733 ss
<< "entity " << name
<< " exists but caps do not match";
736 for (auto& it
: caps
) {
737 if (existing_auth
.caps
.count(it
.first
) == 0 ||
738 !existing_auth
.caps
[it
.first
].contents_equal(it
.second
)) {
739 ss
<< "entity " << name
<< " exists but cap "
740 << it
.first
<< " does not match";
751 int AuthMonitor::add_entity(
752 const EntityName
& name
,
753 const EntityAuth
& auth
)
757 KeyServerData::Incremental auth_inc
;
758 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
759 auth_inc
.name
= name
;
760 auth_inc
.auth
= auth
;
762 dout(10) << " importing " << auth_inc
.name
<< dendl
;
763 dout(30) << " " << auth_inc
.auth
<< dendl
;
764 push_cephx_inc(auth_inc
);
768 int AuthMonitor::validate_osd_destroy(
771 EntityName
& cephx_entity
,
772 EntityName
& lockbox_entity
,
775 assert(paxos
->is_plugged());
777 dout(10) << __func__
<< " id " << id
<< " uuid " << uuid
<< dendl
;
779 string cephx_str
= "osd." + stringify(id
);
780 string lockbox_str
= "client.osd-lockbox." + stringify(uuid
);
782 if (!cephx_entity
.from_str(cephx_str
)) {
783 dout(10) << __func__
<< " invalid cephx entity '"
784 << cephx_str
<< "'" << dendl
;
785 ss
<< "invalid cephx key entity '" << cephx_str
<< "'";
789 if (!lockbox_entity
.from_str(lockbox_str
)) {
790 dout(10) << __func__
<< " invalid lockbox entity '"
791 << lockbox_str
<< "'" << dendl
;
792 ss
<< "invalid lockbox key entity '" << lockbox_str
<< "'";
796 if (!mon
->key_server
.contains(cephx_entity
) &&
797 !mon
->key_server
.contains(lockbox_entity
)) {
804 int AuthMonitor::do_osd_destroy(
805 const EntityName
& cephx_entity
,
806 const EntityName
& lockbox_entity
)
808 assert(paxos
->is_plugged());
810 dout(10) << __func__
<< " cephx " << cephx_entity
811 << " lockbox " << lockbox_entity
<< dendl
;
813 bool removed
= false;
815 int err
= remove_entity(cephx_entity
);
816 if (err
== -ENOENT
) {
817 dout(10) << __func__
<< " " << cephx_entity
<< " does not exist" << dendl
;
822 err
= remove_entity(lockbox_entity
);
823 if (err
== -ENOENT
) {
824 dout(10) << __func__
<< " " << lockbox_entity
<< " does not exist" << dendl
;
830 dout(10) << __func__
<< " entities do not exist -- no-op." << dendl
;
834 // given we have paxos plugged, this will not result in a proposal
835 // being triggered, but it will still be needed so that we get our
836 // pending state encoded into the paxos' pending transaction.
841 bufferlist
_encode_cap(const string
& cap
)
851 const map
<string
,bufferlist
>& caps
)
856 auth
.key
.decode_base64(key
);
857 } catch (buffer::error
& e
) {
864 int AuthMonitor::validate_osd_new(
867 const string
& cephx_secret
,
868 const string
& lockbox_secret
,
869 auth_entity_t
& cephx_entity
,
870 auth_entity_t
& lockbox_entity
,
874 dout(10) << __func__
<< " osd." << id
<< " uuid " << uuid
<< dendl
;
876 map
<string
,bufferlist
> cephx_caps
= {
877 { "osd", _encode_cap("allow *") },
878 { "mon", _encode_cap("allow profile osd") },
879 { "mgr", _encode_cap("allow profile osd") }
881 map
<string
,bufferlist
> lockbox_caps
= {
882 { "mon", _encode_cap("allow command \"config-key get\" "
883 "with key=\"dm-crypt/osd/" +
888 bool has_lockbox
= !lockbox_secret
.empty();
890 string cephx_name
= "osd." + stringify(id
);
891 string lockbox_name
= "client.osd-lockbox." + stringify(uuid
);
893 if (!cephx_entity
.name
.from_str(cephx_name
)) {
894 dout(10) << __func__
<< " invalid cephx entity '"
895 << cephx_name
<< "'" << dendl
;
896 ss
<< "invalid cephx key entity '" << cephx_name
<< "'";
901 if (!lockbox_entity
.name
.from_str(lockbox_name
)) {
902 dout(10) << __func__
<< " invalid cephx lockbox entity '"
903 << lockbox_name
<< "'" << dendl
;
904 ss
<< "invalid cephx lockbox entity '" << lockbox_name
<< "'";
909 if (entity_is_pending(cephx_entity
.name
) ||
910 (has_lockbox
&& entity_is_pending(lockbox_entity
.name
))) {
911 // If we have pending entities for either the cephx secret or the
912 // lockbox secret, then our safest bet is to retry the command at
913 // a later time. These entities may be pending because an `osd new`
914 // command has been run (which is unlikely, due to the nature of
915 // the operation, which will force a paxos proposal), or (more likely)
916 // because a competing client created those entities before we handled
917 // the `osd new` command. Regardless, let's wait and see.
921 if (!is_valid_cephx_key(cephx_secret
)) {
922 ss
<< "invalid cephx secret.";
926 if (has_lockbox
&& !is_valid_cephx_key(lockbox_secret
)) {
927 ss
<< "invalid cephx lockbox secret.";
931 int err
= _create_auth(cephx_entity
.auth
, cephx_secret
, cephx_caps
);
934 bool cephx_is_idempotent
= false, lockbox_is_idempotent
= false;
935 err
= exists_and_matches_entity(cephx_entity
, true, ss
);
937 if (err
!= -ENOENT
) {
942 cephx_is_idempotent
= true;
946 err
= _create_auth(lockbox_entity
.auth
, lockbox_secret
, lockbox_caps
);
948 err
= exists_and_matches_entity(lockbox_entity
, true, ss
);
949 if (err
!= -ENOENT
) {
954 lockbox_is_idempotent
= true;
958 if (cephx_is_idempotent
&& (!has_lockbox
|| lockbox_is_idempotent
)) {
965 int AuthMonitor::do_osd_new(
966 const auth_entity_t
& cephx_entity
,
967 const auth_entity_t
& lockbox_entity
,
970 assert(paxos
->is_plugged());
972 dout(10) << __func__
<< " cephx " << cephx_entity
.name
975 *_dout
<< lockbox_entity
.name
;
981 // we must have validated before reaching this point.
982 // if keys exist, then this means they also match; otherwise we would
983 // have failed before calling this function.
984 bool cephx_exists
= mon
->key_server
.contains(cephx_entity
.name
);
987 int err
= add_entity(cephx_entity
.name
, cephx_entity
.auth
);
992 !mon
->key_server
.contains(lockbox_entity
.name
)) {
993 int err
= add_entity(lockbox_entity
.name
, lockbox_entity
.auth
);
997 // given we have paxos plugged, this will not result in a proposal
998 // being triggered, but it will still be needed so that we get our
999 // pending state encoded into the paxos' pending transaction.
1004 bool AuthMonitor::prepare_command(MonOpRequestRef op
)
1006 MMonCommand
*m
= static_cast<MMonCommand
*>(op
->get_req());
1007 stringstream ss
, ds
;
1012 map
<string
, cmd_vartype
> cmdmap
;
1013 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
1014 // ss has reason for failure
1015 string rs
= ss
.str();
1016 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
1021 vector
<string
>caps_vec
;
1025 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
1028 cmd_getval(g_ceph_context
, cmdmap
, "format", format
, string("plain"));
1029 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
1031 MonSession
*session
= m
->get_session();
1033 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
1037 cmd_getval(g_ceph_context
, cmdmap
, "caps", caps_vec
);
1038 if ((caps_vec
.size() % 2) != 0) {
1039 ss
<< "bad capabilities request; odd number of arguments";
1044 cmd_getval(g_ceph_context
, cmdmap
, "entity", entity_name
);
1045 if (!entity_name
.empty() && !entity
.from_str(entity_name
)) {
1046 ss
<< "bad entity name";
1051 if (prefix
== "auth import") {
1052 bufferlist bl
= m
->get_data();
1053 if (bl
.length() == 0) {
1054 ss
<< "auth import: no data supplied";
1056 mon
->reply_command(op
, -EINVAL
, rs
, get_last_committed());
1059 bufferlist::iterator iter
= bl
.begin();
1062 ::decode(keyring
, iter
);
1063 } catch (const buffer::error
&ex
) {
1064 ss
<< "error decoding keyring" << " " << ex
.what();
1068 err
= import_keyring(keyring
);
1070 ss
<< "auth import: no caps supplied";
1072 mon
->reply_command(op
, -EINVAL
, rs
, get_last_committed());
1075 ss
<< "imported keyring";
1078 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1079 get_last_committed() + 1));
1081 } else if (prefix
== "auth add" && !entity_name
.empty()) {
1082 /* expected behavior:
1083 * - if command reproduces current state, return 0.
1084 * - if command adds brand new entity, handle it.
1085 * - if command adds new state to existing entity, return error.
1087 KeyServerData::Incremental auth_inc
;
1088 auth_inc
.name
= entity
;
1089 bufferlist bl
= m
->get_data();
1090 bool has_keyring
= (bl
.length() > 0);
1091 map
<string
,bufferlist
> new_caps
;
1093 KeyRing new_keyring
;
1095 bufferlist::iterator iter
= bl
.begin();
1097 ::decode(new_keyring
, iter
);
1098 } catch (const buffer::error
&ex
) {
1099 ss
<< "error decoding keyring";
1105 // are we about to have it?
1106 if (entity_is_pending(entity
)) {
1107 wait_for_finished_proposal(op
,
1108 new Monitor::C_Command(mon
, op
, 0, rs
, get_last_committed() + 1));
1112 // build new caps from provided arguments (if available)
1113 for (vector
<string
>::iterator it
= caps_vec
.begin();
1114 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1118 ::encode(*(it
+1), cap
);
1119 new_caps
[sys
] = cap
;
1122 // pull info out of provided keyring
1125 if (!new_keyring
.get_auth(auth_inc
.name
, new_inc
)) {
1126 ss
<< "key for " << auth_inc
.name
1127 << " not found in provided keyring";
1131 if (!new_caps
.empty() && !new_inc
.caps
.empty()) {
1132 ss
<< "caps cannot be specified both in keyring and in command";
1136 if (new_caps
.empty()) {
1137 new_caps
= new_inc
.caps
;
1141 err
= exists_and_matches_entity(auth_inc
.name
, new_inc
,
1142 new_caps
, has_keyring
, ss
);
1143 // if entity/key/caps do not exist in the keyring, just fall through
1144 // and add the entity; otherwise, make sure everything matches (in
1145 // which case it's a no-op), because if not we must fail.
1146 if (err
!= -ENOENT
) {
1158 dout(10) << "AuthMonitor::prepare_command generating random key for "
1159 << auth_inc
.name
<< dendl
;
1160 new_inc
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1162 new_inc
.caps
= new_caps
;
1164 err
= add_entity(auth_inc
.name
, new_inc
);
1167 ss
<< "added key for " << auth_inc
.name
;
1169 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1170 get_last_committed() + 1));
1172 } else if ((prefix
== "auth get-or-create-key" ||
1173 prefix
== "auth get-or-create") &&
1174 !entity_name
.empty()) {
1175 // auth get-or-create <name> [mon osdcapa osd osdcapb ...]
1177 if (!valid_caps(caps_vec
, &ss
)) {
1182 // Parse the list of caps into a map
1183 std::map
<std::string
, bufferlist
> wanted_caps
;
1184 for (vector
<string
>::const_iterator it
= caps_vec
.begin();
1185 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1187 const std::string
&sys
= *it
;
1189 ::encode(*(it
+1), cap
);
1190 wanted_caps
[sys
] = cap
;
1194 EntityAuth entity_auth
;
1195 if (mon
->key_server
.get_auth(entity
, entity_auth
)) {
1196 for (const auto &sys_cap
: wanted_caps
) {
1197 if (entity_auth
.caps
.count(sys_cap
.first
) == 0 ||
1198 !entity_auth
.caps
[sys_cap
.first
].contents_equal(sys_cap
.second
)) {
1199 ss
<< "key for " << entity
<< " exists but cap " << sys_cap
.first
1200 << " does not match";
1206 if (prefix
== "auth get-or-create-key") {
1208 entity_auth
.key
.encode_formatted("auth", f
.get(), rdata
);
1210 ds
<< entity_auth
.key
;
1214 kr
.add(entity
, entity_auth
.key
);
1216 kr
.set_caps(entity
, entity_auth
.caps
);
1217 kr
.encode_formatted("auth", f
.get(), rdata
);
1219 kr
.encode_plaintext(rdata
);
1226 // ...or are we about to?
1227 for (vector
<Incremental
>::iterator p
= pending_auth
.begin();
1228 p
!= pending_auth
.end();
1230 if (p
->inc_type
== AUTH_DATA
) {
1231 KeyServerData::Incremental auth_inc
;
1232 bufferlist::iterator q
= p
->auth_data
.begin();
1233 ::decode(auth_inc
, q
);
1234 if (auth_inc
.op
== KeyServerData::AUTH_INC_ADD
&&
1235 auth_inc
.name
== entity
) {
1236 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1237 get_last_committed() + 1));
1244 KeyServerData::Incremental auth_inc
;
1245 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1246 auth_inc
.name
= entity
;
1247 auth_inc
.auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1248 auth_inc
.auth
.caps
= wanted_caps
;
1250 push_cephx_inc(auth_inc
);
1252 if (prefix
== "auth get-or-create-key") {
1254 auth_inc
.auth
.key
.encode_formatted("auth", f
.get(), rdata
);
1256 ds
<< auth_inc
.auth
.key
;
1260 kr
.add(entity
, auth_inc
.auth
.key
);
1262 kr
.set_caps(entity
, wanted_caps
);
1263 kr
.encode_formatted("auth", f
.get(), rdata
);
1265 kr
.encode_plaintext(rdata
);
1271 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
, rdata
,
1272 get_last_committed() + 1));
1274 } else if (prefix
== "auth caps" && !entity_name
.empty()) {
1275 KeyServerData::Incremental auth_inc
;
1276 auth_inc
.name
= entity
;
1277 if (!mon
->key_server
.get_auth(auth_inc
.name
, auth_inc
.auth
)) {
1278 ss
<< "couldn't find entry " << auth_inc
.name
;
1283 if (!valid_caps(caps_vec
, &ss
)) {
1288 map
<string
,bufferlist
> newcaps
;
1289 for (vector
<string
>::iterator it
= caps_vec
.begin();
1290 it
!= caps_vec
.end(); it
+= 2)
1291 ::encode(*(it
+1), newcaps
[*it
]);
1293 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1294 auth_inc
.auth
.caps
= newcaps
;
1295 push_cephx_inc(auth_inc
);
1297 ss
<< "updated caps for " << auth_inc
.name
;
1299 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1300 get_last_committed() + 1));
1302 } else if ((prefix
== "auth del" || prefix
== "auth rm") &&
1303 !entity_name
.empty()) {
1304 KeyServerData::Incremental auth_inc
;
1305 auth_inc
.name
= entity
;
1306 if (!mon
->key_server
.contains(auth_inc
.name
)) {
1307 ss
<< "entity " << entity
<< " does not exist";
1311 auth_inc
.op
= KeyServerData::AUTH_INC_DEL
;
1312 push_cephx_inc(auth_inc
);
1316 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1317 get_last_committed() + 1));
1322 getline(ss
, rs
, '\0');
1323 mon
->reply_command(op
, err
, rs
, rdata
, get_last_committed());
1327 bool AuthMonitor::prepare_global_id(MonOpRequestRef op
)
1329 dout(10) << "AuthMonitor::prepare_global_id" << dendl
;
1330 increase_max_global_id();
1335 void AuthMonitor::upgrade_format()
1337 unsigned int current
= 2;
1338 if (!mon
->get_quorum_mon_features().contains_all(
1339 ceph::features::mon::FEATURE_LUMINOUS
)) {
1342 if (format_version
>= current
) {
1343 dout(20) << __func__
<< " format " << format_version
<< " is current" << dendl
;
1347 bool changed
= false;
1348 if (format_version
== 0) {
1349 dout(1) << __func__
<< " upgrading from format 0 to 1" << dendl
;
1350 map
<EntityName
, EntityAuth
>::iterator p
;
1351 for (p
= mon
->key_server
.secrets_begin();
1352 p
!= mon
->key_server
.secrets_end();
1354 // grab mon caps, if any
1356 if (p
->second
.caps
.count("mon") == 0)
1359 bufferlist::iterator it
= p
->second
.caps
["mon"].begin();
1360 ::decode(mon_caps
, it
);
1362 catch (buffer::error
) {
1363 dout(10) << __func__
<< " unable to parse mon cap for "
1364 << p
->first
<< dendl
;
1368 string n
= p
->first
.to_str();
1371 // set daemon profiles
1372 if ((p
->first
.is_osd() || p
->first
.is_mds()) &&
1373 mon_caps
== "allow rwx") {
1374 new_caps
= string("allow profile ") + string(p
->first
.get_type_name());
1377 // update bootstrap keys
1378 if (n
== "client.bootstrap-osd") {
1379 new_caps
= "allow profile bootstrap-osd";
1381 if (n
== "client.bootstrap-mds") {
1382 new_caps
= "allow profile bootstrap-mds";
1385 if (new_caps
.length() > 0) {
1386 dout(5) << __func__
<< " updating " << p
->first
<< " mon cap from "
1387 << mon_caps
<< " to " << new_caps
<< dendl
;
1390 ::encode(new_caps
, bl
);
1392 KeyServerData::Incremental auth_inc
;
1393 auth_inc
.name
= p
->first
;
1394 auth_inc
.auth
= p
->second
;
1395 auth_inc
.auth
.caps
["mon"] = bl
;
1396 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1397 push_cephx_inc(auth_inc
);
1403 if (format_version
== 1) {
1404 dout(1) << __func__
<< " upgrading from format 1 to 2" << dendl
;
1405 map
<EntityName
, EntityAuth
>::iterator p
;
1406 for (p
= mon
->key_server
.secrets_begin();
1407 p
!= mon
->key_server
.secrets_end();
1409 string n
= p
->first
.to_str();
1412 if (n
== "client.admin") {
1413 // admin gets it all
1415 } else if (n
.find("osd.") == 0 ||
1416 n
.find("mds.") == 0 ||
1417 n
.find("mon.") == 0) {
1418 // daemons follow their profile
1419 string type
= n
.substr(0, 3);
1420 newcap
= "allow profile " + type
;
1421 } else if (p
->second
.caps
.count("mon")) {
1422 // if there are any mon caps, give them 'r' mgr caps
1426 if (newcap
.length() > 0) {
1427 dout(5) << " giving " << n
<< " mgr '" << newcap
<< "'" << dendl
;
1429 ::encode(newcap
, bl
);
1431 KeyServerData::Incremental auth_inc
;
1432 auth_inc
.name
= p
->first
;
1433 auth_inc
.auth
= p
->second
;
1434 auth_inc
.auth
.caps
["mgr"] = bl
;
1435 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1436 push_cephx_inc(auth_inc
);
1439 if (n
.find("mgr.") == 0 &&
1440 p
->second
.caps
.count("mon")) {
1441 // the kraken ceph-mgr@.service set the mon cap to 'allow *'.
1442 auto blp
= p
->second
.caps
["mon"].begin();
1444 ::decode(oldcaps
, blp
);
1445 if (oldcaps
== "allow *") {
1446 dout(5) << " fixing " << n
<< " mon cap to 'allow profile mgr'"
1449 ::encode("allow profile mgr", bl
);
1450 KeyServerData::Incremental auth_inc
;
1451 auth_inc
.name
= p
->first
;
1452 auth_inc
.auth
= p
->second
;
1453 auth_inc
.auth
.caps
["mon"] = bl
;
1454 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1455 push_cephx_inc(auth_inc
);
1460 // add bootstrap key
1462 KeyServerData::Incremental auth_inc
;
1463 bool r
= auth_inc
.name
.from_str("client.bootstrap-mgr");
1465 ::encode("allow profile bootstrap-mgr", auth_inc
.auth
.caps
["mon"]);
1466 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1468 auth_inc
.auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1469 push_cephx_inc(auth_inc
);
1476 dout(10) << __func__
<< " proposing update from format " << format_version
1477 << " -> " << current
<< dendl
;
1478 format_version
= current
;
1483 void AuthMonitor::dump_info(Formatter
*f
)
1485 /*** WARNING: do not include any privileged information here! ***/
1486 f
->open_object_section("auth");
1487 f
->dump_unsigned("first_committed", get_first_committed());
1488 f
->dump_unsigned("last_committed", get_last_committed());
1489 f
->dump_unsigned("num_secrets", mon
->key_server
.get_num_secrets());