-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgw_swift.cc
654 lines (513 loc) · 15.5 KB
/
rgw_swift.cc
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "common/ceph_json.h"
#include "rgw_common.h"
#include "rgw_swift.h"
#include "rgw_swift_auth.h"
#include "rgw_user.h"
#include "rgw_http_client.h"
#include "rgw_keystone.h"
#include "include/str_list.h"
#include "common/ceph_crypto_cms.h"
#include "common/armor.h"
#define dout_subsys ceph_subsys_rgw
static list<string> roles_list;
class RGWValidateSwiftToken : public RGWHTTPClient {
struct rgw_swift_auth_info *info;
protected:
RGWValidateSwiftToken() : RGWHTTPClient(NULL), info(NULL) {}
public:
RGWValidateSwiftToken(CephContext *_cct, struct rgw_swift_auth_info *_info) : RGWHTTPClient(_cct), info(_info) {}
int receive_header(void *ptr, size_t len);
int receive_data(void *ptr, size_t len) {
return 0;
}
int send_data(void *ptr, size_t len) {
return 0;
}
friend class RGWKeystoneTokenCache;
};
int RGWValidateSwiftToken::receive_header(void *ptr, size_t len)
{
char line[len + 1];
char *s = (char *)ptr, *end = (char *)ptr + len;
char *p = line;
ldout(cct, 10) << "read_http_header" << dendl;
while (s != end) {
if (*s == '\r') {
s++;
continue;
}
if (*s == '\n') {
*p = '\0';
ldout(cct, 10) << "os_auth:" << line << dendl;
// TODO: fill whatever data required here
char *l = line;
char *tok = strsep(&l, " \t:");
if (tok) {
while (l && *l == ' ')
l++;
if (strcmp(tok, "HTTP") == 0) {
info->status = atoi(l);
} else if (strcasecmp(tok, "X-Auth-Groups") == 0) {
info->auth_groups = l;
char *s = strchr(l, ',');
if (s) {
*s = '\0';
info->user = l;
}
} else if (strcasecmp(tok, "X-Auth-Ttl") == 0) {
info->ttl = atoll(l);
}
}
}
if (s != end)
*p++ = *s++;
}
return 0;
}
int RGWSwift::validate_token(const char *token, struct rgw_swift_auth_info *info)
{
if (g_conf->rgw_swift_auth_url.empty())
return -EINVAL;
string auth_url = g_conf->rgw_swift_auth_url;
if (auth_url[auth_url.size() - 1] != '/')
auth_url.append("/");
auth_url.append("token");
char url_buf[auth_url.size() + 1 + strlen(token) + 1];
sprintf(url_buf, "%s/%s", auth_url.c_str(), token);
RGWValidateSwiftToken validate(cct, info);
ldout(cct, 10) << "rgw_swift_validate_token url=" << url_buf << dendl;
int ret = validate.process(url_buf);
if (ret < 0)
return ret;
return 0;
}
class RGWPostHTTPData : public RGWHTTPClient {
bufferlist *bl;
std::string post_data;
size_t post_data_index;
public:
RGWPostHTTPData(CephContext *_cct, bufferlist *_bl) : RGWHTTPClient(_cct), bl(_bl), post_data_index(0) {}
void set_post_data(const std::string& _post_data) {
this->post_data = _post_data;
}
int send_data(void* ptr, size_t len) {
int length_to_copy = 0;
if (post_data_index < post_data.length()) {
length_to_copy = min(post_data.length() - post_data_index, len);
memcpy(ptr, post_data.data() + post_data_index, length_to_copy);
post_data_index += length_to_copy;
}
return length_to_copy;
}
int receive_data(void *ptr, size_t len) {
bl->append((char *)ptr, len);
return 0;
}
int receive_header(void *ptr, size_t len) {
return 0;
}
};
typedef RGWPostHTTPData RGWGetKeystoneAdminToken;
typedef RGWPostHTTPData RGWGetRevokedTokens;
class RGWValidateKeystoneToken : public RGWHTTPClient {
bufferlist *bl;
public:
RGWValidateKeystoneToken(CephContext *_cct, bufferlist *_bl) : RGWHTTPClient(_cct), bl(_bl) {}
int receive_data(void *ptr, size_t len) {
bl->append((char *)ptr, len);
return 0;
}
int receive_header(void *ptr, size_t len) {
return 0;
}
int send_data(void *ptr, size_t len) {
return 0;
}
};
static RGWKeystoneTokenCache *keystone_token_cache = NULL;
static int open_cms_envelope(CephContext *cct, string& src, string& dst)
{
#define BEGIN_CMS "-----BEGIN CMS-----"
#define END_CMS "-----END CMS-----"
int start = src.find(BEGIN_CMS);
if (start < 0) {
ldout(cct, 0) << "failed to find " << BEGIN_CMS << " in response" << dendl;
return -EINVAL;
}
start += sizeof(BEGIN_CMS) - 1;
int end = src.find(END_CMS);
if (end < 0) {
ldout(cct, 0) << "failed to find " << END_CMS << " in response" << dendl;
return -EINVAL;
}
string s = src.substr(start, end - start);
int pos = 0;
do {
int next = s.find('\n', pos);
if (next < 0) {
dst.append(s.substr(pos));
break;
} else {
dst.append(s.substr(pos, next - pos));
}
pos = next + 1;
} while (pos < (int)s.size());
return 0;
}
static int decode_b64_cms(CephContext *cct, const string& signed_b64, bufferlist& bl)
{
bufferptr signed_ber(signed_b64.size() * 2);
char *dest = signed_ber.c_str();
const char *src = signed_b64.c_str();
size_t len = signed_b64.size();
char buf[len + 1];
buf[len] = '\0';
for (size_t i = 0; i < len; i++, src++) {
if (*src != '-')
buf[i] = *src;
else
buf[i] = '/';
}
int ret = ceph_unarmor(dest, dest + signed_ber.length(), buf, buf + signed_b64.size());
if (ret < 0) {
ldout(cct, 0) << "ceph_unarmor() failed, ret=" << ret << dendl;
return ret;
}
bufferlist signed_ber_bl;
signed_ber_bl.append(signed_ber);
ret = ceph_decode_cms(cct, signed_ber_bl, bl);
if (ret < 0) {
ldout(cct, 0) << "ceph_decode_cms returned " << ret << dendl;
return ret;
}
return 0;
}
int RGWSwift::get_keystone_url(std::string& url)
{
bufferlist bl;
RGWGetRevokedTokens req(cct, &bl);
url = cct->_conf->rgw_keystone_url;
if (url.empty()) {
ldout(cct, 0) << "ERROR: keystone url is not configured" << dendl;
return -EINVAL;
}
if (url[url.size() - 1] != '/')
url.append("/");
return 0;
}
int RGWSwift::get_keystone_admin_token(std::string& token)
{
std::string token_url;
if (get_keystone_url(token_url) < 0)
return -EINVAL;
if (cct->_conf->rgw_keystone_admin_token.empty()) {
token_url.append("v2.0/tokens");
KeystoneToken t;
bufferlist token_bl;
RGWGetKeystoneAdminToken token_req(cct, &token_bl);
JSONFormatter jf;
jf.open_object_section("auth");
jf.open_object_section("passwordCredentials");
encode_json("username", cct->_conf->rgw_keystone_admin_user, &jf);
encode_json("password", cct->_conf->rgw_keystone_admin_password, &jf);
jf.close_section();
encode_json("tenantName", cct->_conf->rgw_keystone_admin_tenant, &jf);
jf.close_section();
std::stringstream ss;
jf.flush(ss);
token_req.set_post_data(ss.str());
int ret = token_req.process(token_url.c_str());
if (ret < 0)
return ret;
if (t.parse(cct, token_bl) != 0)
return -EINVAL;
token = t.token.id;
} else {
token = cct->_conf->rgw_keystone_admin_token;
}
return 0;
}
int RGWSwift::check_revoked()
{
string url;
string token;
bufferlist bl;
RGWGetRevokedTokens req(cct, &bl);
if (get_keystone_admin_token(token) < 0)
return -EINVAL;
if (get_keystone_url(url) < 0)
return -EINVAL;
url.append("v2.0/tokens/revoked");
req.append_header("X-Auth-Token", token);
int ret = req.process(url.c_str());
if (ret < 0)
return ret;
bl.append((char)0); // NULL terminate for debug output
ldout(cct, 10) << "request returned " << bl.c_str() << dendl;
JSONParser parser;
if (!parser.parse(bl.c_str(), bl.length())) {
ldout(cct, 0) << "malformed json" << dendl;
return -EINVAL;
}
JSONObjIter iter = parser.find_first("signed");
if (iter.end()) {
ldout(cct, 0) << "revoked tokens response is missing signed section" << dendl;
return -EINVAL;
}
JSONObj *signed_obj = *iter;
string signed_str = signed_obj->get_data();
ldout(cct, 10) << "signed=" << signed_str << dendl;
string signed_b64;
ret = open_cms_envelope(cct, signed_str, signed_b64);
if (ret < 0)
return ret;
ldout(cct, 10) << "content=" << signed_b64 << dendl;
bufferlist json;
ret = decode_b64_cms(cct, signed_b64, json);
if (ret < 0) {
return ret;
}
ldout(cct, 10) << "ceph_decode_cms: decoded: " << json.c_str() << dendl;
JSONParser list_parser;
if (!list_parser.parse(json.c_str(), json.length())) {
ldout(cct, 0) << "malformed json" << dendl;
return -EINVAL;
}
JSONObjIter revoked_iter = list_parser.find_first("revoked");
if (revoked_iter.end()) {
ldout(cct, 0) << "no revoked section in json" << dendl;
return -EINVAL;
}
JSONObj *revoked_obj = *revoked_iter;
JSONObjIter tokens_iter = revoked_obj->find_first();
for (; !tokens_iter.end(); ++tokens_iter) {
JSONObj *o = *tokens_iter;
JSONObj *token = o->find_obj("id");
if (!token) {
ldout(cct, 0) << "bad token in array, missing id" << dendl;
continue;
}
string token_id = token->get_data();
keystone_token_cache->invalidate(token_id);
}
return 0;
}
static void rgw_set_keystone_token_auth_info(KeystoneToken& token, struct rgw_swift_auth_info *info)
{
info->user = token.token.tenant.id;
info->display_name = token.token.tenant.name;
info->status = 200;
}
int RGWSwift::parse_keystone_token_response(const string& token, bufferlist& bl, struct rgw_swift_auth_info *info, KeystoneToken& t)
{
int ret = t.parse(cct, bl);
if (ret < 0)
return ret;
bool found = false;
list<string>::iterator iter;
for (iter = roles_list.begin(); iter != roles_list.end(); ++iter) {
const string& role = *iter;
if ((found=t.user.has_role(role))==true)
break;
}
if (!found) {
ldout(cct, 0) << "user does not hold a matching role; required roles: " << g_conf->rgw_keystone_accepted_roles << dendl;
return -EPERM;
}
ldout(cct, 0) << "validated token: " << t.token.tenant.name << ":" << t.user.name << " expires: " << t.token.expires << dendl;
rgw_set_keystone_token_auth_info(t, info);
return 0;
}
int RGWSwift::update_user_info(RGWRados *store, struct rgw_swift_auth_info *info, RGWUserInfo& user_info)
{
if (rgw_get_user_info_by_uid(store, info->user, user_info) < 0) {
ldout(cct, 0) << "NOTICE: couldn't map swift user" << dendl;
user_info.user_id = info->user;
user_info.display_name = info->display_name;
int ret = rgw_store_user_info(store, user_info, NULL, NULL, 0, true);
if (ret < 0) {
ldout(cct, 0) << "ERROR: failed to store new user's info: ret=" << ret << dendl;
return ret;
}
}
return 0;
}
#define PKI_ANS1_PREFIX "MII"
static bool is_pki_token(const string& token)
{
return token.compare(0, sizeof(PKI_ANS1_PREFIX) - 1, PKI_ANS1_PREFIX) == 0;
}
static void get_token_id(const string& token, string& token_id)
{
if (!is_pki_token(token)) {
token_id = token;
return;
}
unsigned char m[CEPH_CRYPTO_MD5_DIGESTSIZE];
MD5 hash;
hash.Update((const byte *)token.c_str(), token.size());
hash.Final(m);
char calc_md5[CEPH_CRYPTO_MD5_DIGESTSIZE * 2 + 1];
buf_to_hex(m, CEPH_CRYPTO_MD5_DIGESTSIZE, calc_md5);
token_id = calc_md5;
}
static bool decode_pki_token(CephContext *cct, const string& token, bufferlist& bl)
{
if (!is_pki_token(token))
return false;
int ret = decode_b64_cms(cct, token, bl);
if (ret < 0)
return false;
ldout(cct, 20) << "successfully decoded pki token" << dendl;
return true;
}
int RGWSwift::validate_keystone_token(RGWRados *store, const string& token, struct rgw_swift_auth_info *info,
RGWUserInfo& rgw_user)
{
KeystoneToken t;
string token_id;
get_token_id(token, token_id);
ldout(cct, 20) << "token_id=" << token_id << dendl;
/* check cache first */
if (keystone_token_cache->find(token_id, t)) {
rgw_set_keystone_token_auth_info(t, info);
ldout(cct, 20) << "cached token.tenant.id=" << t.token.tenant.id << dendl;
int ret = update_user_info(store, info, rgw_user);
if (ret < 0)
return ret;
return 0;
}
bufferlist bl;
/* check if that's a self signed token that we can decode */
if (!decode_pki_token(cct, token, bl)) {
/* can't decode, just go to the keystone server for validation */
RGWValidateKeystoneToken validate(cct, &bl);
string url = g_conf->rgw_keystone_url;
if (url.empty()) {
ldout(cct, 0) << "ERROR: keystone url is not configured" << dendl;
return -EINVAL;
}
if (url[url.size() - 1] != '/')
url.append("/");
std::string admin_token;
if (get_keystone_admin_token(admin_token) < 0)
return -EINVAL;
if (get_keystone_url(url) < 0)
return -EINVAL;
url.append("v2.0/tokens/");
url.append(token);
validate.append_header("X-Auth-Token", admin_token);
int ret = validate.process(url.c_str());
if (ret < 0)
return ret;
}
bl.append((char)0); // NULL terminate for debug output
ldout(cct, 20) << "received response: " << bl.c_str() << dendl;
int ret = parse_keystone_token_response(token, bl, info, t);
if (ret < 0)
return ret;
ret = update_user_info(store, info, rgw_user);
if (ret < 0)
return ret;
return 0;
}
bool RGWSwift::verify_swift_token(RGWRados *store, req_state *s)
{
if (!s->os_auth_token)
return false;
if (strncmp(s->os_auth_token, "AUTH_rgwtk", 10) == 0) {
int ret = rgw_swift_verify_signed_token(s->cct, store, s->os_auth_token, s->user);
if (ret < 0)
return false;
return true;
}
struct rgw_swift_auth_info info;
info.status = 401; // start with access denied, validate_token might change that
int ret;
if (supports_keystone()) {
ret = validate_keystone_token(store, s->os_auth_token, &info, s->user);
return (ret >= 0);
}
ret = validate_token(s->os_auth_token, &info);
if (ret < 0)
return false;
if (info.user.empty()) {
ldout(cct, 5) << "swift auth didn't authorize a user" << dendl;
return false;
}
s->swift_user = info.user;
s->swift_groups = info.auth_groups;
string swift_user = s->swift_user;
ldout(cct, 10) << "swift user=" << s->swift_user << dendl;
if (rgw_get_user_info_by_swift(store, swift_user, s->user) < 0) {
ldout(cct, 0) << "NOTICE: couldn't map swift user" << dendl;
return false;
}
ldout(cct, 10) << "user_id=" << s->user.user_id << dendl;
return true;
}
void RGWSwift::init()
{
get_str_list(cct->_conf->rgw_keystone_accepted_roles, roles_list);
if (supports_keystone())
init_keystone();
}
void RGWSwift::init_keystone()
{
keystone_token_cache = new RGWKeystoneTokenCache(cct, cct->_conf->rgw_keystone_token_cache_size);
keystone_revoke_thread = new KeystoneRevokeThread(cct, this);
keystone_revoke_thread->create();
}
void RGWSwift::finalize()
{
if (supports_keystone())
finalize_keystone();
}
void RGWSwift::finalize_keystone()
{
delete keystone_token_cache;
keystone_token_cache = NULL;
down_flag.set(1);
if (keystone_revoke_thread) {
keystone_revoke_thread->stop();
keystone_revoke_thread->join();
}
delete keystone_revoke_thread;
keystone_revoke_thread = NULL;
}
RGWSwift *rgw_swift = NULL;
void swift_init(CephContext *cct)
{
rgw_swift = new RGWSwift(cct);
}
void swift_finalize()
{
delete rgw_swift;
}
bool RGWSwift::going_down()
{
return (down_flag.read() != 0);
}
void *RGWSwift::KeystoneRevokeThread::entry() {
do {
dout(2) << "keystone revoke thread: start" << dendl;
int r = swift->check_revoked();
if (r < 0) {
dout(0) << "ERROR: keystone revocation processing returned error r=" << r << dendl;
}
if (swift->going_down())
break;
lock.Lock();
cond.WaitInterval(cct, lock, utime_t(cct->_conf->rgw_keystone_revocation_interval, 0));
lock.Unlock();
} while (!swift->going_down());
return NULL;
}
void RGWSwift::KeystoneRevokeThread::stop()
{
Mutex::Locker l(lock);
cond.Signal();
}