claim.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "claim.h"
  3. #include "registry/registry_internals.h"
  4. #include "aclk/aclk_api.h"
  5. char *claiming_pending_arguments = NULL;
  6. static char *claiming_errors[] = {
  7. "Agent claimed successfully", // 0
  8. "Unknown argument", // 1
  9. "Problems with claiming working directory", // 2
  10. "Missing dependencies", // 3
  11. "Failure to connect to endpoint", // 4
  12. "The CLI didn't work", // 5
  13. "Wrong user", // 6
  14. "Unknown HTTP error message", // 7
  15. "invalid node id", // 8
  16. "invalid node name", // 9
  17. "invalid room id", // 10
  18. "invalid public key", // 11
  19. "token expired/token not found/invalid token", // 12
  20. "already claimed", // 13
  21. "processing claiming", // 14
  22. "Internal Server Error", // 15
  23. "Gateway Timeout", // 16
  24. "Service Unavailable", // 17
  25. "Agent Unique Id Not Readable" // 18
  26. };
  27. /* Retrieve the claim id for the agent.
  28. * Caller owns the string.
  29. */
  30. char *get_agent_claimid()
  31. {
  32. char *result;
  33. rrdhost_aclk_state_lock(localhost);
  34. result = (localhost->aclk_state.claimed_id == NULL) ? NULL : strdupz(localhost->aclk_state.claimed_id);
  35. rrdhost_aclk_state_unlock(localhost);
  36. return result;
  37. }
  38. #define CLAIMING_COMMAND_LENGTH 16384
  39. #define CLAIMING_PROXY_LENGTH CLAIMING_COMMAND_LENGTH/4
  40. extern struct registry registry;
  41. /* rrd_init() and post_conf_load() must have been called before this function */
  42. void claim_agent(char *claiming_arguments)
  43. {
  44. if (!netdata_cloud_setting) {
  45. error("Refusing to claim agent -> cloud functionality has been disabled");
  46. return;
  47. }
  48. #ifndef DISABLE_CLOUD
  49. int exit_code;
  50. pid_t command_pid;
  51. char command_buffer[CLAIMING_COMMAND_LENGTH + 1];
  52. FILE *fp;
  53. // This is guaranteed to be set early in main via post_conf_load()
  54. char *cloud_base_url = appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "cloud base url", NULL);
  55. if (cloud_base_url == NULL)
  56. fatal("Do not move the cloud base url out of post_conf_load!!");
  57. const char *proxy_str;
  58. ACLK_PROXY_TYPE proxy_type;
  59. char proxy_flag[CLAIMING_PROXY_LENGTH] = "-noproxy";
  60. proxy_str = aclk_get_proxy(&proxy_type);
  61. if (proxy_type == PROXY_TYPE_SOCKS5 || proxy_type == PROXY_TYPE_HTTP)
  62. snprintf(proxy_flag, CLAIMING_PROXY_LENGTH, "-proxy=\"%s\"", proxy_str);
  63. snprintfz(command_buffer,
  64. CLAIMING_COMMAND_LENGTH,
  65. "exec netdata-claim.sh %s -hostname=%s -id=%s -url=%s -noreload %s",
  66. proxy_flag,
  67. netdata_configured_hostname,
  68. localhost->machine_guid,
  69. cloud_base_url,
  70. claiming_arguments);
  71. info("Executing agent claiming command 'netdata-claim.sh'");
  72. fp = mypopen(command_buffer, &command_pid);
  73. if(!fp) {
  74. error("Cannot popen(\"%s\").", command_buffer);
  75. return;
  76. }
  77. info("Waiting for claiming command to finish.");
  78. while (fgets(command_buffer, CLAIMING_COMMAND_LENGTH, fp) != NULL) {;}
  79. exit_code = mypclose(fp, command_pid);
  80. info("Agent claiming command returned with code %d", exit_code);
  81. if (0 == exit_code) {
  82. load_claiming_state();
  83. return;
  84. }
  85. if (exit_code < 0) {
  86. error("Agent claiming command failed to complete its run.");
  87. return;
  88. }
  89. errno = 0;
  90. unsigned maximum_known_exit_code = sizeof(claiming_errors) / sizeof(claiming_errors[0]) - 1;
  91. if ((unsigned)exit_code > maximum_known_exit_code) {
  92. error("Agent failed to be claimed with an unknown error.");
  93. return;
  94. }
  95. error("Agent failed to be claimed with the following error message:");
  96. error("\"%s\"", claiming_errors[exit_code]);
  97. #else
  98. UNUSED(claiming_arguments);
  99. UNUSED(claiming_errors);
  100. #endif
  101. }
  102. #ifdef ENABLE_ACLK
  103. extern int aclk_connected, aclk_kill_link, aclk_disable_runtime;
  104. #endif
  105. /* Change the claimed state of the agent.
  106. *
  107. * This only happens when the user has explicitly requested it:
  108. * - via the cli tool by reloading the claiming state
  109. * - after spawning the claim because of a command-line argument
  110. * If this happens with the ACLK active under an old claim then we MUST KILL THE LINK
  111. */
  112. void load_claiming_state(void)
  113. {
  114. // --------------------------------------------------------------------
  115. // Check if the cloud is enabled
  116. #if defined( DISABLE_CLOUD ) || !defined( ENABLE_ACLK )
  117. netdata_cloud_setting = 0;
  118. #else
  119. uuid_t uuid;
  120. // Propagate into aclk and registry. Be kind of atomic...
  121. appconfig_get(&cloud_config, CONFIG_SECTION_GLOBAL, "cloud base url", DEFAULT_CLOUD_BASE_URL);
  122. rrdhost_aclk_state_lock(localhost);
  123. if (localhost->aclk_state.claimed_id) {
  124. if (aclk_connected)
  125. localhost->aclk_state.prev_claimed_id = strdupz(localhost->aclk_state.claimed_id);
  126. freez(localhost->aclk_state.claimed_id);
  127. localhost->aclk_state.claimed_id = NULL;
  128. }
  129. if (aclk_connected)
  130. {
  131. info("Agent was already connected to Cloud - forcing reconnection under new credentials");
  132. aclk_kill_link = 1;
  133. }
  134. aclk_disable_runtime = 0;
  135. char filename[FILENAME_MAX + 1];
  136. snprintfz(filename, FILENAME_MAX, "%s/cloud.d/claimed_id", netdata_configured_varlib_dir);
  137. long bytes_read;
  138. char *claimed_id = read_by_filename(filename, &bytes_read);
  139. if(claimed_id && uuid_parse(claimed_id, uuid)) {
  140. error("claimed_id \"%s\" doesn't look like valid UUID", claimed_id);
  141. freez(claimed_id);
  142. claimed_id = NULL;
  143. }
  144. if(claimed_id) {
  145. localhost->aclk_state.claimed_id = mallocz(UUID_STR_LEN);
  146. uuid_unparse_lower(uuid, localhost->aclk_state.claimed_id);
  147. }
  148. invalidate_node_instances(&localhost->host_uuid, claimed_id ? &uuid : NULL);
  149. store_claim_id(&localhost->host_uuid, claimed_id ? &uuid : NULL);
  150. rrdhost_aclk_state_unlock(localhost);
  151. if (!claimed_id) {
  152. info("Unable to load '%s', setting state to AGENT_UNCLAIMED", filename);
  153. return;
  154. }
  155. freez(claimed_id);
  156. info("File '%s' was found. Setting state to AGENT_CLAIMED.", filename);
  157. netdata_cloud_setting = appconfig_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "enabled", 1);
  158. #endif
  159. }
  160. struct config cloud_config = { .first_section = NULL,
  161. .last_section = NULL,
  162. .mutex = NETDATA_MUTEX_INITIALIZER,
  163. .index = { .avl_tree = { .root = NULL, .compar = appconfig_section_compare },
  164. .rwlock = AVL_LOCK_INITIALIZER } };
  165. void load_cloud_conf(int silent)
  166. {
  167. char *filename;
  168. errno = 0;
  169. int ret = 0;
  170. filename = strdupz_path_subpath(netdata_configured_varlib_dir, "cloud.d/cloud.conf");
  171. ret = appconfig_load(&cloud_config, filename, 1, NULL);
  172. if(!ret && !silent) {
  173. info("CONFIG: cannot load cloud config '%s'. Running with internal defaults.", filename);
  174. }
  175. freez(filename);
  176. }