web_api_v2.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "web_api_v2.h"
  3. #include "../rtc/webrtc.h"
  4. #define BEARER_TOKEN_EXPIRATION 86400
  5. struct bearer_token {
  6. time_t created_s;
  7. time_t expires_s;
  8. };
  9. static void bearer_token_cleanup(void) {
  10. static time_t attempts = 0;
  11. if(++attempts % 1000 != 0)
  12. return;
  13. time_t now_s = now_monotonic_sec();
  14. struct bearer_token *z;
  15. dfe_start_read(netdata_authorized_bearers, z) {
  16. if(z->expires_s < now_s)
  17. dictionary_del(netdata_authorized_bearers, z_dfe.name);
  18. }
  19. dfe_done(z);
  20. dictionary_garbage_collect(netdata_authorized_bearers);
  21. }
  22. void bearer_tokens_init(void) {
  23. netdata_authorized_bearers = dictionary_create_advanced(
  24. DICT_OPTION_DONT_OVERWRITE_VALUE | DICT_OPTION_FIXED_SIZE,
  25. NULL, sizeof(struct bearer_token));
  26. }
  27. static time_t bearer_get_token(uuid_t *uuid) {
  28. char uuid_str[UUID_STR_LEN];
  29. uuid_generate_random(*uuid);
  30. uuid_unparse_lower(*uuid, uuid_str);
  31. struct bearer_token t = { 0 }, *z;
  32. z = dictionary_set(netdata_authorized_bearers, uuid_str, &t, sizeof(t));
  33. if(!z->created_s) {
  34. z->created_s = now_monotonic_sec();
  35. z->expires_s = z->created_s + BEARER_TOKEN_EXPIRATION;
  36. }
  37. bearer_token_cleanup();
  38. return now_realtime_sec() + BEARER_TOKEN_EXPIRATION;
  39. }
  40. #define HTTP_REQUEST_AUTHORIZATION_BEARER "\r\nAuthorization: Bearer "
  41. bool extract_bearer_token_from_request(struct web_client *w, char *dst, size_t dst_len) {
  42. const char *req = buffer_tostring(w->response.data);
  43. size_t req_len = buffer_strlen(w->response.data);
  44. const char *bearer = strcasestr(req, HTTP_REQUEST_AUTHORIZATION_BEARER);
  45. if(!bearer)
  46. return false;
  47. const char *token_start = bearer + sizeof(HTTP_REQUEST_AUTHORIZATION_BEARER) - 1;
  48. while(isspace(*token_start))
  49. token_start++;
  50. const char *token_end = token_start + UUID_STR_LEN - 1 + 2;
  51. if (token_end > req + req_len)
  52. return false;
  53. strncpyz(dst, token_start, dst_len - 1);
  54. uuid_t uuid;
  55. if (uuid_parse(dst, uuid) != 0)
  56. return false;
  57. return true;
  58. }
  59. bool api_check_bearer_token(struct web_client *w) {
  60. if(!netdata_authorized_bearers)
  61. return false;
  62. char token[UUID_STR_LEN];
  63. if(!extract_bearer_token_from_request(w, token, sizeof(token)))
  64. return false;
  65. struct bearer_token *z = dictionary_get(netdata_authorized_bearers, token);
  66. return z && z->expires_s > now_monotonic_sec();
  67. }
  68. static bool verify_agent_uuids(const char *machine_guid, const char *node_id, const char *claim_id) {
  69. if(!machine_guid || !node_id || !claim_id)
  70. return false;
  71. if(strcmp(machine_guid, localhost->machine_guid) != 0)
  72. return false;
  73. char *agent_claim_id = get_agent_claimid();
  74. bool not_verified = (!agent_claim_id || strcmp(claim_id, agent_claim_id) != 0);
  75. freez(agent_claim_id);
  76. if(not_verified || !localhost->node_id)
  77. return false;
  78. char buf[UUID_STR_LEN];
  79. uuid_unparse_lower(*localhost->node_id, buf);
  80. if(strcmp(node_id, buf) != 0)
  81. return false;
  82. return true;
  83. }
  84. int api_v2_bearer_protection(RRDHOST *host __maybe_unused, struct web_client *w __maybe_unused, char *url) {
  85. char *machine_guid = NULL;
  86. char *claim_id = NULL;
  87. char *node_id = NULL;
  88. bool protection = netdata_is_protected_by_bearer;
  89. while (url) {
  90. char *value = strsep_skip_consecutive_separators(&url, "&");
  91. if (!value || !*value) continue;
  92. char *name = strsep_skip_consecutive_separators(&value, "=");
  93. if (!name || !*name) continue;
  94. if (!value || !*value) continue;
  95. if(!strcmp(name, "bearer_protection")) {
  96. if(!strcmp(value, "on") || !strcmp(value, "true") || !strcmp(value, "yes"))
  97. protection = true;
  98. else
  99. protection = false;
  100. }
  101. else if(!strcmp(name, "machine_guid"))
  102. machine_guid = value;
  103. else if(!strcmp(name, "claim_id"))
  104. claim_id = value;
  105. else if(!strcmp(name, "node_id"))
  106. node_id = value;
  107. }
  108. if(!verify_agent_uuids(machine_guid, node_id, claim_id)) {
  109. buffer_flush(w->response.data);
  110. buffer_strcat(w->response.data, "The request is missing or not matching local UUIDs");
  111. return HTTP_RESP_BAD_REQUEST;
  112. }
  113. netdata_is_protected_by_bearer = protection;
  114. BUFFER *wb = w->response.data;
  115. buffer_flush(wb);
  116. buffer_json_initialize(wb, "\"", "\"", 0, true, false);
  117. buffer_json_member_add_boolean(wb, "bearer_protection", netdata_is_protected_by_bearer);
  118. buffer_json_finalize(wb);
  119. return HTTP_RESP_OK;
  120. }
  121. int api_v2_bearer_token(RRDHOST *host __maybe_unused, struct web_client *w __maybe_unused, char *url __maybe_unused) {
  122. char *machine_guid = NULL;
  123. char *claim_id = NULL;
  124. char *node_id = NULL;
  125. while(url) {
  126. char *value = strsep_skip_consecutive_separators(&url, "&");
  127. if (!value || !*value) continue;
  128. char *name = strsep_skip_consecutive_separators(&value, "=");
  129. if (!name || !*name) continue;
  130. if (!value || !*value) continue;
  131. if(!strcmp(name, "machine_guid"))
  132. machine_guid = value;
  133. else if(!strcmp(name, "claim_id"))
  134. claim_id = value;
  135. else if(!strcmp(name, "node_id"))
  136. node_id = value;
  137. }
  138. if(!verify_agent_uuids(machine_guid, node_id, claim_id)) {
  139. buffer_flush(w->response.data);
  140. buffer_strcat(w->response.data, "The request is missing or not matching local UUIDs");
  141. return HTTP_RESP_BAD_REQUEST;
  142. }
  143. uuid_t uuid;
  144. time_t expires_s = bearer_get_token(&uuid);
  145. BUFFER *wb = w->response.data;
  146. buffer_flush(wb);
  147. buffer_json_initialize(wb, "\"", "\"", 0, true, false);
  148. buffer_json_member_add_string(wb, "mg", localhost->machine_guid);
  149. buffer_json_member_add_boolean(wb, "bearer_protection", netdata_is_protected_by_bearer);
  150. buffer_json_member_add_uuid(wb, "token", &uuid);
  151. buffer_json_member_add_time_t(wb, "expiration", expires_s);
  152. buffer_json_finalize(wb);
  153. return HTTP_RESP_OK;
  154. }
  155. static int web_client_api_request_v2_contexts_internal(RRDHOST *host __maybe_unused, struct web_client *w, char *url, CONTEXTS_V2_MODE mode) {
  156. struct api_v2_contexts_request req = { 0 };
  157. while(url) {
  158. char *value = strsep_skip_consecutive_separators(&url, "&");
  159. if(!value || !*value) continue;
  160. char *name = strsep_skip_consecutive_separators(&value, "=");
  161. if(!name || !*name) continue;
  162. if(!value || !*value) continue;
  163. // name and value are now the parameters
  164. // they are not null and not empty
  165. if(!strcmp(name, "scope_nodes"))
  166. req.scope_nodes = value;
  167. else if(!strcmp(name, "nodes"))
  168. req.nodes = value;
  169. else if((mode & (CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_ALERTS | CONTEXTS_V2_ALERT_TRANSITIONS)) && !strcmp(name, "scope_contexts"))
  170. req.scope_contexts = value;
  171. else if((mode & (CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_SEARCH | CONTEXTS_V2_ALERTS | CONTEXTS_V2_ALERT_TRANSITIONS)) && !strcmp(name, "contexts"))
  172. req.contexts = value;
  173. else if((mode & CONTEXTS_V2_SEARCH) && !strcmp(name, "q"))
  174. req.q = value;
  175. else if(!strcmp(name, "options"))
  176. req.options = web_client_api_request_v2_context_options(value);
  177. else if(!strcmp(name, "after"))
  178. req.after = str2l(value);
  179. else if(!strcmp(name, "before"))
  180. req.before = str2l(value);
  181. else if(!strcmp(name, "timeout"))
  182. req.timeout_ms = str2l(value);
  183. else if(mode & (CONTEXTS_V2_ALERTS | CONTEXTS_V2_ALERT_TRANSITIONS)) {
  184. if (!strcmp(name, "alert"))
  185. req.alerts.alert = value;
  186. else if (!strcmp(name, "transition"))
  187. req.alerts.transition = value;
  188. else if(mode & CONTEXTS_V2_ALERTS) {
  189. if (!strcmp(name, "status"))
  190. req.alerts.status = web_client_api_request_v2_alert_status(value);
  191. }
  192. else if(mode & CONTEXTS_V2_ALERT_TRANSITIONS) {
  193. if (!strcmp(name, "last"))
  194. req.alerts.last = strtoul(value, NULL, 0);
  195. else if(!strcmp(name, "context"))
  196. req.contexts = value;
  197. else if (!strcmp(name, "anchor_gi")) {
  198. req.alerts.global_id_anchor = str2ull(value, NULL);
  199. }
  200. else {
  201. for(int i = 0; i < ATF_TOTAL_ENTRIES ;i++) {
  202. if(!strcmp(name, alert_transition_facets[i].query_param))
  203. req.alerts.facets[i] = value;
  204. }
  205. }
  206. }
  207. }
  208. }
  209. if ((mode & CONTEXTS_V2_ALERT_TRANSITIONS) && !req.alerts.last)
  210. req.alerts.last = 1;
  211. buffer_flush(w->response.data);
  212. buffer_no_cacheable(w->response.data);
  213. return rrdcontext_to_json_v2(w->response.data, &req, mode);
  214. }
  215. static int web_client_api_request_v2_alert_transitions(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  216. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_ALERT_TRANSITIONS | CONTEXTS_V2_NODES);
  217. }
  218. static int web_client_api_request_v2_alerts(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  219. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_ALERTS | CONTEXTS_V2_NODES);
  220. }
  221. static int web_client_api_request_v2_functions(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  222. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_FUNCTIONS | CONTEXTS_V2_NODES | CONTEXTS_V2_AGENTS | CONTEXTS_V2_VERSIONS);
  223. }
  224. static int web_client_api_request_v2_versions(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  225. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_VERSIONS);
  226. }
  227. static int web_client_api_request_v2_q(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  228. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_SEARCH | CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_NODES | CONTEXTS_V2_AGENTS | CONTEXTS_V2_VERSIONS);
  229. }
  230. static int web_client_api_request_v2_contexts(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  231. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_CONTEXTS | CONTEXTS_V2_NODES | CONTEXTS_V2_AGENTS | CONTEXTS_V2_VERSIONS);
  232. }
  233. static int web_client_api_request_v2_nodes(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  234. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_NODES | CONTEXTS_V2_NODES_INFO);
  235. }
  236. static int web_client_api_request_v2_info(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  237. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_AGENTS | CONTEXTS_V2_AGENTS_INFO);
  238. }
  239. static int web_client_api_request_v2_node_instances(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  240. return web_client_api_request_v2_contexts_internal(host, w, url, CONTEXTS_V2_NODES | CONTEXTS_V2_NODE_INSTANCES | CONTEXTS_V2_AGENTS | CONTEXTS_V2_AGENTS_INFO | CONTEXTS_V2_VERSIONS);
  241. }
  242. static int web_client_api_request_v2_weights(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  243. return web_client_api_request_weights(host, w, url, WEIGHTS_METHOD_VALUE, WEIGHTS_FORMAT_MULTINODE, 2);
  244. }
  245. static int web_client_api_request_v2_claim(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  246. return api_v2_claim(w, url);
  247. }
  248. static int web_client_api_request_v2_alert_config(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  249. const char *config = NULL;
  250. while(url) {
  251. char *value = strsep_skip_consecutive_separators(&url, "&");
  252. if(!value || !*value) continue;
  253. char *name = strsep_skip_consecutive_separators(&value, "=");
  254. if(!name || !*name) continue;
  255. if(!value || !*value) continue;
  256. // name and value are now the parameters
  257. // they are not null and not empty
  258. if(!strcmp(name, "config"))
  259. config = value;
  260. }
  261. buffer_flush(w->response.data);
  262. if(!config) {
  263. w->response.data->content_type = CT_TEXT_PLAIN;
  264. buffer_strcat(w->response.data, "A config hash ID is required. Add ?config=UUID query param");
  265. return HTTP_RESP_BAD_REQUEST;
  266. }
  267. return contexts_v2_alert_config_to_json(w, config);
  268. }
  269. #define GROUP_BY_KEY_MAX_LENGTH 30
  270. static struct {
  271. char group_by[GROUP_BY_KEY_MAX_LENGTH + 1];
  272. char aggregation[GROUP_BY_KEY_MAX_LENGTH + 1];
  273. char group_by_label[GROUP_BY_KEY_MAX_LENGTH + 1];
  274. } group_by_keys[MAX_QUERY_GROUP_BY_PASSES];
  275. __attribute__((constructor)) void initialize_group_by_keys(void) {
  276. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  277. snprintfz(group_by_keys[g].group_by, GROUP_BY_KEY_MAX_LENGTH, "group_by[%zu]", g);
  278. snprintfz(group_by_keys[g].aggregation, GROUP_BY_KEY_MAX_LENGTH, "aggregation[%zu]", g);
  279. snprintfz(group_by_keys[g].group_by_label, GROUP_BY_KEY_MAX_LENGTH, "group_by_label[%zu]", g);
  280. }
  281. }
  282. static int web_client_api_request_v2_data(RRDHOST *host __maybe_unused, struct web_client *w, char *url) {
  283. usec_t received_ut = now_monotonic_usec();
  284. int ret = HTTP_RESP_BAD_REQUEST;
  285. buffer_flush(w->response.data);
  286. char *google_version = "0.6",
  287. *google_reqId = "0",
  288. *google_sig = "0",
  289. *google_out = "json",
  290. *responseHandler = NULL,
  291. *outFileName = NULL;
  292. time_t last_timestamp_in_data = 0, google_timestamp = 0;
  293. char *scope_nodes = NULL;
  294. char *scope_contexts = NULL;
  295. char *nodes = NULL;
  296. char *contexts = NULL;
  297. char *instances = NULL;
  298. char *dimensions = NULL;
  299. char *before_str = NULL;
  300. char *after_str = NULL;
  301. char *resampling_time_str = NULL;
  302. char *points_str = NULL;
  303. char *timeout_str = NULL;
  304. char *labels = NULL;
  305. char *alerts = NULL;
  306. char *time_group_options = NULL;
  307. char *tier_str = NULL;
  308. size_t tier = 0;
  309. RRDR_TIME_GROUPING time_group = RRDR_GROUPING_AVERAGE;
  310. DATASOURCE_FORMAT format = DATASOURCE_JSON2;
  311. RRDR_OPTIONS options = RRDR_OPTION_VIRTUAL_POINTS | RRDR_OPTION_JSON_WRAP | RRDR_OPTION_RETURN_JWAR;
  312. struct group_by_pass group_by[MAX_QUERY_GROUP_BY_PASSES] = {
  313. {
  314. .group_by = RRDR_GROUP_BY_DIMENSION,
  315. .group_by_label = NULL,
  316. .aggregation = RRDR_GROUP_BY_FUNCTION_AVERAGE,
  317. },
  318. };
  319. size_t group_by_idx = 0, group_by_label_idx = 0, aggregation_idx = 0;
  320. while(url) {
  321. char *value = strsep_skip_consecutive_separators(&url, "&");
  322. if(!value || !*value) continue;
  323. char *name = strsep_skip_consecutive_separators(&value, "=");
  324. if(!name || !*name) continue;
  325. if(!value || !*value) continue;
  326. // name and value are now the parameters
  327. // they are not null and not empty
  328. if(!strcmp(name, "scope_nodes")) scope_nodes = value;
  329. else if(!strcmp(name, "scope_contexts")) scope_contexts = value;
  330. else if(!strcmp(name, "nodes")) nodes = value;
  331. else if(!strcmp(name, "contexts")) contexts = value;
  332. else if(!strcmp(name, "instances")) instances = value;
  333. else if(!strcmp(name, "dimensions")) dimensions = value;
  334. else if(!strcmp(name, "labels")) labels = value;
  335. else if(!strcmp(name, "alerts")) alerts = value;
  336. else if(!strcmp(name, "after")) after_str = value;
  337. else if(!strcmp(name, "before")) before_str = value;
  338. else if(!strcmp(name, "points")) points_str = value;
  339. else if(!strcmp(name, "timeout")) timeout_str = value;
  340. else if(!strcmp(name, "group_by")) {
  341. group_by[group_by_idx++].group_by = group_by_parse(value);
  342. if(group_by_idx >= MAX_QUERY_GROUP_BY_PASSES)
  343. group_by_idx = MAX_QUERY_GROUP_BY_PASSES - 1;
  344. }
  345. else if(!strcmp(name, "group_by_label")) {
  346. group_by[group_by_label_idx++].group_by_label = value;
  347. if(group_by_label_idx >= MAX_QUERY_GROUP_BY_PASSES)
  348. group_by_label_idx = MAX_QUERY_GROUP_BY_PASSES - 1;
  349. }
  350. else if(!strcmp(name, "aggregation")) {
  351. group_by[aggregation_idx++].aggregation = group_by_aggregate_function_parse(value);
  352. if(aggregation_idx >= MAX_QUERY_GROUP_BY_PASSES)
  353. aggregation_idx = MAX_QUERY_GROUP_BY_PASSES - 1;
  354. }
  355. else if(!strcmp(name, "format")) format = web_client_api_request_v1_data_format(value);
  356. else if(!strcmp(name, "options")) options |= web_client_api_request_v1_data_options(value);
  357. else if(!strcmp(name, "time_group")) time_group = time_grouping_parse(value, RRDR_GROUPING_AVERAGE);
  358. else if(!strcmp(name, "time_group_options")) time_group_options = value;
  359. else if(!strcmp(name, "time_resampling")) resampling_time_str = value;
  360. else if(!strcmp(name, "tier")) tier_str = value;
  361. else if(!strcmp(name, "callback")) responseHandler = value;
  362. else if(!strcmp(name, "filename")) outFileName = value;
  363. else if(!strcmp(name, "tqx")) {
  364. // parse Google Visualization API options
  365. // https://developers.google.com/chart/interactive/docs/dev/implementing_data_source
  366. char *tqx_name, *tqx_value;
  367. while(value) {
  368. tqx_value = strsep_skip_consecutive_separators(&value, ";");
  369. if(!tqx_value || !*tqx_value) continue;
  370. tqx_name = strsep_skip_consecutive_separators(&tqx_value, ":");
  371. if(!tqx_name || !*tqx_name) continue;
  372. if(!tqx_value || !*tqx_value) continue;
  373. if(!strcmp(tqx_name, "version"))
  374. google_version = tqx_value;
  375. else if(!strcmp(tqx_name, "reqId"))
  376. google_reqId = tqx_value;
  377. else if(!strcmp(tqx_name, "sig")) {
  378. google_sig = tqx_value;
  379. google_timestamp = strtoul(google_sig, NULL, 0);
  380. }
  381. else if(!strcmp(tqx_name, "out")) {
  382. google_out = tqx_value;
  383. format = web_client_api_request_v1_data_google_format(google_out);
  384. }
  385. else if(!strcmp(tqx_name, "responseHandler"))
  386. responseHandler = tqx_value;
  387. else if(!strcmp(tqx_name, "outFileName"))
  388. outFileName = tqx_value;
  389. }
  390. }
  391. else {
  392. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  393. if(!strcmp(name, group_by_keys[g].group_by))
  394. group_by[g].group_by = group_by_parse(value);
  395. else if(!strcmp(name, group_by_keys[g].group_by_label))
  396. group_by[g].group_by_label = value;
  397. else if(!strcmp(name, group_by_keys[g].aggregation))
  398. group_by[g].aggregation = group_by_aggregate_function_parse(value);
  399. }
  400. }
  401. }
  402. // validate the google parameters given
  403. fix_google_param(google_out);
  404. fix_google_param(google_sig);
  405. fix_google_param(google_reqId);
  406. fix_google_param(google_version);
  407. fix_google_param(responseHandler);
  408. fix_google_param(outFileName);
  409. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  410. if (group_by[g].group_by_label && *group_by[g].group_by_label)
  411. group_by[g].group_by |= RRDR_GROUP_BY_LABEL;
  412. }
  413. if(group_by[0].group_by == RRDR_GROUP_BY_NONE)
  414. group_by[0].group_by = RRDR_GROUP_BY_DIMENSION;
  415. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++) {
  416. if ((group_by[g].group_by & ~(RRDR_GROUP_BY_DIMENSION)) || (options & RRDR_OPTION_PERCENTAGE)) {
  417. options |= RRDR_OPTION_ABSOLUTE;
  418. break;
  419. }
  420. }
  421. if(options & RRDR_OPTION_DEBUG)
  422. options &= ~RRDR_OPTION_MINIFY;
  423. if(tier_str && *tier_str) {
  424. tier = str2ul(tier_str);
  425. if(tier < storage_tiers)
  426. options |= RRDR_OPTION_SELECTED_TIER;
  427. else
  428. tier = 0;
  429. }
  430. time_t before = (before_str && *before_str)?str2l(before_str):0;
  431. time_t after = (after_str && *after_str) ?str2l(after_str):-600;
  432. size_t points = (points_str && *points_str)?str2u(points_str):0;
  433. int timeout = (timeout_str && *timeout_str)?str2i(timeout_str): 0;
  434. time_t resampling_time = (resampling_time_str && *resampling_time_str) ? str2l(resampling_time_str) : 0;
  435. QUERY_TARGET_REQUEST qtr = {
  436. .version = 2,
  437. .scope_nodes = scope_nodes,
  438. .scope_contexts = scope_contexts,
  439. .after = after,
  440. .before = before,
  441. .host = NULL,
  442. .st = NULL,
  443. .nodes = nodes,
  444. .contexts = contexts,
  445. .instances = instances,
  446. .dimensions = dimensions,
  447. .alerts = alerts,
  448. .timeout_ms = timeout,
  449. .points = points,
  450. .format = format,
  451. .options = options,
  452. .time_group_method = time_group,
  453. .time_group_options = time_group_options,
  454. .resampling_time = resampling_time,
  455. .tier = tier,
  456. .chart_label_key = NULL,
  457. .labels = labels,
  458. .query_source = QUERY_SOURCE_API_DATA,
  459. .priority = STORAGE_PRIORITY_NORMAL,
  460. .received_ut = received_ut,
  461. .interrupt_callback = web_client_interrupt_callback,
  462. .interrupt_callback_data = w,
  463. };
  464. for(size_t g = 0; g < MAX_QUERY_GROUP_BY_PASSES ;g++)
  465. qtr.group_by[g] = group_by[g];
  466. QUERY_TARGET *qt = query_target_create(&qtr);
  467. ONEWAYALLOC *owa = NULL;
  468. if(!qt) {
  469. buffer_sprintf(w->response.data, "Failed to prepare the query.");
  470. ret = HTTP_RESP_INTERNAL_SERVER_ERROR;
  471. goto cleanup;
  472. }
  473. web_client_timeout_checkpoint_set(w, timeout);
  474. if(web_client_timeout_checkpoint_and_check(w, NULL)) {
  475. ret = w->response.code;
  476. goto cleanup;
  477. }
  478. if(outFileName && *outFileName) {
  479. buffer_sprintf(w->response.header, "Content-Disposition: attachment; filename=\"%s\"\r\n", outFileName);
  480. netdata_log_debug(D_WEB_CLIENT, "%llu: generating outfilename header: '%s'", w->id, outFileName);
  481. }
  482. if(format == DATASOURCE_DATATABLE_JSONP) {
  483. if(responseHandler == NULL)
  484. responseHandler = "google.visualization.Query.setResponse";
  485. netdata_log_debug(D_WEB_CLIENT_ACCESS, "%llu: GOOGLE JSON/JSONP: version = '%s', reqId = '%s', sig = '%s', out = '%s', responseHandler = '%s', outFileName = '%s'",
  486. w->id, google_version, google_reqId, google_sig, google_out, responseHandler, outFileName
  487. );
  488. buffer_sprintf(
  489. w->response.data,
  490. "%s({version:'%s',reqId:'%s',status:'ok',sig:'%"PRId64"',table:",
  491. responseHandler,
  492. google_version,
  493. google_reqId,
  494. (int64_t)now_realtime_sec());
  495. }
  496. else if(format == DATASOURCE_JSONP) {
  497. if(responseHandler == NULL)
  498. responseHandler = "callback";
  499. buffer_strcat(w->response.data, responseHandler);
  500. buffer_strcat(w->response.data, "(");
  501. }
  502. owa = onewayalloc_create(0);
  503. ret = data_query_execute(owa, w->response.data, qt, &last_timestamp_in_data);
  504. if(format == DATASOURCE_DATATABLE_JSONP) {
  505. if(google_timestamp < last_timestamp_in_data)
  506. buffer_strcat(w->response.data, "});");
  507. else {
  508. // the client already has the latest data
  509. buffer_flush(w->response.data);
  510. buffer_sprintf(w->response.data,
  511. "%s({version:'%s',reqId:'%s',status:'error',errors:[{reason:'not_modified',message:'Data not modified'}]});",
  512. responseHandler, google_version, google_reqId);
  513. }
  514. }
  515. else if(format == DATASOURCE_JSONP)
  516. buffer_strcat(w->response.data, ");");
  517. cleanup:
  518. query_target_release(qt);
  519. onewayalloc_destroy(owa);
  520. return ret;
  521. }
  522. static int web_client_api_request_v2_webrtc(RRDHOST *host __maybe_unused, struct web_client *w, char *url __maybe_unused) {
  523. return webrtc_new_connection(w->post_payload, w->response.data);
  524. }
  525. static struct web_api_command api_commands_v2[] = {
  526. {"info", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_info},
  527. {"data", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_data},
  528. {"weights", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_weights},
  529. {"contexts", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_contexts},
  530. {"nodes", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_nodes},
  531. {"node_instances", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_node_instances},
  532. {"versions", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_versions},
  533. {"functions", 0, WEB_CLIENT_ACL_ACLK_WEBRTC_DASHBOARD_WITH_BEARER | ACL_DEV_OPEN_ACCESS, web_client_api_request_v2_functions},
  534. {"q", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_q},
  535. {"alerts", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_alerts},
  536. {"alert_transitions", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_alert_transitions},
  537. {"alert_config", 0, WEB_CLIENT_ACL_DASHBOARD_ACLK_WEBRTC, web_client_api_request_v2_alert_config},
  538. {"claim", 0, WEB_CLIENT_ACL_NOCHECK, web_client_api_request_v2_claim},
  539. {"rtc_offer", 0, WEB_CLIENT_ACL_ACLK | ACL_DEV_OPEN_ACCESS, web_client_api_request_v2_webrtc},
  540. {"bearer_protection", 0, WEB_CLIENT_ACL_ACLK | ACL_DEV_OPEN_ACCESS, api_v2_bearer_protection},
  541. {"bearer_get_token", 0, WEB_CLIENT_ACL_ACLK | ACL_DEV_OPEN_ACCESS, api_v2_bearer_token},
  542. // terminator
  543. {NULL, 0, WEB_CLIENT_ACL_NONE, NULL},
  544. };
  545. inline int web_client_api_request_v2(RRDHOST *host, struct web_client *w, char *url_path_endpoint) {
  546. static int initialized = 0;
  547. if(unlikely(initialized == 0)) {
  548. initialized = 1;
  549. for(int i = 0; api_commands_v2[i].command ; i++)
  550. api_commands_v2[i].hash = simple_hash(api_commands_v2[i].command);
  551. }
  552. return web_client_api_request_vX(host, w, url_path_endpoint, api_commands_v2);
  553. }