diff --git a/plugins/custom_calyptia/calyptia.c b/plugins/custom_calyptia/calyptia.c index 50158ca0b68..76bea66d0cc 100644 --- a/plugins/custom_calyptia/calyptia.c +++ b/plugins/custom_calyptia/calyptia.c @@ -28,6 +28,8 @@ #include #include +#include + struct calyptia { /* config map options */ flb_sds_t api_key; @@ -316,6 +318,63 @@ static struct flb_output_instance *setup_cloud_output(struct flb_config *config, return cloud; } +static flb_sds_t sha256_to_hex(unsigned char *sha256) +{ + int idx; + flb_sds_t hex; + flb_sds_t tmp; + + hex = flb_sds_create_size(64); + + if (!hex) { + return NULL; + } + + for (idx = 0; idx < 32; idx++) { + tmp = flb_sds_printf(&hex, "%02x", sha256[idx]); + + if (!tmp) { + flb_sds_destroy(hex); + return NULL; + } + + hex = tmp; + } + + flb_sds_len_set(hex, 64); + return hex; +} + +static flb_sds_t get_machine_id(struct calyptia *ctx) +{ + int ret; + char *buf; + size_t blen; + unsigned char sha256_buf[64] = {0}; + + /* retrieve raw machine id */ + ret = flb_utils_get_machine_id(&buf, &blen); + + if (ret == -1) { + flb_plg_error(ctx->ins, "could not obtain machine id"); + return NULL; + } + + ret = flb_hash_simple(FLB_HASH_SHA256, + (unsigned char *) buf, + blen, + sha256_buf, + sizeof(sha256_buf)); + flb_free(buf); + + if (ret != FLB_CRYPTO_SUCCESS) { + return NULL; + } + + /* convert to hex */ + return sha256_to_hex(sha256_buf); +} + static int cb_calyptia_init(struct flb_custom_instance *ins, struct flb_config *config, void *data) @@ -344,6 +403,17 @@ static int cb_calyptia_init(struct flb_custom_instance *ins, /* map instance and local context */ flb_custom_set_context(ins, ctx); + /* If no machine_id has been provided via a configuration option get it from the local machine-id. */ + if (!ctx->machine_id) { + /* machine id */ + ctx->machine_id = get_machine_id(ctx); + + if (ctx->machine_id == NULL) { + flb_plg_error(ctx->ins, "unable to retrieve machine_id"); + return -1; + } + } + /* input collector */ ctx->i = flb_input_new(config, "fluentbit_metrics", NULL, FLB_TRUE); @@ -412,6 +482,10 @@ static int cb_calyptia_init(struct flb_custom_instance *ins, if (ctx->fleet_config_dir) { flb_input_set_property(ctx->fleet, "config_dir", ctx->fleet_config_dir); } + + if (ctx->machine_id) { + flb_input_set_property(ctx->fleet, "machine_id", ctx->machine_id); + } } if (ctx->o) { diff --git a/plugins/in_calyptia_fleet/in_calyptia_fleet.c b/plugins/in_calyptia_fleet/in_calyptia_fleet.c index fc5d0addbde..281d052daf6 100644 --- a/plugins/in_calyptia_fleet/in_calyptia_fleet.c +++ b/plugins/in_calyptia_fleet/in_calyptia_fleet.c @@ -64,6 +64,7 @@ struct flb_in_calyptia_fleet_config { flb_sds_t api_key; flb_sds_t fleet_id; flb_sds_t fleet_name; + flb_sds_t machine_id; flb_sds_t config_dir; flb_sds_t cloud_host; flb_sds_t cloud_port; @@ -641,6 +642,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, flb_sds_t cfgoldname; flb_sds_t cfgcurname; flb_sds_t header; + flb_sds_t hdr; FILE *cfgfp; const char *fbit_last_modified; int fbit_last_modified_len; @@ -737,16 +739,16 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, header = flb_sds_create_size(4096); if (ctx->fleet_name == NULL) { - flb_sds_printf(&header, + hdr = flb_sds_printf(&header, "[CUSTOM]\n" - " Name calyptia\n" - " api_key %s\n" - " fleet_id %s\n" - " add_label fleet_id %s\n" - " fleet.config_dir %s\n" - " calyptia_host %s\n" - " calyptia_port %d\n" - " calyptia_tls %s\n", + " Name calyptia\n" + " api_key %s\n" + " fleet_id %s\n" + " add_label fleet_id %s\n" + " fleet.config_dir %s\n" + " calyptia_host %s\n" + " calyptia_port %d\n" + " calyptia_tls %s\n", ctx->api_key, ctx->fleet_id, ctx->fleet_id, @@ -757,7 +759,7 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, ); } else { - flb_sds_printf(&header, + hdr = flb_sds_printf(&header, "[CUSTOM]\n" " Name calyptia\n" " api_key %s\n" @@ -778,6 +780,17 @@ static int in_calyptia_fleet_collect(struct flb_input_instance *ins, tls_setting_string(ctx->ins->use_tls) ); } + if (hdr == NULL) { + fclose(cfgfp); + goto http_error; + } + if (ctx->machine_id) { + hdr = flb_sds_printf(&header, " machine_id %s\n", ctx->machine_id); + if (hdr == NULL) { + fclose(cfgfp); + goto http_error; + } + } fwrite(header, strlen(header), 1, cfgfp); flb_sds_destroy(header); fwrite(data, client->resp.payload_size, 1, cfgfp); @@ -1010,6 +1023,11 @@ static struct flb_config_map config_map[] = { 0, FLB_TRUE, offsetof(struct flb_in_calyptia_fleet_config, fleet_name), "Calyptia Fleet Name (used to lookup the fleet ID via the cloud API)." }, + { + FLB_CONFIG_MAP_STR, "machine_id", NULL, + 0, FLB_TRUE, offsetof(struct flb_in_calyptia_fleet_config, machine_id), + "Agent Machine ID." + }, { FLB_CONFIG_MAP_INT, "event_fd", "-1", 0, FLB_TRUE, offsetof(struct flb_in_calyptia_fleet_config, event_fd), diff --git a/plugins/out_calyptia/calyptia.c b/plugins/out_calyptia/calyptia.c index a09cee0b702..19811dcc908 100644 --- a/plugins/out_calyptia/calyptia.c +++ b/plugins/out_calyptia/calyptia.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -94,69 +93,6 @@ static void append_labels(struct flb_calyptia *ctx, struct cmt *cmt) } } -static flb_sds_t sha256_to_hex(unsigned char *sha256) -{ - int i; - flb_sds_t hex; - flb_sds_t tmp; - - hex = flb_sds_create_size(64); - if (!hex) { - return NULL; - } - - for (i = 0; i < 32; i++) { - tmp = flb_sds_printf(&hex, "%02x", sha256[i]); - if (!tmp) { - flb_sds_destroy(hex); - return NULL; - } - hex = tmp; - } - - flb_sds_len_set(hex, 64); - return hex; -} - -static int get_machine_id(struct flb_calyptia *ctx, char **out_buf, size_t *out_size) -{ - int ret; - char *buf; - flb_sds_t s_buf; - size_t s; - unsigned char sha256_buf[64] = {0}; - - /* retrieve raw machine id */ - ret = flb_utils_get_machine_id(&buf, &s); - if (ret == -1) { - flb_plg_error(ctx->ins, "could not obtain machine id"); - return -1; - } - - ret = flb_hash_simple(FLB_HASH_SHA256, - (unsigned char *) buf, - s, - sha256_buf, - sizeof(sha256_buf)); - - flb_free(buf); - - if (ret != FLB_CRYPTO_SUCCESS) { - return -1; - } - - /* convert to hex */ - s_buf = sha256_to_hex(sha256_buf); - if (!s_buf) { - return -1; - } - - *out_buf = s_buf; - *out_size = flb_sds_len(s_buf); - - return 0; -} - static void pack_str(msgpack_packer *mp_pck, char *str) { int len; @@ -720,8 +656,6 @@ static struct flb_calyptia *config_init(struct flb_output_instance *ins, { int ret; int flags; - size_t size; - char *machine_id; struct flb_calyptia *ctx; /* Calyptia plugin context */ @@ -768,14 +702,10 @@ static struct flb_calyptia *config_init(struct flb_output_instance *ins, } } - /* If no machine_id has been provided via a configuration option get it from the local machine-id. */ + /* the machine-id is provided by custom calyptia, which invokes this plugin. */ if (!ctx->machine_id) { - /* machine id */ - ret = get_machine_id(ctx, &machine_id, &size); - if (ret == -1) { - return NULL; - } - ctx->machine_id = (flb_sds_t) machine_id; + flb_plg_error(ctx->ins, "machine_id has not been set"); + return NULL; } flb_plg_debug(ctx->ins, "machine_id=%s", ctx->machine_id); @@ -1005,10 +935,6 @@ static int cb_calyptia_exit(void *data, struct flb_config *config) flb_sds_destroy(ctx->agent_token); } - if (ctx->machine_id) { - flb_sds_destroy(ctx->machine_id); - } - if (ctx->env) { flb_env_destroy(ctx->env); }