diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-07-20 04:49:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-07-20 04:49:55 +0000 |
commit | ab1bb5b7f1c3c3a7b240ab7fc8661459ecd7decb (patch) | |
tree | 7a900833aad3ccc685712c6c2a7d87576d54f427 /aclk/aclk_otp.c | |
parent | Adding upstream version 1.40.1. (diff) | |
download | netdata-ab1bb5b7f1c3c3a7b240ab7fc8661459ecd7decb.tar.xz netdata-ab1bb5b7f1c3c3a7b240ab7fc8661459ecd7decb.zip |
Adding upstream version 1.41.0.upstream/1.41.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'aclk/aclk_otp.c')
-rw-r--r-- | aclk/aclk_otp.c | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/aclk/aclk_otp.c b/aclk/aclk_otp.c index 66d751be..46d0f621 100644 --- a/aclk/aclk_otp.c +++ b/aclk/aclk_otp.c @@ -38,7 +38,7 @@ struct auth_data { #define PARSE_ENV_JSON_CHK_TYPE(it, type, name) \ if (json_object_get_type(json_object_iter_peek_value(it)) != type) { \ - error("value of key \"%s\" should be %s", name, #type); \ + netdata_log_error("value of key \"%s\" should be %s", name, #type); \ goto exit; \ } @@ -55,7 +55,7 @@ static int parse_passwd_response(const char *json_str, struct auth_data *auth) { json = json_tokener_parse(json_str); if (!json) { - error("JSON-C failed to parse the payload of http response of /env endpoint"); + netdata_log_error("JSON-C failed to parse the payload of http response of /env endpoint"); return 1; } @@ -88,26 +88,26 @@ static int parse_passwd_response(const char *json_str, struct auth_data *auth) { PARSE_ENV_JSON_CHK_TYPE(&it, json_type_array, JSON_KEY_TOPICS) if (aclk_generate_topic_cache(json_object_iter_peek_value(&it))) { - error("Failed to generate topic cache!"); + netdata_log_error("Failed to generate topic cache!"); goto exit; } json_object_iter_next(&it); continue; } - error("Unknown key \"%s\" in passwd response payload. Ignoring", json_object_iter_peek_name(&it)); + netdata_log_error("Unknown key \"%s\" in passwd response payload. Ignoring", json_object_iter_peek_name(&it)); json_object_iter_next(&it); } if (!auth->client_id) { - error(JSON_KEY_CLIENTID " is compulsory key in /password response"); + netdata_log_error(JSON_KEY_CLIENTID " is compulsory key in /password response"); goto exit; } if (!auth->passwd) { - error(JSON_KEY_PASS " is compulsory in /password response"); + netdata_log_error(JSON_KEY_PASS " is compulsory in /password response"); goto exit; } if (!auth->username) { - error(JSON_KEY_USER " is compulsory in /password response"); + netdata_log_error(JSON_KEY_USER " is compulsory in /password response"); goto exit; } @@ -126,11 +126,11 @@ exit: static const char *get_json_str_by_path(json_object *json, const char *path) { json_object *ptr; if (json_pointer_get(json, path, &ptr)) { - error("Missing compulsory key \"%s\" in error response", path); + netdata_log_error("Missing compulsory key \"%s\" in error response", path); return NULL; } if (json_object_get_type(ptr) != json_type_string) { - error("Value of Key \"%s\" in error response should be string", path); + netdata_log_error("Value of Key \"%s\" in error response should be string", path); return NULL; } return json_object_get_string(ptr); @@ -147,7 +147,7 @@ static int aclk_parse_otp_error(const char *json_str) { json = json_tokener_parse(json_str); if (!json) { - error("JSON-C failed to parse the payload of http response of /env endpoint"); + netdata_log_error("JSON-C failed to parse the payload of http response of /env endpoint"); return 1; } @@ -163,7 +163,7 @@ static int aclk_parse_otp_error(const char *json_str) { // optional field if (!json_pointer_get(json, "/" JSON_KEY_ERTRY, &ptr)) { if (json_object_get_type(ptr) != json_type_boolean) { - error("Error response Key " "/" JSON_KEY_ERTRY " should be of boolean type"); + netdata_log_error("Error response Key " "/" JSON_KEY_ERTRY " should be of boolean type"); goto exit; } block_retry = json_object_get_boolean(ptr); @@ -172,7 +172,7 @@ static int aclk_parse_otp_error(const char *json_str) { // optional field if (!json_pointer_get(json, "/" JSON_KEY_EDELAY, &ptr)) { if (json_object_get_type(ptr) != json_type_int) { - error("Error response Key " "/" JSON_KEY_EDELAY " should be of integer type"); + netdata_log_error("Error response Key " "/" JSON_KEY_EDELAY " should be of integer type"); goto exit; } backoff = json_object_get_int(ptr); @@ -184,7 +184,7 @@ static int aclk_parse_otp_error(const char *json_str) { if (backoff > 0) aclk_block_until = now_monotonic_sec() + backoff; - 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); + 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); rc = 0; exit: json_object_put(json); @@ -205,7 +205,7 @@ static int aclk_parse_otp_error(const char *json_str) { json = json_tokener_parse(json_str); if (!json) { - error("JSON-C failed to parse the payload of http response of /env endpoint"); + netdata_log_error("JSON-C failed to parse the payload of http response of /env endpoint"); return 1; } @@ -236,7 +236,7 @@ static int aclk_parse_otp_error(const char *json_str) { } if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_EDELAY)) { if (json_object_get_type(json_object_iter_peek_value(&it)) != json_type_int) { - error("value of key " JSON_KEY_EDELAY " should be integer"); + netdata_log_error("value of key " JSON_KEY_EDELAY " should be integer"); goto exit; } @@ -246,7 +246,7 @@ static int aclk_parse_otp_error(const char *json_str) { } if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_ERTRY)) { if (json_object_get_type(json_object_iter_peek_value(&it)) != json_type_boolean) { - error("value of key " JSON_KEY_ERTRY " should be integer"); + netdata_log_error("value of key " JSON_KEY_ERTRY " should be integer"); goto exit; } @@ -254,7 +254,7 @@ static int aclk_parse_otp_error(const char *json_str) { json_object_iter_next(&it); continue; } - error("Unknown key \"%s\" in error response payload. Ignoring", json_object_iter_peek_name(&it)); + netdata_log_error("Unknown key \"%s\" in error response payload. Ignoring", json_object_iter_peek_name(&it)); json_object_iter_next(&it); } @@ -264,7 +264,7 @@ static int aclk_parse_otp_error(const char *json_str) { if (backoff > 0) aclk_block_until = now_monotonic_sec() + backoff; - 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); + 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); rc = 0; exit: json_object_put(json); @@ -301,7 +301,7 @@ inline static int base64_decode_helper(unsigned char *out, int *outl, const unsi EVP_DecodeFinal(ctx, remaining_data, &remainder); EVP_ENCODE_CTX_free(ctx); if (remainder) { - error("Unexpected data at EVP_DecodeFinal"); + netdata_log_error("Unexpected data at EVP_DecodeFinal"); return 1; } return 0; @@ -322,12 +322,12 @@ int aclk_get_otp_challenge(url_t *target, const char *agent_id, unsigned char ** req.url = (char *)buffer_tostring(url); if (aclk_https_request(&req, &resp)) { - error ("ACLK_OTP Challenge failed"); + netdata_log_error("ACLK_OTP Challenge failed"); buffer_free(url); return 1; } if (resp.http_code != 200) { - error ("ACLK_OTP Challenge HTTP code not 200 OK (got %d)", resp.http_code); + netdata_log_error("ACLK_OTP Challenge HTTP code not 200 OK (got %d)", resp.http_code); buffer_free(url); if (resp.payload_size) aclk_parse_otp_error(resp.payload); @@ -335,36 +335,36 @@ int aclk_get_otp_challenge(url_t *target, const char *agent_id, unsigned char ** } buffer_free(url); - info ("ACLK_OTP Got Challenge from Cloud"); + netdata_log_info("ACLK_OTP Got Challenge from Cloud"); json_object *json = json_tokener_parse(resp.payload); if (!json) { - error ("Couldn't parse HTTP GET challenge payload"); + netdata_log_error("Couldn't parse HTTP GET challenge payload"); goto cleanup_resp; } json_object *challenge_json; if (!json_object_object_get_ex(json, "challenge", &challenge_json)) { - error ("No key named \"challenge\" in the returned JSON"); + netdata_log_error("No key named \"challenge\" in the returned JSON"); goto cleanup_json; } if (!json_object_is_type(challenge_json, json_type_string)) { - error ("\"challenge\" is not a string JSON type"); + netdata_log_error("\"challenge\" is not a string JSON type"); goto cleanup_json; } const char *challenge_base64; if (!(challenge_base64 = json_object_get_string(challenge_json))) { - error("Failed to extract challenge from JSON object"); + netdata_log_error("Failed to extract challenge from JSON object"); goto cleanup_json; } if (strlen(challenge_base64) != CHALLENGE_LEN_BASE64) { - error("Received Challenge has unexpected length of %zu (expected %d)", strlen(challenge_base64), CHALLENGE_LEN_BASE64); + netdata_log_error("Received Challenge has unexpected length of %zu (expected %d)", strlen(challenge_base64), CHALLENGE_LEN_BASE64); goto cleanup_json; } *challenge = mallocz((CHALLENGE_LEN_BASE64 / 4) * 3); base64_decode_helper(*challenge, challenge_bytes, (const unsigned char*)challenge_base64, strlen(challenge_base64)); if (*challenge_bytes != CHALLENGE_LEN) { - error("Unexpected challenge length of %d instead of %d", *challenge_bytes, CHALLENGE_LEN); + netdata_log_error("Unexpected challenge length of %d instead of %d", *challenge_bytes, CHALLENGE_LEN); freez(*challenge); *challenge = NULL; goto cleanup_json; @@ -405,19 +405,19 @@ int aclk_send_otp_response(const char *agent_id, const unsigned char *response, req.payload_size = strlen(req.payload); if (aclk_https_request(&req, &resp)) { - error ("ACLK_OTP Password error trying to post result to password"); + netdata_log_error("ACLK_OTP Password error trying to post result to password"); goto cleanup_buffers; } if (resp.http_code != 201) { - error ("ACLK_OTP Password HTTP code not 201 Created (got %d)", resp.http_code); + netdata_log_error("ACLK_OTP Password HTTP code not 201 Created (got %d)", resp.http_code); if (resp.payload_size) aclk_parse_otp_error(resp.payload); goto cleanup_response; } - info ("ACLK_OTP Got Password from Cloud"); + netdata_log_info("ACLK_OTP Got Password from Cloud"); if (parse_passwd_response(resp.payload, mqtt_auth)){ - error("Error parsing response of password endpoint"); + netdata_log_error("Error parsing response of password endpoint"); goto cleanup_response; } @@ -470,7 +470,7 @@ static int private_decrypt(RSA *p_key, unsigned char * enc_data, int data_len, u { char err[512]; ERR_error_string_n(ERR_get_error(), err, sizeof(err)); - error("Decryption of the challenge failed: %s", err); + netdata_log_error("Decryption of the challenge failed: %s", err); } return result; } @@ -486,13 +486,13 @@ int aclk_get_mqtt_otp(RSA *p_key, char **mqtt_id, char **mqtt_usr, char **mqtt_p char *agent_id = get_agent_claimid(); if (agent_id == NULL) { - error("Agent was not claimed - cannot perform challenge/response"); + netdata_log_error("Agent was not claimed - cannot perform challenge/response"); return 1; } // Get Challenge if (aclk_get_otp_challenge(target, agent_id, &challenge, &challenge_bytes)) { - error("Error getting challenge"); + netdata_log_error("Error getting challenge"); freez(agent_id); return 1; } @@ -501,7 +501,7 @@ int aclk_get_mqtt_otp(RSA *p_key, char **mqtt_id, char **mqtt_usr, char **mqtt_p unsigned char *response_plaintext; int response_plaintext_bytes = private_decrypt(p_key, challenge, challenge_bytes, &response_plaintext); if (response_plaintext_bytes < 0) { - error ("Couldn't decrypt the challenge received"); + netdata_log_error("Couldn't decrypt the challenge received"); freez(response_plaintext); freez(challenge); freez(agent_id); @@ -512,7 +512,7 @@ int aclk_get_mqtt_otp(RSA *p_key, char **mqtt_id, char **mqtt_usr, char **mqtt_p // Encode and Send Challenge struct auth_data data = { .client_id = NULL, .passwd = NULL, .username = NULL }; if (aclk_send_otp_response(agent_id, response_plaintext, response_plaintext_bytes, target, &data)) { - error("Error getting response"); + netdata_log_error("Error getting response"); freez(response_plaintext); freez(agent_id); return 1; @@ -549,12 +549,12 @@ static int parse_json_env_transport(json_object *json, aclk_transport_desc_t *tr if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_TRP_TYPE)) { PARSE_ENV_JSON_CHK_TYPE(&it, json_type_string, JSON_KEY_TRP_TYPE) if (trp->type != ACLK_TRP_UNKNOWN) { - error(JSON_KEY_TRP_TYPE " set already"); + netdata_log_error(JSON_KEY_TRP_TYPE " set already"); goto exit; } trp->type = aclk_transport_type_t_from_str(json_object_get_string(json_object_iter_peek_value(&it))); if (trp->type == ACLK_TRP_UNKNOWN) { - error(JSON_KEY_TRP_TYPE " unknown type \"%s\"", json_object_get_string(json_object_iter_peek_value(&it))); + netdata_log_error(JSON_KEY_TRP_TYPE " unknown type \"%s\"", json_object_get_string(json_object_iter_peek_value(&it))); goto exit; } json_object_iter_next(&it); @@ -564,25 +564,25 @@ static int parse_json_env_transport(json_object *json, aclk_transport_desc_t *tr if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_TRP_ENDPOINT)) { PARSE_ENV_JSON_CHK_TYPE(&it, json_type_string, JSON_KEY_TRP_ENDPOINT) if (trp->endpoint) { - error(JSON_KEY_TRP_ENDPOINT " set already"); + netdata_log_error(JSON_KEY_TRP_ENDPOINT " set already"); goto exit; } trp->endpoint = strdupz(json_object_get_string(json_object_iter_peek_value(&it))); json_object_iter_next(&it); continue; } - - error ("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it)); + + netdata_log_error("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it)); json_object_iter_next(&it); } if (!trp->endpoint) { - error (JSON_KEY_TRP_ENDPOINT " is missing from JSON dictionary"); + netdata_log_error(JSON_KEY_TRP_ENDPOINT " is missing from JSON dictionary"); goto exit; } if (trp->type == ACLK_TRP_UNKNOWN) { - error ("transport type not set"); + netdata_log_error("transport type not set"); goto exit; } @@ -598,7 +598,7 @@ static int parse_json_env_transports(json_object *json_array, aclk_env_t *env) { json_object *obj; if (env->transports) { - error("transports have been set already"); + netdata_log_error("transports have been set already"); return 1; } @@ -610,7 +610,7 @@ static int parse_json_env_transports(json_object *json_array, aclk_env_t *env) { trp = callocz(1, sizeof(aclk_transport_desc_t)); obj = json_object_array_get_idx(json_array, i); if (parse_json_env_transport(obj, trp)) { - error("error parsing transport idx %d", (int)i); + netdata_log_error("error parsing transport idx %d", (int)i); freez(trp); return 1; } @@ -626,14 +626,14 @@ static int parse_json_env_transports(json_object *json_array, aclk_env_t *env) { static int parse_json_backoff_int(struct json_object_iterator *it, int *out, const char* name, int min, int max) { if (!strcmp(json_object_iter_peek_name(it), name)) { if (json_object_get_type(json_object_iter_peek_value(it)) != json_type_int) { - error("Could not parse \"%s\". Not an integer as expected.", name); + netdata_log_error("Could not parse \"%s\". Not an integer as expected.", name); return MATCHED_ERROR; } *out = json_object_get_int(json_object_iter_peek_value(it)); if (*out < min || *out > max) { - error("Value of \"%s\"=%d out of range (%d-%d).", name, *out, min, max); + netdata_log_error("Value of \"%s\"=%d out of range (%d-%d).", name, *out, min, max); return MATCHED_ERROR; } @@ -675,7 +675,7 @@ static int parse_json_backoff(json_object *json, aclk_backoff_t *backoff) { continue; } - error ("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it)); + netdata_log_error("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it)); json_object_iter_next(&it); } @@ -687,7 +687,7 @@ static int parse_json_env_caps(json_object *json, aclk_env_t *env) { const char *str; if (env->capabilities) { - error("transports have been set already"); + netdata_log_error("transports have been set already"); return 1; } @@ -702,12 +702,12 @@ static int parse_json_env_caps(json_object *json, aclk_env_t *env) { for (size_t i = 0; i < env->capability_count; i++) { obj = json_object_array_get_idx(json, i); if (json_object_get_type(obj) != json_type_string) { - error("Capability at index %d not a string!", (int)i); + netdata_log_error("Capability at index %d not a string!", (int)i); return 1; } str = json_object_get_string(obj); if (!str) { - error("Error parsing capabilities"); + netdata_log_error("Error parsing capabilities"); return 1; } env->capabilities[i] = strdupz(str); @@ -723,7 +723,7 @@ static int parse_json_env(const char *json_str, aclk_env_t *env) { json = json_tokener_parse(json_str); if (!json) { - error("JSON-C failed to parse the payload of http response of /env endpoint"); + netdata_log_error("JSON-C failed to parse the payload of http response of /env endpoint"); return 1; } @@ -734,7 +734,7 @@ static int parse_json_env(const char *json_str, aclk_env_t *env) { if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_AUTH_ENDPOINT)) { PARSE_ENV_JSON_CHK_TYPE(&it, json_type_string, JSON_KEY_AUTH_ENDPOINT) if (env->auth_endpoint) { - error("authEndpoint set already"); + netdata_log_error("authEndpoint set already"); goto exit; } env->auth_endpoint = strdupz(json_object_get_string(json_object_iter_peek_value(&it))); @@ -745,7 +745,7 @@ static int parse_json_env(const char *json_str, aclk_env_t *env) { if (!strcmp(json_object_iter_peek_name(&it), JSON_KEY_ENC)) { PARSE_ENV_JSON_CHK_TYPE(&it, json_type_string, JSON_KEY_ENC) if (env->encoding != ACLK_ENC_UNKNOWN) { - error(JSON_KEY_ENC " set already"); + netdata_log_error(JSON_KEY_ENC " set already"); goto exit; } env->encoding = aclk_encoding_type_t_from_str(json_object_get_string(json_object_iter_peek_value(&it))); @@ -768,7 +768,7 @@ static int parse_json_env(const char *json_str, aclk_env_t *env) { if (parse_json_backoff(json_object_iter_peek_value(&it), &env->backoff)) { env->backoff.base = 0; - error("Error parsing Backoff parameters in env"); + netdata_log_error("Error parsing Backoff parameters in env"); goto exit; } @@ -780,7 +780,7 @@ static int parse_json_env(const char *json_str, aclk_env_t *env) { PARSE_ENV_JSON_CHK_TYPE(&it, json_type_array, JSON_KEY_CAPS) if (parse_json_env_caps(json_object_iter_peek_value(&it), env)) { - error("Error parsing capabilities list"); + netdata_log_error("Error parsing capabilities list"); goto exit; } @@ -788,25 +788,25 @@ static int parse_json_env(const char *json_str, aclk_env_t *env) { continue; } - error ("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it)); + netdata_log_error("unknown JSON key in dictionary (\"%s\")", json_object_iter_peek_name(&it)); json_object_iter_next(&it); } // Check all compulsory keys have been set if (env->transport_count < 1) { - error("env has to return at least one transport"); + netdata_log_error("env has to return at least one transport"); goto exit; } if (!env->auth_endpoint) { - error(JSON_KEY_AUTH_ENDPOINT " is compulsory"); + netdata_log_error(JSON_KEY_AUTH_ENDPOINT " is compulsory"); goto exit; } if (env->encoding == ACLK_ENC_UNKNOWN) { - error(JSON_KEY_ENC " is compulsory"); + netdata_log_error(JSON_KEY_ENC " is compulsory"); goto exit; } if (!env->backoff.base) { - error(JSON_KEY_BACKOFF " is compulsory"); + netdata_log_error(JSON_KEY_BACKOFF " is compulsory"); goto exit; } @@ -830,7 +830,7 @@ int aclk_get_env(aclk_env_t *env, const char* aclk_hostname, int aclk_port) { char *agent_id = get_agent_claimid(); if (agent_id == NULL) { - error("Agent was not claimed - cannot perform challenge/response"); + netdata_log_error("Agent was not claimed - cannot perform challenge/response"); buffer_free(buf); return 1; } @@ -843,35 +843,35 @@ int aclk_get_env(aclk_env_t *env, const char* aclk_hostname, int aclk_port) { req.port = aclk_port; req.url = buf->buffer; if (aclk_https_request(&req, &resp)) { - error("Error trying to contact env endpoint"); + netdata_log_error("Error trying to contact env endpoint"); https_req_response_free(&resp); buffer_free(buf); - return 1; + return 2; } if (resp.http_code != 200) { - error("The HTTP code not 200 OK (Got %d)", resp.http_code); + netdata_log_error("The HTTP code not 200 OK (Got %d)", resp.http_code); if (resp.payload_size) aclk_parse_otp_error(resp.payload); https_req_response_free(&resp); buffer_free(buf); - return 1; + return 3; } if (!resp.payload || !resp.payload_size) { - error("Unexpected empty payload as response to /env call"); + netdata_log_error("Unexpected empty payload as response to /env call"); https_req_response_free(&resp); buffer_free(buf); - return 1; + return 4; } if (parse_json_env(resp.payload, env)) { - error ("error parsing /env message"); + netdata_log_error("error parsing /env message"); https_req_response_free(&resp); buffer_free(buf); - return 1; + return 5; } - info("Getting Cloud /env successful"); + netdata_log_info("Getting Cloud /env successful"); https_req_response_free(&resp); buffer_free(buf); |