master
c 137 lines 5.42 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "claim.h"
4
5 struct config cloud_config = APPCONFIG_INITIALIZER;
6
7 const char *cloud_config_url_get(void) {
8 return inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "url", DEFAULT_CLOUD_BASE_URL);
9 }
10
11 void cloud_config_url_set(const char *url)
12 {
13 if (!url || !*url) return;
14
15 const char *existing = cloud_config_url_get();
16 if (strcmp(existing, url) != 0)
17 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "url", url);
18 }
19
20 static const char *cloud_config_proxy_source = NULL;
21 static bool cloud_config_proxy_explicitly_set = false;
22
23 const char *cloud_config_proxy_get(void) {
24 // load cloud.conf or internal default
25 const char *proxy = inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy", "env");
26
27 // backwards compatibility, from when proxy was in netdata.conf
28 // netdata.conf has bigger priority
29 if (inicfg_exists(&netdata_config, CONFIG_SECTION_CLOUD, "proxy")) {
30 // get it from netdata.conf
31 proxy = inicfg_get(&netdata_config, CONFIG_SECTION_CLOUD, "proxy", proxy);
32
33 // update cloud.conf
34 proxy = inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy", proxy);
35 cloud_config_proxy_source = "netdata.conf [cloud]";
36 cloud_config_proxy_explicitly_set = true;
37 }
38 else {
39 // set in netdata.conf the proxy of cloud.conf
40 inicfg_set(&netdata_config, CONFIG_SECTION_CLOUD, "proxy", proxy);
41 cloud_config_proxy_source = "cloud.conf";
42 }
43
44 return proxy;
45 }
46
47 const char *cloud_config_proxy_source_get(void) {
48 return cloud_config_proxy_source;
49 }
50
51 bool cloud_config_proxy_is_explicitly_set(void) {
52 return cloud_config_proxy_explicitly_set;
53 }
54
55 bool cloud_config_insecure_get(void) {
56 // load it from cloud.conf or use internal default
57 return inicfg_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "insecure", CONFIG_BOOLEAN_NO);
58 }
59
60 static void cloud_conf_load_defaults(void) {
61 inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "url", DEFAULT_CLOUD_BASE_URL);
62 inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy", "env");
63 inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "token", "");
64 inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "rooms", "");
65 inicfg_get_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "insecure", CONFIG_BOOLEAN_NO);
66 inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "machine_guid", "");
67 inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "claimed_id", "");
68 inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "hostname", "");
69 }
70
71 void cloud_conf_load(int silent) {
72 netdata_conf_section_directories();
73
74 errno_clear();
75 char *filename = filename_from_path_entry_strdupz(netdata_configured_cloud_dir, "cloud.conf");
76 int ret = inicfg_load(&cloud_config, filename, 1, NULL);
77
78 if(!ret && !silent)
79 nd_log(NDLS_DAEMON, NDLP_ERR,
80 "CLAIM: cannot load cloud config '%s'. Running with internal defaults.", filename);
81
82 freez(filename);
83
84 inicfg_move(&cloud_config,
85 CONFIG_SECTION_GLOBAL, "cloud base url",
86 CONFIG_SECTION_GLOBAL, "url");
87
88 // check if proxy was explicitly set in cloud.conf before defaults overwrite it
89 cloud_config_proxy_explicitly_set = inicfg_exists(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy");
90
91 cloud_conf_load_defaults();
92 }
93
94 void cloud_conf_init_after_registry(void) {
95 const char *machine_guid = inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "machine_guid", "");
96 const char *hostname = inicfg_get(&cloud_config, CONFIG_SECTION_GLOBAL, "hostname", "");
97
98 // for machine guid and hostname we have to use inicfg_set() for that they will be saved uncommented
99 if(!machine_guid || !*machine_guid)
100 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "machine_guid", machine_guid_get_txt());
101
102 if(!hostname || !*hostname)
103 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "hostname", registry_get_this_machine_hostname());
104 }
105
106 bool cloud_conf_save(void) {
107 char filename[FILENAME_MAX + 1];
108
109 CLEAN_BUFFER *wb = buffer_create(0, NULL);
110 inicfg_generate(&cloud_config, wb, false, false);
111 snprintfz(filename, sizeof(filename), "%s/cloud.conf", netdata_configured_cloud_dir);
112 FILE *fp = fopen(filename, "w");
113 if(!fp) {
114 nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot open file '%s' for writing.", filename);
115 return false;
116 }
117
118 fprintf(fp, "%s", buffer_tostring(wb));
119 fclose(fp);
120 return true;
121 }
122
123 bool cloud_conf_regenerate(const char *claimed_id_str, const char *machine_guid, const char *hostname, const char *token, const char *rooms, const char *url, const char *proxy, bool insecure) {
124 // for backwards compatibility (older agents), save the claimed_id to its file
125 claimed_id_save_to_file(claimed_id_str);
126
127 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "url", url);
128 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "proxy", proxy ? proxy : "");
129 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "token", token ? token : "");
130 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "rooms", rooms ? rooms : "");
131 inicfg_set_boolean(&cloud_config, CONFIG_SECTION_GLOBAL, "insecure", insecure);
132 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "machine_guid", machine_guid ? machine_guid : "");
133 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "claimed_id", claimed_id_str ? claimed_id_str : "");
134 inicfg_set(&cloud_config, CONFIG_SECTION_GLOBAL, "hostname", hostname ? hostname : "");
135
136 return cloud_conf_save();
137 }