Skip to content

Commit 9054d1e

Browse files
committed
[fix](vault) avoid encrypt twice when altering vault (#45156)
1 parent ff4cea8 commit 9054d1e

File tree

2 files changed

+127
-19
lines changed

2 files changed

+127
-19
lines changed

cloud/src/meta-service/meta_service_resource.cpp

+28-16
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,19 @@ static int alter_s3_storage_vault(InstanceInfoPB& instance, std::unique_ptr<Tran
648648
obj_info.has_provider()) {
649649
code = MetaServiceCode::INVALID_ARGUMENT;
650650
std::stringstream ss;
651-
ss << "Only ak, sk can be altered";
651+
ss << "Bucket, endpoint, prefix and provider can not be altered";
652652
msg = ss.str();
653653
return -1;
654654
}
655+
656+
if (obj_info.has_ak() ^ obj_info.has_sk()) {
657+
code = MetaServiceCode::INVALID_ARGUMENT;
658+
std::stringstream ss;
659+
ss << "Accesskey and secretkey must be alter together";
660+
msg = ss.str();
661+
return -1;
662+
}
663+
655664
const auto& name = vault.name();
656665
// Here we try to get mutable iter since we might need to alter the vault name
657666
auto name_itr = std::find_if(instance.mutable_storage_vault_names()->begin(),
@@ -703,22 +712,25 @@ static int alter_s3_storage_vault(InstanceInfoPB& instance, std::unique_ptr<Tran
703712
*name_itr = vault.alter_name();
704713
}
705714
auto origin_vault_info = new_vault.DebugString();
706-
AkSkPair pre {new_vault.obj_info().ak(), new_vault.obj_info().sk()};
707-
const auto& plain_ak = obj_info.has_ak() ? obj_info.ak() : new_vault.obj_info().ak();
708-
const auto& plain_sk = obj_info.has_ak() ? obj_info.sk() : new_vault.obj_info().sk();
709-
AkSkPair plain_ak_sk_pair {plain_ak, plain_sk};
710-
AkSkPair cipher_ak_sk_pair;
711-
EncryptionInfoPB encryption_info;
712-
auto ret = encrypt_ak_sk_helper(plain_ak, plain_sk, &encryption_info, &cipher_ak_sk_pair, code,
713-
msg);
714-
if (ret != 0) {
715-
msg = "failed to encrypt";
716-
code = MetaServiceCode::ERR_ENCRYPT;
717-
LOG(WARNING) << msg;
718-
return -1;
715+
716+
// For ak or sk is not altered.
717+
EncryptionInfoPB encryption_info = new_vault.obj_info().encryption_info();
718+
AkSkPair new_ak_sk_pair {new_vault.obj_info().ak(), new_vault.obj_info().sk()};
719+
720+
if (obj_info.has_ak()) {
721+
// ak and sk must be altered together, there is check before.
722+
auto ret = encrypt_ak_sk_helper(obj_info.ak(), obj_info.sk(), &encryption_info,
723+
&new_ak_sk_pair, code, msg);
724+
if (ret != 0) {
725+
msg = "failed to encrypt";
726+
code = MetaServiceCode::ERR_ENCRYPT;
727+
LOG(WARNING) << msg;
728+
return -1;
729+
}
719730
}
720-
new_vault.mutable_obj_info()->set_ak(cipher_ak_sk_pair.first);
721-
new_vault.mutable_obj_info()->set_sk(cipher_ak_sk_pair.second);
731+
732+
new_vault.mutable_obj_info()->set_ak(new_ak_sk_pair.first);
733+
new_vault.mutable_obj_info()->set_sk(new_ak_sk_pair.second);
722734
new_vault.mutable_obj_info()->mutable_encryption_info()->CopyFrom(encryption_info);
723735
if (obj_info.has_use_path_style()) {
724736
new_vault.mutable_obj_info()->set_use_path_style(obj_info.use_path_style());

regression-test/suites/vault_p0/alter/test_alter_s3_vault.groovy

+99-3
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ suite("test_alter_s3_vault", "nonConcurrent") {
6262
"""
6363
}, "Alter property")
6464

65+
expectExceptionLike({
66+
sql """
67+
ALTER STORAGE VAULT ${suiteName}
68+
PROPERTIES (
69+
"type"="S3",
70+
"s3.access_key" = "new_ak"
71+
);
72+
"""
73+
}, "Alter property")
74+
75+
expectExceptionLike({
76+
sql """
77+
ALTER STORAGE VAULT ${suiteName}
78+
PROPERTIES (
79+
"type"="S3",
80+
"s3.secret_key" = "new_sk"
81+
);
82+
"""
83+
}, "Alter property")
6584

6685
def vaultName = suiteName
6786
String properties;
@@ -75,28 +94,105 @@ suite("test_alter_s3_vault", "nonConcurrent") {
7594
}
7695
}
7796

78-
def newVaultName = suiteName + "_new";
97+
// alter ak sk
98+
sql """
99+
ALTER STORAGE VAULT ${vaultName}
100+
PROPERTIES (
101+
"type"="S3",
102+
"s3.access_key" = "${getS3AK()}",
103+
"s3.secret_key" = "${getS3SK()}"
104+
);
105+
"""
106+
107+
vaultInfos = sql """SHOW STORAGE VAULT;"""
108+
109+
for (int i = 0; i < vaultInfos.size(); i++) {
110+
def name = vaultInfos[i][0]
111+
logger.info("name is ${name}, info ${vaultInfos[i]}")
112+
if (name.equals(vaultName)) {
113+
assert properties == newProperties, "Properties are not the same"
114+
}
115+
}
116+
117+
sql """insert into alter_s3_vault_tbl values("2", "2"); """
118+
119+
120+
// rename
121+
newVaultName = vaultName + "_new";
122+
123+
sql """
124+
ALTER STORAGE VAULT ${vaultName}
125+
PROPERTIES (
126+
"type"="S3",
127+
"VAULT_NAME" = "${newVaultName}"
128+
);
129+
"""
130+
131+
vaultInfos = sql """SHOW STORAGE VAULT;"""
132+
for (int i = 0; i < vaultInfos.size(); i++) {
133+
def name = vaultInfos[i][0]
134+
logger.info("name is ${name}, info ${vaultInfos[i]}")
135+
if (name.equals(newVaultName)) {
136+
assert properties == newProperties, "Properties are not the same"
137+
}
138+
if (name.equals(vaultName)) {
139+
assertTrue(false);
140+
}
141+
}
142+
143+
sql """insert into alter_s3_vault_tbl values("2", "2"); """
144+
145+
// rename + aksk
146+
vaultName = newVaultName
79147

80148
sql """
81149
ALTER STORAGE VAULT ${vaultName}
82150
PROPERTIES (
83151
"type"="S3",
84152
"VAULT_NAME" = "${newVaultName}",
85-
"s3.access_key" = "new_ak"
153+
"s3.access_key" = "${getS3AK()}",
154+
"s3.secret_key" = "${getS3SK()}"
86155
);
87156
"""
88157

158+
vaultInfos = sql """SHOW STORAGE VAULT;"""
159+
for (int i = 0; i < vaultInfos.size(); i++) {
160+
def name = vaultInfos[i][0]
161+
logger.info("name is ${name}, info ${vaultInfos[i]}")
162+
if (name.equals(newVaultName)) {
163+
assert properties == newProperties, "Properties are not the same"
164+
}
165+
if (name.equals(vaultName)) {
166+
assertTrue(false);
167+
}
168+
}
169+
sql """insert into alter_s3_vault_tbl values("2", "2"); """
170+
171+
172+
vaultName = newVaultName;
173+
newVaultName = vaultName + "_new";
174+
89175
vaultInfos = sql """SHOW STORAGE VAULT;"""
90176
boolean exist = false
91177

178+
sql """
179+
ALTER STORAGE VAULT ${vaultName}
180+
PROPERTIES (
181+
"type"="S3",
182+
"VAULT_NAME" = "${newVaultName}",
183+
"s3.access_key" = "new_ak_ak",
184+
"s3.secret_key" = "sk"
185+
);
186+
"""
187+
92188
for (int i = 0; i < vaultInfos.size(); i++) {
93189
def name = vaultInfos[i][0]
94190
logger.info("name is ${name}, info ${vaultInfos[i]}")
95191
if (name.equals(vaultName)) {
96192
assertTrue(false);
97193
}
98194
if (name.equals(newVaultName)) {
99-
assertTrue(vaultInfos[i][2].contains("new_ak"))
195+
assertTrue(vaultInfos[i][2].contains("new_ak_ak"))
100196
exist = true
101197
}
102198
}

0 commit comments

Comments
 (0)