Skip to content

Commit 5417b26

Browse files
committed
[#3276] Check invalid server name during sync
1 parent 4fa627c commit 5417b26

File tree

4 files changed

+142
-95
lines changed

4 files changed

+142
-95
lines changed

src/hooks/dhcp/high_availability/ha_service.cc

+40-38
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ HAService::syncingStateHandler() {
771771
// Perform synchronous leases update.
772772
std::string status_message;
773773
int sync_status = synchronize(status_message,
774-
config_->getFailoverPeerConfig()->getName(),
774+
config_->getFailoverPeerConfig(),
775775
dhcp_disable_timeout);
776776

777777
// If the leases synchronization was successful, let's transition
@@ -1908,11 +1908,9 @@ HAService::startHeartbeat() {
19081908

19091909
void
19101910
HAService::asyncDisableDHCPService(HttpClient& http_client,
1911-
const std::string& server_name,
1911+
const HAConfig::PeerConfigPtr& remote_config,
19121912
const unsigned int max_period,
19131913
PostRequestCallback post_request_action) {
1914-
HAConfig::PeerConfigPtr remote_config = config_->getPeerConfig(server_name);
1915-
19161914
// Create HTTP/1.1 request including our command.
19171915
PostHttpRequestJsonPtr request = boost::make_shared<PostHttpRequestJson>
19181916
(HttpRequest::Method::HTTP_POST, "/", HttpVersion::HTTP_11(),
@@ -1991,10 +1989,8 @@ HAService::asyncDisableDHCPService(HttpClient& http_client,
19911989

19921990
void
19931991
HAService::asyncEnableDHCPService(HttpClient& http_client,
1994-
const std::string& server_name,
1992+
const HAConfig::PeerConfigPtr& remote_config,
19951993
PostRequestCallback post_request_action) {
1996-
HAConfig::PeerConfigPtr remote_config = config_->getPeerConfig(server_name);
1997-
19981994
// Create HTTP/1.1 request including our command.
19991995
PostHttpRequestJsonPtr request = boost::make_shared<PostHttpRequestJson>
20001996
(HttpRequest::Method::HTTP_POST, "/", HttpVersion::HTTP_11(),
@@ -2092,13 +2088,13 @@ HAService::asyncSyncLeases() {
20922088
}
20932089

20942090
lease_sync_filter_.apply();
2095-
asyncSyncLeases(*client_, config_->getFailoverPeerConfig()->getName(),
2091+
asyncSyncLeases(*client_, config_->getFailoverPeerConfig(),
20962092
dhcp_disable_timeout, LeasePtr(), null_action);
20972093
}
20982094

20992095
void
21002096
HAService::asyncSyncLeases(http::HttpClient& http_client,
2101-
const std::string& server_name,
2097+
const HAConfig::PeerConfigPtr& remote_config,
21022098
const unsigned int max_period,
21032099
const dhcp::LeasePtr& last_lease,
21042100
PostSyncCallback post_sync_action,
@@ -2108,8 +2104,8 @@ HAService::asyncSyncLeases(http::HttpClient& http_client,
21082104
// to allocate new leases while we fetch from it. The DHCP service will
21092105
// be disabled for a certain amount of time and will be automatically
21102106
// re-enabled if we die during the synchronization.
2111-
asyncDisableDHCPService(http_client, server_name, max_period,
2112-
[this, &http_client, server_name, max_period, last_lease,
2107+
asyncDisableDHCPService(http_client, remote_config, max_period,
2108+
[this, &http_client, remote_config, max_period, last_lease,
21132109
post_sync_action, dhcp_disabled]
21142110
(const bool success, const std::string& error_message, const int) {
21152111

@@ -2118,7 +2114,7 @@ HAService::asyncSyncLeases(http::HttpClient& http_client,
21182114
if (success) {
21192115
// The last argument indicates that disabling the DHCP
21202116
// service on the partner server was successful.
2121-
asyncSyncLeasesInternal(http_client, server_name, max_period,
2117+
asyncSyncLeasesInternal(http_client, remote_config, max_period,
21222118
last_lease, post_sync_action, true);
21232119

21242120
} else {
@@ -2129,19 +2125,16 @@ HAService::asyncSyncLeases(http::HttpClient& http_client,
21292125

21302126
void
21312127
HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
2132-
const std::string& server_name,
2128+
const HAConfig::PeerConfigPtr& remote_config,
21332129
const unsigned int max_period,
21342130
const dhcp::LeasePtr& last_lease,
21352131
PostSyncCallback post_sync_action,
21362132
const bool dhcp_disabled) {
2137-
2138-
HAConfig::PeerConfigPtr partner_config = config_->getPeerConfig(server_name);
2139-
21402133
// Create HTTP/1.1 request including our command.
21412134
PostHttpRequestJsonPtr request = boost::make_shared<PostHttpRequestJson>
21422135
(HttpRequest::Method::HTTP_POST, "/", HttpVersion::HTTP_11(),
2143-
HostHttpHeader(partner_config->getUrl().getStrippedHostname()));
2144-
partner_config->addBasicAuthHttpHeader(request);
2136+
HostHttpHeader(remote_config->getUrl().getStrippedHostname()));
2137+
remote_config->addBasicAuthHttpHeader(request);
21452138
if (server_type_ == HAServerType::DHCPv4) {
21462139
request->setBodyAsJson(CommandCreator::createLease4GetPage(
21472140
boost::dynamic_pointer_cast<Lease4>(last_lease), config_->getSyncPageLimit()));
@@ -2157,11 +2150,10 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
21572150
HttpResponseJsonPtr response = boost::make_shared<HttpResponseJson>();
21582151

21592152
// Schedule asynchronous HTTP request.
2160-
http_client.asyncSendRequest(partner_config->getUrl(),
2161-
partner_config->getTlsContext(),
2153+
http_client.asyncSendRequest(remote_config->getUrl(),
2154+
remote_config->getTlsContext(),
21622155
request, response,
2163-
[this, partner_config, post_sync_action, &http_client, server_name,
2164-
max_period, dhcp_disabled]
2156+
[this, remote_config, post_sync_action, &http_client, max_period, dhcp_disabled]
21652157
(const boost::system::error_code& ec,
21662158
const HttpResponsePtr& response,
21672159
const std::string& error_str) {
@@ -2183,7 +2175,7 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
21832175
error_message = (ec ? ec.message() : error_str);
21842176
LOG_ERROR(ha_logger, HA_LEASES_SYNC_COMMUNICATIONS_FAILED)
21852177
.arg(config_->getThisServerName())
2186-
.arg(partner_config->getLogLabel())
2178+
.arg(remote_config->getLogLabel())
21872179
.arg(error_message);
21882180

21892181
} else {
@@ -2211,7 +2203,7 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
22112203
LOG_INFO(ha_logger, HA_LEASES_SYNC_LEASE_PAGE_RECEIVED)
22122204
.arg(config_->getThisServerName())
22132205
.arg(leases_element.size())
2214-
.arg(server_name);
2206+
.arg(remote_config->getLogLabel());
22152207

22162208
// Count actually applied leases.
22172209
uint64_t applied_lease_count = 0;
@@ -2314,7 +2306,7 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
23142306
error_message = ex.what();
23152307
LOG_ERROR(ha_logger, HA_LEASES_SYNC_FAILED)
23162308
.arg(config_->getThisServerName())
2317-
.arg(partner_config->getLogLabel())
2309+
.arg(remote_config->getLogLabel())
23182310
.arg(error_message);
23192311
}
23202312
}
@@ -2327,7 +2319,7 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
23272319
} else if (last_lease) {
23282320
// This indicates that there are more leases to be fetched.
23292321
// Therefore, we have to send another leaseX-get-page command.
2330-
asyncSyncLeases(http_client, server_name, max_period, last_lease,
2322+
asyncSyncLeases(http_client, remote_config, max_period, last_lease,
23312323
post_sync_action, dhcp_disabled);
23322324
return;
23332325
}
@@ -2350,20 +2342,32 @@ HAService::asyncSyncLeasesInternal(http::HttpClient& http_client,
23502342
ConstElementPtr
23512343
HAService::processSynchronize(const std::string& server_name,
23522344
const unsigned int max_period) {
2345+
HAConfig::PeerConfigPtr remote_config;
2346+
try {
2347+
remote_config = config_->getPeerConfig(server_name);
2348+
} catch (const std::exception& ex) {
2349+
return (createAnswer(CONTROL_RESULT_ERROR, ex.what()));
2350+
}
2351+
// We must not synchronize with self.
2352+
if (remote_config->getName() == config_->getThisServerName()) {
2353+
return (createAnswer(CONTROL_RESULT_ERROR, "'" + remote_config->getName()
2354+
+ "' points to local server but should point to a partner"));
2355+
}
23532356
std::string answer_message;
2354-
int sync_status = synchronize(answer_message, server_name, max_period);
2357+
int sync_status = synchronize(answer_message, remote_config, max_period);
23552358
return (createAnswer(sync_status, answer_message));
23562359
}
23572360

23582361
int
2359-
HAService::synchronize(std::string& status_message, const std::string& server_name,
2362+
HAService::synchronize(std::string& status_message,
2363+
const HAConfig::PeerConfigPtr& remote_config,
23602364
const unsigned int max_period) {
23612365
lease_sync_filter_.apply();
23622366

23632367
IOServicePtr io_service(new IOService());
23642368
HttpClient client(io_service, false);
23652369

2366-
asyncSyncLeases(client, server_name, max_period, Lease4Ptr(),
2370+
asyncSyncLeases(client, remote_config, max_period, Lease4Ptr(),
23672371
[&](const bool success, const std::string& error_message,
23682372
const bool dhcp_disabled) {
23692373
// If there was a fatal error while fetching the leases, let's
@@ -2381,15 +2385,15 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
23812385
// try to send the ha-sync-complete-notify command to the
23822386
// partner.
23832387
if (success) {
2384-
asyncSyncCompleteNotify(client, server_name,
2388+
asyncSyncCompleteNotify(client, remote_config,
23852389
[&](const bool success,
23862390
const std::string& error_message,
23872391
const int rcode) {
23882392
// This command may not be supported by the partner when it
23892393
// runs an older Kea version. In that case, send the dhcp-enable
23902394
// command as in previous Kea version.
23912395
if (rcode == CONTROL_RESULT_COMMAND_UNSUPPORTED) {
2392-
asyncEnableDHCPService(client, server_name,
2396+
asyncEnableDHCPService(client, remote_config,
23932397
[&](const bool success,
23942398
const std::string& error_message,
23952399
const int) {
@@ -2422,7 +2426,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
24222426
// re-enable the DHCP service. Note, that we don't send the
24232427
// ha-sync-complete-notify command in this case. It is only sent in
24242428
// the case when synchronization ends successfully.
2425-
asyncEnableDHCPService(client, server_name,
2429+
asyncEnableDHCPService(client, remote_config,
24262430
[&](const bool success,
24272431
const std::string& error_message,
24282432
const int) {
@@ -2447,7 +2451,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
24472451

24482452
LOG_INFO(ha_logger, HA_SYNC_START)
24492453
.arg(config_->getThisServerName())
2450-
.arg(server_name);
2454+
.arg(remote_config->getLogLabel());
24512455

24522456
// Measure duration of the synchronization.
24532457
Stopwatch stopwatch;
@@ -2475,7 +2479,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
24752479

24762480
LOG_ERROR(ha_logger, HA_SYNC_FAILED)
24772481
.arg(config_->getThisServerName())
2478-
.arg(server_name)
2482+
.arg(remote_config->getLogLabel())
24792483
.arg(status_message);
24802484

24812485
return (CONTROL_RESULT_ERROR);
@@ -2488,7 +2492,7 @@ HAService::synchronize(std::string& status_message, const std::string& server_na
24882492

24892493
LOG_INFO(ha_logger, HA_SYNC_SUCCESSFUL)
24902494
.arg(config_->getThisServerName())
2491-
.arg(server_name)
2495+
.arg(remote_config->getLogLabel())
24922496
.arg(stopwatch.logFormatLastDuration());
24932497

24942498
return (CONTROL_RESULT_SUCCESS);
@@ -3015,10 +3019,8 @@ HAService::processMaintenanceCancel() {
30153019

30163020
void
30173021
HAService::asyncSyncCompleteNotify(HttpClient& http_client,
3018-
const std::string& server_name,
3022+
const HAConfig::PeerConfigPtr& remote_config,
30193023
PostRequestCallback post_request_action) {
3020-
HAConfig::PeerConfigPtr remote_config = config_->getPeerConfig(server_name);
3021-
30223024
// Create HTTP/1.1 request including our command.
30233025
PostHttpRequestJsonPtr request = boost::make_shared<PostHttpRequestJson>
30243026
(HttpRequest::Method::HTTP_POST, "/", HttpVersion::HTTP_11(),

src/hooks/dhcp/high_availability/ha_service.h

+16-15
Original file line numberDiff line numberDiff line change
@@ -768,14 +768,14 @@ class HAService : public boost::noncopyable, public util::StateModel {
768768
///
769769
/// @param http_client reference to the client to be used to communicate
770770
/// with the other server.
771-
/// @param server_name name of the server to which the command should be
772-
/// sent.
771+
/// @param remote_config config of the partner to which the command should
772+
/// be sent.
773773
/// @param max_period maximum number of seconds for which the DHCP service
774774
/// should be disabled.
775775
/// @param post_request_action pointer to the function to be executed when
776776
/// the request is completed.
777777
void asyncDisableDHCPService(http::HttpClient& http_client,
778-
const std::string& server_name,
778+
const HAConfig::PeerConfigPtr& remote_config,
779779
const unsigned int max_period,
780780
PostRequestCallback post_request_action);
781781

@@ -784,12 +784,12 @@ class HAService : public boost::noncopyable, public util::StateModel {
784784
///
785785
/// @param http_client reference to the client to be used to communicate
786786
/// with the other server.
787-
/// @param server_name name of the server to which the command should be
788-
/// sent.
787+
/// @param remote_config config of the partner to which the command should
788+
/// be sent.
789789
/// @param post_request_action pointer to the function to be executed when
790790
/// the request is completed.
791791
void asyncEnableDHCPService(http::HttpClient& http_client,
792-
const std::string& server_name,
792+
const HAConfig::PeerConfigPtr& remote_config,
793793
PostRequestCallback post_request_action);
794794

795795
/// @brief Disables local DHCP service.
@@ -851,7 +851,7 @@ class HAService : public boost::noncopyable, public util::StateModel {
851851
///
852852
/// @param http_client reference to the client to be used to communicate
853853
/// with the other server.
854-
/// @param server_name name of the server to fetch leases from.
854+
/// @param remote_config config of the partner to fetch leases from.
855855
/// @param max_period maximum number of seconds to disable DHCP service
856856
/// @param last_lease Pointer to the last lease returned on the previous
857857
/// page of leases. This lease is used to set the value of the "from"
@@ -866,7 +866,7 @@ class HAService : public boost::noncopyable, public util::StateModel {
866866
/// @c post_sync_action to indicate whether the DHCP service has to
867867
/// be enabled after the leases synchronization.
868868
void asyncSyncLeases(http::HttpClient& http_client,
869-
const std::string& server_name,
869+
const HAConfig::PeerConfigPtr& remote_config,
870870
const unsigned int max_period,
871871
const dhcp::LeasePtr& last_lease,
872872
PostSyncCallback post_sync_action,
@@ -884,7 +884,7 @@ class HAService : public boost::noncopyable, public util::StateModel {
884884
///
885885
/// @param http_client reference to the client to be used to communicate
886886
/// with the other server.
887-
/// @param server_name name of the server to fetch leases from.
887+
/// @param remote_config config of the partner to fetch leases from.
888888
/// @param max_period maximum number of seconds to disable DHCP service
889889
/// @param last_lease Pointer to the last lease returned on the previous
890890
/// page of leases. This lease is used to set the value of the "from"
@@ -899,7 +899,7 @@ class HAService : public boost::noncopyable, public util::StateModel {
899899
/// @c post_sync_action to indicate whether the DHCP service has to
900900
/// be enabled after the leases synchronization.
901901
void asyncSyncLeasesInternal(http::HttpClient& http_client,
902-
const std::string& server_name,
902+
const HAConfig::PeerConfigPtr& remote_config,
903903
const unsigned int max_period,
904904
const dhcp::LeasePtr& last_lease,
905905
PostSyncCallback post_sync_action,
@@ -944,14 +944,15 @@ class HAService : public boost::noncopyable, public util::StateModel {
944944
/// invokes IOService::run().
945945
///
946946
/// @param [out] status_message status message in textual form.
947-
/// @param server_name name of the server to fetch leases from.
947+
/// @param remote_config config of the server to fetch leases from.
948948
/// @param max_period maximum number of seconds to disable DHCP service
949949
/// of the peer. This value is used in dhcp-disable command issued to
950950
/// the peer before the lease4-get-page command.
951951
///
952952
/// @return Synchronization result according to the status codes returned
953953
/// in responses to control commands.
954-
int synchronize(std::string& status_message, const std::string& server_name,
954+
int synchronize(std::string& status_message,
955+
const HAConfig::PeerConfigPtr& remote_config,
955956
const unsigned int max_period);
956957

957958
/// @brief Sends lease updates from backlog to partner asynchronously.
@@ -1100,12 +1101,12 @@ class HAService : public boost::noncopyable, public util::StateModel {
11001101
///
11011102
/// @param http_client reference to the client to be used to communicate
11021103
/// with the other server.
1103-
/// @param server_name name of the server to which the command should be
1104-
/// sent.
1104+
/// @param remote_config config of the partner to which the command should
1105+
/// be sent.
11051106
/// @param post_request_action pointer to the function to be executed when
11061107
/// the request is completed.
11071108
void asyncSyncCompleteNotify(http::HttpClient& http_client,
1108-
const std::string& server_name,
1109+
const HAConfig::PeerConfigPtr& remote_config,
11091110
PostRequestCallback post_request_action);
11101111

11111112
public:

0 commit comments

Comments
 (0)