4
#include "aclk_util.h"
5
#include "aclk.h"
6
7
-static int aclk_https_request(https_req_t *request, https_req_response_t *response, bool *fallback_ipv4) {
8
- int rc;
7
+static https_client_resp_t aclk_https_request(https_req_t *request, https_req_response_t *response, bool *fallback_ipv4) {
8
+ https_client_resp_t rc;
9
// wrapper for ACLK only which loads ACLK specific proxy settings
10
// then only calls https_request
11
struct mqtt_wss_proxy proxy_conf = { .host = NULL, .port = 0, .username = NULL, .password = NULL, .type = MQTT_WSS_DIRECT };
33
34
#define PARSE_ENV_JSON_CHK_TYPE(it, type, name) \
35
if (json_object_get_type(json_object_iter_peek_value(it)) != type) { \
36
- netdata_log_error("value of key \"%s\" should be %s", name, #type); \
36
+ netdata_log_error("ACLK: value of key \"%s\" should be %s", name, #type); \
37
goto exit; \
38
}
39
50
51
json = json_tokener_parse(json_str);
52
if (!json) {
53
- netdata_log_error("JSON-C failed to parse the payload of http response of /env endpoint");
53
+ netdata_log_error("ACLK: JSON-C failed to parse the payload of http response of /env endpoint");
54
return 1;
55
}
56
83
PARSE_ENV_JSON_CHK_TYPE(&it, json_type_array, JSON_KEY_TOPICS)
84
85
if (aclk_generate_topic_cache(json_object_iter_peek_value(&it))) {
86
- netdata_log_error("Failed to generate topic cache!");
86
+ netdata_log_error("ACLK: Failed to generate topic cache!");
87
goto exit;
88
}
89
json_object_iter_next(&it);
90
continue;
91
}
92
- netdata_log_error("Unknown key \"%s\" in passwd response payload. Ignoring", json_object_iter_peek_name(&it));
92
+ netdata_log_error("ACLK: Unknown key \"%s\" in passwd response payload. Ignoring", json_object_iter_peek_name(&it));
93
json_object_iter_next(&it);
94
}
95
96
if (!auth->client_id) {
97
- netdata_log_error(JSON_KEY_CLIENTID " is compulsory key in /password response");
97
+ netdata_log_error("ACLK: " JSON_KEY_CLIENTID " is compulsory key in /password response");
98
goto exit;
99
}
100
if (!auth->passwd) {
101
- netdata_log_error(JSON_KEY_PASS " is compulsory in /password response");
101
+ netdata_log_error("ACLK: " JSON_KEY_PASS " is compulsory in /password response");
102
goto exit;
103
}
104
if (!auth->username) {
105
- netdata_log_error(JSON_KEY_USER " is compulsory in /password response");
105
+ netdata_log_error("ACLK: " JSON_KEY_USER " is compulsory in /password response");
106
goto exit;
107
}
108
121
static const char *get_json_str_by_path(json_object *json, const char *path) {
122
json_object *ptr;
123
if (json_pointer_get(json, path, &ptr)) {
124
- netdata_log_error("Missing compulsory key \"%s\" in error response", path);
124
+ netdata_log_error("ACLK: Missing compulsory key \"%s\" in error response", path);
125
return NULL;
126
}
127
if (json_object_get_type(ptr) != json_type_string) {
128
- netdata_log_error("Value of Key \"%s\" in error response should be string", path);
128
+ netdata_log_error("ACLK: Value of Key \"%s\" in error response should be string", path);
129
return NULL;
130
}
131
return json_object_get_string(ptr);
142
143
json = json_tokener_parse(json_str);
144
if (!json) {
145
- netdata_log_error("JSON-C failed to parse the payload of http response of /env endpoint");
145
+ netdata_log_error("ACLK: JSON-C failed to parse the payload of http response of /env endpoint");
146
return 1;
147
}
148
158
// optional field
159
if (!json_pointer_get(json, "/" JSON_KEY_ERTRY, &ptr)) {
160
if (json_object_get_type(ptr) != json_type_boolean) {
161
- netdata_log_error("Error response Key " "/" JSON_KEY_ERTRY " should be of boolean type");
161
+ netdata_log_error("ACLK: Error response Key " "/" JSON_KEY_ERTRY " should be of boolean type");
162
goto exit;
163
}
164
block_retry = json_object_get_boolean(ptr);
167
// optional field
168
if (!json_pointer_get(json, "/" JSON_KEY_EDELAY, &ptr)) {
169
if (json_object_get_type(ptr) != json_type_int) {
170
- netdata_log_error("Error response Key " "/" JSON_KEY_EDELAY " should be of integer type");
170
+ netdata_log_error("ACLK: Error response Key " "/" JSON_KEY_EDELAY " should be of integer type");
171
goto exit;
172
}
173
backoff = json_object_get_int(ptr);
179
if (backoff > 0)
180
aclk_block_until = now_monotonic_sec() + backoff;
181
182
- netdata_log_error("Cloud returned EC=\"%s\", Msg-Key:\"%s\", Msg:\"%s\", BlockRetry:%s, Backoff:%ds (-1 unset by cloud)", ec, ek, emsg, block_retry > 0 ? "true" : "false", backoff);
182
+ netdata_log_error("ACLK: Cloud returned EC=\"%s\", Msg-Key:\"%s\", Msg:\"%s\", BlockRetry:%s, Backoff:%ds (-1 unset by cloud)", ec, ek, emsg, block_retry > 0 ? "true" : "false", backoff);
183
rc = 0;
184
exit:
185
json_object_put(json);
200
201
json = json_tokener_parse(json_str);
202
if (!json) {
203
- netdata_log_error("JSON-C failed to parse the payload of http response of /env endpoint");
203
+ netdata_log_error("ACLK: JSON-C failed to parse the payload of http response of /env endpoint");
204
return 1;
205
}
206
231
}
232
if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_EDELAY)) {
233
if (json_object_get_type(json_object_iter_peek_value(&it)) != json_type_int) {
234
- netdata_log_error("value of key " JSON_KEY_EDELAY " should be integer");
234
+ netdata_log_error("ACLK: value of key " JSON_KEY_EDELAY " should be integer");
235
goto exit;
236
}
237
241
}
242
if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_ERTRY)) {
243
if (json_object_get_type(json_object_iter_peek_value(&it)) != json_type_boolean) {
244
- netdata_log_error("value of key " JSON_KEY_ERTRY " should be integer");
244
+ netdata_log_error("ACLK: value of key " JSON_KEY_ERTRY " should be integer");
245
goto exit;
246
}
247
249
json_object_iter_next(&it);
250
continue;
251
}
252
- netdata_log_error("Unknown key \"%s\" in error response payload. Ignoring", json_object_iter_peek_name(&it));
252
+ netdata_log_error("ACLK: Unknown key \"%s\" in error response payload. Ignoring", json_object_iter_peek_name(&it));
253
json_object_iter_next(&it);
254
}
255
259
if (backoff > 0)
260
aclk_block_until = now_monotonic_sec() + backoff;
261
262
- netdata_log_error("Cloud returned EC=\"%s\", Msg-Key:\"%s\", Msg:\"%s\", BlockRetry:%s, Backoff:%ds (-1 unset by cloud)", ec, ek, emsg, block_retry > 0 ? "true" : "false", backoff);
262
+ netdata_log_error("ACLK: Cloud returned EC=\"%s\", Msg-Key:\"%s\", Msg:\"%s\", BlockRetry:%s, Backoff:%ds (-1 unset by cloud)", ec, ek, emsg, block_retry > 0 ? "true" : "false", backoff);
263
rc = 0;
264
exit:
265
json_object_put(json);
271
#define CHALLENGE_LEN_BASE64 344
272
273
#define OTP_URL_PREFIX "/api/v1/auth/node/"
274
-int aclk_get_otp_challenge(url_t *target, const char *agent_id, unsigned char **challenge, int *challenge_bytes, bool *fallback_ipv4)
274
+static https_client_resp_t aclk_get_otp_challenge(url_t *target, const char *agent_id, unsigned char **challenge, int *challenge_bytes, bool *fallback_ipv4)
275
{
276
- int rc = 1;
276
+ https_client_resp_t rc;
277
https_req_t req = HTTPS_REQ_T_INITIALIZER;
278
https_req_response_t resp = HTTPS_REQ_RESPONSE_T_INITIALIZER;
279
284
buffer_sprintf(url, "%s/node/%s/challenge", target->path, agent_id);
285
req.url = (char *)buffer_tostring(url);
286
287
- if (aclk_https_request(&req, &resp, fallback_ipv4)) {
288
- netdata_log_error("ACLK_OTP Challenge failed");
287
+ rc = aclk_https_request(&req, &resp, fallback_ipv4);
288
+ if (rc != HTTPS_CLIENT_RESP_OK) {
289
+ netdata_log_error("ACLK: OTP Challenge failed");
290
buffer_free(url);
290
- return 1;
291
+ return rc;
292
}
293
if (resp.http_code != 200) {
293
- netdata_log_error("ACLK_OTP Challenge HTTP code not 200 OK (got %d)", resp.http_code);
294
+ rc = HTTPS_CLIENT_RESP_OTP_CHALLENGE_NOT_200;
295
+ netdata_log_error("ACLK: OTP Challenge HTTP code not 200 OK (got %d)", resp.http_code);
296
buffer_free(url);
297
if (resp.payload_size)
298
aclk_parse_otp_error(resp.payload);
300
}
301
buffer_free(url);
302
301
- netdata_log_info("ACLK_OTP Got Challenge from Cloud");
302
-
303
json_object *json = json_tokener_parse(resp.payload);
304
if (!json) {
305
- netdata_log_error("Couldn't parse HTTP GET challenge payload");
305
+ rc = HTTPS_CLIENT_RESP_OTP_CHALLENGE_INVALID;
306
+ netdata_log_error("ACLK: couldn't parse HTTP GET challenge payload");
307
goto cleanup_resp;
308
}
309
json_object *challenge_json;
310
if (!json_object_object_get_ex(json, "challenge", &challenge_json)) {
310
- netdata_log_error("No key named \"challenge\" in the returned JSON");
311
+ rc = HTTPS_CLIENT_RESP_OTP_CHALLENGE_INVALID;
312
+ netdata_log_error("ACLK: No key named \"challenge\" in the returned JSON");
313
goto cleanup_json;
314
}
315
if (!json_object_is_type(challenge_json, json_type_string)) {
314
- netdata_log_error("\"challenge\" is not a string JSON type");
316
+ rc = HTTPS_CLIENT_RESP_OTP_CHALLENGE_INVALID;
317
+ netdata_log_error("ACLK: \"challenge\" is not a string JSON type");
318
goto cleanup_json;
319
}
320
const char *challenge_base64;
321
if (!((challenge_base64 = json_object_get_string(challenge_json)))) {
319
- netdata_log_error("Failed to extract challenge from JSON object");
322
+ rc = HTTPS_CLIENT_RESP_OTP_CHALLENGE_INVALID;
323
+ netdata_log_error("ACLK: Failed to extract challenge from JSON object");
324
goto cleanup_json;
325
}
326
if (strlen(challenge_base64) != CHALLENGE_LEN_BASE64) {
323
- netdata_log_error("Received Challenge has unexpected length of %zu (expected %d)", strlen(challenge_base64), CHALLENGE_LEN_BASE64);
327
+ rc = HTTPS_CLIENT_RESP_OTP_CHALLENGE_INVALID;
328
+ netdata_log_error("ACLK: Received Challenge has unexpected length of %zu (expected %d)", strlen(challenge_base64), CHALLENGE_LEN_BASE64);
329
goto cleanup_json;
330
}
331
333
*challenge_bytes = netdata_base64_decode(*challenge, (const unsigned char *) challenge_base64, CHALLENGE_LEN_BASE64);
334
335
if (*challenge_bytes != CHALLENGE_LEN) {
331
- netdata_log_error("Unexpected challenge length of %d instead of %d", *challenge_bytes, CHALLENGE_LEN);
336
+ rc = HTTPS_CLIENT_RESP_OTP_CHALLENGE_INVALID;
337
+ netdata_log_error("ACLK: Unexpected challenge length of %d instead of %d", *challenge_bytes, CHALLENGE_LEN);
338
freez(*challenge);
339
*challenge = NULL;
340
goto cleanup_json;
341
}
336
- rc = 0;
342
+
343
+ rc = HTTPS_CLIENT_RESP_OK;
344
345
cleanup_json:
346
json_object_put(json);
349
return rc;
350
}
351
345
-int aclk_send_otp_response(const char *agent_id, const unsigned char *response, int response_bytes, url_t *target, struct auth_data *mqtt_auth, bool *fallback_ipv4)
352
+static https_client_resp_t aclk_send_otp_response(const char *agent_id, const unsigned char *response, int response_bytes, url_t *target, struct auth_data *mqtt_auth, bool *fallback_ipv4)
353
{
347
- int rc = 1;
354
+ https_client_resp_t rc;
355
https_req_t req = HTTPS_REQ_T_INITIALIZER;
356
https_req_response_t resp = HTTPS_REQ_RESPONSE_T_INITIALIZER;
357
374
req.payload = (char *)buffer_tostring(resp_json);
375
req.payload_size = strlen(req.payload);
376
370
- if (aclk_https_request(&req, &resp, fallback_ipv4)) {
371
- netdata_log_error("ACLK_OTP Password error trying to post result to password");
377
+ rc = aclk_https_request(&req, &resp, fallback_ipv4);
378
+ if (rc != HTTPS_CLIENT_RESP_OK) {
379
+ netdata_log_error("ACLK: OTP Password error trying to post result to password");
380
goto cleanup_buffers;
381
}
382
if (resp.http_code != 201) {
375
- netdata_log_error("ACLK_OTP Password HTTP code not 201 Created (got %d)", resp.http_code);
383
+ rc = HTTPS_CLIENT_RESP_OTP_PASSWORD_NOT_201;
384
+ netdata_log_error("ACLK: OTP Password HTTP code not 201 Created (got %d)", resp.http_code);
385
if (resp.payload_size)
386
aclk_parse_otp_error(resp.payload);
387
goto cleanup_response;
388
}
389
if (resp.payload_size == 0 || resp.payload == NULL) {
381
- netdata_log_error("ACLK_OTP Password response payload is empty despite returning 201 Created!");
390
+ rc = HTTPS_CLIENT_RESP_OTP_PASSWORD_EMPTY;
391
+ netdata_log_error("ACLK: OTP Password response payload is empty despite returning 201 Created!");
392
goto cleanup_response;
393
}
384
- netdata_log_info("ACLK_OTP Got Password from Cloud");
394
395
if (parse_passwd_response(resp.payload, mqtt_auth)){
387
- netdata_log_error("Error parsing response of password endpoint");
396
+ rc = HTTPS_CLIENT_RESP_OTP_PASSWORD_NOT_JSON;
397
+ netdata_log_error("ACLK: Error parsing response of password endpoint");
398
goto cleanup_response;
399
}
400
391
- rc = 0;
401
+ rc = HTTPS_CLIENT_RESP_OK;
402
403
cleanup_response:
404
https_req_response_free(&resp);
447
{
448
char err[512];
449
ERR_error_string_n(ERR_get_error(), err, sizeof(err));
440
- netdata_log_error("Decryption of the challenge failed: %s", err);
450
+ netdata_log_error("ACLK: Decryption of the challenge failed: %s", err);
451
}
452
return result;
453
}
454
455
#if OPENSSL_VERSION_NUMBER >= OPENSSL_VERSION_300
446
-int aclk_get_mqtt_otp(EVP_PKEY *p_key, char **mqtt_id, char **mqtt_usr, char **mqtt_pass, url_t *target, bool *fallback_ipv4)
456
+https_client_resp_t aclk_get_mqtt_otp(EVP_PKEY *p_key, char **mqtt_id, char **mqtt_usr, char **mqtt_pass, url_t *target, bool *fallback_ipv4)
457
#else
448
-int aclk_get_mqtt_otp(RSA *p_key, char **mqtt_id, char **mqtt_usr, char **mqtt_pass, url_t *target, bool *fallback_ipv4)
458
+https_client_resp_t aclk_get_mqtt_otp(RSA *p_key, char **mqtt_id, char **mqtt_usr, char **mqtt_pass, url_t *target, bool *fallback_ipv4)
459
#endif
460
{
461
unsigned char *challenge = NULL;
462
int challenge_bytes;
463
+ https_client_resp_t rc;
464
465
CLAIM_ID claim_id = claim_id_get();
466
if (!claim_id_is_set(claim_id)) {
456
- netdata_log_error("Agent was not claimed - cannot perform challenge/response");
457
- return 1;
467
+ netdata_log_error("ACLK: Agent was not claimed - cannot perform challenge/response");
468
+ return HTTPS_CLIENT_RESP_OTP_AGENT_NOT_CLAIMED;
469
}
470
471
// Get Challenge
461
- if (aclk_get_otp_challenge(target, claim_id.str, &challenge, &challenge_bytes, fallback_ipv4)) {
462
- netdata_log_error("Error getting challenge");
463
- return 1;
472
+ rc = aclk_get_otp_challenge(target, claim_id.str, &challenge, &challenge_bytes, fallback_ipv4);
473
+ if (rc != HTTPS_CLIENT_RESP_OK) {
474
+ netdata_log_error("ACLK: error getting challenge");
475
+ return rc;
476
}
477
478
// Decrypt Challenge / Get response
479
unsigned char *response_plaintext = NULL;
480
int response_plaintext_bytes = private_decrypt(p_key, challenge, challenge_bytes, &response_plaintext);
481
if (response_plaintext_bytes < 0) {
470
- netdata_log_error("Couldn't decrypt the challenge received");
482
+ netdata_log_error("ACLK: Couldn't decrypt the challenge received");
483
freez(response_plaintext);
484
freez(challenge);
473
- return 1;
485
+ return HTTPS_CLIENT_RESP_OTP_CHALLENGE_DECRYPTION_FAILED;
486
}
487
freez(challenge);
488
489
// Encode and Send Challenge
490
struct auth_data data = { .client_id = NULL, .passwd = NULL, .username = NULL };
479
- if (aclk_send_otp_response(claim_id.str, response_plaintext, response_plaintext_bytes, target, &data, fallback_ipv4)) {
480
- netdata_log_error("Error getting response");
491
+ rc = aclk_send_otp_response(claim_id.str, response_plaintext, response_plaintext_bytes, target, &data, fallback_ipv4);
492
+ if (rc != HTTPS_CLIENT_RESP_OK) {
493
+ netdata_log_error("ACLK: Error getting response");
494
freez(response_plaintext);
482
- return 1;
495
+ return rc;
496
}
497
498
*mqtt_pass = data.passwd;
500
*mqtt_id = data.client_id;
501
502
freez(response_plaintext);
490
- return 0;
503
+ return HTTPS_CLIENT_RESP_OK;
504
}
505
506
#define JSON_KEY_ENC "encoding"
525
if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_TRP_TYPE)) {
526
PARSE_ENV_JSON_CHK_TYPE(&it, json_type_string, JSON_KEY_TRP_TYPE)
527
if (trp->type != ACLK_TRP_UNKNOWN) {
515
- netdata_log_error(JSON_KEY_TRP_TYPE " set already");
528
+ netdata_log_error("ACLK: " JSON_KEY_TRP_TYPE " set already");
529
goto exit;
530
}
531
trp->type = aclk_transport_type_t_from_str(json_object_get_string(json_object_iter_peek_value(&it)));
532
if (trp->type == ACLK_TRP_UNKNOWN) {
520
- netdata_log_error(JSON_KEY_TRP_TYPE " unknown type \"%s\"", json_object_get_string(json_object_iter_peek_value(&it)));
533
+ netdata_log_error("ACLK: " JSON_KEY_TRP_TYPE " unknown type \"%s\"", json_object_get_string(json_object_iter_peek_value(&it)));
534
goto exit;
535
}
536
json_object_iter_next(&it);
540
if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_TRP_ENDPOINT)) {
541
PARSE_ENV_JSON_CHK_TYPE(&it, json_type_string, JSON_KEY_TRP_ENDPOINT)
542
if (trp->endpoint) {
530
- netdata_log_error(JSON_KEY_TRP_ENDPOINT " set already");
543
+ netdata_log_error("ACLK: " JSON_KEY_TRP_ENDPOINT " set already");
544
goto exit;
545
}
546
trp->endpoint = strdupz(json_object_get_string(json_object_iter_peek_value(&it)));
548
continue;
549
}
550
538
- netdata_log_error("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it));
551
+ netdata_log_error("ACLK: unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it));
552
json_object_iter_next(&it);
553
}
554
555
if (!trp->endpoint) {
543
- netdata_log_error(JSON_KEY_TRP_ENDPOINT " is missing from JSON dictionary");
556
+ netdata_log_error("ACLK: " JSON_KEY_TRP_ENDPOINT " is missing from JSON dictionary");
557
goto exit;
558
}
559
560
if (trp->type == ACLK_TRP_UNKNOWN) {
548
- netdata_log_error("transport type not set");
561
+ netdata_log_error("ACLK: transport type not set");
562
goto exit;
563
}
564
574
json_object *obj;
575
576
if (env->transports) {
564
- netdata_log_error("transports have been set already");
577
+ netdata_log_error("ACLK: transports have been set already");
578
return 1;
579
}
580
586
trp = callocz(1, sizeof(aclk_transport_desc_t));
587
obj = json_object_array_get_idx(json_array, i);
588
if (parse_json_env_transport(obj, trp)) {
576
- netdata_log_error("error parsing transport idx %d", (int)i);
589
+ netdata_log_error("ACLK: error parsing transport idx %d", (int)i);
590
freez(trp);
591
return 1;
592
}
602
static int parse_json_backoff_int(struct json_object_iterator *it, int *out, const char* name, int min, int max) {
603
if (!strcmp(json_object_iter_peek_name(it), name)) {
604
if (json_object_get_type(json_object_iter_peek_value(it)) != json_type_int) {
592
- netdata_log_error("Could not parse \"%s\". Not an integer as expected.", name);
605
+ netdata_log_error("ACLK: Could not parse \"%s\". Not an integer as expected.", name);
606
return MATCHED_ERROR;
607
}
608
609
*out = json_object_get_int(json_object_iter_peek_value(it));
610
611
if (*out < min || *out > max) {
599
- netdata_log_error("Value of \"%s\"=%d out of range (%d-%d).", name, *out, min, max);
612
+ netdata_log_error("ACLK: Value of \"%s\"=%d out of range (%d-%d).", name, *out, min, max);
613
return MATCHED_ERROR;
614
}
615
651
continue;
652
}
653
641
- netdata_log_error("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it));
654
+ netdata_log_error("ACLK: unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it));
655
json_object_iter_next(&it);
656
}
657
663
const char *str;
664
665
if (env->capabilities) {
653
- netdata_log_error("transports have been set already");
666
+ netdata_log_error("ACLK: transports have been set already");
667
return 1;
668
}
669
678
for (size_t i = 0; i < env->capability_count; i++) {
679
obj = json_object_array_get_idx(json, i);
680
if (json_object_get_type(obj) != json_type_string) {
668
- netdata_log_error("Capability at index %d not a string!", (int)i);
681
+ netdata_log_error("ACLK: Capability at index %d not a string!", (int)i);
682
return 1;
683
}
684
str = json_object_get_string(obj);
685
if (!str) {
673
- netdata_log_error("Error parsing capabilities");
686
+ netdata_log_error("ACLK: Error parsing capabilities");
687
return 1;
688
}
689
env->capabilities[i] = strdupz(str);
699
700
json = json_tokener_parse(json_str);
701
if (!json) {
689
- netdata_log_error("JSON-C failed to parse the payload of http response of /env endpoint");
702
+ netdata_log_error("ACLK: JSON-C failed to parse the payload of http response of /env endpoint");
703
return 1;
704
}
705
710
if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_AUTH_ENDPOINT)) {
711
PARSE_ENV_JSON_CHK_TYPE(&it, json_type_string, JSON_KEY_AUTH_ENDPOINT)
712
if (env->auth_endpoint) {
700
- netdata_log_error("authEndpoint set already");
713
+ netdata_log_error("ACLK: authEndpoint set already");
714
goto exit;
715
}
716
env->auth_endpoint = strdupz(json_object_get_string(json_object_iter_peek_value(&it)));
721
if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_ENC)) {
722
PARSE_ENV_JSON_CHK_TYPE(&it, json_type_string, JSON_KEY_ENC)
723
if (env->encoding != ACLK_ENC_UNKNOWN) {
711
- netdata_log_error(JSON_KEY_ENC " set already");
724
+ netdata_log_error("ACLK: " JSON_KEY_ENC " set already");
725
goto exit;
726
}
727
env->encoding = aclk_encoding_type_t_from_str(json_object_get_string(json_object_iter_peek_value(&it)));
744
745
if (parse_json_backoff(json_object_iter_peek_value(&it), &env->backoff)) {
746
env->backoff.base = 0;
734
- netdata_log_error("Error parsing Backoff parameters in env");
747
+ netdata_log_error("ACLK: Error parsing Backoff parameters in env");
748
goto exit;
749
}
750
756
PARSE_ENV_JSON_CHK_TYPE(&it, json_type_array, JSON_KEY_CAPS)
757
758
if (parse_json_env_caps(json_object_iter_peek_value(&it), env)) {
746
- netdata_log_error("Error parsing capabilities list");
759
+ netdata_log_error("ACLK: Error parsing capabilities list");
760
goto exit;
761
}
762
764
continue;
765
}
766
754
- netdata_log_error("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it));
767
+ netdata_log_error("ACLK: unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it));
768
json_object_iter_next(&it);
769
}
770
771
// Check all compulsory keys have been set
772
if (env->transport_count < 1) {
760
- netdata_log_error("env has to return at least one transport");
773
+ netdata_log_error("ACLK: env has to return at least one transport");
774
goto exit;
775
}
776
if (!env->auth_endpoint) {
764
- netdata_log_error(JSON_KEY_AUTH_ENDPOINT " is compulsory");
777
+ netdata_log_error("ACLK: " JSON_KEY_AUTH_ENDPOINT " is compulsory");
778
goto exit;
779
}
780
if (env->encoding == ACLK_ENC_UNKNOWN) {
768
- netdata_log_error(JSON_KEY_ENC " is compulsory");
781
+ netdata_log_error("ACLK: " JSON_KEY_ENC " is compulsory");
782
goto exit;
783
}
784
if (!env->backoff.base) {
772
- netdata_log_error(JSON_KEY_BACKOFF " is compulsory");
785
+ netdata_log_error("ACLK: " JSON_KEY_BACKOFF " is compulsory");
786
goto exit;
787
}
788
795
return 1;
796
}
797
785
-int aclk_get_env(aclk_env_t *env, const char* aclk_hostname, int aclk_port, bool *fallback_ipv4) {
798
+https_client_resp_t aclk_get_env(aclk_env_t *env, const char* aclk_hostname, int aclk_port, bool *fallback_ipv4) {
799
BUFFER *buf = buffer_create(1024, &netdata_buffers_statistics.buffers_aclk);
800
801
+ https_client_resp_t rc;
802
https_req_t req = HTTPS_REQ_T_INITIALIZER;
803
https_req_response_t resp = HTTPS_REQ_RESPONSE_T_INITIALIZER;
804
806
807
CLAIM_ID claim_id = claim_id_get();
808
if (!claim_id_is_set(claim_id)) {
795
- netdata_log_error("Agent was not claimed - cannot perform challenge/response");
809
+ netdata_log_error("ACLK: failed to get ACLK environment (agent is not claimed)");
810
buffer_free(buf);
797
- return 1;
811
+ return HTTPS_CLIENT_RESP_ENV_AGENT_NOT_CLAIMED;
812
}
813
814
buffer_sprintf(buf, "/api/v1/env?v=%s&cap=proto,ctx&claim_id=%s", &(NETDATA_VERSION[1]) /* skip 'v' at beginning */, claim_id.str);
816
req.host = (char*)aclk_hostname;
817
req.port = aclk_port;
818
req.url = buf->buffer;
805
- if (aclk_https_request(&req, &resp, fallback_ipv4)) {
806
- netdata_log_error("Error trying to contact env endpoint");
819
+ rc = aclk_https_request(&req, &resp, fallback_ipv4);
820
+ if (rc != HTTPS_CLIENT_RESP_OK) {
821
+ netdata_log_error("ACLK: failed to get ACLK environment (cannot contact ENV endpoint)");
822
https_req_response_free(&resp);
823
buffer_free(buf);
809
- return 2;
824
+ return rc;
825
}
826
if (resp.http_code != 200) {
812
- netdata_log_error("The HTTP code not 200 OK (Got %d)", resp.http_code);
827
+ netdata_log_error("ACLK: failed to get ACLK environment (ENV response code is not 200) (got %d)", resp.http_code);
828
if (resp.payload_size)
829
aclk_parse_otp_error(resp.payload);
830
https_req_response_free(&resp);
831
buffer_free(buf);
817
- return 3;
832
+ return HTTPS_CLIENT_RESP_ENV_NOT_200;
833
}
834
835
if (!resp.payload || !resp.payload_size) {
821
- netdata_log_error("Unexpected empty payload as response to /env call");
836
+ netdata_log_error("ACLK: failed to get ACLK environment (ENV response is empty)");
837
https_req_response_free(&resp);
838
buffer_free(buf);
824
- return 4;
839
+ return HTTPS_CLIENT_RESP_ENV_EMPTY;
840
}
841
842
if (parse_json_env(resp.payload, env)) {
828
- netdata_log_error("error parsing /env message");
843
+ netdata_log_error("ACLK: failed to get ACLK environment (ENV response is not JSON)");
844
https_req_response_free(&resp);
845
buffer_free(buf);
831
- return 5;
846
+ return HTTPS_CLIENT_RESP_ENV_NOT_JSON;
847
}
848
834
- netdata_log_info("Getting Cloud /env successful");
835
-
849
https_req_response_free(&resp);
850
buffer_free(buf);
838
- return 0;
851
+ return HTTPS_CLIENT_RESP_OK;
852
}