Skip to content

Commit

Permalink
[BugFix]Avoid printing token in be log (#51728)
Browse files Browse the repository at this point in the history
Signed-off-by: xyllq999 <[email protected]>

(cherry picked from commit 94938dd)
Signed-off-by: xyllq999 <[email protected]>
  • Loading branch information
xyllq999 committed Nov 11, 2024
1 parent 751ba53 commit c0b4cac
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
22 changes: 18 additions & 4 deletions be/src/agent/heartbeat_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <atomic>
#include <ctime>
#include <fstream>
#include <sstream>

#include "agent/master_info.h"
#include "common/process_exit.h"
Expand Down Expand Up @@ -88,18 +89,19 @@ void HeartbeatServer::heartbeat(THeartbeatResult& heartbeat_result, const TMaste
if (!res.ok()) {
MasterInfoPtr ptr;
if (get_master_info(&ptr)) {
LOG(WARNING) << "Fail to handle heartbeat: " << res.status() << " cached master info: " << *ptr
<< " received master info: " << master_info;
LOG(WARNING) << "Fail to handle heartbeat: " << res.status()
<< " cached master info: " << print_master_info(*ptr)
<< " received master info: " << print_master_info(master_info);
} else {
LOG(WARNING) << "Fail to handle heartbeat: " << res.status();
}
} else if (*res == kNeedUpdate) {
LOG(INFO) << "Updating master info: " << master_info;
LOG(INFO) << "Updating master info: " << print_master_info(master_info);
bool r = update_master_info(master_info);
LOG_IF(WARNING, !r) << "Fail to update master info, maybe the master info has been updated by another thread "
"with a larger epoch";
} else if (*res == kNeedUpdateAndReport) {
LOG(INFO) << "Updating master info: " << master_info;
LOG(INFO) << "Updating master info: " << print_master_info(master_info);
bool r = update_master_info(master_info);
LOG_IF(WARNING, !r) << "Fail to update master info, maybe the master info has been updated by another thread "
"with a larger epoch";
Expand Down Expand Up @@ -145,6 +147,18 @@ void HeartbeatServer::heartbeat(THeartbeatResult& heartbeat_result, const TMaste
}
}

std::string HeartbeatServer::print_master_info(const TMasterInfo& master_info) const {
std::ostringstream out;
if (!master_info.__isset.token) {
master_info.printTo(out);
} else {
TMasterInfo master_info_copy(master_info);
master_info_copy.__set_token("<hidden>");
master_info_copy.printTo(out);
}
return out.str();
}

StatusOr<HeartbeatServer::CmpResult> HeartbeatServer::compare_master_info(const TMasterInfo& master_info) {
static const char* LOCALHOST = "127.0.0.1";
static const char* LOCALHOST_IPV6 = "::1";
Expand Down
3 changes: 3 additions & 0 deletions be/src/agent/heartbeat_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class HeartbeatServer : public HeartbeatServiceIf {

StatusOr<CmpResult> compare_master_info(const TMasterInfo& master_info);

// New function to print TMasterInfo object as a string
std::string print_master_info(const TMasterInfo& master_info) const;

StorageEngine* _olap_engine;
}; // class HeartBeatServer

Expand Down
63 changes: 63 additions & 0 deletions be/test/agent/heartbeat_server_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed 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
//
// https://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 "agent/heartbeat_server.h"

#include "gen_cpp/Types_types.h"
#include "gtest/gtest.h"

namespace starrocks {

TEST(HeartbeatServerTest, test_print_master_info_with_token_null) {
HeartbeatServer server;
TMasterInfo master_info;

master_info.network_address.__set_hostname("127.0.0.1");
master_info.network_address.__set_port(8080);
master_info.__set_cluster_id(12345);
master_info.__set_epoch(100);
master_info.__set_backend_ip("192.168.1.1");

std::string expected_output =
"TMasterInfo(network_address=TNetworkAddress(hostname=127.0.0.1, port=8080), "
"cluster_id=12345, epoch=100, token=<null>, backend_ip=192.168.1.1, "
"http_port=<null>, heartbeat_flags=<null>, backend_id=<null>, "
"min_active_txn_id=0, run_mode=<null>, disabled_disks=<null>, "
"decommissioned_disks=<null>, encrypted=<null>)";

EXPECT_EQ(server.print_master_info(master_info), expected_output);
}

TEST(HeartbeatServerTest, test_print_master_info_with_token_hidden) {
HeartbeatServer server;
TMasterInfo master_info;

master_info.network_address.__set_hostname("127.0.0.1");
master_info.network_address.__set_port(8080);
master_info.__set_cluster_id(12345);
master_info.__set_epoch(100);
master_info.__set_token("secret_token");
master_info.__set_backend_ip("192.168.1.1");

std::string expected_output =
"TMasterInfo(network_address=TNetworkAddress(hostname=127.0.0.1, port=8080), "
"cluster_id=12345, epoch=100, token=<hidden>, backend_ip=192.168.1.1, "
"http_port=<null>, heartbeat_flags=<null>, backend_id=<null>, "
"min_active_txn_id=0, run_mode=<null>, disabled_disks=<null>, "
"decommissioned_disks=<null>, encrypted=<null>)";

EXPECT_EQ(server.print_master_info(master_info), expected_output);
}

} // namespace starrocks

0 comments on commit c0b4cac

Please sign in to comment.