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"
22 #include "mon/MDSMonitor.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/assert.h"
35 #define dout_subsys ceph_subsys_mon
37 #define dout_prefix _prefix(_dout, mon, get_last_committed())
38 static ostream
& _prefix(std::ostream
*_dout
, Monitor
*mon
, version_t v
) {
39 return *_dout
<< "mon." << mon
->name
<< "@" << mon
->rank
40 << "(" << mon
->get_state_name()
41 << ").auth v" << v
<< " ";
44 ostream
& operator<<(ostream
&out
, const AuthMonitor
&pm
)
49 bool AuthMonitor::check_rotate()
51 KeyServerData::Incremental rot_inc
;
52 rot_inc
.op
= KeyServerData::AUTH_INC_SET_ROTATING
;
53 if (!mon
->key_server
.updated_rotating(rot_inc
.rotating_bl
, last_rotating_ver
))
55 dout(10) << __func__
<< " updated rotating" << dendl
;
56 push_cephx_inc(rot_inc
);
61 Tick function to update the map based on performance every N seconds
64 void AuthMonitor::tick()
66 if (!is_active()) return;
68 dout(10) << *this << dendl
;
70 if (!mon
->is_leader()) return;
76 void AuthMonitor::on_active()
78 dout(10) << "AuthMonitor::on_active()" << dendl
;
80 if (!mon
->is_leader())
82 mon
->key_server
.start_server();
85 void AuthMonitor::create_initial()
87 dout(10) << "create_initial -- creating initial map" << dendl
;
89 // initialize rotating keys
90 last_rotating_ver
= 0;
92 assert(pending_auth
.size() == 1);
94 if (mon
->is_keyring_required()) {
97 int ret
= mon
->store
->get("mkfs", "keyring", bl
);
98 // fail hard only if there's an error we're not expecting to see
99 assert((ret
== 0) || (ret
== -ENOENT
));
101 // try importing only if there's a key
104 bufferlist::iterator p
= bl
.begin();
106 ::decode(keyring
, p
);
107 import_keyring(keyring
);
111 max_global_id
= MIN_GLOBAL_ID
;
114 inc
.inc_type
= GLOBAL_ID
;
115 inc
.max_global_id
= max_global_id
;
116 pending_auth
.push_back(inc
);
121 void AuthMonitor::update_from_paxos(bool *need_bootstrap
)
123 dout(10) << __func__
<< dendl
;
124 version_t version
= get_last_committed();
125 version_t keys_ver
= mon
->key_server
.get_ver();
126 if (version
== keys_ver
)
128 assert(version
> keys_ver
);
130 version_t latest_full
= get_version_latest_full();
132 dout(10) << __func__
<< " version " << version
<< " keys ver " << keys_ver
133 << " latest " << latest_full
<< dendl
;
135 if ((latest_full
> 0) && (latest_full
> keys_ver
)) {
136 bufferlist latest_bl
;
137 int err
= get_version_full(latest_full
, latest_bl
);
139 assert(latest_bl
.length() != 0);
140 dout(7) << __func__
<< " loading summary e " << latest_full
<< dendl
;
141 dout(7) << __func__
<< " latest length " << latest_bl
.length() << dendl
;
142 bufferlist::iterator p
= latest_bl
.begin();
144 ::decode(struct_v
, p
);
145 ::decode(max_global_id
, p
);
146 ::decode(mon
->key_server
, p
);
147 mon
->key_server
.set_ver(latest_full
);
148 keys_ver
= latest_full
;
151 dout(10) << __func__
<< " key server version " << mon
->key_server
.get_ver() << dendl
;
153 // walk through incrementals
154 while (version
> keys_ver
) {
156 int ret
= get_version(keys_ver
+1, bl
);
160 // reset if we are moving to initial state. we will normally have
161 // keys in here temporarily for bootstrapping that we need to
164 mon
->key_server
.clear_secrets();
166 dout(20) << __func__
<< " walking through version " << (keys_ver
+1)
167 << " len " << bl
.length() << dendl
;
169 bufferlist::iterator p
= bl
.begin();
175 switch (inc
.inc_type
) {
177 max_global_id
= inc
.max_global_id
;
182 KeyServerData::Incremental auth_inc
;
183 bufferlist::iterator iter
= inc
.auth_data
.begin();
184 ::decode(auth_inc
, iter
);
185 mon
->key_server
.apply_data_incremental(auth_inc
);
192 mon
->key_server
.set_ver(keys_ver
);
194 if (keys_ver
== 1 && mon
->is_keyring_required()) {
195 auto t(std::make_shared
<MonitorDBStore::Transaction
>());
196 t
->erase("mkfs", "keyring");
197 mon
->store
->apply_transaction(t
);
201 if (last_allocated_id
== 0)
202 last_allocated_id
= max_global_id
;
204 dout(10) << "update_from_paxos() last_allocated_id=" << last_allocated_id
205 << " max_global_id=" << max_global_id
206 << " format_version " << format_version
210 void AuthMonitor::increase_max_global_id()
212 assert(mon
->is_leader());
214 max_global_id
+= g_conf
->mon_globalid_prealloc
;
215 dout(10) << "increasing max_global_id to " << max_global_id
<< dendl
;
217 inc
.inc_type
= GLOBAL_ID
;
218 inc
.max_global_id
= max_global_id
;
219 pending_auth
.push_back(inc
);
222 bool AuthMonitor::should_propose(double& delay
)
224 return (!pending_auth
.empty());
227 void AuthMonitor::create_pending()
229 pending_auth
.clear();
230 dout(10) << "create_pending v " << (get_last_committed() + 1) << dendl
;
233 void AuthMonitor::encode_pending(MonitorDBStore::TransactionRef t
)
235 dout(10) << __func__
<< " v " << (get_last_committed() + 1) << dendl
;
241 vector
<Incremental
>::iterator p
;
242 for (p
= pending_auth
.begin(); p
!= pending_auth
.end(); ++p
)
243 p
->encode(bl
, mon
->get_quorum_con_features());
245 version_t version
= get_last_committed() + 1;
246 put_version(t
, version
, bl
);
247 put_last_committed(t
, version
);
250 void AuthMonitor::encode_full(MonitorDBStore::TransactionRef t
)
252 version_t version
= mon
->key_server
.get_ver();
253 // do not stash full version 0 as it will never be removed nor read
257 dout(10) << __func__
<< " auth v " << version
<< dendl
;
258 assert(get_last_committed() == version
);
261 Mutex::Locker
l(mon
->key_server
.get_lock());
262 dout(20) << __func__
<< " key server has "
263 << (mon
->key_server
.has_secrets() ? "" : "no ")
264 << "secrets!" << dendl
;
266 ::encode(v
, full_bl
);
267 ::encode(max_global_id
, full_bl
);
268 ::encode(mon
->key_server
, full_bl
);
270 put_version_full(t
, version
, full_bl
);
271 put_version_latest_full(t
, version
);
274 version_t
AuthMonitor::get_trim_to()
276 unsigned max
= g_conf
->paxos_max_join_drift
* 2;
277 version_t version
= get_last_committed();
278 if (mon
->is_leader() && (version
> max
))
279 return version
- max
;
283 bool AuthMonitor::preprocess_query(MonOpRequestRef op
)
285 PaxosServiceMessage
*m
= static_cast<PaxosServiceMessage
*>(op
->get_req());
286 dout(10) << "preprocess_query " << *m
<< " from " << m
->get_orig_source_inst() << dendl
;
287 switch (m
->get_type()) {
288 case MSG_MON_COMMAND
:
289 return preprocess_command(op
);
292 return prep_auth(op
, false);
294 case MSG_MON_GLOBAL_ID
:
303 bool AuthMonitor::prepare_update(MonOpRequestRef op
)
305 PaxosServiceMessage
*m
= static_cast<PaxosServiceMessage
*>(op
->get_req());
306 dout(10) << "prepare_update " << *m
<< " from " << m
->get_orig_source_inst() << dendl
;
307 switch (m
->get_type()) {
308 case MSG_MON_COMMAND
:
309 return prepare_command(op
);
310 case MSG_MON_GLOBAL_ID
:
311 return prepare_global_id(op
);
313 return prep_auth(op
, true);
320 uint64_t AuthMonitor::assign_global_id(MonOpRequestRef op
, bool should_increase_max
)
322 MAuth
*m
= static_cast<MAuth
*>(op
->get_req());
323 int total_mon
= mon
->monmap
->size();
324 dout(10) << "AuthMonitor::assign_global_id m=" << *m
<< " mon=" << mon
->rank
<< "/" << total_mon
325 << " last_allocated=" << last_allocated_id
<< " max_global_id=" << max_global_id
<< dendl
;
327 uint64_t next_global_id
= last_allocated_id
+ 1;
328 int remainder
= next_global_id
% total_mon
;
330 remainder
= total_mon
- remainder
;
331 next_global_id
+= remainder
+ mon
->rank
;
332 dout(10) << "next_global_id should be " << next_global_id
<< dendl
;
334 // if we can't bump the max, bail out now on an out-of-bounds gid
335 if (next_global_id
> max_global_id
&&
336 (!mon
->is_leader() || !should_increase_max
)) {
340 // can we return a gid?
341 bool return_next
= (next_global_id
<= max_global_id
);
344 while (mon
->is_leader() &&
345 (max_global_id
< g_conf
->mon_globalid_prealloc
||
346 next_global_id
>= max_global_id
- g_conf
->mon_globalid_prealloc
/ 2)) {
347 increase_max_global_id();
351 last_allocated_id
= next_global_id
;
352 return next_global_id
;
359 bool AuthMonitor::prep_auth(MonOpRequestRef op
, bool paxos_writable
)
361 MAuth
*m
= static_cast<MAuth
*>(op
->get_req());
362 dout(10) << "prep_auth() blob_size=" << m
->get_auth_payload().length() << dendl
;
364 MonSession
*s
= op
->get_session();
366 dout(10) << "no session, dropping" << dendl
;
371 AuthCapsInfo caps_info
;
373 bufferlist response_bl
;
374 bufferlist::iterator indata
= m
->auth_payload
.begin();
375 __u32 proto
= m
->protocol
;
377 EntityName entity_name
;
380 if (m
->protocol
== 0 && !s
->auth_handler
) {
381 set
<__u32
> supported
;
385 ::decode(struct_v
, indata
);
386 ::decode(supported
, indata
);
387 ::decode(entity_name
, indata
);
388 ::decode(s
->global_id
, indata
);
389 } catch (const buffer::error
&e
) {
390 dout(10) << "failed to decode initial auth message" << dendl
;
395 // do we require cephx signatures?
397 if (!m
->get_connection()->has_feature(CEPH_FEATURE_MSG_AUTH
)) {
398 if (entity_name
.get_type() == CEPH_ENTITY_TYPE_MON
||
399 entity_name
.get_type() == CEPH_ENTITY_TYPE_OSD
||
400 entity_name
.get_type() == CEPH_ENTITY_TYPE_MDS
||
401 entity_name
.get_type() == CEPH_ENTITY_TYPE_MGR
) {
402 if (g_conf
->cephx_cluster_require_signatures
||
403 g_conf
->cephx_require_signatures
) {
404 dout(1) << m
->get_source_inst()
405 << " supports cephx but not signatures and"
406 << " 'cephx [cluster] require signatures = true';"
407 << " disallowing cephx" << dendl
;
408 supported
.erase(CEPH_AUTH_CEPHX
);
411 if (g_conf
->cephx_service_require_signatures
||
412 g_conf
->cephx_require_signatures
) {
413 dout(1) << m
->get_source_inst()
414 << " supports cephx but not signatures and"
415 << " 'cephx [service] require signatures = true';"
416 << " disallowing cephx" << dendl
;
417 supported
.erase(CEPH_AUTH_CEPHX
);
420 } else if (!m
->get_connection()->has_feature(CEPH_FEATURE_CEPHX_V2
)) {
421 if (entity_name
.get_type() == CEPH_ENTITY_TYPE_MON
||
422 entity_name
.get_type() == CEPH_ENTITY_TYPE_OSD
||
423 entity_name
.get_type() == CEPH_ENTITY_TYPE_MDS
||
424 entity_name
.get_type() == CEPH_ENTITY_TYPE_MGR
) {
425 if (g_conf
->cephx_cluster_require_version
>= 2 ||
426 g_conf
->cephx_require_version
>= 2) {
427 dout(1) << m
->get_source_inst()
428 << " supports cephx but not v2 and"
429 << " 'cephx [cluster] require version >= 2';"
430 << " disallowing cephx" << dendl
;
431 supported
.erase(CEPH_AUTH_CEPHX
);
434 if (g_conf
->cephx_service_require_version
>= 2 ||
435 g_conf
->cephx_require_version
>= 2) {
436 dout(1) << m
->get_source_inst()
437 << " supports cephx but not v2 and"
438 << " 'cephx [service] require version >= 2';"
439 << " disallowing cephx" << dendl
;
440 supported
.erase(CEPH_AUTH_CEPHX
);
446 if (entity_name
.get_type() == CEPH_ENTITY_TYPE_MON
||
447 entity_name
.get_type() == CEPH_ENTITY_TYPE_OSD
||
448 entity_name
.get_type() == CEPH_ENTITY_TYPE_MDS
||
449 entity_name
.get_type() == CEPH_ENTITY_TYPE_MGR
)
450 type
= mon
->auth_cluster_required
.pick(supported
);
452 type
= mon
->auth_service_required
.pick(supported
);
454 s
->auth_handler
= get_auth_service_handler(type
, g_ceph_context
, &mon
->key_server
);
455 if (!s
->auth_handler
) {
456 dout(1) << "client did not provide supported auth type" << dendl
;
461 } else if (!s
->auth_handler
) {
462 dout(10) << "protocol specified but no s->auth_handler" << dendl
;
467 /* assign a new global_id? we assume this should only happen on the first
468 request. If a client tries to send it later, it'll screw up its auth
471 s
->global_id
= assign_global_id(op
, paxos_writable
);
474 delete s
->auth_handler
;
475 s
->auth_handler
= NULL
;
477 if (mon
->is_leader() && paxos_writable
) {
478 dout(10) << "increasing global id, waitlisting message" << dendl
;
479 wait_for_active(op
, new C_RetryMessage(this, op
));
483 if (!mon
->is_leader()) {
484 dout(10) << "not the leader, requesting more ids from leader" << dendl
;
485 int leader
= mon
->get_leader();
486 MMonGlobalID
*req
= new MMonGlobalID();
487 req
->old_max_id
= max_global_id
;
488 mon
->messenger
->send_message(req
, mon
->monmap
->get_inst(leader
));
489 wait_for_finished_proposal(op
, new C_RetryMessage(this, op
));
493 assert(!paxos_writable
);
503 // always send the latest monmap.
504 if (m
->monmap_epoch
< mon
->monmap
->get_epoch())
505 mon
->send_latest_monmap(m
->get_connection().get());
507 proto
= s
->auth_handler
->start_session(entity_name
, indata
, response_bl
, caps_info
);
509 if (caps_info
.allow_all
)
510 s
->caps
.set_allow_all();
513 ret
= s
->auth_handler
->handle_request(indata
, response_bl
, s
->global_id
, caps_info
, &auid
);
516 wait_for_active(op
, new C_RetryMessage(this,op
));
519 if (caps_info
.caps
.length()) {
520 bufferlist::iterator p
= caps_info
.caps
.begin();
524 } catch (const buffer::error
&err
) {
525 derr
<< "corrupt cap data for " << entity_name
<< " in auth db" << dendl
;
528 s
->caps
.parse(str
, NULL
);
531 } catch (const buffer::error
&err
) {
533 dout(0) << "caught error when trying to handle auth request, probably malformed request" << dendl
;
537 reply
= new MAuthReply(proto
, &response_bl
, ret
, s
->global_id
);
538 mon
->send_reply(op
, reply
);
543 bool AuthMonitor::preprocess_command(MonOpRequestRef op
)
545 MMonCommand
*m
= static_cast<MMonCommand
*>(op
->get_req());
550 map
<string
, cmd_vartype
> cmdmap
;
551 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
552 // ss has reason for failure
553 string rs
= ss
.str();
554 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
559 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
560 if (prefix
== "auth add" ||
561 prefix
== "auth del" ||
562 prefix
== "auth rm" ||
563 prefix
== "auth get-or-create" ||
564 prefix
== "auth get-or-create-key" ||
565 prefix
== "fs authorize" ||
566 prefix
== "auth import" ||
567 prefix
== "auth caps") {
571 MonSession
*session
= m
->get_session();
573 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
577 // entity might not be supplied, but if it is, it should be valid
579 cmd_getval(g_ceph_context
, cmdmap
, "entity", entity_name
);
581 if (!entity_name
.empty() && !entity
.from_str(entity_name
)) {
582 ss
<< "invalid entity_auth " << entity_name
;
583 mon
->reply_command(op
, -EINVAL
, ss
.str(), get_last_committed());
588 cmd_getval(g_ceph_context
, cmdmap
, "format", format
, string("plain"));
589 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
591 if (prefix
== "auth export") {
593 export_keyring(keyring
);
594 if (!entity_name
.empty()) {
596 if (keyring
.get_auth(entity
, eauth
)) {
598 kr
.add(entity
, eauth
);
600 kr
.encode_formatted("auth", f
.get(), rdata
);
602 kr
.encode_plaintext(rdata
);
603 ss
<< "export " << eauth
;
606 ss
<< "no key for " << eauth
;
611 keyring
.encode_formatted("auth", f
.get(), rdata
);
613 keyring
.encode_plaintext(rdata
);
615 ss
<< "exported master keyring";
618 } else if (prefix
== "auth get" && !entity_name
.empty()) {
620 EntityAuth entity_auth
;
621 if(!mon
->key_server
.get_auth(entity
, entity_auth
)) {
622 ss
<< "failed to find " << entity_name
<< " in keyring";
625 keyring
.add(entity
, entity_auth
);
627 keyring
.encode_formatted("auth", f
.get(), rdata
);
629 keyring
.encode_plaintext(rdata
);
630 ss
<< "exported keyring for " << entity_name
;
633 } else if (prefix
== "auth print-key" ||
634 prefix
== "auth print_key" ||
635 prefix
== "auth get-key") {
637 if (!mon
->key_server
.get_auth(entity
, auth
)) {
638 ss
<< "don't have " << entity
;
643 auth
.key
.encode_formatted("auth", f
.get(), rdata
);
645 auth
.key
.encode_plaintext(rdata
);
648 } else if (prefix
== "auth list" ||
649 prefix
== "auth ls") {
651 mon
->key_server
.encode_formatted("auth", f
.get(), rdata
);
653 mon
->key_server
.encode_plaintext(rdata
);
654 if (rdata
.length() > 0)
655 ss
<< "installed auth entries:" << std::endl
;
657 ss
<< "no installed auth entries!" << std::endl
;
662 ss
<< "invalid command";
669 getline(ss
, rs
, '\0');
670 mon
->reply_command(op
, r
, rs
, rdata
, get_last_committed());
674 void AuthMonitor::export_keyring(KeyRing
& keyring
)
676 mon
->key_server
.export_keyring(keyring
);
679 int AuthMonitor::import_keyring(KeyRing
& keyring
)
681 for (map
<EntityName
, EntityAuth
>::iterator p
= keyring
.get_keys().begin();
682 p
!= keyring
.get_keys().end();
684 if (p
->second
.caps
.empty()) {
685 dout(0) << "import: no caps supplied" << dendl
;
688 int err
= add_entity(p
->first
, p
->second
);
694 int AuthMonitor::remove_entity(const EntityName
&entity
)
696 dout(10) << __func__
<< " " << entity
<< dendl
;
697 if (!mon
->key_server
.contains(entity
))
700 KeyServerData::Incremental auth_inc
;
701 auth_inc
.name
= entity
;
702 auth_inc
.op
= KeyServerData::AUTH_INC_DEL
;
703 push_cephx_inc(auth_inc
);
708 bool AuthMonitor::entity_is_pending(EntityName
& entity
)
710 // are we about to have it?
711 for (auto& p
: pending_auth
) {
712 if (p
.inc_type
== AUTH_DATA
) {
713 KeyServerData::Incremental inc
;
714 bufferlist::iterator q
= p
.auth_data
.begin();
716 if (inc
.op
== KeyServerData::AUTH_INC_ADD
&&
717 inc
.name
== entity
) {
725 int AuthMonitor::exists_and_matches_entity(
726 const auth_entity_t
& entity
,
730 return exists_and_matches_entity(entity
.name
, entity
.auth
,
731 entity
.auth
.caps
, has_secret
, ss
);
734 int AuthMonitor::exists_and_matches_entity(
735 const EntityName
& name
,
736 const EntityAuth
& auth
,
737 const map
<string
,bufferlist
>& caps
,
742 dout(20) << __func__
<< " entity " << name
<< " auth " << auth
743 << " caps " << caps
<< " has_secret " << has_secret
<< dendl
;
745 EntityAuth existing_auth
;
746 // does entry already exist?
747 if (mon
->key_server
.get_auth(name
, existing_auth
)) {
750 if (existing_auth
.key
.get_secret().cmp(auth
.key
.get_secret())) {
751 ss
<< "entity " << name
<< " exists but key does not match";
757 if (caps
.size() != existing_auth
.caps
.size()) {
758 ss
<< "entity " << name
<< " exists but caps do not match";
761 for (auto& it
: caps
) {
762 if (existing_auth
.caps
.count(it
.first
) == 0 ||
763 !existing_auth
.caps
[it
.first
].contents_equal(it
.second
)) {
764 ss
<< "entity " << name
<< " exists but cap "
765 << it
.first
<< " does not match";
776 int AuthMonitor::add_entity(
777 const EntityName
& name
,
778 const EntityAuth
& auth
)
782 KeyServerData::Incremental auth_inc
;
783 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
784 auth_inc
.name
= name
;
785 auth_inc
.auth
= auth
;
787 dout(10) << " importing " << auth_inc
.name
<< dendl
;
788 dout(30) << " " << auth_inc
.auth
<< dendl
;
789 push_cephx_inc(auth_inc
);
793 int AuthMonitor::validate_osd_destroy(
796 EntityName
& cephx_entity
,
797 EntityName
& lockbox_entity
,
800 assert(paxos
->is_plugged());
802 dout(10) << __func__
<< " id " << id
<< " uuid " << uuid
<< dendl
;
804 string cephx_str
= "osd." + stringify(id
);
805 string lockbox_str
= "client.osd-lockbox." + stringify(uuid
);
807 if (!cephx_entity
.from_str(cephx_str
)) {
808 dout(10) << __func__
<< " invalid cephx entity '"
809 << cephx_str
<< "'" << dendl
;
810 ss
<< "invalid cephx key entity '" << cephx_str
<< "'";
814 if (!lockbox_entity
.from_str(lockbox_str
)) {
815 dout(10) << __func__
<< " invalid lockbox entity '"
816 << lockbox_str
<< "'" << dendl
;
817 ss
<< "invalid lockbox key entity '" << lockbox_str
<< "'";
821 if (!mon
->key_server
.contains(cephx_entity
) &&
822 !mon
->key_server
.contains(lockbox_entity
)) {
829 int AuthMonitor::do_osd_destroy(
830 const EntityName
& cephx_entity
,
831 const EntityName
& lockbox_entity
)
833 assert(paxos
->is_plugged());
835 dout(10) << __func__
<< " cephx " << cephx_entity
836 << " lockbox " << lockbox_entity
<< dendl
;
838 bool removed
= false;
840 int err
= remove_entity(cephx_entity
);
841 if (err
== -ENOENT
) {
842 dout(10) << __func__
<< " " << cephx_entity
<< " does not exist" << dendl
;
847 err
= remove_entity(lockbox_entity
);
848 if (err
== -ENOENT
) {
849 dout(10) << __func__
<< " " << lockbox_entity
<< " does not exist" << dendl
;
855 dout(10) << __func__
<< " entities do not exist -- no-op." << dendl
;
859 // given we have paxos plugged, this will not result in a proposal
860 // being triggered, but it will still be needed so that we get our
861 // pending state encoded into the paxos' pending transaction.
866 bufferlist
_encode_cap(const string
& cap
)
876 const map
<string
,bufferlist
>& caps
)
881 auth
.key
.decode_base64(key
);
882 } catch (buffer::error
& e
) {
889 int AuthMonitor::validate_osd_new(
892 const string
& cephx_secret
,
893 const string
& lockbox_secret
,
894 auth_entity_t
& cephx_entity
,
895 auth_entity_t
& lockbox_entity
,
899 dout(10) << __func__
<< " osd." << id
<< " uuid " << uuid
<< dendl
;
901 map
<string
,bufferlist
> cephx_caps
= {
902 { "osd", _encode_cap("allow *") },
903 { "mon", _encode_cap("allow profile osd") },
904 { "mgr", _encode_cap("allow profile osd") }
906 map
<string
,bufferlist
> lockbox_caps
= {
907 { "mon", _encode_cap("allow command \"config-key get\" "
908 "with key=\"dm-crypt/osd/" +
913 bool has_lockbox
= !lockbox_secret
.empty();
915 string cephx_name
= "osd." + stringify(id
);
916 string lockbox_name
= "client.osd-lockbox." + stringify(uuid
);
918 if (!cephx_entity
.name
.from_str(cephx_name
)) {
919 dout(10) << __func__
<< " invalid cephx entity '"
920 << cephx_name
<< "'" << dendl
;
921 ss
<< "invalid cephx key entity '" << cephx_name
<< "'";
926 if (!lockbox_entity
.name
.from_str(lockbox_name
)) {
927 dout(10) << __func__
<< " invalid cephx lockbox entity '"
928 << lockbox_name
<< "'" << dendl
;
929 ss
<< "invalid cephx lockbox entity '" << lockbox_name
<< "'";
934 if (entity_is_pending(cephx_entity
.name
) ||
935 (has_lockbox
&& entity_is_pending(lockbox_entity
.name
))) {
936 // If we have pending entities for either the cephx secret or the
937 // lockbox secret, then our safest bet is to retry the command at
938 // a later time. These entities may be pending because an `osd new`
939 // command has been run (which is unlikely, due to the nature of
940 // the operation, which will force a paxos proposal), or (more likely)
941 // because a competing client created those entities before we handled
942 // the `osd new` command. Regardless, let's wait and see.
946 if (!is_valid_cephx_key(cephx_secret
)) {
947 ss
<< "invalid cephx secret.";
951 if (has_lockbox
&& !is_valid_cephx_key(lockbox_secret
)) {
952 ss
<< "invalid cephx lockbox secret.";
956 int err
= _create_auth(cephx_entity
.auth
, cephx_secret
, cephx_caps
);
959 bool cephx_is_idempotent
= false, lockbox_is_idempotent
= false;
960 err
= exists_and_matches_entity(cephx_entity
, true, ss
);
962 if (err
!= -ENOENT
) {
967 cephx_is_idempotent
= true;
971 err
= _create_auth(lockbox_entity
.auth
, lockbox_secret
, lockbox_caps
);
973 err
= exists_and_matches_entity(lockbox_entity
, true, ss
);
974 if (err
!= -ENOENT
) {
979 lockbox_is_idempotent
= true;
983 if (cephx_is_idempotent
&& (!has_lockbox
|| lockbox_is_idempotent
)) {
990 int AuthMonitor::do_osd_new(
991 const auth_entity_t
& cephx_entity
,
992 const auth_entity_t
& lockbox_entity
,
995 assert(paxos
->is_plugged());
997 dout(10) << __func__
<< " cephx " << cephx_entity
.name
1000 *_dout
<< lockbox_entity
.name
;
1006 // we must have validated before reaching this point.
1007 // if keys exist, then this means they also match; otherwise we would
1008 // have failed before calling this function.
1009 bool cephx_exists
= mon
->key_server
.contains(cephx_entity
.name
);
1011 if (!cephx_exists
) {
1012 int err
= add_entity(cephx_entity
.name
, cephx_entity
.auth
);
1017 !mon
->key_server
.contains(lockbox_entity
.name
)) {
1018 int err
= add_entity(lockbox_entity
.name
, lockbox_entity
.auth
);
1022 // given we have paxos plugged, this will not result in a proposal
1023 // being triggered, but it will still be needed so that we get our
1024 // pending state encoded into the paxos' pending transaction.
1029 bool AuthMonitor::prepare_command(MonOpRequestRef op
)
1031 MMonCommand
*m
= static_cast<MMonCommand
*>(op
->get_req());
1032 stringstream ss
, ds
;
1037 map
<string
, cmd_vartype
> cmdmap
;
1038 if (!cmdmap_from_json(m
->cmd
, &cmdmap
, ss
)) {
1039 // ss has reason for failure
1040 string rs
= ss
.str();
1041 mon
->reply_command(op
, -EINVAL
, rs
, rdata
, get_last_committed());
1046 vector
<string
>caps_vec
;
1050 cmd_getval(g_ceph_context
, cmdmap
, "prefix", prefix
);
1053 cmd_getval(g_ceph_context
, cmdmap
, "format", format
, string("plain"));
1054 boost::scoped_ptr
<Formatter
> f(Formatter::create(format
));
1056 MonSession
*session
= m
->get_session();
1058 mon
->reply_command(op
, -EACCES
, "access denied", rdata
, get_last_committed());
1062 cmd_getval(g_ceph_context
, cmdmap
, "caps", caps_vec
);
1063 if ((caps_vec
.size() % 2) != 0) {
1064 ss
<< "bad capabilities request; odd number of arguments";
1069 cmd_getval(g_ceph_context
, cmdmap
, "entity", entity_name
);
1070 if (!entity_name
.empty() && !entity
.from_str(entity_name
)) {
1071 ss
<< "bad entity name";
1076 if (prefix
== "auth import") {
1077 bufferlist bl
= m
->get_data();
1078 if (bl
.length() == 0) {
1079 ss
<< "auth import: no data supplied";
1081 mon
->reply_command(op
, -EINVAL
, rs
, get_last_committed());
1084 bufferlist::iterator iter
= bl
.begin();
1087 ::decode(keyring
, iter
);
1088 } catch (const buffer::error
&ex
) {
1089 ss
<< "error decoding keyring" << " " << ex
.what();
1093 err
= import_keyring(keyring
);
1095 ss
<< "auth import: no caps supplied";
1097 mon
->reply_command(op
, -EINVAL
, rs
, get_last_committed());
1100 ss
<< "imported keyring";
1103 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1104 get_last_committed() + 1));
1106 } else if (prefix
== "auth add" && !entity_name
.empty()) {
1107 /* expected behavior:
1108 * - if command reproduces current state, return 0.
1109 * - if command adds brand new entity, handle it.
1110 * - if command adds new state to existing entity, return error.
1112 KeyServerData::Incremental auth_inc
;
1113 auth_inc
.name
= entity
;
1114 bufferlist bl
= m
->get_data();
1115 bool has_keyring
= (bl
.length() > 0);
1116 map
<string
,bufferlist
> new_caps
;
1118 KeyRing new_keyring
;
1120 bufferlist::iterator iter
= bl
.begin();
1122 ::decode(new_keyring
, iter
);
1123 } catch (const buffer::error
&ex
) {
1124 ss
<< "error decoding keyring";
1130 // are we about to have it?
1131 if (entity_is_pending(entity
)) {
1132 wait_for_finished_proposal(op
,
1133 new Monitor::C_Command(mon
, op
, 0, rs
, get_last_committed() + 1));
1137 // build new caps from provided arguments (if available)
1138 for (vector
<string
>::iterator it
= caps_vec
.begin();
1139 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1143 ::encode(*(it
+1), cap
);
1144 new_caps
[sys
] = cap
;
1147 // pull info out of provided keyring
1150 if (!new_keyring
.get_auth(auth_inc
.name
, new_inc
)) {
1151 ss
<< "key for " << auth_inc
.name
1152 << " not found in provided keyring";
1156 if (!new_caps
.empty() && !new_inc
.caps
.empty()) {
1157 ss
<< "caps cannot be specified both in keyring and in command";
1161 if (new_caps
.empty()) {
1162 new_caps
= new_inc
.caps
;
1166 err
= exists_and_matches_entity(auth_inc
.name
, new_inc
,
1167 new_caps
, has_keyring
, ss
);
1168 // if entity/key/caps do not exist in the keyring, just fall through
1169 // and add the entity; otherwise, make sure everything matches (in
1170 // which case it's a no-op), because if not we must fail.
1171 if (err
!= -ENOENT
) {
1183 dout(10) << "AuthMonitor::prepare_command generating random key for "
1184 << auth_inc
.name
<< dendl
;
1185 new_inc
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1187 new_inc
.caps
= new_caps
;
1189 err
= add_entity(auth_inc
.name
, new_inc
);
1192 ss
<< "added key for " << auth_inc
.name
;
1194 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1195 get_last_committed() + 1));
1197 } else if ((prefix
== "auth get-or-create-key" ||
1198 prefix
== "auth get-or-create") &&
1199 !entity_name
.empty()) {
1200 // auth get-or-create <name> [mon osdcapa osd osdcapb ...]
1202 if (!valid_caps(caps_vec
, &ss
)) {
1207 // Parse the list of caps into a map
1208 std::map
<std::string
, bufferlist
> wanted_caps
;
1209 for (vector
<string
>::const_iterator it
= caps_vec
.begin();
1210 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1212 const std::string
&sys
= *it
;
1214 ::encode(*(it
+1), cap
);
1215 wanted_caps
[sys
] = cap
;
1219 EntityAuth entity_auth
;
1220 if (mon
->key_server
.get_auth(entity
, entity_auth
)) {
1221 for (const auto &sys_cap
: wanted_caps
) {
1222 if (entity_auth
.caps
.count(sys_cap
.first
) == 0 ||
1223 !entity_auth
.caps
[sys_cap
.first
].contents_equal(sys_cap
.second
)) {
1224 ss
<< "key for " << entity
<< " exists but cap " << sys_cap
.first
1225 << " does not match";
1231 if (prefix
== "auth get-or-create-key") {
1233 entity_auth
.key
.encode_formatted("auth", f
.get(), rdata
);
1235 ds
<< entity_auth
.key
;
1239 kr
.add(entity
, entity_auth
.key
);
1241 kr
.set_caps(entity
, entity_auth
.caps
);
1242 kr
.encode_formatted("auth", f
.get(), rdata
);
1244 kr
.encode_plaintext(rdata
);
1251 // ...or are we about to?
1252 for (vector
<Incremental
>::iterator p
= pending_auth
.begin();
1253 p
!= pending_auth
.end();
1255 if (p
->inc_type
== AUTH_DATA
) {
1256 KeyServerData::Incremental auth_inc
;
1257 bufferlist::iterator q
= p
->auth_data
.begin();
1258 ::decode(auth_inc
, q
);
1259 if (auth_inc
.op
== KeyServerData::AUTH_INC_ADD
&&
1260 auth_inc
.name
== entity
) {
1261 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1262 get_last_committed() + 1));
1269 KeyServerData::Incremental auth_inc
;
1270 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1271 auth_inc
.name
= entity
;
1272 auth_inc
.auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1273 auth_inc
.auth
.caps
= wanted_caps
;
1275 push_cephx_inc(auth_inc
);
1277 if (prefix
== "auth get-or-create-key") {
1279 auth_inc
.auth
.key
.encode_formatted("auth", f
.get(), rdata
);
1281 ds
<< auth_inc
.auth
.key
;
1285 kr
.add(entity
, auth_inc
.auth
.key
);
1287 kr
.set_caps(entity
, wanted_caps
);
1288 kr
.encode_formatted("auth", f
.get(), rdata
);
1290 kr
.encode_plaintext(rdata
);
1296 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
, rdata
,
1297 get_last_committed() + 1));
1299 } else if (prefix
== "fs authorize") {
1301 cmd_getval(g_ceph_context
, cmdmap
, "filesystem", filesystem
);
1302 string mds_cap_string
, osd_cap_string
;
1303 string osd_cap_wanted
= "r";
1305 for (auto it
= caps_vec
.begin();
1306 it
!= caps_vec
.end() && (it
+ 1) != caps_vec
.end();
1308 const string
&path
= *it
;
1309 const string
&cap
= *(it
+1);
1310 if (cap
!= "r" && cap
!= "rw" && cap
!= "rwp") {
1311 ss
<< "Only 'r', 'rw', and 'rwp' permissions are allowed for filesystems.";
1315 if (cap
.find('w') != string::npos
) {
1316 osd_cap_wanted
= "rw";
1319 mds_cap_string
+= mds_cap_string
.empty() ? "" : ", ";
1320 mds_cap_string
+= "allow " + cap
;
1322 mds_cap_string
+= " path=" + path
;
1326 auto fs
= mon
->mdsmon()->get_fsmap().get_filesystem(filesystem
);
1328 ss
<< "filesystem " << filesystem
<< " does not exist.";
1333 auto data_pools
= fs
->mds_map
.get_data_pools();
1334 for (auto p
: data_pools
) {
1335 const string
&pool_name
= mon
->osdmon()->osdmap
.get_pool_name(p
);
1336 osd_cap_string
+= osd_cap_string
.empty() ? "" : ", ";
1337 osd_cap_string
+= "allow " + osd_cap_wanted
+ " pool=" + pool_name
;
1340 std::map
<string
, bufferlist
> wanted_caps
= {
1341 { "mon", _encode_cap("allow r") },
1342 { "osd", _encode_cap(osd_cap_string
) },
1343 { "mds", _encode_cap(mds_cap_string
) }
1346 EntityAuth entity_auth
;
1347 if (mon
->key_server
.get_auth(entity
, entity_auth
)) {
1348 for (const auto &sys_cap
: wanted_caps
) {
1349 if (entity_auth
.caps
.count(sys_cap
.first
) == 0 ||
1350 !entity_auth
.caps
[sys_cap
.first
].contents_equal(sys_cap
.second
)) {
1351 ss
<< entity
<< " already has fs capabilities that differ from those supplied. To generate a new auth key for "
1352 << entity
<< ", first remove " << entity
<< " from configuration files, execute 'ceph auth rm " << entity
<< "', then execute this command again.";
1359 kr
.add(entity
, entity_auth
.key
);
1361 kr
.set_caps(entity
, entity_auth
.caps
);
1362 kr
.encode_formatted("auth", f
.get(), rdata
);
1364 kr
.encode_plaintext(rdata
);
1370 KeyServerData::Incremental auth_inc
;
1371 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1372 auth_inc
.name
= entity
;
1373 auth_inc
.auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1374 auth_inc
.auth
.caps
= wanted_caps
;
1376 push_cephx_inc(auth_inc
);
1378 kr
.add(entity
, auth_inc
.auth
.key
);
1380 kr
.set_caps(entity
, wanted_caps
);
1381 kr
.encode_formatted("auth", f
.get(), rdata
);
1383 kr
.encode_plaintext(rdata
);
1388 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
, rdata
,
1389 get_last_committed() + 1));
1391 } else if (prefix
== "auth caps" && !entity_name
.empty()) {
1392 KeyServerData::Incremental auth_inc
;
1393 auth_inc
.name
= entity
;
1394 if (!mon
->key_server
.get_auth(auth_inc
.name
, auth_inc
.auth
)) {
1395 ss
<< "couldn't find entry " << auth_inc
.name
;
1400 if (!valid_caps(caps_vec
, &ss
)) {
1405 map
<string
,bufferlist
> newcaps
;
1406 for (vector
<string
>::iterator it
= caps_vec
.begin();
1407 it
!= caps_vec
.end(); it
+= 2)
1408 ::encode(*(it
+1), newcaps
[*it
]);
1410 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1411 auth_inc
.auth
.caps
= newcaps
;
1412 push_cephx_inc(auth_inc
);
1414 ss
<< "updated caps for " << auth_inc
.name
;
1416 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1417 get_last_committed() + 1));
1419 } else if ((prefix
== "auth del" || prefix
== "auth rm") &&
1420 !entity_name
.empty()) {
1421 KeyServerData::Incremental auth_inc
;
1422 auth_inc
.name
= entity
;
1423 if (!mon
->key_server
.contains(auth_inc
.name
)) {
1424 ss
<< "entity " << entity
<< " does not exist";
1428 auth_inc
.op
= KeyServerData::AUTH_INC_DEL
;
1429 push_cephx_inc(auth_inc
);
1433 wait_for_finished_proposal(op
, new Monitor::C_Command(mon
, op
, 0, rs
,
1434 get_last_committed() + 1));
1439 getline(ss
, rs
, '\0');
1440 mon
->reply_command(op
, err
, rs
, rdata
, get_last_committed());
1444 bool AuthMonitor::prepare_global_id(MonOpRequestRef op
)
1446 dout(10) << "AuthMonitor::prepare_global_id" << dendl
;
1447 increase_max_global_id();
1452 void AuthMonitor::upgrade_format()
1454 unsigned int current
= 2;
1455 if (!mon
->get_quorum_mon_features().contains_all(
1456 ceph::features::mon::FEATURE_LUMINOUS
)) {
1459 if (format_version
>= current
) {
1460 dout(20) << __func__
<< " format " << format_version
<< " is current" << dendl
;
1464 bool changed
= false;
1465 if (format_version
== 0) {
1466 dout(1) << __func__
<< " upgrading from format 0 to 1" << dendl
;
1467 map
<EntityName
, EntityAuth
>::iterator p
;
1468 for (p
= mon
->key_server
.secrets_begin();
1469 p
!= mon
->key_server
.secrets_end();
1471 // grab mon caps, if any
1473 if (p
->second
.caps
.count("mon") == 0)
1476 bufferlist::iterator it
= p
->second
.caps
["mon"].begin();
1477 ::decode(mon_caps
, it
);
1479 catch (buffer::error
) {
1480 dout(10) << __func__
<< " unable to parse mon cap for "
1481 << p
->first
<< dendl
;
1485 string n
= p
->first
.to_str();
1488 // set daemon profiles
1489 if ((p
->first
.is_osd() || p
->first
.is_mds()) &&
1490 mon_caps
== "allow rwx") {
1491 new_caps
= string("allow profile ") + string(p
->first
.get_type_name());
1494 // update bootstrap keys
1495 if (n
== "client.bootstrap-osd") {
1496 new_caps
= "allow profile bootstrap-osd";
1498 if (n
== "client.bootstrap-mds") {
1499 new_caps
= "allow profile bootstrap-mds";
1502 if (new_caps
.length() > 0) {
1503 dout(5) << __func__
<< " updating " << p
->first
<< " mon cap from "
1504 << mon_caps
<< " to " << new_caps
<< dendl
;
1507 ::encode(new_caps
, bl
);
1509 KeyServerData::Incremental auth_inc
;
1510 auth_inc
.name
= p
->first
;
1511 auth_inc
.auth
= p
->second
;
1512 auth_inc
.auth
.caps
["mon"] = bl
;
1513 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1514 push_cephx_inc(auth_inc
);
1520 if (format_version
== 1) {
1521 dout(1) << __func__
<< " upgrading from format 1 to 2" << dendl
;
1522 map
<EntityName
, EntityAuth
>::iterator p
;
1523 for (p
= mon
->key_server
.secrets_begin();
1524 p
!= mon
->key_server
.secrets_end();
1526 string n
= p
->first
.to_str();
1529 if (n
== "client.admin") {
1530 // admin gets it all
1532 } else if (n
.find("osd.") == 0 ||
1533 n
.find("mds.") == 0 ||
1534 n
.find("mon.") == 0) {
1535 // daemons follow their profile
1536 string type
= n
.substr(0, 3);
1537 newcap
= "allow profile " + type
;
1538 } else if (p
->second
.caps
.count("mon")) {
1539 // if there are any mon caps, give them 'r' mgr caps
1543 if (newcap
.length() > 0) {
1544 dout(5) << " giving " << n
<< " mgr '" << newcap
<< "'" << dendl
;
1546 ::encode(newcap
, bl
);
1548 KeyServerData::Incremental auth_inc
;
1549 auth_inc
.name
= p
->first
;
1550 auth_inc
.auth
= p
->second
;
1551 auth_inc
.auth
.caps
["mgr"] = bl
;
1552 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1553 push_cephx_inc(auth_inc
);
1556 if (n
.find("mgr.") == 0 &&
1557 p
->second
.caps
.count("mon")) {
1558 // the kraken ceph-mgr@.service set the mon cap to 'allow *'.
1559 auto blp
= p
->second
.caps
["mon"].begin();
1561 ::decode(oldcaps
, blp
);
1562 if (oldcaps
== "allow *") {
1563 dout(5) << " fixing " << n
<< " mon cap to 'allow profile mgr'"
1566 ::encode("allow profile mgr", bl
);
1567 KeyServerData::Incremental auth_inc
;
1568 auth_inc
.name
= p
->first
;
1569 auth_inc
.auth
= p
->second
;
1570 auth_inc
.auth
.caps
["mon"] = bl
;
1571 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1572 push_cephx_inc(auth_inc
);
1577 // add bootstrap key if it does not already exist
1578 // (might have already been get-or-create'd by
1579 // ceph-create-keys)
1580 EntityName bootstrap_mgr_name
;
1581 int r
= bootstrap_mgr_name
.from_str("client.bootstrap-mgr");
1583 if (!mon
->key_server
.contains(bootstrap_mgr_name
)) {
1584 KeyServerData::Incremental auth_inc
;
1585 auth_inc
.name
= bootstrap_mgr_name
;
1586 ::encode("allow profile bootstrap-mgr", auth_inc
.auth
.caps
["mon"]);
1587 auth_inc
.op
= KeyServerData::AUTH_INC_ADD
;
1589 auth_inc
.auth
.key
.create(g_ceph_context
, CEPH_CRYPTO_AES
);
1590 push_cephx_inc(auth_inc
);
1597 dout(10) << __func__
<< " proposing update from format " << format_version
1598 << " -> " << current
<< dendl
;
1599 format_version
= current
;
1604 void AuthMonitor::dump_info(Formatter
*f
)
1606 /*** WARNING: do not include any privileged information here! ***/
1607 f
->open_object_section("auth");
1608 f
->dump_unsigned("first_committed", get_first_committed());
1609 f
->dump_unsigned("last_committed", get_last_committed());
1610 f
->dump_unsigned("num_secrets", mon
->key_server
.get_num_secrets());