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"
15 #include <arpa/inet.h>
16 #include "rgw_iam_policy.h"
19 constexpr int dout_subsys
= ceph_subsys_rgw
;
29 using std::stringstream
;
33 using std::unordered_map
;
35 using boost::container::flat_set
;
37 using boost::optional
;
39 using boost::regex_constants::ECMAScript
;
40 using boost::regex_constants::optimize
;
41 using boost::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"
65 optional
<Partition
> to_partition(const smatch::value_type
& p
,
68 return Partition::aws
;
69 } else if (p
== "aws-cn") {
70 return Partition::aws_cn
;
71 } else if (p
== "aws-us-gov") {
72 return Partition::aws_us_gov
;
73 } else if (p
== "*" && wildcards
) {
74 return Partition::wildcard
;
82 optional
<Service
> to_service(const smatch::value_type
& s
,
84 static const unordered_map
<string
, Service
> services
= {
85 { "acm", Service::acm
},
86 { "apigateway", Service::apigateway
},
87 { "appstream", Service::appstream
},
88 { "artifact", Service::artifact
},
89 { "autoscaling", Service::autoscaling
},
90 { "aws-marketplace", Service::aws_marketplace
},
91 { "aws-marketplace-management",
92 Service::aws_marketplace_management
},
93 { "aws-portal", Service::aws_portal
},
94 { "cloudformation", Service::cloudformation
},
95 { "cloudfront", Service::cloudfront
},
96 { "cloudhsm", Service::cloudhsm
},
97 { "cloudsearch", Service::cloudsearch
},
98 { "cloudtrail", Service::cloudtrail
},
99 { "cloudwatch", Service::cloudwatch
},
100 { "codebuild", Service::codebuild
},
101 { "codecommit", Service::codecommit
},
102 { "codedeploy", Service::codedeploy
},
103 { "codepipeline", Service::codepipeline
},
104 { "cognito-identity", Service::cognito_identity
},
105 { "cognito-idp", Service::cognito_idp
},
106 { "cognito-sync", Service::cognito_sync
},
107 { "config", Service::config
},
108 { "datapipeline", Service::datapipeline
},
109 { "devicefarm", Service::devicefarm
},
110 { "directconnect", Service::directconnect
},
111 { "dms", Service::dms
},
112 { "ds", Service::ds
},
113 { "dynamodb", Service::dynamodb
},
114 { "ec2", Service::ec2
},
115 { "ecr", Service::ecr
},
116 { "ecs", Service::ecs
},
117 { "elasticache", Service::elasticache
},
118 { "elasticbeanstalk", Service::elasticbeanstalk
},
119 { "elasticfilesystem", Service::elasticfilesystem
},
120 { "elasticloadbalancing", Service::elasticloadbalancing
},
121 { "elasticmapreduce", Service::elasticmapreduce
},
122 { "elastictranscoder", Service::elastictranscoder
},
123 { "es", Service::es
},
124 { "events", Service::events
},
125 { "firehose", Service::firehose
},
126 { "gamelift", Service::gamelift
},
127 { "glacier", Service::glacier
},
128 { "health", Service::health
},
129 { "iam", Service::iam
},
130 { "importexport", Service::importexport
},
131 { "inspector", Service::inspector
},
132 { "iot", Service::iot
},
133 { "kinesis", Service::kinesis
},
134 { "kinesisanalytics", Service::kinesisanalytics
},
135 { "kms", Service::kms
},
136 { "lambda", Service::lambda
},
137 { "lightsail", Service::lightsail
},
138 { "logs", Service::logs
},
139 { "machinelearning", Service::machinelearning
},
140 { "mobileanalytics", Service::mobileanalytics
},
141 { "mobilehub", Service::mobilehub
},
142 { "opsworks", Service::opsworks
},
143 { "opsworks-cm", Service::opsworks_cm
},
144 { "polly", Service::polly
},
145 { "rds", Service::rds
},
146 { "redshift", Service::redshift
},
147 { "route53", Service::route53
},
148 { "route53domains", Service::route53domains
},
149 { "s3", Service::s3
},
150 { "sdb", Service::sdb
},
151 { "servicecatalog", Service::servicecatalog
},
152 { "ses", Service::ses
},
153 { "sns", Service::sns
},
154 { "sqs", Service::sqs
},
155 { "ssm", Service::ssm
},
156 { "states", Service::states
},
157 { "storagegateway", Service::storagegateway
},
158 { "sts", Service::sts
},
159 { "support", Service::support
},
160 { "swf", Service::swf
},
161 { "trustedadvisor", Service::trustedadvisor
},
162 { "waf", Service::waf
},
163 { "workmail", Service::workmail
},
164 { "workspaces", Service::workspaces
}};
166 if (wildcards
&& s
== "*") {
167 return Service::wildcard
;
170 auto i
= services
.find(s
);
171 if (i
== services
.end()) {
179 ARN::ARN(const rgw_obj
& o
)
180 : partition(Partition::aws
),
181 service(Service::s3
),
183 account(o
.bucket
.tenant
),
184 resource(o
.bucket
.name
)
186 resource
.push_back('/');
187 resource
.append(o
.key
.name
);
190 ARN::ARN(const rgw_bucket
& b
)
191 : partition(Partition::aws
),
192 service(Service::s3
),
197 ARN::ARN(const rgw_bucket
& b
, const string
& o
)
198 : partition(Partition::aws
),
199 service(Service::s3
),
203 resource
.push_back('/');
207 optional
<ARN
> ARN::parse(const string
& s
, bool wildcards
) {
208 static const char str_wild
[] = "arn:([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)";
209 static const regex
rx_wild(str_wild
,
210 sizeof(str_wild
) - 1,
211 ECMAScript
| optimize
);
212 static const char str_no_wild
[]
213 = "arn:([^:*]*):([^:*]*):([^:*]*):([^:*]*):([^:*]*)";
214 static const regex
rx_no_wild(str_no_wild
,
215 sizeof(str_no_wild
) - 1,
216 ECMAScript
| optimize
);
220 if ((s
== "*") && wildcards
) {
221 return ARN(Partition::wildcard
, Service::wildcard
, "*", "*", "*");
222 } else if (regex_match(s
, match
, wildcards
? rx_wild
: rx_no_wild
)) {
223 ceph_assert(match
.size() == 6);
227 auto p
= to_partition(match
[1], wildcards
);
234 auto s
= to_service(match
[2], wildcards
);
242 a
.account
= match
[4];
243 a
.resource
= match
[5];
250 string
ARN::to_string() const {
253 if (partition
== Partition::aws
) {
255 } else if (partition
== Partition::aws_cn
) {
257 } else if (partition
== Partition::aws_us_gov
) {
258 s
.append("aws-us-gov:");
263 static const unordered_map
<Service
, string
> services
= {
264 { Service::acm
, "acm" },
265 { Service::apigateway
, "apigateway" },
266 { Service::appstream
, "appstream" },
267 { Service::artifact
, "artifact" },
268 { Service::autoscaling
, "autoscaling" },
269 { Service::aws_marketplace
, "aws-marketplace" },
270 { Service::aws_marketplace_management
, "aws-marketplace-management" },
271 { Service::aws_portal
, "aws-portal" },
272 { Service::cloudformation
, "cloudformation" },
273 { Service::cloudfront
, "cloudfront" },
274 { Service::cloudhsm
, "cloudhsm" },
275 { Service::cloudsearch
, "cloudsearch" },
276 { Service::cloudtrail
, "cloudtrail" },
277 { Service::cloudwatch
, "cloudwatch" },
278 { Service::codebuild
, "codebuild" },
279 { Service::codecommit
, "codecommit" },
280 { Service::codedeploy
, "codedeploy" },
281 { Service::codepipeline
, "codepipeline" },
282 { Service::cognito_identity
, "cognito-identity" },
283 { Service::cognito_idp
, "cognito-idp" },
284 { Service::cognito_sync
, "cognito-sync" },
285 { Service::config
, "config" },
286 { Service::datapipeline
, "datapipeline" },
287 { Service::devicefarm
, "devicefarm" },
288 { Service::directconnect
, "directconnect" },
289 { Service::dms
, "dms" },
290 { Service::ds
, "ds" },
291 { Service::dynamodb
, "dynamodb" },
292 { Service::ec2
, "ec2" },
293 { Service::ecr
, "ecr" },
294 { Service::ecs
, "ecs" },
295 { Service::elasticache
, "elasticache" },
296 { Service::elasticbeanstalk
, "elasticbeanstalk" },
297 { Service::elasticfilesystem
, "elasticfilesystem" },
298 { Service::elasticloadbalancing
, "elasticloadbalancing" },
299 { Service::elasticmapreduce
, "elasticmapreduce" },
300 { Service::elastictranscoder
, "elastictranscoder" },
301 { Service::es
, "es" },
302 { Service::events
, "events" },
303 { Service::firehose
, "firehose" },
304 { Service::gamelift
, "gamelift" },
305 { Service::glacier
, "glacier" },
306 { Service::health
, "health" },
307 { Service::iam
, "iam" },
308 { Service::importexport
, "importexport" },
309 { Service::inspector
, "inspector" },
310 { Service::iot
, "iot" },
311 { Service::kinesis
, "kinesis" },
312 { Service::kinesisanalytics
, "kinesisanalytics" },
313 { Service::kms
, "kms" },
314 { Service::lambda
, "lambda" },
315 { Service::lightsail
, "lightsail" },
316 { Service::logs
, "logs" },
317 { Service::machinelearning
, "machinelearning" },
318 { Service::mobileanalytics
, "mobileanalytics" },
319 { Service::mobilehub
, "mobilehub" },
320 { Service::opsworks
, "opsworks" },
321 { Service::opsworks_cm
, "opsworks-cm" },
322 { Service::polly
, "polly" },
323 { Service::rds
, "rds" },
324 { Service::redshift
, "redshift" },
325 { Service::route53
, "route53" },
326 { Service::route53domains
, "route53domains" },
327 { Service::s3
, "s3" },
328 { Service::sdb
, "sdb" },
329 { Service::servicecatalog
, "servicecatalog" },
330 { Service::ses
, "ses" },
331 { Service::sns
, "sns" },
332 { Service::sqs
, "sqs" },
333 { Service::ssm
, "ssm" },
334 { Service::states
, "states" },
335 { Service::storagegateway
, "storagegateway" },
336 { Service::sts
, "sts" },
337 { Service::support
, "support" },
338 { Service::swf
, "swf" },
339 { Service::trustedadvisor
, "trustedadvisor" },
340 { Service::waf
, "waf" },
341 { Service::workmail
, "workmail" },
342 { Service::workspaces
, "workspaces" }};
344 auto i
= services
.find(service
);
345 if (i
!= services
.end()) {
363 bool operator ==(const ARN
& l
, const ARN
& r
) {
364 return ((l
.partition
== r
.partition
) &&
365 (l
.service
== r
.service
) &&
366 (l
.region
== r
.region
) &&
367 (l
.account
== r
.account
) &&
368 (l
.resource
== r
.resource
));
370 bool operator <(const ARN
& l
, const ARN
& r
) {
371 return ((l
.partition
< r
.partition
) ||
372 (l
.service
< r
.service
) ||
373 (l
.region
< r
.region
) ||
374 (l
.account
< r
.account
) ||
375 (l
.resource
< r
.resource
));
378 // The candidate is not allowed to have wildcards. The only way to
379 // do that sanely would be to use unification rather than matching.
380 bool ARN::match(const ARN
& candidate
) const {
381 if ((candidate
.partition
== Partition::wildcard
) ||
382 (partition
!= candidate
.partition
&& partition
383 != Partition::wildcard
)) {
387 if ((candidate
.service
== Service::wildcard
) ||
388 (service
!= candidate
.service
&& service
!= Service::wildcard
)) {
392 if (!::match(region
, candidate
.region
, MATCH_POLICY_ARN
)) {
396 if (!::match(account
, candidate
.account
, MATCH_POLICY_ARN
)) {
400 if (!::match(resource
, candidate
.resource
, MATCH_POLICY_ARN
)) {
407 static const actpair actpairs
[] =
408 {{ "s3:AbortMultipartUpload", s3AbortMultipartUpload
},
409 { "s3:CreateBucket", s3CreateBucket
},
410 { "s3:DeleteBucketPolicy", s3DeleteBucketPolicy
},
411 { "s3:DeleteBucket", s3DeleteBucket
},
412 { "s3:DeleteBucketWebsite", s3DeleteBucketWebsite
},
413 { "s3:DeleteObject", s3DeleteObject
},
414 { "s3:DeleteObjectVersion", s3DeleteObjectVersion
},
415 { "s3:DeleteObjectTagging", s3DeleteObjectTagging
},
416 { "s3:DeleteObjectVersionTagging", s3DeleteObjectVersionTagging
},
417 { "s3:DeleteReplicationConfiguration", s3DeleteReplicationConfiguration
},
418 { "s3:GetAccelerateConfiguration", s3GetAccelerateConfiguration
},
419 { "s3:GetBucketAcl", s3GetBucketAcl
},
420 { "s3:GetBucketCORS", s3GetBucketCORS
},
421 { "s3:GetBucketLocation", s3GetBucketLocation
},
422 { "s3:GetBucketLogging", s3GetBucketLogging
},
423 { "s3:GetBucketNotification", s3GetBucketNotification
},
424 { "s3:GetBucketPolicy", s3GetBucketPolicy
},
425 { "s3:GetBucketRequestPayment", s3GetBucketRequestPayment
},
426 { "s3:GetBucketTagging", s3GetBucketTagging
},
427 { "s3:GetBucketVersioning", s3GetBucketVersioning
},
428 { "s3:GetBucketWebsite", s3GetBucketWebsite
},
429 { "s3:GetLifecycleConfiguration", s3GetLifecycleConfiguration
},
430 { "s3:GetObjectAcl", s3GetObjectAcl
},
431 { "s3:GetObject", s3GetObject
},
432 { "s3:GetObjectTorrent", s3GetObjectTorrent
},
433 { "s3:GetObjectVersionAcl", s3GetObjectVersionAcl
},
434 { "s3:GetObjectVersion", s3GetObjectVersion
},
435 { "s3:GetObjectVersionTorrent", s3GetObjectVersionTorrent
},
436 { "s3:GetObjectTagging", s3GetObjectTagging
},
437 { "s3:GetObjectVersionTagging", s3GetObjectVersionTagging
},
438 { "s3:GetReplicationConfiguration", s3GetReplicationConfiguration
},
439 { "s3:ListAllMyBuckets", s3ListAllMyBuckets
},
440 { "s3:ListBucketMultiPartUploads", s3ListBucketMultiPartUploads
},
441 { "s3:ListBucket", s3ListBucket
},
442 { "s3:ListBucketVersions", s3ListBucketVersions
},
443 { "s3:ListMultipartUploadParts", s3ListMultipartUploadParts
},
444 { "s3:PutAccelerateConfiguration", s3PutAccelerateConfiguration
},
445 { "s3:PutBucketAcl", s3PutBucketAcl
},
446 { "s3:PutBucketCORS", s3PutBucketCORS
},
447 { "s3:PutBucketLogging", s3PutBucketLogging
},
448 { "s3:PutBucketNotification", s3PutBucketNotification
},
449 { "s3:PutBucketPolicy", s3PutBucketPolicy
},
450 { "s3:PutBucketRequestPayment", s3PutBucketRequestPayment
},
451 { "s3:PutBucketTagging", s3PutBucketTagging
},
452 { "s3:PutBucketVersioning", s3PutBucketVersioning
},
453 { "s3:PutBucketWebsite", s3PutBucketWebsite
},
454 { "s3:PutLifecycleConfiguration", s3PutLifecycleConfiguration
},
455 { "s3:PutObjectAcl", s3PutObjectAcl
},
456 { "s3:PutObject", s3PutObject
},
457 { "s3:PutObjectVersionAcl", s3PutObjectVersionAcl
},
458 { "s3:PutObjectTagging", s3PutObjectTagging
},
459 { "s3:PutObjectVersionTagging", s3PutObjectVersionTagging
},
460 { "s3:PutReplicationConfiguration", s3PutReplicationConfiguration
},
461 { "s3:RestoreObject", s3RestoreObject
}};
465 const Keyword top
[1]{"<Top>", TokenKind::pseudo
, TokenID::Top
, 0, false,
467 const Keyword cond_key
[1]{"<Condition Key>", TokenKind::cond_key
,
468 TokenID::CondKey
, 0, true, false};
474 bool arraying
= false;
475 bool objecting
= false;
476 bool cond_ifexists
= false;
480 ParseState(PolicyParser
* pp
, const Keyword
* w
)
488 if (w
->arrayable
&& !arraying
) {
497 bool key(const char* s
, size_t l
);
498 bool do_string(CephContext
* cct
, const char* s
, size_t l
);
499 bool number(const char* str
, size_t l
);
502 // If this confuses you, look up the Curiously Recurring Template Pattern
503 struct PolicyParser
: public BaseReaderHandler
<UTF8
<>, PolicyParser
> {
505 std::vector
<ParseState
> s
;
507 const string
& tenant
;
513 uint32_t dex(TokenID in
) const {
515 case TokenID::Version
:
519 case TokenID::Statement
:
523 case TokenID::Effect
:
525 case TokenID::Principal
:
527 case TokenID::NotPrincipal
:
529 case TokenID::Action
:
531 case TokenID::NotAction
:
533 case TokenID::Resource
:
535 case TokenID::NotResource
:
537 case TokenID::Condition
:
541 case TokenID::Federated
:
543 case TokenID::Service
:
545 case TokenID::CanonicalUser
:
551 bool test(TokenID in
) {
552 return seen
& dex(in
);
554 void set(TokenID in
) {
556 if (in
== TokenID::Sid
|| in
== TokenID::Effect
|| in
== TokenID::Principal
|| in
== TokenID::NotPrincipal
||
557 in
== TokenID::Action
|| in
== TokenID::NotAction
|| in
== TokenID::Resource
|| in
== TokenID::NotResource
||
558 in
== TokenID::Condition
|| in
== TokenID::AWS
|| in
== TokenID::Federated
|| in
== TokenID::Service
||
559 in
== TokenID::CanonicalUser
) {
563 void set(std::initializer_list
<TokenID
> l
) {
566 if (in
== TokenID::Sid
|| in
== TokenID::Effect
|| in
== TokenID::Principal
|| in
== TokenID::NotPrincipal
||
567 in
== TokenID::Action
|| in
== TokenID::NotAction
|| in
== TokenID::Resource
|| in
== TokenID::NotResource
||
568 in
== TokenID::Condition
|| in
== TokenID::AWS
|| in
== TokenID::Federated
|| in
== TokenID::Service
||
569 in
== TokenID::CanonicalUser
) {
574 void reset(TokenID in
) {
576 if (in
== TokenID::Sid
|| in
== TokenID::Effect
|| in
== TokenID::Principal
|| in
== TokenID::NotPrincipal
||
577 in
== TokenID::Action
|| in
== TokenID::NotAction
|| in
== TokenID::Resource
|| in
== TokenID::NotResource
||
578 in
== TokenID::Condition
|| in
== TokenID::AWS
|| in
== TokenID::Federated
|| in
== TokenID::Service
||
579 in
== TokenID::CanonicalUser
) {
583 void reset(std::initializer_list
<TokenID
> l
) {
586 if (in
== TokenID::Sid
|| in
== TokenID::Effect
|| in
== TokenID::Principal
|| in
== TokenID::NotPrincipal
||
587 in
== TokenID::Action
|| in
== TokenID::NotAction
|| in
== TokenID::Resource
|| in
== TokenID::NotResource
||
588 in
== TokenID::Condition
|| in
== TokenID::AWS
|| in
== TokenID::Federated
|| in
== TokenID::Service
||
589 in
== TokenID::CanonicalUser
) {
594 void reset(std::set
<TokenID
> v
) {
601 PolicyParser(CephContext
* cct
, const string
& tenant
, Policy
& policy
)
602 : cct(cct
), tenant(tenant
), policy(policy
) {}
603 PolicyParser(const PolicyParser
& policy
) = delete;
607 s
.push_back({this, top
});
608 s
.back().objecting
= true;
612 return s
.back().obj_start();
614 bool EndObject(SizeType memberCount
) {
618 return s
.back().obj_end();
620 bool Key(const char* str
, SizeType length
, bool copy
) {
624 return s
.back().key(str
, length
);
627 bool String(const char* str
, SizeType length
, bool copy
) {
631 return s
.back().do_string(cct
, str
, length
);
633 bool RawNumber(const char* str
, SizeType length
, bool copy
) {
638 return s
.back().number(str
, length
);
645 return s
.back().array_start();
647 bool EndArray(SizeType
) {
652 return s
.back().array_end();
661 // I really despise this misfeature of C++.
663 bool ParseState::obj_end() {
676 bool ParseState::key(const char* s
, size_t l
) {
678 bool ifexists
= false;
679 if (w
->id
== TokenID::Condition
&& w
->kind
== TokenKind::statement
) {
680 static constexpr char IfExists
[] = "IfExists";
681 if (boost::algorithm::ends_with(boost::string_view
{s
, l
}, IfExists
)) {
683 token_len
-= sizeof(IfExists
)-1;
686 auto k
= pp
->tokens
.lookup(s
, token_len
);
689 if (w
->kind
== TokenKind::cond_op
) {
690 auto& t
= pp
->policy
.statements
.back();
691 pp
->s
.emplace_back(pp
, cond_key
);
692 t
.conditions
.emplace_back(w
->id
, s
, l
, cond_ifexists
);
699 // If the token we're going with belongs within the condition at the
700 // top of the stack and we haven't already encountered it, push it
703 if ((((w
->id
== TokenID::Top
) && (k
->kind
== TokenKind::top
)) ||
705 ((w
->id
== TokenID::Statement
) && (k
->kind
== TokenKind::statement
)) ||
708 ((w
->id
== TokenID::Principal
|| w
->id
== TokenID::NotPrincipal
) &&
709 (k
->kind
== TokenKind::princ_type
))) &&
711 // Check that it hasn't been encountered. Note that this
712 // conjoins with the run of disjunctions above.
715 pp
->s
.emplace_back(pp
, k
);
717 } else if ((w
->id
== TokenID::Condition
) &&
718 (k
->kind
== TokenKind::cond_op
)) {
719 pp
->s
.emplace_back(pp
, k
);
720 pp
->s
.back().cond_ifexists
= ifexists
;
726 // I should just rewrite a few helper functions to use iterators,
727 // which will make all of this ever so much nicer.
728 static optional
<Principal
> parse_principal(CephContext
* cct
, TokenID t
,
731 if ((t
== TokenID::AWS
) && (s
== "*")) {
732 return Principal::wildcard();
734 // Do nothing for now.
735 } else if (t
== TokenID::CanonicalUser
) {
738 } else if (t
== TokenID::AWS
) {
739 auto a
= ARN::parse(s
);
741 if (std::none_of(s
.begin(), s
.end(),
743 return (c
== ':') || (c
== '/');
745 // Since tenants are simply prefixes, there's no really good
746 // way to see if one exists or not. So we return the thing and
747 // let them try to match against it.
748 return Principal::tenant(std::move(s
));
752 if (a
->resource
== "root") {
753 return Principal::tenant(std::move(a
->account
));
756 static const char rx_str
[] = "([^/]*)/(.*)";
757 static const regex
rx(rx_str
, sizeof(rx_str
) - 1,
758 ECMAScript
| optimize
);
760 if (regex_match(a
->resource
, match
, rx
)) {
761 ceph_assert(match
.size() == 3);
763 if (match
[1] == "user") {
764 return Principal::user(std::move(a
->account
),
768 if (match
[1] == "role") {
769 return Principal::role(std::move(a
->account
),
775 ldout(cct
, 0) << "Supplied principal is discarded: " << s
<< dendl
;
779 bool ParseState::do_string(CephContext
* cct
, const char* s
, size_t l
) {
780 auto k
= pp
->tokens
.lookup(s
, l
);
781 Policy
& p
= pp
->policy
;
782 Statement
* t
= p
.statements
.empty() ? nullptr : &(p
.statements
.back());
785 if ((w
->id
== TokenID::Version
) && k
&&
786 k
->kind
== TokenKind::version_key
) {
787 p
.version
= static_cast<Version
>(k
->specific
);
788 } else if (w
->id
== TokenID::Id
) {
793 } else if (w
->id
== TokenID::Sid
) {
794 t
->sid
.emplace(s
, l
);
795 } else if ((w
->id
== TokenID::Effect
) &&
796 k
->kind
== TokenKind::effect_key
) {
797 t
->effect
= static_cast<Effect
>(k
->specific
);
798 } else if (w
->id
== TokenID::Principal
&& s
&& *s
== '*') {
799 t
->princ
.emplace(Principal::wildcard());
800 } else if (w
->id
== TokenID::NotPrincipal
&& s
&& *s
== '*') {
801 t
->noprinc
.emplace(Principal::wildcard());
802 } else if ((w
->id
== TokenID::Action
) ||
803 (w
->id
== TokenID::NotAction
)) {
804 for (auto& p
: actpairs
) {
805 if (match({s
, l
}, p
.name
, MATCH_POLICY_ACTION
)) {
806 (w
->id
== TokenID::Action
? t
->action
: t
->notaction
) |= p
.bit
;
809 } else if (w
->id
== TokenID::Resource
|| w
->id
== TokenID::NotResource
) {
810 auto a
= ARN::parse({s
, l
}, true);
811 // You can't specify resources for someone ELSE'S account.
812 if (a
&& (a
->account
.empty() || a
->account
== pp
->tenant
||
813 a
->account
== "*")) {
814 if (a
->account
.empty() || a
->account
== "*")
815 a
->account
= pp
->tenant
;
816 (w
->id
== TokenID::Resource
? t
->resource
: t
->notresource
)
817 .emplace(std::move(*a
));
820 ldout(cct
, 0) << "Supplied resource is discarded: " << string(s
, l
)
822 } else if (w
->kind
== TokenKind::cond_key
) {
823 auto& t
= pp
->policy
.statements
.back();
824 t
.conditions
.back().vals
.emplace_back(s
, l
);
828 } else if (w
->kind
== TokenKind::princ_type
) {
829 ceph_assert(pp
->s
.size() > 1);
830 auto& pri
= pp
->s
[pp
->s
.size() - 2].w
->id
== TokenID::Principal
?
831 t
->princ
: t
->noprinc
;
833 auto o
= parse_principal(pp
->cct
, w
->id
, string(s
, l
));
835 pri
.emplace(std::move(*o
));
850 bool ParseState::number(const char* s
, size_t l
) {
852 if (w
->kind
== TokenKind::cond_key
) {
853 auto& t
= pp
->policy
.statements
.back();
854 t
.conditions
.back().vals
.emplace_back(s
, l
);
869 void ParseState::reset() {
873 bool ParseState::obj_start() {
874 if (w
->objectable
&& !objecting
) {
876 if (w
->id
== TokenID::Statement
) {
877 pp
->policy
.statements
.push_back({});
887 bool ParseState::array_end() {
888 if (arraying
&& !objecting
) {
896 ostream
& operator <<(ostream
& m
, const MaskedIP
& ip
) {
897 // I have a theory about why std::bitset is the way it is.
899 for (int i
= 15; i
>= 0; --i
) {
901 for (int j
= 7; j
>= 0; --j
) {
902 b
|= (ip
.addr
[(i
* 8) + j
] << j
);
910 // It involves Satan.
911 for (int i
= 3; i
>= 0; --i
) {
913 for (int j
= 7; j
>= 0; --j
) {
914 b
|= (ip
.addr
[(i
* 8) + j
] << j
);
922 m
<< "/" << ip
.prefix
;
923 // It would explain a lot
927 string
to_string(const MaskedIP
& m
) {
933 bool Condition::eval(const Environment
& env
) const {
934 auto i
= env
.find(key
);
935 if (op
== TokenID::Null
) {
936 return i
== env
.end() ? true : false;
939 if (i
== env
.end()) {
942 const auto& s
= i
->second
;
946 case TokenID::StringEquals
:
947 return orrible(std::equal_to
<std::string
>(), s
, vals
);
949 case TokenID::StringNotEquals
:
950 return orrible(std::not2(std::equal_to
<std::string
>()),
953 case TokenID::StringEqualsIgnoreCase
:
954 return orrible(ci_equal_to(), s
, vals
);
956 case TokenID::StringNotEqualsIgnoreCase
:
957 return orrible(std::not2(ci_equal_to()), s
, vals
);
959 // Implement actual StringLike with wildcarding later
960 case TokenID::StringLike
:
961 return orrible(std::equal_to
<std::string
>(), s
, vals
);
962 case TokenID::StringNotLike
:
963 return orrible(std::not2(std::equal_to
<std::string
>()),
967 case TokenID::NumericEquals
:
968 return shortible(std::equal_to
<double>(), as_number
, s
, vals
);
970 case TokenID::NumericNotEquals
:
971 return shortible(std::not2(std::equal_to
<double>()),
975 case TokenID::NumericLessThan
:
976 return shortible(std::less
<double>(), as_number
, s
, vals
);
979 case TokenID::NumericLessThanEquals
:
980 return shortible(std::less_equal
<double>(), as_number
, s
, vals
);
982 case TokenID::NumericGreaterThan
:
983 return shortible(std::greater
<double>(), as_number
, s
, vals
);
985 case TokenID::NumericGreaterThanEquals
:
986 return shortible(std::greater_equal
<double>(), as_number
, s
, vals
);
989 case TokenID::DateEquals
:
990 return shortible(std::equal_to
<ceph::real_time
>(), as_date
, s
, vals
);
992 case TokenID::DateNotEquals
:
993 return shortible(std::not2(std::equal_to
<ceph::real_time
>()),
996 case TokenID::DateLessThan
:
997 return shortible(std::less
<ceph::real_time
>(), as_date
, s
, vals
);
1000 case TokenID::DateLessThanEquals
:
1001 return shortible(std::less_equal
<ceph::real_time
>(), as_date
, s
, vals
);
1003 case TokenID::DateGreaterThan
:
1004 return shortible(std::greater
<ceph::real_time
>(), as_date
, s
, vals
);
1006 case TokenID::DateGreaterThanEquals
:
1007 return shortible(std::greater_equal
<ceph::real_time
>(), as_date
, s
,
1012 return shortible(std::equal_to
<bool>(), as_bool
, s
, vals
);
1015 case TokenID::BinaryEquals
:
1016 return shortible(std::equal_to
<ceph::bufferlist
>(), as_binary
, s
,
1020 case TokenID::IpAddress
:
1021 return shortible(std::equal_to
<MaskedIP
>(), as_network
, s
, vals
);
1023 case TokenID::NotIpAddress
:
1024 return shortible(std::not2(std::equal_to
<MaskedIP
>()), as_network
, s
,
1028 // Amazon Resource Names! (Does S3 need this?)
1029 TokenID::ArnEquals
, TokenID::ArnNotEquals
, TokenID::ArnLike
,
1030 TokenID::ArnNotLike
,
1038 optional
<MaskedIP
> Condition::as_network(const string
& s
) {
1045 auto slash
= s
.find('/');
1046 if (slash
== string::npos
) {
1047 m
.prefix
= m
.v6
? 128 : 32;
1050 m
.prefix
= strtoul(s
.data() + slash
+ 1, &end
, 10);
1051 if (*end
!= 0 || (m
.v6
&& m
.prefix
> 128) ||
1052 (!m
.v6
&& m
.prefix
> 32)) {
1060 if (slash
!= string::npos
) {
1061 t
.assign(s
, 0, slash
);
1066 struct sockaddr_in6 a
;
1067 if (inet_pton(AF_INET6
, p
->c_str(), static_cast<void*>(&a
)) != 1) {
1071 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[0]) << 0;
1072 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[1]) << 8;
1073 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[2]) << 16;
1074 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[3]) << 24;
1075 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[4]) << 32;
1076 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[5]) << 40;
1077 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[6]) << 48;
1078 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[7]) << 56;
1079 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[8]) << 64;
1080 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[9]) << 72;
1081 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[10]) << 80;
1082 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[11]) << 88;
1083 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[12]) << 96;
1084 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[13]) << 104;
1085 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[14]) << 112;
1086 m
.addr
|= Address(a
.sin6_addr
.s6_addr
[15]) << 120;
1088 struct sockaddr_in a
;
1089 if (inet_pton(AF_INET
, p
->c_str(), static_cast<void*>(&a
)) != 1) {
1092 m
.addr
= ntohl(a
.sin_addr
.s_addr
);
1099 const char* condop_string(const TokenID t
) {
1101 case TokenID::StringEquals
:
1102 return "StringEquals";
1104 case TokenID::StringNotEquals
:
1105 return "StringNotEquals";
1107 case TokenID::StringEqualsIgnoreCase
:
1108 return "StringEqualsIgnoreCase";
1110 case TokenID::StringNotEqualsIgnoreCase
:
1111 return "StringNotEqualsIgnoreCase";
1113 case TokenID::StringLike
:
1114 return "StringLike";
1116 case TokenID::StringNotLike
:
1117 return "StringNotLike";
1120 case TokenID::NumericEquals
:
1121 return "NumericEquals";
1123 case TokenID::NumericNotEquals
:
1124 return "NumericNotEquals";
1126 case TokenID::NumericLessThan
:
1127 return "NumericLessThan";
1129 case TokenID::NumericLessThanEquals
:
1130 return "NumericLessThanEquals";
1132 case TokenID::NumericGreaterThan
:
1133 return "NumericGreaterThan";
1135 case TokenID::NumericGreaterThanEquals
:
1136 return "NumericGreaterThanEquals";
1138 case TokenID::DateEquals
:
1139 return "DateEquals";
1141 case TokenID::DateNotEquals
:
1142 return "DateNotEquals";
1144 case TokenID::DateLessThan
:
1145 return "DateLessThan";
1147 case TokenID::DateLessThanEquals
:
1148 return "DateLessThanEquals";
1150 case TokenID::DateGreaterThan
:
1151 return "DateGreaterThan";
1153 case TokenID::DateGreaterThanEquals
:
1154 return "DateGreaterThanEquals";
1159 case TokenID::BinaryEquals
:
1160 return "BinaryEquals";
1162 case TokenID::IpAddress
:
1163 return "case TokenID::IpAddress";
1165 case TokenID::NotIpAddress
:
1166 return "NotIpAddress";
1168 case TokenID::ArnEquals
:
1171 case TokenID::ArnNotEquals
:
1172 return "ArnNotEquals";
1174 case TokenID::ArnLike
:
1177 case TokenID::ArnNotLike
:
1178 return "ArnNotLike";
1184 return "InvalidConditionOperator";
1188 template<typename Iterator
>
1189 ostream
& print_array(ostream
& m
, Iterator begin
, Iterator end
) {
1193 auto beforelast
= end
- 1;
1195 for (auto i
= begin
; i
!= end
; ++i
) {
1197 if (i
!= beforelast
) {
1209 ostream
& operator <<(ostream
& m
, const Condition
& c
) {
1210 m
<< "{ " << condop_string(c
.op
);
1214 m
<< ": { " << c
.key
;
1215 print_array(m
, c
.vals
.cbegin(), c
.vals
.cend());
1219 string
to_string(const Condition
& c
) {
1225 Effect
Statement::eval(const Environment
& e
,
1226 optional
<const rgw::auth::Identity
&> ida
,
1227 uint64_t act
, const ARN
& res
) const {
1228 if (ida
&& (!ida
->is_identity(princ
) || ida
->is_identity(noprinc
))) {
1229 return Effect::Pass
;
1233 if (!std::any_of(resource
.begin(), resource
.end(),
1234 [&res
](const ARN
& pattern
) {
1235 return pattern
.match(res
);
1237 (std::any_of(notresource
.begin(), notresource
.end(),
1238 [&res
](const ARN
& pattern
) {
1239 return pattern
.match(res
);
1241 return Effect::Pass
;
1244 if (!(action
& act
) || (notaction
& act
)) {
1245 return Effect::Pass
;
1248 if (std::all_of(conditions
.begin(),
1250 [&e
](const Condition
& c
) { return c
.eval(e
);})) {
1254 return Effect::Pass
;
1258 const char* action_bit_string(uint64_t action
) {
1261 return "s3:GetObject";
1263 case s3GetObjectVersion
:
1264 return "s3:GetObjectVersion";
1267 return "s3:PutObject";
1269 case s3GetObjectAcl
:
1270 return "s3:GetObjectAcl";
1272 case s3GetObjectVersionAcl
:
1273 return "s3:GetObjectVersionAcl";
1275 case s3PutObjectAcl
:
1276 return "s3:PutObjectAcl";
1278 case s3PutObjectVersionAcl
:
1279 return "s3:PutObjectVersionAcl";
1281 case s3DeleteObject
:
1282 return "s3:DeleteObject";
1284 case s3DeleteObjectVersion
:
1285 return "s3:DeleteObjectVersion";
1287 case s3ListMultipartUploadParts
:
1288 return "s3:ListMultipartUploadParts";
1290 case s3AbortMultipartUpload
:
1291 return "s3:AbortMultipartUpload";
1293 case s3GetObjectTorrent
:
1294 return "s3:GetObjectTorrent";
1296 case s3GetObjectVersionTorrent
:
1297 return "s3:GetObjectVersionTorrent";
1299 case s3RestoreObject
:
1300 return "s3:RestoreObject";
1302 case s3CreateBucket
:
1303 return "s3:CreateBucket";
1305 case s3DeleteBucket
:
1306 return "s3:DeleteBucket";
1309 return "s3:ListBucket";
1311 case s3ListBucketVersions
:
1312 return "s3:ListBucketVersions";
1313 case s3ListAllMyBuckets
:
1314 return "s3:ListAllMyBuckets";
1316 case s3ListBucketMultiPartUploads
:
1317 return "s3:ListBucketMultiPartUploads";
1319 case s3GetAccelerateConfiguration
:
1320 return "s3:GetAccelerateConfiguration";
1322 case s3PutAccelerateConfiguration
:
1323 return "s3:PutAccelerateConfiguration";
1325 case s3GetBucketAcl
:
1326 return "s3:GetBucketAcl";
1328 case s3PutBucketAcl
:
1329 return "s3:PutBucketAcl";
1331 case s3GetBucketCORS
:
1332 return "s3:GetBucketCORS";
1334 case s3PutBucketCORS
:
1335 return "s3:PutBucketCORS";
1337 case s3GetBucketVersioning
:
1338 return "s3:GetBucketVersioning";
1340 case s3PutBucketVersioning
:
1341 return "s3:PutBucketVersioning";
1343 case s3GetBucketRequestPayment
:
1344 return "s3:GetBucketRequestPayment";
1346 case s3PutBucketRequestPayment
:
1347 return "s3:PutBucketRequestPayment";
1349 case s3GetBucketLocation
:
1350 return "s3:GetBucketLocation";
1352 case s3GetBucketPolicy
:
1353 return "s3:GetBucketPolicy";
1355 case s3DeleteBucketPolicy
:
1356 return "s3:DeleteBucketPolicy";
1358 case s3PutBucketPolicy
:
1359 return "s3:PutBucketPolicy";
1361 case s3GetBucketNotification
:
1362 return "s3:GetBucketNotification";
1364 case s3PutBucketNotification
:
1365 return "s3:PutBucketNotification";
1367 case s3GetBucketLogging
:
1368 return "s3:GetBucketLogging";
1370 case s3PutBucketLogging
:
1371 return "s3:PutBucketLogging";
1373 case s3GetBucketTagging
:
1374 return "s3:GetBucketTagging";
1376 case s3PutBucketTagging
:
1377 return "s3:PutBucketTagging";
1379 case s3GetBucketWebsite
:
1380 return "s3:GetBucketWebsite";
1382 case s3PutBucketWebsite
:
1383 return "s3:PutBucketWebsite";
1385 case s3DeleteBucketWebsite
:
1386 return "s3:DeleteBucketWebsite";
1388 case s3GetLifecycleConfiguration
:
1389 return "s3:GetLifecycleConfiguration";
1391 case s3PutLifecycleConfiguration
:
1392 return "s3:PutLifecycleConfiguration";
1394 case s3PutReplicationConfiguration
:
1395 return "s3:PutReplicationConfiguration";
1397 case s3GetReplicationConfiguration
:
1398 return "s3:GetReplicationConfiguration";
1400 case s3DeleteReplicationConfiguration
:
1401 return "s3:DeleteReplicationConfiguration";
1403 case s3PutObjectTagging
:
1404 return "s3:PutObjectTagging";
1406 case s3PutObjectVersionTagging
:
1407 return "s3:PutObjectVersionTagging";
1409 case s3GetObjectTagging
:
1410 return "s3:GetObjectTagging";
1412 case s3GetObjectVersionTagging
:
1413 return "s3:GetObjectVersionTagging";
1415 case s3DeleteObjectTagging
:
1416 return "s3:DeleteObjectTagging";
1418 case s3DeleteObjectVersionTagging
:
1419 return "s3:DeleteObjectVersionTagging";
1424 ostream
& print_actions(ostream
& m
, const uint64_t a
) {
1427 for (auto i
= 0U; i
< s3Count
; ++i
) {
1434 m
<< action_bit_string(1 << i
);
1446 ostream
& operator <<(ostream
& m
, const Statement
& s
) {
1449 m
<< "Sid: " << *s
.sid
<< ", ";
1451 if (!s
.princ
.empty()) {
1453 print_array(m
, s
.princ
.cbegin(), s
.princ
.cend());
1456 if (!s
.noprinc
.empty()) {
1457 m
<< "NotPrincipal: ";
1458 print_array(m
, s
.noprinc
.cbegin(), s
.noprinc
.cend());
1463 (s
.effect
== Effect::Allow
?
1464 (const char*) "Allow" :
1465 (const char*) "Deny");
1467 if (s
.action
|| s
.notaction
|| !s
.resource
.empty() ||
1468 !s
.notresource
.empty() || !s
.conditions
.empty()) {
1474 print_actions(m
, s
.action
);
1476 if (s
.notaction
|| !s
.resource
.empty() ||
1477 !s
.notresource
.empty() || !s
.conditions
.empty()) {
1484 print_actions(m
, s
.notaction
);
1486 if (!s
.resource
.empty() || !s
.notresource
.empty() ||
1487 !s
.conditions
.empty()) {
1492 if (!s
.resource
.empty()) {
1494 print_array(m
, s
.resource
.cbegin(), s
.resource
.cend());
1496 if (!s
.notresource
.empty() || !s
.conditions
.empty()) {
1501 if (!s
.notresource
.empty()) {
1502 m
<< "NotResource: ";
1503 print_array(m
, s
.notresource
.cbegin(), s
.notresource
.cend());
1505 if (!s
.conditions
.empty()) {
1510 if (!s
.conditions
.empty()) {
1512 print_array(m
, s
.conditions
.cbegin(), s
.conditions
.cend());
1518 string
to_string(const Statement
& s
) {
1524 Policy::Policy(CephContext
* cct
, const string
& tenant
,
1525 const bufferlist
& _text
)
1526 : text(_text
.to_str()) {
1527 StringStream
ss(text
.data());
1528 PolicyParser
pp(cct
, tenant
, *this);
1529 auto pr
= Reader
{}.Parse
<kParseNumbersAsStringsFlag
|
1530 kParseCommentsFlag
>(ss
, pp
);
1532 throw PolicyParseException(std::move(pr
));
1536 Effect
Policy::eval(const Environment
& e
,
1537 optional
<const rgw::auth::Identity
&> ida
,
1538 std::uint64_t action
, const ARN
& resource
) const {
1539 auto allowed
= false;
1540 for (auto& s
: statements
) {
1541 auto g
= s
.eval(e
, ida
, action
, resource
);
1542 if (g
== Effect::Deny
) {
1544 } else if (g
== Effect::Allow
) {
1548 return allowed
? Effect::Allow
: Effect::Pass
;
1551 ostream
& operator <<(ostream
& m
, const Policy
& p
) {
1553 << (p
.version
== Version::v2008_10_17
? "2008-10-17" : "2012-10-17");
1555 if (p
.id
|| !p
.statements
.empty()) {
1560 m
<< "Id: " << *p
.id
;
1561 if (!p
.statements
.empty()) {
1566 if (!p
.statements
.empty()) {
1567 m
<< "Statements: ";
1568 print_array(m
, p
.statements
.cbegin(), p
.statements
.cend());
1574 string
to_string(const Policy
& p
) {