Skip to content

Conversation

@ymao1
Copy link
Contributor

@ymao1 ymao1 commented Dec 1, 2025

Resolves #243719

Summary

This PR switches to marking API keys used by tasks as ready for invalidation instead of immediately invalidating them. This is done by:

  • Creating a new saved object of type api_key_to_invalidate that contains the apiKeyId of the API key to invalidate (does not contain the API key itself)
  • Creating a new recurring background task that queries for api_key_to_invalidate saved objects. When it finds an API key ID, it queries for any existing tasks that may still be using it and does not invalidate if there are any tasks that need it.

There is an alerting task that does the same things for API keys created by alerting rules. In the future, we will move alerting rule API key management to task manager and we can remove the duplicate task.

Verify

@github-actions
Copy link
Contributor

github-actions bot commented Dec 1, 2025

🔍 Preview links for changed docs

@ymao1
Copy link
Contributor Author

ymao1 commented Dec 2, 2025

@elasticmachine run docs-build

@ymao1 ymao1 changed the title wip [Response Ops][Task Manager] Wait to invalidate API keys until no tasks are using it Dec 2, 2025
const PAGE_SIZE = 100;
export const TASK_ID = `Alerts-${TASK_TYPE}`;

const invalidateAPIKeys = async (
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved all of these functions to the task manager plugin so they can be reused by both tasks.

includedHiddenTypes: ['task'],
})
);
plugin.taskManager.registerApiKeyInvalidateFn(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Due to circular dependency issues, we cannot have the task manager plugin depend on the security plugin, so we have to use this sidecar plugin in order to use security functions within task manager.

event_loop_delay: eventLoopDelaySchema,
invalidate_api_key_task: schema.object({
interval: schema.string({ validate: validateDuration, defaultValue: '5m' }),
removalDelay: schema.string({ validate: validateDuration, defaultValue: '1h' }),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as alerting, we introduce a removal delay to remove the possibility of race conditions between the task that's being deleted and any other child tasks that may be scheduled using the same API key.

'--xpack.eventLog.logEntries=true',
'--xpack.eventLog.indexEntries=true',
'--xpack.task_manager.monitored_aggregated_stats_refresh_rate=5000',
'--xpack.task_manager.invalidate_api_key_task.removalDelay="1s"',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reducing the removal delay for testing

return apiKey.id === result.userScope?.apiKeyId;
}).length
).eql(0);
).eql(1);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated this test that previously checked whether the API key was deleted after a task was run. Now it checks that the API key is still there an an api_key_to_invalidate SO is created. Then it manually triggers the invalidate API key task and checks that the API key is deleted after that.

@ymao1 ymao1 self-assigned this Dec 2, 2025
@ymao1 ymao1 added release_note:skip Skip the PR/issue when compiling release notes Feature:Task Manager backport:skip This PR does not require backporting Team:ResponseOps Platform ResponseOps team (formerly the Cases and Alerting teams) t// labels Dec 2, 2025
@ymao1 ymao1 added the v9.3.0 label Dec 2, 2025
@ymao1 ymao1 marked this pull request as ready for review December 2, 2025 22:00
@ymao1 ymao1 requested review from a team as code owners December 2, 2025 22:00
@elasticmachine
Copy link
Contributor

Pinging @elastic/response-ops (Team:ResponseOps)

Copy link
Member

@jbudz jbudz left a comment

Choose a reason for hiding this comment

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

src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker LGTM

@ymao1
Copy link
Contributor Author

ymao1 commented Dec 3, 2025

@elasticmachine merge upstream

@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #24 / Alerting builtin alertTypes es_query rule runs correctly: threshold on ungrouped hit count < > for esQuery search type
  • [job] [logs] FTR Configs #60 / Alerting bulkDisable should bulk disable and untrack

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
taskManager 74 76 +2

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
taskManager 9 10 +1
Unknown metric groups

API count

id before after diff
taskManager 120 122 +2

References to deprecated APIs

id before after diff
taskManagerDependencies 0 2 +2

History

cc @ymao1

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

Labels

backport:skip This PR does not require backporting Feature:Task Manager release_note:skip Skip the PR/issue when compiling release notes Team:ResponseOps Platform ResponseOps team (formerly the Cases and Alerting teams) t// v9.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Response Ops][Task Manager] Should not invalidate API key for task if it is being used by another task

4 participants