1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
|
/*
Copyright (C) 2021 The Falco Authors.
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
http://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.
*/
//
// mesos_http.cpp
//
#if !defined(CYGWING_AGENT) && !defined(_WIN32)
#ifdef HAS_CAPTURE
#include "mesos_http.h"
#include "curl/curl.h"
#include "curl/easy.h"
#include "sinsp.h"
#include "sinsp_int.h"
#include "sinsp_curl.h"
#include "json_error_log.h"
#include "mesos.h"
#define BUFFERSIZE 512 // b64 needs this macro
#include "b64/encode.h"
#include <sstream>
#include <stdexcept>
#include <unistd.h>
#include <sys/ioctl.h>
#include <cstring>
mesos_http::mesos_http(mesos& m, const uri& url,
bool discover_mesos_lead_master,
bool discover_marathon,
int timeout_ms, const string& token):
m_sync_curl(curl_easy_init()),
m_select_curl(curl_easy_init()),
m_mesos(m),
m_url(url),
m_connected(true),
m_watch_socket(-1),
m_timeout_ms(timeout_ms),
m_callback_func(0),
m_curl_version(curl_version_info(CURLVERSION_NOW)),
m_is_mesos_state(url.to_string().find(mesos::default_state_api) != std::string::npos),
m_discover_lead_master(discover_mesos_lead_master),
m_discover_marathon(discover_marathon),
m_token(token)
{
if(!m_sync_curl || !m_select_curl)
{
throw sinsp_exception("mesos_http: CURL initialization failed.");
}
ASSERT(m_curl_version);
m_request = make_request(url, m_curl_version);
if(!m_token.empty())
{
m_sync_curl_headers.add(string("Authorization: token=") + m_token);
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_HTTPHEADER, m_sync_curl_headers.ptr()));
}
if(m_url.is_secure())
{
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_SSL_VERIFYPEER, 0));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_SSL_VERIFYHOST, 0));
check_error(curl_easy_setopt(m_select_curl, CURLOPT_SSL_VERIFYPEER, 0));
check_error(curl_easy_setopt(m_select_curl, CURLOPT_SSL_VERIFYHOST, 0));
}
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_FORBID_REUSE, 1L));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_CONNECTTIMEOUT_MS, m_timeout_ms));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_TIMEOUT_MS, m_timeout_ms));
check_error(curl_easy_setopt(m_select_curl, CURLOPT_CONNECTTIMEOUT_MS, m_timeout_ms));
discover_mesos_leader();
}
mesos_http::~mesos_http()
{
cleanup();
}
void mesos_http::cleanup()
{
cleanup(&m_sync_curl);
cleanup(&m_select_curl);
}
void mesos_http::cleanup(CURL** curl)
{
if(curl && *curl)
{
curl_easy_cleanup(*curl);
*curl = 0;
}
m_connected = false;
}
void mesos_http::set_token(const string& token)
{
m_token = token;
m_request = make_request(m_url, m_curl_version);
}
Json::Value mesos_http::get_state_frameworks()
{
Json::Value frameworks;
std::ostringstream os;
CURLcode res = get_data(m_url.to_string(), os);
if(res == CURLE_OK)
{
Json::Value root;
Json::Reader reader;
if(reader.parse(os.str(), root))
{
frameworks = root["frameworks"];
if(frameworks.isNull() || !frameworks.isArray())
{
throw sinsp_exception("mesos_http: Unexpected condition while detecting Mesos master: frameworks entry not found.");
}
}
else
{
std::string errstr;
errstr = reader.getFormattedErrorMessages();
g_logger.log(os.str(), sinsp_logger::SEV_DEBUG);
g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
throw sinsp_exception("mesos_http: Mesos master leader detection failed in get_state_frameworks(): Invalid JSON (" + errstr + ")");
}
}
else
{
throw sinsp_exception(std::string("mesos_http: Mesos master leader [") + m_url.to_string(false) +
"] detection failed: " + curl_easy_strerror(res));
}
return frameworks;
}
void mesos_http::discover_mesos_leader()
{
if(m_is_mesos_state)
{
g_logger.log("mesos_http: Inspecting Mesos leader [" + m_url.to_string(false) + ']', sinsp_logger::SEV_DEBUG);
std::ostringstream os;
CURLcode res = get_data(m_url.to_string(), os);
if(res == CURLE_OK)
{
long http_response_code = 0;
check_error(curl_easy_getinfo(m_sync_curl, CURLINFO_RESPONSE_CODE, &http_response_code));
if(sinsp_curl::is_redirect(http_response_code))
{
uri newurl(m_redirect);
m_url.set_host(newurl.get_host());
g_logger.log("mesos_http: Detected Mesos master leader HTTP redirect: [" + m_url.to_string(false) + ']', sinsp_logger::SEV_INFO);
discover_mesos_leader();
return;
}
Json::Value root;
Json::Reader reader;
if(reader.parse(os.str(), root))
{
const Json::Value& frameworks = root["frameworks"];
if(frameworks.isNull() || !frameworks.isArray())
{
throw sinsp_exception("mesos_http: Unexpected condition while detecting Mesos master: frameworks entry not found.");
}
g_logger.log("Found " + std::to_string(frameworks.size()) + " Mesos frameworks", sinsp_logger::SEV_DEBUG);
if(frameworks.size()) // this is master leader
{
discover_framework_uris(frameworks);
g_logger.log("mesos_http: Found Mesos master leader [" + m_url.to_string(false) + ']', sinsp_logger::SEV_INFO);
return;
}
else if(!m_discover_lead_master) // this is standby server and autodiscovery is disabled
{
throw sinsp_exception("mesos_http: Detected standby Mesos master: autodiscovery not enabled. Giving up (will retry).");
}
else // autodiscovery is enabled, find where is the master
{
const Json::Value& leader = root["leader"];
if(!leader.isNull() && leader.isString())
{
std::string leader_address = leader.asString();
std::string::size_type pos = leader_address.find('@');
if(pos != std::string::npos && (pos + 1) < leader_address.size())
{
std::string address = m_url.get_scheme() + "://";
if(!m_mesos.m_mesos_credentials.first.empty())
{
address.append(m_mesos.m_mesos_credentials.first).append(1, ':').append(m_mesos.m_mesos_credentials.second).append(1, '@');
}
address.append(leader_address.substr(pos + 1)).append(mesos::default_state_api);
if(address != m_url.to_string(true))
{
g_logger.log("mesos_http: Detected Mesos master leader redirect: [" + uri(address).to_string(false) + ']', sinsp_logger::SEV_INFO);
m_url = address;
discover_mesos_leader();
}
else
{
throw sinsp_exception("mesos_http: Mesos master leader not discovered at [" + uri(address).to_string(false) + "] . "
"Giving up temporarily ...");
}
}
else
{
throw sinsp_exception("mesos_http: Unexpected leader entry format while detecting Mesos master: " + leader_address);
}
}
else
{
g_logger.log(os.str(), sinsp_logger::SEV_DEBUG);
throw sinsp_exception("mesos_http: Unexpected condition while detecting Mesos master leader [" + m_url.to_string(false) +
"]: leader entry not found.");
}
}
}
else
{
std::string errstr;
errstr = reader.getFormattedErrorMessages();
g_logger.log(os.str(), sinsp_logger::SEV_DEBUG);
g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
throw sinsp_exception("mesos_http: Mesos master leader [" + m_url.to_string(false) + "] detection failed: Invalid JSON (" + errstr + ")");
}
}
else
{
throw sinsp_exception("mesos_http: Mesos master leader [" + m_url.to_string(false) + "] detection failed: "
+ curl_easy_strerror(res));
}
}
}
std::string mesos_http::get_framework_url(const Json::Value& framework)
{
const Json::Value& fw_name = framework["name"];
bool is_marathon = false;
if(!fw_name.isNull() && fw_name.isConvertibleTo(Json::stringValue))
{
is_marathon = mesos_framework::is_root_marathon(fw_name.asString());
}
bool has_creds = !m_mesos.m_marathon_credentials.first.empty();
Json::Value fw_url = framework["webui_url"];
if(!fw_url.isNull() && fw_url.isString() && !fw_url.asString().empty())
{
uri url(fw_url.asString());
if(is_marathon && has_creds)
{
url.set_credentials(m_mesos.m_marathon_credentials);
}
return url.to_string(true);
}
else
{
fw_url = framework["hostname"];
if(!fw_url.isNull() && fw_url.isString() && !fw_url.asString().empty())
{
uri url(std::string("http://").append(fw_url.asString()).append(":8080"));
if(is_marathon && has_creds)
{
url.set_credentials(m_mesos.m_marathon_credentials);
}
return url.to_string(true);
}
}
return "";
}
bool mesos_http::is_framework_active(const Json::Value& framework)
{
Json::Value active = framework["active"];
if(!active.isNull() && active.isBool() && active.asBool())
{
return true;
}
return false;
}
void mesos_http::discover_framework_uris(const Json::Value& frameworks)
{
m_marathon_uris.clear();
if(frameworks.isNull())
{
throw sinsp_exception("mesos_http: Unexpected condition while inspecting Marathon framework: frameworks entry not found.");
}
if(frameworks.isArray())
{
g_logger.log("Discovered " + std::to_string(frameworks.size()) + " frameworks.", sinsp_logger::SEV_DEBUG);
for(const auto& framework : frameworks)
{
const Json::Value& id = framework["id"];
if(id.isNull() || !id.isString())
{
throw sinsp_exception("mesos_http: Unexpected condition while detecting Marathon framework: ID entry not found.");
}
else
{
const Json::Value& active = framework["active"];
const Json::Value& fw_name = framework["name"];
std::string name;
if(!fw_name.isNull() && fw_name.isString())
{
name = fw_name.asString();
}
g_logger.log("Examining " + name + " [" + id.asString() + "] framework.", sinsp_logger::SEV_DEBUG);
if(!active.isNull() && active.isBool() && active.asBool())
{
std::string framework_url = get_framework_url(framework);
if(!framework_url.empty())
{
if(m_discover_marathon)
{
if(mesos_framework::is_root_marathon(name))
{
g_logger.log(std::string("mesos_http: Found Marathon framework ").append(name).append(" (").append(id.asString()).append(") at [").append(framework_url).append(1, ']'),
sinsp_logger::SEV_INFO);
if(!m_marathon_uris.size())
{
m_marathon_uris.emplace_back(get_framework_url(framework));
}
else
{
g_logger.log("mesos_http: Multiple marathon URIs discovered; only the first one (" + m_marathon_uris[0] + ") will have effect;"
" others will be treated as generic frameworks.", sinsp_logger::SEV_WARNING);
}
}
else
{
g_logger.log(std::string("mesos_http: Skipping non-Marathon framework URL detection ").append(name).append(" (").append(id.asString()).append(1, ')'), sinsp_logger::SEV_DEBUG);
}
}
else
{
g_logger.log(std::string("mesos_http: Marathon detection not enabled."), sinsp_logger::SEV_DEBUG);
}
}
else
{
if(m_discover_marathon && mesos_framework::is_root_marathon(name))
{
std::string errstr = "mesos_http: Can not obtain URL for Marathon framework.";
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
}
}
}
else // framework exists, but is not active - remove it if we were watching it so far
{
g_logger.log(std::string("mesos_http: Mesos framework ").append(name).append(" (").append(id.asString()).append(") deactivated."), sinsp_logger::SEV_INFO);
std::string framework_url = get_framework_url(framework);
for(marathon_uri_t::iterator it = m_marathon_uris.begin(); it != m_marathon_uris.end();)
{
if(framework_url == *it)
{
it = m_marathon_uris.erase(it);
}
else { ++it; }
}
}
}
}
}
else
{
throw sinsp_exception("mesos_http: Mesos master leader detection failed: " + m_url.to_string(false));
}
}
std::string mesos_http::make_request(uri url, curl_version_info_data* curl_version)
{
std::ostringstream request;
std::string host_and_port = url.get_host();
int port = url.get_port();
if(port)
{
host_and_port.append(1, ':').append(std::to_string(port));
}
request << "GET " << url.get_path();
std::string query = url.get_query();
if(!query.empty())
{
request << '?' << query;
}
request << " HTTP/1.1\r\nConnection: Keep-Alive\r\nUser-Agent: " LIBSINSP_USER_AGENT;
if(curl_version && curl_version->version)
{
request << " (curl " << curl_version->version << ')';
}
request << "\r\nHost: " << host_and_port << "\r\nAccept: */*\r\n";
std::string creds = url.get_credentials();
if(!creds.empty())
{
std::istringstream is(creds);
std::ostringstream os;
base64::encoder().encode(is, os);
request << "Authorization: Basic " << os.str() << "\r\n";
}
if(!m_token.empty())
{
request << "Authorization: token=" << m_token << "\r\n";
}
request << "\r\n";
return request.str();
}
CURLcode mesos_http::get_data(const std::string& url, std::ostream& os)
{
g_logger.log(std::string("mesos_http: Retrieving data from ") + uri(url).to_string(false), sinsp_logger::SEV_DEBUG);
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_URL, url.c_str()));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_HEADERDATA, m_redirect));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_HEADERFUNCTION, sinsp_curl::header_callback));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_NOSIGNAL, 1)); //Prevent "longjmp causes uninitialized stack frame" bug
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_ACCEPT_ENCODING, "deflate"));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_TIMEOUT_MS, m_timeout_ms));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_WRITEFUNCTION, sinsp_curl::write_data));
check_error(curl_easy_setopt(m_sync_curl, CURLOPT_WRITEDATA, &os));
return curl_easy_perform(m_sync_curl);
}
bool mesos_http::get_all_data(callback_func_t parse)
{
std::ostringstream os;
CURLcode res = get_data(m_url.to_string(), os);
if(res != CURLE_OK)
{
std::string errstr = std::string("Could not fetch url:") + curl_easy_strerror(res);
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log("", errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
m_connected = false;
}
else
{
// HTTP errors are not returned by curl API
// error will be in the response stream
long http_code = 0;
curl_easy_getinfo(m_sync_curl, CURLINFO_RESPONSE_CODE, &http_code);
if(http_code >= 400)
{
m_connected = false;
return false;
}
else if(sinsp_curl::is_redirect(http_code))
{
g_logger.log("mesos_http: HTTP redirect (" + std::to_string(http_code) + ')', sinsp_logger::SEV_DEBUG);
if(sinsp_curl::handle_redirect(m_url, std::string(m_redirect), os))
{
os.str("");
return get_all_data(parse);
}
}
Json::Reader reader;
json_ptr_t root(new Json::Value());
if(reader.parse(os.str(), *root))
{
(m_mesos.*parse)(root, m_framework_id);
}
else
{
std::string errstr;
errstr = reader.getFormattedErrorMessages();
g_logger.log("mesos_http: Mesos or Marathon Invalid JSON received from [" + m_url.to_string(false) + "]: " + errstr, sinsp_logger::SEV_WARNING);
g_logger.log("JSON: <" + os.str() + '>', sinsp_logger::SEV_DEBUG);
g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
}
m_connected = true;
}
return res == CURLE_OK;
}
int mesos_http::wait(int for_recv)
{
struct timeval tv;
fd_set infd, outfd, errfd;
int res;
tv.tv_sec = m_timeout_ms / 1000;
tv.tv_usec = (m_timeout_ms % 1000) * 1000;
FD_ZERO(&infd);
FD_ZERO(&outfd);
FD_ZERO(&errfd);
FD_SET(m_watch_socket, &errfd);
if(for_recv)
{
FD_SET(m_watch_socket, &infd);
}
else
{
FD_SET(m_watch_socket, &outfd);
}
res = select(m_watch_socket + 1, &infd, &outfd, &errfd, &tv);
return res;
}
int mesos_http::get_socket(long timeout_ms)
{
if(m_request.empty())
{
throw sinsp_exception("mesos_http: Cannot create watch socket (request empty).");
}
if(timeout_ms != -1)
{
m_timeout_ms = timeout_ms;
}
if(m_watch_socket < 0 || !m_connected)
{
long sockextr;
std::string url = get_url().to_string();
check_error(curl_easy_setopt(m_select_curl, CURLOPT_URL, url.c_str()));
check_error(curl_easy_setopt(m_select_curl, CURLOPT_CONNECT_ONLY, 1L));
#if LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 25
// enable TCP keep-alive for this transfer
check_error(curl_easy_setopt(m_select_curl, CURLOPT_TCP_KEEPALIVE, 1L));
// keep-alive idle time
check_error(curl_easy_setopt(m_select_curl, CURLOPT_TCP_KEEPIDLE, 300L));
// interval time between keep-alive probes
check_error(curl_easy_setopt(m_select_curl, CURLOPT_TCP_KEEPINTVL, 10L));
#endif // LIBCURL_VERSION_MAJOR >= 7 && LIBCURL_VERSION_MINOR >= 25
check_error(curl_easy_perform(m_select_curl));
check_error(curl_easy_getinfo(m_select_curl, CURLINFO_LASTSOCKET, &sockextr));
m_watch_socket = sockextr;
if(!wait(0))
{
throw sinsp_exception("mesos_http: Error obtaining socket: timeout.");
}
g_logger.log(std::string("mesos_http: Connected; collecting data from ") + uri(url).to_string(false), sinsp_logger::SEV_DEBUG);
}
if(m_watch_socket <= 0)
{
throw sinsp_exception("mesos_http: Error obtaining socket: " + std::to_string(m_watch_socket));
}
m_connected = true;
return m_watch_socket;
}
void mesos_http::send_request()
{
if(m_request.empty())
{
throw sinsp_exception("mesos_http: Mesos send request (empty).");
}
if(m_watch_socket < 0)
{
throw sinsp_exception("mesos_http: Mesos send invalid socket.");
}
//size_t iolen = send(m_watch_socket, m_request.c_str(), m_request.size(), 0);
size_t iolen;
check_error(curl_easy_send(m_select_curl, m_request.c_str(), m_request.size(), &iolen));
if((iolen <= 0) || (m_request.size() != iolen))
{
throw sinsp_exception("mesos_http: Mesos send socket connection error.");
}
else if(!wait(0))
{
throw sinsp_exception("mesos_http: Mesos send timeout.");
}
g_logger.log(m_request, sinsp_logger::SEV_DEBUG);
}
bool purge_chunked_markers(std::string& data)
{
std::string::size_type pos = data.find("}\r\n0");
if(pos != std::string::npos)
{
data = data.substr(0, pos);
}
const std::string nl = "\r\n";
std::string::size_type begin, end;
while((begin = data.find(nl)) != std::string::npos)
{
end = data.find(nl, begin + 2);
if(end != std::string::npos)
{
data.erase(begin, end + 2 - begin);
}
else // newlines must come in pairs
{
return false;
}
}
return true;
}
void mesos_http::handle_json(std::string::size_type end_pos, bool chunked)
{
if(end_pos != std::string::npos)
{
if(m_data_buf.length() >= end_pos + 1)
{
m_data_buf = m_data_buf.substr(0, end_pos + 1);
if(chunked && !purge_chunked_markers(m_data_buf))
{
std::string errstr = "mesos_http: Invalid Mesos or Marathon JSON data detected (chunked transfer).";
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log(m_data_buf, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
(m_mesos.*m_callback_func)(nullptr, m_framework_id);
}
else
{
(m_mesos.*m_callback_func)(try_parse(m_data_buf, m_url.to_string()), m_framework_id);
}
m_data_buf.clear();
m_content_length = std::string::npos;
}
}
}
bool mesos_http::detect_chunked_transfer(const std::string& data)
{
if(m_content_length == std::string::npos)
{
std::string::size_type cl_pos = data.find("Content-Length:");
if(cl_pos != std::string::npos)
{
std::string::size_type nl_pos = data.find("\r\n", cl_pos);
if(nl_pos != std::string::npos)
{
cl_pos += std::string("Content-Length:").length();
std::string cl = data.substr(cl_pos, nl_pos - cl_pos);
long len = strtol(cl.c_str(), NULL, 10);
if(len == 0L || len == LONG_MAX || len == LONG_MIN || errno == ERANGE)
{
std::string errstr = "Invalid HTTP content length from [: " + m_url.to_string(false) + ']' +
std::to_string(len);
(m_mesos.*m_callback_func)(nullptr, m_framework_id);
m_data_buf.clear();
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log(data, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
return false;
}
else
{
m_content_length = static_cast<std::string::size_type>(len);
}
}
}
}
return true;
}
void mesos_http::extract_data(std::string& data)
{
if(!detect_chunked_transfer(data))
{
string errstr = "mesos_http: An error occurred while detecting chunked transfer.";
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log(data, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
return;
}
if(m_data_buf.empty())
{
m_data_buf = data;
std::string::size_type pos = m_data_buf.find("\r\n{");
if(pos != std::string::npos) // JSON begin
{
m_data_buf = m_data_buf.substr(pos + 2);
}
}
else
{
m_data_buf.append(data);
}
bool chunked = (m_content_length == std::string::npos);
if(chunked)
{
handle_json(m_data_buf.find("}\r\n0"), true);
}
else if (m_data_buf.length() >= m_content_length)
{
handle_json(m_data_buf.length() - 1, false);
}
return;
}
bool mesos_http::on_data()
{
if(!m_callback_func)
{
throw sinsp_exception("mesos_http: Cannot parse data (parse function null).");
}
size_t iolen = 0;
char buf[1024];
buf[0] = '\0';
std::string data;
CURLcode ret;
std::string errstr;
try
{
do
{
check_error(ret = curl_easy_recv(m_select_curl, buf, sizeof(buf), &iolen));
if(iolen > 0)
{
data.append(buf, iolen);
}
else if(ret != CURLE_AGAIN) { goto connection_closed; }
} while(iolen && ret != CURLE_AGAIN);
if(data.size())
{
extract_data(data);
}
}
catch(const sinsp_exception& ex)
{
errstr = std::string("mesos_http: Data receive error [" + m_url.to_string() + "]: ").append(ex.what());
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log(buf, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
return false;
}
return true;
connection_closed:
errstr = "mesos_http: Mesos or Marathon API connection [" + m_url.to_string() + "] closed.";
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log(buf, errstr, sinsp_utils::get_current_time_ns(), m_url.to_string());
m_connected = false;
return false;
}
void mesos_http::on_error(const std::string& /*err*/, bool /*disconnect*/)
{
m_connected = false;
}
void mesos_http::check_error(CURLcode res)
{
if(CURLE_OK != res && CURLE_AGAIN != res)
{
m_connected = false;
std::ostringstream os;
os << "Error: " << curl_easy_strerror(res);
throw sinsp_exception(os.str());
}
}
std::string mesos_http::make_uri(const std::string& path)
{
uri url = get_url();
std::string target_uri = url.get_scheme();
target_uri.append("://");
std::string user = url.get_user();
if(!user.empty())
{
target_uri.append(user).append(1, ':').append(url.get_password()).append(1, '@');
}
target_uri.append(url.get_host());
int port = url.get_port();
if(port)
{
target_uri.append(1, ':').append(std::to_string(port));
}
target_uri.append(path);
return target_uri;
}
Json::Value mesos_http::get_task_labels(const std::string& task_id)
{
std::ostringstream os;
std::string uri = make_uri("/master/tasks");
CURLcode res = get_data(uri, os);
Json::Value labels;
if(res != CURLE_OK)
{
std::string errstr = curl_easy_strerror(res);
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log(task_id, errstr, sinsp_utils::get_current_time_ns(), uri);
return labels;
}
try
{
Json::Value root;
Json::Reader reader;
if(reader.parse(os.str(), root, false))
{
Json::Value tasks = root["tasks"];
if(!tasks.isNull())
{
for(const auto& task : tasks)
{
Json::Value id = task["id"];
if(!id.isNull() && id.isString() && id.asString() == task_id)
{
Json::Value statuses = task["statuses"];
if(!statuses.isNull())
{
double tstamp = 0.0;
for(const auto& status : statuses)
{
// only task with most recent status
// "TASK_RUNNING" considered
Json::Value ts = status["timestamp"];
if(!ts.isNull() && ts.isNumeric() && ts.asDouble() > tstamp)
{
Json::Value st = status["state"];
if(!st.isNull() && st.isString())
{
if(st.asString() == "TASK_RUNNING")
{
labels = task["labels"];
tstamp = ts.asDouble();
}
else
{
labels.clear();
}
}
}
}
if(!labels.empty()) // currently running task found
{
return labels;
}
}
}
}
}
}
else
{
std::string errstr;
errstr = "mesos_http: Error parsing tasks (" + reader.getFormattedErrorMessages() + ").";
g_logger.log(errstr + "\nJSON:\n---\n" + os.str() + "\n---", sinsp_logger::SEV_ERROR);
g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), uri);
}
}
catch(const std::exception& ex)
{
std::string errstr = std::string("mesos_http: Error parsing tasks:") + ex.what();
g_logger.log(errstr, sinsp_logger::SEV_ERROR);
g_json_error_log.log(os.str(), errstr, sinsp_utils::get_current_time_ns(), uri);
}
return labels;
}
#endif // HAS_CAPTURE
#endif // CYGWING_AGENT
|