]>
git.proxmox.com Git - ceph.git/blob - ceph/src/osd/OSDCap.cc
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) 2009-2011 New Dream Network
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.hpp>
17 #include <boost/spirit/include/phoenix_operator.hpp>
18 #include <boost/spirit/include/phoenix.hpp>
19 #include <boost/algorithm/string/predicate.hpp>
22 #include "common/config.h"
23 #include "common/debug.h"
24 #include "include/ipaddr.h"
30 ostream
& operator<<(ostream
& out
, const osd_rwxa_t
& p
)
39 if ((p
& OSD_CAP_X
) == OSD_CAP_X
) {
42 if (p
& OSD_CAP_CLS_R
)
44 if (p
& OSD_CAP_CLS_W
)
45 out
<< " class-write";
50 ostream
& operator<<(ostream
& out
, const OSDCapSpec
& s
)
53 return out
<< s
.allow
;
54 if (s
.class_name
.length()) {
55 out
<< "class '" << s
.class_name
<< "'";
56 if (!s
.method_name
.empty()) {
57 out
<< " '" << s
.method_name
<< "'";
63 ostream
& operator<<(ostream
& out
, const OSDCapPoolNamespace
& pns
)
65 if (!pns
.pool_name
.empty()) {
66 out
<< "pool " << pns
.pool_name
<< " ";
70 if (pns
.nspace
->empty()) {
80 ostream
& operator<<(ostream
&out
, const OSDCapPoolTag
&pt
)
82 out
<< "app " << pt
.application
<< " key " << pt
.key
<< " val " << pt
.value
87 ostream
& operator<<(ostream
& out
, const OSDCapMatch
& m
)
89 if (!m
.pool_namespace
.pool_name
.empty() || m
.pool_namespace
.nspace
) {
90 out
<< m
.pool_namespace
;
93 if (!m
.pool_tag
.application
.empty()) {
97 if (m
.object_prefix
.length()) {
98 out
<< "object_prefix " << m
.object_prefix
<< " ";
103 ostream
& operator<<(ostream
& out
, const OSDCapProfile
& m
)
105 out
<< "profile " << m
.name
;
106 out
<< m
.pool_namespace
;
110 bool OSDCapPoolNamespace::is_match(const std::string
& pn
,
111 const std::string
& ns
) const
113 if (!pool_name
.empty()) {
114 if (pool_name
!= pn
) {
119 if (!nspace
->empty() && nspace
->back() == '*' &&
120 boost::starts_with(ns
, nspace
->substr(0, nspace
->length() - 1))) {
131 bool OSDCapPoolNamespace::is_match_all() const
133 if (!pool_name
.empty())
140 bool OSDCapPoolTag::is_match(const app_map_t
& app_map
) const
142 if (application
.empty()) {
145 auto kv_map
= app_map
.find(application
);
146 if (kv_map
== app_map
.end()) {
149 if (!key
.compare("*") && !value
.compare("*")) {
152 if (!key
.compare("*")) {
153 for (auto it
: kv_map
->second
) {
154 if (it
.second
== value
) {
160 auto kv_val
= kv_map
->second
.find(key
);
161 if (kv_val
== kv_map
->second
.end()) {
164 if (!value
.compare("*")) {
167 return kv_val
->second
== value
;
170 bool OSDCapPoolTag::is_match_all() const {
171 return application
.empty();
174 bool OSDCapMatch::is_match(const string
& pn
, const string
& ns
,
175 const OSDCapPoolTag::app_map_t
& app_map
,
176 const string
& object
) const
178 if (!pool_namespace
.is_match(pn
, ns
)) {
180 } else if (!pool_tag
.is_match(app_map
)) {
184 if (object_prefix
.length()) {
185 if (object
.find(object_prefix
) != 0)
191 bool OSDCapMatch::is_match_all() const
193 if (!pool_namespace
.is_match_all()) {
195 } else if (!pool_tag
.is_match_all()) {
199 if (object_prefix
.length()) {
205 ostream
& operator<<(ostream
& out
, const OSDCapGrant
& g
)
208 if (g
.profile
.is_valid()) {
209 out
<< g
.profile
<< " [";
210 for (auto it
= g
.profile_grants
.cbegin();
211 it
!= g
.profile_grants
.cend(); ++it
) {
212 if (it
!= g
.profile_grants
.cbegin()) {
219 out
<< g
.match
<< g
.spec
;
221 if (g
.network
.size()) {
222 out
<< " network " << g
.network
;
228 void OSDCapGrant::set_network(const string
& n
)
231 network_valid
= ::parse_network(n
.c_str(), &network_parsed
, &network_prefix
);
234 bool OSDCapGrant::allow_all() const
236 if (profile
.is_valid()) {
237 return std::any_of(profile_grants
.cbegin(), profile_grants
.cend(),
238 [](const OSDCapGrant
& grant
) {
239 return grant
.allow_all();
243 return (match
.is_match_all() && spec
.allow_all());
246 bool OSDCapGrant::is_capable(
247 const string
& pool_name
,
249 const OSDCapPoolTag::app_map_t
& application_metadata
,
250 const string
& object
,
253 const std::vector
<OpInfo::ClassInfo
>& classes
,
254 const entity_addr_t
& addr
,
255 std::vector
<bool>* class_allowed
) const
257 osd_rwxa_t allow
= 0;
259 if (network
.size() &&
261 !network_contains(network_parsed
,
267 if (profile
.is_valid()) {
268 return std::any_of(profile_grants
.cbegin(), profile_grants
.cend(),
269 [&](const OSDCapGrant
& grant
) {
270 return grant
.is_capable(pool_name
, ns
,
271 application_metadata
,
273 op_may_write
, classes
, addr
,
277 if (match
.is_match(pool_name
, ns
, application_metadata
, object
)) {
278 allow
= allow
| spec
.allow
;
279 if ((op_may_read
&& !(allow
& OSD_CAP_R
)) ||
280 (op_may_write
&& !(allow
& OSD_CAP_W
))) {
283 if (!classes
.empty()) {
285 if (spec
.allow_all()) {
289 // compare this grant to each class in the operation
290 for (size_t i
= 0; i
< classes
.size(); ++i
) {
291 // check 'allow class foo [method_name]'
292 if (!spec
.class_name
.empty() &&
293 classes
[i
].class_name
== spec
.class_name
&&
294 (spec
.method_name
.empty() ||
295 classes
[i
].method_name
== spec
.method_name
)) {
296 (*class_allowed
)[i
] = true;
299 // check 'allow x | class-{rw}': must be on allow list
300 if (!classes
[i
].allowed
) {
303 if ((classes
[i
].read
&& !(allow
& OSD_CAP_CLS_R
)) ||
304 (classes
[i
].write
&& !(allow
& OSD_CAP_CLS_W
))) {
307 (*class_allowed
)[i
] = true;
309 if (!std::all_of(class_allowed
->cbegin(), class_allowed
->cend(),
310 [](bool v
) { return v
; })) {
320 void OSDCapGrant::expand_profile()
322 if (profile
.name
== "read-only") {
323 // grants READ-ONLY caps to the OSD
324 profile_grants
.emplace_back(OSDCapMatch(profile
.pool_namespace
),
325 OSDCapSpec(osd_rwxa_t(OSD_CAP_R
)));
328 if (profile
.name
== "read-write") {
329 // grants READ-WRITE caps to the OSD
330 profile_grants
.emplace_back(OSDCapMatch(profile
.pool_namespace
),
331 OSDCapSpec(osd_rwxa_t(OSD_CAP_R
| OSD_CAP_W
)));
334 if (profile
.name
== "rbd") {
335 // RBD read-write grant
336 profile_grants
.emplace_back(OSDCapMatch(string(), "rbd_info"),
337 OSDCapSpec(osd_rwxa_t(OSD_CAP_R
)));
338 profile_grants
.emplace_back(OSDCapMatch(string(), "rbd_children"),
339 OSDCapSpec(osd_rwxa_t(OSD_CAP_CLS_R
)));
340 profile_grants
.emplace_back(OSDCapMatch(string(), "rbd_mirroring"),
341 OSDCapSpec(osd_rwxa_t(OSD_CAP_CLS_R
)));
342 profile_grants
.emplace_back(OSDCapMatch(profile
.pool_namespace
.pool_name
),
343 OSDCapSpec("rbd", "metadata_list"));
344 profile_grants
.emplace_back(OSDCapMatch(profile
.pool_namespace
),
345 OSDCapSpec(osd_rwxa_t(OSD_CAP_R
|
349 if (profile
.name
== "rbd-read-only") {
350 // RBD read-only grant
351 profile_grants
.emplace_back(OSDCapMatch(profile
.pool_namespace
),
352 OSDCapSpec(osd_rwxa_t(OSD_CAP_R
|
354 profile_grants
.emplace_back(OSDCapMatch(profile
.pool_namespace
,
356 OSDCapSpec("rbd", "child_attach"));
357 profile_grants
.emplace_back(OSDCapMatch(profile
.pool_namespace
,
359 OSDCapSpec("rbd", "child_detach"));
363 bool OSDCap::allow_all() const
365 for (auto &grant
: grants
) {
366 if (grant
.allow_all()) {
373 void OSDCap::set_allow_all()
376 grants
.push_back(OSDCapGrant(OSDCapMatch(), OSDCapSpec(OSD_CAP_ANY
)));
379 bool OSDCap::is_capable(const string
& pool_name
, const string
& ns
,
380 const OSDCapPoolTag::app_map_t
& application_metadata
,
381 const string
& object
,
382 bool op_may_read
, bool op_may_write
,
383 const std::vector
<OpInfo::ClassInfo
>& classes
,
384 const entity_addr_t
& addr
) const
386 std::vector
<bool> class_allowed(classes
.size(), false);
387 for (auto &grant
: grants
) {
388 if (grant
.is_capable(pool_name
, ns
, application_metadata
,
389 object
, op_may_read
, op_may_write
, classes
, addr
,
399 namespace qi
= boost::spirit::qi
;
400 namespace ascii
= boost::spirit::ascii
;
401 namespace phoenix
= boost::phoenix
;
403 template <typename Iterator
>
404 struct OSDCapParser
: qi::grammar
<Iterator
, OSDCap()>
406 OSDCapParser() : OSDCapParser::base_type(osdcap
)
420 lexeme
['"' >> +(char_
- '"') >> '"'] |
421 lexeme
['\'' >> +(char_
- '\'') >> '\''];
423 lexeme
['"' >> *(char_
- '"') >> '"'] |
424 lexeme
['\'' >> *(char_
- '\'') >> '\''];
425 unquoted_word
%= +char_("a-zA-Z0-9_./-");
426 str
%= quoted_string
| unquoted_word
;
427 estr
%= equoted_string
| unquoted_word
;
428 network_str
%= +char_("/.:a-fA-F0-9][");
430 spaces
= +ascii::space
;
432 wildcard
= (lit('*') | lit("all")) [_val
= "*"];
434 pool_name
%= -(spaces
>> lit("pool") >> (lit('=') | spaces
) >> str
);
435 nspace
%= (spaces
>> lit("namespace")
436 >> (lit('=') | spaces
)
437 >> estr
>> -char_('*'));
439 // match := [pool[=]<poolname> [namespace[=]<namespace>]] [object_prefix <prefix>]
440 object_prefix
%= -(spaces
>> lit("object_prefix") >> spaces
>> str
);
441 pooltag
%= (spaces
>> lit("tag")
442 >> spaces
>> str
// application
443 >> spaces
>> (wildcard
| str
) // key
444 >> -spaces
>> lit('=') >> -spaces
>> (wildcard
| str
)); // value
447 pooltag
[_val
= phoenix::construct
<OSDCapMatch
>(_1
)] |
448 (nspace
>> pooltag
) [_val
= phoenix::construct
<OSDCapMatch
>(_1
, _2
)] |
449 (pool_name
>> nspace
>> object_prefix
) [_val
= phoenix::construct
<OSDCapMatch
>(_1
, _2
, _3
)] |
450 (pool_name
>> object_prefix
) [_val
= phoenix::construct
<OSDCapMatch
>(_1
, _2
)]
453 // rwxa := * | [r][w][x] [class-read] [class-write]
455 (spaces
>> wildcard
[_val
= OSD_CAP_ANY
]) |
459 ( lit('r')[_val
|= OSD_CAP_R
] ||
460 lit('w')[_val
|= OSD_CAP_W
] ||
461 lit('x')[_val
|= OSD_CAP_X
] )) ||
462 ( (spaces
>> lit("class-read")[_val
|= OSD_CAP_CLS_R
]) ||
463 (spaces
>> lit("class-write")[_val
|= OSD_CAP_CLS_W
]) ));
465 // capspec := * | rwx | class <name> [<method name>]
466 class_name
%= (spaces
>> lit("class") >> spaces
>> str
);
467 method_name
%= -(spaces
>> str
);
469 (rwxa
) [_val
= phoenix::construct
<OSDCapSpec
>(_1
)] |
470 (class_name
>> method_name
) [_val
= phoenix::construct
<OSDCapSpec
>(_1
, _2
)]);
472 // profile := profile <name> [pool[=]<pool> [namespace[=]<namespace>]]
473 profile_name
%= (lit("profile") >> (lit('=') | spaces
) >> str
);
475 (profile_name
>> pool_name
>> nspace
) [_val
= phoenix::construct
<OSDCapProfile
>(_1
, _2
, _3
)] |
476 (profile_name
>> pool_name
) [_val
= phoenix::construct
<OSDCapProfile
>(_1
, _2
)]);
478 // grant := allow match capspec
479 grant
= (*ascii::blank
>>
480 ((lit("allow") >> capspec
>> match
>>
481 -(spaces
>> lit("network") >> spaces
>> network_str
))
482 [_val
= phoenix::construct
<OSDCapGrant
>(_2
, _1
, _3
)] |
483 (lit("allow") >> match
>> capspec
>>
484 -(spaces
>> lit("network") >> spaces
>> network_str
))
485 [_val
= phoenix::construct
<OSDCapGrant
>(_1
, _2
, _3
)] |
486 (profile
>> -(spaces
>> lit("network") >> spaces
>> network_str
))
487 [_val
= phoenix::construct
<OSDCapGrant
>(_1
, _2
)]
489 // osdcap := grant [grant ...]
490 grants
%= (grant
% (lit(';') | lit(',')));
491 osdcap
= grants
[_val
= phoenix::construct
<OSDCap
>(_1
)];
493 qi::rule
<Iterator
> spaces
;
494 qi::rule
<Iterator
, unsigned()> rwxa
;
495 qi::rule
<Iterator
, string()> quoted_string
, equoted_string
;
496 qi::rule
<Iterator
, string()> unquoted_word
;
497 qi::rule
<Iterator
, string()> str
, estr
, network_str
;
498 qi::rule
<Iterator
, string()> wildcard
;
499 qi::rule
<Iterator
, string()> class_name
;
500 qi::rule
<Iterator
, string()> method_name
;
501 qi::rule
<Iterator
, OSDCapSpec()> capspec
;
502 qi::rule
<Iterator
, string()> pool_name
;
503 qi::rule
<Iterator
, string()> nspace
;
504 qi::rule
<Iterator
, string()> object_prefix
;
505 qi::rule
<Iterator
, OSDCapPoolTag()> pooltag
;
506 qi::rule
<Iterator
, OSDCapMatch()> match
;
507 qi::rule
<Iterator
, string()> profile_name
;
508 qi::rule
<Iterator
, OSDCapProfile()> profile
;
509 qi::rule
<Iterator
, OSDCapGrant()> grant
;
510 qi::rule
<Iterator
, std::vector
<OSDCapGrant
>()> grants
;
511 qi::rule
<Iterator
, OSDCap()> osdcap
;
514 bool OSDCap::parse(const string
& str
, ostream
*err
)
516 OSDCapParser
<string::const_iterator
> g
;
517 string::const_iterator iter
= str
.begin();
518 string::const_iterator end
= str
.end();
520 bool r
= qi::phrase_parse(iter
, end
, g
, ascii::space
, *this);
521 if (r
&& iter
== end
)
524 // Make sure no grants are kept after parsing failed!
528 *err
<< "osd capability parse failed, stopped at '" << std::string(iter
, end
)
529 << "' of '" << str
<< "'";