/src/fluent-bit/plugins/in_http/http_config.c
Line | Count | Source |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* Fluent Bit |
4 | | * ========== |
5 | | * Copyright (C) 2015-2026 The Fluent Bit Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <fluent-bit/flb_input_plugin.h> |
21 | | #include <fluent-bit/flb_oauth2_jwt.h> |
22 | | |
23 | | #include "http.h" |
24 | | #include "http_config.h" |
25 | | |
26 | | struct flb_http *http_config_create(struct flb_input_instance *ins) |
27 | 0 | { |
28 | 0 | char port[8]; |
29 | 0 | int ret; |
30 | 0 | struct flb_http *ctx; |
31 | |
|
32 | 0 | ctx = flb_calloc(1, sizeof(struct flb_http)); |
33 | 0 | if (!ctx) { |
34 | 0 | flb_errno(); |
35 | 0 | return NULL; |
36 | 0 | } |
37 | 0 | ctx->ins = ins; |
38 | |
|
39 | 0 | ctx->oauth2_cfg.jwks_refresh_interval = 300; |
40 | | |
41 | | /* Load the config map */ |
42 | 0 | ret = flb_input_config_map_set(ins, (void *) ctx); |
43 | 0 | if (ret == -1) { |
44 | 0 | flb_free(ctx); |
45 | 0 | return NULL; |
46 | 0 | } |
47 | | |
48 | | /* Apply OAuth2 JWT config map properties if any */ |
49 | 0 | if (ins->oauth2_jwt_config_map && mk_list_size(&ins->oauth2_jwt_properties) > 0) { |
50 | 0 | ret = flb_config_map_set(ins->config, |
51 | 0 | &ins->oauth2_jwt_properties, |
52 | 0 | ins->oauth2_jwt_config_map, |
53 | 0 | &ctx->oauth2_cfg); |
54 | 0 | if (ret == -1) { |
55 | 0 | flb_free(ctx); |
56 | 0 | return NULL; |
57 | 0 | } |
58 | 0 | } |
59 | | |
60 | | /* Listen interface (if not set, defaults to 0.0.0.0:9880) */ |
61 | 0 | flb_input_net_default_listener("0.0.0.0", 9880, ins); |
62 | |
|
63 | 0 | ctx->listen = flb_strdup(ins->host.listen); |
64 | 0 | snprintf(port, sizeof(port) - 1, "%d", ins->host.port); |
65 | 0 | ctx->tcp_port = flb_strdup(port); |
66 | |
|
67 | 0 | ret = flb_log_event_encoder_init(&ctx->log_encoder, |
68 | 0 | FLB_LOG_EVENT_FORMAT_DEFAULT); |
69 | |
|
70 | 0 | if (ret != FLB_EVENT_ENCODER_SUCCESS) { |
71 | 0 | flb_plg_error(ctx->ins, "error initializing event encoder : %d", ret); |
72 | 0 | http_config_destroy(ctx); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | | |
76 | | /* Create record accessor for tag_key if specified */ |
77 | 0 | if (ctx->tag_key) { |
78 | 0 | ctx->ra_tag_key = flb_ra_create(ctx->tag_key, FLB_TRUE); |
79 | 0 | if (!ctx->ra_tag_key) { |
80 | 0 | flb_plg_error(ctx->ins, "invalid record accessor pattern for tag_key: %s", ctx->tag_key); |
81 | 0 | http_config_destroy(ctx); |
82 | 0 | return NULL; |
83 | 0 | } |
84 | 0 | } |
85 | | |
86 | 0 | return ctx; |
87 | 0 | } |
88 | | |
89 | | int http_config_destroy(struct flb_http *ctx) |
90 | 0 | { |
91 | 0 | if (ctx->ra_tag_key) { |
92 | 0 | flb_ra_destroy(ctx->ra_tag_key); |
93 | 0 | } |
94 | |
|
95 | 0 | flb_log_event_encoder_destroy(&ctx->log_encoder); |
96 | 0 | flb_http_server_destroy(&ctx->http_server); |
97 | |
|
98 | 0 | if (ctx->oauth2_ctx) { |
99 | 0 | flb_oauth2_jwt_context_destroy(ctx->oauth2_ctx); |
100 | 0 | ctx->oauth2_ctx = NULL; |
101 | 0 | ctx->oauth2_cfg.issuer = NULL; |
102 | 0 | ctx->oauth2_cfg.jwks_url = NULL; |
103 | 0 | ctx->oauth2_cfg.allowed_audience = NULL; |
104 | 0 | } |
105 | 0 | else { |
106 | 0 | if (ctx->oauth2_cfg.issuer) { |
107 | 0 | flb_sds_destroy(ctx->oauth2_cfg.issuer); |
108 | 0 | } |
109 | |
|
110 | 0 | if (ctx->oauth2_cfg.jwks_url) { |
111 | 0 | flb_sds_destroy(ctx->oauth2_cfg.jwks_url); |
112 | 0 | } |
113 | |
|
114 | 0 | if (ctx->oauth2_cfg.allowed_audience) { |
115 | 0 | flb_sds_destroy(ctx->oauth2_cfg.allowed_audience); |
116 | 0 | } |
117 | 0 | } |
118 | | |
119 | |
|
120 | 0 | flb_free(ctx->listen); |
121 | 0 | flb_free(ctx->tcp_port); |
122 | 0 | flb_free(ctx); |
123 | 0 | return 0; |
124 | 0 | } |