Skip to content

Commit

Permalink
refactor upstream creation
Browse files Browse the repository at this point in the history
Signed-off-by: adiforluls <[email protected]>
  • Loading branch information
adiforluls committed Sep 26, 2023
1 parent a215630 commit 21da7e2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 67 deletions.
10 changes: 8 additions & 2 deletions plugins/out_oracle_log_analytics/oci_logan.c
Original file line number Diff line number Diff line change
Expand Up @@ -1064,19 +1064,25 @@ static void cb_oci_logan_flush(struct flb_event_chunk *event_chunk,
{
struct flb_oci_logan *ctx = out_context;
int ret = -1;
flb_sds_t host = NULL;

if (strcasecmp(ctx->auth_type, INSTANCE_PRINCIPAL) == 0) {
ret = refresh_security_token(ctx, config);
if (ret != 0) {
flb_errno();
// flb_oci_logan_conf_destroy(ctx);
FLB_OUTPUT_RETURN(FLB_RETRY);
}
ctx->private_key = ctx->fed_client->private_key;
ctx->region = ctx->fed_client->region;
flb_sds_snprintf(&ctx->key_id, flb_sds_alloc(ctx->key_id),
"ST$%s", ctx->fed_client->security_token);
flb_plg_info(ctx->ins, "key_id = %s", ctx->key_id);
ret = set_upstream_ctx(ctx, ctx->ins, config);
if (ret != 0) {
flb_errno();
flb_plg_error(ctx->ins, "cannot create Upstream context");
FLB_OUTPUT_RETURN(FLB_ERROR);
}
flb_plg_debug(ctx->ins, "key_id = %s", ctx->key_id);
}
if (strcasecmp(ctx->auth_type, WORKLOAD_IDENTITY) == 0) {
ret = refresh_oke_workload_security_token(ctx, config);
Expand Down
2 changes: 1 addition & 1 deletion plugins/out_oracle_log_analytics/oci_logan.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@

#define INSTANCE_PRINCIPAL "instance_principal"
#define USER_PRINCIPAL "user_principal"
#define WORKLOAD_IDENTITY "workload_identity"
#define WORKLOAD_IDENTITY "oke_workload_identity"

#define FLB_OKE_DEFAULT_SA_CERT_PATH "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
#define FLB_OKE_TOKEN_PATH "/var/run/secrets/kubernetes.io/serviceaccount/token"
Expand Down
151 changes: 87 additions & 64 deletions plugins/out_oracle_log_analytics/oci_logan_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,88 @@ static int log_event_metadata_create(struct flb_oci_logan *ctx)

return 0;
}

int set_upstream_ctx(struct flb_oci_logan *ctx,
struct flb_output_instance *ins,
struct flb_config *config)
{
struct flb_upstream *upstream;
flb_sds_t host = NULL;
int io_flags = 0, default_port;
const char *tmp;
int ret = 0;
char *protocol = NULL;
char *p_host = NULL;
char *p_port = NULL;
char *p_uri = NULL;

if (ins->host.name) {
host = ins->host.name;
}
else {
if (!ctx->region ) {
flb_errno();
flb_plg_error(ctx->ins, "Region is required");
return -1;
}
host = flb_sds_create_size(512);
flb_sds_snprintf(&host, flb_sds_alloc(host), "loganalytics.%s.oci.oraclecloud.com", ctx->region);
}

io_flags = FLB_IO_TCP;
default_port = 80;

#ifdef FLB_HAVE_TLS
if (ins->use_tls == FLB_TRUE) {
io_flags = FLB_IO_TLS;
default_port = 443;
}
#endif

if (ins->host.ipv6 == FLB_TRUE) {
io_flags |= FLB_IO_IPV6;
}

flb_output_net_default(host, default_port, ins);
flb_sds_destroy(host);

if (ctx->proxy) {
ret = flb_utils_url_split(tmp, &protocol, &p_host, &p_port, &p_uri);
if (ret == -1) {
flb_plg_error(ctx->ins, "could not parse proxy parameter: '%s'", tmp);
return -1;
}

ctx->proxy_host = p_host;
ctx->proxy_port = atoi(p_port);
flb_free(protocol);
flb_free(p_port);
flb_free(p_uri);
flb_free(p_host);
}

if (ctx->proxy) {
upstream = flb_upstream_create(config, ctx->proxy_host, ctx->proxy_port,
io_flags, ins->tls);
}
else {
/* Prepare an upstream handler */
upstream = flb_upstream_create(config, ins->host.name, ins->host.port,
io_flags, ins->tls);
}

if (!upstream) {
flb_plg_error(ctx->ins, "cannot create Upstream context");
return -1;
}
ctx->u = upstream;

/* Set instance flags into upstream */
flb_output_upstream_set(ctx->u, ins);

return 0;
}

struct flb_oci_logan *flb_oci_logan_conf_create(struct flb_output_instance *ins,
struct flb_config *config) {
struct flb_oci_logan *ctx;
Expand Down Expand Up @@ -987,20 +1069,6 @@ struct flb_oci_logan *flb_oci_logan_conf_create(struct flb_output_instance *ins,
}
}

if (ins->host.name) {
host = ins->host.name;
}
else {
if (!ctx->region) {
flb_errno();
flb_plg_error(ctx->ins, "Region is required");
flb_oci_logan_conf_destroy(ctx);
return NULL;
}
host = flb_sds_create_size(512);
flb_sds_snprintf(&host, flb_sds_alloc(host), "loganalytics.%s.oci.oraclecloud.com", ctx->region);
}

if (!ctx->uri) {
if (!ctx->namespace) {
flb_errno();
Expand Down Expand Up @@ -1028,61 +1096,16 @@ struct flb_oci_logan *flb_oci_logan_conf_create(struct flb_output_instance *ins,
"%s/%s/%s", ctx->tenancy, ctx->user, ctx->key_fingerprint);
}


/* Check if SSL/TLS is enabled */
io_flags = FLB_IO_TCP;
default_port = 80;

#ifdef FLB_HAVE_TLS
if (ins->use_tls == FLB_TRUE) {
io_flags = FLB_IO_TLS;
default_port = 443;
}
#endif

if (ins->host.ipv6 == FLB_TRUE) {
io_flags |= FLB_IO_IPV6;
}

flb_output_net_default(host, default_port, ins);
flb_sds_destroy(host);

if (ctx->proxy) {
ret = flb_utils_url_split(tmp, &protocol, &p_host, &p_port, &p_uri);
if (ret == -1) {
flb_plg_error(ctx->ins, "could not parse proxy parameter: '%s'", tmp);
if (strcasecmp(ctx->auth_type, USER_PRINCIPAL) == 0 ||
strcasecmp(ctx->auth_type, WORKLOAD_IDENTITY) == 0) {
ret = set_upstream_ctx(ctx, ins, config);
if (ret != 0) {
flb_plg_error(ctx->ins, "cannot create Upstream context");
flb_oci_logan_conf_destroy(ctx);
return NULL;
}

ctx->proxy_host = p_host;
ctx->proxy_port = atoi(p_port);
flb_free(protocol);
flb_free(p_port);
flb_free(p_uri);
flb_free(p_host);
}

if (ctx->proxy) {
upstream = flb_upstream_create(config, ctx->proxy_host, ctx->proxy_port,
io_flags, ins->tls);
}
else {
/* Prepare an upstream handler */
upstream = flb_upstream_create(config, ins->host.name, ins->host.port,
io_flags, ins->tls);
}

if (!upstream) {
flb_plg_error(ctx->ins, "cannot create Upstream context");
flb_oci_logan_conf_destroy(ctx);
return NULL;
}
ctx->u = upstream;

/* Set instance flags into upstream */
flb_output_upstream_set(ctx->u, ins);

return ctx;
}

Expand Down
3 changes: 3 additions & 0 deletions plugins/out_oracle_log_analytics/oci_logan_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ int refresh_security_token(struct flb_oci_logan *ctx,
struct flb_config *config);
int refresh_oke_workload_security_token(struct flb_oci_logan *ctx,
struct flb_config *config);
int set_upstream_ctx(struct flb_oci_logan *ctx,
struct flb_output_instance *ins,
struct flb_config *config);

#endif

0 comments on commit 21da7e2

Please sign in to comment.