Skip to content
Open
58 changes: 58 additions & 0 deletions include/fluent-bit/flb_plugin_alias.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/* Fluent Bit
* ==========
* Copyright (C) 2015-2026 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef FLB_PLUGIN_ALIAS_H
#define FLB_PLUGIN_ALIAS_H

#include <stddef.h>

/*
* Returned by flb_plugin_alias_rewrite() when an alias exists but an internal
* error prevents generating a rewritten string.
*/
#define FLB_PLUGIN_ALIAS_ERR ((char *) -1)

struct flb_plugin_alias_entry {
int plugin_type;
const char *alias_name;
const char *plugin_name;
};

/*
* Returns the canonical plugin name for alias_name when a mapping exists,
* otherwise returns NULL.
*/
const char *flb_plugin_alias_get(int plugin_type, const char *alias_name,
size_t alias_name_length);

/*
* Rewrites plugin_reference when it starts with a known alias.
*
* Return values:
* - NULL: no rewrite needed
* - FLB_PLUGIN_ALIAS_ERR: rewrite needed but failed
* - allocated string: rewritten plugin reference (caller must free)
*/
char *flb_plugin_alias_rewrite(int plugin_type, const char *plugin_reference);
Comment thread
coderabbitai[bot] marked this conversation as resolved.

void flb_plugin_alias_set_custom_entries(
const struct flb_plugin_alias_entry *entries);
void flb_plugin_alias_reset_custom_entries(void);

#endif
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ set(src
flb_crypto.c
flb_random.c
flb_plugin.c
flb_plugin_alias.c
flb_gzip.c
flb_snappy.c
flb_zstd.c
Expand Down
21 changes: 20 additions & 1 deletion src/flb_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_metrics.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_plugin_alias.h>
#include <chunkio/chunkio.h>

#ifdef FLB_HAVE_CHUNK_TRACE
Expand Down Expand Up @@ -435,6 +437,8 @@ struct flb_filter_instance *flb_filter_new(struct flb_config *config,
const char *filter, void *data)
{
int id;
char *filter_name;
const char *effective_filter_name;
struct mk_list *head;
struct flb_filter_plugin *plugin;
struct flb_filter_instance *instance = NULL;
Expand All @@ -443,21 +447,35 @@ struct flb_filter_instance *flb_filter_new(struct flb_config *config,
return NULL;
}

filter_name = flb_plugin_alias_rewrite(FLB_PLUGIN_FILTER, filter);
if (filter_name == FLB_PLUGIN_ALIAS_ERR) {
return NULL;
}

if (filter_name != NULL) {
effective_filter_name = filter_name;
}
else {
effective_filter_name = filter;
}

mk_list_foreach(head, &config->filter_plugins) {
plugin = mk_list_entry(head, struct flb_filter_plugin, _head);
if (strcasecmp(plugin->name, filter) == 0) {
if (strcasecmp(plugin->name, effective_filter_name) == 0) {
break;
}
plugin = NULL;
}

if (!plugin) {
flb_free(filter_name);
return NULL;
}

instance = flb_calloc(1, sizeof(struct flb_filter_instance));
if (!instance) {
flb_errno();
flb_free(filter_name);
return NULL;
}
instance->config = config;
Expand Down Expand Up @@ -493,6 +511,7 @@ struct flb_filter_instance *flb_filter_new(struct flb_config *config,

mk_list_init(&instance->properties);
mk_list_add(&instance->_head, &config->filters);
flb_free(filter_name);

return instance;
}
Expand Down
19 changes: 18 additions & 1 deletion src/flb_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <fluent-bit/flb_ring_buffer.h>
#include <fluent-bit/flb_processor.h>
#include <fluent-bit/flb_oauth2_jwt.h>
#include <fluent-bit/flb_plugin_alias.h>

/* input plugin macro helpers */
#include <fluent-bit/flb_input_plugin.h>
Expand Down Expand Up @@ -275,6 +276,8 @@ struct flb_input_instance *flb_input_new(struct flb_config *config,
int id;
int ret;
int flags = 0;
char *input_uri;
const char *input_name;
struct mk_list *head;
struct flb_input_plugin *plugin;
struct flb_input_instance *instance = NULL;
Expand All @@ -289,9 +292,21 @@ struct flb_input_instance *flb_input_new(struct flb_config *config,
return NULL;
}

input_uri = flb_plugin_alias_rewrite(FLB_PLUGIN_INPUT, input);
if (input_uri == FLB_PLUGIN_ALIAS_ERR) {
return NULL;
}

if (input_uri != NULL) {
input_name = input_uri;
}
else {
input_name = input;
}

mk_list_foreach(head, &config->in_plugins) {
plugin = mk_list_entry(head, struct flb_input_plugin, _head);
if (!check_protocol(plugin->name, input)) {
if (!check_protocol(plugin->name, input_name)) {
plugin = NULL;
continue;
}
Expand All @@ -301,6 +316,7 @@ struct flb_input_instance *flb_input_new(struct flb_config *config,
* requirement.
*/
if (public_only == FLB_TRUE && plugin->flags & FLB_INPUT_PRIVATE) {
flb_free(input_uri);
return NULL;
}

Expand Down Expand Up @@ -578,6 +594,7 @@ struct flb_input_instance *flb_input_new(struct flb_config *config,
instance->test_formatter.callback = plugin->test_formatter.callback;
}

flb_free(input_uri);
return instance;
}

Expand Down
12 changes: 11 additions & 1 deletion src/flb_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ int flb_net_host_set(const char *plugin_name, struct flb_net_host *host, const c
int len;
int olen;
const char *s, *e, *u;
const char *separator;

memset(host, '\0', sizeof(struct flb_net_host));

Expand All @@ -174,7 +175,16 @@ int flb_net_host_set(const char *plugin_name, struct flb_net_host *host, const c
return -1;
}

s = address + len;
separator = strchr(address, ':');
if (separator != NULL &&
separator != address &&
separator[1] == '/' &&
separator[2] == '/') {
s = separator + 3;
}
else {
s = address + len;
}
if (*s == '[') {
/* IPv6 address (RFC 3986) */
e = strchr(++s, ']');
Expand Down
58 changes: 51 additions & 7 deletions src/flb_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_plugin.h>
#include <fluent-bit/flb_plugin_alias.h>
#include <fluent-bit/flb_plugin_proxy.h>
#include <fluent-bit/flb_http_client_debug.h>
#include <fluent-bit/flb_output_thread.h>
Expand Down Expand Up @@ -128,6 +129,7 @@ static int check_protocol(const char *prot, const char *output)
{
int len;
char *p;
const char *alias_target;

p = strstr(output, "://");
if (p && p != output) {
Expand All @@ -137,12 +139,16 @@ static int check_protocol(const char *prot, const char *output)
len = strlen(output);
}

if (strlen(prot) != len) {
return 0;
/* Output plugin match */
if (strlen(prot) == (size_t) len &&
strncasecmp(prot, output, len) == 0) {
return 1;
}

/* Output plugin match */
if (strncasecmp(prot, output, len) == 0) {
alias_target = flb_plugin_alias_get(FLB_PLUGIN_OUTPUT, output, len);
if (alias_target != NULL &&
strlen(alias_target) == strlen(prot) &&
strcasecmp(prot, alias_target) == 0) {
return 1;
}

Expand Down Expand Up @@ -676,6 +682,8 @@ struct flb_output_instance *flb_output_new(struct flb_config *config,
{
int ret = -1;
int flags = 0;
const char *output_name;
char *output_uri;
struct mk_list *head;
struct flb_output_plugin *plugin;
struct flb_output_instance *instance = NULL;
Expand All @@ -684,9 +692,12 @@ struct flb_output_instance *flb_output_new(struct flb_config *config,
return NULL;
}

output_name = output;
output_uri = NULL;

mk_list_foreach(head, &config->out_plugins) {
plugin = mk_list_entry(head, struct flb_output_plugin, _head);
if (!check_protocol(plugin->name, output)) {
if (!check_protocol(plugin->name, output_name)) {
plugin = NULL;
continue;
}
Expand Down Expand Up @@ -818,12 +829,45 @@ struct flb_output_instance *flb_output_new(struct flb_config *config,
# endif
#endif

output_uri = flb_plugin_alias_rewrite(FLB_PLUGIN_OUTPUT, output_name);
if (output_uri == FLB_PLUGIN_ALIAS_ERR) {
if ((instance->flags & FLB_OUTPUT_SYNCHRONOUS) &&
instance->singleplex_queue != NULL) {
flb_task_queue_destroy(instance->singleplex_queue);
}
if (instance->callback != NULL) {
flb_callback_destroy(instance->callback);
}
if (plugin->type != FLB_OUTPUT_PLUGIN_CORE &&
instance->context != NULL) {
flb_free(instance->context);
}
flb_free(instance->http_server_config);
flb_free(instance);
return NULL;
}
else if (output_uri != NULL) {
output_name = output_uri;
}

if (plugin->flags & FLB_OUTPUT_NET) {
ret = flb_net_host_set(plugin->name, &instance->host, output);
ret = flb_net_host_set(plugin->name, &instance->host, output_name);
if (output_uri != NULL) {
flb_free(output_uri);
}

if (ret != 0) {
if (instance->flags & FLB_OUTPUT_SYNCHRONOUS) {
if ((instance->flags & FLB_OUTPUT_SYNCHRONOUS) &&
instance->singleplex_queue != NULL) {
flb_task_queue_destroy(instance->singleplex_queue);
}
if (instance->callback != NULL) {
flb_callback_destroy(instance->callback);
}
if (plugin->type != FLB_OUTPUT_PLUGIN_CORE &&
instance->context != NULL) {
flb_free(instance->context);
}
flb_free(instance->http_server_config);
flb_free(instance);
return NULL;
Expand Down
Loading
Loading