Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/configuration/oauth2-audience.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Upgrading OAuth2 JWT validation on HTTP and OpenTelemetry inputs

Before this change, an input could start with the following OAuth2 settings
and accept a token issued for another service.

```yaml
oauth2.validate: true
oauth2.issuer: https://issuer.example
oauth2.jwks_url: https://issuer.example/keys
```

After upgrading, that configuration fails at startup. Add the audience your
issuer assigns to this input. For example, if the identifier is
`telemetry-ingest`, add this setting to the same input.

```yaml
oauth2.allowed_audience: telemetry-ingest
```

The token's `aud` string or one of its array entries must match this value.
To explicitly retain the previous behavior, set `oauth2.allow_any_audience: true`
and omit `oauth2.allowed_audience`. This logs a startup warning and permits
tokens intended for other services if they pass the remaining JWT checks.
1 change: 1 addition & 0 deletions include/fluent-bit/flb_oauth2_jwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct flb_oauth2_jwt_cfg {
flb_sds_t issuer; /* expected issuer */
flb_sds_t jwks_url; /* JWKS endpoint */
flb_sds_t allowed_audience; /* audience claim to enforce */
int allow_any_audience; /* explicitly skip audience matching */
struct mk_list *allowed_clients; /* list of authorized azp/client_id/appid */
int jwks_refresh_interval; /* refresh cadence in seconds */
size_t jwks_buffer_max_size; /* maximum JWKS HTTP response size */
Expand Down
18 changes: 0 additions & 18 deletions plugins/in_http/http_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,7 @@ int http_config_destroy(struct flb_http *ctx)

if (ctx->oauth2_ctx) {
flb_oauth2_jwt_context_destroy(ctx->oauth2_ctx);
ctx->oauth2_ctx = NULL;
ctx->oauth2_cfg.issuer = NULL;
ctx->oauth2_cfg.jwks_url = NULL;
ctx->oauth2_cfg.allowed_audience = NULL;
}
else {
if (ctx->oauth2_cfg.issuer) {
flb_sds_destroy(ctx->oauth2_cfg.issuer);
}

if (ctx->oauth2_cfg.jwks_url) {
flb_sds_destroy(ctx->oauth2_cfg.jwks_url);
}

if (ctx->oauth2_cfg.allowed_audience) {
flb_sds_destroy(ctx->oauth2_cfg.allowed_audience);
}
}


flb_free(ctx->listen);
flb_free(ctx->tcp_port);
Expand Down
28 changes: 26 additions & 2 deletions src/flb_oauth2_jwt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,25 @@ struct flb_oauth2_jwt_ctx *flb_oauth2_jwt_context_create(struct flb_config *conf
ctx->cfg.jwks_response_timeout = OAUTH2_JWKS_TIMEOUT_DEFAULT;
}

if (ctx->cfg.validate) {
if (ctx->cfg.allow_any_audience && ctx->cfg.allowed_audience) {
flb_error("[oauth2_jwt] configure only one of oauth2.allowed_audience "
"and oauth2.allow_any_audience");
flb_free(ctx);
return NULL;
}

if (ctx->cfg.allow_any_audience) {
flb_warn("[oauth2_jwt] oauth2.allow_any_audience is enabled; "
"tokens for other audiences may be accepted");
}
else if (!ctx->cfg.allowed_audience || ctx->cfg.allowed_audience[0] == '\0') {
flb_error("[oauth2_jwt] oauth2.validate requires a non-empty oauth2.allowed_audience");
flb_free(ctx);
return NULL;
}
}

if (ctx->cfg.jwks_buffer_max_size < FLB_HTTP_DATA_SIZE_MAX ||
ctx->cfg.jwks_buffer_max_size > OAUTH2_JWKS_BUFFER_LIMIT_SIZE) {
flb_error("[oauth2_jwt] oauth2.jwks_buffer_max_size must be between "
Expand Down Expand Up @@ -1507,7 +1526,7 @@ int flb_oauth2_jwt_validate(struct flb_oauth2_jwt_ctx *ctx,
}

/* Check audience */
if (ctx->cfg.allowed_audience) {
if (!ctx->cfg.allow_any_audience) {
/* Check audience by re-parsing the payload to handle both string and array formats.
* Per JWT spec (RFC 7519), when aud is an array, the token is valid if ANY element
* in the array matches the expected audience. */
Expand Down Expand Up @@ -1582,7 +1601,12 @@ static struct flb_config_map oauth2_jwt_config_map[] = {
{
FLB_CONFIG_MAP_STR, "oauth2.allowed_audience", NULL,
0, FLB_TRUE, offsetof(struct flb_oauth2_jwt_cfg, allowed_audience),
"Audience claim to enforce for OAuth2 JWT validation"
"Expected JWT audience, required when validation is enabled unless allow_any_audience is set"
},
{
FLB_CONFIG_MAP_BOOL, "oauth2.allow_any_audience", "false",
0, FLB_TRUE, offsetof(struct flb_oauth2_jwt_cfg, allow_any_audience),
"Accept any JWT audience instead of requiring a match with oauth2.allowed_audience"
},
{
FLB_CONFIG_MAP_SLIST_1, "oauth2.allowed_clients", NULL,
Expand Down
52 changes: 52 additions & 0 deletions tests/integration/scenarios/in_http/tests/test_in_http_001.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@

import pytest
import requests
import yaml
from flask import Response

from server.http_server import MOCK_JWKS_BODY, app, data_storage, http_server_run
from utils.fluent_bit_manager import FluentBitStartupError
from utils.http_matrix import PROTOCOL_CASES, run_curl_request
from utils.oauth2 import duplicate_header_token, duplicate_jwks_body, track_jwks_requests
from utils.input_pause_resume import (
Expand Down Expand Up @@ -696,6 +698,56 @@ def test_in_http_health_endpoint(case):
assert payload["timestamp"].endswith("Z")


@pytest.mark.parametrize("plugin", ["http", "opentelemetry"])
@pytest.mark.parametrize("policy,error", [
({}, "requires a non-empty oauth2.allowed_audience"),
({"oauth2.allowed_audience": ""}, "oauth2.allowed_audience"),
({"oauth2.allowed_audience": "audience", "oauth2.allow_any_audience": True},
"configure only one of oauth2.allowed_audience and oauth2.allow_any_audience"),
])
def test_oauth2_audience_startup(tmp_path, plugin, policy, error):
config = {
"pipeline": {
"inputs": [{
"name": plugin, "oauth2.validate": True, "oauth2.issuer": "issuer",
"oauth2.jwks_url": "http://127.0.0.1/keys", **policy,
}],
"outputs": [{"name": "null", "match": "*"}],
},
}
path = tmp_path / "oauth.yaml"
path.write_text(yaml.safe_dump(config))
service = FluentBitTestService(str(path))
with pytest.raises(FluentBitStartupError, match=r"exited early with code [1-9][0-9]*\."):
service.start()
assert error in Path(service.flb.log_file).read_text()


@pytest.mark.parametrize("policy,expected_status", [
({"oauth2.allowed_audience": "different-service"}, 401),
({"oauth2.allow_any_audience": True}, 201),
])
def test_in_http_oauth2_audience_policy(tmp_path, policy, expected_status):
source = Path(__file__).resolve().parents[1] / "config" / "in_http_oauth2.yaml"
config = yaml.safe_load(source.read_text())
input_config = config["pipeline"]["inputs"][0]
del input_config["oauth2.allowed_audience"]
input_config.update(policy)
path = tmp_path / "oauth.yaml"
path.write_text(yaml.safe_dump(config))
service = Service(str(path))
service.start()
try:
response = requests.post(f"http://127.0.0.1:{service.flb_listener_port}/",
json={"message": "audience test"},
headers={"Authorization": f"Bearer {MOCK_VALID_JWT}"}, timeout=10)
assert response.status_code == expected_status
finally:
service.stop()
if policy.get("oauth2.allow_any_audience"):
assert "tokens for other audiences may be accepted" in Path(service.flb.log_file).read_text()


def test_in_http_oauth2_requires_bearer_token():
service = Service("in_http_oauth2.yaml")
service.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def serve():
"inputs": [{
"name": plugin, "port": "${FLUENT_BIT_TEST_LISTENER_PORT}",
"oauth2.validate": True, "oauth2.issuer": "issuer",
"oauth2.allowed_audience": "audience",
"oauth2.jwks_url": (
f"{'https' if https else 'http'}://127.0.0.1:{listener.getsockname()[1]}/keys"
),
Expand Down
Loading