web_api.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "web_api.h"
  3. bool netdata_is_protected_by_bearer = false; // this is controlled by cloud, at the point the agent logs in - this should also be saved to /var/lib/netdata
  4. DICTIONARY *netdata_authorized_bearers = NULL;
  5. static short int web_client_check_acl_and_bearer(struct web_client *w, WEB_CLIENT_ACL endpoint_acl) {
  6. if(endpoint_acl == WEB_CLIENT_ACL_NONE || (endpoint_acl & WEB_CLIENT_ACL_NOCHECK))
  7. // the endpoint is totally public
  8. return HTTP_RESP_OK;
  9. bool acl_allows = w->acl & endpoint_acl;
  10. if(!acl_allows)
  11. // the channel we received the request from (w->acl) is not compatible with the endpoint
  12. return HTTP_RESP_FORBIDDEN;
  13. if(!netdata_is_protected_by_bearer && !(endpoint_acl & WEB_CLIENT_ACL_BEARER_REQUIRED))
  14. // bearer protection is not enabled and is not required by the endpoint
  15. return HTTP_RESP_OK;
  16. if(!(endpoint_acl & (WEB_CLIENT_ACL_BEARER_REQUIRED|WEB_CLIENT_ACL_BEARER_OPTIONAL)))
  17. // endpoint does not require a bearer
  18. return HTTP_RESP_OK;
  19. if((w->acl & (WEB_CLIENT_ACL_ACLK|WEB_CLIENT_ACL_WEBRTC)))
  20. // the request is coming from ACLK or WEBRTC (authorized already),
  21. return HTTP_RESP_OK;
  22. // at this point we need a bearer to serve the request
  23. // either because:
  24. //
  25. // 1. WEB_CLIENT_ACL_BEARER_REQUIRED, or
  26. // 2. netdata_is_protected_by_bearer == true
  27. //
  28. BEARER_STATUS t = api_check_bearer_token(w);
  29. if(t == BEARER_STATUS_AVAILABLE_AND_VALIDATED)
  30. // we have a valid bearer on the request
  31. return HTTP_RESP_OK;
  32. netdata_log_info("BEARER: bearer is required for request: code %d", t);
  33. return HTTP_RESP_PRECOND_FAIL;
  34. }
  35. int web_client_api_request_vX(RRDHOST *host, struct web_client *w, char *url_path_endpoint, struct web_api_command *api_commands) {
  36. buffer_no_cacheable(w->response.data);
  37. if(unlikely(!url_path_endpoint || !*url_path_endpoint)) {
  38. buffer_flush(w->response.data);
  39. buffer_sprintf(w->response.data, "Which API command?");
  40. return HTTP_RESP_BAD_REQUEST;
  41. }
  42. char *api_command = strchr(url_path_endpoint, '/');
  43. if (likely(api_command == NULL)) // only config command supports subpaths for now
  44. api_command = url_path_endpoint;
  45. else {
  46. size_t api_command_len = api_command - url_path_endpoint;
  47. api_command = callocz(1, api_command_len + 1);
  48. memcpy(api_command, url_path_endpoint, api_command_len);
  49. }
  50. uint32_t hash = simple_hash(api_command);
  51. for(int i = 0; api_commands[i].command ; i++) {
  52. if(unlikely(hash == api_commands[i].hash && !strcmp(api_command, api_commands[i].command))) {
  53. if(unlikely(!api_commands[i].allow_subpaths && api_command != url_path_endpoint)) {
  54. buffer_flush(w->response.data);
  55. buffer_sprintf(w->response.data, "API command '%s' does not support subpaths.", api_command);
  56. freez(api_command);
  57. return HTTP_RESP_BAD_REQUEST;
  58. }
  59. if (api_command != url_path_endpoint)
  60. freez(api_command);
  61. short int code = web_client_check_acl_and_bearer(w, api_commands[i].acl);
  62. if(code != HTTP_RESP_OK) {
  63. if(code == HTTP_RESP_FORBIDDEN)
  64. return web_client_permission_denied(w);
  65. if(code == HTTP_RESP_PRECOND_FAIL)
  66. return web_client_bearer_required(w);
  67. buffer_flush(w->response.data);
  68. buffer_sprintf(w->response.data, "Failed with code %d", code);
  69. w->response.code = code;
  70. return code;
  71. }
  72. char *query_string = (char *)buffer_tostring(w->url_query_string_decoded);
  73. if(*query_string == '?')
  74. query_string = &query_string[1];
  75. return api_commands[i].callback(host, w, query_string);
  76. }
  77. }
  78. if (api_command != url_path_endpoint)
  79. freez(api_command);
  80. buffer_flush(w->response.data);
  81. buffer_strcat(w->response.data, "Unsupported API command: ");
  82. buffer_strcat_htmlescape(w->response.data, url_path_endpoint);
  83. return HTTP_RESP_NOT_FOUND;
  84. }
  85. RRDCONTEXT_TO_JSON_OPTIONS rrdcontext_to_json_parse_options(char *o) {
  86. RRDCONTEXT_TO_JSON_OPTIONS options = RRDCONTEXT_OPTION_NONE;
  87. char *tok;
  88. while(o && *o && (tok = strsep_skip_consecutive_separators(&o, ", |"))) {
  89. if(!*tok) continue;
  90. if(!strcmp(tok, "full") || !strcmp(tok, "all"))
  91. options |= RRDCONTEXT_OPTIONS_ALL;
  92. else if(!strcmp(tok, "charts") || !strcmp(tok, "instances"))
  93. options |= RRDCONTEXT_OPTION_SHOW_INSTANCES;
  94. else if(!strcmp(tok, "dimensions") || !strcmp(tok, "metrics"))
  95. options |= RRDCONTEXT_OPTION_SHOW_METRICS;
  96. else if(!strcmp(tok, "queue"))
  97. options |= RRDCONTEXT_OPTION_SHOW_QUEUED;
  98. else if(!strcmp(tok, "flags"))
  99. options |= RRDCONTEXT_OPTION_SHOW_FLAGS;
  100. else if(!strcmp(tok, "uuids"))
  101. options |= RRDCONTEXT_OPTION_SHOW_UUIDS;
  102. else if(!strcmp(tok, "deleted"))
  103. options |= RRDCONTEXT_OPTION_SHOW_DELETED;
  104. else if(!strcmp(tok, "labels"))
  105. options |= RRDCONTEXT_OPTION_SHOW_LABELS;
  106. else if(!strcmp(tok, "deepscan"))
  107. options |= RRDCONTEXT_OPTION_DEEPSCAN;
  108. else if(!strcmp(tok, "hidden"))
  109. options |= RRDCONTEXT_OPTION_SHOW_HIDDEN;
  110. }
  111. return options;
  112. }
  113. int web_client_api_request_weights(RRDHOST *host, struct web_client *w, char *url, WEIGHTS_METHOD method, WEIGHTS_FORMAT format, size_t api_version) {
  114. if (!netdata_ready)
  115. return HTTP_RESP_BACKEND_FETCH_FAILED;
  116. time_t baseline_after = 0, baseline_before = 0, after = 0, before = 0;
  117. size_t points = 0;
  118. RRDR_OPTIONS options = 0;
  119. RRDR_TIME_GROUPING time_group_method = RRDR_GROUPING_AVERAGE;
  120. time_t timeout_ms = 0;
  121. size_t tier = 0;
  122. const char *time_group_options = NULL, *scope_contexts = NULL, *scope_nodes = NULL, *contexts = NULL, *nodes = NULL,
  123. *instances = NULL, *dimensions = NULL, *labels = NULL, *alerts = NULL;
  124. struct group_by_pass group_by = {
  125. .group_by = RRDR_GROUP_BY_NONE,
  126. .group_by_label = NULL,
  127. .aggregation = RRDR_GROUP_BY_FUNCTION_AVERAGE,
  128. };
  129. while (url) {
  130. char *value = strsep_skip_consecutive_separators(&url, "&");
  131. if (!value || !*value)
  132. continue;
  133. char *name = strsep_skip_consecutive_separators(&value, "=");
  134. if (!name || !*name)
  135. continue;
  136. if (!value || !*value)
  137. continue;
  138. if (!strcmp(name, "baseline_after"))
  139. baseline_after = str2l(value);
  140. else if (!strcmp(name, "baseline_before"))
  141. baseline_before = str2l(value);
  142. else if (!strcmp(name, "after") || !strcmp(name, "highlight_after"))
  143. after = str2l(value);
  144. else if (!strcmp(name, "before") || !strcmp(name, "highlight_before"))
  145. before = str2l(value);
  146. else if (!strcmp(name, "points") || !strcmp(name, "max_points"))
  147. points = str2ul(value);
  148. else if (!strcmp(name, "timeout"))
  149. timeout_ms = str2l(value);
  150. else if((api_version == 1 && !strcmp(name, "group")) || (api_version >= 2 && !strcmp(name, "time_group")))
  151. time_group_method = time_grouping_parse(value, RRDR_GROUPING_AVERAGE);
  152. else if((api_version == 1 && !strcmp(name, "group_options")) || (api_version >= 2 && !strcmp(name, "time_group_options")))
  153. time_group_options = value;
  154. else if(!strcmp(name, "options"))
  155. options |= web_client_api_request_v1_data_options(value);
  156. else if(!strcmp(name, "method"))
  157. method = weights_string_to_method(value);
  158. else if(api_version == 1 && (!strcmp(name, "context") || !strcmp(name, "contexts")))
  159. scope_contexts = value;
  160. else if(api_version >= 2 && !strcmp(name, "scope_nodes")) scope_nodes = value;
  161. else if(api_version >= 2 && !strcmp(name, "scope_contexts")) scope_contexts = value;
  162. else if(api_version >= 2 && !strcmp(name, "nodes")) nodes = value;
  163. else if(api_version >= 2 && !strcmp(name, "contexts")) contexts = value;
  164. else if(api_version >= 2 && !strcmp(name, "instances")) instances = value;
  165. else if(api_version >= 2 && !strcmp(name, "dimensions")) dimensions = value;
  166. else if(api_version >= 2 && !strcmp(name, "labels")) labels = value;
  167. else if(api_version >= 2 && !strcmp(name, "alerts")) alerts = value;
  168. else if(api_version >= 2 && (!strcmp(name, "group_by") || !strcmp(name, "group_by[0]"))) {
  169. group_by.group_by = group_by_parse(value);
  170. }
  171. else if(api_version >= 2 && (!strcmp(name, "group_by_label") || !strcmp(name, "group_by_label[0]"))) {
  172. group_by.group_by_label = value;
  173. }
  174. else if(api_version >= 2 && (!strcmp(name, "aggregation") || !strcmp(name, "aggregation[0]"))) {
  175. group_by.aggregation = group_by_aggregate_function_parse(value);
  176. }
  177. else if(!strcmp(name, "tier")) {
  178. tier = str2ul(value);
  179. if(tier < storage_tiers)
  180. options |= RRDR_OPTION_SELECTED_TIER;
  181. else
  182. tier = 0;
  183. }
  184. }
  185. if(options == 0)
  186. // the user did not set any options
  187. options = RRDR_OPTION_NOT_ALIGNED | RRDR_OPTION_NULL2ZERO | RRDR_OPTION_NONZERO;
  188. else
  189. // the user set some options, add also these
  190. options |= RRDR_OPTION_NOT_ALIGNED | RRDR_OPTION_NULL2ZERO;
  191. if(options & RRDR_OPTION_PERCENTAGE)
  192. options |= RRDR_OPTION_ABSOLUTE;
  193. if(options & RRDR_OPTION_DEBUG)
  194. options &= ~RRDR_OPTION_MINIFY;
  195. BUFFER *wb = w->response.data;
  196. buffer_flush(wb);
  197. wb->content_type = CT_APPLICATION_JSON;
  198. QUERY_WEIGHTS_REQUEST qwr = {
  199. .version = api_version,
  200. .host = (api_version == 1) ? NULL : host,
  201. .scope_nodes = scope_nodes,
  202. .scope_contexts = scope_contexts,
  203. .nodes = nodes,
  204. .contexts = contexts,
  205. .instances = instances,
  206. .dimensions = dimensions,
  207. .labels = labels,
  208. .alerts = alerts,
  209. .group_by = {
  210. .group_by = group_by.group_by,
  211. .group_by_label = group_by.group_by_label,
  212. .aggregation = group_by.aggregation,
  213. },
  214. .method = method,
  215. .format = format,
  216. .time_group_method = time_group_method,
  217. .time_group_options = time_group_options,
  218. .baseline_after = baseline_after,
  219. .baseline_before = baseline_before,
  220. .after = after,
  221. .before = before,
  222. .points = points,
  223. .options = options,
  224. .tier = tier,
  225. .timeout_ms = timeout_ms,
  226. .interrupt_callback = web_client_interrupt_callback,
  227. .interrupt_callback_data = w,
  228. };
  229. return web_api_v12_weights(wb, &qwr);
  230. }
  231. bool web_client_interrupt_callback(void *data) {
  232. struct web_client *w = data;
  233. if(w->interrupt.callback)
  234. return w->interrupt.callback(w, w->interrupt.callback_data);
  235. return sock_has_output_error(w->ofd);
  236. }