[Alerting] Provision UIAM APIKeys for alerting rules#254211
[Alerting] Provision UIAM APIKeys for alerting rules#254211ersin-erdal merged 63 commits intoelastic:mainfrom
Conversation
…tion_tests/ci_checks
# Conflicts: # x-pack/platform/plugins/shared/alerting/server/plugin.ts
…tion_tests/ci_checks
# Conflicts: # src/core/packages/saved-objects/server-internal/src/object_types/index.ts # x-pack/platform/plugins/shared/alerting/server/plugin.ts
|
Pinging @elastic/response-ops (Team:ResponseOps) |
azasypkin
left a comment
There was a problem hiding this comment.
Changes in x-pack/platform/plugins/shared/alerting/server/saved_objects/index.ts LGTM: a newly defined uiam_api_keys_provisioning_status SO type doesn't have any encrypted fields.
| core.featureFlags | ||
| .getBooleanValue$(PROVISION_UIAM_API_KEYS_FLAG, false) | ||
| .subscribe((enabled: boolean) => { | ||
| this.applyProvisioningFlag(enabled, taskManager).catch(() => {}); | ||
| }); |
There was a problem hiding this comment.
we need to keep the reference to this subscription, and unsubscribe on plugin stop
| const keys = apiKeysToConvert.map(({ attributes }) => attributes.apiKey!); | ||
| const convertResponse = await context.uiamConvert(keys); | ||
| if (convertResponse === null) { | ||
| throw new Error('License required for the UIAM convert API is not enabled'); |
There was a problem hiding this comment.
does it happen with a missing license only?
There was a problem hiding this comment.
Yes uiam.convert returns null only when license is missing
| page += 1; | ||
| } | ||
| if (ruleIds.size === 0) return undefined; | ||
| return nodeTypes.function.buildNode('not', convertRuleIdsToKueryNode(Array.from(ruleIds))); |
There was a problem hiding this comment.
It's unlikely we hit the clause limit, buy it's worth adding a safeguard here?
There was a problem hiding this comment.
Done, Claude recommended me to use chunks there.
But I don't think we would hit the limit.
Default limit is 4096 and the project has the most rules has 3600 rules.
⏳ Build in-progress, with failures
Failed CI Steps
History
|
Merge timing conflict between elastic#257678 and elastic#254211
This PR adds a background task that migrates alerting rules from legacy
API keys to UIAM (Unified Identity and Access Management) API keys on
Serverless. The task runs only when the
`alerting.rules.provisionUiamApiKeys` feature flag is enabled and only
in Serverless mode.
### What's in scope
- **New task** `alerting:api_key_provisioning`
- Runs every 1 minute (configurable interval).
- Timeout: 5 minutes.
- Only registered and started when `isServerless` is true.
- Scheduled when `alerting.rules.provisionUiamApiKeys` is enabled;
removed when the flag is disabled.
- **Provisioning flow**
1. Find rules that have `apiKey` but no `uiamApiKey`, and where the key
was not user-created (`apiKeyCreatedByUser !== true`).
2. Exclude rules that already have a provisioning status of `completed`
or `skipped` (via new saved object type).
3. Call the core UIAM convert API to convert legacy API keys to UIAM
format.
4. Bulk-update rules with the new `uiamApiKey`.
5. Write provisioning status per rule (completed / failed / skipped).
THe SO is in the Alerting/Cases scope (Maybe should be independent since
we want to use it for TM provisioning too)
6. If a rule update fails after a successful conversion, the newly
created UIAM API key is marked for invalidation (orphan handling).
- **New saved object type**
`uiam-api-keys-provisioning-status` — one document per rule with
`entityId`, `entityType`, `status` (completed | failed | skipped),
optional `message`, and `@timestamp`. Used to avoid re-processing and to
report status.
- **Skipped rules**
Rules are skipped (and status recorded) when:
- No `apiKey`, or
- Already have `uiamApiKey`, or
- `apiKeyCreatedByUser === true`.
- **Batching**
Rules are read in batches of 300. If more rules remain, the task
reschedules itself 1 minute later.
### Out of scope / behavior
- **Non-Serverless**: Task is not registered; no behavior change.
- **Flag off**: Task is not scheduled; no conversion runs.
- **License**: If the UIAM convert API is unavailable (e.g. license),
the task throws and Task Manager will retry.
## To verify:
1. Add below configs to your serverless.dev.yml
```
xpack.alerting.rules.apiKeyType: 'es'
xpack.alerting.invalidateApiKeysTask.removalDelay: '10s'
xpack.alerting.invalidateApiKeysTask.interval: '1m'
feature_flags.overrides:
alerting.rules.provisionUiamApiKeys: false
```
so we can create some rules with the ES API Keys.
2. Run Kibana and Elasticsearch with the below commands:
`yarn es serverless --projectType observability`
`yarn start --serverless oblt --run-examples`
3. Login with the `system_indices_superuser` user.
4. Create 4-5 rules and let them run at least for one time.
5. Stop your ES and Kibana
6. Update the below FF in your serverless.yml
```
feature_flags.overrides:
alerting.rules.provisionUiamApiKeys: true
```
7. Update `GET_RULES_BATCH_SIZE` to `2` in
`uiam_api_key_provisioning_task.ts`
8. Run Kibana and Elasticsearch with the below commands:
`yarn es serverless --projectType observability --uiam`
`yarn start --serverless oblt --run-examples --uiam`
9. The task should run and update the first 2 rules with `uiamApiKey`
```
GET /.kibana_alerting_cases_*/_search
{
"query": {
"match": {
"type": {
"query": "alert"
}
}
}
}
```
10. Wait for 1m, the task should run again and update the next 2 rules.
11. You can observe the provisioning status with:
```
GET /.kibana_alerting_cases_*/_search
{
"query": {
"match": {
"type": {
"query": "uiam_api_keys_provisioning_status"
}
}
}
}
```
12. If your rules run often (every 1m) Rule update may fail. In this
case we set the provisioning status to failed and fetch the rule in the
next batch again. We also set the orphaned converted apiKeys to be
invalidated.
You can see them by using the below query:
```
GET /.kibana_alerting_cases_*/_search
{
"query": {
"match": {
"type": {
"query": "api_key_pending_invalidation"
}
}
}
}
```
---------
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Tyler Smalley <tyler.smalley@elastic.co>
Merge timing conflict between elastic#257678 and elastic#254211
This PR adds a background task that migrates alerting rules from legacy
API keys to UIAM (Unified Identity and Access Management) API keys on
Serverless. The task runs only when the
`alerting.rules.provisionUiamApiKeys` feature flag is enabled and only
in Serverless mode.
### What's in scope
- **New task** `alerting:api_key_provisioning`
- Runs every 1 minute (configurable interval).
- Timeout: 5 minutes.
- Only registered and started when `isServerless` is true.
- Scheduled when `alerting.rules.provisionUiamApiKeys` is enabled;
removed when the flag is disabled.
- **Provisioning flow**
1. Find rules that have `apiKey` but no `uiamApiKey`, and where the key
was not user-created (`apiKeyCreatedByUser !== true`).
2. Exclude rules that already have a provisioning status of `completed`
or `skipped` (via new saved object type).
3. Call the core UIAM convert API to convert legacy API keys to UIAM
format.
4. Bulk-update rules with the new `uiamApiKey`.
5. Write provisioning status per rule (completed / failed / skipped).
THe SO is in the Alerting/Cases scope (Maybe should be independent since
we want to use it for TM provisioning too)
6. If a rule update fails after a successful conversion, the newly
created UIAM API key is marked for invalidation (orphan handling).
- **New saved object type**
`uiam-api-keys-provisioning-status` — one document per rule with
`entityId`, `entityType`, `status` (completed | failed | skipped),
optional `message`, and `@timestamp`. Used to avoid re-processing and to
report status.
- **Skipped rules**
Rules are skipped (and status recorded) when:
- No `apiKey`, or
- Already have `uiamApiKey`, or
- `apiKeyCreatedByUser === true`.
- **Batching**
Rules are read in batches of 300. If more rules remain, the task
reschedules itself 1 minute later.
### Out of scope / behavior
- **Non-Serverless**: Task is not registered; no behavior change.
- **Flag off**: Task is not scheduled; no conversion runs.
- **License**: If the UIAM convert API is unavailable (e.g. license),
the task throws and Task Manager will retry.
## To verify:
1. Add below configs to your serverless.dev.yml
```
xpack.alerting.rules.apiKeyType: 'es'
xpack.alerting.invalidateApiKeysTask.removalDelay: '10s'
xpack.alerting.invalidateApiKeysTask.interval: '1m'
feature_flags.overrides:
alerting.rules.provisionUiamApiKeys: false
```
so we can create some rules with the ES API Keys.
2. Run Kibana and Elasticsearch with the below commands:
`yarn es serverless --projectType observability`
`yarn start --serverless oblt --run-examples`
3. Login with the `system_indices_superuser` user.
4. Create 4-5 rules and let them run at least for one time.
5. Stop your ES and Kibana
6. Update the below FF in your serverless.yml
```
feature_flags.overrides:
alerting.rules.provisionUiamApiKeys: true
```
7. Update `GET_RULES_BATCH_SIZE` to `2` in
`uiam_api_key_provisioning_task.ts`
8. Run Kibana and Elasticsearch with the below commands:
`yarn es serverless --projectType observability --uiam`
`yarn start --serverless oblt --run-examples --uiam`
9. The task should run and update the first 2 rules with `uiamApiKey`
```
GET /.kibana_alerting_cases_*/_search
{
"query": {
"match": {
"type": {
"query": "alert"
}
}
}
}
```
10. Wait for 1m, the task should run again and update the next 2 rules.
11. You can observe the provisioning status with:
```
GET /.kibana_alerting_cases_*/_search
{
"query": {
"match": {
"type": {
"query": "uiam_api_keys_provisioning_status"
}
}
}
}
```
12. If your rules run often (every 1m) Rule update may fail. In this
case we set the provisioning status to failed and fetch the rule in the
next batch again. We also set the orphaned converted apiKeys to be
invalidated.
You can see them by using the below query:
```
GET /.kibana_alerting_cases_*/_search
{
"query": {
"match": {
"type": {
"query": "api_key_pending_invalidation"
}
}
}
}
```
---------
Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Tyler Smalley <tyler.smalley@elastic.co>
Merge timing conflict between elastic#257678 and elastic#254211
This PR adds a background task that migrates alerting rules from legacy API keys to UIAM (Unified Identity and Access Management) API keys on Serverless. The task runs only when the
alerting.rules.provisionUiamApiKeysfeature flag is enabled and only in Serverless mode.What's in scope
New task
alerting:api_key_provisioningisServerlessis true.alerting.rules.provisionUiamApiKeysis enabled; removed when the flag is disabled.Provisioning flow
apiKeybut nouiamApiKey, and where the key was not user-created (apiKeyCreatedByUser !== true).completedorskipped(via new saved object type).uiamApiKey.New saved object type
uiam-api-keys-provisioning-status— one document per rule withentityId,entityType,status(completed | failed | skipped), optionalmessage, and@timestamp. Used to avoid re-processing and to report status.Skipped rules
Rules are skipped (and status recorded) when:
apiKey, oruiamApiKey, orapiKeyCreatedByUser === true.Batching
Rules are read in batches of 300. If more rules remain, the task reschedules itself 1 minute later.
Out of scope / behavior
To verify:
so we can create some rules with the ES API Keys.
Run Kibana and Elasticsearch with the below commands:
yarn es serverless --projectType observabilityyarn start --serverless oblt --run-examplesLogin with the
system_indices_superuseruser.Create 4-5 rules and let them run at least for one time.
Stop your ES and Kibana
Update the below FF in your serverless.yml
GET_RULES_BATCH_SIZEto2inuiam_api_key_provisioning_task.tsyarn es serverless --projectType observability --uiamyarn start --serverless oblt --run-examples --uiamuiamApiKeyYou can see them by using the below query: