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
;
33 using std::stringstream
;
37 using std::unordered_map
;
39 using boost::container::flat_set
;
41 using std::regex_constants::ECMAScript
;
42 using std::regex_constants::optimize
;
43 using std::regex_match
;
46 using rapidjson::BaseReaderHandler
;
47 using rapidjson::UTF8
;
48 using rapidjson::SizeType
;
49 using rapidjson::Reader
;
50 using rapidjson::kParseCommentsFlag
;
51 using rapidjson::kParseNumbersAsStringsFlag
;
52 using rapidjson::StringStream
;
53 using rapidjson::ParseResult
;
55 using rgw::auth::Principal
;
59 #include "rgw_iam_policy_keywords.frag.cc"
68 static const actpair actpairs
[] =
69 {{ "s3:AbortMultipartUpload", s3AbortMultipartUpload
},
70 { "s3:CreateBucket", s3CreateBucket
},
71 { "s3:DeleteBucketPolicy", s3DeleteBucketPolicy
},
72 { "s3:DeleteBucket", s3DeleteBucket
},
73 { "s3:DeleteBucketWebsite", s3DeleteBucketWebsite
},
74 { "s3:DeleteObject", s3DeleteObject
},
75 { "s3:DeleteObjectVersion", s3DeleteObjectVersion
},
76 { "s3:DeleteObjectTagging", s3DeleteObjectTagging
},
77 { "s3:DeleteObjectVersionTagging", s3DeleteObjectVersionTagging
},
78 { "s3:DeleteBucketPublicAccessBlock", s3DeleteBucketPublicAccessBlock
},
79 { "s3:DeletePublicAccessBlock", s3DeletePublicAccessBlock
},
80 { "s3:DeleteReplicationConfiguration", s3DeleteReplicationConfiguration
},
81 { "s3:GetAccelerateConfiguration", s3GetAccelerateConfiguration
},
82 { "s3:GetBucketAcl", s3GetBucketAcl
},
83 { "s3:GetBucketCORS", s3GetBucketCORS
},
84 { "s3:GetBucketEncryption", s3GetBucketEncryption
},
85 { "s3:GetBucketLocation", s3GetBucketLocation
},
86 { "s3:GetBucketLogging", s3GetBucketLogging
},
87 { "s3:GetBucketNotification", s3GetBucketNotification
},
88 { "s3:GetBucketPolicy", s3GetBucketPolicy
},
89 { "s3:GetBucketPolicyStatus", s3GetBucketPolicyStatus
},
90 { "s3:GetBucketPublicAccessBlock", s3GetBucketPublicAccessBlock
},
91 { "s3:GetBucketRequestPayment", s3GetBucketRequestPayment
},
92 { "s3:GetBucketTagging", s3GetBucketTagging
},
93 { "s3:GetBucketVersioning", s3GetBucketVersioning
},
94 { "s3:GetBucketWebsite", s3GetBucketWebsite
},
95 { "s3:GetLifecycleConfiguration", s3GetLifecycleConfiguration
},
96 { "s3:GetBucketObjectLockConfiguration", s3GetBucketObjectLockConfiguration
},
97 { "s3:GetPublicAccessBlock", s3GetPublicAccessBlock
},
98 { "s3:GetObjectAcl", s3GetObjectAcl
},
99 { "s3:GetObject", s3GetObject
},
100 { "s3:GetObjectTorrent", s3GetObjectTorrent
},
101 { "s3:GetObjectVersionAcl", s3GetObjectVersionAcl
},
102 { "s3:GetObjectVersion", s3GetObjectVersion
},
103 { "s3:GetObjectVersionTorrent", s3GetObjectVersionTorrent
},
104 { "s3:GetObjectTagging", s3GetObjectTagging
},
105 { "s3:GetObjectVersionTagging", s3GetObjectVersionTagging
},
106 { "s3:GetObjectRetention", s3GetObjectRetention
},
107 { "s3:GetObjectLegalHold", s3GetObjectLegalHold
},
108 { "s3:GetReplicationConfiguration", s3GetReplicationConfiguration
},
109 { "s3:ListAllMyBuckets", s3ListAllMyBuckets
},
110 { "s3:ListBucketMultipartUploads", s3ListBucketMultipartUploads
},
111 { "s3:ListBucket", s3ListBucket
},
112 { "s3:ListBucketVersions", s3ListBucketVersions
},
113 { "s3:ListMultipartUploadParts", s3ListMultipartUploadParts
},
114 { "s3:PutAccelerateConfiguration", s3PutAccelerateConfiguration
},
115 { "s3:PutBucketAcl", s3PutBucketAcl
},
116 { "s3:PutBucketCORS", s3PutBucketCORS
},
117 { "s3:PutBucketEncryption", s3PutBucketEncryption
},
118 { "s3:PutBucketLogging", s3PutBucketLogging
},
119 { "s3:PutBucketNotification", s3PutBucketNotification
},
120 { "s3:PutBucketPolicy", s3PutBucketPolicy
},
121 { "s3:PutBucketRequestPayment", s3PutBucketRequestPayment
},
122 { "s3:PutBucketTagging", s3PutBucketTagging
},
123 { "s3:PutBucketVersioning", s3PutBucketVersioning
},
124 { "s3:PutBucketWebsite", s3PutBucketWebsite
},
125 { "s3:PutLifecycleConfiguration", s3PutLifecycleConfiguration
},
126 { "s3:PutBucketObjectLockConfiguration", s3PutBucketObjectLockConfiguration
},
127 { "s3:PutObjectAcl", s3PutObjectAcl
},
128 { "s3:PutObject", s3PutObject
},
129 { "s3:PutObjectVersionAcl", s3PutObjectVersionAcl
},
130 { "s3:PutObjectTagging", s3PutObjectTagging
},
131 { "s3:PutObjectVersionTagging", s3PutObjectVersionTagging
},
132 { "s3:PutObjectRetention", s3PutObjectRetention
},
133 { "s3:PutObjectLegalHold", s3PutObjectLegalHold
},
134 { "s3:BypassGovernanceRetention", s3BypassGovernanceRetention
},
135 { "s3:PutBucketPublicAccessBlock", s3PutBucketPublicAccessBlock
},
136 { "s3:PutPublicAccessBlock", s3PutPublicAccessBlock
},
137 { "s3:PutReplicationConfiguration", s3PutReplicationConfiguration
},
138 { "s3:RestoreObject", s3RestoreObject
},
139 { "iam:PutUserPolicy", iamPutUserPolicy
},
140 { "iam:GetUserPolicy", iamGetUserPolicy
},
141 { "iam:DeleteUserPolicy", iamDeleteUserPolicy
},
142 { "iam:ListUserPolicies", iamListUserPolicies
},
143 { "iam:CreateRole", iamCreateRole
},
144 { "iam:DeleteRole", iamDeleteRole
},
145 { "iam:GetRole", iamGetRole
},
146 { "iam:ModifyRole", iamModifyRole
},
147 { "iam:ListRoles", iamListRoles
},
148 { "iam:PutRolePolicy", iamPutRolePolicy
},
149 { "iam:GetRolePolicy", iamGetRolePolicy
},
150 { "iam:ListRolePolicies", iamListRolePolicies
},
151 { "iam:DeleteRolePolicy", iamDeleteRolePolicy
},
152 { "iam:CreateOIDCProvider", iamCreateOIDCProvider
},
153 { "iam:DeleteOIDCProvider", iamDeleteOIDCProvider
},
154 { "iam:GetOIDCProvider", iamGetOIDCProvider
},
155 { "iam:ListOIDCProviders", iamListOIDCProviders
},
156 { "iam:TagRole", iamTagRole
},
157 { "iam:ListRoleTags", iamListRoleTags
},
158 { "iam:UntagRole", iamUntagRole
},
159 { "sts:AssumeRole", stsAssumeRole
},
160 { "sts:AssumeRoleWithWebIdentity", stsAssumeRoleWithWebIdentity
},
161 { "sts:GetSessionToken", stsGetSessionToken
},
162 { "sts:TagSession", stsTagSession
},
167 const Keyword top
[1]{"<Top>", TokenKind::pseudo
, TokenID::Top
, 0, false,
169 const Keyword cond_key
[1]{"<Condition Key>", TokenKind::cond_key
,
170 TokenID::CondKey
, 0, true, false};
176 bool arraying
= false;
177 bool objecting
= false;
178 bool cond_ifexists
= false;
182 ParseState(PolicyParser
* pp
, const Keyword
* w
)
190 if (w
->arrayable
&& !arraying
) {
199 bool key(const char* s
, size_t l
);
200 bool do_string(CephContext
* cct
, const char* s
, size_t l
);
201 bool number(const char* str
, size_t l
);
204 // If this confuses you, look up the Curiously Recurring Template Pattern
205 struct PolicyParser
: public BaseReaderHandler
<UTF8
<>, PolicyParser
> {
207 std::vector
<ParseState
> s
;
209 const string
& tenant
;
215 uint32_t dex(TokenID in
) const {
217 case TokenID::Version
:
221 case TokenID::Statement
:
225 case TokenID::Effect
:
227 case TokenID::Principal
:
229 case TokenID::NotPrincipal
:
231 case TokenID::Action
:
233 case TokenID::NotAction
:
235 case TokenID::Resource
:
237 case TokenID::NotResource
:
239 case TokenID::Condition
:
243 case TokenID::Federated
:
245 case TokenID::Service
:
247 case TokenID::CanonicalUser
:
253 bool test(TokenID in
) {
254 return seen
& dex(in
);
256 void set(TokenID in
) {
258 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
259 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
260 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
261 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
262 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
263 dex(TokenID::Federated
) | dex(TokenID::Service
) |
264 dex(TokenID::CanonicalUser
))) {
268 void set(std::initializer_list
<TokenID
> l
) {
271 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
272 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
273 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
274 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
275 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
276 dex(TokenID::Federated
) | dex(TokenID::Service
) |
277 dex(TokenID::CanonicalUser
))) {
282 void reset(TokenID in
) {
284 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
285 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
286 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
287 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
288 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
289 dex(TokenID::Federated
) | dex(TokenID::Service
) |
290 dex(TokenID::CanonicalUser
))) {
294 void reset(std::initializer_list
<TokenID
> l
) {
297 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
298 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
299 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
300 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
301 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
302 dex(TokenID::Federated
) | dex(TokenID::Service
) |
303 dex(TokenID::CanonicalUser
))) {
308 void reset(uint32_t& v
) {
313 PolicyParser(CephContext
* cct
, const string
& tenant
, Policy
& policy
)
314 : cct(cct
), tenant(tenant
), policy(policy
) {}
315 PolicyParser(const PolicyParser
& policy
) = delete;
319 s
.push_back({this, top
});
320 s
.back().objecting
= true;
324 return s
.back().obj_start();
326 bool EndObject(SizeType memberCount
) {
330 return s
.back().obj_end();
332 bool Key(const char* str
, SizeType length
, bool copy
) {
336 return s
.back().key(str
, length
);
339 bool String(const char* str
, SizeType length
, bool copy
) {
343 return s
.back().do_string(cct
, str
, length
);
345 bool RawNumber(const char* str
, SizeType length
, bool copy
) {
350 return s
.back().number(str
, length
);
357 return s
.back().array_start();
359 bool EndArray(SizeType
) {
364 return s
.back().array_end();
373 // I really despise this misfeature of C++.
375 bool ParseState::obj_end() {
388 bool ParseState::key(const char* s
, size_t l
) {
390 bool ifexists
= false;
391 if (w
->id
== TokenID::Condition
&& w
->kind
== TokenKind::statement
) {
392 static constexpr char IfExists
[] = "IfExists";
393 if (boost::algorithm::ends_with(std::string_view
{s
, l
}, IfExists
)) {
395 token_len
-= sizeof(IfExists
)-1;
398 auto k
= pp
->tokens
.lookup(s
, token_len
);
401 if (w
->kind
== TokenKind::cond_op
) {
403 auto& t
= pp
->policy
.statements
.back();
404 auto c_ife
= cond_ifexists
;
405 pp
->s
.emplace_back(pp
, cond_key
);
406 t
.conditions
.emplace_back(id
, s
, l
, c_ife
);
413 // If the token we're going with belongs within the condition at the
414 // top of the stack and we haven't already encountered it, push it
417 if ((((w
->id
== TokenID::Top
) && (k
->kind
== TokenKind::top
)) ||
419 ((w
->id
== TokenID::Statement
) && (k
->kind
== TokenKind::statement
)) ||
422 ((w
->id
== TokenID::Principal
|| w
->id
== TokenID::NotPrincipal
) &&
423 (k
->kind
== TokenKind::princ_type
))) &&
425 // Check that it hasn't been encountered. Note that this
426 // conjoins with the run of disjunctions above.
429 pp
->s
.emplace_back(pp
, k
);
431 } else if ((w
->id
== TokenID::Condition
) &&
432 (k
->kind
== TokenKind::cond_op
)) {
433 pp
->s
.emplace_back(pp
, k
);
434 pp
->s
.back().cond_ifexists
= ifexists
;
440 // I should just rewrite a few helper functions to use iterators,
441 // which will make all of this ever so much nicer.
442 static boost::optional
<Principal
> parse_principal(CephContext
* cct
, TokenID t
,
445 if ((t
== TokenID::AWS
) && (s
== "*")) {
446 return Principal::wildcard();
448 // Do nothing for now.
449 } else if (t
== TokenID::CanonicalUser
) {
451 } // AWS and Federated ARNs
452 else if (t
== TokenID::AWS
|| t
== TokenID::Federated
) {
453 if (auto a
= ARN::parse(s
)) {
454 if (a
->resource
== "root") {
455 return Principal::tenant(std::move(a
->account
));
458 static const char rx_str
[] = "([^/]*)/(.*)";
459 static const regex
rx(rx_str
, sizeof(rx_str
) - 1,
460 std::regex_constants::ECMAScript
|
461 std::regex_constants::optimize
);
463 if (regex_match(a
->resource
, match
, rx
) && match
.size() == 3) {
464 if (match
[1] == "user") {
465 return Principal::user(std::move(a
->account
),
469 if (match
[1] == "role") {
470 return Principal::role(std::move(a
->account
),
474 if (match
[1] == "oidc-provider") {
475 return Principal::oidc_provider(std::move(match
[2]));
477 if (match
[1] == "assumed-role") {
478 return Principal::assumed_role(std::move(a
->account
), match
[2]);
482 if (std::none_of(s
.begin(), s
.end(),
484 return (c
== ':') || (c
== '/');
486 // Since tenants are simply prefixes, there's no really good
487 // way to see if one exists or not. So we return the thing and
488 // let them try to match against it.
489 return Principal::tenant(std::move(s
));
494 ldout(cct
, 0) << "Supplied principal is discarded: " << s
<< dendl
;
498 bool ParseState::do_string(CephContext
* cct
, const char* s
, size_t l
) {
499 auto k
= pp
->tokens
.lookup(s
, l
);
500 Policy
& p
= pp
->policy
;
501 bool is_action
= false;
502 bool is_validaction
= false;
503 Statement
* t
= p
.statements
.empty() ? nullptr : &(p
.statements
.back());
506 if ((w
->id
== TokenID::Version
) && k
&&
507 k
->kind
== TokenKind::version_key
) {
508 p
.version
= static_cast<Version
>(k
->specific
);
509 } else if (w
->id
== TokenID::Id
) {
514 } else if (w
->id
== TokenID::Sid
) {
515 t
->sid
.emplace(s
, l
);
516 } else if ((w
->id
== TokenID::Effect
) && k
&&
517 k
->kind
== TokenKind::effect_key
) {
518 t
->effect
= static_cast<Effect
>(k
->specific
);
519 } else if (w
->id
== TokenID::Principal
&& s
&& *s
== '*') {
520 t
->princ
.emplace(Principal::wildcard());
521 } else if (w
->id
== TokenID::NotPrincipal
&& s
&& *s
== '*') {
522 t
->noprinc
.emplace(Principal::wildcard());
523 } else if ((w
->id
== TokenID::Action
) ||
524 (w
->id
== TokenID::NotAction
)) {
527 is_validaction
= true;
528 (w
->id
== TokenID::Action
?
529 t
->action
= allValue
: t
->notaction
= allValue
);
531 for (auto& p
: actpairs
) {
532 if (match_policy({s
, l
}, p
.name
, MATCH_POLICY_ACTION
)) {
533 is_validaction
= true;
534 (w
->id
== TokenID::Action
? t
->action
[p
.bit
] = 1 : t
->notaction
[p
.bit
] = 1);
536 if ((t
->action
& s3AllValue
) == s3AllValue
) {
537 t
->action
[s3All
] = 1;
539 if ((t
->notaction
& s3AllValue
) == s3AllValue
) {
540 t
->notaction
[s3All
] = 1;
542 if ((t
->action
& iamAllValue
) == iamAllValue
) {
543 t
->action
[iamAll
] = 1;
545 if ((t
->notaction
& iamAllValue
) == iamAllValue
) {
546 t
->notaction
[iamAll
] = 1;
548 if ((t
->action
& stsAllValue
) == stsAllValue
) {
549 t
->action
[stsAll
] = 1;
551 if ((t
->notaction
& stsAllValue
) == stsAllValue
) {
552 t
->notaction
[stsAll
] = 1;
556 } else if (w
->id
== TokenID::Resource
|| w
->id
== TokenID::NotResource
) {
557 auto a
= ARN::parse({s
, l
}, true);
558 // You can't specify resources for someone ELSE'S account.
559 if (a
&& (a
->account
.empty() || a
->account
== pp
->tenant
||
560 a
->account
== "*")) {
561 if (a
->account
.empty() || a
->account
== "*")
562 a
->account
= pp
->tenant
;
563 (w
->id
== TokenID::Resource
? t
->resource
: t
->notresource
)
564 .emplace(std::move(*a
));
566 ldout(cct
, 0) << "Supplied resource is discarded: " << string(s
, l
)
570 } else if (w
->kind
== TokenKind::cond_key
) {
571 auto& t
= pp
->policy
.statements
.back();
572 if (l
> 0 && *s
== '$') {
573 if (l
>= 2 && *(s
+1) == '{') {
574 if (l
> 0 && *(s
+l
-1) == '}') {
575 t
.conditions
.back().isruntime
= true;
583 t
.conditions
.back().vals
.emplace_back(s
, l
);
587 } else if (w
->kind
== TokenKind::princ_type
) {
588 if (pp
->s
.size() <= 1) {
591 auto& pri
= pp
->s
[pp
->s
.size() - 2].w
->id
== TokenID::Principal
?
592 t
->princ
: t
->noprinc
;
595 if (auto o
= parse_principal(pp
->cct
, w
->id
, string(s
, l
))) {
596 pri
.emplace(std::move(*o
));
609 if (is_action
&& !is_validaction
){
616 bool ParseState::number(const char* s
, size_t l
) {
618 if (w
->kind
== TokenKind::cond_key
) {
619 auto& t
= pp
->policy
.statements
.back();
620 t
.conditions
.back().vals
.emplace_back(s
, l
);
635 void ParseState::reset() {
639 bool ParseState::obj_start() {
640 if (w
->objectable
&& !objecting
) {
642 if (w
->id
== TokenID::Statement
) {
643 pp
->policy
.statements
.emplace_back();
653 bool ParseState::array_end() {
654 if (arraying
&& !objecting
) {
662 ostream
& operator <<(ostream
& m
, const MaskedIP
& ip
) {
663 // I have a theory about why std::bitset is the way it is.
665 for (int i
= 7; i
>= 0; --i
) {
667 for (int j
= 15; j
>= 0; --j
) {
668 hextet
|= (ip
.addr
[(i
* 16) + j
] << j
);
670 m
<< hex
<< (unsigned int) hextet
;
676 // It involves Satan.
677 for (int i
= 3; i
>= 0; --i
) {
679 for (int j
= 7; j
>= 0; --j
) {
680 b
|= (ip
.addr
[(i
* 8) + j
] << j
);
682 m
<< (unsigned int) b
;
688 m
<< "/" << dec
<< ip
.prefix
;
689 // It would explain a lot
693 bool Condition::eval(const Environment
& env
) const {
694 std::vector
<std::string
> runtime_vals
;
695 auto i
= env
.find(key
);
696 if (op
== TokenID::Null
) {
697 return i
== env
.end() ? true : false;
700 if (i
== env
.end()) {
701 if (op
== TokenID::ForAllValuesStringEquals
||
702 op
== TokenID::ForAllValuesStringEqualsIgnoreCase
||
703 op
== TokenID::ForAllValuesStringLike
) {
711 string k
= vals
.back();
712 k
.erase(0,2); //erase $, {
713 k
.erase(k
.length() - 1, 1); //erase }
714 const auto& it
= env
.equal_range(k
);
715 for (auto itr
= it
.first
; itr
!= it
.second
; itr
++) {
716 runtime_vals
.emplace_back(itr
->second
);
719 const auto& s
= i
->second
;
721 const auto& itr
= env
.equal_range(key
);
725 case TokenID::ForAnyValueStringEquals
:
726 case TokenID::StringEquals
:
727 return orrible(std::equal_to
<std::string
>(), itr
, isruntime
? runtime_vals
: vals
);
729 case TokenID::StringNotEquals
:
730 return orrible(std::not_fn(std::equal_to
<std::string
>()),
731 itr
, isruntime
? runtime_vals
: vals
);
733 case TokenID::ForAnyValueStringEqualsIgnoreCase
:
734 case TokenID::StringEqualsIgnoreCase
:
735 return orrible(ci_equal_to(), itr
, isruntime
? runtime_vals
: vals
);
737 case TokenID::StringNotEqualsIgnoreCase
:
738 return orrible(std::not_fn(ci_equal_to()), itr
, isruntime
? runtime_vals
: vals
);
740 case TokenID::ForAnyValueStringLike
:
741 case TokenID::StringLike
:
742 return orrible(string_like(), itr
, isruntime
? runtime_vals
: vals
);
744 case TokenID::StringNotLike
:
745 return orrible(std::not_fn(string_like()), itr
, isruntime
? runtime_vals
: vals
);
747 case TokenID::ForAllValuesStringEquals
:
748 return andible(std::equal_to
<std::string
>(), itr
, isruntime
? runtime_vals
: vals
);
750 case TokenID::ForAllValuesStringLike
:
751 return andible(string_like(), itr
, isruntime
? runtime_vals
: vals
);
753 case TokenID::ForAllValuesStringEqualsIgnoreCase
:
754 return andible(ci_equal_to(), itr
, isruntime
? runtime_vals
: vals
);
757 case TokenID::NumericEquals
:
758 return shortible(std::equal_to
<double>(), as_number
, s
, vals
);
760 case TokenID::NumericNotEquals
:
761 return shortible(std::not_fn(std::equal_to
<double>()),
765 case TokenID::NumericLessThan
:
766 return shortible(std::less
<double>(), as_number
, s
, vals
);
769 case TokenID::NumericLessThanEquals
:
770 return shortible(std::less_equal
<double>(), as_number
, s
, vals
);
772 case TokenID::NumericGreaterThan
:
773 return shortible(std::greater
<double>(), as_number
, s
, vals
);
775 case TokenID::NumericGreaterThanEquals
:
776 return shortible(std::greater_equal
<double>(), as_number
, s
, vals
);
779 case TokenID::DateEquals
:
780 return shortible(std::equal_to
<ceph::real_time
>(), as_date
, s
, vals
);
782 case TokenID::DateNotEquals
:
783 return shortible(std::not_fn(std::equal_to
<ceph::real_time
>()),
786 case TokenID::DateLessThan
:
787 return shortible(std::less
<ceph::real_time
>(), as_date
, s
, vals
);
790 case TokenID::DateLessThanEquals
:
791 return shortible(std::less_equal
<ceph::real_time
>(), as_date
, s
, vals
);
793 case TokenID::DateGreaterThan
:
794 return shortible(std::greater
<ceph::real_time
>(), as_date
, s
, vals
);
796 case TokenID::DateGreaterThanEquals
:
797 return shortible(std::greater_equal
<ceph::real_time
>(), as_date
, s
,
802 return shortible(std::equal_to
<bool>(), as_bool
, s
, vals
);
805 case TokenID::BinaryEquals
:
806 return shortible(std::equal_to
<ceph::bufferlist
>(), as_binary
, s
,
810 case TokenID::IpAddress
:
811 return shortible(std::equal_to
<MaskedIP
>(), as_network
, s
, vals
);
813 case TokenID::NotIpAddress
:
815 auto xc
= as_network(s
);
820 for (const string
& d
: vals
) {
821 auto xd
= as_network(d
);
834 // Amazon Resource Names! (Does S3 need this?)
835 TokenID::ArnEquals
, TokenID::ArnNotEquals
, TokenID::ArnLike
,
844 boost::optional
<MaskedIP
> Condition::as_network(const string
& s
) {
850 m
.v6
= (s
.find(':') == string::npos
) ? false : true;
852 auto slash
= s
.find('/');
853 if (slash
== string::npos
) {
854 m
.prefix
= m
.v6
? 128 : 32;
857 m
.prefix
= strtoul(s
.data() + slash
+ 1, &end
, 10);
858 if (*end
!= 0 || (m
.v6
&& m
.prefix
> 128) ||
859 (!m
.v6
&& m
.prefix
> 32)) {
867 if (slash
!= string::npos
) {
868 t
.assign(s
, 0, slash
);
874 if (inet_pton(AF_INET6
, p
->c_str(), static_cast<void*>(&a
)) != 1) {
878 m
.addr
|= Address(a
.s6_addr
[15]) << 0;
879 m
.addr
|= Address(a
.s6_addr
[14]) << 8;
880 m
.addr
|= Address(a
.s6_addr
[13]) << 16;
881 m
.addr
|= Address(a
.s6_addr
[12]) << 24;
882 m
.addr
|= Address(a
.s6_addr
[11]) << 32;
883 m
.addr
|= Address(a
.s6_addr
[10]) << 40;
884 m
.addr
|= Address(a
.s6_addr
[9]) << 48;
885 m
.addr
|= Address(a
.s6_addr
[8]) << 56;
886 m
.addr
|= Address(a
.s6_addr
[7]) << 64;
887 m
.addr
|= Address(a
.s6_addr
[6]) << 72;
888 m
.addr
|= Address(a
.s6_addr
[5]) << 80;
889 m
.addr
|= Address(a
.s6_addr
[4]) << 88;
890 m
.addr
|= Address(a
.s6_addr
[3]) << 96;
891 m
.addr
|= Address(a
.s6_addr
[2]) << 104;
892 m
.addr
|= Address(a
.s6_addr
[1]) << 112;
893 m
.addr
|= Address(a
.s6_addr
[0]) << 120;
896 if (inet_pton(AF_INET
, p
->c_str(), static_cast<void*>(&a
)) != 1) {
900 m
.addr
= ntohl(a
.s_addr
);
907 const char* condop_string(const TokenID t
) {
909 case TokenID::StringEquals
:
910 return "StringEquals";
912 case TokenID::StringNotEquals
:
913 return "StringNotEquals";
915 case TokenID::StringEqualsIgnoreCase
:
916 return "StringEqualsIgnoreCase";
918 case TokenID::StringNotEqualsIgnoreCase
:
919 return "StringNotEqualsIgnoreCase";
921 case TokenID::StringLike
:
924 case TokenID::StringNotLike
:
925 return "StringNotLike";
928 case TokenID::NumericEquals
:
929 return "NumericEquals";
931 case TokenID::NumericNotEquals
:
932 return "NumericNotEquals";
934 case TokenID::NumericLessThan
:
935 return "NumericLessThan";
937 case TokenID::NumericLessThanEquals
:
938 return "NumericLessThanEquals";
940 case TokenID::NumericGreaterThan
:
941 return "NumericGreaterThan";
943 case TokenID::NumericGreaterThanEquals
:
944 return "NumericGreaterThanEquals";
946 case TokenID::DateEquals
:
949 case TokenID::DateNotEquals
:
950 return "DateNotEquals";
952 case TokenID::DateLessThan
:
953 return "DateLessThan";
955 case TokenID::DateLessThanEquals
:
956 return "DateLessThanEquals";
958 case TokenID::DateGreaterThan
:
959 return "DateGreaterThan";
961 case TokenID::DateGreaterThanEquals
:
962 return "DateGreaterThanEquals";
967 case TokenID::BinaryEquals
:
968 return "BinaryEquals";
970 case TokenID::IpAddress
:
971 return "case TokenID::IpAddress";
973 case TokenID::NotIpAddress
:
974 return "NotIpAddress";
976 case TokenID::ArnEquals
:
979 case TokenID::ArnNotEquals
:
980 return "ArnNotEquals";
982 case TokenID::ArnLike
:
985 case TokenID::ArnNotLike
:
992 return "InvalidConditionOperator";
996 template<typename Iterator
>
997 ostream
& print_array(ostream
& m
, Iterator begin
, Iterator end
) {
1002 std::copy(begin
, end
, std::experimental::make_ostream_joiner(m
, ", "));
1008 template<typename Iterator
>
1009 ostream
& print_dict(ostream
& m
, Iterator begin
, Iterator end
) {
1011 std::copy(begin
, end
, std::experimental::make_ostream_joiner(m
, ", "));
1018 ostream
& operator <<(ostream
& m
, const Condition
& c
) {
1019 m
<< condop_string(c
.op
);
1023 m
<< ": { " << c
.key
;
1024 print_array(m
, c
.vals
.cbegin(), c
.vals
.cend());
1028 Effect
Statement::eval(const Environment
& e
,
1029 boost::optional
<const rgw::auth::Identity
&> ida
,
1030 uint64_t act
, boost::optional
<const ARN
&> res
, boost::optional
<PolicyPrincipal
&> princ_type
) const {
1032 if (eval_principal(e
, ida
, princ_type
) == Effect::Deny
) {
1033 return Effect::Pass
;
1036 if (res
&& resource
.empty() && notresource
.empty()) {
1037 return Effect::Pass
;
1039 if (!res
&& (!resource
.empty() || !notresource
.empty())) {
1040 return Effect::Pass
;
1042 if (!resource
.empty() && res
) {
1043 if (!std::any_of(resource
.begin(), resource
.end(),
1044 [&res
](const ARN
& pattern
) {
1045 return pattern
.match(*res
);
1047 return Effect::Pass
;
1049 } else if (!notresource
.empty() && res
) {
1050 if (std::any_of(notresource
.begin(), notresource
.end(),
1051 [&res
](const ARN
& pattern
) {
1052 return pattern
.match(*res
);
1054 return Effect::Pass
;
1058 if (!(action
[act
] == 1) || (notaction
[act
] == 1)) {
1059 return Effect::Pass
;
1062 if (std::all_of(conditions
.begin(),
1064 [&e
](const Condition
& c
) { return c
.eval(e
);})) {
1068 return Effect::Pass
;
1071 Effect
Statement::eval_principal(const Environment
& e
,
1072 boost::optional
<const rgw::auth::Identity
&> ida
, boost::optional
<PolicyPrincipal
&> princ_type
) const {
1074 *princ_type
= PolicyPrincipal::Other
;
1077 if (princ
.empty() && noprinc
.empty()) {
1078 return Effect::Deny
;
1080 if (ida
->get_identity_type() != TYPE_ROLE
&& !princ
.empty() && !ida
->is_identity(princ
)) {
1081 return Effect::Deny
;
1083 if (ida
->get_identity_type() == TYPE_ROLE
&& !princ
.empty()) {
1084 bool princ_matched
= false;
1085 for (auto p
: princ
) { // Check each principal to determine the type of the one that has matched
1086 boost::container::flat_set
<Principal
> id
;
1088 if (ida
->is_identity(id
)) {
1089 if (p
.is_assumed_role() || p
.is_user()) {
1090 if (princ_type
) *princ_type
= PolicyPrincipal::Session
;
1092 if (princ_type
) *princ_type
= PolicyPrincipal::Role
;
1094 princ_matched
= true;
1097 if (!princ_matched
) {
1098 return Effect::Deny
;
1100 } else if (!noprinc
.empty() && ida
->is_identity(noprinc
)) {
1101 return Effect::Deny
;
1104 return Effect::Allow
;
1107 Effect
Statement::eval_conditions(const Environment
& e
) const {
1108 if (std::all_of(conditions
.begin(),
1110 [&e
](const Condition
& c
) { return c
.eval(e
);})) {
1111 return Effect::Allow
;
1113 return Effect::Deny
;
1117 const char* action_bit_string(uint64_t action
) {
1120 return "s3:GetObject";
1122 case s3GetObjectVersion
:
1123 return "s3:GetObjectVersion";
1126 return "s3:PutObject";
1128 case s3GetObjectAcl
:
1129 return "s3:GetObjectAcl";
1131 case s3GetObjectVersionAcl
:
1132 return "s3:GetObjectVersionAcl";
1134 case s3PutObjectAcl
:
1135 return "s3:PutObjectAcl";
1137 case s3PutObjectVersionAcl
:
1138 return "s3:PutObjectVersionAcl";
1140 case s3DeleteObject
:
1141 return "s3:DeleteObject";
1143 case s3DeleteObjectVersion
:
1144 return "s3:DeleteObjectVersion";
1146 case s3ListMultipartUploadParts
:
1147 return "s3:ListMultipartUploadParts";
1149 case s3AbortMultipartUpload
:
1150 return "s3:AbortMultipartUpload";
1152 case s3GetObjectTorrent
:
1153 return "s3:GetObjectTorrent";
1155 case s3GetObjectVersionTorrent
:
1156 return "s3:GetObjectVersionTorrent";
1158 case s3RestoreObject
:
1159 return "s3:RestoreObject";
1161 case s3CreateBucket
:
1162 return "s3:CreateBucket";
1164 case s3DeleteBucket
:
1165 return "s3:DeleteBucket";
1168 return "s3:ListBucket";
1170 case s3ListBucketVersions
:
1171 return "s3:ListBucketVersions";
1172 case s3ListAllMyBuckets
:
1173 return "s3:ListAllMyBuckets";
1175 case s3ListBucketMultipartUploads
:
1176 return "s3:ListBucketMultipartUploads";
1178 case s3GetAccelerateConfiguration
:
1179 return "s3:GetAccelerateConfiguration";
1181 case s3PutAccelerateConfiguration
:
1182 return "s3:PutAccelerateConfiguration";
1184 case s3GetBucketAcl
:
1185 return "s3:GetBucketAcl";
1187 case s3PutBucketAcl
:
1188 return "s3:PutBucketAcl";
1190 case s3GetBucketCORS
:
1191 return "s3:GetBucketCORS";
1193 case s3PutBucketCORS
:
1194 return "s3:PutBucketCORS";
1196 case s3GetBucketEncryption
:
1197 return "s3:GetBucketEncryption";
1199 case s3PutBucketEncryption
:
1200 return "s3:PutBucketEncryption";
1202 case s3GetBucketVersioning
:
1203 return "s3:GetBucketVersioning";
1205 case s3PutBucketVersioning
:
1206 return "s3:PutBucketVersioning";
1208 case s3GetBucketRequestPayment
:
1209 return "s3:GetBucketRequestPayment";
1211 case s3PutBucketRequestPayment
:
1212 return "s3:PutBucketRequestPayment";
1214 case s3GetBucketLocation
:
1215 return "s3:GetBucketLocation";
1217 case s3GetBucketPolicy
:
1218 return "s3:GetBucketPolicy";
1220 case s3DeleteBucketPolicy
:
1221 return "s3:DeleteBucketPolicy";
1223 case s3PutBucketPolicy
:
1224 return "s3:PutBucketPolicy";
1226 case s3GetBucketNotification
:
1227 return "s3:GetBucketNotification";
1229 case s3PutBucketNotification
:
1230 return "s3:PutBucketNotification";
1232 case s3GetBucketLogging
:
1233 return "s3:GetBucketLogging";
1235 case s3PutBucketLogging
:
1236 return "s3:PutBucketLogging";
1238 case s3GetBucketTagging
:
1239 return "s3:GetBucketTagging";
1241 case s3PutBucketTagging
:
1242 return "s3:PutBucketTagging";
1244 case s3GetBucketWebsite
:
1245 return "s3:GetBucketWebsite";
1247 case s3PutBucketWebsite
:
1248 return "s3:PutBucketWebsite";
1250 case s3DeleteBucketWebsite
:
1251 return "s3:DeleteBucketWebsite";
1253 case s3GetLifecycleConfiguration
:
1254 return "s3:GetLifecycleConfiguration";
1256 case s3PutLifecycleConfiguration
:
1257 return "s3:PutLifecycleConfiguration";
1259 case s3PutReplicationConfiguration
:
1260 return "s3:PutReplicationConfiguration";
1262 case s3GetReplicationConfiguration
:
1263 return "s3:GetReplicationConfiguration";
1265 case s3DeleteReplicationConfiguration
:
1266 return "s3:DeleteReplicationConfiguration";
1268 case s3PutObjectTagging
:
1269 return "s3:PutObjectTagging";
1271 case s3PutObjectVersionTagging
:
1272 return "s3:PutObjectVersionTagging";
1274 case s3GetObjectTagging
:
1275 return "s3:GetObjectTagging";
1277 case s3GetObjectVersionTagging
:
1278 return "s3:GetObjectVersionTagging";
1280 case s3DeleteObjectTagging
:
1281 return "s3:DeleteObjectTagging";
1283 case s3DeleteObjectVersionTagging
:
1284 return "s3:DeleteObjectVersionTagging";
1286 case s3PutBucketObjectLockConfiguration
:
1287 return "s3:PutBucketObjectLockConfiguration";
1289 case s3GetBucketObjectLockConfiguration
:
1290 return "s3:GetBucketObjectLockConfiguration";
1292 case s3PutObjectRetention
:
1293 return "s3:PutObjectRetention";
1295 case s3GetObjectRetention
:
1296 return "s3:GetObjectRetention";
1298 case s3PutObjectLegalHold
:
1299 return "s3:PutObjectLegalHold";
1301 case s3GetObjectLegalHold
:
1302 return "s3:GetObjectLegalHold";
1304 case s3BypassGovernanceRetention
:
1305 return "s3:BypassGovernanceRetention";
1307 case iamPutUserPolicy
:
1308 return "iam:PutUserPolicy";
1310 case iamGetUserPolicy
:
1311 return "iam:GetUserPolicy";
1313 case iamListUserPolicies
:
1314 return "iam:ListUserPolicies";
1316 case iamDeleteUserPolicy
:
1317 return "iam:DeleteUserPolicy";
1320 return "iam:CreateRole";
1323 return "iam:DeleteRole";
1326 return "iam:GetRole";
1329 return "iam:ModifyRole";
1332 return "iam:ListRoles";
1334 case iamPutRolePolicy
:
1335 return "iam:PutRolePolicy";
1337 case iamGetRolePolicy
:
1338 return "iam:GetRolePolicy";
1340 case iamListRolePolicies
:
1341 return "iam:ListRolePolicies";
1343 case iamDeleteRolePolicy
:
1344 return "iam:DeleteRolePolicy";
1346 case iamCreateOIDCProvider
:
1347 return "iam:CreateOIDCProvider";
1349 case iamDeleteOIDCProvider
:
1350 return "iam:DeleteOIDCProvider";
1352 case iamGetOIDCProvider
:
1353 return "iam:GetOIDCProvider";
1355 case iamListOIDCProviders
:
1356 return "iam:ListOIDCProviders";
1359 return "iam:TagRole";
1361 case iamListRoleTags
:
1362 return "iam:ListRoleTags";
1365 return "iam:UntagRole";
1368 return "sts:AssumeRole";
1370 case stsAssumeRoleWithWebIdentity
:
1371 return "sts:AssumeRoleWithWebIdentity";
1373 case stsGetSessionToken
:
1374 return "sts:GetSessionToken";
1377 return "sts:TagSession";
1382 ostream
& print_actions(ostream
& m
, const Action_t a
) {
1385 for (auto i
= 0U; i
< allCount
; ++i
) {
1392 m
<< action_bit_string(i
);
1404 ostream
& operator <<(ostream
& m
, const Statement
& s
) {
1407 m
<< "Sid: " << *s
.sid
<< ", ";
1409 if (!s
.princ
.empty()) {
1411 print_dict(m
, s
.princ
.cbegin(), s
.princ
.cend());
1414 if (!s
.noprinc
.empty()) {
1415 m
<< "NotPrincipal: ";
1416 print_dict(m
, s
.noprinc
.cbegin(), s
.noprinc
.cend());
1421 (s
.effect
== Effect::Allow
?
1422 (const char*) "Allow" :
1423 (const char*) "Deny");
1425 if (s
.action
.any() || s
.notaction
.any() || !s
.resource
.empty() ||
1426 !s
.notresource
.empty() || !s
.conditions
.empty()) {
1430 if (s
.action
.any()) {
1432 print_actions(m
, s
.action
);
1434 if (s
.notaction
.any() || !s
.resource
.empty() ||
1435 !s
.notresource
.empty() || !s
.conditions
.empty()) {
1440 if (s
.notaction
.any()) {
1442 print_actions(m
, s
.notaction
);
1444 if (!s
.resource
.empty() || !s
.notresource
.empty() ||
1445 !s
.conditions
.empty()) {
1450 if (!s
.resource
.empty()) {
1452 print_array(m
, s
.resource
.cbegin(), s
.resource
.cend());
1454 if (!s
.notresource
.empty() || !s
.conditions
.empty()) {
1459 if (!s
.notresource
.empty()) {
1460 m
<< "NotResource: ";
1461 print_array(m
, s
.notresource
.cbegin(), s
.notresource
.cend());
1463 if (!s
.conditions
.empty()) {
1468 if (!s
.conditions
.empty()) {
1470 print_dict(m
, s
.conditions
.cbegin(), s
.conditions
.cend());
1476 Policy::Policy(CephContext
* cct
, const string
& tenant
,
1477 const bufferlist
& _text
)
1478 : text(_text
.to_str()) {
1479 StringStream
ss(text
.data());
1480 PolicyParser
pp(cct
, tenant
, *this);
1481 auto pr
= Reader
{}.Parse
<kParseNumbersAsStringsFlag
|
1482 kParseCommentsFlag
>(ss
, pp
);
1484 throw PolicyParseException(std::move(pr
));
1488 Effect
Policy::eval(const Environment
& e
,
1489 boost::optional
<const rgw::auth::Identity
&> ida
,
1490 std::uint64_t action
, boost::optional
<const ARN
&> resource
,
1491 boost::optional
<PolicyPrincipal
&> princ_type
) const {
1492 auto allowed
= false;
1493 for (auto& s
: statements
) {
1494 auto g
= s
.eval(e
, ida
, action
, resource
, princ_type
);
1495 if (g
== Effect::Deny
) {
1497 } else if (g
== Effect::Allow
) {
1501 return allowed
? Effect::Allow
: Effect::Pass
;
1504 Effect
Policy::eval_principal(const Environment
& e
,
1505 boost::optional
<const rgw::auth::Identity
&> ida
, boost::optional
<PolicyPrincipal
&> princ_type
) const {
1506 auto allowed
= false;
1507 for (auto& s
: statements
) {
1508 auto g
= s
.eval_principal(e
, ida
, princ_type
);
1509 if (g
== Effect::Deny
) {
1511 } else if (g
== Effect::Allow
) {
1515 return allowed
? Effect::Allow
: Effect::Deny
;
1518 Effect
Policy::eval_conditions(const Environment
& e
) const {
1519 auto allowed
= false;
1520 for (auto& s
: statements
) {
1521 auto g
= s
.eval_conditions(e
);
1522 if (g
== Effect::Deny
) {
1524 } else if (g
== Effect::Allow
) {
1528 return allowed
? Effect::Allow
: Effect::Deny
;
1531 ostream
& operator <<(ostream
& m
, const Policy
& p
) {
1533 << (p
.version
== Version::v2008_10_17
? "2008-10-17" : "2012-10-17");
1535 if (p
.id
|| !p
.statements
.empty()) {
1540 m
<< "Id: " << *p
.id
;
1541 if (!p
.statements
.empty()) {
1546 if (!p
.statements
.empty()) {
1547 m
<< "Statements: ";
1548 print_array(m
, p
.statements
.cbegin(), p
.statements
.cend());
1554 static const Environment iam_all_env
= {
1555 {"aws:SourceIp","1.1.1.1"},
1556 {"aws:UserId","anonymous"},
1557 {"s3:x-amz-server-side-encryption-aws-kms-key-id","secret"}
1560 struct IsPublicStatement
1562 bool operator() (const Statement
&s
) const {
1563 if (s
.effect
== Effect::Allow
) {
1564 for (const auto& p
: s
.princ
) {
1565 if (p
.is_wildcard()) {
1566 return s
.eval_conditions(iam_all_env
) == Effect::Allow
;
1569 // no princ should not contain fixed values
1570 return std::none_of(s
.noprinc
.begin(), s
.noprinc
.end(), [](const rgw::auth::Principal
& p
) {
1571 return p
.is_wildcard();
1579 bool is_public(const Policy
& p
)
1581 return std::any_of(p
.statements
.begin(), p
.statements
.end(), IsPublicStatement());