forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_service_resource.cpp
3817 lines (3550 loc) · 154 KB
/
meta_service_resource.cpp
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include <brpc/channel.h>
#include <butil/guid.h>
#include <fmt/core.h>
#include <gen_cpp/cloud.pb.h>
#include <algorithm>
#include <charconv>
#include <chrono>
#include <numeric>
#include <regex>
#include <string>
#include <tuple>
#include "common/encryption_util.h"
#include "common/logging.h"
#include "common/network_util.h"
#include "common/string_util.h"
#include "cpp/sync_point.h"
#include "meta-service/keys.h"
#include "meta-service/meta_service.h"
#include "meta-service/meta_service_helper.h"
#include "meta-service/txn_kv.h"
#include "meta-service/txn_kv_error.h"
using namespace std::chrono;
namespace {
constexpr char pattern_str[] = "^[a-zA-Z][0-9a-zA-Z_]*$";
bool is_valid_storage_vault_name(const std::string& str) {
const std::regex pattern(pattern_str);
return std::regex_match(str, pattern);
}
} // namespace
namespace doris::cloud {
static void* run_bthread_work(void* arg) {
auto f = reinterpret_cast<std::function<void()>*>(arg);
(*f)();
delete f;
return nullptr;
}
static std::string_view print_cluster_status(const ClusterStatus& status) {
switch (status) {
case ClusterStatus::UNKNOWN:
return "UNKNOWN";
case ClusterStatus::NORMAL:
return "NORMAL";
case ClusterStatus::SUSPENDED:
return "SUSPENDED";
case ClusterStatus::TO_RESUME:
return "TO_RESUME";
case ClusterStatus::MANUAL_SHUTDOWN:
return "MANUAL_SHUTDOWN";
default:
return "UNKNOWN";
}
}
static int encrypt_ak_sk_helper(const std::string plain_ak, const std::string plain_sk,
EncryptionInfoPB* encryption_info, AkSkPair* cipher_ak_sk_pair,
MetaServiceCode& code, std::string& msg) {
std::string key;
int64_t key_id;
LOG_INFO("enter encrypt_ak_sk_helper, plain_ak {}", plain_ak);
int ret = get_newest_encryption_key_for_ak_sk(&key_id, &key);
TEST_SYNC_POINT_CALLBACK("encrypt_ak_sk:get_encryption_key", &ret, &key, &key_id);
if (ret != 0) {
msg = "failed to get encryption key";
code = MetaServiceCode::ERR_ENCRYPT;
LOG(WARNING) << msg;
return -1;
}
auto& encryption_method = get_encryption_method_for_ak_sk();
AkSkPair plain_ak_sk_pair {plain_ak, plain_sk};
ret = encrypt_ak_sk(plain_ak_sk_pair, encryption_method, key, cipher_ak_sk_pair);
if (ret != 0) {
msg = "failed to encrypt";
code = MetaServiceCode::ERR_ENCRYPT;
LOG(WARNING) << msg;
return -1;
}
encryption_info->set_key_id(key_id);
encryption_info->set_encryption_method(encryption_method);
return 0;
}
static int decrypt_ak_sk_helper(std::string_view cipher_ak, std::string_view cipher_sk,
const EncryptionInfoPB& encryption_info, AkSkPair* plain_ak_sk_pair,
MetaServiceCode& code, std::string& msg) {
int ret = decrypt_ak_sk_helper(cipher_ak, cipher_sk, encryption_info, plain_ak_sk_pair);
if (ret != 0) {
msg = "failed to decrypt";
code = MetaServiceCode::ERR_DECPYPT;
}
return ret;
}
int decrypt_instance_info(InstanceInfoPB& instance, const std::string& instance_id,
MetaServiceCode& code, std::string& msg,
std::shared_ptr<Transaction>& txn) {
for (auto& obj_info : *instance.mutable_obj_info()) {
if (obj_info.has_encryption_info()) {
AkSkPair plain_ak_sk_pair;
int ret = decrypt_ak_sk_helper(obj_info.ak(), obj_info.sk(), obj_info.encryption_info(),
&plain_ak_sk_pair, code, msg);
if (ret != 0) return -1;
obj_info.set_ak(std::move(plain_ak_sk_pair.first));
obj_info.set_sk(std::move(plain_ak_sk_pair.second));
}
}
if (instance.has_ram_user() && instance.ram_user().has_encryption_info()) {
auto& ram_user = *instance.mutable_ram_user();
AkSkPair plain_ak_sk_pair;
int ret = decrypt_ak_sk_helper(ram_user.ak(), ram_user.sk(), ram_user.encryption_info(),
&plain_ak_sk_pair, code, msg);
if (ret != 0) return -1;
ram_user.set_ak(std::move(plain_ak_sk_pair.first));
ram_user.set_sk(std::move(plain_ak_sk_pair.second));
}
std::string val;
TxnErrorCode err = txn->get(system_meta_service_arn_info_key(), &val);
if (err == TxnErrorCode::TXN_KEY_NOT_FOUND) {
// For compatibility, use arn_info of config
RamUserPB iam_user;
iam_user.set_user_id(config::arn_id);
iam_user.set_external_id(instance_id);
iam_user.set_ak(config::arn_ak);
iam_user.set_sk(config::arn_sk);
instance.mutable_iam_user()->CopyFrom(iam_user);
} else if (err == TxnErrorCode::TXN_OK) {
RamUserPB iam_user;
if (!iam_user.ParseFromString(val)) {
code = MetaServiceCode::PROTOBUF_PARSE_ERR;
msg = "failed to parse RamUserPB";
LOG(WARNING) << msg;
return -1;
}
AkSkPair plain_ak_sk_pair;
int ret = decrypt_ak_sk_helper(iam_user.ak(), iam_user.sk(), iam_user.encryption_info(),
&plain_ak_sk_pair, code, msg);
if (ret != 0) return -1;
iam_user.set_ak(std::move(plain_ak_sk_pair.first));
iam_user.set_sk(std::move(plain_ak_sk_pair.second));
instance.mutable_iam_user()->CopyFrom(iam_user);
} else {
code = cast_as<ErrCategory::READ>(err);
msg = fmt::format("failed to get arn_info_key, err={}", err);
LOG(WARNING) << msg;
return -1;
}
for (auto& stage : *instance.mutable_stages()) {
if (stage.has_obj_info() && stage.obj_info().has_encryption_info()) {
auto& obj_info = *stage.mutable_obj_info();
AkSkPair plain_ak_sk_pair;
int ret = decrypt_ak_sk_helper(obj_info.ak(), obj_info.sk(), obj_info.encryption_info(),
&plain_ak_sk_pair, code, msg);
if (ret != 0) return -1;
obj_info.set_ak(std::move(plain_ak_sk_pair.first));
obj_info.set_sk(std::move(plain_ak_sk_pair.second));
}
}
return 0;
}
static int decrypt_and_update_ak_sk(ObjectStoreInfoPB& obj_info, MetaServiceCode& code,
std::string& msg) {
if (obj_info.has_encryption_info()) {
AkSkPair plain_ak_sk_pair;
if (int ret = decrypt_ak_sk_helper(obj_info.ak(), obj_info.sk(), obj_info.encryption_info(),
&plain_ak_sk_pair, code, msg);
ret != 0) {
return ret;
}
obj_info.set_ak(std::move(plain_ak_sk_pair.first));
obj_info.set_sk(std::move(plain_ak_sk_pair.second));
}
return 0;
};
void MetaServiceImpl::get_obj_store_info(google::protobuf::RpcController* controller,
const GetObjStoreInfoRequest* request,
GetObjStoreInfoResponse* response,
::google::protobuf::Closure* done) {
RPC_PREPROCESS(get_obj_store_info);
// Prepare data
std::string cloud_unique_id = request->has_cloud_unique_id() ? request->cloud_unique_id() : "";
if (cloud_unique_id.empty()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "cloud unique id not set";
return;
}
instance_id = get_instance_id(resource_mgr_, cloud_unique_id);
if (instance_id.empty()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "empty instance_id";
LOG(INFO) << msg << ", cloud_unique_id=" << cloud_unique_id;
return;
}
RPC_RATE_LIMIT(get_obj_store_info)
InstanceKeyInfo key_info {instance_id};
std::string key;
std::string val;
instance_key(key_info, &key);
std::unique_ptr<Transaction> txn;
TxnErrorCode err = txn_kv_->create_txn(&txn);
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::CREATE>(err);
msg = "failed to create txn";
LOG(WARNING) << msg << " err=" << err;
return;
}
err = txn->get(key, &val);
LOG(INFO) << "get instance_key=" << hex(key);
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::READ>(err);
ss << "failed to get instance, instance_id=" << instance_id << " err=" << err;
msg = ss.str();
return;
}
InstanceInfoPB instance;
if (!instance.ParseFromString(val)) {
code = MetaServiceCode::PROTOBUF_PARSE_ERR;
msg = "failed to parse InstanceInfoPB";
return;
}
for (auto& obj_info : *instance.mutable_obj_info()) {
if (auto ret = decrypt_and_update_ak_sk(obj_info, code, msg); ret != 0) {
return;
}
}
response->set_enable_storage_vault(instance.enable_storage_vault());
// Iterate all the resources to return to the rpc caller
if (!instance.resource_ids().empty()) {
std::string storage_vault_start = storage_vault_key({instance.instance_id(), ""});
std::string storage_vault_end = storage_vault_key({instance.instance_id(), "\xff"});
std::unique_ptr<RangeGetIterator> it;
do {
TxnErrorCode err = txn->get(storage_vault_start, storage_vault_end, &it);
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::READ>(err);
msg = fmt::format("internal error, failed to get storage vault, err={}", err);
LOG(WARNING) << msg;
return;
}
while (it->has_next()) {
auto [k, v] = it->next();
auto* vault = response->add_storage_vault();
if (!vault->ParseFromArray(v.data(), v.size())) {
code = MetaServiceCode::PROTOBUF_PARSE_ERR;
msg = fmt::format("malformed storage vault, unable to deserialize key={}",
hex(k));
LOG(WARNING) << msg << " key=" << hex(k);
return;
}
if (!it->has_next()) {
storage_vault_start = k;
}
}
storage_vault_start.push_back('\x00'); // Update to next smallest key for iteration
} while (it->more());
}
for (auto& vault : *response->mutable_storage_vault()) {
if (vault.has_obj_info()) {
if (auto ret = decrypt_and_update_ak_sk(*vault.mutable_obj_info(), code, msg);
ret != 0) {
return;
}
}
}
response->mutable_obj_info()->CopyFrom(instance.obj_info());
if (instance.has_default_storage_vault_id()) {
response->set_default_storage_vault_id(instance.default_storage_vault_id());
response->set_default_storage_vault_name(instance.default_storage_vault_name());
}
}
// The next available vault id would be max(max(obj info id), max(vault id)) + 1.
static std::string next_available_vault_id(const InstanceInfoPB& instance) {
int vault_id = 0;
auto max = [](int prev, const auto& last) {
int last_id = 0;
std::string_view value = "0";
if constexpr (std::is_same_v<std::decay_t<decltype(last)>, ObjectStoreInfoPB>) {
value = last.id();
} else if constexpr (std::is_same_v<std::decay_t<decltype(last)>, std::string>) {
value = last;
}
if (auto [_, ec] = std::from_chars(value.data(), value.data() + value.size(), last_id);
ec != std::errc {}) [[unlikely]] {
LOG_WARNING("Invalid resource id format: {}", value);
last_id = 0;
DCHECK(false);
}
return std::max(prev, last_id);
};
auto prev = std::accumulate(
instance.resource_ids().begin(), instance.resource_ids().end(),
std::accumulate(instance.obj_info().begin(), instance.obj_info().end(), vault_id, max),
max);
return std::to_string(prev + 1);
}
namespace detail {
// Validate and normalize hdfs prefix. Return true if prefix is valid.
bool normalize_hdfs_prefix(std::string& prefix) {
if (prefix.empty()) {
return true;
}
if (prefix.find("://") != std::string::npos) {
// Should not contain scheme
return false;
}
trim(prefix);
return true;
}
// Validate and normalize hdfs fs_name. Return true if fs_name is valid.
bool normalize_hdfs_fs_name(std::string& fs_name) {
if (fs_name.empty()) {
return false;
}
// Should check scheme existence?
trim(fs_name);
return !fs_name.empty();
}
} // namespace detail
static int add_hdfs_storage_vault(InstanceInfoPB& instance, Transaction* txn,
StorageVaultPB& hdfs_param, MetaServiceCode& code,
std::string& msg) {
if (!hdfs_param.has_hdfs_info()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = fmt::format("vault_name={} passed invalid argument", hdfs_param.name());
return -1;
}
using namespace detail;
// Check and normalize hdfs conf
auto* prefix = hdfs_param.mutable_hdfs_info()->mutable_prefix();
if (!normalize_hdfs_prefix(*prefix)) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = fmt::format("invalid prefix: {}", *prefix);
return -1;
}
if (config::enable_distinguish_hdfs_path) {
auto uuid_suffix = butil::GenerateGUID();
if (uuid_suffix.empty()) [[unlikely]] {
code = MetaServiceCode::UNDEFINED_ERR;
msg = fmt::format("failed to generate one suffix for hdfs prefix: {}", *prefix);
return -1;
}
*prefix = fmt::format("{}_{}", *prefix, uuid_suffix);
}
auto* fs_name = hdfs_param.mutable_hdfs_info()->mutable_build_conf()->mutable_fs_name();
if (!normalize_hdfs_fs_name(*fs_name)) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = fmt::format("invalid fs_name: {}", *fs_name);
return -1;
}
std::string key;
std::string vault_id = next_available_vault_id(instance);
storage_vault_key({instance.instance_id(), vault_id}, &key);
hdfs_param.set_id(vault_id);
std::string val = hdfs_param.SerializeAsString();
txn->put(key, val);
LOG_INFO("try to put storage vault_id={}, vault_name={}, vault_key={}", vault_id,
hdfs_param.name(), hex(key));
instance.mutable_resource_ids()->Add(std::move(vault_id));
*instance.mutable_storage_vault_names()->Add() = hdfs_param.name();
return 0;
}
static void create_object_info_with_encrypt(const InstanceInfoPB& instance, ObjectStoreInfoPB* obj,
bool sse_enabled, MetaServiceCode& code,
std::string& msg) {
std::string plain_ak = obj->has_ak() ? obj->ak() : "";
std::string plain_sk = obj->has_sk() ? obj->sk() : "";
std::string bucket = obj->has_bucket() ? obj->bucket() : "";
std::string prefix = obj->has_prefix() ? obj->prefix() : "";
// format prefix, such as `/aa/bb/`, `aa/bb//`, `//aa/bb`, ` /aa/bb` -> `aa/bb`
trim(prefix);
std::string endpoint = obj->has_endpoint() ? obj->endpoint() : "";
std::string external_endpoint = obj->has_external_endpoint() ? obj->external_endpoint() : "";
std::string region = obj->has_region() ? obj->region() : "";
// ATTN: prefix may be empty
if (plain_ak.empty() || plain_sk.empty() || bucket.empty() || endpoint.empty() ||
region.empty() || !obj->has_provider() || external_endpoint.empty()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "s3 conf info err, please check it";
return;
}
EncryptionInfoPB encryption_info;
AkSkPair cipher_ak_sk_pair;
auto ret = encrypt_ak_sk_helper(plain_ak, plain_sk, &encryption_info, &cipher_ak_sk_pair, code,
msg);
TEST_SYNC_POINT_CALLBACK("create_object_info_with_encrypt", &ret, &code, &msg);
if (ret != 0) {
return;
}
obj->set_ak(std::move(cipher_ak_sk_pair.first));
obj->set_sk(std::move(cipher_ak_sk_pair.second));
obj->mutable_encryption_info()->CopyFrom(encryption_info);
obj->set_bucket(bucket);
obj->set_prefix(prefix);
obj->set_endpoint(endpoint);
obj->set_external_endpoint(external_endpoint);
obj->set_region(region);
obj->set_id(next_available_vault_id(instance));
auto now_time = std::chrono::system_clock::now();
uint64_t time =
std::chrono::duration_cast<std::chrono::seconds>(now_time.time_since_epoch()).count();
obj->set_ctime(time);
obj->set_mtime(time);
obj->set_sse_enabled(sse_enabled);
}
static int add_vault_into_instance(InstanceInfoPB& instance, Transaction* txn,
StorageVaultPB& vault_param, MetaServiceCode& code,
std::string& msg) {
if (std::find_if(instance.storage_vault_names().begin(), instance.storage_vault_names().end(),
[&vault_param](const auto& name) { return name == vault_param.name(); }) !=
instance.storage_vault_names().end()) {
code = MetaServiceCode::ALREADY_EXISTED;
msg = fmt::format("vault_name={} already created", vault_param.name());
return -1;
}
if (vault_param.has_hdfs_info()) {
return add_hdfs_storage_vault(instance, txn, vault_param, code, msg);
}
create_object_info_with_encrypt(instance, vault_param.mutable_obj_info(), true, code, msg);
if (code != MetaServiceCode::OK) {
return -1;
}
vault_param.mutable_obj_info()->CopyFrom(vault_param.obj_info());
vault_param.set_id(vault_param.obj_info().id());
auto vault_key = storage_vault_key({instance.instance_id(), vault_param.obj_info().id()});
*instance.mutable_resource_ids()->Add() = vault_param.id();
*instance.mutable_storage_vault_names()->Add() = vault_param.name();
LOG_INFO("try to put storage vault_id={}, vault_name={}, vault_key={}", vault_param.id(),
vault_param.name(), hex(vault_key));
txn->put(vault_key, vault_param.SerializeAsString());
return 0;
}
static int remove_hdfs_storage_vault(InstanceInfoPB& instance, Transaction* txn,
const StorageVaultPB& hdfs_info, MetaServiceCode& code,
std::string& msg) {
std::string_view vault_name = hdfs_info.name();
auto name_iter = std::find_if(instance.storage_vault_names().begin(),
instance.storage_vault_names().end(),
[&](const auto& name) { return vault_name == name; });
if (name_iter == instance.storage_vault_names().end()) {
code = MetaServiceCode::STORAGE_VAULT_NOT_FOUND;
msg = fmt::format("vault_name={} not found", vault_name);
return -1;
}
auto vault_idx = name_iter - instance.storage_vault_names().begin();
auto vault_id_iter = instance.resource_ids().begin() + vault_idx;
std::string_view vault_id = *vault_id_iter;
std::string vault_key = storage_vault_key({instance.instance_id(), vault_id});
txn->remove(vault_key);
instance.mutable_storage_vault_names()->DeleteSubrange(vault_idx, 1);
instance.mutable_resource_ids()->DeleteSubrange(vault_idx, 1);
LOG(INFO) << "remove storage_vault_key=" << hex(vault_key);
return 0;
}
// Log vault message and origin default storage vault message for potential tracing
static void set_default_vault_log_helper(const InstanceInfoPB& instance,
std::string_view vault_name, std::string_view vault_id) {
auto vault_msg = fmt::format("instance {} tries to set default vault as {}, id {}",
instance.instance_id(), vault_id, vault_name);
if (instance.has_default_storage_vault_id()) {
vault_msg = fmt::format("{}, origin default vault name {}, vault id {}", vault_msg,
instance.default_storage_vault_name(),
instance.default_storage_vault_id());
}
LOG(INFO) << vault_msg;
}
static int alter_hdfs_storage_vault(InstanceInfoPB& instance, std::unique_ptr<Transaction> txn,
const StorageVaultPB& vault, MetaServiceCode& code,
std::string& msg) {
if (!vault.has_hdfs_info()) {
code = MetaServiceCode::INVALID_ARGUMENT;
std::stringstream ss;
ss << "There is no hdfs vault provided";
msg = ss.str();
return -1;
}
const auto& hdfs_info = vault.hdfs_info();
if (hdfs_info.has_prefix() || !hdfs_info.has_build_conf() ||
hdfs_info.build_conf().has_fs_name()) {
code = MetaServiceCode::INVALID_ARGUMENT;
std::stringstream ss;
ss << "You can not alter prefix or fs name because it might lose previoud written data";
msg = ss.str();
return -1;
}
const auto& name = vault.name();
// Here we try to get mutable iter since we might need to alter the vault name
auto name_itr = std::find_if(instance.mutable_storage_vault_names()->begin(),
instance.mutable_storage_vault_names()->end(),
[&](const auto& vault_name) { return name == vault_name; });
if (name_itr == instance.storage_vault_names().end()) {
code = MetaServiceCode::INVALID_ARGUMENT;
std::stringstream ss;
ss << "invalid storage vault name, not found, name =" << name;
msg = ss.str();
return -1;
}
auto pos = name_itr - instance.storage_vault_names().begin();
std::string vault_id = instance.resource_ids().begin()[pos];
auto vault_key = storage_vault_key({instance.instance_id(), vault_id});
std::string val;
auto err = txn->get(vault_key, &val);
LOG(INFO) << "get vault_key=" << hex(vault_key);
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::READ>(err);
std::stringstream ss;
ss << "failed to get storage vault, vault_id=" << vault_id << ", vault_name="
<< "" << name << " err=" << err;
msg = ss.str();
return -1;
}
StorageVaultPB new_vault;
new_vault.ParseFromString(val);
auto origin_vault_info = new_vault.DebugString();
if (vault.has_alter_name()) {
if (!is_valid_storage_vault_name(vault.alter_name())) {
code = MetaServiceCode::INVALID_ARGUMENT;
std::stringstream ss;
ss << "invalid storage vault name =" << vault.alter_name() << " the name must satisfy "
<< pattern_str;
msg = ss.str();
return -1;
}
new_vault.set_name(vault.alter_name());
*name_itr = vault.alter_name();
}
auto* alter_hdfs_info = new_vault.mutable_hdfs_info();
if (hdfs_info.build_conf().has_hdfs_kerberos_keytab()) {
alter_hdfs_info->mutable_build_conf()->set_hdfs_kerberos_keytab(
hdfs_info.build_conf().hdfs_kerberos_keytab());
}
if (hdfs_info.build_conf().has_hdfs_kerberos_principal()) {
alter_hdfs_info->mutable_build_conf()->set_hdfs_kerberos_principal(
hdfs_info.build_conf().hdfs_kerberos_principal());
}
if (hdfs_info.build_conf().has_user()) {
alter_hdfs_info->mutable_build_conf()->set_user(hdfs_info.build_conf().user());
}
if (0 != hdfs_info.build_conf().hdfs_confs_size()) {
alter_hdfs_info->mutable_build_conf()->mutable_hdfs_confs()->Add(
hdfs_info.build_conf().hdfs_confs().begin(),
hdfs_info.build_conf().hdfs_confs().end());
}
auto new_vault_info = new_vault.DebugString();
val = new_vault.SerializeAsString();
if (val.empty()) {
msg = "failed to serialize";
code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR;
return -1;
}
txn->put(vault_key, val);
LOG(INFO) << "put vault_id=" << vault_id << ", vault_key=" << hex(vault_key)
<< ", origin vault=" << origin_vault_info << ", new_vault=" << new_vault_info;
err = txn->commit();
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::COMMIT>(err);
msg = fmt::format("failed to commit kv txn, err={}", err);
LOG(WARNING) << msg;
}
return 0;
}
static int alter_s3_storage_vault(InstanceInfoPB& instance, std::unique_ptr<Transaction> txn,
const StorageVaultPB& vault, MetaServiceCode& code,
std::string& msg) {
if (!vault.has_obj_info()) {
code = MetaServiceCode::INVALID_ARGUMENT;
std::stringstream ss;
ss << "There is no s3 vault provided";
msg = ss.str();
return -1;
}
const auto& obj_info = vault.obj_info();
if (obj_info.has_bucket() || obj_info.has_endpoint() || obj_info.has_prefix() ||
obj_info.has_provider()) {
code = MetaServiceCode::INVALID_ARGUMENT;
std::stringstream ss;
ss << "Only ak, sk can be altered";
msg = ss.str();
return -1;
}
const auto& name = vault.name();
// Here we try to get mutable iter since we might need to alter the vault name
auto name_itr = std::find_if(instance.mutable_storage_vault_names()->begin(),
instance.mutable_storage_vault_names()->end(),
[&](const auto& vault_name) { return name == vault_name; });
if (name_itr == instance.storage_vault_names().end()) {
code = MetaServiceCode::INVALID_ARGUMENT;
std::stringstream ss;
ss << "invalid storage vault name, not found, name =" << name;
msg = ss.str();
return -1;
}
auto pos = name_itr - instance.storage_vault_names().begin();
std::string vault_id = instance.resource_ids().begin()[pos];
auto vault_key = storage_vault_key({instance.instance_id(), vault_id});
std::string val;
auto err = txn->get(vault_key, &val);
LOG(INFO) << "get vault_key=" << hex(vault_key);
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::READ>(err);
std::stringstream ss;
ss << "failed to get storage vault, vault_id=" << vault_id << ", vault_name="
<< "" << name << " err=" << err;
msg = ss.str();
return -1;
}
StorageVaultPB new_vault;
new_vault.ParseFromString(val);
if (vault.has_alter_name()) {
if (!is_valid_storage_vault_name(vault.alter_name())) {
code = MetaServiceCode::INVALID_ARGUMENT;
std::stringstream ss;
ss << "invalid storage vault name =" << vault.alter_name() << " the name must satisfy "
<< pattern_str;
msg = ss.str();
return -1;
}
new_vault.set_name(vault.alter_name());
*name_itr = vault.alter_name();
}
auto origin_vault_info = new_vault.DebugString();
AkSkPair pre {new_vault.obj_info().ak(), new_vault.obj_info().sk()};
const auto& plain_ak = obj_info.has_ak() ? obj_info.ak() : new_vault.obj_info().ak();
const auto& plain_sk = obj_info.has_ak() ? obj_info.sk() : new_vault.obj_info().sk();
AkSkPair plain_ak_sk_pair {plain_ak, plain_sk};
AkSkPair cipher_ak_sk_pair;
EncryptionInfoPB encryption_info;
auto ret = encrypt_ak_sk_helper(plain_ak, plain_sk, &encryption_info, &cipher_ak_sk_pair, code,
msg);
if (ret != 0) {
msg = "failed to encrypt";
code = MetaServiceCode::ERR_ENCRYPT;
LOG(WARNING) << msg;
return -1;
}
new_vault.mutable_obj_info()->set_ak(cipher_ak_sk_pair.first);
new_vault.mutable_obj_info()->set_sk(cipher_ak_sk_pair.second);
new_vault.mutable_obj_info()->mutable_encryption_info()->CopyFrom(encryption_info);
if (obj_info.has_use_path_style()) {
new_vault.mutable_obj_info()->set_use_path_style(obj_info.use_path_style());
}
auto new_vault_info = new_vault.DebugString();
val = new_vault.SerializeAsString();
if (val.empty()) {
msg = "failed to serialize";
code = MetaServiceCode::PROTOBUF_SERIALIZE_ERR;
return -1;
}
txn->put(vault_key, val);
LOG(INFO) << "put vault_id=" << vault_id << ", vault_key=" << hex(vault_key)
<< ", origin vault=" << origin_vault_info << ", new vault=" << new_vault_info;
err = txn->commit();
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::COMMIT>(err);
msg = fmt::format("failed to commit kv txn, err={}", err);
LOG(WARNING) << msg;
}
return 0;
}
struct ObjectStorageDesc {
std::string& ak;
std::string& sk;
std::string& bucket;
std::string& prefix;
std::string& endpoint;
std::string& external_endpoint;
std::string& region;
};
static int extract_object_storage_info(const AlterObjStoreInfoRequest* request,
MetaServiceCode& code, std::string& msg,
ObjectStorageDesc& obj_desc,
EncryptionInfoPB& encryption_info,
AkSkPair& cipher_ak_sk_pair) {
if (!request->has_obj() && (!request->has_vault() || !request->vault().has_obj_info())) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "s3 obj info err " + proto_to_json(*request);
return -1;
}
auto& [ak, sk, bucket, prefix, endpoint, external_endpoint, region] = obj_desc;
const auto& obj = request->has_obj() ? request->obj() : request->vault().obj_info();
// Prepare data
if (!obj.has_ak() || !obj.has_sk()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "s3 obj info err " + proto_to_json(*request);
return -1;
}
std::string plain_ak = obj.has_ak() ? obj.ak() : "";
std::string plain_sk = obj.has_sk() ? obj.sk() : "";
auto ret = encrypt_ak_sk_helper(plain_ak, plain_sk, &encryption_info, &cipher_ak_sk_pair, code,
msg);
if (ret != 0) {
return -1;
}
TEST_SYNC_POINT_CALLBACK("extract_object_storage_info:get_aksk_pair", &cipher_ak_sk_pair);
ak = cipher_ak_sk_pair.first;
sk = cipher_ak_sk_pair.second;
bucket = obj.has_bucket() ? obj.bucket() : "";
prefix = obj.has_prefix() ? obj.prefix() : "";
endpoint = obj.has_endpoint() ? obj.endpoint() : "";
external_endpoint = obj.has_external_endpoint() ? obj.external_endpoint() : "";
region = obj.has_region() ? obj.region() : "";
// obj size > 1k, refuse
if (obj.ByteSizeLong() > 1024) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "s3 obj info greater than 1k " + proto_to_json(*request);
return -1;
};
return 0;
}
static ObjectStoreInfoPB object_info_pb_factory(ObjectStorageDesc& obj_info,
const ObjectStoreInfoPB& obj,
InstanceInfoPB& instance,
EncryptionInfoPB& encryption_info,
AkSkPair& cipher_ak_sk_pair) {
ObjectStoreInfoPB last_item;
auto& [ak, sk, bucket, prefix, endpoint, external_endpoint, region] = obj_info;
auto now_time = std::chrono::system_clock::now();
uint64_t time =
std::chrono::duration_cast<std::chrono::seconds>(now_time.time_since_epoch()).count();
last_item.set_ctime(time);
last_item.set_mtime(time);
last_item.set_id(next_available_vault_id(instance));
if (obj.has_user_id()) {
last_item.set_user_id(obj.user_id());
}
last_item.set_ak(std::move(cipher_ak_sk_pair.first));
last_item.set_sk(std::move(cipher_ak_sk_pair.second));
last_item.mutable_encryption_info()->CopyFrom(encryption_info);
last_item.set_bucket(bucket);
// format prefix, such as `/aa/bb/`, `aa/bb//`, `//aa/bb`, ` /aa/bb` -> `aa/bb`
trim(prefix);
last_item.set_prefix(prefix);
last_item.set_endpoint(endpoint);
last_item.set_external_endpoint(external_endpoint);
last_item.set_region(region);
last_item.set_provider(obj.provider());
last_item.set_sse_enabled(instance.sse_enabled());
return last_item;
}
void MetaServiceImpl::alter_storage_vault(google::protobuf::RpcController* controller,
const AlterObjStoreInfoRequest* request,
AlterObjStoreInfoResponse* response,
::google::protobuf::Closure* done) {
std::string ak, sk, bucket, prefix, endpoint, external_endpoint, region;
EncryptionInfoPB encryption_info;
AkSkPair cipher_ak_sk_pair;
RPC_PREPROCESS(alter_storage_vault);
switch (request->op()) {
case AlterObjStoreInfoRequest::ADD_S3_VAULT:
case AlterObjStoreInfoRequest::DROP_S3_VAULT: {
auto tmp_desc =
ObjectStorageDesc {ak, sk, bucket, prefix, endpoint, external_endpoint, region};
if (0 != extract_object_storage_info(request, code, msg, tmp_desc, encryption_info,
cipher_ak_sk_pair)) {
return;
}
} break;
case AlterObjStoreInfoRequest::ADD_BUILT_IN_VAULT: {
// It should at least has one hdfs info or obj info inside storage vault
if ((!request->has_vault())) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "storage vault is set " + proto_to_json(*request);
return;
}
break;
}
case AlterObjStoreInfoRequest::ADD_HDFS_INFO:
case AlterObjStoreInfoRequest::DROP_HDFS_INFO: {
if (!request->has_vault() || !request->vault().has_name()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "hdfs info is not found " + proto_to_json(*request);
return;
}
} break;
case AlterObjStoreInfoRequest::SET_DEFAULT_VAULT: {
if (!request->has_vault() || !request->vault().has_name()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "hdfs info is not found " + proto_to_json(*request);
return;
}
break;
}
case AlterObjStoreInfoRequest::ALTER_S3_VAULT:
break;
case AlterObjStoreInfoRequest::ALTER_HDFS_VAULT:
break;
case AlterObjStoreInfoRequest::UNSET_DEFAULT_VAULT:
break;
case AlterObjStoreInfoRequest::UNKNOWN: {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "Unknown alter info " + proto_to_json(*request);
return;
} break;
default:
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "Unknown alter obj store info, request info " + proto_to_json(*request);
LOG_WARNING("Unknown alter obj store info, request info {}", request->DebugString());
return;
}
// TODO(dx): check s3 info right
std::string cloud_unique_id = request->has_cloud_unique_id() ? request->cloud_unique_id() : "";
if (cloud_unique_id.empty()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "cloud unique id not set";
return;
}
instance_id = get_instance_id(resource_mgr_, cloud_unique_id);
if (instance_id.empty()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "empty instance_id";
LOG(INFO) << msg << ", cloud_unique_id=" << cloud_unique_id;
return;
}
RPC_RATE_LIMIT(alter_obj_store_info)
InstanceKeyInfo key_info {instance_id};
std::string key;
std::string val;
instance_key(key_info, &key);
std::unique_ptr<Transaction> txn;
TxnErrorCode err = txn_kv_->create_txn(&txn);
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::CREATE>(err);
msg = "failed to create txn";
LOG(WARNING) << msg << " err=" << err;
return;
}
err = txn->get(key, &val);
LOG(INFO) << "get instance_key=" << hex(key);
if (err != TxnErrorCode::TXN_OK) {
code = cast_as<ErrCategory::READ>(err);
ss << "failed to get instance, instance_id=" << instance_id << " err=" << err;
msg = ss.str();
return;
}
InstanceInfoPB instance;
if (!instance.ParseFromString(val)) {
code = MetaServiceCode::PROTOBUF_PARSE_ERR;
msg = "failed to parse InstanceInfoPB";
return;
}
if (instance.status() == InstanceInfoPB::DELETED) {
code = MetaServiceCode::CLUSTER_NOT_FOUND;
msg = "instance status has been set delete, plz check it";
return;
}
switch (request->op()) {
case AlterObjStoreInfoRequest::ADD_S3_VAULT: {
if (!instance.enable_storage_vault()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "Storage vault doesn't support storage vault";
return;
}
auto& obj = request->has_obj() ? request->obj() : request->vault().obj_info();
if (!obj.has_provider()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "s3 conf lease provider info";
return;
}
if (instance.obj_info().size() >= 10) {
code = MetaServiceCode::UNDEFINED_ERR;
msg = "this instance history has greater than 10 objs, please new another instance";
return;
}
// ATTN: prefix may be empty
if (ak.empty() || sk.empty() || bucket.empty() || endpoint.empty() || region.empty()) {
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "s3 conf info err, please check it";
return;
}
auto& objs = instance.obj_info();
for (auto& it : objs) {
if (bucket == it.bucket() && prefix == it.prefix() && endpoint == it.endpoint() &&
region == it.region() && ak == it.ak() && sk == it.sk() &&
obj.provider() == it.provider() && external_endpoint == it.external_endpoint()) {
// err, anything not changed
code = MetaServiceCode::INVALID_ARGUMENT;
msg = "original obj infos has a same conf, please check it";
return;
}
}
// calc id
auto tmp_tuple =
ObjectStorageDesc {ak, sk, bucket, prefix, endpoint, external_endpoint, region};
ObjectStoreInfoPB last_item = object_info_pb_factory(tmp_tuple, obj, instance,
encryption_info, cipher_ak_sk_pair);
if (instance.storage_vault_names().end() !=
std::find_if(instance.storage_vault_names().begin(),
instance.storage_vault_names().end(),
[&](const std::string& candidate_name) {
return candidate_name == request->vault().name();
})) {
code = MetaServiceCode::ALREADY_EXISTED;
msg = fmt::format("vault_name={} already created", request->vault().name());
return;
}
StorageVaultPB vault;
vault.set_id(last_item.id());
vault.set_name(request->vault().name());
*instance.mutable_resource_ids()->Add() = vault.id();
*instance.mutable_storage_vault_names()->Add() = vault.name();
vault.mutable_obj_info()->MergeFrom(last_item);
auto vault_key = storage_vault_key({instance.instance_id(), last_item.id()});
txn->put(vault_key, vault.SerializeAsString());
if (request->has_set_as_default_storage_vault() &&
request->set_as_default_storage_vault()) {
response->set_default_storage_vault_replaced(instance.has_default_storage_vault_id());
set_default_vault_log_helper(instance, vault.name(), vault.id());
instance.set_default_storage_vault_id(vault.id());
instance.set_default_storage_vault_name(vault.name());
}
response->set_storage_vault_id(vault.id());
LOG_INFO("try to put storage vault_id={}, vault_name={}, vault_key={}", vault.id(),
vault.name(), hex(vault_key));
} break;
case AlterObjStoreInfoRequest::ADD_HDFS_INFO: {
if (auto ret = add_vault_into_instance(