This is an automated email from the ASF dual-hosted git repository.
deardeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new ceeec73b2c3 [fix](cloud) Reject unregistered cloud_unique_id and
mismatched instance_id in meta service (#69732)
ceeec73b2c3 is described below
commit ceeec73b2c330797a99812cbfe4bc49b8c236742
Author: deardeng <[email protected]>
AuthorDate: Mon Nov 23 10:22:45 2026 +0800
[fix](cloud) Reject unregistered cloud_unique_id and mismatched instance_id
in meta service (#69732)
This PR strengthens instance identity validation in Meta Service RPCs.
Existing management APIs remain compatible.
---
cloud/src/meta-service/meta_service.cpp | 46 ++++++++++++
cloud/src/meta-service/meta_service.h | 11 +++
cloud/test/meta_service_http_test.cpp | 65 +++++++++++++++++
cloud/test/meta_service_test.cpp | 112 ++++++++++++++++++++++++++++-
cloud/test/recycle_versioned_keys_test.cpp | 13 ++++
cloud/test/resource_test.cpp | 1 +
6 files changed, 246 insertions(+), 2 deletions(-)
diff --git a/cloud/src/meta-service/meta_service.cpp
b/cloud/src/meta-service/meta_service.cpp
index adfdbb45288..d4649debdd1 100644
--- a/cloud/src/meta-service/meta_service.cpp
+++ b/cloud/src/meta-service/meta_service.cpp
@@ -130,12 +130,33 @@ std::string get_instance_id(const
std::shared_ptr<ResourceManager>& rc_mgr,
<< id;
return "";
}
+ if (code != TxnErrorCode::TXN_OK) {
+ // Reject rather than accept a cloud_unique_id whose node
membership is unchecked.
+ LOG(WARNING) << "use degraded format cloud_unique_id, but
failed to get instance, "
+ "instance_id="
+ << id << " err=" << msg;
+ return "";
+ }
if (instance.has_status() && instance.status() ==
InstanceInfoPB::DELETED) {
msg = "instance status has been set delete, plz check it,
recycle_state=" +
InstanceRecycleState_Name(instance.recycle_state());
LOG(WARNING) << msg << ", instance_id=" << id;
return "";
}
+ // Anyone knowing the instance_id can build a degraded format
cloud_unique_id, so it
+ // is only trusted when it is a registered node of that instance.
+ bool registered = false;
+ for (const auto& cluster : instance.clusters()) {
+ for (const auto& node : cluster.nodes()) {
+ registered |= node.cloud_unique_id() == cloud_unique_id;
+ }
+ }
+ if (!registered) {
+ LOG(WARNING) << "use degraded format cloud_unique_id, but node
is not registered, "
+ "instance_id="
+ << id;
+ return "";
+ }
}
return id;
}
@@ -216,6 +237,31 @@ bool MetaServiceProxy::check_strict_instance_auth(const
::google::protobuf::Mess
return false;
}
+bool MetaServiceProxy::check_request_instance_id(const
::google::protobuf::Message& req,
+ MetaServiceResponseStatus*
status) {
+ const auto* descriptor = req.GetDescriptor();
+ const auto* reflection = req.GetReflection();
+ // call_impl only checks requests with an instance_id field.
+ const auto* instance_id_field = descriptor->FindFieldByName("instance_id");
+ CHECK(instance_id_field != nullptr) << descriptor->name();
+ std::string instance_id = reflection->GetString(req, instance_id_field);
+ const auto* cloud_unique_id_field =
descriptor->FindFieldByName("cloud_unique_id");
+ std::string cloud_unique_id = cloud_unique_id_field == nullptr
+ ? ""
+ : reflection->GetString(req,
cloud_unique_id_field);
+ // Nothing to cross check without both fields. Requests naming an instance
by instance_id only
+ // are still accepted for compatibility, e.g. FE alter_cluster and
create_instance.
+ if (instance_id.empty() || cloud_unique_id.empty() ||
+ get_instance_id(resource_mgr(), cloud_unique_id) == instance_id) {
+ return true;
+ }
+ status->set_code(MetaServiceCode::INVALID_ARGUMENT);
+ status->set_msg("instance_id does not belong to cloud_unique_id");
+ LOG(WARNING) << status->msg() << ", request=" << descriptor->name()
+ << " instance_id=" << instance_id;
+ return false;
+}
+
// Return `true` if tablet has been dropped; otherwise or it may not determine
when meeting errors, return false
bool is_dropped_tablet(Transaction* txn, const std::string& instance_id,
int64_t index_id,
int64_t partition_id) {
diff --git a/cloud/src/meta-service/meta_service.h
b/cloud/src/meta-service/meta_service.h
index cff8e029e61..e9f4f900d4c 100644
--- a/cloud/src/meta-service/meta_service.h
+++ b/cloud/src/meta-service/meta_service.h
@@ -1068,6 +1068,9 @@ private:
// cloud_unique_id.
static bool check_strict_instance_auth(const ::google::protobuf::Message&
req,
MetaServiceResponseStatus* status);
+ // Reject a request whose instance_id is not the instance its
cloud_unique_id belongs to.
+ bool check_request_instance_id(const ::google::protobuf::Message& req,
+ MetaServiceResponseStatus* status);
template <typename Request, typename Response>
using MetaServiceMethod = void
(cloud::MetaService::*)(::google::protobuf::RpcController*,
@@ -1099,6 +1102,14 @@ private:
return;
}
+ // Only rpc goes through the proxy, the http api (authorized by token)
calls impl directly.
+ static const bool names_instance =
+ !std::is_same_v<Request, BootstrapInstanceRequest> &&
+ Request::descriptor()->FindFieldByName("instance_id") !=
nullptr;
+ if (names_instance && !check_request_instance_id(*req,
resp->mutable_status())) {
+ return;
+ }
+
// life span of this defer MUST be longer than `done`
std::unique_ptr<int, std::function<void(int*)>> defer_injection(
(int*)(0x01), [&, this](int*) { idempotent_injection(method,
req, resp); });
diff --git a/cloud/test/meta_service_http_test.cpp
b/cloud/test/meta_service_http_test.cpp
index 8b20749fd0a..abe062c64f3 100644
--- a/cloud/test/meta_service_http_test.cpp
+++ b/cloud/test/meta_service_http_test.cpp
@@ -1706,6 +1706,71 @@ private:
std::vector<std::string> messages_;
};
+TEST(MetaServiceHttpTest, InstanceValidationDoesNotLogCloudUniqueId) {
+ auto meta_service = get_meta_service(false);
+ const bool check_instance_id = config::enable_check_instance_id;
+ config::enable_check_instance_id = true;
+ auto* sp = SyncPoint::get_instance();
+ DORIS_CLOUD_DEFER {
+ config::enable_check_instance_id = check_instance_id;
+ sp->clear_all_call_backs();
+ sp->disable_processing();
+ };
+
+ const std::string instance_id = "validation_log_instance";
+ const std::string cloud_unique_id = "1:" + instance_id +
":registered-secret-marker";
+ const std::string forged_cloud_unique_id = "1:" + instance_id +
":forged-secret-marker";
+ InstanceInfoPB instance;
+ instance.set_instance_id(instance_id);
+ auto* cluster = instance.add_clusters();
+ cluster->set_cluster_id("validation_log_cluster");
+ cluster->set_cluster_name("validation_log_cluster");
+ cluster->set_type(ClusterPB::COMPUTE);
+ auto* node = cluster->add_nodes();
+ node->set_cloud_unique_id(cloud_unique_id);
+ node->set_ip("127.0.0.1");
+ node->set_heartbeat_port(9050);
+ std::unique_ptr<Transaction> txn;
+ ASSERT_EQ(meta_service->txn_kv()->create_txn(&txn), TxnErrorCode::TXN_OK);
+ txn->put(instance_key({instance_id}), instance.SerializeAsString());
+ ASSERT_EQ(txn->commit(), TxnErrorCode::TXN_OK);
+ meta_service->resource_mgr()->refresh_instance(instance_id, instance);
+
+ auto get_cluster = [&](const std::string& unique_id, const std::string&
request_instance_id) {
+ brpc::Controller ctrl;
+ GetClusterRequest req;
+ req.set_cloud_unique_id(unique_id);
+ req.set_instance_id(request_instance_id);
+ GetClusterResponse res;
+ meta_service->get_cluster(&ctrl, &req, &res, nullptr);
+ return res.status().code();
+ };
+ ServerLogSink sink;
+ ASSERT_EQ(get_cluster(cloud_unique_id, instance_id), MetaServiceCode::OK);
+ EXPECT_EQ(get_cluster(cloud_unique_id, "other_instance"),
MetaServiceCode::INVALID_ARGUMENT);
+ EXPECT_EQ(get_cluster(forged_cloud_unique_id, ""),
MetaServiceCode::INVALID_ARGUMENT);
+
+ sp->set_call_back("get_instance_id_err", [](auto&& args) {
+ *try_any_cast<std::string*>(args[0]) = "can't find node from cache";
+ });
+ sp->set_call_back("is_instance_id_registered", [](auto&& args) {
+ *try_any_cast<TxnErrorCode*>(args[0]) = TxnErrorCode::TXN_TOO_OLD;
+ });
+ sp->enable_processing();
+ EXPECT_EQ(get_cluster(cloud_unique_id, ""),
MetaServiceCode::INVALID_ARGUMENT);
+
+ std::string logs;
+ for (const auto& message : sink.messages()) {
+ logs += message + "\n";
+ }
+ EXPECT_NE(logs.find("instance_id does not belong to cloud_unique_id"),
std::string::npos);
+ EXPECT_NE(logs.find("but node is not registered"), std::string::npos);
+ EXPECT_NE(logs.find("but failed to get instance"), std::string::npos);
+ EXPECT_NE(logs.find("instance_id=" + instance_id), std::string::npos);
+ EXPECT_EQ(logs.find("registered-secret-marker"), std::string::npos);
+ EXPECT_EQ(logs.find("forged-secret-marker"), std::string::npos);
+}
+
TEST(MetaServiceHttpTest, HttpRequestLogsMaskCredentials) {
const int old_vlog_level = google::SetVLOGLevel("meta_service_http", -1);
DORIS_CLOUD_DEFER {
diff --git a/cloud/test/meta_service_test.cpp b/cloud/test/meta_service_test.cpp
index 3b8e7061201..d476fbedb27 100644
--- a/cloud/test/meta_service_test.cpp
+++ b/cloud/test/meta_service_test.cpp
@@ -455,9 +455,10 @@ TEST(MetaServiceTest, GetInstanceIdTest) {
TxnErrorCode* c0 = try_any_cast<TxnErrorCode*>(args[0]);
*c0 = TxnErrorCode::TXN_OK;
});
+ // instance is registered, but the node is not
instance_id =
get_instance_id(ms->resource_mgr(),
"1:ALBJLH4Q-check-invalid:m-n3qdpyal27rh8iprxx");
- ASSERT_EQ(instance_id, "ALBJLH4Q-check-invalid");
+ ASSERT_EQ(instance_id, "");
config::enable_check_instance_id = false;
sp->clear_all_call_backs();
@@ -1522,7 +1523,7 @@ TEST(MetaServiceTest, BootstrapInstanceTest) {
meta_service->alter_instance(&ctrl, &req, &response, nullptr);
return response.status().code();
};
- EXPECT_EQ(rename_instance(instance_id),
MetaServiceCode::CLUSTER_NOT_FOUND);
+ EXPECT_EQ(rename_instance(instance_id), MetaServiceCode::INVALID_ARGUMENT);
node->set_cloud_unique_id("1:another_instance:fe");
EXPECT_EQ(bootstrap(), MetaServiceCode::INVALID_ARGUMENT);
@@ -1758,6 +1759,113 @@ TEST(MetaServiceTest, StrictInstanceAuthTest) {
EXPECT_FALSE(rejected(get_cluster_status(false)));
}
+// Write an instance with one registered node into kv and the resource manager.
+static void register_instance_node(MetaServiceProxy* meta_service, const
std::string& instance_id,
+ const std::string& cloud_unique_id) {
+ InstanceInfoPB instance;
+ instance.set_instance_id(instance_id);
+ auto* cluster = instance.add_clusters();
+ cluster->set_cluster_name(instance_id + "_cluster");
+ cluster->set_cluster_id(instance_id + "_cluster_id");
+ cluster->set_type(ClusterPB::COMPUTE);
+ auto* node = cluster->add_nodes();
+ node->set_cloud_unique_id(cloud_unique_id);
+ node->set_ip("127.0.0.1");
+ node->set_heartbeat_port(9050);
+
+ std::string key;
+ instance_key({instance_id}, &key);
+ std::unique_ptr<Transaction> txn;
+ ASSERT_EQ(meta_service->txn_kv()->create_txn(&txn), TxnErrorCode::TXN_OK);
+ txn->put(key, instance.SerializeAsString());
+ ASSERT_EQ(txn->commit(), TxnErrorCode::TXN_OK);
+ meta_service->resource_mgr()->refresh_instance(instance_id, instance);
+}
+
+TEST(MetaServiceTest, GetInstanceIdKvErrorTest) {
+ extern std::string get_instance_id(const std::shared_ptr<ResourceManager>&
rc_mgr,
+ const std::string& cloud_unique_id);
+ auto meta_service = get_meta_service(false);
+ bool enable_check_instance_id = config::enable_check_instance_id;
+ config::enable_check_instance_id = true;
+ auto sp = SyncPoint::get_instance();
+ DORIS_CLOUD_DEFER {
+ config::enable_check_instance_id = enable_check_instance_id;
+ sp->clear_all_call_backs();
+ sp->disable_processing();
+ };
+ const std::string instance_id = "kv_error_instance";
+ const std::string cloud_unique_id = "1:" + instance_id + ":be";
+ register_instance_node(meta_service.get(), instance_id, cloud_unique_id);
+ // Take the degraded path, as for a registered node not in the cache yet.
+ sp->set_call_back("get_instance_id_err", [](auto&& args) {
+ *try_any_cast<std::string*>(args[0]) = "can't find node from cache";
+ });
+ sp->enable_processing();
+ ASSERT_EQ(get_instance_id(meta_service->resource_mgr(), cloud_unique_id),
instance_id);
+
+ // A failed instance read is rejected rather than trusted.
+ sp->set_call_back("is_instance_id_registered", [](auto&& args) {
+ *try_any_cast<TxnErrorCode*>(args[0]) = TxnErrorCode::TXN_TOO_OLD;
+ });
+ ASSERT_EQ(get_instance_id(meta_service->resource_mgr(), cloud_unique_id),
"");
+}
+
+TEST(MetaServiceTest, GetClusterForgedCloudUniqueIdTest) {
+ auto meta_service = get_meta_service(false);
+ bool enable_check_instance_id = config::enable_check_instance_id;
+ config::enable_check_instance_id = true;
+ DORIS_CLOUD_DEFER {
+ config::enable_check_instance_id = enable_check_instance_id;
+ };
+ auto register_node = [&](const std::string& instance_id, const
std::string& cloud_unique_id) {
+ register_instance_node(meta_service.get(), instance_id,
cloud_unique_id);
+ };
+ const std::string instance_a = "forged_cuid_instance_a";
+ const std::string node_a = "1:" + instance_a + ":registered_node";
+ const std::string instance_b = "forged_cuid_instance_b";
+ const std::string node_b = "1:" + instance_b + ":registered_node";
+ register_node(instance_a, node_a);
+ register_node(instance_b, node_b);
+
+ auto get_cluster = [&](const std::string& cloud_unique_id, const
std::string& req_instance_id) {
+ brpc::Controller cntl;
+ GetClusterRequest req;
+ req.set_cloud_unique_id(cloud_unique_id);
+ if (!req_instance_id.empty()) {
+ req.set_instance_id(req_instance_id);
+ }
+ GetClusterResponse res;
+ meta_service->get_cluster(&cntl, &req, &res, nullptr);
+ return res;
+ };
+
+ // registered cloud_unique_id, with or without its own instance_id
+ for (const auto& req_instance_id : {std::string(), instance_b}) {
+ auto res = get_cluster(node_b, req_instance_id);
+ ASSERT_EQ(res.status().code(), MetaServiceCode::OK) <<
res.status().msg();
+ ASSERT_EQ(res.cluster(0).nodes(0).cloud_unique_id(), node_b);
+ }
+ // forged third segment must not leak the registered cloud_unique_id
+ {
+ auto res = get_cluster("1:" + instance_b + ":forged", "");
+ EXPECT_EQ(res.status().code(), MetaServiceCode::INVALID_ARGUMENT);
+ EXPECT_EQ(res.cluster_size(), 0);
+ }
+ // unresolvable cloud_unique_id with a request supplied instance_id
+ {
+ auto res = get_cluster("not_registered", instance_b);
+ EXPECT_EQ(res.status().code(), MetaServiceCode::INVALID_ARGUMENT);
+ EXPECT_EQ(res.cluster_size(), 0);
+ }
+ // cloud_unique_id of instance a with instance_id of instance b
+ {
+ auto res = get_cluster(node_a, instance_b);
+ EXPECT_EQ(res.status().code(), MetaServiceCode::INVALID_ARGUMENT);
+ EXPECT_EQ(res.cluster_size(), 0);
+ }
+}
+
TEST(MetaServiceTest, BeginTxnTest) {
auto meta_service = get_meta_service();
int64_t db_id = 666;
diff --git a/cloud/test/recycle_versioned_keys_test.cpp
b/cloud/test/recycle_versioned_keys_test.cpp
index 33464c9eafa..2d5413f3f85 100644
--- a/cloud/test/recycle_versioned_keys_test.cpp
+++ b/cloud/test/recycle_versioned_keys_test.cpp
@@ -103,6 +103,8 @@ void create_and_refresh_instance(
InstanceInfoPB instance_info;
instance_info.set_instance_id(instance_id);
instance_info.set_multi_version_status(multi_version_status);
+ instance_info.add_clusters()->add_nodes()->set_cloud_unique_id(
+ fmt::format("1:{}:0", instance_id));
auto* obj_info = instance_info.mutable_obj_info()->Add();
obj_info->set_id(std::string(RESOURCE_ID));
obj_info->set_ak("mock_ak");
@@ -552,6 +554,17 @@ void get_instance(MetaServiceProxy* meta_service, const
std::string& cloud_uniqu
void drop_instance(MetaServiceProxy* meta_service, const std::string&
instance_id) {
ScopedLegacyInstanceRpc legacy_instance_rpc;
+ // An instance with clusters can not be dropped, remove them first.
+ std::unique_ptr<Transaction> txn;
+ ASSERT_EQ(meta_service->txn_kv()->create_txn(&txn), TxnErrorCode::TXN_OK);
+ std::string val;
+ ASSERT_EQ(txn->get(instance_key(instance_id), &val), TxnErrorCode::TXN_OK);
+ InstanceInfoPB instance_info;
+ ASSERT_TRUE(instance_info.ParseFromString(val));
+ instance_info.clear_clusters();
+ txn->put(instance_key(instance_id), instance_info.SerializeAsString());
+ ASSERT_EQ(txn->commit(), TxnErrorCode::TXN_OK);
+
brpc::Controller cntl;
AlterInstanceRequest req;
AlterInstanceResponse res;
diff --git a/cloud/test/resource_test.cpp b/cloud/test/resource_test.cpp
index 15093dd066d..382f818d5fe 100644
--- a/cloud/test/resource_test.cpp
+++ b/cloud/test/resource_test.cpp
@@ -842,6 +842,7 @@ static void
create_instance_with_storage_vault(MetaServiceProxy* meta_service,
}
instance.set_snapshot_switch_status(enable_snapshot ? SNAPSHOT_SWITCH_ON
:
SNAPSHOT_SWITCH_DISABLED);
+ instance.add_clusters()->add_nodes()->set_cloud_unique_id("1:" +
instance_id + ":test");
StorageVaultPB vault;
vault.set_id(vault_id);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]