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::append(const string
& name
, const string
& val
)
873 if (name
.compare(0, sizeof(RGW_SYS_PARAM_PREFIX
) - 1, RGW_SYS_PARAM_PREFIX
) == 0) {
874 sys_val_map
[name
] = val
;
879 // when sub_resources exclusive by object are added, please remember to update obj_sub_resource in RGWHTTPArgs::exist_obj_excl_sub_resource().
880 if ((name
.compare("acl") == 0) ||
881 (name
.compare("cors") == 0) ||
882 (name
.compare("notification") == 0) ||
883 (name
.compare("location") == 0) ||
884 (name
.compare("logging") == 0) ||
885 (name
.compare("usage") == 0) ||
886 (name
.compare("lifecycle") == 0) ||
887 (name
.compare("delete") == 0) ||
888 (name
.compare("uploads") == 0) ||
889 (name
.compare("partNumber") == 0) ||
890 (name
.compare("uploadId") == 0) ||
891 (name
.compare("versionId") == 0) ||
892 (name
.compare("start-date") == 0) ||
893 (name
.compare("end-date") == 0) ||
894 (name
.compare("versions") == 0) ||
895 (name
.compare("versioning") == 0) ||
896 (name
.compare("website") == 0) ||
897 (name
.compare("requestPayment") == 0) ||
898 (name
.compare("torrent") == 0) ||
899 (name
.compare("tagging") == 0) ||
900 (name
.compare("append") == 0) ||
901 (name
.compare("position") == 0) ||
902 (name
.compare("policyStatus") == 0) ||
903 (name
.compare("publicAccessBlock") == 0)) {
904 sub_resources
[name
] = val
;
905 } else if (name
[0] == 'r') { // root of all evil
906 if ((name
.compare("response-content-type") == 0) ||
907 (name
.compare("response-content-language") == 0) ||
908 (name
.compare("response-expires") == 0) ||
909 (name
.compare("response-cache-control") == 0) ||
910 (name
.compare("response-content-disposition") == 0) ||
911 (name
.compare("response-content-encoding") == 0)) {
912 sub_resources
[name
] = val
;
913 has_resp_modifier
= true;
915 } else if ((name
.compare("subuser") == 0) ||
916 (name
.compare("key") == 0) ||
917 (name
.compare("caps") == 0) ||
918 (name
.compare("index") == 0) ||
919 (name
.compare("policy") == 0) ||
920 (name
.compare("quota") == 0) ||
921 (name
.compare("list") == 0) ||
922 (name
.compare("object") == 0) ||
923 (name
.compare("sync") == 0)) {
924 if (!admin_subresource_added
) {
925 sub_resources
[name
] = "";
926 admin_subresource_added
= true;
931 const string
& RGWHTTPArgs::get(const string
& name
, bool *exists
) const
933 auto iter
= val_map
.find(name
);
934 bool e
= (iter
!= std::end(val_map
));
942 boost::optional
<const std::string
&>
943 RGWHTTPArgs::get_optional(const std::string
& name
) const
946 const std::string
& value
= get(name
, &exists
);
954 int RGWHTTPArgs::get_bool(const string
& name
, bool *val
, bool *exists
) const
956 map
<string
, string
>::const_iterator iter
;
957 iter
= val_map
.find(name
);
958 bool e
= (iter
!= val_map
.end());
963 const char *s
= iter
->second
.c_str();
965 if (strcasecmp(s
, "false") == 0) {
967 } else if (strcasecmp(s
, "true") == 0) {
977 int RGWHTTPArgs::get_bool(const char *name
, bool *val
, bool *exists
) const
980 return get_bool(s
, val
, exists
);
983 void RGWHTTPArgs::get_bool(const char *name
, bool *val
, bool def_val
) const
986 if ((get_bool(name
, val
, &exists
) < 0) ||
992 int RGWHTTPArgs::get_int(const char *name
, int *val
, int def_val
) const
996 val_str
= get(name
, &exists
);
1004 *val
= (int)strict_strtol(val_str
.c_str(), 10, &err
);
1012 string
RGWHTTPArgs::sys_get(const string
& name
, bool * const exists
) const
1014 const auto iter
= sys_val_map
.find(name
);
1015 const bool e
= (iter
!= sys_val_map
.end());
1021 return e
? iter
->second
: string();
1024 bool rgw_transport_is_secure(CephContext
*cct
, const RGWEnv
& env
)
1026 const auto& m
= env
.get_map();
1027 // frontend connected with ssl
1028 if (m
.count("SERVER_PORT_SECURE")) {
1031 // ignore proxy headers unless explicitly enabled
1032 if (!cct
->_conf
->rgw_trust_forwarded_https
) {
1035 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
1036 // Forwarded: by=<identifier>; for=<identifier>; host=<host>; proto=<http|https>
1037 auto i
= m
.find("HTTP_FORWARDED");
1038 if (i
!= m
.end() && i
->second
.find("proto=https") != std::string::npos
) {
1041 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-Proto
1042 i
= m
.find("HTTP_X_FORWARDED_PROTO");
1043 if (i
!= m
.end() && i
->second
== "https") {
1052 struct perm_state_from_req_state
: public perm_state_base
{
1053 req_state
* const s
;
1054 perm_state_from_req_state(req_state
* const _s
)
1055 : perm_state_base(_s
->cct
,
1057 _s
->auth
.identity
.get(),
1058 _s
->bucket
.get() ? _s
->bucket
->get_info() : RGWBucketInfo(),
1060 _s
->defer_to_bucket_acls
,
1061 _s
->bucket_access_conf
),
1064 std::optional
<bool> get_request_payer() const override
{
1065 const char *request_payer
= s
->info
.env
->get("HTTP_X_AMZ_REQUEST_PAYER");
1066 if (!request_payer
) {
1068 request_payer
= s
->info
.args
.get("x-amz-request-payer", &exists
).c_str();
1074 if (strcasecmp(request_payer
, "requester") == 0) {
1078 return std::nullopt
;
1081 const char *get_referer() const override
{
1082 return s
->info
.env
->get("HTTP_REFERER");
1086 Effect
eval_or_pass(const boost::optional
<Policy
>& policy
,
1087 const rgw::IAM::Environment
& env
,
1088 boost::optional
<const rgw::auth::Identity
&> id
,
1090 const ARN
& resource
,
1091 boost::optional
<rgw::IAM::PolicyPrincipal
&> princ_type
=boost::none
) {
1093 return Effect::Pass
;
1095 return policy
->eval(env
, id
, op
, resource
, princ_type
);
1100 Effect
eval_identity_or_session_policies(const vector
<Policy
>& policies
,
1101 const rgw::IAM::Environment
& env
,
1104 auto policy_res
= Effect::Pass
, prev_res
= Effect::Pass
;
1105 for (auto& policy
: policies
) {
1106 if (policy_res
= eval_or_pass(policy
, env
, boost::none
, op
, arn
); policy_res
== Effect::Deny
)
1108 else if (policy_res
== Effect::Allow
)
1109 prev_res
= Effect::Allow
;
1110 else if (policy_res
== Effect::Pass
&& prev_res
== Effect::Allow
)
1111 policy_res
= Effect::Allow
;
1116 bool verify_user_permission(const DoutPrefixProvider
* dpp
,
1117 perm_state_base
* const s
,
1118 RGWAccessControlPolicy
* const user_acl
,
1119 const vector
<rgw::IAM::Policy
>& user_policies
,
1120 const vector
<rgw::IAM::Policy
>& session_policies
,
1121 const rgw::ARN
& res
,
1124 auto identity_policy_res
= eval_identity_or_session_policies(user_policies
, s
->env
, op
, res
);
1125 if (identity_policy_res
== Effect::Deny
) {
1129 if (! session_policies
.empty()) {
1130 auto session_policy_res
= eval_identity_or_session_policies(session_policies
, s
->env
, op
, res
);
1131 if (session_policy_res
== Effect::Deny
) {
1134 //Intersection of identity policies and session policies
1135 if (identity_policy_res
== Effect::Allow
&& session_policy_res
== Effect::Allow
) {
1141 if (identity_policy_res
== Effect::Allow
) {
1145 if (op
== rgw::IAM::s3CreateBucket
|| op
== rgw::IAM::s3ListAllMyBuckets
) {
1146 auto perm
= op_to_perm(op
);
1148 return verify_user_permission_no_policy(dpp
, s
, user_acl
, perm
);
1154 bool verify_user_permission_no_policy(const DoutPrefixProvider
* dpp
,
1155 struct perm_state_base
* const s
,
1156 RGWAccessControlPolicy
* const user_acl
,
1159 if (s
->identity
->get_identity_type() == TYPE_ROLE
)
1162 /* S3 doesn't support account ACLs. */
1166 if ((perm
& (int)s
->perm_mask
) != perm
)
1169 return user_acl
->verify_permission(dpp
, *s
->identity
, perm
, perm
);
1172 bool verify_user_permission(const DoutPrefixProvider
* dpp
,
1173 struct req_state
* const s
,
1174 const rgw::ARN
& res
,
1177 perm_state_from_req_state
ps(s
);
1178 return verify_user_permission(dpp
, &ps
, s
->user_acl
.get(), s
->iam_user_policies
, s
->session_policies
, res
, op
);
1181 bool verify_user_permission_no_policy(const DoutPrefixProvider
* dpp
,
1182 struct req_state
* const s
,
1185 perm_state_from_req_state
ps(s
);
1186 return verify_user_permission_no_policy(dpp
, &ps
, s
->user_acl
.get(), perm
);
1189 bool verify_requester_payer_permission(struct perm_state_base
*s
)
1191 if (!s
->bucket_info
.requester_pays
)
1194 if (s
->identity
->is_owner_of(s
->bucket_info
.owner
))
1197 if (s
->identity
->is_anonymous()) {
1201 auto request_payer
= s
->get_request_payer();
1202 if (request_payer
) {
1203 return *request_payer
;
1209 bool verify_bucket_permission(const DoutPrefixProvider
* dpp
,
1210 struct perm_state_base
* const s
,
1211 const rgw_bucket
& bucket
,
1212 RGWAccessControlPolicy
* const user_acl
,
1213 RGWAccessControlPolicy
* const bucket_acl
,
1214 const boost::optional
<Policy
>& bucket_policy
,
1215 const vector
<Policy
>& identity_policies
,
1216 const vector
<Policy
>& session_policies
,
1219 if (!verify_requester_payer_permission(s
))
1222 auto identity_policy_res
= eval_identity_or_session_policies(identity_policies
, s
->env
, op
, ARN(bucket
));
1223 if (identity_policy_res
== Effect::Deny
)
1226 rgw::IAM::PolicyPrincipal princ_type
= rgw::IAM::PolicyPrincipal::Other
;
1227 auto r
= eval_or_pass(bucket_policy
, s
->env
, *s
->identity
,
1228 op
, ARN(bucket
), princ_type
);
1229 if (r
== Effect::Deny
)
1232 //Take into account session policies, if the identity making a request is a role
1233 if (!session_policies
.empty()) {
1234 auto session_policy_res
= eval_identity_or_session_policies(session_policies
, s
->env
, op
, ARN(bucket
));
1235 if (session_policy_res
== Effect::Deny
) {
1238 if (princ_type
== rgw::IAM::PolicyPrincipal::Role
) {
1239 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
1240 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) ||
1241 (session_policy_res
== Effect::Allow
&& r
== Effect::Allow
))
1243 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Session
) {
1244 //Intersection of session policy and identity policy plus bucket policy
1245 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) || r
== Effect::Allow
)
1247 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Other
) {// there was no match in the bucket policy
1248 if (session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
)
1254 if (r
== Effect::Allow
|| identity_policy_res
== Effect::Allow
)
1255 // It looks like S3 ACLs only GRANT permissions rather than
1256 // denying them, so this should be safe.
1259 const auto perm
= op_to_perm(op
);
1261 return verify_bucket_permission_no_policy(dpp
, s
, user_acl
, bucket_acl
, perm
);
1264 bool verify_bucket_permission(const DoutPrefixProvider
* dpp
,
1265 struct req_state
* const s
,
1266 const rgw_bucket
& bucket
,
1267 RGWAccessControlPolicy
* const user_acl
,
1268 RGWAccessControlPolicy
* const bucket_acl
,
1269 const boost::optional
<Policy
>& bucket_policy
,
1270 const vector
<Policy
>& user_policies
,
1271 const vector
<Policy
>& session_policies
,
1274 perm_state_from_req_state
ps(s
);
1275 return verify_bucket_permission(dpp
, &ps
, bucket
,
1276 user_acl
, bucket_acl
,
1277 bucket_policy
, user_policies
,
1278 session_policies
, op
);
1281 bool verify_bucket_permission_no_policy(const DoutPrefixProvider
* dpp
, struct perm_state_base
* const s
,
1282 RGWAccessControlPolicy
* const user_acl
,
1283 RGWAccessControlPolicy
* const bucket_acl
,
1289 if ((perm
& (int)s
->perm_mask
) != perm
)
1292 if (bucket_acl
->verify_permission(dpp
, *s
->identity
, perm
, perm
,
1294 s
->bucket_access_conf
&&
1295 s
->bucket_access_conf
->ignore_public_acls()))
1301 return user_acl
->verify_permission(dpp
, *s
->identity
, perm
, perm
);
1304 bool verify_bucket_permission_no_policy(const DoutPrefixProvider
* dpp
, struct req_state
* const s
,
1305 RGWAccessControlPolicy
* const user_acl
,
1306 RGWAccessControlPolicy
* const bucket_acl
,
1309 perm_state_from_req_state
ps(s
);
1310 return verify_bucket_permission_no_policy(dpp
,
1317 bool verify_bucket_permission_no_policy(const DoutPrefixProvider
* dpp
, struct req_state
* const s
, const int perm
)
1319 perm_state_from_req_state
ps(s
);
1321 if (!verify_requester_payer_permission(&ps
))
1324 return verify_bucket_permission_no_policy(dpp
,
1327 s
->bucket_acl
.get(),
1331 bool verify_bucket_permission(const DoutPrefixProvider
* dpp
, struct req_state
* const s
, const uint64_t op
)
1333 if (rgw::sal::Bucket::empty(s
->bucket
)) {
1334 // request is missing a bucket name
1338 perm_state_from_req_state
ps(s
);
1340 return verify_bucket_permission(dpp
,
1342 s
->bucket
->get_key(),
1344 s
->bucket_acl
.get(),
1346 s
->iam_user_policies
,
1347 s
->session_policies
,
1351 // Authorize anyone permitted by the bucket policy, identity policies, session policies and the bucket owner
1352 // unless explicitly denied by the policy.
1354 int verify_bucket_owner_or_policy(struct req_state
* const s
,
1357 auto identity_policy_res
= eval_identity_or_session_policies(s
->iam_user_policies
, s
->env
, op
, ARN(s
->bucket
->get_key()));
1358 if (identity_policy_res
== Effect::Deny
) {
1362 rgw::IAM::PolicyPrincipal princ_type
= rgw::IAM::PolicyPrincipal::Other
;
1363 auto e
= eval_or_pass(s
->iam_policy
,
1364 s
->env
, *s
->auth
.identity
,
1365 op
, ARN(s
->bucket
->get_key()), princ_type
);
1366 if (e
== Effect::Deny
) {
1370 if (!s
->session_policies
.empty()) {
1371 auto session_policy_res
= eval_identity_or_session_policies(s
->session_policies
, s
->env
, op
, ARN(s
->bucket
->get_key()));
1372 if (session_policy_res
== Effect::Deny
) {
1375 if (princ_type
== rgw::IAM::PolicyPrincipal::Role
) {
1376 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
1377 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) ||
1378 (session_policy_res
== Effect::Allow
&& e
== Effect::Allow
))
1380 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Session
) {
1381 //Intersection of session policy and identity policy plus bucket policy
1382 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) || e
== Effect::Allow
)
1384 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Other
) {// there was no match in the bucket policy
1385 if (session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
)
1391 if (e
== Effect::Allow
||
1392 identity_policy_res
== Effect::Allow
||
1393 (e
== Effect::Pass
&&
1394 identity_policy_res
== Effect::Pass
&&
1395 s
->auth
.identity
->is_owner_of(s
->bucket_owner
.get_id()))) {
1403 static inline bool check_deferred_bucket_perms(const DoutPrefixProvider
* dpp
,
1404 struct perm_state_base
* const s
,
1405 const rgw_bucket
& bucket
,
1406 RGWAccessControlPolicy
* const user_acl
,
1407 RGWAccessControlPolicy
* const bucket_acl
,
1408 const boost::optional
<Policy
>& bucket_policy
,
1409 const vector
<Policy
>& identity_policies
,
1410 const vector
<Policy
>& session_policies
,
1411 const uint8_t deferred_check
,
1414 return (s
->defer_to_bucket_acls
== deferred_check \
1415 && verify_bucket_permission(dpp
, s
, bucket
, user_acl
, bucket_acl
, bucket_policy
, identity_policies
, session_policies
,op
));
1418 static inline bool check_deferred_bucket_only_acl(const DoutPrefixProvider
* dpp
,
1419 struct perm_state_base
* const s
,
1420 RGWAccessControlPolicy
* const user_acl
,
1421 RGWAccessControlPolicy
* const bucket_acl
,
1422 const uint8_t deferred_check
,
1425 return (s
->defer_to_bucket_acls
== deferred_check \
1426 && verify_bucket_permission_no_policy(dpp
, s
, user_acl
, bucket_acl
, perm
));
1429 bool verify_object_permission(const DoutPrefixProvider
* dpp
, struct perm_state_base
* const s
,
1431 RGWAccessControlPolicy
* const user_acl
,
1432 RGWAccessControlPolicy
* const bucket_acl
,
1433 RGWAccessControlPolicy
* const object_acl
,
1434 const boost::optional
<Policy
>& bucket_policy
,
1435 const vector
<Policy
>& identity_policies
,
1436 const vector
<Policy
>& session_policies
,
1439 if (!verify_requester_payer_permission(s
))
1442 auto identity_policy_res
= eval_identity_or_session_policies(identity_policies
, s
->env
, op
, ARN(obj
));
1443 if (identity_policy_res
== Effect::Deny
)
1446 rgw::IAM::PolicyPrincipal princ_type
= rgw::IAM::PolicyPrincipal::Other
;
1447 auto r
= eval_or_pass(bucket_policy
, s
->env
, *s
->identity
, op
, ARN(obj
), princ_type
);
1448 if (r
== Effect::Deny
)
1451 if (!session_policies
.empty()) {
1452 auto session_policy_res
= eval_identity_or_session_policies(session_policies
, s
->env
, op
, ARN(obj
));
1453 if (session_policy_res
== Effect::Deny
) {
1456 if (princ_type
== rgw::IAM::PolicyPrincipal::Role
) {
1457 //Intersection of session policy and identity policy plus intersection of session policy and bucket policy
1458 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) ||
1459 (session_policy_res
== Effect::Allow
&& r
== Effect::Allow
))
1461 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Session
) {
1462 //Intersection of session policy and identity policy plus bucket policy
1463 if ((session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
) || r
== Effect::Allow
)
1465 } else if (princ_type
== rgw::IAM::PolicyPrincipal::Other
) {// there was no match in the bucket policy
1466 if (session_policy_res
== Effect::Allow
&& identity_policy_res
== Effect::Allow
)
1472 if (r
== Effect::Allow
|| identity_policy_res
== Effect::Allow
)
1473 // It looks like S3 ACLs only GRANT permissions rather than
1474 // denying them, so this should be safe.
1477 const auto perm
= op_to_perm(op
);
1479 if (check_deferred_bucket_perms(dpp
, s
, obj
.bucket
, user_acl
, bucket_acl
, bucket_policy
,
1480 identity_policies
, session_policies
, RGW_DEFER_TO_BUCKET_ACLS_RECURSE
, op
) ||
1481 check_deferred_bucket_perms(dpp
, s
, obj
.bucket
, user_acl
, bucket_acl
, bucket_policy
,
1482 identity_policies
, session_policies
, RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL
, rgw::IAM::s3All
)) {
1490 bool ret
= object_acl
->verify_permission(dpp
, *s
->identity
, s
->perm_mask
, perm
,
1491 nullptr, /* http_referrer */
1492 s
->bucket_access_conf
&&
1493 s
->bucket_access_conf
->ignore_public_acls());
1498 if (!s
->cct
->_conf
->rgw_enforce_swift_acls
)
1501 if ((perm
& (int)s
->perm_mask
) != perm
)
1505 if (perm
& (RGW_PERM_READ
| RGW_PERM_READ_ACP
))
1506 swift_perm
|= RGW_PERM_READ_OBJS
;
1507 if (perm
& RGW_PERM_WRITE
)
1508 swift_perm
|= RGW_PERM_WRITE_OBJS
;
1513 /* we already verified the user mask above, so we pass swift_perm as the mask here,
1514 otherwise the mask might not cover the swift permissions bits */
1515 if (bucket_acl
->verify_permission(dpp
, *s
->identity
, swift_perm
, swift_perm
,
1522 return user_acl
->verify_permission(dpp
, *s
->identity
, swift_perm
, swift_perm
);
1525 bool verify_object_permission(const DoutPrefixProvider
* dpp
, struct req_state
* const s
,
1527 RGWAccessControlPolicy
* const user_acl
,
1528 RGWAccessControlPolicy
* const bucket_acl
,
1529 RGWAccessControlPolicy
* const object_acl
,
1530 const boost::optional
<Policy
>& bucket_policy
,
1531 const vector
<Policy
>& identity_policies
,
1532 const vector
<Policy
>& session_policies
,
1535 perm_state_from_req_state
ps(s
);
1536 return verify_object_permission(dpp
, &ps
, obj
,
1537 user_acl
, bucket_acl
,
1538 object_acl
, bucket_policy
,
1539 identity_policies
, session_policies
, op
);
1542 bool verify_object_permission_no_policy(const DoutPrefixProvider
* dpp
,
1543 struct perm_state_base
* const s
,
1544 RGWAccessControlPolicy
* const user_acl
,
1545 RGWAccessControlPolicy
* const bucket_acl
,
1546 RGWAccessControlPolicy
* const object_acl
,
1549 if (check_deferred_bucket_only_acl(dpp
, s
, user_acl
, bucket_acl
, RGW_DEFER_TO_BUCKET_ACLS_RECURSE
, perm
) ||
1550 check_deferred_bucket_only_acl(dpp
, s
, user_acl
, bucket_acl
, RGW_DEFER_TO_BUCKET_ACLS_FULL_CONTROL
, RGW_PERM_FULL_CONTROL
)) {
1558 bool ret
= object_acl
->verify_permission(dpp
, *s
->identity
, s
->perm_mask
, perm
,
1559 nullptr, /* http referrer */
1560 s
->bucket_access_conf
&&
1561 s
->bucket_access_conf
->ignore_public_acls());
1566 if (!s
->cct
->_conf
->rgw_enforce_swift_acls
)
1569 if ((perm
& (int)s
->perm_mask
) != perm
)
1573 if (perm
& (RGW_PERM_READ
| RGW_PERM_READ_ACP
))
1574 swift_perm
|= RGW_PERM_READ_OBJS
;
1575 if (perm
& RGW_PERM_WRITE
)
1576 swift_perm
|= RGW_PERM_WRITE_OBJS
;
1581 /* we already verified the user mask above, so we pass swift_perm as the mask here,
1582 otherwise the mask might not cover the swift permissions bits */
1583 if (bucket_acl
->verify_permission(dpp
, *s
->identity
, swift_perm
, swift_perm
,
1590 return user_acl
->verify_permission(dpp
, *s
->identity
, swift_perm
, swift_perm
);
1593 bool verify_object_permission_no_policy(const DoutPrefixProvider
* dpp
, struct req_state
*s
, int perm
)
1595 perm_state_from_req_state
ps(s
);
1597 if (!verify_requester_payer_permission(&ps
))
1600 return verify_object_permission_no_policy(dpp
,
1603 s
->bucket_acl
.get(),
1604 s
->object_acl
.get(),
1608 bool verify_object_permission(const DoutPrefixProvider
* dpp
, struct req_state
*s
, uint64_t op
)
1610 perm_state_from_req_state
ps(s
);
1612 return verify_object_permission(dpp
,
1614 rgw_obj(s
->bucket
->get_key(), s
->object
->get_key()),
1616 s
->bucket_acl
.get(),
1617 s
->object_acl
.get(),
1619 s
->iam_user_policies
,
1620 s
->session_policies
,
1625 int verify_object_lock(const DoutPrefixProvider
* dpp
, const rgw::sal::Attrs
& attrs
, const bool bypass_perm
, const bool bypass_governance_mode
) {
1626 auto aiter
= attrs
.find(RGW_ATTR_OBJECT_RETENTION
);
1627 if (aiter
!= attrs
.end()) {
1628 RGWObjectRetention obj_retention
;
1630 decode(obj_retention
, aiter
->second
);
1631 } catch (buffer::error
& err
) {
1632 ldpp_dout(dpp
, 0) << "ERROR: failed to decode RGWObjectRetention" << dendl
;
1635 if (ceph::real_clock::to_time_t(obj_retention
.get_retain_until_date()) > ceph_clock_now()) {
1636 if (obj_retention
.get_mode().compare("GOVERNANCE") != 0 || !bypass_perm
|| !bypass_governance_mode
) {
1641 aiter
= attrs
.find(RGW_ATTR_OBJECT_LEGAL_HOLD
);
1642 if (aiter
!= attrs
.end()) {
1643 RGWObjectLegalHold obj_legal_hold
;
1645 decode(obj_legal_hold
, aiter
->second
);
1646 } catch (buffer::error
& err
) {
1647 ldpp_dout(dpp
, 0) << "ERROR: failed to decode RGWObjectLegalHold" << dendl
;
1650 if (obj_legal_hold
.is_enabled()) {
1665 // FIPS zeroization audit 20191115: this memset is not security related.
1666 memset(table
, -1, sizeof(table
));
1668 for (i
= '0'; i
<='9'; i
++)
1670 for (i
= 'A'; i
<='F'; i
++)
1671 table
[i
] = i
- 'A' + 0xa;
1672 for (i
= 'a'; i
<='f'; i
++)
1673 table
[i
] = i
- 'a' + 0xa;
1676 char to_num(char c
) {
1677 return table
[(int)c
];
1681 static char hex_to_num(char c
)
1683 static HexTable hex_table
;
1684 return hex_table
.to_num(c
);
1687 std::string
url_decode(const std::string_view
& src_str
, bool in_query
)
1689 std::string dest_str
;
1690 dest_str
.reserve(src_str
.length() + 1);
1692 for (auto src
= std::begin(src_str
); src
!= std::end(src_str
); ++src
) {
1694 if (!in_query
|| *src
!= '+') {
1698 dest_str
.push_back(*src
);
1700 dest_str
.push_back(' ');
1703 /* 3 == strlen("%%XX") */
1704 if (std::distance(src
, std::end(src_str
)) < 3) {
1709 const char c1
= hex_to_num(*src
++);
1710 const char c2
= hex_to_num(*src
);
1711 if (c1
< 0 || c2
< 0) {
1712 return std::string();
1714 dest_str
.push_back(c1
<< 4 | c2
);
1722 void rgw_uri_escape_char(char c
, string
& dst
)
1725 snprintf(buf
, sizeof(buf
), "%%%.2X", (int)(unsigned char)c
);
1729 static bool char_needs_url_encoding(char c
)
1731 if (c
<= 0x20 || c
>= 0x7f)
1761 void url_encode(const string
& src
, string
& dst
, bool encode_slash
)
1763 const char *p
= src
.c_str();
1764 for (unsigned i
= 0; i
< src
.size(); i
++, p
++) {
1765 if ((!encode_slash
&& *p
== 0x2F) || !char_needs_url_encoding(*p
)) {
1768 rgw_uri_escape_char(*p
, dst
);
1773 std::string
url_encode(const std::string
& src
, bool encode_slash
)
1776 url_encode(src
, dst
, encode_slash
);
1781 std::string
url_remove_prefix(const std::string
& url
)
1783 std::string dst
= url
;
1784 auto pos
= dst
.find("http://");
1785 if (pos
== std::string::npos
) {
1786 pos
= dst
.find("https://");
1787 if (pos
!= std::string::npos
) {
1790 pos
= dst
.find("www.");
1791 if (pos
!= std::string::npos
) {
1802 string
rgw_trim_whitespace(const string
& src
)
1809 for (; start
!= (int)src
.size(); start
++) {
1810 if (!isspace(src
[start
]))
1814 int end
= src
.size() - 1;
1819 for (; end
> start
; end
--) {
1820 if (!isspace(src
[end
]))
1824 return src
.substr(start
, end
- start
+ 1);
1827 std::string_view
rgw_trim_whitespace(const std::string_view
& src
)
1829 std::string_view res
= src
;
1831 while (res
.size() > 0 && std::isspace(res
.front())) {
1832 res
.remove_prefix(1);
1834 while (res
.size() > 0 && std::isspace(res
.back())) {
1835 res
.remove_suffix(1);
1840 string
rgw_trim_quotes(const string
& val
)
1842 string s
= rgw_trim_whitespace(val
);
1847 int end
= s
.size() - 1;
1848 int quotes_count
= 0;
1850 if (s
[start
] == '"') {
1854 if (s
[end
] == '"') {
1858 if (quotes_count
== 2) {
1859 return s
.substr(start
, end
- start
+ 1);
1864 static struct rgw_name_to_flag cap_names
[] = { {"*", RGW_CAP_ALL
},
1865 {"read", RGW_CAP_READ
},
1866 {"write", RGW_CAP_WRITE
},
1869 int RGWUserCaps::parse_cap_perm(const string
& str
, uint32_t *perm
)
1871 return rgw_parse_list_of_flags(cap_names
, str
, perm
);
1874 int RGWUserCaps::get_cap(const string
& cap
, string
& type
, uint32_t *pperm
)
1876 int pos
= cap
.find('=');
1878 type
= rgw_trim_whitespace(cap
.substr(0, pos
));
1881 if (!is_valid_cap_type(type
))
1882 return -ERR_INVALID_CAP
;
1886 if (pos
< (int)cap
.size() - 1) {
1887 cap_perm
= cap
.substr(pos
+ 1);
1888 int r
= RGWUserCaps::parse_cap_perm(cap_perm
, &perm
);
1898 int RGWUserCaps::add_cap(const string
& cap
)
1903 int r
= get_cap(cap
, type
, &perm
);
1912 int RGWUserCaps::remove_cap(const string
& cap
)
1917 int r
= get_cap(cap
, type
, &perm
);
1921 map
<string
, uint32_t>::iterator iter
= caps
.find(type
);
1922 if (iter
== caps
.end())
1925 uint32_t& old_perm
= iter
->second
;
1933 int RGWUserCaps::add_from_string(const string
& str
)
1937 auto end
= str
.find(';', start
);
1938 if (end
== string::npos
)
1941 int r
= add_cap(str
.substr(start
, end
- start
));
1946 } while (start
< (int)str
.size());
1951 int RGWUserCaps::remove_from_string(const string
& str
)
1955 auto end
= str
.find(';', start
);
1956 if (end
== string::npos
)
1959 int r
= remove_cap(str
.substr(start
, end
- start
));
1964 } while (start
< (int)str
.size());
1969 void RGWUserCaps::dump(Formatter
*f
) const
1974 void RGWUserCaps::dump(Formatter
*f
, const char *name
) const
1976 f
->open_array_section(name
);
1977 map
<string
, uint32_t>::const_iterator iter
;
1978 for (iter
= caps
.begin(); iter
!= caps
.end(); ++iter
)
1980 f
->open_object_section("cap");
1981 f
->dump_string("type", iter
->first
);
1982 uint32_t perm
= iter
->second
;
1984 for (int i
=0; cap_names
[i
].type_name
; i
++) {
1985 if ((perm
& cap_names
[i
].flag
) == cap_names
[i
].flag
) {
1986 if (perm_str
.size())
1987 perm_str
.append(", ");
1989 perm_str
.append(cap_names
[i
].type_name
);
1990 perm
&= ~cap_names
[i
].flag
;
1993 if (perm_str
.empty())
1994 perm_str
= "<none>";
1996 f
->dump_string("perm", perm_str
);
2007 void decode_json(JSONObj
*obj
) {
2008 JSONDecoder::decode_json("type", type
, obj
);
2010 JSONDecoder::decode_json("perm", perm_str
, obj
);
2011 if (RGWUserCaps::parse_cap_perm(perm_str
, &perm
) < 0) {
2012 throw JSONDecoder::err("failed to parse permissions");
2017 void RGWUserCaps::decode_json(JSONObj
*obj
)
2019 list
<RGWUserCap
> caps_list
;
2020 decode_json_obj(caps_list
, obj
);
2022 list
<RGWUserCap
>::iterator iter
;
2023 for (iter
= caps_list
.begin(); iter
!= caps_list
.end(); ++iter
) {
2024 RGWUserCap
& cap
= *iter
;
2025 caps
[cap
.type
] = cap
.perm
;
2029 int RGWUserCaps::check_cap(const string
& cap
, uint32_t perm
) const
2031 auto iter
= caps
.find(cap
);
2033 if ((iter
== caps
.end()) ||
2034 (iter
->second
& perm
) != perm
) {
2041 bool RGWUserCaps::is_valid_cap_type(const string
& tp
)
2043 static const char *cap_type
[] = { "user",
2059 for (unsigned int i
= 0; i
< sizeof(cap_type
) / sizeof(char *); ++i
) {
2060 if (tp
.compare(cap_type
[i
]) == 0) {
2068 void rgw_pool::from_str(const string
& s
)
2070 size_t pos
= rgw_unescape_str(s
, 0, '\\', ':', &name
);
2071 if (pos
!= string::npos
) {
2072 pos
= rgw_unescape_str(s
, pos
, '\\', ':', &ns
);
2073 /* ignore return; if pos != string::npos it means that we had a colon
2074 * in the middle of ns that wasn't escaped, we're going to stop there
2079 string
rgw_pool::to_str() const
2082 rgw_escape_str(name
, '\\', ':', &esc_name
);
2087 rgw_escape_str(ns
, '\\', ':', &esc_ns
);
2088 return esc_name
+ ":" + esc_ns
;
2091 void rgw_raw_obj::decode_from_rgw_obj(bufferlist::const_iterator
& bl
)
2095 decode(old_obj
, bl
);
2097 get_obj_bucket_and_oid_loc(old_obj
, oid
, loc
);
2098 pool
= old_obj
.get_explicit_data_pool();
2101 static struct rgw_name_to_flag op_type_mapping
[] = { {"*", RGW_OP_TYPE_ALL
},
2102 {"read", RGW_OP_TYPE_READ
},
2103 {"write", RGW_OP_TYPE_WRITE
},
2104 {"delete", RGW_OP_TYPE_DELETE
},
2108 int rgw_parse_op_type_list(const string
& str
, uint32_t *perm
)
2110 return rgw_parse_list_of_flags(op_type_mapping
, str
, perm
);
2113 bool match_policy(std::string_view pattern
, std::string_view input
,
2116 const uint32_t flag2
= flag
& (MATCH_POLICY_ACTION
|MATCH_POLICY_ARN
) ?
2117 MATCH_CASE_INSENSITIVE
: 0;
2118 const bool colonblocks
= !(flag
& (MATCH_POLICY_RESOURCE
|
2119 MATCH_POLICY_STRING
));
2121 const auto npos
= std::string_view::npos
;
2122 std::string_view::size_type last_pos_input
= 0, last_pos_pattern
= 0;
2124 auto cur_pos_input
= colonblocks
? input
.find(":", last_pos_input
) : npos
;
2125 auto cur_pos_pattern
=
2126 colonblocks
? pattern
.find(":", last_pos_pattern
) : npos
;
2128 auto substr_input
= input
.substr(last_pos_input
, cur_pos_input
);
2129 auto substr_pattern
= pattern
.substr(last_pos_pattern
, cur_pos_pattern
);
2131 if (!match_wildcards(substr_pattern
, substr_input
, flag2
))
2134 if (cur_pos_pattern
== npos
)
2135 return cur_pos_input
== npos
;
2136 if (cur_pos_input
== npos
)
2139 last_pos_pattern
= cur_pos_pattern
+ 1;
2140 last_pos_input
= cur_pos_input
+ 1;
2145 * make attrs look-like-this
2146 * converts underscores to dashes
2148 string
lowercase_dash_http_attr(const string
& orig
)
2150 const char *s
= orig
.c_str();
2151 char buf
[orig
.size() + 1];
2152 buf
[orig
.size()] = '\0';
2154 for (size_t i
= 0; i
< orig
.size(); ++i
, ++s
) {
2160 buf
[i
] = tolower(*s
);
2167 * make attrs Look-Like-This
2168 * converts underscores to dashes
2170 string
camelcase_dash_http_attr(const string
& orig
)
2172 const char *s
= orig
.c_str();
2173 char buf
[orig
.size() + 1];
2174 buf
[orig
.size()] = '\0';
2176 bool last_sep
= true;
2178 for (size_t i
= 0; i
< orig
.size(); ++i
, ++s
) {
2187 buf
[i
] = toupper(*s
);
2189 buf
[i
] = tolower(*s
);
2197 RGWBucketInfo::RGWBucketInfo()
2201 RGWBucketInfo::~RGWBucketInfo()
2205 void RGWBucketInfo::encode(bufferlist
& bl
) const {
2206 ENCODE_START(23, 4, bl
);
2208 encode(owner
.id
, bl
);
2210 encode(zonegroup
, bl
);
2211 uint64_t ct
= real_clock::to_time_t(creation_time
);
2213 encode(placement_rule
, bl
);
2214 encode(has_instance_obj
, bl
);
2216 encode(requester_pays
, bl
);
2217 encode(owner
.tenant
, bl
);
2218 encode(has_website
, bl
);
2220 encode(website_conf
, bl
);
2222 encode(swift_versioning
, bl
);
2223 if (swift_versioning
) {
2224 encode(swift_ver_location
, bl
);
2226 encode(creation_time
, bl
);
2227 encode(mdsearch_config
, bl
);
2228 encode(reshard_status
, bl
);
2229 encode(new_bucket_instance_id
, bl
);
2230 if (obj_lock_enabled()) {
2231 encode(obj_lock
, bl
);
2233 bool has_sync_policy
= !empty_sync_policy();
2234 encode(has_sync_policy
, bl
);
2235 if (has_sync_policy
) {
2236 encode(*sync_policy
, bl
);
2239 encode(owner
.ns
, bl
);
2243 void RGWBucketInfo::decode(bufferlist::const_iterator
& bl
) {
2244 DECODE_START_LEGACY_COMPAT_LEN_32(23, 4, 4, bl
);
2246 if (struct_v
>= 2) {
2254 decode(zonegroup
, bl
);
2255 if (struct_v
>= 6) {
2259 creation_time
= ceph::real_clock::from_time_t((time_t)ct
);
2262 decode(placement_rule
, bl
);
2264 decode(has_instance_obj
, bl
);
2267 static constexpr uint8_t new_layout_v
= 22;
2268 if (struct_v
>= 10 && struct_v
< new_layout_v
)
2269 decode(layout
.current_index
.layout
.normal
.num_shards
, bl
);
2270 if (struct_v
>= 11 && struct_v
< new_layout_v
)
2271 decode(layout
.current_index
.layout
.normal
.hash_type
, bl
);
2273 decode(requester_pays
, bl
);
2275 decode(owner
.tenant
, bl
);
2276 if (struct_v
>= 14) {
2277 decode(has_website
, bl
);
2279 decode(website_conf
, bl
);
2281 website_conf
= RGWBucketWebsiteConf();
2284 if (struct_v
>= 15 && struct_v
< new_layout_v
) {
2287 layout
.current_index
.layout
.type
= (rgw::BucketIndexType
)it
;
2289 layout
.current_index
.layout
.type
= rgw::BucketIndexType::Normal
;
2291 swift_versioning
= false;
2292 swift_ver_location
.clear();
2293 if (struct_v
>= 16) {
2294 decode(swift_versioning
, bl
);
2295 if (swift_versioning
) {
2296 decode(swift_ver_location
, bl
);
2299 if (struct_v
>= 17) {
2300 decode(creation_time
, bl
);
2302 if (struct_v
>= 18) {
2303 decode(mdsearch_config
, bl
);
2305 if (struct_v
>= 19) {
2306 decode(reshard_status
, bl
);
2307 decode(new_bucket_instance_id
, bl
);
2309 if (struct_v
>= 20 && obj_lock_enabled()) {
2310 decode(obj_lock
, bl
);
2312 if (struct_v
>= 21) {
2313 decode(sync_policy
, bl
);
2315 if (struct_v
>= 22) {
2318 if (struct_v
>= 23) {
2319 decode(owner
.ns
, bl
);
2322 if (layout
.logs
.empty() &&
2323 layout
.current_index
.layout
.type
== rgw::BucketIndexType::Normal
) {
2324 layout
.logs
.push_back(rgw::log_layout_from_index(0, layout
.current_index
.layout
.normal
));
2329 void RGWBucketInfo::set_sync_policy(rgw_sync_policy_info
&& policy
)
2331 sync_policy
= std::move(policy
);
2334 bool RGWBucketInfo::empty_sync_policy() const
2340 return sync_policy
->empty();
2344 struct rgw_placement_rule
;
2347 void decode_json_obj(rgw_pool
& pool
, JSONObj
*obj
)
2350 decode_json_obj(s
, obj
);
2354 void encode_json(const char *name
, const rgw_placement_rule
& r
, Formatter
*f
)
2356 encode_json(name
, r
.to_str(), f
);
2359 void encode_json(const char *name
, const rgw_pool
& pool
, Formatter
*f
)
2361 f
->dump_string(name
, pool
.to_str());
2364 void encode_json(const char *name
, const RGWUserCaps
& val
, Formatter
*f
)
2369 void RGWBucketEnt::generate_test_instances(list
<RGWBucketEnt
*>& o
)
2371 RGWBucketEnt
*e
= new RGWBucketEnt
;
2372 init_bucket(&e
->bucket
, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
2374 e
->size_rounded
= 4096;
2377 o
.push_back(new RGWBucketEnt
);
2380 void RGWBucketEnt::dump(Formatter
*f
) const
2382 encode_json("bucket", bucket
, f
);
2383 encode_json("size", size
, f
);
2384 encode_json("size_rounded", size_rounded
, f
);
2385 utime_t
ut(creation_time
);
2386 encode_json("mtime", ut
, f
); /* mtime / creation time discrepency needed for backward compatibility */
2387 encode_json("count", count
, f
);
2388 encode_json("placement_rule", placement_rule
.to_str(), f
);
2391 void rgw_obj::generate_test_instances(list
<rgw_obj
*>& o
)
2394 init_bucket(&b
, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
2395 rgw_obj
*obj
= new rgw_obj(b
, "object");
2397 o
.push_back(new rgw_obj
);
2400 void rgw_bucket_placement::dump(Formatter
*f
) const
2402 encode_json("bucket", bucket
, f
);
2403 encode_json("placement_rule", placement_rule
, f
);
2406 void RGWBucketInfo::generate_test_instances(list
<RGWBucketInfo
*>& o
)
2408 // Since things without a log will have one synthesized on decode,
2409 // ensure the things we attempt to encode will have one added so we
2410 // round-trip properly.
2411 auto gen_layout
= [](rgw::BucketLayout
& l
) {
2412 l
.current_index
.gen
= 0;
2413 l
.current_index
.layout
.normal
.hash_type
= rgw::BucketHashType::Mod
;
2414 l
.current_index
.layout
.type
= rgw::BucketIndexType::Normal
;
2415 l
.current_index
.layout
.normal
.num_shards
= 11;
2416 l
.logs
.push_back(log_layout_from_index(
2417 l
.current_index
.gen
,
2418 l
.current_index
.layout
.normal
));
2422 RGWBucketInfo
*i
= new RGWBucketInfo
;
2423 init_bucket(&i
->bucket
, "tenant", "bucket", "pool", ".index_pool", "marker", "10");
2425 i
->flags
= BUCKET_SUSPENDED
;
2426 gen_layout(i
->layout
);
2428 i
= new RGWBucketInfo
;
2429 gen_layout(i
->layout
);
2433 void RGWBucketInfo::dump(Formatter
*f
) const
2435 encode_json("bucket", bucket
, f
);
2436 utime_t
ut(creation_time
);
2437 encode_json("creation_time", ut
, f
);
2438 encode_json("owner", owner
.to_str(), f
);
2439 encode_json("flags", flags
, f
);
2440 encode_json("zonegroup", zonegroup
, f
);
2441 encode_json("placement_rule", placement_rule
, f
);
2442 encode_json("has_instance_obj", has_instance_obj
, f
);
2443 encode_json("quota", quota
, f
);
2444 encode_json("num_shards", layout
.current_index
.layout
.normal
.num_shards
, f
);
2445 encode_json("bi_shard_hash_type", (uint32_t)layout
.current_index
.layout
.normal
.hash_type
, f
);
2446 encode_json("requester_pays", requester_pays
, f
);
2447 encode_json("has_website", has_website
, f
);
2449 encode_json("website_conf", website_conf
, f
);
2451 encode_json("swift_versioning", swift_versioning
, f
);
2452 encode_json("swift_ver_location", swift_ver_location
, f
);
2453 encode_json("index_type", (uint32_t)layout
.current_index
.layout
.type
, f
);
2454 encode_json("mdsearch_config", mdsearch_config
, f
);
2455 encode_json("reshard_status", (int)reshard_status
, f
);
2456 encode_json("new_bucket_instance_id", new_bucket_instance_id
, f
);
2457 if (!empty_sync_policy()) {
2458 encode_json("sync_policy", *sync_policy
, f
);
2462 void RGWBucketInfo::decode_json(JSONObj
*obj
) {
2463 JSONDecoder::decode_json("bucket", bucket
, obj
);
2465 JSONDecoder::decode_json("creation_time", ut
, obj
);
2466 creation_time
= ut
.to_real_time();
2467 JSONDecoder::decode_json("owner", owner
, obj
);
2468 JSONDecoder::decode_json("flags", flags
, obj
);
2469 JSONDecoder::decode_json("zonegroup", zonegroup
, obj
);
2470 /* backward compatability with region */
2471 if (zonegroup
.empty()) {
2472 JSONDecoder::decode_json("region", zonegroup
, obj
);
2475 JSONDecoder::decode_json("placement_rule", pr
, obj
);
2476 placement_rule
.from_str(pr
);
2477 JSONDecoder::decode_json("has_instance_obj", has_instance_obj
, obj
);
2478 JSONDecoder::decode_json("quota", quota
, obj
);
2479 JSONDecoder::decode_json("num_shards", layout
.current_index
.layout
.normal
.num_shards
, obj
);
2481 JSONDecoder::decode_json("bi_shard_hash_type", hash_type
, obj
);
2482 layout
.current_index
.layout
.normal
.hash_type
= static_cast<rgw::BucketHashType
>(hash_type
);
2483 JSONDecoder::decode_json("requester_pays", requester_pays
, obj
);
2484 JSONDecoder::decode_json("has_website", has_website
, obj
);
2486 JSONDecoder::decode_json("website_conf", website_conf
, obj
);
2488 JSONDecoder::decode_json("swift_versioning", swift_versioning
, obj
);
2489 JSONDecoder::decode_json("swift_ver_location", swift_ver_location
, obj
);
2491 JSONDecoder::decode_json("index_type", it
, obj
);
2492 layout
.current_index
.layout
.type
= (rgw::BucketIndexType
)it
;
2493 JSONDecoder::decode_json("mdsearch_config", mdsearch_config
, obj
);
2495 JSONDecoder::decode_json("reshard_status", rs
, obj
);
2496 reshard_status
= (cls_rgw_reshard_status
)rs
;
2498 rgw_sync_policy_info sp
;
2499 JSONDecoder::decode_json("sync_policy", sp
, obj
);
2501 set_sync_policy(std::move(sp
));
2505 void RGWUserInfo::generate_test_instances(list
<RGWUserInfo
*>& o
)
2507 RGWUserInfo
*i
= new RGWUserInfo
;
2508 i
->user_id
= "user_id";
2509 i
->display_name
= "display_name";
2510 i
->user_email
= "user@email";
2511 RGWAccessKey k1
, k2
;
2515 k2
.subuser
= "subuser";
2519 i
->access_keys
[k1
.id
] = k1
;
2520 i
->swift_keys
[k2
.id
] = k2
;
2521 i
->subusers
[u
.name
] = u
;
2524 o
.push_back(new RGWUserInfo
);
2527 static void user_info_dump_subuser(const char *name
, const RGWSubUser
& subuser
, Formatter
*f
, void *parent
)
2529 RGWUserInfo
*info
= static_cast<RGWUserInfo
*>(parent
);
2530 subuser
.dump(f
, info
->user_id
.to_str());
2533 static void user_info_dump_key(const char *name
, const RGWAccessKey
& key
, Formatter
*f
, void *parent
)
2535 RGWUserInfo
*info
= static_cast<RGWUserInfo
*>(parent
);
2536 key
.dump(f
, info
->user_id
.to_str(), false);
2539 static void user_info_dump_swift_key(const char *name
, const RGWAccessKey
& key
, Formatter
*f
, void *parent
)
2541 RGWUserInfo
*info
= static_cast<RGWUserInfo
*>(parent
);
2542 key
.dump(f
, info
->user_id
.to_str(), true);
2545 static void decode_access_keys(map
<string
, RGWAccessKey
>& m
, JSONObj
*o
)
2552 static void decode_swift_keys(map
<string
, RGWAccessKey
>& m
, JSONObj
*o
)
2555 k
.decode_json(o
, true);
2559 static void decode_subusers(map
<string
, RGWSubUser
>& m
, JSONObj
*o
)
2567 struct rgw_flags_desc
{
2572 static struct rgw_flags_desc rgw_perms
[] = {
2573 { RGW_PERM_FULL_CONTROL
, "full-control" },
2574 { RGW_PERM_READ
| RGW_PERM_WRITE
, "read-write" },
2575 { RGW_PERM_READ
, "read" },
2576 { RGW_PERM_WRITE
, "write" },
2577 { RGW_PERM_READ_ACP
, "read-acp" },
2578 { RGW_PERM_WRITE_ACP
, "write-acp" },
2582 void rgw_perm_to_str(uint32_t mask
, char *buf
, int len
)
2584 const char *sep
= "";
2587 snprintf(buf
, len
, "<none>");
2591 uint32_t orig_mask
= mask
;
2592 for (int i
= 0; rgw_perms
[i
].mask
; i
++) {
2593 struct rgw_flags_desc
*desc
= &rgw_perms
[i
];
2594 if ((mask
& desc
->mask
) == desc
->mask
) {
2595 pos
+= snprintf(buf
+ pos
, len
- pos
, "%s%s", sep
, desc
->str
);
2599 mask
&= ~desc
->mask
;
2604 if (mask
== orig_mask
) // no change
2609 uint32_t rgw_str_to_perm(const char *str
)
2611 if (strcasecmp(str
, "") == 0)
2612 return RGW_PERM_NONE
;
2613 else if (strcasecmp(str
, "read") == 0)
2614 return RGW_PERM_READ
;
2615 else if (strcasecmp(str
, "write") == 0)
2616 return RGW_PERM_WRITE
;
2617 else if (strcasecmp(str
, "readwrite") == 0)
2618 return RGW_PERM_READ
| RGW_PERM_WRITE
;
2619 else if (strcasecmp(str
, "full") == 0)
2620 return RGW_PERM_FULL_CONTROL
;
2622 return RGW_PERM_INVALID
;
2626 static void mask_to_str(T
*mask_list
, uint32_t mask
, char *buf
, int len
)
2628 const char *sep
= "";
2631 snprintf(buf
, len
, "<none>");
2635 uint32_t orig_mask
= mask
;
2636 for (int i
= 0; mask_list
[i
].mask
; i
++) {
2637 T
*desc
= &mask_list
[i
];
2638 if ((mask
& desc
->mask
) == desc
->mask
) {
2639 pos
+= snprintf(buf
+ pos
, len
- pos
, "%s%s", sep
, desc
->str
);
2643 mask
&= ~desc
->mask
;
2648 if (mask
== orig_mask
) // no change
2653 static void perm_to_str(uint32_t mask
, char *buf
, int len
)
2655 return mask_to_str(rgw_perms
, mask
, buf
, len
);
2658 static struct rgw_flags_desc op_type_flags
[] = {
2659 { RGW_OP_TYPE_READ
, "read" },
2660 { RGW_OP_TYPE_WRITE
, "write" },
2661 { RGW_OP_TYPE_DELETE
, "delete" },
2665 void op_type_to_str(uint32_t mask
, char *buf
, int len
)
2667 return mask_to_str(op_type_flags
, mask
, buf
, len
);
2670 void RGWRateLimitInfo::decode_json(JSONObj
*obj
)
2672 JSONDecoder::decode_json("max_read_ops", max_read_ops
, obj
);
2673 JSONDecoder::decode_json("max_write_ops", max_write_ops
, obj
);
2674 JSONDecoder::decode_json("max_read_bytes", max_read_ops
, obj
);
2675 JSONDecoder::decode_json("max_write_bytes", max_write_ops
, obj
);
2676 JSONDecoder::decode_json("enabled", enabled
, obj
);
2679 void RGWRateLimitInfo::dump(Formatter
*f
) const
2681 f
->dump_int("max_read_ops", max_read_ops
);
2682 f
->dump_int("max_write_ops", max_write_ops
);
2683 f
->dump_int("max_read_bytes", max_read_bytes
);
2684 f
->dump_int("max_write_bytes", max_write_bytes
);
2685 f
->dump_bool("enabled", enabled
);
2688 void RGWUserInfo::dump(Formatter
*f
) const
2691 encode_json("user_id", user_id
.to_str(), f
);
2692 encode_json("display_name", display_name
, f
);
2693 encode_json("email", user_email
, f
);
2694 encode_json("suspended", (int)suspended
, f
);
2695 encode_json("max_buckets", (int)max_buckets
, f
);
2697 encode_json_map("subusers", NULL
, "subuser", NULL
, user_info_dump_subuser
,(void *)this, subusers
, f
);
2698 encode_json_map("keys", NULL
, "key", NULL
, user_info_dump_key
,(void *)this, access_keys
, f
);
2699 encode_json_map("swift_keys", NULL
, "key", NULL
, user_info_dump_swift_key
,(void *)this, swift_keys
, f
);
2701 encode_json("caps", caps
, f
);
2704 op_type_to_str(op_mask
, buf
, sizeof(buf
));
2705 encode_json("op_mask", (const char *)buf
, f
);
2707 if (system
) { /* no need to show it for every user */
2708 encode_json("system", (bool)system
, f
);
2711 encode_json("admin", (bool)admin
, f
);
2713 encode_json("default_placement", default_placement
.name
, f
);
2714 encode_json("default_storage_class", default_placement
.storage_class
, f
);
2715 encode_json("placement_tags", placement_tags
, f
);
2716 encode_json("bucket_quota", bucket_quota
, f
);
2717 encode_json("user_quota", user_quota
, f
);
2718 encode_json("temp_url_keys", temp_url_keys
, f
);
2720 string user_source_type
;
2721 switch ((RGWIdentityType
)type
) {
2723 user_source_type
= "rgw";
2726 user_source_type
= "keystone";
2729 user_source_type
= "ldap";
2732 user_source_type
= "none";
2735 user_source_type
= "none";
2738 encode_json("type", user_source_type
, f
);
2739 encode_json("mfa_ids", mfa_ids
, f
);
2742 void RGWUserInfo::decode_json(JSONObj
*obj
)
2746 JSONDecoder::decode_json("user_id", uid
, obj
, true);
2747 user_id
.from_str(uid
);
2749 JSONDecoder::decode_json("display_name", display_name
, obj
);
2750 JSONDecoder::decode_json("email", user_email
, obj
);
2752 JSONDecoder::decode_json("suspended", susp
, obj
);
2753 suspended
= (__u8
)susp
;
2754 JSONDecoder::decode_json("max_buckets", max_buckets
, obj
);
2756 JSONDecoder::decode_json("keys", access_keys
, decode_access_keys
, obj
);
2757 JSONDecoder::decode_json("swift_keys", swift_keys
, decode_swift_keys
, obj
);
2758 JSONDecoder::decode_json("subusers", subusers
, decode_subusers
, obj
);
2760 JSONDecoder::decode_json("caps", caps
, obj
);
2763 JSONDecoder::decode_json("op_mask", mask_str
, obj
);
2764 rgw_parse_op_type_list(mask_str
, &op_mask
);
2767 JSONDecoder::decode_json("system", sys
, obj
);
2770 JSONDecoder::decode_json("admin", ad
, obj
);
2772 JSONDecoder::decode_json("default_placement", default_placement
.name
, obj
);
2773 JSONDecoder::decode_json("default_storage_class", default_placement
.storage_class
, obj
);
2774 JSONDecoder::decode_json("placement_tags", placement_tags
, obj
);
2775 JSONDecoder::decode_json("bucket_quota", bucket_quota
, obj
);
2776 JSONDecoder::decode_json("user_quota", user_quota
, obj
);
2777 JSONDecoder::decode_json("temp_url_keys", temp_url_keys
, obj
);
2779 string user_source_type
;
2780 JSONDecoder::decode_json("type", user_source_type
, obj
);
2781 if (user_source_type
== "rgw") {
2783 } else if (user_source_type
== "keystone") {
2784 type
= TYPE_KEYSTONE
;
2785 } else if (user_source_type
== "ldap") {
2787 } else if (user_source_type
== "none") {
2790 JSONDecoder::decode_json("mfa_ids", mfa_ids
, obj
);
2794 void RGWSubUser::generate_test_instances(list
<RGWSubUser
*>& o
)
2796 RGWSubUser
*u
= new RGWSubUser
;
2800 o
.push_back(new RGWSubUser
);
2803 void RGWSubUser::dump(Formatter
*f
) const
2805 encode_json("id", name
, f
);
2807 perm_to_str(perm_mask
, buf
, sizeof(buf
));
2808 encode_json("permissions", (const char *)buf
, f
);
2811 void RGWSubUser::dump(Formatter
*f
, const string
& user
) const
2816 encode_json("id", s
, f
);
2818 perm_to_str(perm_mask
, buf
, sizeof(buf
));
2819 encode_json("permissions", (const char *)buf
, f
);
2822 uint32_t str_to_perm(const string
& s
)
2824 if (s
.compare("read") == 0)
2825 return RGW_PERM_READ
;
2826 else if (s
.compare("write") == 0)
2827 return RGW_PERM_WRITE
;
2828 else if (s
.compare("read-write") == 0)
2829 return RGW_PERM_READ
| RGW_PERM_WRITE
;
2830 else if (s
.compare("full-control") == 0)
2831 return RGW_PERM_FULL_CONTROL
;
2835 void RGWSubUser::decode_json(JSONObj
*obj
)
2838 JSONDecoder::decode_json("id", uid
, obj
);
2839 int pos
= uid
.find(':');
2841 name
= uid
.substr(pos
+ 1);
2843 JSONDecoder::decode_json("permissions", perm_str
, obj
);
2844 perm_mask
= str_to_perm(perm_str
);
2847 void RGWAccessKey::generate_test_instances(list
<RGWAccessKey
*>& o
)
2849 RGWAccessKey
*k
= new RGWAccessKey
;
2852 k
->subuser
= "subuser";
2854 o
.push_back(new RGWAccessKey
);
2857 void RGWAccessKey::dump(Formatter
*f
) const
2859 encode_json("access_key", id
, f
);
2860 encode_json("secret_key", key
, f
);
2861 encode_json("subuser", subuser
, f
);
2864 void RGWAccessKey::dump_plain(Formatter
*f
) const
2866 encode_json("access_key", id
, f
);
2867 encode_json("secret_key", key
, f
);
2870 void RGWAccessKey::dump(Formatter
*f
, const string
& user
, bool swift
) const
2873 if (!subuser
.empty()) {
2877 encode_json("user", u
, f
);
2879 encode_json("access_key", id
, f
);
2881 encode_json("secret_key", key
, f
);
2884 void RGWAccessKey::decode_json(JSONObj
*obj
) {
2885 JSONDecoder::decode_json("access_key", id
, obj
, true);
2886 JSONDecoder::decode_json("secret_key", key
, obj
, true);
2887 if (!JSONDecoder::decode_json("subuser", subuser
, obj
)) {
2889 JSONDecoder::decode_json("user", user
, obj
);
2890 int pos
= user
.find(':');
2892 subuser
= user
.substr(pos
+ 1);
2897 void RGWAccessKey::decode_json(JSONObj
*obj
, bool swift
) {
2903 if (!JSONDecoder::decode_json("subuser", subuser
, obj
)) {
2904 JSONDecoder::decode_json("user", id
, obj
, true);
2905 int pos
= id
.find(':');
2907 subuser
= id
.substr(pos
+ 1);
2910 JSONDecoder::decode_json("secret_key", key
, obj
, true);
2913 void RGWStorageStats::dump(Formatter
*f
) const
2915 encode_json("size", size
, f
);
2916 encode_json("size_actual", size_rounded
, f
);
2917 if (dump_utilized
) {
2918 encode_json("size_utilized", size_utilized
, f
);
2920 encode_json("size_kb", rgw_rounded_kb(size
), f
);
2921 encode_json("size_kb_actual", rgw_rounded_kb(size_rounded
), f
);
2922 if (dump_utilized
) {
2923 encode_json("size_kb_utilized", rgw_rounded_kb(size_utilized
), f
);
2925 encode_json("num_objects", num_objects
, f
);
2928 void rgw_obj_key::dump(Formatter
*f
) const
2930 encode_json("name", name
, f
);
2931 encode_json("instance", instance
, f
);
2932 encode_json("ns", ns
, f
);
2935 void rgw_obj_key::decode_json(JSONObj
*obj
)
2937 JSONDecoder::decode_json("name", name
, obj
);
2938 JSONDecoder::decode_json("instance", instance
, obj
);
2939 JSONDecoder::decode_json("ns", ns
, obj
);
2942 void rgw_raw_obj::dump(Formatter
*f
) const
2944 encode_json("pool", pool
, f
);
2945 encode_json("oid", oid
, f
);
2946 encode_json("loc", loc
, f
);
2949 void rgw_raw_obj::decode_json(JSONObj
*obj
) {
2950 JSONDecoder::decode_json("pool", pool
, obj
);
2951 JSONDecoder::decode_json("oid", oid
, obj
);
2952 JSONDecoder::decode_json("loc", loc
, obj
);
2955 void rgw_obj::dump(Formatter
*f
) const
2957 encode_json("bucket", bucket
, f
);
2958 encode_json("key", key
, f
);