1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab ft=cpp
8 #include <boost/tokenizer.hpp>
10 #include "json_spirit/json_spirit.h"
11 #include "common/ceph_json.h"
12 #include "common/Formatter.h"
15 #include "rgw_common.h"
17 #include "rgw_string.h"
18 #include "rgw_http_errors.h"
20 #include "rgw_data_sync.h"
22 #include "common/ceph_crypto.h"
23 #include "common/armor.h"
24 #include "common/errno.h"
25 #include "common/Clock.h"
26 #include "common/convenience.h"
27 #include "common/strtol.h"
28 #include "include/str_list.h"
29 #include "rgw_crypt_sanitize.h"
30 #include "rgw_bucket_sync.h"
31 #include "rgw_sync_policy.h"
33 #include "services/svc_zone.h"
37 #define dout_context g_ceph_context
38 #define dout_subsys ceph_subsys_rgw
41 using rgw::IAM::Effect
;
42 using rgw::IAM::op_to_perm
;
43 using rgw::IAM::Policy
;
45 const uint32_t RGWBucketInfo::NUM_SHARDS_BLIND_BUCKET(UINT32_MAX
);
47 rgw_http_errors
rgw_http_s3_errors({
49 { STATUS_CREATED
, {201, "Created" }},
50 { STATUS_ACCEPTED
, {202, "Accepted" }},
51 { STATUS_NO_CONTENT
, {204, "NoContent" }},
52 { STATUS_PARTIAL_CONTENT
, {206, "" }},
53 { ERR_PERMANENT_REDIRECT
, {301, "PermanentRedirect" }},
54 { ERR_WEBSITE_REDIRECT
, {301, "WebsiteRedirect" }},
55 { STATUS_REDIRECT
, {303, "" }},
56 { ERR_NOT_MODIFIED
, {304, "NotModified" }},
57 { EINVAL
, {400, "InvalidArgument" }},
58 { ERR_INVALID_REQUEST
, {400, "InvalidRequest" }},
59 { ERR_INVALID_DIGEST
, {400, "InvalidDigest" }},
60 { ERR_BAD_DIGEST
, {400, "BadDigest" }},
61 { ERR_INVALID_LOCATION_CONSTRAINT
, {400, "InvalidLocationConstraint" }},
62 { ERR_ZONEGROUP_DEFAULT_PLACEMENT_MISCONFIGURATION
, {400, "ZonegroupDefaultPlacementMisconfiguration" }},
63 { ERR_INVALID_BUCKET_NAME
, {400, "InvalidBucketName" }},
64 { ERR_INVALID_OBJECT_NAME
, {400, "InvalidObjectName" }},
65 { ERR_UNRESOLVABLE_EMAIL
, {400, "UnresolvableGrantByEmailAddress" }},
66 { ERR_INVALID_PART
, {400, "InvalidPart" }},
67 { ERR_INVALID_PART_ORDER
, {400, "InvalidPartOrder" }},
68 { ERR_REQUEST_TIMEOUT
, {400, "RequestTimeout" }},
69 { ERR_TOO_LARGE
, {400, "EntityTooLarge" }},
70 { ERR_TOO_SMALL
, {400, "EntityTooSmall" }},
71 { ERR_TOO_MANY_BUCKETS
, {400, "TooManyBuckets" }},
72 { ERR_MALFORMED_XML
, {400, "MalformedXML" }},
73 { ERR_AMZ_CONTENT_SHA256_MISMATCH
, {400, "XAmzContentSHA256Mismatch" }},
74 { ERR_MALFORMED_DOC
, {400, "MalformedPolicyDocument"}},
75 { ERR_INVALID_TAG
, {400, "InvalidTag"}},
76 { ERR_MALFORMED_ACL_ERROR
, {400, "MalformedACLError" }},
77 { ERR_INVALID_CORS_RULES_ERROR
, {400, "InvalidRequest" }},
78 { ERR_INVALID_WEBSITE_ROUTING_RULES_ERROR
, {400, "InvalidRequest" }},
79 { ERR_INVALID_ENCRYPTION_ALGORITHM
, {400, "InvalidEncryptionAlgorithmError" }},
80 { ERR_INVALID_RETENTION_PERIOD
,{400, "InvalidRetentionPeriod"}},
81 { ERR_LIMIT_EXCEEDED
, {400, "LimitExceeded" }},
82 { ERR_LENGTH_REQUIRED
, {411, "MissingContentLength" }},
83 { EACCES
, {403, "AccessDenied" }},
84 { EPERM
, {403, "AccessDenied" }},
85 { ERR_SIGNATURE_NO_MATCH
, {403, "SignatureDoesNotMatch" }},
86 { ERR_INVALID_ACCESS_KEY
, {403, "InvalidAccessKeyId" }},
87 { ERR_USER_SUSPENDED
, {403, "UserSuspended" }},
88 { ERR_REQUEST_TIME_SKEWED
, {403, "RequestTimeTooSkewed" }},
89 { ERR_QUOTA_EXCEEDED
, {403, "QuotaExceeded" }},
90 { ERR_MFA_REQUIRED
, {403, "AccessDenied" }},
91 { ENOENT
, {404, "NoSuchKey" }},
92 { ERR_NO_SUCH_BUCKET
, {404, "NoSuchBucket" }},
93 { ERR_NO_SUCH_WEBSITE_CONFIGURATION
, {404, "NoSuchWebsiteConfiguration" }},
94 { ERR_NO_SUCH_UPLOAD
, {404, "NoSuchUpload" }},
95 { ERR_NOT_FOUND
, {404, "Not Found"}},
96 { ERR_NO_SUCH_LC
, {404, "NoSuchLifecycleConfiguration"}},
97 { ERR_NO_SUCH_BUCKET_POLICY
, {404, "NoSuchBucketPolicy"}},
98 { ERR_NO_SUCH_USER
, {404, "NoSuchUser"}},
99 { ERR_NO_ROLE_FOUND
, {404, "NoSuchEntity"}},
100 { ERR_NO_CORS_FOUND
, {404, "NoSuchCORSConfiguration"}},
101 { ERR_NO_SUCH_SUBUSER
, {404, "NoSuchSubUser"}},
102 { ERR_NO_SUCH_ENTITY
, {404, "NoSuchEntity"}},
103 { ERR_NO_SUCH_CORS_CONFIGURATION
, {404, "NoSuchCORSConfiguration"}},
104 { ERR_NO_SUCH_OBJECT_LOCK_CONFIGURATION
, {404, "ObjectLockConfigurationNotFoundError"}},
105 { ERR_METHOD_NOT_ALLOWED
, {405, "MethodNotAllowed" }},
106 { ETIMEDOUT
, {408, "RequestTimeout" }},
107 { EEXIST
, {409, "BucketAlreadyExists" }},
108 { ERR_USER_EXIST
, {409, "UserAlreadyExists" }},
109 { ERR_EMAIL_EXIST
, {409, "EmailExists" }},
110 { ERR_KEY_EXIST
, {409, "KeyExists"}},
111 { ERR_TAG_CONFLICT
, {409, "OperationAborted"}},
112 { ERR_POSITION_NOT_EQUAL_TO_LENGTH
, {409, "PositionNotEqualToLength"}},
113 { ERR_OBJECT_NOT_APPENDABLE
, {409, "ObjectNotAppendable"}},
114 { ERR_INVALID_BUCKET_STATE
, {409, "InvalidBucketState"}},
115 { ERR_INVALID_OBJECT_STATE
, {403, "InvalidObjectState"}},
116 { ERR_INVALID_SECRET_KEY
, {400, "InvalidSecretKey"}},
117 { ERR_INVALID_KEY_TYPE
, {400, "InvalidKeyType"}},
118 { ERR_INVALID_CAP
, {400, "InvalidCapability"}},
119 { ERR_INVALID_TENANT_NAME
, {400, "InvalidTenantName" }},
120 { ENOTEMPTY
, {409, "BucketNotEmpty" }},
121 { ERR_PRECONDITION_FAILED
, {412, "PreconditionFailed" }},
122 { ERANGE
, {416, "InvalidRange" }},
123 { ERR_UNPROCESSABLE_ENTITY
, {422, "UnprocessableEntity" }},
124 { ERR_LOCKED
, {423, "Locked" }},
125 { ERR_INTERNAL_ERROR
, {500, "InternalError" }},
126 { ERR_NOT_IMPLEMENTED
, {501, "NotImplemented" }},
127 { ERR_SERVICE_UNAVAILABLE
, {503, "ServiceUnavailable"}},
128 { ERR_RATE_LIMITED
, {503, "SlowDown"}},
129 { ERR_ZERO_IN_URL
, {400, "InvalidRequest" }},
130 { ERR_NO_SUCH_TAG_SET
, {404, "NoSuchTagSetError"}},
131 { ERR_NO_SUCH_BUCKET_ENCRYPTION_CONFIGURATION
, {404, "ServerSideEncryptionConfigurationNotFoundError"}},
134 rgw_http_errors
rgw_http_swift_errors({
135 { EACCES
, {403, "AccessDenied" }},
136 { EPERM
, {401, "AccessDenied" }},
137 { ENAMETOOLONG
, {400, "Metadata name too long" }},
138 { ERR_USER_SUSPENDED
, {401, "UserSuspended" }},
139 { ERR_INVALID_UTF8
, {412, "Invalid UTF8" }},
140 { ERR_BAD_URL
, {412, "Bad URL" }},
141 { ERR_NOT_SLO_MANIFEST
, {400, "Not an SLO manifest" }},
142 { ERR_QUOTA_EXCEEDED
, {413, "QuotaExceeded" }},
143 { ENOTEMPTY
, {409, "There was a conflict when trying "
144 "to complete your request." }},
145 /* FIXME(rzarzynski): we need to find a way to apply Swift's error handling
146 * procedures also for ERR_ZERO_IN_URL. This make a problem as the validation
147 * is performed very early, even before setting the req_state::proto_flags. */
148 { ERR_ZERO_IN_URL
, {412, "Invalid UTF8 or contains NULL"}},
149 { ERR_RATE_LIMITED
, {498, "Rate Limited"}},
152 rgw_http_errors
rgw_http_sts_errors({
153 { ERR_PACKED_POLICY_TOO_LARGE
, {400, "PackedPolicyTooLarge" }},
154 { ERR_INVALID_IDENTITY_TOKEN
, {400, "InvalidIdentityToken" }},
157 rgw_http_errors
rgw_http_iam_errors({
158 { EINVAL
, {400, "InvalidInput" }},
159 { ENOENT
, {404, "NoSuchEntity"}},
160 { ERR_ROLE_EXISTS
, {409, "EntityAlreadyExists"}},
161 { ERR_DELETE_CONFLICT
, {409, "DeleteConflict"}},
162 { EEXIST
, {409, "EntityAlreadyExists"}},
163 { ERR_INTERNAL_ERROR
, {500, "ServiceFailure" }},
167 using namespace ceph::crypto
;
186 return (http_ret
== 200);
192 return !(http_ret
>= 200 && http_ret
<= 399);
195 // The requestURI transferred from the frontend can be abs_path or absoluteURI
196 // If it is absoluteURI, we should adjust it to abs_path for the following
197 // S3 authorization and some other processes depending on the requestURI
198 // The absoluteURI can start with "http://", "https://", "ws://" or "wss://"
199 static string
get_abs_path(const string
& request_uri
) {
200 const static string ABS_PREFIXS
[] = {"http://", "https://", "ws://", "wss://"};
202 for (int i
= 0; i
< 4; ++i
) {
203 if (boost::algorithm::starts_with(request_uri
, ABS_PREFIXS
[i
])) {
208 if (!isAbs
) { // it is not a valid absolute uri
211 size_t beg_pos
= request_uri
.find("://") + 3;
212 size_t len
= request_uri
.size();
213 beg_pos
= request_uri
.find('/', beg_pos
);
214 if (beg_pos
== string::npos
) return request_uri
;
215 return request_uri
.substr(beg_pos
, len
- beg_pos
);
218 req_info::req_info(CephContext
*cct
, const class RGWEnv
*env
) : env(env
) {
219 method
= env
->get("REQUEST_METHOD", "");
220 script_uri
= env
->get("SCRIPT_URI", cct
->_conf
->rgw_script_uri
.c_str());
221 request_uri
= env
->get("REQUEST_URI", cct
->_conf
->rgw_request_uri
.c_str());
222 if (request_uri
[0] != '/') {
223 request_uri
= get_abs_path(request_uri
);
225 auto pos
= request_uri
.find('?');
226 if (pos
!= string::npos
) {
227 request_params
= request_uri
.substr(pos
+ 1);
228 request_uri
= request_uri
.substr(0, pos
);
230 request_params
= env
->get("QUERY_STRING", "");
232 host
= env
->get("HTTP_HOST", "");
234 // strip off any trailing :port from host (added by CrossFTP and maybe others)
235 size_t colon_offset
= host
.find_last_of(':');
236 if (colon_offset
!= string::npos
) {
237 bool all_digits
= true;
238 for (unsigned i
= colon_offset
+ 1; i
< host
.size(); ++i
) {
239 if (!isdigit(host
[i
])) {
245 host
.resize(colon_offset
);
250 void req_info::rebuild_from(req_info
& src
)
253 script_uri
= src
.script_uri
;
255 if (src
.effective_uri
.empty()) {
256 request_uri
= src
.request_uri
;
258 request_uri
= src
.effective_uri
;
260 effective_uri
.clear();
263 x_meta_map
= src
.x_meta_map
;
264 x_meta_map
.erase("x-amz-date");
268 req_state::req_state(CephContext
* _cct
, RGWEnv
* e
, uint64_t id
)
269 : cct(_cct
), info(_cct
, e
), id(id
)
271 enable_ops_log
= e
->get_enable_ops_log();
272 enable_usage_log
= e
->get_enable_usage_log();
273 defer_to_bucket_acls
= e
->get_defer_to_bucket_acls();
278 req_state::~req_state() {
282 std::ostream
& req_state::gen_prefix(std::ostream
& out
) const
284 auto p
= out
.precision();
285 return out
<< "req " << id
<< ' '
286 << std::setprecision(3) << std::fixed
<< time_elapsed() // '0.123s'
287 << std::setprecision(p
) << std::defaultfloat
<< ' ';
290 bool search_err(rgw_http_errors
& errs
, int err_no
, int& http_ret
, string
& code
)
292 auto r
= errs
.find(err_no
);
293 if (r
!= errs
.end()) {
294 http_ret
= r
->second
.first
;
295 code
= r
->second
.second
;
301 void set_req_state_err(struct rgw_err
& err
, /* out */
303 const int prot_flags
) /* in */
310 if (prot_flags
& RGW_REST_SWIFT
) {
311 if (search_err(rgw_http_swift_errors
, err_no
, err
.http_ret
, err
.err_code
))
315 if (prot_flags
& RGW_REST_STS
) {
316 if (search_err(rgw_http_sts_errors
, err_no
, err
.http_ret
, err
.err_code
))
320 if (prot_flags
& RGW_REST_IAM
) {
321 if (search_err(rgw_http_iam_errors
, err_no
, err
.http_ret
, err
.err_code
))
325 //Default to searching in s3 errors
326 if (search_err(rgw_http_s3_errors
, err_no
, err
.http_ret
, err
.err_code
))
328 dout(0) << "WARNING: set_req_state_err err_no=" << err_no
329 << " resorting to 500" << dendl
;
332 err
.err_code
= "UnknownError";
335 void set_req_state_err(struct req_state
* s
, int err_no
, const string
& err_msg
)
338 set_req_state_err(s
, err_no
);
339 if (s
->prot_flags
& RGW_REST_SWIFT
&& !err_msg
.empty()) {
340 /* TODO(rzarzynski): there never ever should be a check like this one.
341 * It's here only for the sake of the patch's backportability. Further
342 * commits will move the logic to a per-RGWHandler replacement of
343 * the end_header() function. Alternativaly, we might consider making
344 * that just for the dump(). Please take a look on @cbodley's comments
345 * in PR #10690 (https://github.com/ceph/ceph/pull/10690). */
346 s
->err
.err_code
= err_msg
;
348 s
->err
.message
= err_msg
;
353 void set_req_state_err(struct req_state
* s
, int err_no
)
356 set_req_state_err(s
->err
, err_no
, s
->prot_flags
);
360 void dump(struct req_state
* s
)
362 if (s
->format
!= RGW_FORMAT_HTML
)
363 s
->formatter
->open_object_section("Error");
364 if (!s
->err
.err_code
.empty())
365 s
->formatter
->dump_string("Code", s
->err
.err_code
);
366 if (!s
->err
.message
.empty())
367 s
->formatter
->dump_string("Message", s
->err
.message
);
368 if (!s
->bucket_name
.empty()) // TODO: connect to expose_bucket
369 s
->formatter
->dump_string("BucketName", s
->bucket_name
);
370 if (!s
->trans_id
.empty()) // TODO: connect to expose_bucket or another toggle
371 s
->formatter
->dump_string("RequestId", s
->trans_id
);
372 s
->formatter
->dump_string("HostId", s
->host_id
);
373 if (s
->format
!= RGW_FORMAT_HTML
)
374 s
->formatter
->close_section();
382 #define STR_LEN_ENTRY(s) { s, sizeof(s) - 1 }
384 struct str_len meta_prefixes
[] = { STR_LEN_ENTRY("HTTP_X_AMZ"),
385 STR_LEN_ENTRY("HTTP_X_GOOG"),
386 STR_LEN_ENTRY("HTTP_X_DHO"),
387 STR_LEN_ENTRY("HTTP_X_RGW"),
388 STR_LEN_ENTRY("HTTP_X_OBJECT"),
389 STR_LEN_ENTRY("HTTP_X_CONTAINER"),
390 STR_LEN_ENTRY("HTTP_X_ACCOUNT"),
393 void req_info::init_meta_info(const DoutPrefixProvider
*dpp
, bool *found_bad_meta
)
396 crypt_attribute_map
.clear();
398 for (const auto& kv
: env
->get_map()) {
400 const string
& header_name
= kv
.first
;
401 const string
& val
= kv
.second
;
402 for (int prefix_num
= 0; (prefix
= meta_prefixes
[prefix_num
].str
) != NULL
; prefix_num
++) {
403 int len
= meta_prefixes
[prefix_num
].len
;
404 const char *p
= header_name
.c_str();
405 if (strncmp(p
, prefix
, len
) == 0) {
406 ldpp_dout(dpp
, 10) << "meta>> " << p
<< dendl
;
407 const char *name
= p
+len
; /* skip the prefix */
408 int name_len
= header_name
.size() - len
;
410 if (found_bad_meta
&& strncmp(name
, "_META_", name_len
) == 0)
411 *found_bad_meta
= true;
413 char name_low
[meta_prefixes
[0].len
+ name_len
+ 1];
414 snprintf(name_low
, meta_prefixes
[0].len
- 5 + name_len
+ 1, "%s%s", meta_prefixes
[0].str
+ 5 /* skip HTTP_ */, name
); // normalize meta prefix
416 for (j
= 0; name_low
[j
]; j
++) {
417 if (name_low
[j
] == '_')
419 else if (name_low
[j
] == '-')
422 name_low
[j
] = tolower(name_low
[j
]);
426 auto it
= x_meta_map
.find(name_low
);
427 if (it
!= x_meta_map
.end()) {
428 string old
= it
->second
;
429 boost::algorithm::trim_right(old
);
432 x_meta_map
[name_low
] = old
;
434 x_meta_map
[name_low
] = val
;
436 if (strncmp(name_low
, "x-amz-server-side-encryption", 20) == 0) {
437 crypt_attribute_map
[name_low
] = val
;
442 for (const auto& kv
: x_meta_map
) {
443 ldpp_dout(dpp
, 10) << "x>> " << kv
.first
<< ":" << rgw::crypt_sanitize::x_meta_map
{kv
.first
, kv
.second
} << dendl
;
447 std::ostream
& operator<<(std::ostream
& oss
, const rgw_err
&err
)
449 oss
<< "rgw_err(http_ret=" << err
.http_ret
<< ", err_code='" << err
.err_code
<< "') ";
453 void rgw_add_amz_meta_header(
454 meta_map_t
& x_meta_map
,
455 const std::string
& k
,
456 const std::string
& v
)
458 auto it
= x_meta_map
.find(k
);
459 if (it
!= x_meta_map
.end()) {
460 std::string old
= it
->second
;
461 boost::algorithm::trim_right(old
);
470 bool rgw_set_amz_meta_header(
471 meta_map_t
& x_meta_map
,
472 const std::string
& k
,
473 const std::string
& v
,
474 rgw_set_action_if_set a
)
476 auto it
{ x_meta_map
.find(k
) };
477 bool r
{ it
!= x_meta_map
.end() };
485 std::string old
{ it
->second
};
486 boost::algorithm::trim_right(old
);
499 string
rgw_string_unquote(const string
& s
)
501 if (s
[0] != '"' || s
.size() < 2)
505 for (len
= s
.size(); len
> 2; --len
) {
506 if (s
[len
- 1] != ' ')
513 return s
.substr(1, len
- 2);
516 static bool check_str_end(const char *s
)
529 static bool check_gmt_end(const char *s
)
534 while (isspace(*s
)) {
538 /* check for correct timezone */
539 if ((strncmp(s
, "GMT", 3) != 0) &&
540 (strncmp(s
, "UTC", 3) != 0)) {
547 static bool parse_rfc850(const char *s
, struct tm
*t
)
549 // FIPS zeroization audit 20191115: this memset is not security related.
550 memset(t
, 0, sizeof(*t
));
551 return check_gmt_end(strptime(s
, "%A, %d-%b-%y %H:%M:%S ", t
));
554 static bool parse_asctime(const char *s
, struct tm
*t
)
556 // FIPS zeroization audit 20191115: this memset is not security related.
557 memset(t
, 0, sizeof(*t
));
558 return check_str_end(strptime(s
, "%a %b %d %H:%M:%S %Y", t
));
561 static bool parse_rfc1123(const char *s
, struct tm
*t
)
563 // FIPS zeroization audit 20191115: this memset is not security related.
564 memset(t
, 0, sizeof(*t
));
565 return check_gmt_end(strptime(s
, "%a, %d %b %Y %H:%M:%S ", t
));
568 static bool parse_rfc1123_alt(const char *s
, struct tm
*t
)
570 // FIPS zeroization audit 20191115: this memset is not security related.
571 memset(t
, 0, sizeof(*t
));
572 return check_str_end(strptime(s
, "%a, %d %b %Y %H:%M:%S %z", t
));
575 bool parse_rfc2616(const char *s
, struct tm
*t
)
577 return parse_rfc850(s
, t
) || parse_asctime(s
, t
) || parse_rfc1123(s
, t
) || parse_rfc1123_alt(s
,t
);
580 bool parse_iso8601(const char *s
, struct tm
*t
, uint32_t *pns
, bool extended_format
)
582 // FIPS zeroization audit 20191115: this memset is not security related.
583 memset(t
, 0, sizeof(*t
));
589 if (extended_format
) {
590 p
= strptime(s
, "%Y-%m-%dT%T", t
);
592 p
= strptime(s
, "%Y-%m-%d %T", t
);
595 p
= strptime(s
, "%Y%m%dT%H%M%S", t
);
598 dout(0) << "parse_iso8601 failed" << dendl
;
601 const std::string_view str
= rgw_trim_whitespace(std::string_view(p
));
602 int len
= str
.size();
604 if (len
== 0 || (len
== 1 && str
[0] == 'Z'))
612 std::string_view nsstr
= str
.substr(1, len
- 2);
613 int r
= stringtoul(std::string(nsstr
), &ms
);
621 if (nsstr
.size() > 9) {
622 nsstr
= nsstr
.substr(0, 9);
625 uint64_t mul_table
[] = { 0,
637 *pns
= ms
* mul_table
[nsstr
.size()];
642 int parse_key_value(string
& in_str
, const char *delim
, string
& key
, string
& val
)
647 auto pos
= in_str
.find(delim
);
648 if (pos
== string::npos
)
651 key
= rgw_trim_whitespace(in_str
.substr(0, pos
));
652 val
= rgw_trim_whitespace(in_str
.substr(pos
+ 1));
657 int parse_key_value(string
& in_str
, string
& key
, string
& val
)
659 return parse_key_value(in_str
, "=", key
,val
);
662 boost::optional
<std::pair
<std::string_view
, std::string_view
>>
663 parse_key_value(const std::string_view
& in_str
,
664 const std::string_view
& delim
)
666 const size_t pos
= in_str
.find(delim
);
667 if (pos
== std::string_view::npos
) {
671 const auto key
= rgw_trim_whitespace(in_str
.substr(0, pos
));
672 const auto val
= rgw_trim_whitespace(in_str
.substr(pos
+ 1));
674 return std::make_pair(key
, val
);
677 boost::optional
<std::pair
<std::string_view
, std::string_view
>>
678 parse_key_value(const std::string_view
& in_str
)
680 return parse_key_value(in_str
, "=");
683 int parse_time(const char *time_str
, real_time
*time
)
688 if (!parse_rfc2616(time_str
, &tm
) && !parse_iso8601(time_str
, &tm
, &ns
)) {
692 time_t sec
= internal_timegm(&tm
);
693 *time
= utime_t(sec
, ns
).to_real_time();
698 #define TIME_BUF_SIZE 128
700 void rgw_to_iso8601(const real_time
& t
, char *dest
, int buf_size
)
704 char buf
[TIME_BUF_SIZE
];
706 time_t epoch
= ut
.sec();
707 struct tm
*tmp
= gmtime_r(&epoch
, &result
);
711 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%T", tmp
) == 0)
714 snprintf(dest
, buf_size
, "%s.%03dZ", buf
, (int)(ut
.usec() / 1000));
717 void rgw_to_iso8601(const real_time
& t
, string
*dest
)
719 char buf
[TIME_BUF_SIZE
];
720 rgw_to_iso8601(t
, buf
, sizeof(buf
));
725 string
rgw_to_asctime(const utime_t
& t
)
733 * calculate the sha1 value of a given msg and key
735 void calc_hmac_sha1(const char *key
, int key_len
,
736 const char *msg
, int msg_len
, char *dest
)
737 /* destination should be CEPH_CRYPTO_HMACSHA1_DIGESTSIZE bytes long */
739 HMACSHA1
hmac((const unsigned char *)key
, key_len
);
740 hmac
.Update((const unsigned char *)msg
, msg_len
);
741 hmac
.Final((unsigned char *)dest
);
745 * calculate the sha256 value of a given msg and key
747 void calc_hmac_sha256(const char *key
, int key_len
,
748 const char *msg
, int msg_len
, char *dest
)
750 char hash_sha256
[CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
];
752 HMACSHA256
hmac((const unsigned char *)key
, key_len
);
753 hmac
.Update((const unsigned char *)msg
, msg_len
);
754 hmac
.Final((unsigned char *)hash_sha256
);
756 memcpy(dest
, hash_sha256
, CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
);
759 using ceph::crypto::SHA256
;
762 * calculate the sha256 hash value of a given msg
764 sha256_digest_t
calc_hash_sha256(const std::string_view
& msg
)
766 sha256_digest_t hash
;
769 hasher
.Update(reinterpret_cast<const unsigned char*>(msg
.data()), msg
.size());
770 hasher
.Final(hash
.v
);
775 SHA256
* calc_hash_sha256_open_stream()
780 void calc_hash_sha256_update_stream(SHA256
*hash
, const char *msg
, int len
)
782 hash
->Update((const unsigned char *)msg
, len
);
785 string
calc_hash_sha256_close_stream(SHA256
**phash
)
787 SHA256
*hash
= *phash
;
789 hash
= calc_hash_sha256_open_stream();
791 char hash_sha256
[CEPH_CRYPTO_HMACSHA256_DIGESTSIZE
];
793 hash
->Final((unsigned char *)hash_sha256
);
795 char hex_str
[(CEPH_CRYPTO_SHA256_DIGESTSIZE
* 2) + 1];
796 buf_to_hex((unsigned char *)hash_sha256
, CEPH_CRYPTO_SHA256_DIGESTSIZE
, hex_str
);
801 return std::string(hex_str
);
804 std::string
calc_hash_sha256_restart_stream(SHA256
**phash
)
806 const auto hash
= calc_hash_sha256_close_stream(phash
);
807 *phash
= calc_hash_sha256_open_stream();
814 auto delim_pos
= str
.find('=');
817 if (delim_pos
== string::npos
) {
822 name
= str
.substr(0, delim_pos
);
823 val
= str
.substr(delim_pos
+ 1);
829 int RGWHTTPArgs::parse(const DoutPrefixProvider
*dpp
)
841 int fpos
= str
.find('&', pos
);
846 std::string nameval
= url_decode(str
.substr(pos
, fpos
- pos
), true);
847 NameVal
nv(std::move(nameval
));
848 int ret
= nv
.parse();
850 string
& name
= nv
.get_name();
851 if (name
.find("X-Amz-") != string::npos
) {
852 std::for_each(name
.begin(),
856 c
= ::tolower(static_cast<unsigned char>(c
));
860 string
& val
= nv
.get_val();
861 ldpp_dout(dpp
, 10) << "name: " << name
<< " val: " << val
<< dendl
;
871 void RGWHTTPArgs::remove(const string
& name
)
873 auto val_iter
= val_map
.find(name
);
874 if (val_iter
!= std::end(val_map
)) {
875 val_map
.erase(val_iter
);
878 auto sys_val_iter
= sys_val_map
.find(name
);
879 if (sys_val_iter
!= std::end(sys_val_map
)) {
880 sys_val_map
.erase(sys_val_iter
);
883 auto subres_iter
= sub_resources
.find(name
);
884 if (subres_iter
!= std::end(sub_resources
)) {
885 sub_resources
.erase(subres_iter
);
889 void RGWHTTPArgs::append(const string
& name
, const string
& val
)
891 if (name
.compare(0, sizeof(RGW_SYS_PARAM_PREFIX
) - 1, RGW_SYS_PARAM_PREFIX
) == 0) {
892 sys_val_map
[name
] = val
;
897 // when sub_resources exclusive by object are added, please remember to update obj_sub_resource in RGWHTTPArgs::exist_obj_excl_sub_resource().
898 if ((name
.compare("acl") == 0) ||
899 (name
.compare("cors") == 0) ||
900 (name
.compare("notification") == 0) ||
901 (name
.compare("location") == 0) ||
902 (name
.compare("logging") == 0) ||
903 (name
.compare("usage") == 0) ||
904 (name
.compare("lifecycle") == 0) ||
905 (name
.compare("delete") == 0) ||
906 (name
.compare("uploads") == 0) ||
907 (name
.compare("partNumber") == 0) ||
908 (name
.compare("uploadId") == 0) ||
909 (name
.compare("versionId") == 0) ||
910 (name
.compare("start-date") == 0) ||
911 (name
.compare("end-date") == 0) ||
912 (name
.compare("versions") == 0) ||
913 (name
.compare("versioning") == 0) ||
914 (name
.compare("website") == 0) ||
915 (name
.compare("requestPayment") == 0) ||
916 (name
.compare("torrent") == 0) ||
917 (name
.compare("tagging") == 0) ||
918 (name
.compare("append") == 0) ||
919 (name
.compare("position") == 0) ||
920 (name
.compare("policyStatus") == 0) ||
921 (name
.compare("publicAccessBlock") == 0)) {
922 sub_resources
[name
] = val
;
923 } else if (name
[0] == 'r') { // root of all evil
924 if ((name
.compare("response-content-type") == 0) ||
925 (name
.compare("response-content-language") == 0) ||
926 (name
.compare("response-expires") == 0) ||
927 (name
.compare("response-cache-control") == 0) ||
928 (name
.compare("response-content-disposition") == 0) ||
929 (name
.compare("response-content-encoding") == 0)) {
930 sub_resources
[name
] = val
;
931 has_resp_modifier
= true;
933 } else if ((name
.compare("subuser") == 0) ||
934 (name
.compare("key") == 0) ||
935 (name
.compare("caps") == 0) ||
936 (name
.compare("index") == 0) ||
937 (name
.compare("policy") == 0) ||
938 (name
.compare("quota") == 0) ||
939 (name
.compare("list") == 0) ||
940 (name
.compare("object") == 0) ||
941 (name
.compare("sync") == 0)) {
942 if (!admin_subresource_added
) {
943 sub_resources
[name
] = "";
944 admin_subresource_added
= true;
949 const string
& RGWHTTPArgs::get(const string
& name
, bool *exists
) const
951 auto iter
= val_map
.find(name
);
952 bool e
= (iter
!= std::end(val_map
));
960 boost::optional
<const std::string
&>
961 RGWHTTPArgs::get_optional(const std::string
& name
) const
964 const std::string
& value
= get(name
, &exists
);
972 int RGWHTTPArgs::get_bool(const string
& name
, bool *val
, bool *exists
) const
974 map
<string
, string
>::const_iterator iter
;
975 iter
= val_map
.find(name
);
976 bool e
= (iter
!= val_map
.end());
981 const char *s
= iter
->second
.c_str();
983 if (strcasecmp(s
, "false") == 0) {
985 } else if (strcasecmp(s
, "true") == 0) {
995 int RGWHTTPArgs::get_bool(const char *name
, bool *val
, bool *exists
) const
998 return get_bool(s
, val
, exists
);
1001 void RGWHTTPArgs::get_bool(const char *name
, bool *val
, bool def_val
) const
1003 bool exists
= false;
1004 if ((get_bool(name
, val
, &exists
) < 0) ||
1010 int RGWHTTPArgs::get_int(const char *name
, int *val
, int def_val
) const
1012 bool exists
= false;
1014 val_str
= get(name
, &exists
);
1022 *val
= (int)strict_strtol(val_str
.c_str(), 10, &err
);
1030 string
RGWHTTPArgs::sys_get(const string
& name
, bool * const exists
) const
1032 const auto iter
= sys_val_map
.find(name
);
1033 const bool e
= (iter
!= sys_val_map
.end());
1039 return e
? iter
->second
: string();
1042 bool rgw_transport_is_secure(CephContext
*cct
, const RGWEnv
& env
)
1044 const auto& m
= env
.get_map();
1045 // frontend connected with ssl
1046 if (m
.count("SERVER_PORT_SECURE")) {
1049 // ignore proxy headers unless explicitly enabled
1050 if (!cct
->_conf
->rgw_trust_forwarded_https
) {
1053 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
1054 // Forwarded: by=<identifier>; for=<identifier>; host=<host>; proto=<http|https>
1055 auto i
= m
.find("HTTP_FORWARDED");
1056 if (i
!= m
.end() && i
->second
.find("proto=https") != std::string::npos
) {
1059 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto
1060 i
= m
.find("HTTP_X_FORWARDED_PROTO");
1061 if (i
!= m
.end() && i
->second
== "https") {
1070 struct perm_state_from_req_state
: public perm_state_base
{
1071 req_state
* const s
;
1072 perm_state_from_req_state(req_state
* const _s
)
1073 : perm_state_base(_s
->cct
,
1075 _s
->auth
.identity
.get(),
1076 _s
->bucket
.get() ? _s
->bucket
->get_info() : RGWBucketInfo(),
1078 _s
->defer_to_bucket_acls
,
1079 _s
->bucket_access_conf
),
1082 std::optional
<bool> get_request_payer() const override
{
1083 const char *request_payer
= s
->info
.env
->get("HTTP_X_AMZ_REQUEST_PAYER");
1084 if (!request_payer
) {
1086 request_payer
= s
->info
.args
.get("x-amz-request-payer", &exists
).c_str();
1092 if (strcasecmp(request_payer
, "requester") == 0) {
1096 return std::nullopt
;
1099 const char *get_referer() const override
{
1100 return s
->info
.env
->get("HTTP_REFERER");
1104 Effect
eval_or_pass(const boost::optional
<Policy
>& policy
,
1105 const rgw::IAM::Environment
& env
,
1106 boost::optional
<const rgw::auth::Identity
&> id
,
1108 const ARN
& resource
,
1109 boost::optional
<rgw::IAM::PolicyPrincipal
&> princ_type
=boost::none
) {
1111 return Effect::Pass
;
1113 return policy
->eval(env
, id
, op
, resource
, princ_type
);
1118 Effect
eval_identity_or_session_policies(const vector
<Policy
>& policies
,
1119 const rgw::IAM::Environment
& env
,
1122 auto policy_res
= Effect::Pass
, prev_res
= Effect::Pass
;
1123 for (auto& policy
: policies
) {
1124 if (policy_res
= eval_or_pass(policy
, env
, boost::none
, op
, arn
); policy_res
== Effect::Deny
)
1126 else if (policy_res
== Effect::Allow
)
1127 prev_res
= Effect::Allow
;
1128 else if (policy_res
== Effect::Pass
&& prev_res
== Effect::Allow
)
1129 policy_res
= Effect::Allow
;
1134 bool verify_user_permission(const DoutPrefixProvider
* dpp
,
1135 perm_state_base
* const s
,
1136 RGWAccessControlPolicy
* const user_acl
,
1137 const vector
<rgw::IAM::Policy
>& user_policies
,
1138 const vector
<rgw::IAM::Policy
>& session_policies
,
1139 const rgw::ARN
& res
,
1142 auto identity_policy_res
= eval_identity_or_session_policies(user_policies
, s
->env
, op
, res
);
1143 if (identity_policy_res
== Effect::Deny
) {
1147 if (! session_policies
.empty()) {
1148 auto session_policy_res
= eval_identity_or_session_policies(session_policies
, s
->env
, op
, res
);
1149 if (session_policy_res
== Effect::Deny
) {
1152 //Intersection of identity policies and session policies
1153 if (identity_policy_res
== Effect::Allow
&& session_policy_res
== Effect::Allow
) {
1159 if (identity_policy_res
== Effect::Allow
) {
1163 if (op
== rgw::IAM::s3CreateBucket
|| op
== rgw::IAM::s3ListAllMyBuckets
) {
1164 auto perm
= op_to_perm(op
);
1166 return verify_user_permission_no_policy(dpp
, s
, user_acl
, perm
);
1172 bool verify_user_permission_no_policy(const DoutPrefixProvider
* dpp
,
1173 struct perm_state_base
* const s
,
1174 RGWAccessControlPolicy
* const user_acl
,
1177 if (s
->identity
->get_identity_type() == TYPE_ROLE
)
1180 /* S3 doesn't support account ACLs. */
1184 if ((perm
& (int)s
->perm_mask
) != perm
)
1187 return user_acl
->verify_permission(dpp
, *s
->identity
, perm
, perm
);
1190 bool verify_user_permission(const DoutPrefixProvider
* dpp
,
1191 struct req_state
* const s
,
1192 const rgw::ARN
& res
,
1195 perm_state_from_req_state
ps(s
);
1196 return verify_user_permission(dpp
, &ps
, s
->user_acl
.get(), s
->iam_user_policies
, s
->session_policies
, res
, op
);
1199 bool verify_user_permission_no_policy(const DoutPrefixProvider
* dpp
,
1200 struct req_state
* const s
,
1203 perm_state_from_req_state
ps(s
);
1204 return verify_user_permission_no_policy(dpp
, &ps
, s
->user_acl
.get(), perm
);
1207 bool verify_requester_payer_permission(struct perm_state_base
*s
)
1209 if (!s
->bucket_info
.requester_pays
)
1212 if (s
->identity
->is_owner_of(s
->bucket_info
.owner
))
1215 if (s
->identity
->is_anonymous()) {
1219 auto request_payer
= s
->get_request_payer();
1220 if (request_payer
) {
1221 return *request_payer
;
1227 bool verify_bucket_permission(const DoutPrefixProvider
* dpp
,
1228 struct perm_state_base
* const s
,
1229 const rgw_bucket
& bucket
,
1230 RGWAccessControlPolicy
* const user_acl
,
1231 RGWAccessControlPolicy
* const bucket_acl
,
1232 const boost::optional
<Policy
>& bucket_policy
,
1233 const vector
<Policy
>& identity_policies
,
1234 const vector
<Policy
>& session_policies
,
1237 if (!verify_requester_payer_permission(s
))
1240 auto identity_policy_res
= eval_identity_or_session_policies(identity_policies
, s
->env
, op
, ARN(bucket
));
1241 if (identity_policy_res
== Effect::Deny
)
1244 rgw::IAM::PolicyPrincipal princ_type
= rgw::IAM::PolicyPrincipal::Other
;
1245 auto r
= eval_or_pass(bucket_policy
, s
->env
, *s
->identity
,
1246 op
, ARN(bucket
), princ_type
);
1247 if (r
== Effect::Deny
)
1250 //Take into account session policies, if the identity making a request is a role
1251 if (!session_policies
.empty()) {
1252 auto session_policy_res
= eval_identity_or_session_policies(session_policies
, s
->env
, op
, ARN(bucket
));
1253 if (session_policy_res
== Effect::Deny
) {
1256 if (princ_type
== rgw::IAM::PolicyPrincipal::Role
) {
1257 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
1258 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) ||
1259 (session_policy_res
== Effect::Allow
&& r
== Effect::Allow
))
1261 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Session
) {
1262 //Intersection of session policy and identity policy plus bucket policy
1263 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) || r
== Effect::Allow
)
1265 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Other
) {// there was no match in the bucket policy
1266 if (session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
)
1272 if (r
== Effect::Allow
|| identity_policy_res
== Effect::Allow
)
1273 // It looks like S3 ACLs only GRANT permissions rather than
1274 // denying them, so this should be safe.
1277 const auto perm
= op_to_perm(op
);
1279 return verify_bucket_permission_no_policy(dpp
, s
, user_acl
, bucket_acl
, perm
);
1282 bool verify_bucket_permission(const DoutPrefixProvider
* dpp
,
1283 struct req_state
* const s
,
1284 const rgw_bucket
& bucket
,
1285 RGWAccessControlPolicy
* const user_acl
,
1286 RGWAccessControlPolicy
* const bucket_acl
,
1287 const boost::optional
<Policy
>& bucket_policy
,
1288 const vector
<Policy
>& user_policies
,
1289 const vector
<Policy
>& session_policies
,
1292 perm_state_from_req_state
ps(s
);
1293 return verify_bucket_permission(dpp
, &ps
, bucket
,
1294 user_acl
, bucket_acl
,
1295 bucket_policy
, user_policies
,
1296 session_policies
, op
);
1299 bool verify_bucket_permission_no_policy(const DoutPrefixProvider
* dpp
, struct perm_state_base
* const s
,
1300 RGWAccessControlPolicy
* const user_acl
,
1301 RGWAccessControlPolicy
* const bucket_acl
,
1307 if ((perm
& (int)s
->perm_mask
) != perm
)
1310 if (bucket_acl
->verify_permission(dpp
, *s
->identity
, perm
, perm
,
1312 s
->bucket_access_conf
&&
1313 s
->bucket_access_conf
->ignore_public_acls()))
1319 return user_acl
->verify_permission(dpp
, *s
->identity
, perm
, perm
);
1322 bool verify_bucket_permission_no_policy(const DoutPrefixProvider
* dpp
, struct req_state
* const s
,
1323 RGWAccessControlPolicy
* const user_acl
,
1324 RGWAccessControlPolicy
* const bucket_acl
,
1327 perm_state_from_req_state
ps(s
);
1328 return verify_bucket_permission_no_policy(dpp
,
1335 bool verify_bucket_permission_no_policy(const DoutPrefixProvider
* dpp
, struct req_state
* const s
, const int perm
)
1337 perm_state_from_req_state
ps(s
);
1339 if (!verify_requester_payer_permission(&ps
))
1342 return verify_bucket_permission_no_policy(dpp
,
1345 s
->bucket_acl
.get(),
1349 bool verify_bucket_permission(const DoutPrefixProvider
* dpp
, struct req_state
* const s
, const uint64_t op
)
1351 if (rgw::sal::Bucket::empty(s
->bucket
)) {
1352 // request is missing a bucket name
1356 perm_state_from_req_state
ps(s
);
1358 return verify_bucket_permission(dpp
,
1360 s
->bucket
->get_key(),
1362 s
->bucket_acl
.get(),
1364 s
->iam_user_policies
,
1365 s
->session_policies
,
1369 // Authorize anyone permitted by the bucket policy, identity policies, session policies and the bucket owner
1370 // unless explicitly denied by the policy.
1372 int verify_bucket_owner_or_policy(struct req_state
* const s
,
1375 auto identity_policy_res
= eval_identity_or_session_policies(s
->iam_user_policies
, s
->env
, op
, ARN(s
->bucket
->get_key()));
1376 if (identity_policy_res
== Effect::Deny
) {
1380 rgw::IAM::PolicyPrincipal princ_type
= rgw::IAM::PolicyPrincipal::Other
;
1381 auto e
= eval_or_pass(s
->iam_policy
,
1382 s
->env
, *s
->auth
.identity
,
1383 op
, ARN(s
->bucket
->get_key()), princ_type
);
1384 if (e
== Effect::Deny
) {
1388 if (!s
->session_policies
.empty()) {
1389 auto session_policy_res
= eval_identity_or_session_policies(s
->session_policies
, s
->env
, op
, ARN(s
->bucket
->get_key()));
1390 if (session_policy_res
== Effect::Deny
) {
1393 if (princ_type
== rgw::IAM::PolicyPrincipal::Role
) {
1394 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
1395 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) ||
1396 (session_policy_res
== Effect::Allow
&& e
== Effect::Allow
))
1398 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Session
) {
1399 //Intersection of session policy and identity policy plus bucket policy
1400 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) || e
== Effect::Allow
)
1402 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Other
) {// there was no match in the bucket policy
1403 if (session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
)
1409 if (e
== Effect::Allow
||
1410 identity_policy_res
== Effect::Allow
||
1411 (e
== Effect::Pass
&&
1412 identity_policy_res
== Effect::Pass
&&
1413 s
->auth
.identity
->is_owner_of(s
->bucket_owner
.get_id()))) {
1421 static inline bool check_deferred_bucket_perms(const DoutPrefixProvider
* dpp
,
1422 struct perm_state_base
* const s
,
1423 const rgw_bucket
& bucket
,
1424 RGWAccessControlPolicy
* const user_acl
,
1425 RGWAccessControlPolicy
* const bucket_acl
,
1426 const boost::optional
<Policy
>& bucket_policy
,
1427 const vector
<Policy
>& identity_policies
,
1428 const vector
<Policy
>& session_policies
,
1429 const uint8_t deferred_check
,
1432 return (s
->defer_to_bucket_acls
== deferred_check \
1433 && verify_bucket_permission(dpp
, s
, bucket
, user_acl
, bucket_acl
, bucket_policy
, identity_policies
, session_policies
,op
));
1436 static inline bool check_deferred_bucket_only_acl(const DoutPrefixProvider
* dpp
,
1437 struct perm_state_base
* const s
,
1438 RGWAccessControlPolicy
* const user_acl
,
1439 RGWAccessControlPolicy
* const bucket_acl
,
1440 const uint8_t deferred_check
,
1443 return (s
->defer_to_bucket_acls
== deferred_check \
1444 && verify_bucket_permission_no_policy(dpp
, s
, user_acl
, bucket_acl
, perm
));
1447 bool verify_object_permission(const DoutPrefixProvider
* dpp
, struct perm_state_base
* const s
,
1449 RGWAccessControlPolicy
* const user_acl
,
1450 RGWAccessControlPolicy
* const bucket_acl
,
1451 RGWAccessControlPolicy
* const object_acl
,
1452 const boost::optional
<Policy
>& bucket_policy
,
1453 const vector
<Policy
>& identity_policies
,
1454 const vector
<Policy
>& session_policies
,
1457 if (!verify_requester_payer_permission(s
))
1460 auto identity_policy_res
= eval_identity_or_session_policies(identity_policies
, s
->env
, op
, ARN(obj
));
1461 if (identity_policy_res
== Effect::Deny
)
1464 rgw::IAM::PolicyPrincipal princ_type
= rgw::IAM::PolicyPrincipal::Other
;
1465 auto r
= eval_or_pass(bucket_policy
, s
->env
, *s
->identity
, op
, ARN(obj
), princ_type
);
1466 if (r
== Effect::Deny
)
1469 if (!session_policies
.empty()) {
1470 auto session_policy_res
= eval_identity_or_session_policies(session_policies
, s
->env
, op
, ARN(obj
));
1471 if (session_policy_res
== Effect::Deny
) {
1474 if (princ_type
== rgw::IAM::PolicyPrincipal::Role
) {
1475 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
1476 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) ||
1477 (session_policy_res
== Effect::Allow
&& r
== Effect::Allow
))
1479 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Session
) {
1480 //Intersection of session policy and identity policy plus bucket policy
1481 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) || r
== Effect::Allow
)
1483 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Other
) {// there was no match in the bucket policy
1484 if (session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
)
1490 if (r
== Effect::Allow
|| identity_policy_res
== Effect::Allow
)
1491 // It looks like S3 ACLs only GRANT permissions rather than
1492 // denying them, so this should be safe.
1495 const auto perm
= op_to_perm(op
);
1497 if (check_deferred_bucket_perms(dpp
, s
, obj
.bucket
, user_acl
, bucket_acl
, bucket_policy
,
1498 identity_policies
, session_policies
, RGW_DEFER_TO_BUCKET_ACLS_RECURSE
, op
) ||
1499 check_deferred_bucket_perms(dpp
, s
, obj
.bucket
, user_acl
, bucket_acl
, bucket_policy
,
1500 identity_policies
, session_policies
, RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL
, rgw::IAM::s3All
)) {
1508 bool ret
= object_acl
->verify_permission(dpp
, *s
->identity
, s
->perm_mask
, perm
,
1509 nullptr, /* http_referrer */
1510 s
->bucket_access_conf
&&
1511 s
->bucket_access_conf
->ignore_public_acls());
1516 if (!s
->cct
->_conf
->rgw_enforce_swift_acls
)
1519 if ((perm
& (int)s
->perm_mask
) != perm
)
1523 if (perm
& (RGW_PERM_READ
| RGW_PERM_READ_ACP
))
1524 swift_perm
|= RGW_PERM_READ_OBJS
;
1525 if (perm
& RGW_PERM_WRITE
)
1526 swift_perm
|= RGW_PERM_WRITE_OBJS
;
1531 /* we already verified the user mask above, so we pass swift_perm as the mask here,
1532 otherwise the mask might not cover the swift permissions bits */
1533 if (bucket_acl
->verify_permission(dpp
, *s
->identity
, swift_perm
, swift_perm
,
1540 return user_acl
->verify_permission(dpp
, *s
->identity
, swift_perm
, swift_perm
);
1543 bool verify_object_permission(const DoutPrefixProvider
* dpp
, struct req_state
* const s
,
1545 RGWAccessControlPolicy
* const user_acl
,
1546 RGWAccessControlPolicy
* const bucket_acl
,
1547 RGWAccessControlPolicy
* const object_acl
,
1548 const boost::optional
<Policy
>& bucket_policy
,
1549 const vector
<Policy
>& identity_policies
,
1550 const vector
<Policy
>& session_policies
,
1553 perm_state_from_req_state
ps(s
);
1554 return verify_object_permission(dpp
, &ps
, obj
,
1555 user_acl
, bucket_acl
,
1556 object_acl
, bucket_policy
,
1557 identity_policies
, session_policies
, op
);
1560 bool verify_object_permission_no_policy(const DoutPrefixProvider
* dpp
,
1561 struct perm_state_base
* const s
,
1562 RGWAccessControlPolicy
* const user_acl
,
1563 RGWAccessControlPolicy
* const bucket_acl
,
1564 RGWAccessControlPolicy
* const object_acl
,
1567 if (check_deferred_bucket_only_acl(dpp
, s
, user_acl
, bucket_acl
, RGW_DEFER_TO_BUCKET_ACLS_RECURSE
, perm
) ||
1568 check_deferred_bucket_only_acl(dpp
, s
, user_acl
, bucket_acl
, RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL
, RGW_PERM_FULL_CONTROL
)) {
1576 bool ret
= object_acl
->verify_permission(dpp
, *s
->identity
, s
->perm_mask
, perm
,
1577 nullptr, /* http referrer */
1578 s
->bucket_access_conf
&&
1579 s
->bucket_access_conf
->ignore_public_acls());
1584 if (!s
->cct
->_conf
->rgw_enforce_swift_acls
)
1587 if ((perm
& (int)s
->perm_mask
) != perm
)
1591 if (perm
& (RGW_PERM_READ
| RGW_PERM_READ_ACP
))
1592 swift_perm
|= RGW_PERM_READ_OBJS
;
1593 if (perm
& RGW_PERM_WRITE
)
1594 swift_perm
|= RGW_PERM_WRITE_OBJS
;
1599 /* we already verified the user mask above, so we pass swift_perm as the mask here,
1600 otherwise the mask might not cover the swift permissions bits */
1601 if (bucket_acl
->verify_permission(dpp
, *s
->identity
, swift_perm
, swift_perm
,
1608 return user_acl
->verify_permission(dpp
, *s
->identity
, swift_perm
, swift_perm
);
1611 bool verify_object_permission_no_policy(const DoutPrefixProvider
* dpp
, struct req_state
*s
, int perm
)
1613 perm_state_from_req_state
ps(s
);
1615 if (!verify_requester_payer_permission(&ps
))
1618 return verify_object_permission_no_policy(dpp
,
1621 s
->bucket_acl
.get(),
1622 s
->object_acl
.get(),
1626 bool verify_object_permission(const DoutPrefixProvider
* dpp
, struct req_state
*s
, uint64_t op
)
1628 perm_state_from_req_state
ps(s
);
1630 return verify_object_permission(dpp
,
1632 rgw_obj(s
->bucket
->get_key(), s
->object
->get_key()),
1634 s
->bucket_acl
.get(),
1635 s
->object_acl
.get(),
1637 s
->iam_user_policies
,
1638 s
->session_policies
,
1643 int verify_object_lock(const DoutPrefixProvider
* dpp
, const rgw::sal::Attrs
& attrs
, const bool bypass_perm
, const bool bypass_governance_mode
) {
1644 auto aiter
= attrs
.find(RGW_ATTR_OBJECT_RETENTION
);
1645 if (aiter
!= attrs
.end()) {
1646 RGWObjectRetention obj_retention
;
1648 decode(obj_retention
, aiter
->second
);
1649 } catch (buffer::error
& err
) {
1650 ldpp_dout(dpp
, 0) << "ERROR: failed to decode RGWObjectRetention" << dendl
;
1653 if (ceph::real_clock::to_time_t(obj_retention
.get_retain_until_date()) > ceph_clock_now()) {
1654 if (obj_retention
.get_mode().compare("GOVERNANCE") != 0 || !bypass_perm
|| !bypass_governance_mode
) {
1659 aiter
= attrs
.find(RGW_ATTR_OBJECT_LEGAL_HOLD
);
1660 if (aiter
!= attrs
.end()) {
1661 RGWObjectLegalHold obj_legal_hold
;
1663 decode(obj_legal_hold
, aiter
->second
);
1664 } catch (buffer::error
& err
) {
1665 ldpp_dout(dpp
, 0) << "ERROR: failed to decode RGWObjectLegalHold" << dendl
;
1668 if (obj_legal_hold
.is_enabled()) {
1683 // FIPS zeroization audit 20191115: this memset is not security related.
1684 memset(table
, -1, sizeof(table
));
1686 for (i
= '0'; i
<='9'; i
++)
1688 for (i
= 'A'; i
<='F'; i
++)
1689 table
[i
] = i
- 'A' + 0xa;
1690 for (i
= 'a'; i
<='f'; i
++)
1691 table
[i
] = i
- 'a' + 0xa;
1694 char to_num(char c
) {
1695 return table
[(int)c
];
1699 static char hex_to_num(char c
)
1701 static HexTable hex_table
;
1702 return hex_table
.to_num(c
);
1705 std::string
url_decode(const std::string_view
& src_str
, bool in_query
)
1707 std::string dest_str
;
1708 dest_str
.reserve(src_str
.length() + 1);
1710 for (auto src
= std::begin(src_str
); src
!= std::end(src_str
); ++src
) {
1712 if (!in_query
|| *src
!= '+') {
1716 dest_str
.push_back(*src
);
1718 dest_str
.push_back(' ');
1721 /* 3 == strlen("%%XX") */
1722 if (std::distance(src
, std::end(src_str
)) < 3) {
1727 const char c1
= hex_to_num(*src
++);
1728 const char c2
= hex_to_num(*src
);
1729 if (c1
< 0 || c2
< 0) {
1730 return std::string();
1732 dest_str
.push_back(c1
<< 4 | c2
);
1740 void rgw_uri_escape_char(char c
, string
& dst
)
1743 snprintf(buf
, sizeof(buf
), "%%%.2X", (int)(unsigned char)c
);
1747 static bool char_needs_url_encoding(char c
)
1749 if (c
<= 0x20 || c
>= 0x7f)
1779 void url_encode(const string
& src
, string
& dst
, bool encode_slash
)
1781 const char *p
= src
.c_str();
1782 for (unsigned i
= 0; i
< src
.size(); i
++, p
++) {
1783 if ((!encode_slash
&& *p
== 0x2F) || !char_needs_url_encoding(*p
)) {
1786 rgw_uri_escape_char(*p
, dst
);
1791 std::string
url_encode(const std::string
& src
, bool encode_slash
)
1794 url_encode(src
, dst
, encode_slash
);
1799 std::string
url_remove_prefix(const std::string
& url
)
1801 std::string dst
= url
;
1802 auto pos
= dst
.find("http://");
1803 if (pos
== std::string::npos
) {
1804 pos
= dst
.find("https://");
1805 if (pos
!= std::string::npos
) {
1808 pos
= dst
.find("www.");
1809 if (pos
!= std::string::npos
) {
1820 string
rgw_trim_whitespace(const string
& src
)
1827 for (; start
!= (int)src
.size(); start
++) {
1828 if (!isspace(src
[start
]))
1832 int end
= src
.size() - 1;
1837 for (; end
> start
; end
--) {
1838 if (!isspace(src
[end
]))
1842 return src
.substr(start
, end
- start
+ 1);
1845 std::string_view
rgw_trim_whitespace(const std::string_view
& src
)
1847 std::string_view res
= src
;
1849 while (res
.size() > 0 && std::isspace(res
.front())) {
1850 res
.remove_prefix(1);
1852 while (res
.size() > 0 && std::isspace(res
.back())) {
1853 res
.remove_suffix(1);
1858 string
rgw_trim_quotes(const string
& val
)
1860 string s
= rgw_trim_whitespace(val
);
1865 int end
= s
.size() - 1;
1866 int quotes_count
= 0;
1868 if (s
[start
] == '"') {
1872 if (s
[end
] == '"') {
1876 if (quotes_count
== 2) {
1877 return s
.substr(start
, end
- start
+ 1);
1882 static struct rgw_name_to_flag cap_names
[] = { {"*", RGW_CAP_ALL
},
1883 {"read", RGW_CAP_READ
},
1884 {"write", RGW_CAP_WRITE
},
1887 int RGWUserCaps::parse_cap_perm(const string
& str
, uint32_t *perm
)
1889 return rgw_parse_list_of_flags(cap_names
, str
, perm
);
1892 int RGWUserCaps::get_cap(const string
& cap
, string
& type
, uint32_t *pperm
)
1894 int pos
= cap
.find('=');
1896 type
= rgw_trim_whitespace(cap
.substr(0, pos
));
1899 if (!is_valid_cap_type(type
))
1900 return -ERR_INVALID_CAP
;
1904 if (pos
< (int)cap
.size() - 1) {
1905 cap_perm
= cap
.substr(pos
+ 1);
1906 int r
= RGWUserCaps::parse_cap_perm(cap_perm
, &perm
);
1916 int RGWUserCaps::add_cap(const string
& cap
)
1921 int r
= get_cap(cap
, type
, &perm
);
1930 int RGWUserCaps::remove_cap(const string
& cap
)
1935 int r
= get_cap(cap
, type
, &perm
);
1939 map
<string
, uint32_t>::iterator iter
= caps
.find(type
);
1940 if (iter
== caps
.end())
1943 uint32_t& old_perm
= iter
->second
;
1951 int RGWUserCaps::add_from_string(const string
& str
)
1955 auto end
= str
.find(';', start
);
1956 if (end
== string::npos
)
1959 int r
= add_cap(str
.substr(start
, end
- start
));
1964 } while (start
< (int)str
.size());
1969 int RGWUserCaps::remove_from_string(const string
& str
)
1973 auto end
= str
.find(';', start
);
1974 if (end
== string::npos
)
1977 int r
= remove_cap(str
.substr(start
, end
- start
));
1982 } while (start
< (int)str
.size());
1987 void RGWUserCaps::dump(Formatter
*f
) const
1992 void RGWUserCaps::dump(Formatter
*f
, const char *name
) const
1994 f
->open_array_section(name
);
1995 map
<string
, uint32_t>::const_iterator iter
;
1996 for (iter
= caps
.begin(); iter
!= caps
.end(); ++iter
)
1998 f
->open_object_section("cap");
1999 f
->dump_string("type", iter
->first
);
2000 uint32_t perm
= iter
->second
;
2002 for (int i
=0; cap_names
[i
].type_name
; i
++) {
2003 if ((perm
& cap_names
[i
].flag
) == cap_names
[i
].flag
) {
2004 if (perm_str
.size())
2005 perm_str
.append(", ");
2007 perm_str
.append(cap_names
[i
].type_name
);
2008 perm
&= ~cap_names
[i
].flag
;
2011 if (perm_str
.empty())
2012 perm_str
= "<none>";
2014 f
->dump_string("perm", perm_str
);
2025 void decode_json(JSONObj
*obj
) {
2026 JSONDecoder::decode_json("type", type
, obj
);
2028 JSONDecoder::decode_json("perm", perm_str
, obj
);
2029 if (RGWUserCaps::parse_cap_perm(perm_str
, &perm
) < 0) {
2030 throw JSONDecoder::err("failed to parse permissions");
2035 void RGWUserCaps::decode_json(JSONObj
*obj
)
2037 list
<RGWUserCap
> caps_list
;
2038 decode_json_obj(caps_list
, obj
);
2040 list
<RGWUserCap
>::iterator iter
;
2041 for (iter
= caps_list
.begin(); iter
!= caps_list
.end(); ++iter
) {
2042 RGWUserCap
& cap
= *iter
;
2043 caps
[cap
.type
] = cap
.perm
;
2047 int RGWUserCaps::check_cap(const string
& cap
, uint32_t perm
) const
2049 auto iter
= caps
.find(cap
);
2051 if ((iter
== caps
.end()) ||
2052 (iter
->second
& perm
) != perm
) {
2059 bool RGWUserCaps::is_valid_cap_type(const string
& tp
)
2061 static const char *cap_type
[] = { "user",
2077 for (unsigned int i
= 0; i
< sizeof(cap_type
) / sizeof(char *); ++i
) {
2078 if (tp
.compare(cap_type
[i
]) == 0) {
2086 void rgw_pool::from_str(const string
& s
)
2088 size_t pos
= rgw_unescape_str(s
, 0, '\\', ':', &name
);
2089 if (pos
!= string::npos
) {
2090 pos
= rgw_unescape_str(s
, pos
, '\\', ':', &ns
);
2091 /* ignore return; if pos != string::npos it means that we had a colon
2092 * in the middle of ns that wasn't escaped, we're going to stop there
2097 string
rgw_pool::to_str() const
2100 rgw_escape_str(name
, '\\', ':', &esc_name
);
2105 rgw_escape_str(ns
, '\\', ':', &esc_ns
);
2106 return esc_name
+ ":" + esc_ns
;
2109 void rgw_raw_obj::decode_from_rgw_obj(bufferlist::const_iterator
& bl
)
2113 decode(old_obj
, bl
);
2115 get_obj_bucket_and_oid_loc(old_obj
, oid
, loc
);
2116 pool
= old_obj
.get_explicit_data_pool();
2119 static struct rgw_name_to_flag op_type_mapping
[] = { {"*", RGW_OP_TYPE_ALL
},
2120 {"read", RGW_OP_TYPE_READ
},
2121 {"write", RGW_OP_TYPE_WRITE
},
2122 {"delete", RGW_OP_TYPE_DELETE
},
2126 int rgw_parse_op_type_list(const string
& str
, uint32_t *perm
)
2128 return rgw_parse_list_of_flags(op_type_mapping
, str
, perm
);
2131 bool match_policy(std::string_view pattern
, std::string_view input
,
2134 const uint32_t flag2
= flag
& (MATCH_POLICY_ACTION
|MATCH_POLICY_ARN
) ?
2135 MATCH_CASE_INSENSITIVE
: 0;
2136 const bool colonblocks
= !(flag
& (MATCH_POLICY_RESOURCE
|
2137 MATCH_POLICY_STRING
));
2139 const auto npos
= std::string_view::npos
;
2140 std::string_view::size_type last_pos_input
= 0, last_pos_pattern
= 0;
2142 auto cur_pos_input
= colonblocks
? input
.find(":", last_pos_input
) : npos
;
2143 auto cur_pos_pattern
=
2144 colonblocks
? pattern
.find(":", last_pos_pattern
) : npos
;
2146 auto substr_input
= input
.substr(last_pos_input
, cur_pos_input
);
2147 auto substr_pattern
= pattern
.substr(last_pos_pattern
, cur_pos_pattern
);
2149 if (!match_wildcards(substr_pattern
, substr_input
, flag2
))
2152 if (cur_pos_pattern
== npos
)
2153 return cur_pos_input
== npos
;
2154 if (cur_pos_input
== npos
)
2157 last_pos_pattern
= cur_pos_pattern
+ 1;
2158 last_pos_input
= cur_pos_input
+ 1;
2163 * make attrs look-like-this
2164 * converts underscores to dashes
2166 string
lowercase_dash_http_attr(const string
& orig
)
2168 const char *s
= orig
.c_str();
2169 char buf
[orig
.size() + 1];
2170 buf
[orig
.size()] = '\0';
2172 for (size_t i
= 0; i
< orig
.size(); ++i
, ++s
) {
2178 buf
[i
] = tolower(*s
);
2185 * make attrs Look-Like-This
2186 * converts underscores to dashes
2188 string
camelcase_dash_http_attr(const string
& orig
)
2190 const char *s
= orig
.c_str();
2191 char buf
[orig
.size() + 1];
2192 buf
[orig
.size()] = '\0';
2194 bool last_sep
= true;
2196 for (size_t i
= 0; i
< orig
.size(); ++i
, ++s
) {
2205 buf
[i
] = toupper(*s
);
2207 buf
[i
] = tolower(*s
);
2215 RGWBucketInfo::RGWBucketInfo()
2219 RGWBucketInfo::~RGWBucketInfo()
2223 void RGWBucketInfo::encode(bufferlist
& bl
) const {
2224 ENCODE_START(23, 4, bl
);
2226 encode(owner
.id
, bl
);
2228 encode(zonegroup
, bl
);
2229 uint64_t ct
= real_clock::to_time_t(creation_time
);
2231 encode(placement_rule
, bl
);
2232 encode(has_instance_obj
, bl
);
2234 encode(requester_pays
, bl
);
2235 encode(owner
.tenant
, bl
);
2236 encode(has_website
, bl
);
2238 encode(website_conf
, bl
);
2240 encode(swift_versioning
, bl
);
2241 if (swift_versioning
) {
2242 encode(swift_ver_location
, bl
);
2244 encode(creation_time
, bl
);
2245 encode(mdsearch_config
, bl
);
2246 encode(reshard_status
, bl
);
2247 encode(new_bucket_instance_id
, bl
);
2248 if (obj_lock_enabled()) {
2249 encode(obj_lock
, bl
);
2251 bool has_sync_policy
= !empty_sync_policy();
2252 encode(has_sync_policy
, bl
);
2253 if (has_sync_policy
) {
2254 encode(*sync_policy
, bl
);
2257 encode(owner
.ns
, bl
);
2261 void RGWBucketInfo::decode(bufferlist::const_iterator
& bl
) {
2262 DECODE_START_LEGACY_COMPAT_LEN_32(23, 4, 4, bl
);
2264 if (struct_v
>= 2) {
2272 decode(zonegroup
, bl
);
2273 if (struct_v
>= 6) {
2277 creation_time
= ceph::real_clock::from_time_t((time_t)ct
);
2280 decode(placement_rule
, bl
);
2282 decode(has_instance_obj
, bl
);
2285 static constexpr uint8_t new_layout_v
= 22;
2286 if (struct_v
>= 10 && struct_v
< new_layout_v
)
2287 decode(layout
.current_index
.layout
.normal
.num_shards
, bl
);
2288 if (struct_v
>= 11 && struct_v
< new_layout_v
)
2289 decode(layout
.current_index
.layout
.normal
.hash_type
, bl
);
2291 decode(requester_pays
, bl
);
2293 decode(owner
.tenant
, bl
);
2294 if (struct_v
>= 14) {
2295 decode(has_website
, bl
);
2297 decode(website_conf
, bl
);
2299 website_conf
= RGWBucketWebsiteConf();
2302 if (struct_v
>= 15 && struct_v
< new_layout_v
) {
2305 layout
.current_index
.layout
.type
= (rgw::BucketIndexType
)it
;
2307 layout
.current_index
.layout
.type
= rgw::BucketIndexType::Normal
;
2309 swift_versioning
= false;
2310 swift_ver_location
.clear();
2311 if (struct_v
>= 16) {
2312 decode(swift_versioning
, bl
);
2313 if (swift_versioning
) {
2314 decode(swift_ver_location
, bl
);
2317 if (struct_v
>= 17) {
2318 decode(creation_time
, bl
);
2320 if (struct_v
>= 18) {
2321 decode(mdsearch_config
, bl
);
2323 if (struct_v
>= 19) {
2324 decode(reshard_status
, bl
);
2325 decode(new_bucket_instance_id
, bl
);
2327 if (struct_v
>= 20 && obj_lock_enabled()) {
2328 decode(obj_lock
, bl
);
2330 if (struct_v
>= 21) {
2331 decode(sync_policy
, bl
);
2333 if (struct_v
>= 22) {
2336 if (struct_v
>= 23) {
2337 decode(owner
.ns
, bl
);
2340 if (layout
.logs
.empty() &&
2341 layout
.current_index
.layout
.type
== rgw::BucketIndexType::Normal
) {
2342 layout
.logs
.push_back(rgw::log_layout_from_index(0, layout
.current_index
.layout
.normal
));
2347 void RGWBucketInfo::set_sync_policy(rgw_sync_policy_info
&& policy
)
2349 sync_policy
= std::move(policy
);
2352 bool RGWBucketInfo::empty_sync_policy() const
2358 return sync_policy
->empty();
2362 struct rgw_placement_rule
;
2365 void decode_json_obj(rgw_pool
& pool
, JSONObj
*obj
)
2368 decode_json_obj(s
, obj
);
2372 void encode_json(const char *name
, const rgw_placement_rule
& r
, Formatter
*f
)
2374 encode_json(name
, r
.to_str(), f
);
2377 void encode_json(const char *name
, const rgw_pool
& pool
, Formatter
*f
)
2379 f
->dump_string(name
, pool
.to_str());
2382 void encode_json(const char *name
, const RGWUserCaps
& val
, Formatter
*f
)
2387 void RGWBucketEnt::generate_test_instances(list
<RGWBucketEnt
*>& o
)
2389 RGWBucketEnt
*e
= new RGWBucketEnt
;
2390 init_bucket(&e
->bucket
, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
2392 e
->size_rounded
= 4096;
2395 o
.push_back(new RGWBucketEnt
);
2398 void RGWBucketEnt::dump(Formatter
*f
) const
2400 encode_json("bucket", bucket
, f
);
2401 encode_json("size", size
, f
);
2402 encode_json("size_rounded", size_rounded
, f
);
2403 utime_t
ut(creation_time
);
2404 encode_json("mtime", ut
, f
); /* mtime / creation time discrepency needed for backward compatibility */
2405 encode_json("count", count
, f
);
2406 encode_json("placement_rule", placement_rule
.to_str(), f
);
2409 void rgw_obj::generate_test_instances(list
<rgw_obj
*>& o
)
2412 init_bucket(&b
, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
2413 rgw_obj
*obj
= new rgw_obj(b
, "object");
2415 o
.push_back(new rgw_obj
);
2418 void rgw_bucket_placement::dump(Formatter
*f
) const
2420 encode_json("bucket", bucket
, f
);
2421 encode_json("placement_rule", placement_rule
, f
);
2424 void RGWBucketInfo::generate_test_instances(list
<RGWBucketInfo
*>& o
)
2426 // Since things without a log will have one synthesized on decode,
2427 // ensure the things we attempt to encode will have one added so we
2428 // round-trip properly.
2429 auto gen_layout
= [](rgw::BucketLayout
& l
) {
2430 l
.current_index
.gen
= 0;
2431 l
.current_index
.layout
.normal
.hash_type
= rgw::BucketHashType::Mod
;
2432 l
.current_index
.layout
.type
= rgw::BucketIndexType::Normal
;
2433 l
.current_index
.layout
.normal
.num_shards
= 11;
2434 l
.logs
.push_back(log_layout_from_index(
2435 l
.current_index
.gen
,
2436 l
.current_index
.layout
.normal
));
2440 RGWBucketInfo
*i
= new RGWBucketInfo
;
2441 init_bucket(&i
->bucket
, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
2443 i
->flags
= BUCKET_SUSPENDED
;
2444 gen_layout(i
->layout
);
2446 i
= new RGWBucketInfo
;
2447 gen_layout(i
->layout
);
2451 void RGWBucketInfo::dump(Formatter
*f
) const
2453 encode_json("bucket", bucket
, f
);
2454 utime_t
ut(creation_time
);
2455 encode_json("creation_time", ut
, f
);
2456 encode_json("owner", owner
.to_str(), f
);
2457 encode_json("flags", flags
, f
);
2458 encode_json("zonegroup", zonegroup
, f
);
2459 encode_json("placement_rule", placement_rule
, f
);
2460 encode_json("has_instance_obj", has_instance_obj
, f
);
2461 encode_json("quota", quota
, f
);
2462 encode_json("num_shards", layout
.current_index
.layout
.normal
.num_shards
, f
);
2463 encode_json("bi_shard_hash_type", (uint32_t)layout
.current_index
.layout
.normal
.hash_type
, f
);
2464 encode_json("requester_pays", requester_pays
, f
);
2465 encode_json("has_website", has_website
, f
);
2467 encode_json("website_conf", website_conf
, f
);
2469 encode_json("swift_versioning", swift_versioning
, f
);
2470 encode_json("swift_ver_location", swift_ver_location
, f
);
2471 encode_json("index_type", (uint32_t)layout
.current_index
.layout
.type
, f
);
2472 encode_json("mdsearch_config", mdsearch_config
, f
);
2473 encode_json("reshard_status", (int)reshard_status
, f
);
2474 encode_json("new_bucket_instance_id", new_bucket_instance_id
, f
);
2475 if (!empty_sync_policy()) {
2476 encode_json("sync_policy", *sync_policy
, f
);
2480 void RGWBucketInfo::decode_json(JSONObj
*obj
) {
2481 JSONDecoder::decode_json("bucket", bucket
, obj
);
2483 JSONDecoder::decode_json("creation_time", ut
, obj
);
2484 creation_time
= ut
.to_real_time();
2485 JSONDecoder::decode_json("owner", owner
, obj
);
2486 JSONDecoder::decode_json("flags", flags
, obj
);
2487 JSONDecoder::decode_json("zonegroup", zonegroup
, obj
);
2488 /* backward compatability with region */
2489 if (zonegroup
.empty()) {
2490 JSONDecoder::decode_json("region", zonegroup
, obj
);
2493 JSONDecoder::decode_json("placement_rule", pr
, obj
);
2494 placement_rule
.from_str(pr
);
2495 JSONDecoder::decode_json("has_instance_obj", has_instance_obj
, obj
);
2496 JSONDecoder::decode_json("quota", quota
, obj
);
2497 JSONDecoder::decode_json("num_shards", layout
.current_index
.layout
.normal
.num_shards
, obj
);
2499 JSONDecoder::decode_json("bi_shard_hash_type", hash_type
, obj
);
2500 layout
.current_index
.layout
.normal
.hash_type
= static_cast<rgw::BucketHashType
>(hash_type
);
2501 JSONDecoder::decode_json("requester_pays", requester_pays
, obj
);
2502 JSONDecoder::decode_json("has_website", has_website
, obj
);
2504 JSONDecoder::decode_json("website_conf", website_conf
, obj
);
2506 JSONDecoder::decode_json("swift_versioning", swift_versioning
, obj
);
2507 JSONDecoder::decode_json("swift_ver_location", swift_ver_location
, obj
);
2509 JSONDecoder::decode_json("index_type", it
, obj
);
2510 layout
.current_index
.layout
.type
= (rgw::BucketIndexType
)it
;
2511 JSONDecoder::decode_json("mdsearch_config", mdsearch_config
, obj
);
2513 JSONDecoder::decode_json("reshard_status", rs
, obj
);
2514 reshard_status
= (cls_rgw_reshard_status
)rs
;
2516 rgw_sync_policy_info sp
;
2517 JSONDecoder::decode_json("sync_policy", sp
, obj
);
2519 set_sync_policy(std::move(sp
));
2523 void RGWUserInfo::generate_test_instances(list
<RGWUserInfo
*>& o
)
2525 RGWUserInfo
*i
= new RGWUserInfo
;
2526 i
->user_id
= "user_id";
2527 i
->display_name
= "display_name";
2528 i
->user_email
= "user@email";
2529 RGWAccessKey k1
, k2
;
2533 k2
.subuser
= "subuser";
2537 i
->access_keys
[k1
.id
] = k1
;
2538 i
->swift_keys
[k2
.id
] = k2
;
2539 i
->subusers
[u
.name
] = u
;
2542 o
.push_back(new RGWUserInfo
);
2545 static void user_info_dump_subuser(const char *name
, const RGWSubUser
& subuser
, Formatter
*f
, void *parent
)
2547 RGWUserInfo
*info
= static_cast<RGWUserInfo
*>(parent
);
2548 subuser
.dump(f
, info
->user_id
.to_str());
2551 static void user_info_dump_key(const char *name
, const RGWAccessKey
& key
, Formatter
*f
, void *parent
)
2553 RGWUserInfo
*info
= static_cast<RGWUserInfo
*>(parent
);
2554 key
.dump(f
, info
->user_id
.to_str(), false);
2557 static void user_info_dump_swift_key(const char *name
, const RGWAccessKey
& key
, Formatter
*f
, void *parent
)
2559 RGWUserInfo
*info
= static_cast<RGWUserInfo
*>(parent
);
2560 key
.dump(f
, info
->user_id
.to_str(), true);
2563 static void decode_access_keys(map
<string
, RGWAccessKey
>& m
, JSONObj
*o
)
2570 static void decode_swift_keys(map
<string
, RGWAccessKey
>& m
, JSONObj
*o
)
2573 k
.decode_json(o
, true);
2577 static void decode_subusers(map
<string
, RGWSubUser
>& m
, JSONObj
*o
)
2585 struct rgw_flags_desc
{
2590 static struct rgw_flags_desc rgw_perms
[] = {
2591 { RGW_PERM_FULL_CONTROL
, "full-control" },
2592 { RGW_PERM_READ
| RGW_PERM_WRITE
, "read-write" },
2593 { RGW_PERM_READ
, "read" },
2594 { RGW_PERM_WRITE
, "write" },
2595 { RGW_PERM_READ_ACP
, "read-acp" },
2596 { RGW_PERM_WRITE_ACP
, "write-acp" },
2600 void rgw_perm_to_str(uint32_t mask
, char *buf
, int len
)
2602 const char *sep
= "";
2605 snprintf(buf
, len
, "<none>");
2609 uint32_t orig_mask
= mask
;
2610 for (int i
= 0; rgw_perms
[i
].mask
; i
++) {
2611 struct rgw_flags_desc
*desc
= &rgw_perms
[i
];
2612 if ((mask
& desc
->mask
) == desc
->mask
) {
2613 pos
+= snprintf(buf
+ pos
, len
- pos
, "%s%s", sep
, desc
->str
);
2617 mask
&= ~desc
->mask
;
2622 if (mask
== orig_mask
) // no change
2627 uint32_t rgw_str_to_perm(const char *str
)
2629 if (strcasecmp(str
, "") == 0)
2630 return RGW_PERM_NONE
;
2631 else if (strcasecmp(str
, "read") == 0)
2632 return RGW_PERM_READ
;
2633 else if (strcasecmp(str
, "write") == 0)
2634 return RGW_PERM_WRITE
;
2635 else if (strcasecmp(str
, "readwrite") == 0)
2636 return RGW_PERM_READ
| RGW_PERM_WRITE
;
2637 else if (strcasecmp(str
, "full") == 0)
2638 return RGW_PERM_FULL_CONTROL
;
2640 return RGW_PERM_INVALID
;
2644 static void mask_to_str(T
*mask_list
, uint32_t mask
, char *buf
, int len
)
2646 const char *sep
= "";
2649 snprintf(buf
, len
, "<none>");
2653 uint32_t orig_mask
= mask
;
2654 for (int i
= 0; mask_list
[i
].mask
; i
++) {
2655 T
*desc
= &mask_list
[i
];
2656 if ((mask
& desc
->mask
) == desc
->mask
) {
2657 pos
+= snprintf(buf
+ pos
, len
- pos
, "%s%s", sep
, desc
->str
);
2661 mask
&= ~desc
->mask
;
2666 if (mask
== orig_mask
) // no change
2671 static void perm_to_str(uint32_t mask
, char *buf
, int len
)
2673 return mask_to_str(rgw_perms
, mask
, buf
, len
);
2676 static struct rgw_flags_desc op_type_flags
[] = {
2677 { RGW_OP_TYPE_READ
, "read" },
2678 { RGW_OP_TYPE_WRITE
, "write" },
2679 { RGW_OP_TYPE_DELETE
, "delete" },
2683 void op_type_to_str(uint32_t mask
, char *buf
, int len
)
2685 return mask_to_str(op_type_flags
, mask
, buf
, len
);
2688 void RGWRateLimitInfo::decode_json(JSONObj
*obj
)
2690 JSONDecoder::decode_json("max_read_ops", max_read_ops
, obj
);
2691 JSONDecoder::decode_json("max_write_ops", max_write_ops
, obj
);
2692 JSONDecoder::decode_json("max_read_bytes", max_read_ops
, obj
);
2693 JSONDecoder::decode_json("max_write_bytes", max_write_ops
, obj
);
2694 JSONDecoder::decode_json("enabled", enabled
, obj
);
2697 void RGWRateLimitInfo::dump(Formatter
*f
) const
2699 f
->dump_int("max_read_ops", max_read_ops
);
2700 f
->dump_int("max_write_ops", max_write_ops
);
2701 f
->dump_int("max_read_bytes", max_read_bytes
);
2702 f
->dump_int("max_write_bytes", max_write_bytes
);
2703 f
->dump_bool("enabled", enabled
);
2706 void RGWUserInfo::dump(Formatter
*f
) const
2709 encode_json("user_id", user_id
.to_str(), f
);
2710 encode_json("display_name", display_name
, f
);
2711 encode_json("email", user_email
, f
);
2712 encode_json("suspended", (int)suspended
, f
);
2713 encode_json("max_buckets", (int)max_buckets
, f
);
2715 encode_json_map("subusers", NULL
, "subuser", NULL
, user_info_dump_subuser
,(void *)this, subusers
, f
);
2716 encode_json_map("keys", NULL
, "key", NULL
, user_info_dump_key
,(void *)this, access_keys
, f
);
2717 encode_json_map("swift_keys", NULL
, "key", NULL
, user_info_dump_swift_key
,(void *)this, swift_keys
, f
);
2719 encode_json("caps", caps
, f
);
2722 op_type_to_str(op_mask
, buf
, sizeof(buf
));
2723 encode_json("op_mask", (const char *)buf
, f
);
2725 if (system
) { /* no need to show it for every user */
2726 encode_json("system", (bool)system
, f
);
2729 encode_json("admin", (bool)admin
, f
);
2731 encode_json("default_placement", default_placement
.name
, f
);
2732 encode_json("default_storage_class", default_placement
.storage_class
, f
);
2733 encode_json("placement_tags", placement_tags
, f
);
2734 encode_json("bucket_quota", bucket_quota
, f
);
2735 encode_json("user_quota", user_quota
, f
);
2736 encode_json("temp_url_keys", temp_url_keys
, f
);
2738 string user_source_type
;
2739 switch ((RGWIdentityType
)type
) {
2741 user_source_type
= "rgw";
2744 user_source_type
= "keystone";
2747 user_source_type
= "ldap";
2750 user_source_type
= "none";
2753 user_source_type
= "none";
2756 encode_json("type", user_source_type
, f
);
2757 encode_json("mfa_ids", mfa_ids
, f
);
2760 void RGWUserInfo::decode_json(JSONObj
*obj
)
2764 JSONDecoder::decode_json("user_id", uid
, obj
, true);
2765 user_id
.from_str(uid
);
2767 JSONDecoder::decode_json("display_name", display_name
, obj
);
2768 JSONDecoder::decode_json("email", user_email
, obj
);
2770 JSONDecoder::decode_json("suspended", susp
, obj
);
2771 suspended
= (__u8
)susp
;
2772 JSONDecoder::decode_json("max_buckets", max_buckets
, obj
);
2774 JSONDecoder::decode_json("keys", access_keys
, decode_access_keys
, obj
);
2775 JSONDecoder::decode_json("swift_keys", swift_keys
, decode_swift_keys
, obj
);
2776 JSONDecoder::decode_json("subusers", subusers
, decode_subusers
, obj
);
2778 JSONDecoder::decode_json("caps", caps
, obj
);
2781 JSONDecoder::decode_json("op_mask", mask_str
, obj
);
2782 rgw_parse_op_type_list(mask_str
, &op_mask
);
2785 JSONDecoder::decode_json("system", sys
, obj
);
2788 JSONDecoder::decode_json("admin", ad
, obj
);
2790 JSONDecoder::decode_json("default_placement", default_placement
.name
, obj
);
2791 JSONDecoder::decode_json("default_storage_class", default_placement
.storage_class
, obj
);
2792 JSONDecoder::decode_json("placement_tags", placement_tags
, obj
);
2793 JSONDecoder::decode_json("bucket_quota", bucket_quota
, obj
);
2794 JSONDecoder::decode_json("user_quota", user_quota
, obj
);
2795 JSONDecoder::decode_json("temp_url_keys", temp_url_keys
, obj
);
2797 string user_source_type
;
2798 JSONDecoder::decode_json("type", user_source_type
, obj
);
2799 if (user_source_type
== "rgw") {
2801 } else if (user_source_type
== "keystone") {
2802 type
= TYPE_KEYSTONE
;
2803 } else if (user_source_type
== "ldap") {
2805 } else if (user_source_type
== "none") {
2808 JSONDecoder::decode_json("mfa_ids", mfa_ids
, obj
);
2812 void RGWSubUser::generate_test_instances(list
<RGWSubUser
*>& o
)
2814 RGWSubUser
*u
= new RGWSubUser
;
2818 o
.push_back(new RGWSubUser
);
2821 void RGWSubUser::dump(Formatter
*f
) const
2823 encode_json("id", name
, f
);
2825 perm_to_str(perm_mask
, buf
, sizeof(buf
));
2826 encode_json("permissions", (const char *)buf
, f
);
2829 void RGWSubUser::dump(Formatter
*f
, const string
& user
) const
2834 encode_json("id", s
, f
);
2836 perm_to_str(perm_mask
, buf
, sizeof(buf
));
2837 encode_json("permissions", (const char *)buf
, f
);
2840 uint32_t str_to_perm(const string
& s
)
2842 if (s
.compare("read") == 0)
2843 return RGW_PERM_READ
;
2844 else if (s
.compare("write") == 0)
2845 return RGW_PERM_WRITE
;
2846 else if (s
.compare("read-write") == 0)
2847 return RGW_PERM_READ
| RGW_PERM_WRITE
;
2848 else if (s
.compare("full-control") == 0)
2849 return RGW_PERM_FULL_CONTROL
;
2853 void RGWSubUser::decode_json(JSONObj
*obj
)
2856 JSONDecoder::decode_json("id", uid
, obj
);
2857 int pos
= uid
.find(':');
2859 name
= uid
.substr(pos
+ 1);
2861 JSONDecoder::decode_json("permissions", perm_str
, obj
);
2862 perm_mask
= str_to_perm(perm_str
);
2865 void RGWAccessKey::generate_test_instances(list
<RGWAccessKey
*>& o
)
2867 RGWAccessKey
*k
= new RGWAccessKey
;
2870 k
->subuser
= "subuser";
2872 o
.push_back(new RGWAccessKey
);
2875 void RGWAccessKey::dump(Formatter
*f
) const
2877 encode_json("access_key", id
, f
);
2878 encode_json("secret_key", key
, f
);
2879 encode_json("subuser", subuser
, f
);
2882 void RGWAccessKey::dump_plain(Formatter
*f
) const
2884 encode_json("access_key", id
, f
);
2885 encode_json("secret_key", key
, f
);
2888 void RGWAccessKey::dump(Formatter
*f
, const string
& user
, bool swift
) const
2891 if (!subuser
.empty()) {
2895 encode_json("user", u
, f
);
2897 encode_json("access_key", id
, f
);
2899 encode_json("secret_key", key
, f
);
2902 void RGWAccessKey::decode_json(JSONObj
*obj
) {
2903 JSONDecoder::decode_json("access_key", id
, obj
, true);
2904 JSONDecoder::decode_json("secret_key", key
, obj
, true);
2905 if (!JSONDecoder::decode_json("subuser", subuser
, obj
)) {
2907 JSONDecoder::decode_json("user", user
, obj
);
2908 int pos
= user
.find(':');
2910 subuser
= user
.substr(pos
+ 1);
2915 void RGWAccessKey::decode_json(JSONObj
*obj
, bool swift
) {
2921 if (!JSONDecoder::decode_json("subuser", subuser
, obj
)) {
2922 JSONDecoder::decode_json("user", id
, obj
, true);
2923 int pos
= id
.find(':');
2925 subuser
= id
.substr(pos
+ 1);
2928 JSONDecoder::decode_json("secret_key", key
, obj
, true);
2931 void RGWStorageStats::dump(Formatter
*f
) const
2933 encode_json("size", size
, f
);
2934 encode_json("size_actual", size_rounded
, f
);
2935 if (dump_utilized
) {
2936 encode_json("size_utilized", size_utilized
, f
);
2938 encode_json("size_kb", rgw_rounded_kb(size
), f
);
2939 encode_json("size_kb_actual", rgw_rounded_kb(size_rounded
), f
);
2940 if (dump_utilized
) {
2941 encode_json("size_kb_utilized", rgw_rounded_kb(size_utilized
), f
);
2943 encode_json("num_objects", num_objects
, f
);
2946 void rgw_obj_key::dump(Formatter
*f
) const
2948 encode_json("name", name
, f
);
2949 encode_json("instance", instance
, f
);
2950 encode_json("ns", ns
, f
);
2953 void rgw_obj_key::decode_json(JSONObj
*obj
)
2955 JSONDecoder::decode_json("name", name
, obj
);
2956 JSONDecoder::decode_json("instance", instance
, obj
);
2957 JSONDecoder::decode_json("ns", ns
, obj
);
2960 void rgw_raw_obj::dump(Formatter
*f
) const
2962 encode_json("pool", pool
, f
);
2963 encode_json("oid", oid
, f
);
2964 encode_json("loc", loc
, f
);
2967 void rgw_raw_obj::decode_json(JSONObj
*obj
) {
2968 JSONDecoder::decode_json("pool", pool
, obj
);
2969 JSONDecoder::decode_json("oid", oid
, obj
);
2970 JSONDecoder::decode_json("loc", loc
, obj
);
2973 void rgw_obj::dump(Formatter
*f
) const
2975 encode_json("bucket", bucket
, f
);
2976 encode_json("key", key
, f
);