]>
git.proxmox.com Git - ceph.git/blob - ceph/src/mon/MonCap.cc
b5c5ab6a5bf32763d31558f13c296d20fe7a916a
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) 2013 Inktank
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.
15 #include <boost/config/warning_disable.hpp>
16 #include <boost/spirit/include/qi_uint.hpp>
17 #include <boost/spirit/include/qi.hpp>
18 #include <boost/fusion/include/std_pair.hpp>
19 #include <boost/spirit/include/phoenix.hpp>
20 #include <boost/fusion/adapted/struct/adapt_struct.hpp>
21 #include <boost/fusion/include/adapt_struct.hpp>
22 #include <boost/algorithm/string/predicate.hpp>
25 #include "include/stringify.h"
26 #include "include/ipaddr.h"
27 #include "common/debug.h"
28 #include "common/Formatter.h"
33 #include "include/ceph_assert.h"
42 using ceph::bufferlist
;
43 using ceph::Formatter
;
45 static inline bool is_not_alnum_space(char c
)
47 return !(isalpha(c
) || isdigit(c
) || (c
== '-') || (c
== '_'));
50 static std::string
maybe_quote_string(const std::string
& str
)
52 if (find_if(str
.begin(), str
.end(), is_not_alnum_space
) == str
.end())
54 return string("\"") + str
+ string("\"");
57 #define dout_subsys ceph_subsys_mon
59 ostream
& operator<<(ostream
& out
, const mon_rwxa_t
& p
)
73 ostream
& operator<<(ostream
& out
, const StringConstraint
& c
)
75 switch (c
.match_type
) {
76 case StringConstraint::MATCH_TYPE_EQUAL
:
77 return out
<< "value " << c
.value
;
78 case StringConstraint::MATCH_TYPE_PREFIX
:
79 return out
<< "prefix " << c
.value
;
80 case StringConstraint::MATCH_TYPE_REGEX
:
81 return out
<< "regex " << c
.value
;
88 ostream
& operator<<(ostream
& out
, const MonCapGrant
& m
)
91 if (m
.service
.length()) {
92 out
<< " service " << maybe_quote_string(m
.service
);
94 if (m
.command
.length()) {
95 out
<< " command " << maybe_quote_string(m
.command
);
96 if (!m
.command_args
.empty()) {
98 for (auto p
= m
.command_args
.begin();
99 p
!= m
.command_args
.end();
101 switch (p
->second
.match_type
) {
102 case StringConstraint::MATCH_TYPE_EQUAL
:
103 out
<< " " << maybe_quote_string(p
->first
) << "="
104 << maybe_quote_string(p
->second
.value
);
106 case StringConstraint::MATCH_TYPE_PREFIX
:
107 out
<< " " << maybe_quote_string(p
->first
) << " prefix "
108 << maybe_quote_string(p
->second
.value
);
110 case StringConstraint::MATCH_TYPE_REGEX
:
111 out
<< " " << maybe_quote_string(p
->first
) << " regex "
112 << maybe_quote_string(p
->second
.value
);
120 if (m
.profile
.length()) {
121 out
<< " profile " << maybe_quote_string(m
.profile
);
124 out
<< " " << m
.allow
;
125 if (m
.network
.size())
126 out
<< " network " << m
.network
;
132 // fusion lets us easily populate structs via the qi parser.
134 typedef map
<string
,StringConstraint
> kvmap
;
136 BOOST_FUSION_ADAPT_STRUCT(MonCapGrant
,
137 (std::string
, service
)
138 (std::string
, profile
)
139 (std::string
, command
)
140 (kvmap
, command_args
)
142 (std::string
, network
))
144 BOOST_FUSION_ADAPT_STRUCT(StringConstraint
,
145 (StringConstraint::MatchType
, match_type
)
146 (std::string
, value
))
150 void MonCapGrant::parse_network()
152 network_valid
= ::parse_network(network
.c_str(), &network_parsed
,
156 void MonCapGrant::expand_profile(const EntityName
& name
) const
158 // only generate this list once
159 if (!profile_grants
.empty())
162 if (profile
== "read-only") {
163 // grants READ-ONLY caps monitor-wide
164 // 'auth' requires MON_CAP_X even for RO, which we do not grant here.
165 profile_grants
.push_back(mon_rwxa_t(MON_CAP_R
));
169 if (profile
== "read-write") {
170 // grants READ-WRITE caps monitor-wide
171 // 'auth' requires MON_CAP_X for all operations, which we do not grant.
172 profile_grants
.push_back(mon_rwxa_t(MON_CAP_R
| MON_CAP_W
));
176 if (profile
== "mon") {
177 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_ALL
));
178 profile_grants
.push_back(MonCapGrant("log", MON_CAP_ALL
));
180 if (profile
== "osd") {
181 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_ALL
));
182 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
));
183 profile_grants
.push_back(MonCapGrant("pg", MON_CAP_R
| MON_CAP_W
));
184 profile_grants
.push_back(MonCapGrant("log", MON_CAP_W
));
186 if (profile
== "mds") {
187 profile_grants
.push_back(MonCapGrant("mds", MON_CAP_ALL
));
188 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
));
189 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
));
190 // This command grant is checked explicitly in MRemoveSnaps handling
191 profile_grants
.push_back(MonCapGrant("osd pool rmsnap"));
192 profile_grants
.push_back(MonCapGrant("osd blacklist"));
193 profile_grants
.push_back(MonCapGrant("log", MON_CAP_W
));
195 if (profile
== "mgr") {
196 profile_grants
.push_back(MonCapGrant("mgr", MON_CAP_ALL
));
197 profile_grants
.push_back(MonCapGrant("log", MON_CAP_R
| MON_CAP_W
));
198 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
| MON_CAP_W
));
199 profile_grants
.push_back(MonCapGrant("mds", MON_CAP_R
| MON_CAP_W
));
200 profile_grants
.push_back(MonCapGrant("fs", MON_CAP_R
| MON_CAP_W
));
201 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
| MON_CAP_W
));
202 profile_grants
.push_back(MonCapGrant("auth", MON_CAP_R
| MON_CAP_X
));
203 profile_grants
.push_back(MonCapGrant("config-key", MON_CAP_R
| MON_CAP_W
));
204 profile_grants
.push_back(MonCapGrant("config", MON_CAP_R
| MON_CAP_W
));
205 // cephadm orchestrator provisions new daemon keys and updates caps
206 profile_grants
.push_back(MonCapGrant("auth get-or-create"));
207 profile_grants
.push_back(MonCapGrant("auth caps"));
208 profile_grants
.push_back(MonCapGrant("auth rm"));
209 // tell commands (this is a bit of a kludge)
210 profile_grants
.push_back(MonCapGrant("smart"));
212 if (profile
== "osd" || profile
== "mds" || profile
== "mon" ||
214 StringConstraint
constraint(StringConstraint::MATCH_TYPE_PREFIX
,
215 string("daemon-private/") + stringify(name
) +
217 std::string prefix
= string("daemon-private/") + stringify(name
) + string("/");
218 profile_grants
.push_back(MonCapGrant("config-key get", "key", constraint
));
219 profile_grants
.push_back(MonCapGrant("config-key put", "key", constraint
));
220 profile_grants
.push_back(MonCapGrant("config-key set", "key", constraint
));
221 profile_grants
.push_back(MonCapGrant("config-key exists", "key", constraint
));
222 profile_grants
.push_back(MonCapGrant("config-key delete", "key", constraint
));
224 if (profile
== "bootstrap-osd") {
225 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
)); // read monmap
226 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
)); // read osdmap
227 profile_grants
.push_back(MonCapGrant("mon getmap"));
228 profile_grants
.push_back(MonCapGrant("osd new"));
229 profile_grants
.push_back(MonCapGrant("osd purge-new"));
231 if (profile
== "bootstrap-mds") {
232 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
)); // read monmap
233 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
)); // read osdmap
234 profile_grants
.push_back(MonCapGrant("mon getmap"));
235 profile_grants
.push_back(MonCapGrant("auth get-or-create")); // FIXME: this can expose other mds keys
236 profile_grants
.back().command_args
["entity"] = StringConstraint(
237 StringConstraint::MATCH_TYPE_PREFIX
, "mds.");
238 profile_grants
.back().command_args
["caps_mon"] = StringConstraint(
239 StringConstraint::MATCH_TYPE_EQUAL
, "allow profile mds");
240 profile_grants
.back().command_args
["caps_osd"] = StringConstraint(
241 StringConstraint::MATCH_TYPE_EQUAL
, "allow rwx");
242 profile_grants
.back().command_args
["caps_mds"] = StringConstraint(
243 StringConstraint::MATCH_TYPE_EQUAL
, "allow");
245 if (profile
== "bootstrap-mgr") {
246 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
)); // read monmap
247 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
)); // read osdmap
248 profile_grants
.push_back(MonCapGrant("mon getmap"));
249 profile_grants
.push_back(MonCapGrant("auth get-or-create")); // FIXME: this can expose other mgr keys
250 profile_grants
.back().command_args
["entity"] = StringConstraint(
251 StringConstraint::MATCH_TYPE_PREFIX
, "mgr.");
252 profile_grants
.back().command_args
["caps_mon"] = StringConstraint(
253 StringConstraint::MATCH_TYPE_EQUAL
, "allow profile mgr");
255 if (profile
== "bootstrap-rgw") {
256 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
)); // read monmap
257 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
)); // read osdmap
258 profile_grants
.push_back(MonCapGrant("mon getmap"));
259 profile_grants
.push_back(MonCapGrant("auth get-or-create")); // FIXME: this can expose other mds keys
260 profile_grants
.back().command_args
["entity"] = StringConstraint(
261 StringConstraint::MATCH_TYPE_PREFIX
, "client.rgw.");
262 profile_grants
.back().command_args
["caps_mon"] = StringConstraint(
263 StringConstraint::MATCH_TYPE_EQUAL
, "allow rw");
264 profile_grants
.back().command_args
["caps_osd"] = StringConstraint(
265 StringConstraint::MATCH_TYPE_EQUAL
, "allow rwx");
267 if (profile
== "bootstrap-rbd" || profile
== "bootstrap-rbd-mirror") {
268 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
)); // read monmap
269 profile_grants
.push_back(MonCapGrant("auth get-or-create")); // FIXME: this can expose other rbd keys
270 profile_grants
.back().command_args
["entity"] = StringConstraint(
271 StringConstraint::MATCH_TYPE_PREFIX
, "client.");
272 profile_grants
.back().command_args
["caps_mon"] = StringConstraint(
273 StringConstraint::MATCH_TYPE_EQUAL
,
274 (profile
== "bootstrap-rbd-mirror" ? "profile rbd-mirror" :
276 profile_grants
.back().command_args
["caps_osd"] = StringConstraint(
277 StringConstraint::MATCH_TYPE_REGEX
,
278 "^([ ,]*profile(=|[ ]+)['\"]?rbd[^ ,'\"]*['\"]?([ ]+pool(=|[ ]+)['\"]?[^,'\"]+['\"]?)?)+$");
280 if (profile
== "fs-client") {
281 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
));
282 profile_grants
.push_back(MonCapGrant("mds", MON_CAP_R
));
283 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
));
284 profile_grants
.push_back(MonCapGrant("pg", MON_CAP_R
));
286 if (profile
== "simple-rados-client") {
287 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
));
288 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
));
289 profile_grants
.push_back(MonCapGrant("pg", MON_CAP_R
));
291 if (boost::starts_with(profile
, "rbd")) {
292 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
));
293 profile_grants
.push_back(MonCapGrant("osd", MON_CAP_R
));
294 profile_grants
.push_back(MonCapGrant("pg", MON_CAP_R
));
296 // exclusive lock dead-client blacklisting (IP+nonce required)
297 profile_grants
.push_back(MonCapGrant("osd blacklist"));
298 profile_grants
.back().command_args
["blacklistop"] = StringConstraint(
299 StringConstraint::MATCH_TYPE_EQUAL
, "add");
300 profile_grants
.back().command_args
["addr"] = StringConstraint(
301 StringConstraint::MATCH_TYPE_REGEX
, "^[^/]+/[0-9]+$");
304 if (profile
== "rbd-mirror") {
305 StringConstraint
constraint(StringConstraint::MATCH_TYPE_PREFIX
,
307 profile_grants
.push_back(MonCapGrant("config-key get", "key", constraint
));
308 } else if (profile
== "rbd-mirror-peer") {
309 StringConstraint
constraint(StringConstraint::MATCH_TYPE_REGEX
,
311 profile_grants
.push_back(MonCapGrant("config-key get", "key", constraint
));
313 constraint
= StringConstraint(StringConstraint::MATCH_TYPE_PREFIX
,
315 profile_grants
.push_back(MonCapGrant("config-key set", "key", constraint
));
317 else if (profile
== "crash") {
318 // TODO: we could limit this to getting the monmap and mgrmap...
319 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
));
321 if (profile
== "role-definer") {
322 // grants ALL caps to the auth subsystem, read-only on the
323 // monitor subsystem and nothing else.
324 profile_grants
.push_back(MonCapGrant("mon", MON_CAP_R
));
325 profile_grants
.push_back(MonCapGrant("auth", MON_CAP_ALL
));
329 mon_rwxa_t
MonCapGrant::get_allowed(CephContext
*cct
,
331 const std::string
& s
, const std::string
& c
,
332 const map
<string
,string
>& c_args
) const
334 if (profile
.length()) {
335 expand_profile(name
);
337 for (auto p
= profile_grants
.begin();
338 p
!= profile_grants
.end(); ++p
)
339 a
= a
| p
->get_allowed(cct
, name
, s
, c
, c_args
);
342 if (service
.length()) {
347 if (command
.length()) {
350 for (map
<string
,StringConstraint
>::const_iterator p
= command_args
.begin(); p
!= command_args
.end(); ++p
) {
351 map
<string
,string
>::const_iterator q
= c_args
.find(p
->first
);
352 // argument must be present if a constraint exists
353 if (q
== c_args
.end())
355 switch (p
->second
.match_type
) {
356 case StringConstraint::MATCH_TYPE_EQUAL
:
357 if (p
->second
.value
!= q
->second
)
360 case StringConstraint::MATCH_TYPE_PREFIX
:
361 if (q
->second
.find(p
->second
.value
) != 0)
364 case StringConstraint::MATCH_TYPE_REGEX
:
367 p
->second
.value
, std::regex::extended
);
368 if (!std::regex_match(q
->second
, pattern
))
370 } catch(const std::regex_error
&) {
380 // we don't allow config-key service to be accessed with blanket caps other
381 // than '*' (i.e., 'any'), and that should have been checked by the caller
382 // via 'is_allow_all()'.
383 if (s
== "config-key") {
389 ostream
& operator<<(ostream
&out
, const MonCap
& m
)
391 for (vector
<MonCapGrant
>::const_iterator p
= m
.grants
.begin(); p
!= m
.grants
.end(); ++p
) {
392 if (p
!= m
.grants
.begin())
399 bool MonCap::is_allow_all() const
401 for (vector
<MonCapGrant
>::const_iterator p
= grants
.begin(); p
!= grants
.end(); ++p
)
402 if (p
->is_allow_all())
407 void MonCap::set_allow_all()
410 grants
.push_back(MonCapGrant(MON_CAP_ANY
));
414 bool MonCap::is_capable(
417 const string
& service
,
418 const string
& command
, const map
<string
,string
>& command_args
,
419 bool op_may_read
, bool op_may_write
, bool op_may_exec
,
420 const entity_addr_t
& addr
) const
423 ldout(cct
, 20) << "is_capable service=" << service
<< " command=" << command
424 << (op_may_read
? " read":"")
425 << (op_may_write
? " write":"")
426 << (op_may_exec
? " exec":"")
428 << " on cap " << *this
431 mon_rwxa_t allow
= 0;
432 for (vector
<MonCapGrant
>::const_iterator p
= grants
.begin();
433 p
!= grants
.end(); ++p
) {
435 ldout(cct
, 20) << " allow so far " << allow
<< ", doing grant " << *p
438 if (p
->network
.size() &&
439 (!p
->network_valid
||
440 !network_contains(p
->network_parsed
,
446 if (p
->is_allow_all()) {
448 ldout(cct
, 20) << " allow all" << dendl
;
452 // check enumerated caps
453 allow
= allow
| p
->get_allowed(cct
, name
, service
, command
, command_args
);
454 if ((!op_may_read
|| (allow
& MON_CAP_R
)) &&
455 (!op_may_write
|| (allow
& MON_CAP_W
)) &&
456 (!op_may_exec
|| (allow
& MON_CAP_X
))) {
458 ldout(cct
, 20) << " match" << dendl
;
465 void MonCap::encode(bufferlist
& bl
) const
467 ENCODE_START(4, 4, bl
); // legacy MonCaps was 3, 3
472 void MonCap::decode(bufferlist::const_iterator
& bl
)
481 void MonCap::dump(Formatter
*f
) const
483 f
->dump_string("text", text
);
486 void MonCap::generate_test_instances(list
<MonCap
*>& ls
)
488 ls
.push_back(new MonCap
);
489 ls
.push_back(new MonCap
);
490 ls
.back()->parse("allow *");
491 ls
.push_back(new MonCap
);
492 ls
.back()->parse("allow rwx");
493 ls
.push_back(new MonCap
);
494 ls
.back()->parse("allow service foo x");
495 ls
.push_back(new MonCap
);
496 ls
.back()->parse("allow command bar x");
497 ls
.push_back(new MonCap
);
498 ls
.back()->parse("allow service foo r, allow command bar x");
499 ls
.push_back(new MonCap
);
500 ls
.back()->parse("allow command bar with k1=v1 x");
501 ls
.push_back(new MonCap
);
502 ls
.back()->parse("allow command bar with k1=v1 k2=v2 x");
506 namespace qi
= boost::spirit::qi
;
507 namespace ascii
= boost::spirit::ascii
;
508 namespace phoenix
= boost::phoenix
;
511 template <typename Iterator
>
512 struct MonCapParser
: qi::grammar
<Iterator
, MonCap()>
514 MonCapParser() : MonCapParser::base_type(moncap
)
518 using qi::ulong_long
;
529 lexeme
['"' >> +(char_
- '"') >> '"'] |
530 lexeme
['\'' >> +(char_
- '\'') >> '\''];
531 unquoted_word
%= +char_("a-zA-Z0-9_./-");
532 str
%= quoted_string
| unquoted_word
;
533 network_str
%= +char_("/.:a-fA-F0-9][");
535 spaces
= +(lit(' ') | lit('\n') | lit('\t'));
537 // command := command[=]cmd [k1=v1 k2=v2 ...]
538 str_match
= '=' >> qi::attr(StringConstraint::MATCH_TYPE_EQUAL
) >> str
;
539 str_prefix
= spaces
>> lit("prefix") >> spaces
>>
540 qi::attr(StringConstraint::MATCH_TYPE_PREFIX
) >> str
;
541 str_regex
= spaces
>> lit("regex") >> spaces
>>
542 qi::attr(StringConstraint::MATCH_TYPE_REGEX
) >> str
;
543 kv_pair
= str
>> (str_match
| str_prefix
| str_regex
);
544 kv_map
%= kv_pair
>> *(spaces
>> kv_pair
);
545 command_match
= -spaces
>> lit("allow") >> spaces
>> lit("command") >> (lit('=') | spaces
)
546 >> qi::attr(string()) >> qi::attr(string())
548 >> -(spaces
>> lit("with") >> spaces
>> kv_map
)
550 >> -(spaces
>> lit("network") >> spaces
>> network_str
);
553 service_match
%= -spaces
>> lit("allow") >> spaces
>> lit("service") >> (lit('=') | spaces
)
554 >> str
>> qi::attr(string()) >> qi::attr(string())
555 >> qi::attr(map
<string
,StringConstraint
>())
557 >> -(spaces
>> lit("network") >> spaces
>> network_str
);
560 profile_match
%= -spaces
>> -(lit("allow") >> spaces
)
561 >> lit("profile") >> (lit('=') | spaces
)
562 >> qi::attr(string())
564 >> qi::attr(string())
565 >> qi::attr(map
<string
,StringConstraint
>())
567 >> -(spaces
>> lit("network") >> spaces
>> network_str
);
570 rwxa_match
%= -spaces
>> lit("allow") >> spaces
571 >> qi::attr(string()) >> qi::attr(string()) >> qi::attr(string())
572 >> qi::attr(map
<string
,StringConstraint
>())
574 >> -(spaces
>> lit("network") >> spaces
>> network_str
);
576 // rwxa := * | [r][w][x]
578 (lit("*")[_val
= MON_CAP_ANY
]) |
579 (lit("all")[_val
= MON_CAP_ANY
]) |
581 ( lit('r')[_val
|= MON_CAP_R
] ||
582 lit('w')[_val
|= MON_CAP_W
] ||
583 lit('x')[_val
|= MON_CAP_X
]
587 // grant := allow ...
588 grant
= -spaces
>> (rwxa_match
| profile_match
| service_match
| command_match
) >> -spaces
;
590 // moncap := grant [grant ...]
591 grants
%= (grant
% (*lit(' ') >> (lit(';') | lit(',')) >> *lit(' ')));
592 moncap
= grants
[_val
= phoenix::construct
<MonCap
>(_1
)];
595 qi::rule
<Iterator
> spaces
;
596 qi::rule
<Iterator
, unsigned()> rwxa
;
597 qi::rule
<Iterator
, string()> quoted_string
;
598 qi::rule
<Iterator
, string()> unquoted_word
;
599 qi::rule
<Iterator
, string()> str
, network_str
;
601 qi::rule
<Iterator
, StringConstraint()> str_match
, str_prefix
, str_regex
;
602 qi::rule
<Iterator
, pair
<string
, StringConstraint
>()> kv_pair
;
603 qi::rule
<Iterator
, map
<string
, StringConstraint
>()> kv_map
;
605 qi::rule
<Iterator
, MonCapGrant()> rwxa_match
;
606 qi::rule
<Iterator
, MonCapGrant()> command_match
;
607 qi::rule
<Iterator
, MonCapGrant()> service_match
;
608 qi::rule
<Iterator
, MonCapGrant()> profile_match
;
609 qi::rule
<Iterator
, MonCapGrant()> grant
;
610 qi::rule
<Iterator
, std::vector
<MonCapGrant
>()> grants
;
611 qi::rule
<Iterator
, MonCap()> moncap
;
614 bool MonCap::parse(const string
& str
, ostream
*err
)
616 auto iter
= str
.begin();
617 auto end
= str
.end();
619 MonCapParser
<string::const_iterator
> exp
;
620 bool r
= qi::parse(iter
, end
, exp
, *this);
621 if (r
&& iter
== end
) {
623 for (auto& g
: grants
) {
629 // Make sure no grants are kept after parsing failed!
634 *err
<< "mon capability parse failed, stopped at '"
635 << std::string(iter
, end
)
636 << "' of '" << str
<< "'";
638 *err
<< "mon capability parse failed, stopped at end of '" << str
<< "'";