Skip to content

Retire API keys for ES output when user switches to non-ES output - #8382

Open
khushijain21 wants to merge 5 commits into
elastic:mainfrom
khushijain21:invalidate-api-keys
Open

khushijain21 wants to merge 5 commits into
elastic:mainfrom
khushijain21:invalidate-api-keys

Conversation

@khushijain21

@khushijain21 khushijain21 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What is the problem this PR solves?

When a user switched from ES output to logstash/kafka, we never retire the api keys of ES output. In this PR, we fix that.

How does this PR solve the problem?

// Explain HOW you solved the problem in your code. It is possible that during PR reviews this changes and then this section should be updated.

This PR retires and deletes all stored api keys if the new policy does not contain ES output.
The case where a user simply switches to another ES output, the api keys are already retired correctly on main.

How to test this PR locally

Design Checklist

  • I have ensured my design is stateless and will work when multiple fleet-server instances are behind a load balancer.
  • I have or intend to scale test my changes, ensuring it will work reliably with 100K+ agents connected.
  • I have included fail safe mechanisms to limit the load on fleet-server: rate limiting, circuit breakers, caching, load shedding, etc.

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in ./changelog/fragments using the changelog tool

Related issues

Copilot AI balanced review requested due to automatic review settings November 26, 2026 04:30
@mergify

mergify Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

This pull request does not have a backport label. Could you fix it @khushijain21? 🙏
To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-./d./d is the label to automatically backport to the 8./d branch. /d is the digit
  • backport-active-all is the label that automatically backports to all active branches.
  • backport-active-8 is the label that automatically backports to all active minor branches for the 8 major.
  • backport-active-9 is the label that automatically backports to all active minor branches for the 9 major.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new test cannot compile, and credential retirement can prematurely stop ingestion or discard records needed to retry invalidation of still-valid remote keys.

4 open issues
What changed in this PR

This PR extends Fleet Server’s output credential cleanup to retire Elasticsearch API keys when agents switch to non-ES outputs such as Logstash or Kafka.

Changes:

  • Adds cleanup when no API-key-bearing output remains and handles output-type changes when another keyed output survives.
  • Shares key invalidation and secret cleanup helpers between policy processing and acknowledgement handlers.
  • Adds regression tests and a bug-fix changelog entry.
File Description
internal/​pkg/​policy/​retire_outputs.go Adds retirement logic and shared credential cleanup helpers.
internal/​pkg/​policy/​policy_output.go Handles outputs changing to types without API keys.
internal/​pkg/​policy/​policy_output_test.go Tests retirement and output-type transitions.
internal/​pkg/​api/​handleCheckin.go Invokes retirement during policy preparation.
internal/​pkg/​api/​handleCheckin_test.go Tests retirement records on a surviving output.
internal/​pkg/​api/​handleAck.go Uses shared cleanup helpers and renames the policy-ack handler.
internal/​pkg/​api/​handleAck_test.go Updates tests to call shared secret cleanup.
changelog/​fragments/​1795667311-retire-output-api-keys-when-an-output-changes-to-logstash-or-kafka.yaml Documents the credential retirement fix.

Review effort: Balanced

Comment thread internal/pkg/api/handleCheckin_test.go Resolved
Comment thread internal/pkg/policy/retire_outputs.go Resolved
Comment thread internal/pkg/policy/retire_outputs.go Resolved
Comment thread internal/pkg/policy/retire_outputs.go Resolved
@khushijain21 khushijain21 changed the title Retire API keys of ES output when user switch to non-ES output Retire API keys for ES output when user switches to non-ES output Nov 26, 2026
Copilot AI balanced review requested due to automatic review settings November 26, 2026 06:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical issue leaves the old remote Elasticsearch API key valid after an output-type change when the remote-client cache is empty, because invalidation targets the primary cluster instead of the original remote cluster.

1 open issue
4 resolved since last review

Review effort: Balanced

Comment on lines +328 to +332
retiring := model.ToRetireAPIKeyIdsItems{
ID: prev.APIKeyID,
RetiredAt: time.Now().UTC().Format(time.RFC3339),
Output: p.Name,
OutputType: prev.Type,
Copilot AI balanced review requested due to automatic review settings November 26, 2026 06:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Key retirement can leave remote Elasticsearch credentials valid after a restart or invalidate still-live keys on a delayed acknowledgement because retirement records lack a durable reference to the original remote configuration and the target policy revision.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

High severity Gate key retirement on confirmation of the target policy revision

internal/​pkg/​policy/​policy_output.go:332

The retirement record is not tied to the policy revision that removes this key. If the stored revision is 1 and revision 3 switches to Logstash, a delayed acknowledgement of revision 2 (still using Elasticsearch) passes the acknowledgement gate and invalidates the still-live key, interrupting ingestion. Persist the target policy ID/revision with the retirement record and make both acknowledgement and check-in cleanup wait until that transition is confirmed.

Medium severity Test cleanup of keyless outputs through both cleanup paths

internal/​pkg/​policy/​policy_output.go:120

The new tests verify that preparation parks keys, but do not exercise acknowledgement or check-in cleanup for the resulting keyless Logstash/Kafka entries. A regression that skips those entries during cleanup would leave the key leak undetected. Add regression coverage using reloaded agent state to verify that keys remain valid before the output switch is confirmed, then are invalidated and their stored secrets deleted through both cleanup paths.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 26, 2026 07:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

An in-place remote Elasticsearch-to-Logstash/Kafka switch loses the original cluster routing information, so key invalidation after a restart or on another instance targets the primary cluster and leaves the old remote key valid.

1 open issue

Review effort: Balanced

@khushijain21
khushijain21 marked this pull request as ready for review November 26, 2026 09:14
@khushijain21
khushijain21 requested a review from a team as a code owner November 26, 2026 09:14
Copilot AI balanced review requested due to automatic review settings November 26, 2026 09:15
@mergify

mergify Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Retiring keys after a same-name remote Elasticsearch output switches to Logstash or Kafka can fall back to the primary cluster without a cached remote client, leaving the old remote API key valid.

1 open issue

Review effort: Balanced

@samuelvl
samuelvl self-requested a review November 26, 2026 09:32

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants