1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
10 #include <boost/regex.hpp>
12 #include "rapidjson/reader.h"
14 #include "common/backport14.h"
16 #include <arpa/inet.h>
17 #include "rgw_iam_policy.h"
20 constexpr int dout_subsys
= ceph_subsys_rgw
;
30 using std::stringstream
;
34 using std::unordered_map
;
36 using boost::container::flat_set
;
38 using boost::optional
;
40 using boost::regex_constants::ECMAScript
;
41 using boost::regex_constants::optimize
;
42 using boost::regex_match
;
45 using rapidjson::BaseReaderHandler
;
46 using rapidjson::UTF8
;
47 using rapidjson::SizeType
;
48 using rapidjson::Reader
;
49 using rapidjson::kParseCommentsFlag
;
50 using rapidjson::kParseNumbersAsStringsFlag
;
51 using rapidjson::StringStream
;
52 using rapidjson::ParseResult
;
54 using rgw::auth::Principal
;
58 #include "rgw_iam_policy_keywords.frag.cc"
66 optional
<Partition
> to_partition(const smatch::value_type
& p
,
69 return Partition::aws
;
70 } else if (p
== "aws-cn") {
71 return Partition::aws_cn
;
72 } else if (p
== "aws-us-gov") {
73 return Partition::aws_us_gov
;
74 } else if (p
== "*" && wildcards
) {
75 return Partition::wildcard
;
83 optional
<Service
> to_service(const smatch::value_type
& s
,
85 static const unordered_map
<string
, Service
> services
= {
86 { "acm", Service::acm
},
87 { "apigateway", Service::apigateway
},
88 { "appstream", Service::appstream
},
89 { "artifact", Service::artifact
},
90 { "autoscaling", Service::autoscaling
},
91 { "aws-marketplace", Service::aws_marketplace
},
92 { "aws-marketplace-management",
93 Service::aws_marketplace_management
},
94 { "aws-portal", Service::aws_portal
},
95 { "cloudformation", Service::cloudformation
},
96 { "cloudfront", Service::cloudfront
},
97 { "cloudhsm", Service::cloudhsm
},
98 { "cloudsearch", Service::cloudsearch
},
99 { "cloudtrail", Service::cloudtrail
},
100 { "cloudwatch", Service::cloudwatch
},
101 { "codebuild", Service::codebuild
},
102 { "codecommit", Service::codecommit
},
103 { "codedeploy", Service::codedeploy
},
104 { "codepipeline", Service::codepipeline
},
105 { "cognito-identity", Service::cognito_identity
},
106 { "cognito-idp", Service::cognito_idp
},
107 { "cognito-sync", Service::cognito_sync
},
108 { "config", Service::config
},
109 { "datapipeline", Service::datapipeline
},
110 { "devicefarm", Service::devicefarm
},
111 { "directconnect", Service::directconnect
},
112 { "dms", Service::dms
},
113 { "ds", Service::ds
},
114 { "dynamodb", Service::dynamodb
},
115 { "ec2", Service::ec2
},
116 { "ecr", Service::ecr
},
117 { "ecs", Service::ecs
},
118 { "elasticache", Service::elasticache
},
119 { "elasticbeanstalk", Service::elasticbeanstalk
},
120 { "elasticfilesystem", Service::elasticfilesystem
},
121 { "elasticloadbalancing", Service::elasticloadbalancing
},
122 { "elasticmapreduce", Service::elasticmapreduce
},
123 { "elastictranscoder", Service::elastictranscoder
},
124 { "es", Service::es
},
125 { "events", Service::events
},
126 { "firehose", Service::firehose
},
127 { "gamelift", Service::gamelift
},
128 { "glacier", Service::glacier
},
129 { "health", Service::health
},
130 { "iam", Service::iam
},
131 { "importexport", Service::importexport
},
132 { "inspector", Service::inspector
},
133 { "iot", Service::iot
},
134 { "kinesis", Service::kinesis
},
135 { "kinesisanalytics", Service::kinesisanalytics
},
136 { "kms", Service::kms
},
137 { "lambda", Service::lambda
},
138 { "lightsail", Service::lightsail
},
139 { "logs", Service::logs
},
140 { "machinelearning", Service::machinelearning
},
141 { "mobileanalytics", Service::mobileanalytics
},
142 { "mobilehub", Service::mobilehub
},
143 { "opsworks", Service::opsworks
},
144 { "opsworks-cm", Service::opsworks_cm
},
145 { "polly", Service::polly
},
146 { "rds", Service::rds
},
147 { "redshift", Service::redshift
},
148 { "route53", Service::route53
},
149 { "route53domains", Service::route53domains
},
150 { "s3", Service::s3
},
151 { "sdb", Service::sdb
},
152 { "servicecatalog", Service::servicecatalog
},
153 { "ses", Service::ses
},
154 { "sns", Service::sns
},
155 { "sqs", Service::sqs
},
156 { "ssm", Service::ssm
},
157 { "states", Service::states
},
158 { "storagegateway", Service::storagegateway
},
159 { "sts", Service::sts
},
160 { "support", Service::support
},
161 { "swf", Service::swf
},
162 { "trustedadvisor", Service::trustedadvisor
},
163 { "waf", Service::waf
},
164 { "workmail", Service::workmail
},
165 { "workspaces", Service::workspaces
}};
167 if (wildcards
&& s
== "*") {
168 return Service::wildcard
;
171 auto i
= services
.find(s
);
172 if (i
== services
.end()) {
180 ARN::ARN(const rgw_obj
& o
)
181 : partition(Partition::aws
),
182 service(Service::s3
),
184 account(o
.bucket
.tenant
),
185 resource(o
.bucket
.name
)
187 resource
.push_back('/');
188 resource
.append(o
.key
.name
);
191 ARN::ARN(const rgw_bucket
& b
)
192 : partition(Partition::aws
),
193 service(Service::s3
),
198 ARN::ARN(const rgw_bucket
& b
, const string
& o
)
199 : partition(Partition::aws
),
200 service(Service::s3
),
204 resource
.push_back('/');
208 optional
<ARN
> ARN::parse(const string
& s
, bool wildcards
) {
209 static const char str_wild
[] = "arn:([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)";
210 static const regex
rx_wild(str_wild
,
211 sizeof(str_wild
) - 1,
212 ECMAScript
| optimize
);
213 static const char str_no_wild
[]
214 = "arn:([^:*]*):([^:*]*):([^:*]*):([^:*]*):([^:*]*)";
215 static const regex
rx_no_wild(str_no_wild
,
216 sizeof(str_no_wild
) - 1,
217 ECMAScript
| optimize
);
221 if ((s
== "*") && wildcards
) {
222 return ARN(Partition::wildcard
, Service::wildcard
, "*", "*", "*");
223 } else if (regex_match(s
, match
, wildcards
? rx_wild
: rx_no_wild
) &&
225 if (auto p
= to_partition(match
[1], wildcards
)) {
226 if (auto s
= to_service(match
[2], wildcards
)) {
227 return ARN(*p
, *s
, match
[3], match
[4], match
[5]);
234 string
ARN::to_string() const {
237 if (partition
== Partition::aws
) {
239 } else if (partition
== Partition::aws_cn
) {
241 } else if (partition
== Partition::aws_us_gov
) {
242 s
.append("aws-us-gov:");
247 static const unordered_map
<Service
, string
> services
= {
248 { Service::acm
, "acm" },
249 { Service::apigateway
, "apigateway" },
250 { Service::appstream
, "appstream" },
251 { Service::artifact
, "artifact" },
252 { Service::autoscaling
, "autoscaling" },
253 { Service::aws_marketplace
, "aws-marketplace" },
254 { Service::aws_marketplace_management
, "aws-marketplace-management" },
255 { Service::aws_portal
, "aws-portal" },
256 { Service::cloudformation
, "cloudformation" },
257 { Service::cloudfront
, "cloudfront" },
258 { Service::cloudhsm
, "cloudhsm" },
259 { Service::cloudsearch
, "cloudsearch" },
260 { Service::cloudtrail
, "cloudtrail" },
261 { Service::cloudwatch
, "cloudwatch" },
262 { Service::codebuild
, "codebuild" },
263 { Service::codecommit
, "codecommit" },
264 { Service::codedeploy
, "codedeploy" },
265 { Service::codepipeline
, "codepipeline" },
266 { Service::cognito_identity
, "cognito-identity" },
267 { Service::cognito_idp
, "cognito-idp" },
268 { Service::cognito_sync
, "cognito-sync" },
269 { Service::config
, "config" },
270 { Service::datapipeline
, "datapipeline" },
271 { Service::devicefarm
, "devicefarm" },
272 { Service::directconnect
, "directconnect" },
273 { Service::dms
, "dms" },
274 { Service::ds
, "ds" },
275 { Service::dynamodb
, "dynamodb" },
276 { Service::ec2
, "ec2" },
277 { Service::ecr
, "ecr" },
278 { Service::ecs
, "ecs" },
279 { Service::elasticache
, "elasticache" },
280 { Service::elasticbeanstalk
, "elasticbeanstalk" },
281 { Service::elasticfilesystem
, "elasticfilesystem" },
282 { Service::elasticloadbalancing
, "elasticloadbalancing" },
283 { Service::elasticmapreduce
, "elasticmapreduce" },
284 { Service::elastictranscoder
, "elastictranscoder" },
285 { Service::es
, "es" },
286 { Service::events
, "events" },
287 { Service::firehose
, "firehose" },
288 { Service::gamelift
, "gamelift" },
289 { Service::glacier
, "glacier" },
290 { Service::health
, "health" },
291 { Service::iam
, "iam" },
292 { Service::importexport
, "importexport" },
293 { Service::inspector
, "inspector" },
294 { Service::iot
, "iot" },
295 { Service::kinesis
, "kinesis" },
296 { Service::kinesisanalytics
, "kinesisanalytics" },
297 { Service::kms
, "kms" },
298 { Service::lambda
, "lambda" },
299 { Service::lightsail
, "lightsail" },
300 { Service::logs
, "logs" },
301 { Service::machinelearning
, "machinelearning" },
302 { Service::mobileanalytics
, "mobileanalytics" },
303 { Service::mobilehub
, "mobilehub" },
304 { Service::opsworks
, "opsworks" },
305 { Service::opsworks_cm
, "opsworks-cm" },
306 { Service::polly
, "polly" },
307 { Service::rds
, "rds" },
308 { Service::redshift
, "redshift" },
309 { Service::route53
, "route53" },
310 { Service::route53domains
, "route53domains" },
311 { Service::s3
, "s3" },
312 { Service::sdb
, "sdb" },
313 { Service::servicecatalog
, "servicecatalog" },
314 { Service::ses
, "ses" },
315 { Service::sns
, "sns" },
316 { Service::sqs
, "sqs" },
317 { Service::ssm
, "ssm" },
318 { Service::states
, "states" },
319 { Service::storagegateway
, "storagegateway" },
320 { Service::sts
, "sts" },
321 { Service::support
, "support" },
322 { Service::swf
, "swf" },
323 { Service::trustedadvisor
, "trustedadvisor" },
324 { Service::waf
, "waf" },
325 { Service::workmail
, "workmail" },
326 { Service::workspaces
, "workspaces" }};
328 auto i
= services
.find(service
);
329 if (i
!= services
.end()) {
347 bool operator ==(const ARN
& l
, const ARN
& r
) {
348 return ((l
.partition
== r
.partition
) &&
349 (l
.service
== r
.service
) &&
350 (l
.region
== r
.region
) &&
351 (l
.account
== r
.account
) &&
352 (l
.resource
== r
.resource
));
354 bool operator <(const ARN
& l
, const ARN
& r
) {
355 return ((l
.partition
< r
.partition
) ||
356 (l
.service
< r
.service
) ||
357 (l
.region
< r
.region
) ||
358 (l
.account
< r
.account
) ||
359 (l
.resource
< r
.resource
));
362 // The candidate is not allowed to have wildcards. The only way to
363 // do that sanely would be to use unification rather than matching.
364 bool ARN::match(const ARN
& candidate
) const {
365 if ((candidate
.partition
== Partition::wildcard
) ||
366 (partition
!= candidate
.partition
&& partition
367 != Partition::wildcard
)) {
371 if ((candidate
.service
== Service::wildcard
) ||
372 (service
!= candidate
.service
&& service
!= Service::wildcard
)) {
376 if (!match_policy(region
, candidate
.region
, MATCH_POLICY_ARN
)) {
380 if (!match_policy(account
, candidate
.account
, MATCH_POLICY_ARN
)) {
384 if (!match_policy(resource
, candidate
.resource
, MATCH_POLICY_ARN
)) {
391 static const actpair actpairs
[] =
392 {{ "s3:AbortMultipartUpload", s3AbortMultipartUpload
},
393 { "s3:CreateBucket", s3CreateBucket
},
394 { "s3:DeleteBucketPolicy", s3DeleteBucketPolicy
},
395 { "s3:DeleteBucket", s3DeleteBucket
},
396 { "s3:DeleteBucketWebsite", s3DeleteBucketWebsite
},
397 { "s3:DeleteObject", s3DeleteObject
},
398 { "s3:DeleteObjectVersion", s3DeleteObjectVersion
},
399 { "s3:DeleteObjectTagging", s3DeleteObjectTagging
},
400 { "s3:DeleteObjectVersionTagging", s3DeleteObjectVersionTagging
},
401 { "s3:DeleteReplicationConfiguration", s3DeleteReplicationConfiguration
},
402 { "s3:GetAccelerateConfiguration", s3GetAccelerateConfiguration
},
403 { "s3:GetBucketAcl", s3GetBucketAcl
},
404 { "s3:GetBucketCORS", s3GetBucketCORS
},
405 { "s3:GetBucketLocation", s3GetBucketLocation
},
406 { "s3:GetBucketLogging", s3GetBucketLogging
},
407 { "s3:GetBucketNotification", s3GetBucketNotification
},
408 { "s3:GetBucketPolicy", s3GetBucketPolicy
},
409 { "s3:GetBucketRequestPayment", s3GetBucketRequestPayment
},
410 { "s3:GetBucketTagging", s3GetBucketTagging
},
411 { "s3:GetBucketVersioning", s3GetBucketVersioning
},
412 { "s3:GetBucketWebsite", s3GetBucketWebsite
},
413 { "s3:GetLifecycleConfiguration", s3GetLifecycleConfiguration
},
414 { "s3:GetObjectAcl", s3GetObjectAcl
},
415 { "s3:GetObject", s3GetObject
},
416 { "s3:GetObjectTorrent", s3GetObjectTorrent
},
417 { "s3:GetObjectVersionAcl", s3GetObjectVersionAcl
},
418 { "s3:GetObjectVersion", s3GetObjectVersion
},
419 { "s3:GetObjectVersionTorrent", s3GetObjectVersionTorrent
},
420 { "s3:GetObjectTagging", s3GetObjectTagging
},
421 { "s3:GetObjectVersionTagging", s3GetObjectVersionTagging
},
422 { "s3:GetReplicationConfiguration", s3GetReplicationConfiguration
},
423 { "s3:ListAllMyBuckets", s3ListAllMyBuckets
},
424 { "s3:ListBucketMultipartUploads", s3ListBucketMultipartUploads
},
425 { "s3:ListBucket", s3ListBucket
},
426 { "s3:ListBucketVersions", s3ListBucketVersions
},
427 { "s3:ListMultipartUploadParts", s3ListMultipartUploadParts
},
428 { "s3:PutAccelerateConfiguration", s3PutAccelerateConfiguration
},
429 { "s3:PutBucketAcl", s3PutBucketAcl
},
430 { "s3:PutBucketCORS", s3PutBucketCORS
},
431 { "s3:PutBucketLogging", s3PutBucketLogging
},
432 { "s3:PutBucketNotification", s3PutBucketNotification
},
433 { "s3:PutBucketPolicy", s3PutBucketPolicy
},
434 { "s3:PutBucketRequestPayment", s3PutBucketRequestPayment
},
435 { "s3:PutBucketTagging", s3PutBucketTagging
},
436 { "s3:PutBucketVersioning", s3PutBucketVersioning
},
437 { "s3:PutBucketWebsite", s3PutBucketWebsite
},
438 { "s3:PutLifecycleConfiguration", s3PutLifecycleConfiguration
},
439 { "s3:PutObjectAcl", s3PutObjectAcl
},
440 { "s3:PutObject", s3PutObject
},
441 { "s3:PutObjectVersionAcl", s3PutObjectVersionAcl
},
442 { "s3:PutObjectTagging", s3PutObjectTagging
},
443 { "s3:PutObjectVersionTagging", s3PutObjectVersionTagging
},
444 { "s3:PutReplicationConfiguration", s3PutReplicationConfiguration
},
445 { "s3:RestoreObject", s3RestoreObject
}};
449 const Keyword top
[1]{"<Top>", TokenKind::pseudo
, TokenID::Top
, 0, false,
451 const Keyword cond_key
[1]{"<Condition Key>", TokenKind::cond_key
,
452 TokenID::CondKey
, 0, true, false};
458 bool arraying
= false;
459 bool objecting
= false;
460 bool cond_ifexists
= false;
464 ParseState(PolicyParser
* pp
, const Keyword
* w
)
472 if (w
->arrayable
&& !arraying
) {
481 bool key(const char* s
, size_t l
);
482 bool do_string(CephContext
* cct
, const char* s
, size_t l
);
483 bool number(const char* str
, size_t l
);
486 // If this confuses you, look up the Curiously Recurring Template Pattern
487 struct PolicyParser
: public BaseReaderHandler
<UTF8
<>, PolicyParser
> {
489 std::vector
<ParseState
> s
;
491 const string
& tenant
;
497 uint32_t dex(TokenID in
) const {
499 case TokenID::Version
:
503 case TokenID::Statement
:
507 case TokenID::Effect
:
509 case TokenID::Principal
:
511 case TokenID::NotPrincipal
:
513 case TokenID::Action
:
515 case TokenID::NotAction
:
517 case TokenID::Resource
:
519 case TokenID::NotResource
:
521 case TokenID::Condition
:
525 case TokenID::Federated
:
527 case TokenID::Service
:
529 case TokenID::CanonicalUser
:
535 bool test(TokenID in
) {
536 return seen
& dex(in
);
538 void set(TokenID in
) {
540 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
541 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
542 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
543 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
544 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
545 dex(TokenID::Federated
) | dex(TokenID::Service
) |
546 dex(TokenID::CanonicalUser
))) {
550 void set(std::initializer_list
<TokenID
> l
) {
553 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
554 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
555 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
556 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
557 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
558 dex(TokenID::Federated
) | dex(TokenID::Service
) |
559 dex(TokenID::CanonicalUser
))) {
564 void reset(TokenID in
) {
566 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
567 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
568 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
569 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
570 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
571 dex(TokenID::Federated
) | dex(TokenID::Service
) |
572 dex(TokenID::CanonicalUser
))) {
576 void reset(std::initializer_list
<TokenID
> l
) {
579 if (dex(in
) & (dex(TokenID::Sid
) | dex(TokenID::Effect
) |
580 dex(TokenID::Principal
) | dex(TokenID::NotPrincipal
) |
581 dex(TokenID::Action
) | dex(TokenID::NotAction
) |
582 dex(TokenID::Resource
) | dex(TokenID::NotResource
) |
583 dex(TokenID::Condition
) | dex(TokenID::AWS
) |
584 dex(TokenID::Federated
) | dex(TokenID::Service
) |
585 dex(TokenID::CanonicalUser
))) {
590 void reset(uint32_t& v
) {
595 PolicyParser(CephContext
* cct
, const string
& tenant
, Policy
& policy
)
596 : cct(cct
), tenant(tenant
), policy(policy
) {}
597 PolicyParser(const PolicyParser
& policy
) = delete;
601 s
.push_back({this, top
});
602 s
.back().objecting
= true;
606 return s
.back().obj_start();
608 bool EndObject(SizeType memberCount
) {
612 return s
.back().obj_end();
614 bool Key(const char* str
, SizeType length
, bool copy
) {
618 return s
.back().key(str
, length
);
621 bool String(const char* str
, SizeType length
, bool copy
) {
625 return s
.back().do_string(cct
, str
, length
);
627 bool RawNumber(const char* str
, SizeType length
, bool copy
) {
632 return s
.back().number(str
, length
);
639 return s
.back().array_start();
641 bool EndArray(SizeType
) {
646 return s
.back().array_end();
655 // I really despise this misfeature of C++.
657 bool ParseState::obj_end() {
670 bool ParseState::key(const char* s
, size_t l
) {
672 bool ifexists
= false;
673 if (w
->id
== TokenID::Condition
&& w
->kind
== TokenKind::statement
) {
674 static constexpr char IfExists
[] = "IfExists";
675 if (boost::algorithm::ends_with(boost::string_view
{s
, l
}, IfExists
)) {
677 token_len
-= sizeof(IfExists
)-1;
680 auto k
= pp
->tokens
.lookup(s
, token_len
);
683 if (w
->kind
== TokenKind::cond_op
) {
685 auto& t
= pp
->policy
.statements
.back();
686 auto c_ife
= cond_ifexists
;
687 pp
->s
.emplace_back(pp
, cond_key
);
688 t
.conditions
.emplace_back(id
, s
, l
, c_ife
);
695 // If the token we're going with belongs within the condition at the
696 // top of the stack and we haven't already encountered it, push it
699 if ((((w
->id
== TokenID::Top
) && (k
->kind
== TokenKind::top
)) ||
701 ((w
->id
== TokenID::Statement
) && (k
->kind
== TokenKind::statement
)) ||
704 ((w
->id
== TokenID::Principal
|| w
->id
== TokenID::NotPrincipal
) &&
705 (k
->kind
== TokenKind::princ_type
))) &&
707 // Check that it hasn't been encountered. Note that this
708 // conjoins with the run of disjunctions above.
711 pp
->s
.emplace_back(pp
, k
);
713 } else if ((w
->id
== TokenID::Condition
) &&
714 (k
->kind
== TokenKind::cond_op
)) {
715 pp
->s
.emplace_back(pp
, k
);
716 pp
->s
.back().cond_ifexists
= ifexists
;
722 // I should just rewrite a few helper functions to use iterators,
723 // which will make all of this ever so much nicer.
724 static optional
<Principal
> parse_principal(CephContext
* cct
, TokenID t
,
727 if ((t
== TokenID::AWS
) && (s
== "*")) {
728 return Principal::wildcard();
730 // Do nothing for now.
731 } else if (t
== TokenID::CanonicalUser
) {
734 } else if (t
== TokenID::AWS
) {
735 if (auto a
= ARN::parse(s
)) {
736 if (a
->resource
== "root") {
737 return Principal::tenant(std::move(a
->account
));
740 static const char rx_str
[] = "([^/]*)/(.*)";
741 static const regex
rx(rx_str
, sizeof(rx_str
) - 1,
742 ECMAScript
| optimize
);
744 if (regex_match(a
->resource
, match
, rx
) && match
.size() == 3) {
745 if (match
[1] == "user") {
746 return Principal::user(std::move(a
->account
),
750 if (match
[1] == "role") {
751 return Principal::role(std::move(a
->account
),
756 if (std::none_of(s
.begin(), s
.end(),
758 return (c
== ':') || (c
== '/');
760 // Since tenants are simply prefixes, there's no really good
761 // way to see if one exists or not. So we return the thing and
762 // let them try to match against it.
763 return Principal::tenant(std::move(s
));
768 ldout(cct
, 0) << "Supplied principal is discarded: " << s
<< dendl
;
772 bool ParseState::do_string(CephContext
* cct
, const char* s
, size_t l
) {
773 auto k
= pp
->tokens
.lookup(s
, l
);
774 Policy
& p
= pp
->policy
;
775 Statement
* t
= p
.statements
.empty() ? nullptr : &(p
.statements
.back());
778 if ((w
->id
== TokenID::Version
) && k
&&
779 k
->kind
== TokenKind::version_key
) {
780 p
.version
= static_cast<Version
>(k
->specific
);
781 } else if (w
->id
== TokenID::Id
) {
786 } else if (w
->id
== TokenID::Sid
) {
787 t
->sid
.emplace(s
, l
);
788 } else if ((w
->id
== TokenID::Effect
) && k
&&
789 k
->kind
== TokenKind::effect_key
) {
790 t
->effect
= static_cast<Effect
>(k
->specific
);
791 } else if (w
->id
== TokenID::Principal
&& s
&& *s
== '*') {
792 t
->princ
.emplace(Principal::wildcard());
793 } else if (w
->id
== TokenID::NotPrincipal
&& s
&& *s
== '*') {
794 t
->noprinc
.emplace(Principal::wildcard());
795 } else if ((w
->id
== TokenID::Action
) ||
796 (w
->id
== TokenID::NotAction
)) {
797 for (auto& p
: actpairs
) {
798 if (match_policy({s
, l
}, p
.name
, MATCH_POLICY_ACTION
)) {
799 (w
->id
== TokenID::Action
? t
->action
: t
->notaction
) |= p
.bit
;
802 } else if (w
->id
== TokenID::Resource
|| w
->id
== TokenID::NotResource
) {
803 auto a
= ARN::parse({s
, l
}, true);
804 // You can't specify resources for someone ELSE'S account.
805 if (a
&& (a
->account
.empty() || a
->account
== pp
->tenant
||
806 a
->account
== "*")) {
807 if (a
->account
.empty() || a
->account
== "*")
808 a
->account
= pp
->tenant
;
809 (w
->id
== TokenID::Resource
? t
->resource
: t
->notresource
)
810 .emplace(std::move(*a
));
813 ldout(cct
, 0) << "Supplied resource is discarded: " << string(s
, l
)
815 } else if (w
->kind
== TokenKind::cond_key
) {
816 auto& t
= pp
->policy
.statements
.back();
817 t
.conditions
.back().vals
.emplace_back(s
, l
);
821 } else if (w
->kind
== TokenKind::princ_type
) {
822 if (pp
->s
.size() <= 1) {
825 auto& pri
= pp
->s
[pp
->s
.size() - 2].w
->id
== TokenID::Principal
?
826 t
->princ
: t
->noprinc
;
829 if (auto o
= parse_principal(pp
->cct
, w
->id
, string(s
, l
))) {
830 pri
.emplace(std::move(*o
));
846 bool ParseState::number(const char* s
, size_t l
) {
848 if (w
->kind
== TokenKind::cond_key
) {
849 auto& t
= pp
->policy
.statements
.back();
850 t
.conditions
.back().vals
.emplace_back(s
, l
);
865 void ParseState::reset() {
869 bool ParseState::obj_start() {
870 if (w
->objectable
&& !objecting
) {
872 if (w
->id
== TokenID::Statement
) {
873 pp
->policy
.statements
.push_back({});
883 bool ParseState::array_end() {
884 if (arraying
&& !objecting
) {
892 ostream
& operator <<(ostream
& m
, const MaskedIP
& ip
) {
893 // I have a theory about why std::bitset is the way it is.
895 for (int i
= 7; i
>= 0; --i
) {
897 for (int j
= 15; j
>= 0; --j
) {
898 hextet
|= (ip
.addr
[(i
* 16) + j
] << j
);
900 m
<< hex
<< (unsigned int) hextet
;
906 // It involves Satan.
907 for (int i
= 3; i
>= 0; --i
) {
909 for (int j
= 7; j
>= 0; --j
) {
910 b
|= (ip
.addr
[(i
* 8) + j
] << j
);
912 m
<< (unsigned int) b
;
918 m
<< "/" << dec
<< ip
.prefix
;
919 // It would explain a lot
923 string
to_string(const MaskedIP
& m
) {
929 bool Condition::eval(const Environment
& env
) const {
930 auto i
= env
.find(key
);
931 if (op
== TokenID::Null
) {
932 return i
== env
.end() ? true : false;
935 if (i
== env
.end()) {
938 const auto& s
= i
->second
;
942 case TokenID::StringEquals
:
943 return orrible(std::equal_to
<std::string
>(), s
, vals
);
945 case TokenID::StringNotEquals
:
946 return orrible(ceph::not_fn(std::equal_to
<std::string
>()),
949 case TokenID::StringEqualsIgnoreCase
:
950 return orrible(ci_equal_to(), s
, vals
);
952 case TokenID::StringNotEqualsIgnoreCase
:
953 return orrible(ceph::not_fn(ci_equal_to()), s
, vals
);
955 case TokenID::StringLike
:
956 return orrible(string_like(), s
, vals
);
958 case TokenID::StringNotLike
:
959 return orrible(ceph::not_fn(string_like()), s
, vals
);
962 case TokenID::NumericEquals
:
963 return shortible(std::equal_to
<double>(), as_number
, s
, vals
);
965 case TokenID::NumericNotEquals
:
966 return shortible(ceph::not_fn(std::equal_to
<double>()),
970 case TokenID::NumericLessThan
:
971 return shortible(std::less
<double>(), as_number
, s
, vals
);
974 case TokenID::NumericLessThanEquals
:
975 return shortible(std::less_equal
<double>(), as_number
, s
, vals
);
977 case TokenID::NumericGreaterThan
:
978 return shortible(std::greater
<double>(), as_number
, s
, vals
);
980 case TokenID::NumericGreaterThanEquals
:
981 return shortible(std::greater_equal
<double>(), as_number
, s
, vals
);
984 case TokenID::DateEquals
:
985 return shortible(std::equal_to
<ceph::real_time
>(), as_date
, s
, vals
);
987 case TokenID::DateNotEquals
:
988 return shortible(ceph::not_fn(std::equal_to
<ceph::real_time
>()),
991 case TokenID::DateLessThan
:
992 return shortible(std::less
<ceph::real_time
>(), as_date
, s
, vals
);
995 case TokenID::DateLessThanEquals
:
996 return shortible(std::less_equal
<ceph::real_time
>(), as_date
, s
, vals
);
998 case TokenID::DateGreaterThan
:
999 return shortible(std::greater
<ceph::real_time
>(), as_date
, s
, vals
);
1001 case TokenID::DateGreaterThanEquals
:
1002 return shortible(std::greater_equal
<ceph::real_time
>(), as_date
, s
,
1007 return shortible(std::equal_to
<bool>(), as_bool
, s
, vals
);
1010 case TokenID::BinaryEquals
:
1011 return shortible(std::equal_to
<ceph::bufferlist
>(), as_binary
, s
,
1015 case TokenID::IpAddress
:
1016 return shortible(std::equal_to
<MaskedIP
>(), as_network
, s
, vals
);
1018 case TokenID::NotIpAddress
:
1020 auto xc
= as_network(s
);
1025 for (const string
& d
: vals
) {
1026 auto xd
= as_network(d
);
1039 // Amazon Resource Names! (Does S3 need this?)
1040 TokenID::ArnEquals
, TokenID::ArnNotEquals
, TokenID::ArnLike
,
1041 TokenID::ArnNotLike
,
1049 optional
<MaskedIP
> Condition::as_network(const string
& s
) {
1055 m
.v6
= (s
.find(':') == string::npos
) ? false : true;
1056 auto slash
= s
.find('/');
1057 if (slash
== string::npos
) {
1058 m
.prefix
= m
.v6
? 128 : 32;
1061 m
.prefix
= strtoul(s
.data() + slash
+ 1, &end
, 10);
1062 if (*end
!= 0 || (m
.v6
&& m
.prefix
> 128) ||
1063 (!m
.v6
&& m
.prefix
> 32)) {
1071 if (slash
!= string::npos
) {
1072 t
.assign(s
, 0, slash
);
1078 if (inet_pton(AF_INET6
, p
->c_str(), static_cast<void*>(&a
)) != 1) {
1082 m
.addr
|= Address(a
.s6_addr
[15]) << 0;
1083 m
.addr
|= Address(a
.s6_addr
[14]) << 8;
1084 m
.addr
|= Address(a
.s6_addr
[13]) << 16;
1085 m
.addr
|= Address(a
.s6_addr
[12]) << 24;
1086 m
.addr
|= Address(a
.s6_addr
[11]) << 32;
1087 m
.addr
|= Address(a
.s6_addr
[10]) << 40;
1088 m
.addr
|= Address(a
.s6_addr
[9]) << 48;
1089 m
.addr
|= Address(a
.s6_addr
[8]) << 56;
1090 m
.addr
|= Address(a
.s6_addr
[7]) << 64;
1091 m
.addr
|= Address(a
.s6_addr
[6]) << 72;
1092 m
.addr
|= Address(a
.s6_addr
[5]) << 80;
1093 m
.addr
|= Address(a
.s6_addr
[4]) << 88;
1094 m
.addr
|= Address(a
.s6_addr
[3]) << 96;
1095 m
.addr
|= Address(a
.s6_addr
[2]) << 104;
1096 m
.addr
|= Address(a
.s6_addr
[1]) << 112;
1097 m
.addr
|= Address(a
.s6_addr
[0]) << 120;
1100 if (inet_pton(AF_INET
, p
->c_str(), static_cast<void*>(&a
)) != 1) {
1104 m
.addr
= ntohl(a
.s_addr
);
1111 const char* condop_string(const TokenID t
) {
1113 case TokenID::StringEquals
:
1114 return "StringEquals";
1116 case TokenID::StringNotEquals
:
1117 return "StringNotEquals";
1119 case TokenID::StringEqualsIgnoreCase
:
1120 return "StringEqualsIgnoreCase";
1122 case TokenID::StringNotEqualsIgnoreCase
:
1123 return "StringNotEqualsIgnoreCase";
1125 case TokenID::StringLike
:
1126 return "StringLike";
1128 case TokenID::StringNotLike
:
1129 return "StringNotLike";
1132 case TokenID::NumericEquals
:
1133 return "NumericEquals";
1135 case TokenID::NumericNotEquals
:
1136 return "NumericNotEquals";
1138 case TokenID::NumericLessThan
:
1139 return "NumericLessThan";
1141 case TokenID::NumericLessThanEquals
:
1142 return "NumericLessThanEquals";
1144 case TokenID::NumericGreaterThan
:
1145 return "NumericGreaterThan";
1147 case TokenID::NumericGreaterThanEquals
:
1148 return "NumericGreaterThanEquals";
1150 case TokenID::DateEquals
:
1151 return "DateEquals";
1153 case TokenID::DateNotEquals
:
1154 return "DateNotEquals";
1156 case TokenID::DateLessThan
:
1157 return "DateLessThan";
1159 case TokenID::DateLessThanEquals
:
1160 return "DateLessThanEquals";
1162 case TokenID::DateGreaterThan
:
1163 return "DateGreaterThan";
1165 case TokenID::DateGreaterThanEquals
:
1166 return "DateGreaterThanEquals";
1171 case TokenID::BinaryEquals
:
1172 return "BinaryEquals";
1174 case TokenID::IpAddress
:
1175 return "case TokenID::IpAddress";
1177 case TokenID::NotIpAddress
:
1178 return "NotIpAddress";
1180 case TokenID::ArnEquals
:
1183 case TokenID::ArnNotEquals
:
1184 return "ArnNotEquals";
1186 case TokenID::ArnLike
:
1189 case TokenID::ArnNotLike
:
1190 return "ArnNotLike";
1196 return "InvalidConditionOperator";
1200 template<typename Iterator
>
1201 ostream
& print_array(ostream
& m
, Iterator begin
, Iterator end
) {
1205 auto beforelast
= end
- 1;
1207 for (auto i
= begin
; i
!= end
; ++i
) {
1209 if (i
!= beforelast
) {
1221 ostream
& operator <<(ostream
& m
, const Condition
& c
) {
1222 m
<< "{ " << condop_string(c
.op
);
1226 m
<< ": { " << c
.key
;
1227 print_array(m
, c
.vals
.cbegin(), c
.vals
.cend());
1231 string
to_string(const Condition
& c
) {
1237 Effect
Statement::eval(const Environment
& e
,
1238 optional
<const rgw::auth::Identity
&> ida
,
1239 uint64_t act
, const ARN
& res
) const {
1240 if (ida
&& (!ida
->is_identity(princ
) || ida
->is_identity(noprinc
))) {
1241 return Effect::Pass
;
1245 if (!std::any_of(resource
.begin(), resource
.end(),
1246 [&res
](const ARN
& pattern
) {
1247 return pattern
.match(res
);
1249 (std::any_of(notresource
.begin(), notresource
.end(),
1250 [&res
](const ARN
& pattern
) {
1251 return pattern
.match(res
);
1253 return Effect::Pass
;
1256 if (!(action
& act
) || (notaction
& act
)) {
1257 return Effect::Pass
;
1260 if (std::all_of(conditions
.begin(),
1262 [&e
](const Condition
& c
) { return c
.eval(e
);})) {
1266 return Effect::Pass
;
1270 const char* action_bit_string(uint64_t action
) {
1273 return "s3:GetObject";
1275 case s3GetObjectVersion
:
1276 return "s3:GetObjectVersion";
1279 return "s3:PutObject";
1281 case s3GetObjectAcl
:
1282 return "s3:GetObjectAcl";
1284 case s3GetObjectVersionAcl
:
1285 return "s3:GetObjectVersionAcl";
1287 case s3PutObjectAcl
:
1288 return "s3:PutObjectAcl";
1290 case s3PutObjectVersionAcl
:
1291 return "s3:PutObjectVersionAcl";
1293 case s3DeleteObject
:
1294 return "s3:DeleteObject";
1296 case s3DeleteObjectVersion
:
1297 return "s3:DeleteObjectVersion";
1299 case s3ListMultipartUploadParts
:
1300 return "s3:ListMultipartUploadParts";
1302 case s3AbortMultipartUpload
:
1303 return "s3:AbortMultipartUpload";
1305 case s3GetObjectTorrent
:
1306 return "s3:GetObjectTorrent";
1308 case s3GetObjectVersionTorrent
:
1309 return "s3:GetObjectVersionTorrent";
1311 case s3RestoreObject
:
1312 return "s3:RestoreObject";
1314 case s3CreateBucket
:
1315 return "s3:CreateBucket";
1317 case s3DeleteBucket
:
1318 return "s3:DeleteBucket";
1321 return "s3:ListBucket";
1323 case s3ListBucketVersions
:
1324 return "s3:ListBucketVersions";
1325 case s3ListAllMyBuckets
:
1326 return "s3:ListAllMyBuckets";
1328 case s3ListBucketMultipartUploads
:
1329 return "s3:ListBucketMultipartUploads";
1331 case s3GetAccelerateConfiguration
:
1332 return "s3:GetAccelerateConfiguration";
1334 case s3PutAccelerateConfiguration
:
1335 return "s3:PutAccelerateConfiguration";
1337 case s3GetBucketAcl
:
1338 return "s3:GetBucketAcl";
1340 case s3PutBucketAcl
:
1341 return "s3:PutBucketAcl";
1343 case s3GetBucketCORS
:
1344 return "s3:GetBucketCORS";
1346 case s3PutBucketCORS
:
1347 return "s3:PutBucketCORS";
1349 case s3GetBucketVersioning
:
1350 return "s3:GetBucketVersioning";
1352 case s3PutBucketVersioning
:
1353 return "s3:PutBucketVersioning";
1355 case s3GetBucketRequestPayment
:
1356 return "s3:GetBucketRequestPayment";
1358 case s3PutBucketRequestPayment
:
1359 return "s3:PutBucketRequestPayment";
1361 case s3GetBucketLocation
:
1362 return "s3:GetBucketLocation";
1364 case s3GetBucketPolicy
:
1365 return "s3:GetBucketPolicy";
1367 case s3DeleteBucketPolicy
:
1368 return "s3:DeleteBucketPolicy";
1370 case s3PutBucketPolicy
:
1371 return "s3:PutBucketPolicy";
1373 case s3GetBucketNotification
:
1374 return "s3:GetBucketNotification";
1376 case s3PutBucketNotification
:
1377 return "s3:PutBucketNotification";
1379 case s3GetBucketLogging
:
1380 return "s3:GetBucketLogging";
1382 case s3PutBucketLogging
:
1383 return "s3:PutBucketLogging";
1385 case s3GetBucketTagging
:
1386 return "s3:GetBucketTagging";
1388 case s3PutBucketTagging
:
1389 return "s3:PutBucketTagging";
1391 case s3GetBucketWebsite
:
1392 return "s3:GetBucketWebsite";
1394 case s3PutBucketWebsite
:
1395 return "s3:PutBucketWebsite";
1397 case s3DeleteBucketWebsite
:
1398 return "s3:DeleteBucketWebsite";
1400 case s3GetLifecycleConfiguration
:
1401 return "s3:GetLifecycleConfiguration";
1403 case s3PutLifecycleConfiguration
:
1404 return "s3:PutLifecycleConfiguration";
1406 case s3PutReplicationConfiguration
:
1407 return "s3:PutReplicationConfiguration";
1409 case s3GetReplicationConfiguration
:
1410 return "s3:GetReplicationConfiguration";
1412 case s3DeleteReplicationConfiguration
:
1413 return "s3:DeleteReplicationConfiguration";
1415 case s3PutObjectTagging
:
1416 return "s3:PutObjectTagging";
1418 case s3PutObjectVersionTagging
:
1419 return "s3:PutObjectVersionTagging";
1421 case s3GetObjectTagging
:
1422 return "s3:GetObjectTagging";
1424 case s3GetObjectVersionTagging
:
1425 return "s3:GetObjectVersionTagging";
1427 case s3DeleteObjectTagging
:
1428 return "s3:DeleteObjectTagging";
1430 case s3DeleteObjectVersionTagging
:
1431 return "s3:DeleteObjectVersionTagging";
1436 ostream
& print_actions(ostream
& m
, const uint64_t a
) {
1439 for (auto i
= 0U; i
< s3Count
; ++i
) {
1446 m
<< action_bit_string(1 << i
);
1458 ostream
& operator <<(ostream
& m
, const Statement
& s
) {
1461 m
<< "Sid: " << *s
.sid
<< ", ";
1463 if (!s
.princ
.empty()) {
1465 print_array(m
, s
.princ
.cbegin(), s
.princ
.cend());
1468 if (!s
.noprinc
.empty()) {
1469 m
<< "NotPrincipal: ";
1470 print_array(m
, s
.noprinc
.cbegin(), s
.noprinc
.cend());
1475 (s
.effect
== Effect::Allow
?
1476 (const char*) "Allow" :
1477 (const char*) "Deny");
1479 if (s
.action
|| s
.notaction
|| !s
.resource
.empty() ||
1480 !s
.notresource
.empty() || !s
.conditions
.empty()) {
1486 print_actions(m
, s
.action
);
1488 if (s
.notaction
|| !s
.resource
.empty() ||
1489 !s
.notresource
.empty() || !s
.conditions
.empty()) {
1496 print_actions(m
, s
.notaction
);
1498 if (!s
.resource
.empty() || !s
.notresource
.empty() ||
1499 !s
.conditions
.empty()) {
1504 if (!s
.resource
.empty()) {
1506 print_array(m
, s
.resource
.cbegin(), s
.resource
.cend());
1508 if (!s
.notresource
.empty() || !s
.conditions
.empty()) {
1513 if (!s
.notresource
.empty()) {
1514 m
<< "NotResource: ";
1515 print_array(m
, s
.notresource
.cbegin(), s
.notresource
.cend());
1517 if (!s
.conditions
.empty()) {
1522 if (!s
.conditions
.empty()) {
1524 print_array(m
, s
.conditions
.cbegin(), s
.conditions
.cend());
1530 string
to_string(const Statement
& s
) {
1536 Policy::Policy(CephContext
* cct
, const string
& tenant
,
1537 const bufferlist
& _text
)
1538 : text(_text
.to_str()) {
1539 StringStream
ss(text
.data());
1540 PolicyParser
pp(cct
, tenant
, *this);
1541 auto pr
= Reader
{}.Parse
<kParseNumbersAsStringsFlag
|
1542 kParseCommentsFlag
>(ss
, pp
);
1544 throw PolicyParseException(std::move(pr
));
1548 Effect
Policy::eval(const Environment
& e
,
1549 optional
<const rgw::auth::Identity
&> ida
,
1550 std::uint64_t action
, const ARN
& resource
) const {
1551 auto allowed
= false;
1552 for (auto& s
: statements
) {
1553 auto g
= s
.eval(e
, ida
, action
, resource
);
1554 if (g
== Effect::Deny
) {
1556 } else if (g
== Effect::Allow
) {
1560 return allowed
? Effect::Allow
: Effect::Pass
;
1563 ostream
& operator <<(ostream
& m
, const Policy
& p
) {
1565 << (p
.version
== Version::v2008_10_17
? "2008-10-17" : "2012-10-17");
1567 if (p
.id
|| !p
.statements
.empty()) {
1572 m
<< "Id: " << *p
.id
;
1573 if (!p
.statements
.empty()) {
1578 if (!p
.statements
.empty()) {
1579 m
<< "Statements: ";
1580 print_array(m
, p
.statements
.cbegin(), p
.statements
.cend());
1586 string
to_string(const Policy
& p
) {