Skip to content

Commit

Permalink
custom_calyptia: generate the default machine_id in custom_calyptia.
Browse files Browse the repository at this point in the history
Generate the default machine_id inside the custom_calyptia plugin instead of in
the out_calyptia plugin. This same machine_id then gets set for the
out_calyptia and in_calyptia_fleet plugin when relevant.

Signed-off-by: Phillip Whelan <[email protected]>
  • Loading branch information
pwhelan authored and edsiper committed Sep 4, 2023
1 parent f7731f3 commit 305aa9e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 87 deletions.
74 changes: 74 additions & 0 deletions plugins/custom_calyptia/calyptia.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <fluent-bit/flb_filter.h>
#include <fluent-bit/flb_output.h>

#include <fluent-bit/flb_hash.h>

struct calyptia {
/* config map options */
flb_sds_t api_key;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
38 changes: 28 additions & 10 deletions plugins/in_calyptia_fleet/in_calyptia_fleet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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"
Expand All @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
80 changes: 3 additions & 77 deletions plugins/out_calyptia/calyptia.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_upstream.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_hash.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_version.h>
#include <fluent-bit/flb_metrics.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 305aa9e

Please sign in to comment.