summaryrefslogtreecommitdiffstats
path: root/src/rgw/rgw_rest_ratelimit.cc
blob: b482b4f82c49a7358d8af5302a7242529335506f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#include "rgw_rest_ratelimit.h"
class RGWOp_Ratelimit_Info : public RGWRESTOp {
int check_caps(const RGWUserCaps& caps) override {
  return caps.check_cap("ratelimit", RGW_CAP_READ);
}
  
  void execute(optional_yield y) override;

  const char* name() const override { return "get_ratelimit_info"; }
};
void RGWOp_Ratelimit_Info::execute(optional_yield y)
{
  ldpp_dout(this, 20) << "" << dendl;
  std::string uid_str;
  std::string ratelimit_scope;
  std::string bucket_name;
  std::string tenant_name;
  bool global = false;
  RESTArgs::get_string(s, "uid", uid_str, &uid_str);
  RESTArgs::get_string(s, "ratelimit-scope", ratelimit_scope, &ratelimit_scope);
  RESTArgs::get_string(s, "bucket", bucket_name, &bucket_name);
  RESTArgs::get_string(s, "tenant", tenant_name, &tenant_name);
  // RESTArgs::get_bool default value to true even if global is empty
  bool exists;
  std::string sval = s->info.args.get("global", &exists);
  if (exists) {
    if (!boost::iequals(sval,"true") && !boost::iequals(sval,"false")) {
      op_ret = -EINVAL;
      ldpp_dout(this, 20) << "global is not equal to true or false" << dendl;
      return;
    }
  }
  RESTArgs::get_bool(s, "global", false, &global);

  if (ratelimit_scope == "bucket" && !bucket_name.empty() && !global) {
    std::unique_ptr<rgw::sal::Bucket> bucket;
    int r = driver->get_bucket(s, nullptr, tenant_name, bucket_name, &bucket, y);
    if (r != 0) {
      op_ret = r;
      ldpp_dout(this, 0) << "Error on getting bucket info" << dendl;
      return;
    }
    RGWRateLimitInfo ratelimit_info;
    auto iter = bucket->get_attrs().find(RGW_ATTR_RATELIMIT);
    if (iter != bucket->get_attrs().end()) {
      try {
        bufferlist& bl = iter->second;
        auto biter = bl.cbegin();
        decode(ratelimit_info, biter);
      } catch (buffer::error& err) {
        ldpp_dout(this, 0) << "Error on decoding ratelimit info from bucket" << dendl;
        op_ret = -EIO;
        return;
      }
    }
    flusher.start(0);
    s->formatter->open_object_section("bucket_ratelimit");
    encode_json("bucket_ratelimit", ratelimit_info, s->formatter);
    s->formatter->close_section();
    flusher.flush();
    return;
  }
  if (ratelimit_scope == "user" && !uid_str.empty() && !global) {
    RGWRateLimitInfo ratelimit_info;
    rgw_user user(uid_str);
    std::unique_ptr<rgw::sal::User> user_sal;
    user_sal = driver->get_user(user);
    if (!rgw::sal::User::empty(user_sal)) {
      op_ret = user_sal->load_user(this, y);
      if (op_ret) {
        ldpp_dout(this, 0) << "Cannot load user info" << dendl;
        return;
      }
    } else {
      ldpp_dout(this, 0) << "User does not exist" << dendl;
      op_ret = -ENOENT;
      return;
    }

    auto iter = user_sal->get_attrs().find(RGW_ATTR_RATELIMIT);
    if(iter != user_sal->get_attrs().end()) {
      try {
        bufferlist& bl = iter->second;
        auto biter = bl.cbegin();
        decode(ratelimit_info, biter);
      } catch (buffer::error& err) {
        ldpp_dout(this, 0) << "Error on decoding ratelimit info from user" << dendl;
        op_ret = -EIO;
        return;
      }
    }
    flusher.start(0);
    s->formatter->open_object_section("user_ratelimit");
    encode_json("user_ratelimit", ratelimit_info, s->formatter);
    s->formatter->close_section();
    flusher.flush();
  }
  if (global) {
    std::string realm_id = driver->get_zone()->get_realm_id();
    RGWPeriodConfig period_config;
    op_ret = period_config.read(this, static_cast<rgw::sal::RadosStore*>(driver)->svc()->sysobj, realm_id, y);
    if (op_ret && op_ret != -ENOENT) {
      ldpp_dout(this, 0) << "Error on period config read" << dendl;
      return;
    }
    flusher.start(0);
    s->formatter->open_object_section("period_config");
    encode_json("bucket_ratelimit", period_config.bucket_ratelimit, s->formatter);
    encode_json("user_ratelimit", period_config.user_ratelimit, s->formatter);
    encode_json("anonymous_ratelimit", period_config.anon_ratelimit, s->formatter);
    s->formatter->close_section();
    flusher.flush();
    return;
  }
  op_ret = -EINVAL;
  return;
}

class RGWOp_Ratelimit_Set : public RGWRESTOp {
  int check_caps(const RGWUserCaps& caps) override {
    return caps.check_cap("ratelimit", RGW_CAP_WRITE);
  }

  void execute(optional_yield y) override;

  const char* name() const override { return "put_ratelimit_info"; }

  void set_ratelimit_info(bool have_max_read_ops, int64_t max_read_ops, bool have_max_write_ops, int64_t max_write_ops,
                          bool have_max_read_bytes, int64_t max_read_bytes, bool have_max_write_bytes, int64_t max_write_bytes,
                          bool have_enabled, bool enabled, bool& ratelimit_configured, RGWRateLimitInfo& ratelimit_info);
};


  void RGWOp_Ratelimit_Set::set_ratelimit_info(bool have_max_read_ops, int64_t max_read_ops, bool have_max_write_ops, int64_t max_write_ops,
                          bool have_max_read_bytes, int64_t max_read_bytes, bool have_max_write_bytes, int64_t max_write_bytes,
                          bool have_enabled, bool enabled, bool& ratelimit_configured, RGWRateLimitInfo& ratelimit_info) 
  {
    if (have_max_read_ops) {
      if (max_read_ops >= 0) {
        ratelimit_info.max_read_ops = max_read_ops;
        ratelimit_configured = true;
      }
    }
    if (have_max_write_ops) {
      if (max_write_ops >= 0) {
        ratelimit_info.max_write_ops = max_write_ops;
        ratelimit_configured = true;
      }
    }
    if (have_max_read_bytes) {
      if (max_read_bytes >= 0) {
        ratelimit_info.max_read_bytes = max_read_bytes;
        ratelimit_configured = true;
      }
    }
    if (have_max_write_bytes) {
      if (max_write_bytes >= 0) {
        ratelimit_info.max_write_bytes = max_write_bytes;
        ratelimit_configured = true;
      }
    }
    if (have_enabled) {
      ratelimit_info.enabled = enabled;
      ratelimit_configured = true;
    }
    if (!ratelimit_configured) {
      ldpp_dout(this, 0) << "No rate limit configuration arguments have been sent" << dendl;
      op_ret = -EINVAL;
      return;
    }

  }


void RGWOp_Ratelimit_Set::execute(optional_yield y)
{
  std::string uid_str;
  std::string ratelimit_scope;
  std::string bucket_name;
  std::string tenant_name;
  RGWRateLimitInfo ratelimit_info;
  bool ratelimit_configured = false;
  bool enabled = false;
  bool have_enabled = false;
  bool global = false;
  int64_t max_read_ops = 0;
  bool have_max_read_ops = false;
  int64_t max_write_ops = 0;
  bool have_max_write_ops = false;
  int64_t max_read_bytes = 0;
  bool have_max_read_bytes = false;
  int64_t max_write_bytes = 0;
  bool have_max_write_bytes = false;
  RESTArgs::get_string(s, "uid", uid_str, &uid_str);
  RESTArgs::get_string(s, "ratelimit-scope", ratelimit_scope, &ratelimit_scope);
  RESTArgs::get_string(s, "bucket", bucket_name, &bucket_name);
  RESTArgs::get_string(s, "tenant", tenant_name, &tenant_name);
  // check there was no -EINVAL coming from get_int64
  op_ret = RESTArgs::get_int64(s, "max-read-ops", 0, &max_read_ops, &have_max_read_ops);
  op_ret |= RESTArgs::get_int64(s, "max-write-ops", 0, &max_write_ops, &have_max_write_ops);
  op_ret |= RESTArgs::get_int64(s, "max-read-bytes", 0, &max_read_bytes, &have_max_read_bytes);
  op_ret |= RESTArgs::get_int64(s, "max-write-bytes", 0, &max_write_bytes, &have_max_write_bytes);
  if (op_ret) {
    ldpp_dout(this, 0) << "one of the maximum arguments could not be parsed" << dendl;
    return;
  }
  // RESTArgs::get_bool default value to true even if enabled or global are empty
  std::string sval = s->info.args.get("enabled", &have_enabled);
  if (have_enabled) {
    if (!boost::iequals(sval,"true") && !boost::iequals(sval,"false")) {
      ldpp_dout(this, 20) << "enabled is not equal to true or false" << dendl;
      op_ret = -EINVAL;
      return;
    }
  }
  RESTArgs::get_bool(s, "enabled", false, &enabled, &have_enabled);
  bool exists;
  sval = s->info.args.get("global", &exists);
  if (exists) {
    if (!boost::iequals(sval,"true") && !boost::iequals(sval,"false")) {
      ldpp_dout(this, 20) << "global is not equal to true or faslse" << dendl;
      op_ret = -EINVAL;
      return;
    }
  }
  RESTArgs::get_bool(s, "global", false, &global, nullptr);
  set_ratelimit_info(have_max_read_ops, max_read_ops, have_max_write_ops, max_write_ops,
                     have_max_read_bytes, max_read_bytes, have_max_write_bytes, max_write_bytes,
                     have_enabled, enabled, ratelimit_configured, ratelimit_info);
  if (op_ret) {
    return;
  }
  if (ratelimit_scope == "user" && !uid_str.empty() && !global) {
    rgw_user user(uid_str);
    std::unique_ptr<rgw::sal::User> user_sal;
    user_sal = driver->get_user(user);
    if (!rgw::sal::User::empty(user_sal)) {
      op_ret = user_sal->load_user(this, y);
      if (op_ret) {
        ldpp_dout(this, 0) << "Cannot load user info" << dendl;
        return;
      }
    } else {
      ldpp_dout(this, 0) << "User does not exist" << dendl;
      op_ret = -ENOENT;
      return;
    }
    auto iter = user_sal->get_attrs().find(RGW_ATTR_RATELIMIT);
    if (iter != user_sal->get_attrs().end()) {
      try {
        bufferlist& bl = iter->second;
        auto biter = bl.cbegin();
        decode(ratelimit_info, biter);
      } catch (buffer::error& err) {
        ldpp_dout(this, 0) << "Error on decoding ratelimit info from user" << dendl;
        op_ret = -EIO;
        return;
      }
    }
    set_ratelimit_info(have_max_read_ops, max_read_ops, have_max_write_ops, max_write_ops,
                       have_max_read_bytes, max_read_bytes, have_max_write_bytes, max_write_bytes,
                       have_enabled, enabled, ratelimit_configured, ratelimit_info);
    bufferlist bl;
    ratelimit_info.encode(bl);
    rgw::sal::Attrs attr;
    attr[RGW_ATTR_RATELIMIT] = bl;
    op_ret = user_sal->merge_and_store_attrs(this, attr, y);
    return;
  }

  if (ratelimit_scope == "bucket" && !bucket_name.empty() && !global) {
    ldpp_dout(this, 0) << "getting bucket info" << dendl;
    std::unique_ptr<rgw::sal::Bucket> bucket;
    op_ret = driver->get_bucket(this, nullptr, tenant_name, bucket_name, &bucket, y);
    if (op_ret) {
      ldpp_dout(this, 0) << "Error on getting bucket info" << dendl;
      return;
    }
    auto iter = bucket->get_attrs().find(RGW_ATTR_RATELIMIT);
    if (iter != bucket->get_attrs().end()) {
      try {
        bufferlist& bl = iter->second;
        auto biter = bl.cbegin();
        decode(ratelimit_info, biter);
      } catch (buffer::error& err) {
        ldpp_dout(this, 0) << "Error on decoding ratelimit info from bucket" << dendl;
        op_ret = -EIO;
        return;
      }
    }
    bufferlist bl;
    set_ratelimit_info(have_max_read_ops, max_read_ops, have_max_write_ops, max_write_ops,
                       have_max_read_bytes, max_read_bytes, have_max_write_bytes, max_write_bytes,
                       have_enabled, enabled, ratelimit_configured, ratelimit_info);
    ratelimit_info.encode(bl);
    rgw::sal::Attrs attr;
    attr[RGW_ATTR_RATELIMIT] = bl;
    op_ret = bucket->merge_and_store_attrs(this, attr, y);
    return;
  }
  if (global) {
    std::string realm_id = driver->get_zone()->get_realm_id();
    RGWPeriodConfig period_config;
    op_ret = period_config.read(s, static_cast<rgw::sal::RadosStore*>(driver)->svc()->sysobj, realm_id, y);
    if (op_ret && op_ret != -ENOENT) {
      ldpp_dout(this, 0) << "Error on period config read" << dendl;
      return;
    }
    if (ratelimit_scope == "bucket") {
      ratelimit_info = period_config.bucket_ratelimit;
      set_ratelimit_info(have_max_read_ops, max_read_ops, have_max_write_ops, max_write_ops,
                         have_max_read_bytes, max_read_bytes, have_max_write_bytes, max_write_bytes,
                         have_enabled, enabled, ratelimit_configured, ratelimit_info);
      period_config.bucket_ratelimit = ratelimit_info;
      op_ret = period_config.write(s, static_cast<rgw::sal::RadosStore*>(driver)->svc()->sysobj, realm_id, y);
      return;
    }
    if (ratelimit_scope == "anon") {
      ratelimit_info = period_config.anon_ratelimit;
      set_ratelimit_info(have_max_read_ops, max_read_ops, have_max_write_ops, max_write_ops,
                         have_max_read_bytes, max_read_bytes, have_max_write_bytes, max_write_bytes,
                         have_enabled, enabled, ratelimit_configured, ratelimit_info);
      period_config.anon_ratelimit = ratelimit_info;
      op_ret = period_config.write(s, static_cast<rgw::sal::RadosStore*>(driver)->svc()->sysobj, realm_id, y);
      return;
    }
    if (ratelimit_scope == "user") {
      ratelimit_info = period_config.user_ratelimit;
      set_ratelimit_info(have_max_read_ops, max_read_ops, have_max_write_ops, max_write_ops,
                         have_max_read_bytes, max_read_bytes, have_max_write_bytes, max_write_bytes,
                         have_enabled, enabled, ratelimit_configured, ratelimit_info);
      period_config.user_ratelimit = ratelimit_info;
      op_ret = period_config.write(s, static_cast<rgw::sal::RadosStore*>(driver)->svc()->sysobj, realm_id, y);
      return;
    }
  }
  op_ret = -EINVAL;
  return;
}
RGWOp* RGWHandler_Ratelimit::op_get()
{
  return new RGWOp_Ratelimit_Info;
}
RGWOp* RGWHandler_Ratelimit::op_post()
{
  return new RGWOp_Ratelimit_Set;
}