Compare commits

..
Author SHA1 Message Date
Brett T. Warden b84ef8e50b Publish a JSON message to telemetry server
Replace the custom HTTP headers and plain text payload with a JSON
object. This is the new v3 API implementation.
2023-03-21 17:22:14 -07:00
Brett T. Warden 4482392f02 Link telemetry post daemon against json-c
also link its test
2023-03-21 17:21:55 -07:00
Brett T. Warden 059b785b39 Check for json-c
json-c is now required, in order to construct the JSON payload.
2023-03-21 16:11:02 -07:00
Brett T. Warden e525725723 Update default URL API to v3
Updating the HTTP API from v2 to v3 to support JSON message
encapsulation instead of custom HTTP headers with a single-field POST
field for the payload.
2023-03-21 16:07:13 -07:00
10 changed files with 49 additions and 65 deletions
+8 -6
View File
@@ -8,13 +8,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.sha }}
- uses: actions/checkout@v1
- name: install dependencies
run: |
sudo apt-get update
sudo apt-get --fix-missing install libcurl4-gnutls-dev valgrind libelf-dev libdw-dev libjson-c-dev check
run: sudo apt-get install libcurl4-gnutls-dev valgrind libelf-dev libdw-dev
- name: install check
run: wget https://github.com/libcheck/check/releases/download/0.12.0/check-0.12.0.tar.gz &&
tar xf check-0.12.0.tar.gz &&
pushd check-0.12.0 &&
./configure --prefix=/usr --libdir=/usr/lib/x86_64-linux-gnu &&
make -j8 && sudo make install && popd
- name: autogen
run: sh autogen.sh
- name: configure
View File
+1 -1
View File
@@ -4,7 +4,7 @@ ACLOCAL_AMFLAGS = -I build-aux/m4
EXTRA_DIST = \
AUTHORS \
LICENSE
LICENSE.LGPL-2.1
DISTCHECK_CONFIGURE_FLAGS = \
--with-systemdsystemunitdir=$$dc_install_base/$(systemdunitdir) \
+1 -1
View File
@@ -2,7 +2,7 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ([2.69])
AC_INIT([telemetrics-client], [2.4.2], [https://clearlinux.org/])
AC_INIT([telemetrics-client], [2.3.5], [https://clearlinux.org/])
AC_CONFIG_AUX_DIR([build-aux])
AM_INIT_AUTOMAKE([1.14 -Wall -Werror -Wno-extra-portability foreign subdir-objects])
AM_SILENT_RULES([yes])
+2 -2
View File
@@ -124,8 +124,8 @@ bool read_record(char *fullpath, char *headers[], char **body, char **cfg_file)
}
memset(*body, 0, (size_t)size);
//read rest of file. '-1' ensures it's null terminated
size_t newlen = fread(*body, sizeof(char), (size_t)size - 1, fp);
//read rest of file
size_t newlen = fread(*body, sizeof(char), (size_t)size, fp);
if (newlen == 0) {
telem_perror("Error reading staged file");
result = false;
+1 -3
View File
@@ -421,7 +421,6 @@ static void print_record(char *record_id)
recordfp = fopen(filepath, "r");
if (!recordfp) {
telem_log(LOG_INFO, "Could not open record %s: %s\n", record_id, strerror(errno));
free(filepath);
return;
}
@@ -662,8 +661,7 @@ int prune_journal(struct TelemJournal *telem_journal, char *tmp_dir)
telem_journal->fptr = fopen(telem_journal->journal_file, "a+");
if (!telem_journal->fptr) {
telem_log(LOG_ERR, "Error re-opening journal file\n");
rc = 1;
goto quit;
return rc;
}
// update record count
telem_journal->record_count = telem_journal->record_count - count;
+1 -1
View File
@@ -159,7 +159,7 @@ int main(int argc, char **argv)
break;
case 'H':
str = "org.clearlinux/heartbeat/ping";
memcpy(classification, str, strlen(str) + 1);
memcpy(classification, str, strlen(str));
break;
case 'l':
payload_options |= PAYLOAD_LOCALE;
+8 -20
View File
@@ -25,7 +25,6 @@
#include <limits.h>
#include <stdbool.h>
#include <errno.h>
#include <fcntl.h>
#include "spool.h"
#include "telempostdaemon.h"
@@ -150,37 +149,27 @@ void process_spooled_record(const char *spool_dir, char *name,
}
(*records_processed)++;
// Use file descriptor to mitigate TOCTOU
int fd = open(record_name, O_RDONLY | O_NOFOLLOW);
if (fd == -1) {
telem_perror("Unable to open record in spool");
goto exit;
}
if (fstat(fd, &buf) == -1) {
telem_perror("Unable to fstat record in spool");
close(fd);
goto exit;
ret = stat(record_name, &buf);
if (ret == -1) {
telem_perror("Unable to stat record in spool");
free(record_name);
return;
}
/*
* If file is a regular file, if uid is different than process uid,
* or if mtime is greater than record expiry, delete the file.
* If file is a regular file , if uid is diff than process uid
* or if mtime is greater than record expiry delete the file
*/
if (record_expiry_config() == -1) {
telem_log(LOG_ERR, "Invalid record expiry value\n");
close(fd);
exit(EXIT_FAILURE);
}
if (!S_ISREG(buf.st_mode) ||
(current_time - buf.st_mtime > (record_expiry_config() * 60)) ||
(buf.st_uid != getuid())) {
(buf.st_uid != getuid())) {
unlink(record_name);
close(fd);
} else if (post_succeeded && *records_sent <= TM_SPOOL_MAX_SEND_RECORDS) {
close(fd);
transmit_spooled_record(record_name, &post_succeeded, buf.st_size);
if (!post_succeeded) {
@@ -205,7 +194,6 @@ void process_spooled_record(const char *spool_dir, char *name,
}
}
}
exit:
free(record_name);
}
+5 -6
View File
@@ -977,18 +977,17 @@ int tm_set_payload(struct telem_ref *t_ref, char *payload)
size_t payload_len;
int ret = 0;
if (payload == NULL) {
telem_log(LOG_WARNING, "payload pointer is NULL\n");
payload_len = strlen((char *)payload);
if (payload_len > MAX_PAYLOAD_LENGTH) {
return -EINVAL;
}
payload_len = strnlen(payload, MAX_PAYLOAD_LENGTH);
if (payload_is_ascii(payload, payload_len) != 0) {
return -EINVAL;
}
t_ref->record->payload = strndup(payload, payload_len);
t_ref->record->payload = strdup(payload);
if (!t_ref->record->payload) {
telem_log(LOG_CRIT, "CRIT: Out of memory\n");
@@ -1274,7 +1273,7 @@ int tm_send_record(struct telem_ref *t_ref)
*/
record_size = (2 * sizeof(uint32_t)) + total_size + 1;
data = (char *)calloc(sizeof(char), record_size);
data = calloc(sizeof(char), record_size);
if (!data) {
telem_log(LOG_CRIT, "CRIT: Out of memory\n");
close(sfd);
+22 -25
View File
@@ -266,7 +266,7 @@ bool post_record_http(char *headers[], char *body, char *cfg)
// settings than explicitly requested.
// However, report success so the record gets deleted.
res = 0;
goto done;
goto Done;
}
reload_config();
telem_debug("DEBUG: override server_addr:%s\n", server_addr_config());
@@ -288,35 +288,32 @@ bool post_record_http(char *headers[], char *body, char *cfg)
/* TODO: check if memory needs to be released */
}
// Errors for any curl_easy_* functions will store nice error messages
// in errorbuf, so send log messages with errorbuf contents
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorbuf);
curl_easy_setopt(curl, CURLOPT_URL, server_addr_config());
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
curl_easy_setopt(curl, CURLOPT_POST, 1);
#ifdef DEBUG
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
#endif
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
custom_headers = curl_slist_append(custom_headers, tid_header);
// This should be set by probes/libtelemetry in the future
custom_headers = curl_slist_append(custom_headers, content);
// Errors for any curl_easy_* functions will store nice error messages
// in errorbuf, so send log messages with errorbuf contents
if (curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorbuf) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_URL, server_addr_config()) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 5L) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_POST, 1) != CURLE_OK ||
#ifdef DEBUG
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1) != CURLE_OK ||
#endif
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, custom_headers) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_body) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(json_body)) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY) != CURLE_OK) {
telem_log(LOG_ERR, "curl_easy_setopt(): Failed to set one or more options\n");
goto exit;
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, custom_headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_body);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(json_body));
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
if (strlen(cert_file) > 0) {
if (access(cert_file, F_OK) != -1) {
if (curl_easy_setopt(curl, CURLOPT_CAINFO, cert_file) != CURLE_OK) {
telem_log(LOG_ERR, "curl_easy_setopt(): Failed to set CAINFO\n");
goto exit;
}
curl_easy_setopt(curl, CURLOPT_CAINFO, cert_file);
telem_log(LOG_INFO, "cafile was set to %s\n", cert_file);
}
}
@@ -347,12 +344,12 @@ bool post_record_http(char *headers[], char *body, char *cfg)
telem_log(LOG_INFO, "Record sent successfully\n");
}
exit:
curl_slist_free_all(custom_headers);
curl_easy_cleanup(curl);
curl_global_cleanup();
done:
Done:
if (json_body) {
free(json_body);
json_body = NULL;