snitch: Check http response codes to be OK
Several snitch drivers make http requests to get region/dc/zone/rack/whatever from the cloud provider. They blindly rely on the response being successfull and read response body to parse the data they need from. That's not nice, add checks for requests finish with http OK statuses. refs: #12185 Signed-off-by: Pavel Emelyanov <xemul@scylladb.com> Closes #12287
This commit is contained in:
committed by
Avi Kivity
parent
c7cee0da40
commit
fe4cf231bc
@@ -12,6 +12,7 @@
|
||||
#include <seastar/core/coroutine.hh>
|
||||
#include <seastar/core/seastar.hh>
|
||||
#include <seastar/http/response_parser.hh>
|
||||
#include <seastar/http/reply.hh>
|
||||
#include <seastar/net/api.hh>
|
||||
#include <seastar/net/dns.hh>
|
||||
|
||||
@@ -83,6 +84,10 @@ future<sstring> azure_snitch::azure_api_call(sstring path) {
|
||||
|
||||
// Read HTTP response header first
|
||||
auto rsp = parser.get_parsed_response();
|
||||
if (rsp->_status_code != static_cast<int>(http::reply::status_type::ok)) {
|
||||
throw std::runtime_error(format("Error: HTTP response status {}", rsp->_status_code));
|
||||
}
|
||||
|
||||
auto it = rsp->_headers.find("Content-Length");
|
||||
if (it == rsp->_headers.end()) {
|
||||
throw std::runtime_error("Error: HTTP response does not contain: Content-Length\n");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <seastar/core/seastar.hh>
|
||||
#include <seastar/core/sleep.hh>
|
||||
#include <seastar/core/do_with.hh>
|
||||
#include <seastar/http/reply.hh>
|
||||
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
@@ -126,6 +127,9 @@ future<sstring> ec2_snitch::aws_api_call_once(sstring addr, uint16_t port, sstri
|
||||
if (rc == 403) {
|
||||
return make_exception_future<sstring>(std::runtime_error("Error: Unauthorized response received when trying to communicate with instance metadata service."));
|
||||
}
|
||||
if (_rsp->_status_code != static_cast<int>(http::reply::status_type::ok)) {
|
||||
return make_exception_future<sstring>(std::runtime_error(format("Error: HTTP response status {}", _rsp->_status_code)));
|
||||
}
|
||||
|
||||
auto it = _rsp->_headers.find("Content-Length");
|
||||
if (it == _rsp->_headers.end()) {
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <seastar/net/dns.hh>
|
||||
#include <seastar/core/seastar.hh>
|
||||
#include "locator/gce_snitch.hh"
|
||||
#include <seastar/http/reply.hh>
|
||||
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
@@ -99,6 +100,10 @@ future<sstring> gce_snitch::gce_api_call(sstring addr, sstring cmd) {
|
||||
|
||||
// Read HTTP response header first
|
||||
auto rsp = parser.get_parsed_response();
|
||||
if (rsp->_status_code != static_cast<int>(http::reply::status_type::ok)) {
|
||||
throw std::runtime_error(format("Error: HTTP response status {}", rsp->_status_code));
|
||||
}
|
||||
|
||||
auto it = rsp->_headers.find("Content-Length");
|
||||
if (it == rsp->_headers.end()) {
|
||||
throw std::runtime_error("Error: HTTP response does not contain: Content-Length\n");
|
||||
|
||||
Reference in New Issue
Block a user