Allow connecting to arbitrary MQTT WSS broker for devs (#9999)
* adds a simple way to disable of ACLK challenge for devs * remove unnecessary back and forth conversions (int<->str) for ACLK port
Timotej S committed
Oct 6, 2020 at 11:12 UTC
bc1fa29ec33f492cf27f709b24c5fc5bf952881b
5 files changed
+42
-33
aclk/aclk_common.c
+6
-8
@@ -199,7 +199,7 @@ const char *aclk_get_proxy(ACLK_PROXY_TYPE *type)
199
return proxy;
200
}
201
202
-int aclk_decode_base_url(char *url, char **aclk_hostname, char **aclk_port)
202
+int aclk_decode_base_url(char *url, char **aclk_hostname, int *aclk_port)
203
{
204
int pos = 0;
205
if (!strncmp("https://", url, 8)) {
@@ -213,8 +213,8 @@ int aclk_decode_base_url(char *url, char **aclk_hostname, char **aclk_port)
213
host_end++;
214
if (url[host_end] == 0) {
215
*aclk_hostname = strdupz(url + pos);
216
- *aclk_port = strdupz("443");
217
- info("Setting ACLK target host=%s port=%s from %s", *aclk_hostname, *aclk_port, url);
216
+ *aclk_port = 443;
217
+ info("Setting ACLK target host=%s port=%d from %s", *aclk_hostname, *aclk_port, url);
218
return 0;
219
}
220
if (url[host_end] == ':') {
@@ -227,15 +227,13 @@ int aclk_decode_base_url(char *url, char **aclk_hostname, char **aclk_port)
227
error("Port specified in %s is invalid", url);
228
return 0;
229
}
230
- *aclk_port = callocz(port_end - host_end + 1, 1);
231
- for (int i = host_end + 1; i < port_end; i++)
232
- (*aclk_port)[i - host_end - 1] = url[i];
230
+ *aclk_port = atoi(&url[host_end+1]);
231
}
232
if (url[host_end] == '/') {
235
- *aclk_port = strdupz("443");
233
+ *aclk_port = 443;
234
*aclk_hostname = callocz(1, host_end - pos + 1);
235
strncpy(*aclk_hostname, url+pos, host_end - pos);
236
}
239
- info("Setting ACLK target host=%s port=%s from %s", *aclk_hostname, *aclk_port, url);
237
+ info("Setting ACLK target host=%s port=%d from %s", *aclk_hostname, *aclk_port, url);
238
return 0;
239
}
aclk/aclk_common.h
+1
-1
@@ -81,7 +81,7 @@ const char *aclk_proxy_type_to_s(ACLK_PROXY_TYPE *type);
81
ACLK_PROXY_TYPE aclk_verify_proxy(const char *string);
82
const char *aclk_lws_wss_get_proxy_setting(ACLK_PROXY_TYPE *type);
83
void safe_log_proxy_censor(char *proxy);
84
-int aclk_decode_base_url(char *url, char **aclk_hostname, char **aclk_port);
84
+int aclk_decode_base_url(char *url, char **aclk_hostname, int *aclk_port);
85
const char *aclk_get_proxy(ACLK_PROXY_TYPE *type);
86
87
#endif //ACLK_COMMON_H
aclk/aclk_lws_https_client.c
+2
-2
@@ -147,7 +147,7 @@ static void simple_hcc_log_divert(int level, const char *line)
147
error("Libwebsockets: %s", line);
148
}
149
150
-int aclk_send_https_request(char *method, char *host, char *port, char *url, char *b, size_t b_size, char *payload)
150
+int aclk_send_https_request(char *method, char *host, int port, char *url, char *b, size_t b_size, char *payload)
151
{
152
info("%s %s", __func__, method);
153
@@ -198,7 +198,7 @@ int aclk_send_https_request(char *method, char *host, char *port, char *url, cha
198
i.ssl_connection |= LCCSCF_SKIP_SERVER_CERT_HOSTNAME_CHECK;
199
#endif
200
201
- i.port = atoi(port);
201
+ i.port = port;
202
i.address = host;
203
i.path = url;
204
aclk/aclk_lws_https_client.h
+1
-1
@@ -13,6 +13,6 @@
13
#define SEND_HTTPS_REQUEST_TIMEOUT 30
14
#endif
15
16
-int aclk_send_https_request(char *method, char *host, char *port, char *url, char *b, size_t b_size, char *payload);
16
+int aclk_send_https_request(char *method, char *host, int port, char *url, char *b, size_t b_size, char *payload);
17
18
#endif /* NETDATA_LWS_HTTPS_CLIENT_H */
aclk/agent_cloud_link.c
+32
-21
@@ -723,7 +723,7 @@ int private_decrypt(unsigned char * enc_data, int data_len, unsigned char *decry
723
return result;
724
}
725
726
-void aclk_get_challenge(char *aclk_hostname, char *aclk_port)
726
+void aclk_get_challenge(char *aclk_hostname, int port)
727
{
728
char *data_buffer = mallocz(NETDATA_WEB_RESPONSE_INITIAL_SIZE);
729
debug(D_ACLK, "Performing challenge-response sequence");
@@ -742,8 +742,8 @@ void aclk_get_challenge(char *aclk_hostname, char *aclk_port)
742
}
743
char url[1024];
744
sprintf(url, "/api/v1/auth/node/%s/challenge", agent_id);
745
- info("Retrieving challenge from cloud: %s %s %s", aclk_hostname, aclk_port, url);
746
- if(aclk_send_https_request("GET", aclk_hostname, aclk_port, url, data_buffer, NETDATA_WEB_RESPONSE_INITIAL_SIZE, NULL))
745
+ info("Retrieving challenge from cloud: %s %d %s", aclk_hostname, port, url);
746
+ if(aclk_send_https_request("GET", aclk_hostname, port, url, data_buffer, NETDATA_WEB_RESPONSE_INITIAL_SIZE, NULL))
747
{
748
error("Challenge failed: %s", data_buffer);
749
goto CLEANUP;
@@ -780,7 +780,7 @@ void aclk_get_challenge(char *aclk_hostname, char *aclk_port)
780
debug(D_ACLK, "Password phase: %s",response_json);
781
// TODO - host
782
sprintf(url, "/api/v1/auth/node/%s/password", agent_id);
783
- if(aclk_send_https_request("POST", aclk_hostname, aclk_port, url, data_buffer, NETDATA_WEB_RESPONSE_INITIAL_SIZE, response_json))
783
+ if(aclk_send_https_request("POST", aclk_hostname, port, url, data_buffer, NETDATA_WEB_RESPONSE_INITIAL_SIZE, response_json))
784
{
785
error("Challenge-response failed: %s", data_buffer);
786
goto CLEANUP;
@@ -819,24 +819,42 @@ CLEANUP:
819
#pragma endregion
820
#endif
821
822
-static void aclk_try_to_connect(char *hostname, char *port, int port_num)
822
+static void aclk_try_to_connect(char *hostname, int port)
823
{
824
+ int rc;
825
+
826
+// this is usefull for developers working on ACLK
827
+// allows connecting agent to any MQTT broker
828
+// for debugging, development and testing purposes
829
+#ifndef ACLK_DISABLE_CHALLENGE
830
if (!aclk_private_key) {
825
- error("Cannot try to establish the agent cloud link - no private key available!");
826
- return;
831
+ error("Cannot try to establish the agent cloud link - no private key available!");
832
+ return;
833
}
834
+#endif
835
+
836
info("Attempting to establish the agent cloud link");
837
+#ifdef ACLK_DISABLE_CHALLENGE
838
+ error("Agent built with ACLK_DISABLE_CHALLENGE. This is for testing "
839
+ "and development purposes only. Warranty void. Won't be able "
840
+ "to connect to Netdata Cloud.");
841
+ if (aclk_password == NULL)
842
+ aclk_password = strdupz("anon");
843
+#else
844
aclk_get_challenge(hostname, port);
845
if (aclk_password == NULL)
846
return;
832
- int rc;
847
+#endif
848
+
849
aclk_connecting = 1;
850
create_publish_base_topic();
851
+
852
ACLK_SHARED_STATE_LOCK;
853
aclk_shared_state.version_neg = 0;
854
aclk_shared_state.version_neg_wait_till = 0;
855
ACLK_SHARED_STATE_UNLOCK;
839
- rc = mqtt_attempt_connection(hostname, port_num, aclk_username, aclk_password);
856
+
857
+ rc = mqtt_attempt_connection(hostname, port, aclk_username, aclk_password);
858
if (unlikely(rc)) {
859
error("Failed to initialize the agent cloud link library");
860
}
@@ -936,8 +954,7 @@ void *aclk_main(void *ptr)
954
}
955
956
char *aclk_hostname = NULL; // Initializers are over-written but prevent gcc complaining about clobbering.
939
- char *aclk_port = NULL;
940
- uint32_t port_num = 0;
957
+ int port_num = 0;
958
info("Waiting for netdata to be claimed");
959
while(1) {
960
char *agent_id = is_agent_claimed();
@@ -955,15 +972,10 @@ void *aclk_main(void *ptr)
972
error("Do not move the cloud base url out of post_conf_load!!");
973
goto exited;
974
}
958
- if (aclk_decode_base_url(cloud_base_url, &aclk_hostname, &aclk_port)) {
975
+ if (aclk_decode_base_url(cloud_base_url, &aclk_hostname, &port_num))
976
error("Agent is claimed but the configuration is invalid, please fix");
960
- }
961
- else
962
- {
963
- port_num = atoi(aclk_port); // SSL library uses the string, MQTT uses the numeric value
964
- if (!create_private_key() && !_mqtt_lib_init())
977
+ else if (!create_private_key() && !_mqtt_lib_init())
978
break;
966
- }
979
980
for (int i=0; i<60; i++) {
981
if (netdata_exit)
@@ -998,7 +1010,7 @@ void *aclk_main(void *ptr)
1010
}
1011
if (unlikely(!netdata_exit && !aclk_connected && !aclk_force_reconnect)) {
1012
if (unlikely(!first_init)) {
1001
- aclk_try_to_connect(aclk_hostname, aclk_port, port_num);
1013
+ aclk_try_to_connect(aclk_hostname, port_num);
1014
first_init = 1;
1015
} else {
1016
if (aclk_connecting == 0) {
@@ -1009,7 +1021,7 @@ void *aclk_main(void *ptr)
1021
}
1022
if (now_realtime_usec() >= reconnect_expiry) {
1023
reconnect_expiry = 0;
1012
- aclk_try_to_connect(aclk_hostname, aclk_port, port_num);
1024
+ aclk_try_to_connect(aclk_hostname, port_num);
1025
}
1026
sleep_usec(USEC_PER_MS * 100);
1027
}
@@ -1047,7 +1059,6 @@ exited:
1059
freez(aclk_username);
1060
freez(aclk_password);
1061
freez(aclk_hostname);
1050
- freez(aclk_port);
1062
if (aclk_private_key != NULL)
1063
RSA_free(aclk_private_key);
1064