1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
12 #include <experimental/iterator>
14 #include "rapidjson/reader.h"
17 #include <arpa/inet.h>
18 #include "rgw_iam_policy.h"
21 constexpr int dout_subsys
= ceph_subsys_rgw
;
31 using std::stringstream
;
35 using std::unordered_map
;
37 using boost::container::flat_set
;
39 using std::regex_constants::ECMAScript
;
40 using std::regex_constants::optimize
;
41 using std::regex_match
;
44 using rapidjson::BaseReaderHandler
;
45 using rapidjson::UTF8
;
46 using rapidjson::SizeType
;
47 using rapidjson::Reader
;
48 using rapidjson::kParseCommentsFlag
;
49 using rapidjson::kParseNumbersAsStringsFlag
;
50 using rapidjson::StringStream
;
51 using rapidjson::ParseResult
;
53 using rgw::auth::Principal
;
57 #include "rgw_iam_policy_keywords.frag.cc"
66 static const actpair actpairs
[] =
67 {{ "s3:AbortMultipartUpload", s3AbortMultipartUpload
},
68 { "s3:CreateBucket", s3CreateBucket
},
69 { "s3:DeleteBucketPolicy", s3DeleteBucketPolicy
},
70 { "s3:DeleteBucket", s3DeleteBucket
},
71 { "s3:DeleteBucketWebsite", s3DeleteBucketWebsite
},
72 { "s3:DeleteObject", s3DeleteObject
},
73 { "s3:DeleteObjectVersion", s3DeleteObjectVersion
},
74 { "s3:DeleteObjectTagging", s3DeleteObjectTagging
},
75 { "s3:DeleteObjectVersionTagging", s3DeleteObjectVersionTagging
},
76 { "s3:DeleteBucketPublicAccessBlock", s3DeleteBucketPublicAccessBlock
},
77 { "s3:DeletePublicAccessBlock", s3DeletePublicAccessBlock
},
78 { "s3:DeleteReplicationConfiguration", s3DeleteReplicationConfiguration
},
79 { "s3:GetAccelerateConfiguration", s3GetAccelerateConfiguration
},
80 { "s3:GetBucketAcl", s3GetBucketAcl
},
81 { "s3:GetBucketCORS", s3GetBucketCORS
},
82 { "s3:GetBucketLocation", s3GetBucketLocation
},
83 { "s3:GetBucketLogging", s3GetBucketLogging
},
84 { "s3:GetBucketNotification", s3GetBucketNotification
},
85 { "s3:GetBucketPolicy", s3GetBucketPolicy
},
86 { "s3:GetBucketPolicyStatus", s3GetBucketPolicyStatus
},
87 { "s3:GetBucketPublicAccessBlock", s3GetBucketPublicAccessBlock
},
88 { "s3:GetBucketRequestPayment", s3GetBucketRequestPayment
},
89 { "s3:GetBucketTagging", s3GetBucketTagging
},
90 { "s3:GetBucketVersioning", s3GetBucketVersioning
},
91 { "s3:GetBucketWebsite", s3GetBucketWebsite
},
92 { "s3:GetLifecycleConfiguration", s3GetLifecycleConfiguration
},
93 { "s3:GetBucketObjectLockConfiguration", s3GetBucketObjectLockConfiguration
},
94 { "s3:GetPublicAccessBlock", s3GetPublicAccessBlock
},
95 { "s3:GetObjectAcl", s3GetObjectAcl
},
96 { "s3:GetObject", s3GetObject
},
97 { "s3:GetObjectTorrent", s3GetObjectTorrent
},
98 { "s3:GetObjectVersionAcl", s3GetObjectVersionAcl
},
99 { "s3:GetObjectVersion", s3GetObjectVersion
},
100 { "s3:GetObjectVersionTorrent", s3GetObjectVersionTorrent
},
101 { "s3:GetObjectTagging", s3GetObjectTagging
},
102 { "s3:GetObjectVersionTagging", s3GetObjectVersionTagging
},
103 { "s3:GetObjectRetention", s3GetObjectRetention
},
104 { "s3:GetObjectLegalHold", s3GetObjectLegalHold
},
105 { "s3:GetReplicationConfiguration", s3GetReplicationConfiguration
},
106 { "s3:ListAllMyBuckets", s3ListAllMyBuckets
},
107 { "s3:ListBucketMultipartUploads", s3ListBucketMultipartUploads
},
108 { "s3:ListBucket", s3ListBucket
},
109 { "s3:ListBucketVersions", s3ListBucketVersions
},
110 { "s3:ListMultipartUploadParts", s3ListMultipartUploadParts
},
111 { "s3:PutAccelerateConfiguration", s3PutAccelerateConfiguration
},
112 { "s3:PutBucketAcl", s3PutBucketAcl
},
113 { "s3:PutBucketCORS", s3PutBucketCORS
},
114 { "s3:PutBucketLogging", s3PutBucketLogging
},
115 { "s3:PutBucketNotification", s3PutBucketNotification
},
116 { "s3:PutBucketPolicy", s3PutBucketPolicy
},
117 { "s3:PutBucketRequestPayment", s3PutBucketRequestPayment
},
118 { "s3:PutBucketTagging", s3PutBucketTagging
},
119 { "s3:PutBucketVersioning", s3PutBucketVersioning
},
120 { "s3:PutBucketWebsite", s3PutBucketWebsite
},
121 { "s3:PutLifecycleConfiguration", s3PutLifecycleConfiguration
},
122 { "s3:PutBucketObjectLockConfiguration", s3PutBucketObjectLockConfiguration
},
123 { "s3:PutObjectAcl", s3PutObjectAcl
},
124 { "s3:PutObject", s3PutObject
},
125 { "s3:PutObjectVersionAcl", s3PutObjectVersionAcl
},
126 { "s3:PutObjectTagging", s3PutObjectTagging
},
127 { "s3:PutObjectVersionTagging", s3PutObjectVersionTagging
},
128 { "s3:PutObjectRetention", s3PutObjectRetention
},
129 { "s3:PutObjectLegalHold", s3PutObjectLegalHold
},
130 { "s3:BypassGovernanceRetention", s3BypassGovernanceRetention
},
131 { "s3:PutBucketPublicAccessBlock", s3PutBucketPublicAccessBlock
},
132 { "s3:PutPublicAccessBlock", s3PutPublicAccessBlock
},
133 { "s3:PutReplicationConfiguration", s3PutReplicationConfiguration
},
134 { "s3:RestoreObject", s3RestoreObject
},
135 { "iam:PutUserPolicy", iamPutUserPolicy
},
136 { "iam:GetUserPolicy", iamGetUserPolicy
},
137 { "iam:DeleteUserPolicy", iamDeleteUserPolicy
},
138 { "iam:ListUserPolicies", iamListUserPolicies
},
139 { "iam:CreateRole", iamCreateRole
},
140 { "iam:DeleteRole", iamDeleteRole
},
141 { "iam:GetRole", iamGetRole
},
142 { "iam:ModifyRole", iamModifyRole
},
143 { "iam:ListRoles", iamListRoles
},
144 { "iam:PutRolePolicy", iamPutRolePolicy
},
145 { "iam:GetRolePolicy", iamGetRolePolicy
},
146 { "iam:ListRolePolicies", iamListRolePolicies
},
147 { "iam:DeleteRolePolicy", iamDeleteRolePolicy
},
148 { "iam:CreateOIDCProvider", iamCreateOIDCProvider
},
149 { "iam:DeleteOIDCProvider", iamDeleteOIDCProvider
},
150 { "iam:GetOIDCProvider", iamGetOIDCProvider
},
151 { "iam:ListOIDCProviders", iamListOIDCProviders
},
152 { "sts:AssumeRole", stsAssumeRole
},
153 { "sts:AssumeRoleWithWebIdentity", stsAssumeRoleWithWebIdentity
},
154 { "sts:GetSessionToken", stsGetSessionToken
},
159 const Keyword top
[1]{"<Top>", TokenKind::pseudo
, TokenID::Top
, 0, false,
161 const Keyword cond_key
[1]{"<Condition Key>", TokenKind::cond_key
,
162 TokenID::CondKey
, 0, true, false};
168 bool arraying
= false;
169 bool objecting
= false;
170 bool cond_ifexists
= false;
174 ParseState(PolicyParser
* pp
, const Keyword
* w
)
182 if (w
->arrayable
&& !arraying
) {
191 bool key(const char* s
, size_t l
);
192 bool do_string(CephContext
* cct
, const char* s
, size_t l
);
193 bool number(const char* str
, size_t l
);
196 // If this confuses you, look up the Curiously Recurring Template Pattern
197 struct PolicyParser
: public BaseReaderHandler
<UTF8
<>, PolicyParser
> {
199 std::vector
<ParseState
> s
;
201 const string
& tenant
;
207 uint32_t dex(TokenID in
) const {
209 case TokenID::Version
:
213 case TokenID::Statement
:
217 case TokenID::Effect
:
219 case TokenID::Principal
:
221 case TokenID::NotPrincipal
:
223 case TokenID::Action
:
225 case TokenID::NotAction
:
227 case TokenID::Resource
:
229 case TokenID::NotResource
:
231 case TokenID::Condition
:
235 case TokenID::Federated
:
237 case TokenID::Service
:
239 case TokenID::CanonicalUser
:
245 bool test(TokenID in
) {
246 return seen
& dex(in
);
248 void set(TokenID in
) {
250 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
251 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
252 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
253 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
254 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
255 dex(TokenID::Federated
) | dex(TokenID::Service
) |
256 dex(TokenID::CanonicalUser
))) {
260 void set(std::initializer_list
<TokenID
> l
) {
263 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
264 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
265 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
266 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
267 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
268 dex(TokenID::Federated
) | dex(TokenID::Service
) |
269 dex(TokenID::CanonicalUser
))) {
274 void reset(TokenID in
) {
276 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
277 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
278 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
279 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
280 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
281 dex(TokenID::Federated
) | dex(TokenID::Service
) |
282 dex(TokenID::CanonicalUser
))) {
286 void reset(std::initializer_list
<TokenID
> l
) {
289 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
290 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
291 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
292 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
293 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
294 dex(TokenID::Federated
) | dex(TokenID::Service
) |
295 dex(TokenID::CanonicalUser
))) {
300 void reset(uint32_t& v
) {
305 PolicyParser(CephContext
* cct
, const string
& tenant
, Policy
& policy
)
306 : cct(cct
), tenant(tenant
), policy(policy
) {}
307 PolicyParser(const PolicyParser
& policy
) = delete;
311 s
.push_back({this, top
});
312 s
.back().objecting
= true;
316 return s
.back().obj_start();
318 bool EndObject(SizeType memberCount
) {
322 return s
.back().obj_end();
324 bool Key(const char* str
, SizeType length
, bool copy
) {
328 return s
.back().key(str
, length
);
331 bool String(const char* str
, SizeType length
, bool copy
) {
335 return s
.back().do_string(cct
, str
, length
);
337 bool RawNumber(const char* str
, SizeType length
, bool copy
) {
342 return s
.back().number(str
, length
);
349 return s
.back().array_start();
351 bool EndArray(SizeType
) {
356 return s
.back().array_end();
365 // I really despise this misfeature of C++.
367 bool ParseState::obj_end() {
380 bool ParseState::key(const char* s
, size_t l
) {
382 bool ifexists
= false;
383 if (w
->id
== TokenID::Condition
&& w
->kind
== TokenKind::statement
) {
384 static constexpr char IfExists
[] = "IfExists";
385 if (boost::algorithm::ends_with(std::string_view
{s
, l
}, IfExists
)) {
387 token_len
-= sizeof(IfExists
)-1;
390 auto k
= pp
->tokens
.lookup(s
, token_len
);
393 if (w
->kind
== TokenKind::cond_op
) {
395 auto& t
= pp
->policy
.statements
.back();
396 auto c_ife
= cond_ifexists
;
397 pp
->s
.emplace_back(pp
, cond_key
);
398 t
.conditions
.emplace_back(id
, s
, l
, c_ife
);
405 // If the token we're going with belongs within the condition at the
406 // top of the stack and we haven't already encountered it, push it
409 if ((((w
->id
== TokenID::Top
) && (k
->kind
== TokenKind::top
)) ||
411 ((w
->id
== TokenID::Statement
) && (k
->kind
== TokenKind::statement
)) ||
414 ((w
->id
== TokenID::Principal
|| w
->id
== TokenID::NotPrincipal
) &&
415 (k
->kind
== TokenKind::princ_type
))) &&
417 // Check that it hasn't been encountered. Note that this
418 // conjoins with the run of disjunctions above.
421 pp
->s
.emplace_back(pp
, k
);
423 } else if ((w
->id
== TokenID::Condition
) &&
424 (k
->kind
== TokenKind::cond_op
)) {
425 pp
->s
.emplace_back(pp
, k
);
426 pp
->s
.back().cond_ifexists
= ifexists
;
432 // I should just rewrite a few helper functions to use iterators,
433 // which will make all of this ever so much nicer.
434 static boost::optional
<Principal
> parse_principal(CephContext
* cct
, TokenID t
,
437 if ((t
== TokenID::AWS
) && (s
== "*")) {
438 return Principal::wildcard();
440 // Do nothing for now.
441 } else if (t
== TokenID::CanonicalUser
) {
443 } // AWS and Federated ARNs
444 else if (t
== TokenID::AWS
|| t
== TokenID::Federated
) {
445 if (auto a
= ARN::parse(s
)) {
446 if (a
->resource
== "root") {
447 return Principal::tenant(std::move(a
->account
));
450 static const char rx_str
[] = "([^/]*)/(.*)";
451 static const regex
rx(rx_str
, sizeof(rx_str
) - 1,
452 std::regex_constants::ECMAScript
|
453 std::regex_constants::optimize
);
455 if (regex_match(a
->resource
, match
, rx
) && match
.size() == 3) {
456 if (match
[1] == "user") {
457 return Principal::user(std::move(a
->account
),
461 if (match
[1] == "role") {
462 return Principal::role(std::move(a
->account
),
466 if (match
[1] == "oidc-provider") {
467 return Principal::oidc_provider(std::move(match
[2]));
469 if (match
[1] == "assumed-role") {
470 return Principal::assumed_role(std::move(a
->account
), match
[2]);
474 if (std::none_of(s
.begin(), s
.end(),
476 return (c
== ':') || (c
== '/');
478 // Since tenants are simply prefixes, there's no really good
479 // way to see if one exists or not. So we return the thing and
480 // let them try to match against it.
481 return Principal::tenant(std::move(s
));
486 ldout(cct
, 0) << "Supplied principal is discarded: " << s
<< dendl
;
490 bool ParseState::do_string(CephContext
* cct
, const char* s
, size_t l
) {
491 auto k
= pp
->tokens
.lookup(s
, l
);
492 Policy
& p
= pp
->policy
;
493 bool is_action
= false;
494 bool is_validaction
= false;
495 Statement
* t
= p
.statements
.empty() ? nullptr : &(p
.statements
.back());
498 if ((w
->id
== TokenID::Version
) && k
&&
499 k
->kind
== TokenKind::version_key
) {
500 p
.version
= static_cast<Version
>(k
->specific
);
501 } else if (w
->id
== TokenID::Id
) {
506 } else if (w
->id
== TokenID::Sid
) {
507 t
->sid
.emplace(s
, l
);
508 } else if ((w
->id
== TokenID::Effect
) && k
&&
509 k
->kind
== TokenKind::effect_key
) {
510 t
->effect
= static_cast<Effect
>(k
->specific
);
511 } else if (w
->id
== TokenID::Principal
&& s
&& *s
== '*') {
512 t
->princ
.emplace(Principal::wildcard());
513 } else if (w
->id
== TokenID::NotPrincipal
&& s
&& *s
== '*') {
514 t
->noprinc
.emplace(Principal::wildcard());
515 } else if ((w
->id
== TokenID::Action
) ||
516 (w
->id
== TokenID::NotAction
)) {
519 is_validaction
= true;
520 (w
->id
== TokenID::Action
?
521 t
->action
= allValue
: t
->notaction
= allValue
);
523 for (auto& p
: actpairs
) {
524 if (match_policy({s
, l
}, p
.name
, MATCH_POLICY_ACTION
)) {
525 is_validaction
= true;
526 (w
->id
== TokenID::Action
? t
->action
[p
.bit
] = 1 : t
->notaction
[p
.bit
] = 1);
528 if ((t
->action
& s3AllValue
) == s3AllValue
) {
529 t
->action
[s3All
] = 1;
531 if ((t
->notaction
& s3AllValue
) == s3AllValue
) {
532 t
->notaction
[s3All
] = 1;
534 if ((t
->action
& iamAllValue
) == iamAllValue
) {
535 t
->action
[iamAll
] = 1;
537 if ((t
->notaction
& iamAllValue
) == iamAllValue
) {
538 t
->notaction
[iamAll
] = 1;
540 if ((t
->action
& stsAllValue
) == stsAllValue
) {
541 t
->action
[stsAll
] = 1;
543 if ((t
->notaction
& stsAllValue
) == stsAllValue
) {
544 t
->notaction
[stsAll
] = 1;
548 } else if (w
->id
== TokenID::Resource
|| w
->id
== TokenID::NotResource
) {
549 auto a
= ARN::parse({s
, l
}, true);
550 // You can't specify resources for someone ELSE'S account.
551 if (a
&& (a
->account
.empty() || a
->account
== pp
->tenant
||
552 a
->account
== "*")) {
553 if (a
->account
.empty() || a
->account
== "*")
554 a
->account
= pp
->tenant
;
555 (w
->id
== TokenID::Resource
? t
->resource
: t
->notresource
)
556 .emplace(std::move(*a
));
559 ldout(cct
, 0) << "Supplied resource is discarded: " << string(s
, l
)
561 } else if (w
->kind
== TokenKind::cond_key
) {
562 auto& t
= pp
->policy
.statements
.back();
563 t
.conditions
.back().vals
.emplace_back(s
, l
);
567 } else if (w
->kind
== TokenKind::princ_type
) {
568 if (pp
->s
.size() <= 1) {
571 auto& pri
= pp
->s
[pp
->s
.size() - 2].w
->id
== TokenID::Principal
?
572 t
->princ
: t
->noprinc
;
575 if (auto o
= parse_principal(pp
->cct
, w
->id
, string(s
, l
))) {
576 pri
.emplace(std::move(*o
));
589 if (is_action
&& !is_validaction
){
596 bool ParseState::number(const char* s
, size_t l
) {
598 if (w
->kind
== TokenKind::cond_key
) {
599 auto& t
= pp
->policy
.statements
.back();
600 t
.conditions
.back().vals
.emplace_back(s
, l
);
615 void ParseState::reset() {
619 bool ParseState::obj_start() {
620 if (w
->objectable
&& !objecting
) {
622 if (w
->id
== TokenID::Statement
) {
623 pp
->policy
.statements
.emplace_back();
633 bool ParseState::array_end() {
634 if (arraying
&& !objecting
) {
642 ostream
& operator <<(ostream
& m
, const MaskedIP
& ip
) {
643 // I have a theory about why std::bitset is the way it is.
645 for (int i
= 7; i
>= 0; --i
) {
647 for (int j
= 15; j
>= 0; --j
) {
648 hextet
|= (ip
.addr
[(i
* 16) + j
] << j
);
650 m
<< hex
<< (unsigned int) hextet
;
656 // It involves Satan.
657 for (int i
= 3; i
>= 0; --i
) {
659 for (int j
= 7; j
>= 0; --j
) {
660 b
|= (ip
.addr
[(i
* 8) + j
] << j
);
662 m
<< (unsigned int) b
;
668 m
<< "/" << dec
<< ip
.prefix
;
669 // It would explain a lot
673 bool Condition::eval(const Environment
& env
) const {
674 auto i
= env
.find(key
);
675 if (op
== TokenID::Null
) {
676 return i
== env
.end() ? true : false;
679 if (i
== env
.end()) {
682 const auto& s
= i
->second
;
686 case TokenID::StringEquals
:
687 return orrible(std::equal_to
<std::string
>(), s
, vals
);
689 case TokenID::StringNotEquals
:
690 return orrible(std::not_fn(std::equal_to
<std::string
>()),
693 case TokenID::StringEqualsIgnoreCase
:
694 return orrible(ci_equal_to(), s
, vals
);
696 case TokenID::StringNotEqualsIgnoreCase
:
697 return orrible(std::not_fn(ci_equal_to()), s
, vals
);
699 case TokenID::StringLike
:
700 return orrible(string_like(), s
, vals
);
702 case TokenID::StringNotLike
:
703 return orrible(std::not_fn(string_like()), s
, vals
);
706 case TokenID::NumericEquals
:
707 return shortible(std::equal_to
<double>(), as_number
, s
, vals
);
709 case TokenID::NumericNotEquals
:
710 return shortible(std::not_fn(std::equal_to
<double>()),
714 case TokenID::NumericLessThan
:
715 return shortible(std::less
<double>(), as_number
, s
, vals
);
718 case TokenID::NumericLessThanEquals
:
719 return shortible(std::less_equal
<double>(), as_number
, s
, vals
);
721 case TokenID::NumericGreaterThan
:
722 return shortible(std::greater
<double>(), as_number
, s
, vals
);
724 case TokenID::NumericGreaterThanEquals
:
725 return shortible(std::greater_equal
<double>(), as_number
, s
, vals
);
728 case TokenID::DateEquals
:
729 return shortible(std::equal_to
<ceph::real_time
>(), as_date
, s
, vals
);
731 case TokenID::DateNotEquals
:
732 return shortible(std::not_fn(std::equal_to
<ceph::real_time
>()),
735 case TokenID::DateLessThan
:
736 return shortible(std::less
<ceph::real_time
>(), as_date
, s
, vals
);
739 case TokenID::DateLessThanEquals
:
740 return shortible(std::less_equal
<ceph::real_time
>(), as_date
, s
, vals
);
742 case TokenID::DateGreaterThan
:
743 return shortible(std::greater
<ceph::real_time
>(), as_date
, s
, vals
);
745 case TokenID::DateGreaterThanEquals
:
746 return shortible(std::greater_equal
<ceph::real_time
>(), as_date
, s
,
751 return shortible(std::equal_to
<bool>(), as_bool
, s
, vals
);
754 case TokenID::BinaryEquals
:
755 return shortible(std::equal_to
<ceph::bufferlist
>(), as_binary
, s
,
759 case TokenID::IpAddress
:
760 return shortible(std::equal_to
<MaskedIP
>(), as_network
, s
, vals
);
762 case TokenID::NotIpAddress
:
764 auto xc
= as_network(s
);
769 for (const string
& d
: vals
) {
770 auto xd
= as_network(d
);
783 // Amazon Resource Names! (Does S3 need this?)
784 TokenID::ArnEquals
, TokenID::ArnNotEquals
, TokenID::ArnLike
,
793 boost::optional
<MaskedIP
> Condition::as_network(const string
& s
) {
799 m
.v6
= (s
.find(':') == string::npos
) ? false : true;
801 auto slash
= s
.find('/');
802 if (slash
== string::npos
) {
803 m
.prefix
= m
.v6
? 128 : 32;
806 m
.prefix
= strtoul(s
.data() + slash
+ 1, &end
, 10);
807 if (*end
!= 0 || (m
.v6
&& m
.prefix
> 128) ||
808 (!m
.v6
&& m
.prefix
> 32)) {
816 if (slash
!= string::npos
) {
817 t
.assign(s
, 0, slash
);
823 if (inet_pton(AF_INET6
, p
->c_str(), static_cast<void*>(&a
)) != 1) {
827 m
.addr
|= Address(a
.s6_addr
[15]) << 0;
828 m
.addr
|= Address(a
.s6_addr
[14]) << 8;
829 m
.addr
|= Address(a
.s6_addr
[13]) << 16;
830 m
.addr
|= Address(a
.s6_addr
[12]) << 24;
831 m
.addr
|= Address(a
.s6_addr
[11]) << 32;
832 m
.addr
|= Address(a
.s6_addr
[10]) << 40;
833 m
.addr
|= Address(a
.s6_addr
[9]) << 48;
834 m
.addr
|= Address(a
.s6_addr
[8]) << 56;
835 m
.addr
|= Address(a
.s6_addr
[7]) << 64;
836 m
.addr
|= Address(a
.s6_addr
[6]) << 72;
837 m
.addr
|= Address(a
.s6_addr
[5]) << 80;
838 m
.addr
|= Address(a
.s6_addr
[4]) << 88;
839 m
.addr
|= Address(a
.s6_addr
[3]) << 96;
840 m
.addr
|= Address(a
.s6_addr
[2]) << 104;
841 m
.addr
|= Address(a
.s6_addr
[1]) << 112;
842 m
.addr
|= Address(a
.s6_addr
[0]) << 120;
845 if (inet_pton(AF_INET
, p
->c_str(), static_cast<void*>(&a
)) != 1) {
849 m
.addr
= ntohl(a
.s_addr
);
856 const char* condop_string(const TokenID t
) {
858 case TokenID::StringEquals
:
859 return "StringEquals";
861 case TokenID::StringNotEquals
:
862 return "StringNotEquals";
864 case TokenID::StringEqualsIgnoreCase
:
865 return "StringEqualsIgnoreCase";
867 case TokenID::StringNotEqualsIgnoreCase
:
868 return "StringNotEqualsIgnoreCase";
870 case TokenID::StringLike
:
873 case TokenID::StringNotLike
:
874 return "StringNotLike";
877 case TokenID::NumericEquals
:
878 return "NumericEquals";
880 case TokenID::NumericNotEquals
:
881 return "NumericNotEquals";
883 case TokenID::NumericLessThan
:
884 return "NumericLessThan";
886 case TokenID::NumericLessThanEquals
:
887 return "NumericLessThanEquals";
889 case TokenID::NumericGreaterThan
:
890 return "NumericGreaterThan";
892 case TokenID::NumericGreaterThanEquals
:
893 return "NumericGreaterThanEquals";
895 case TokenID::DateEquals
:
898 case TokenID::DateNotEquals
:
899 return "DateNotEquals";
901 case TokenID::DateLessThan
:
902 return "DateLessThan";
904 case TokenID::DateLessThanEquals
:
905 return "DateLessThanEquals";
907 case TokenID::DateGreaterThan
:
908 return "DateGreaterThan";
910 case TokenID::DateGreaterThanEquals
:
911 return "DateGreaterThanEquals";
916 case TokenID::BinaryEquals
:
917 return "BinaryEquals";
919 case TokenID::IpAddress
:
920 return "case TokenID::IpAddress";
922 case TokenID::NotIpAddress
:
923 return "NotIpAddress";
925 case TokenID::ArnEquals
:
928 case TokenID::ArnNotEquals
:
929 return "ArnNotEquals";
931 case TokenID::ArnLike
:
934 case TokenID::ArnNotLike
:
941 return "InvalidConditionOperator";
945 template<typename Iterator
>
946 ostream
& print_array(ostream
& m
, Iterator begin
, Iterator end
) {
951 std::copy(begin
, end
, std::experimental::make_ostream_joiner(m
, ", "));
957 template<typename Iterator
>
958 ostream
& print_dict(ostream
& m
, Iterator begin
, Iterator end
) {
960 std::copy(begin
, end
, std::experimental::make_ostream_joiner(m
, ", "));
967 ostream
& operator <<(ostream
& m
, const Condition
& c
) {
968 m
<< condop_string(c
.op
);
972 m
<< ": { " << c
.key
;
973 print_array(m
, c
.vals
.cbegin(), c
.vals
.cend());
977 Effect
Statement::eval(const Environment
& e
,
978 boost::optional
<const rgw::auth::Identity
&> ida
,
979 uint64_t act
, const ARN
& res
, boost::optional
<PolicyPrincipal
&> princ_type
) const {
981 if (eval_principal(e
, ida
, princ_type
) == Effect::Deny
) {
985 if (!resource
.empty()) {
986 if (!std::any_of(resource
.begin(), resource
.end(),
987 [&res
](const ARN
& pattern
) {
988 return pattern
.match(res
);
992 } else if (!notresource
.empty()) {
993 if (std::any_of(notresource
.begin(), notresource
.end(),
994 [&res
](const ARN
& pattern
) {
995 return pattern
.match(res
);
1001 if (!(action
[act
] == 1) || (notaction
[act
] == 1)) {
1002 return Effect::Pass
;
1005 if (std::all_of(conditions
.begin(),
1007 [&e
](const Condition
& c
) { return c
.eval(e
);})) {
1011 return Effect::Pass
;
1014 Effect
Statement::eval_principal(const Environment
& e
,
1015 boost::optional
<const rgw::auth::Identity
&> ida
, boost::optional
<PolicyPrincipal
&> princ_type
) const {
1017 *princ_type
= PolicyPrincipal::Other
;
1020 if (princ
.empty() && noprinc
.empty()) {
1021 return Effect::Deny
;
1023 if (ida
->get_identity_type() != TYPE_ROLE
&& !princ
.empty() && !ida
->is_identity(princ
)) {
1024 return Effect::Deny
;
1026 if (ida
->get_identity_type() == TYPE_ROLE
&& !princ
.empty()) {
1027 bool princ_matched
= false;
1028 for (auto p
: princ
) { // Check each principal to determine the type of the one that has matched
1029 boost::container::flat_set
<Principal
> id
;
1031 if (ida
->is_identity(id
)) {
1032 if (p
.is_assumed_role() || p
.is_user()) {
1033 if (princ_type
) *princ_type
= PolicyPrincipal::Session
;
1035 if (princ_type
) *princ_type
= PolicyPrincipal::Role
;
1037 princ_matched
= true;
1040 if (!princ_matched
) {
1041 return Effect::Deny
;
1043 } else if (!noprinc
.empty() && ida
->is_identity(noprinc
)) {
1044 return Effect::Deny
;
1047 return Effect::Allow
;
1050 Effect
Statement::eval_conditions(const Environment
& e
) const {
1051 if (std::all_of(conditions
.begin(),
1053 [&e
](const Condition
& c
) { return c
.eval(e
);})) {
1054 return Effect::Allow
;
1056 return Effect::Deny
;
1060 const char* action_bit_string(uint64_t action
) {
1063 return "s3:GetObject";
1065 case s3GetObjectVersion
:
1066 return "s3:GetObjectVersion";
1069 return "s3:PutObject";
1071 case s3GetObjectAcl
:
1072 return "s3:GetObjectAcl";
1074 case s3GetObjectVersionAcl
:
1075 return "s3:GetObjectVersionAcl";
1077 case s3PutObjectAcl
:
1078 return "s3:PutObjectAcl";
1080 case s3PutObjectVersionAcl
:
1081 return "s3:PutObjectVersionAcl";
1083 case s3DeleteObject
:
1084 return "s3:DeleteObject";
1086 case s3DeleteObjectVersion
:
1087 return "s3:DeleteObjectVersion";
1089 case s3ListMultipartUploadParts
:
1090 return "s3:ListMultipartUploadParts";
1092 case s3AbortMultipartUpload
:
1093 return "s3:AbortMultipartUpload";
1095 case s3GetObjectTorrent
:
1096 return "s3:GetObjectTorrent";
1098 case s3GetObjectVersionTorrent
:
1099 return "s3:GetObjectVersionTorrent";
1101 case s3RestoreObject
:
1102 return "s3:RestoreObject";
1104 case s3CreateBucket
:
1105 return "s3:CreateBucket";
1107 case s3DeleteBucket
:
1108 return "s3:DeleteBucket";
1111 return "s3:ListBucket";
1113 case s3ListBucketVersions
:
1114 return "s3:ListBucketVersions";
1115 case s3ListAllMyBuckets
:
1116 return "s3:ListAllMyBuckets";
1118 case s3ListBucketMultipartUploads
:
1119 return "s3:ListBucketMultipartUploads";
1121 case s3GetAccelerateConfiguration
:
1122 return "s3:GetAccelerateConfiguration";
1124 case s3PutAccelerateConfiguration
:
1125 return "s3:PutAccelerateConfiguration";
1127 case s3GetBucketAcl
:
1128 return "s3:GetBucketAcl";
1130 case s3PutBucketAcl
:
1131 return "s3:PutBucketAcl";
1133 case s3GetBucketCORS
:
1134 return "s3:GetBucketCORS";
1136 case s3PutBucketCORS
:
1137 return "s3:PutBucketCORS";
1139 case s3GetBucketVersioning
:
1140 return "s3:GetBucketVersioning";
1142 case s3PutBucketVersioning
:
1143 return "s3:PutBucketVersioning";
1145 case s3GetBucketRequestPayment
:
1146 return "s3:GetBucketRequestPayment";
1148 case s3PutBucketRequestPayment
:
1149 return "s3:PutBucketRequestPayment";
1151 case s3GetBucketLocation
:
1152 return "s3:GetBucketLocation";
1154 case s3GetBucketPolicy
:
1155 return "s3:GetBucketPolicy";
1157 case s3DeleteBucketPolicy
:
1158 return "s3:DeleteBucketPolicy";
1160 case s3PutBucketPolicy
:
1161 return "s3:PutBucketPolicy";
1163 case s3GetBucketNotification
:
1164 return "s3:GetBucketNotification";
1166 case s3PutBucketNotification
:
1167 return "s3:PutBucketNotification";
1169 case s3GetBucketLogging
:
1170 return "s3:GetBucketLogging";
1172 case s3PutBucketLogging
:
1173 return "s3:PutBucketLogging";
1175 case s3GetBucketTagging
:
1176 return "s3:GetBucketTagging";
1178 case s3PutBucketTagging
:
1179 return "s3:PutBucketTagging";
1181 case s3GetBucketWebsite
:
1182 return "s3:GetBucketWebsite";
1184 case s3PutBucketWebsite
:
1185 return "s3:PutBucketWebsite";
1187 case s3DeleteBucketWebsite
:
1188 return "s3:DeleteBucketWebsite";
1190 case s3GetLifecycleConfiguration
:
1191 return "s3:GetLifecycleConfiguration";
1193 case s3PutLifecycleConfiguration
:
1194 return "s3:PutLifecycleConfiguration";
1196 case s3PutReplicationConfiguration
:
1197 return "s3:PutReplicationConfiguration";
1199 case s3GetReplicationConfiguration
:
1200 return "s3:GetReplicationConfiguration";
1202 case s3DeleteReplicationConfiguration
:
1203 return "s3:DeleteReplicationConfiguration";
1205 case s3PutObjectTagging
:
1206 return "s3:PutObjectTagging";
1208 case s3PutObjectVersionTagging
:
1209 return "s3:PutObjectVersionTagging";
1211 case s3GetObjectTagging
:
1212 return "s3:GetObjectTagging";
1214 case s3GetObjectVersionTagging
:
1215 return "s3:GetObjectVersionTagging";
1217 case s3DeleteObjectTagging
:
1218 return "s3:DeleteObjectTagging";
1220 case s3DeleteObjectVersionTagging
:
1221 return "s3:DeleteObjectVersionTagging";
1223 case s3PutBucketObjectLockConfiguration
:
1224 return "s3:PutBucketObjectLockConfiguration";
1226 case s3GetBucketObjectLockConfiguration
:
1227 return "s3:GetBucketObjectLockConfiguration";
1229 case s3PutObjectRetention
:
1230 return "s3:PutObjectRetention";
1232 case s3GetObjectRetention
:
1233 return "s3:GetObjectRetention";
1235 case s3PutObjectLegalHold
:
1236 return "s3:PutObjectLegalHold";
1238 case s3GetObjectLegalHold
:
1239 return "s3:GetObjectLegalHold";
1241 case s3BypassGovernanceRetention
:
1242 return "s3:BypassGovernanceRetention";
1244 case iamPutUserPolicy
:
1245 return "iam:PutUserPolicy";
1247 case iamGetUserPolicy
:
1248 return "iam:GetUserPolicy";
1250 case iamListUserPolicies
:
1251 return "iam:ListUserPolicies";
1253 case iamDeleteUserPolicy
:
1254 return "iam:DeleteUserPolicy";
1257 return "iam:CreateRole";
1260 return "iam:DeleteRole";
1263 return "iam:GetRole";
1266 return "iam:ModifyRole";
1269 return "iam:ListRoles";
1271 case iamPutRolePolicy
:
1272 return "iam:PutRolePolicy";
1274 case iamGetRolePolicy
:
1275 return "iam:GetRolePolicy";
1277 case iamListRolePolicies
:
1278 return "iam:ListRolePolicies";
1280 case iamDeleteRolePolicy
:
1281 return "iam:DeleteRolePolicy";
1283 case iamCreateOIDCProvider
:
1284 return "iam:CreateOIDCProvider";
1286 case iamDeleteOIDCProvider
:
1287 return "iam:DeleteOIDCProvider";
1289 case iamGetOIDCProvider
:
1290 return "iam:GetOIDCProvider";
1292 case iamListOIDCProviders
:
1293 return "iam:ListOIDCProviders";
1296 return "sts:AssumeRole";
1298 case stsAssumeRoleWithWebIdentity
:
1299 return "sts:AssumeRoleWithWebIdentity";
1301 case stsGetSessionToken
:
1302 return "sts:GetSessionToken";
1307 ostream
& print_actions(ostream
& m
, const Action_t a
) {
1310 for (auto i
= 0U; i
< allCount
; ++i
) {
1317 m
<< action_bit_string(i
);
1329 ostream
& operator <<(ostream
& m
, const Statement
& s
) {
1332 m
<< "Sid: " << *s
.sid
<< ", ";
1334 if (!s
.princ
.empty()) {
1336 print_dict(m
, s
.princ
.cbegin(), s
.princ
.cend());
1339 if (!s
.noprinc
.empty()) {
1340 m
<< "NotPrincipal: ";
1341 print_dict(m
, s
.noprinc
.cbegin(), s
.noprinc
.cend());
1346 (s
.effect
== Effect::Allow
?
1347 (const char*) "Allow" :
1348 (const char*) "Deny");
1350 if (s
.action
.any() || s
.notaction
.any() || !s
.resource
.empty() ||
1351 !s
.notresource
.empty() || !s
.conditions
.empty()) {
1355 if (s
.action
.any()) {
1357 print_actions(m
, s
.action
);
1359 if (s
.notaction
.any() || !s
.resource
.empty() ||
1360 !s
.notresource
.empty() || !s
.conditions
.empty()) {
1365 if (s
.notaction
.any()) {
1367 print_actions(m
, s
.notaction
);
1369 if (!s
.resource
.empty() || !s
.notresource
.empty() ||
1370 !s
.conditions
.empty()) {
1375 if (!s
.resource
.empty()) {
1377 print_array(m
, s
.resource
.cbegin(), s
.resource
.cend());
1379 if (!s
.notresource
.empty() || !s
.conditions
.empty()) {
1384 if (!s
.notresource
.empty()) {
1385 m
<< "NotResource: ";
1386 print_array(m
, s
.notresource
.cbegin(), s
.notresource
.cend());
1388 if (!s
.conditions
.empty()) {
1393 if (!s
.conditions
.empty()) {
1395 print_dict(m
, s
.conditions
.cbegin(), s
.conditions
.cend());
1401 Policy::Policy(CephContext
* cct
, const string
& tenant
,
1402 const bufferlist
& _text
)
1403 : text(_text
.to_str()) {
1404 StringStream
ss(text
.data());
1405 PolicyParser
pp(cct
, tenant
, *this);
1406 auto pr
= Reader
{}.Parse
<kParseNumbersAsStringsFlag
|
1407 kParseCommentsFlag
>(ss
, pp
);
1409 throw PolicyParseException(std::move(pr
));
1413 Effect
Policy::eval(const Environment
& e
,
1414 boost::optional
<const rgw::auth::Identity
&> ida
,
1415 std::uint64_t action
, const ARN
& resource
,
1416 boost::optional
<PolicyPrincipal
&> princ_type
) const {
1417 auto allowed
= false;
1418 for (auto& s
: statements
) {
1419 auto g
= s
.eval(e
, ida
, action
, resource
, princ_type
);
1420 if (g
== Effect::Deny
) {
1422 } else if (g
== Effect::Allow
) {
1426 return allowed
? Effect::Allow
: Effect::Pass
;
1429 Effect
Policy::eval_principal(const Environment
& e
,
1430 boost::optional
<const rgw::auth::Identity
&> ida
, boost::optional
<PolicyPrincipal
&> princ_type
) const {
1431 auto allowed
= false;
1432 for (auto& s
: statements
) {
1433 auto g
= s
.eval_principal(e
, ida
, princ_type
);
1434 if (g
== Effect::Deny
) {
1436 } else if (g
== Effect::Allow
) {
1440 return allowed
? Effect::Allow
: Effect::Deny
;
1443 Effect
Policy::eval_conditions(const Environment
& e
) const {
1444 auto allowed
= false;
1445 for (auto& s
: statements
) {
1446 auto g
= s
.eval_conditions(e
);
1447 if (g
== Effect::Deny
) {
1449 } else if (g
== Effect::Allow
) {
1453 return allowed
? Effect::Allow
: Effect::Deny
;
1456 ostream
& operator <<(ostream
& m
, const Policy
& p
) {
1458 << (p
.version
== Version::v2008_10_17
? "2008-10-17" : "2012-10-17");
1460 if (p
.id
|| !p
.statements
.empty()) {
1465 m
<< "Id: " << *p
.id
;
1466 if (!p
.statements
.empty()) {
1471 if (!p
.statements
.empty()) {
1472 m
<< "Statements: ";
1473 print_array(m
, p
.statements
.cbegin(), p
.statements
.cend());
1479 static const Environment iam_all_env
= {
1480 {"aws:SourceIp","1.1.1.1"},
1481 {"aws:UserId","anonymous"},
1482 {"s3:x-amz-server-side-encryption-aws-kms-key-id","secret"}
1485 struct IsPublicStatement
1487 bool operator() (const Statement
&s
) const {
1488 if (s
.effect
== Effect::Allow
) {
1489 for (const auto& p
: s
.princ
) {
1490 if (p
.is_wildcard()) {
1491 return s
.eval_conditions(iam_all_env
) == Effect::Allow
;
1494 // no princ should not contain fixed values
1495 return std::none_of(s
.noprinc
.begin(), s
.noprinc
.end(), [](const rgw::auth::Principal
& p
) {
1496 return p
.is_wildcard();
1504 bool is_public(const Policy
& p
)
1506 return std::any_of(p
.statements
.begin(), p
.statements
.end(), IsPublicStatement());