Skip to content

x-pack/filebeat/input/cel: default User-Agent broken with OAuth2 before 9.3.0 #50867

@chrisberkhout

Description

@chrisberkhout

Bug report

  • Version: < 9.3.0
  • Component: Filebeat CEL input

Summary

The Filebeat CEL input does not apply the documented default User-Agent header to CEL-initiated HTTP requests when auth.oauth2 is enabled before 9.3.0.

The default CEL request user-agent behavior was added in #39587 for 8.15.0. The CEL input documentation says:

By default, this value is assigned to all requests' user-agent headers unless the CEL program has already set the user-agent header value.

However, with auth.oauth2 enabled in Filebeat 8.19.0, for example, requests made from the CEL program are sent with Go's default user-agent:

Go-http-client/1.1

instead of the Filebeat user-agent exposed through the CEL useragent global.

Affected versions

I observed this in 8.19.0.

From code inspection, the issue is still present in v8.19.15 and v9.2.8.

It appears fixed on main and in v9.3.0, coincidentally, via #47014.

Steps to reproduce

  1. Configure a Filebeat CEL input using auth.oauth2.
filebeat.inputs:
  - type: cel
    interval: 1m
    resource.url: https://example.invalid
    auth.oauth2:
      client.id: test-client
      client.secret: test-secret
      token_url: https://example.invalid/oauth/token
      endpoint_params:
        grant_type: client_credentials
    program: |
      post_request(
        state.url + "/graphql",
        "application/json",
        {"query": "{}"}.encode_json()
      ).do_request().as(resp, {
        "events": []
      })
  1. Observe the HTTP request sent by the CEL program, for example with request tracing, a test server, or upstream API logs.

  2. Check the request's User-Agent header.

Actual behavior

The CEL request has:

User-Agent: Go-http-client/1.1

Expected behavior

The CEL request should have Filebeat's default user-agent, equivalent to the CEL useragent global, unless the CEL program explicitly sets a User-Agent header.

For example:

User-Agent: Elastic-Filebeat/<version> (...)

Code analysis

In v8.19.0, x-pack/filebeat/input/cel/input.go returns early when OAuth2 is enabled:

if cfg.Auth.OAuth2.isEnabled() {
    authClient, err := cfg.Auth.OAuth2.client(ctx, c)
    if err != nil {
        return nil, nil, err
    }
    return authClient, trace, nil
}

c.Transport = userAgentDecorator{
    UserAgent: userAgent,
    Transport: c.Transport,
}

This means non-OAuth2 clients get the default user-agent wrapper, but OAuth2 clients skip it.

On main, this has changed to:

if cfg.Auth.OAuth2.isEnabled() {
    c, err = cfg.Auth.OAuth2.client(ctx, c)
    if err != nil {
        return nil, nil, nil, nil, err
    }
}

c.Transport = userAgentDecorator{
    UserAgent: userAgent,
    Transport: c.Transport,
}

So the issue appears to be fixed on main but not backported to 8.17.x, 8.18.x, 8.19.x or 9.0.x, 9.1.x, 9.2.x.

Workaround

CEL programs can explicitly set the header:

post_request(
  state.url + "/graphql",
  "application/json",
  body
).with({
  "Header": {
    "Content-Type": ["application/json"],
    "User-Agent": [useragent],
  },
}).do_request()

Metadata

Metadata

Assignees

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions