-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Task Manager] Use strategy pattern for API key management #259373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
7d49821
[Task Manager] Use strategy pattern for API key management
ersin-erdal e09be06
Changes from node scripts/lint_ts_projects --fix
kibanamachine 3edf02b
Changes from node scripts/regenerate_moon_projects.js --update
kibanamachine 440fed0
fix tests
ersin-erdal 15647e3
fix snapshots
ersin-erdal 2dc8ed5
Merge branch 'main' into api-keys-strategies
ersin-erdal f8019a0
Merge branch 'main' into api-keys-strategies
ersin-erdal b89299c
Merge branch 'main' into api-keys-strategies
ersin-erdal b850acb
Changes from node scripts/eslint_all_files --no-cache --fix
kibanamachine f533e97
Merge branch 'main' into api-keys-strategies
ersin-erdal 60edaff
Merge branch 'main' into api-keys-strategies
ersin-erdal 6860ba7
remove redundant yarn.lock
ersin-erdal 13c821e
Merge branch 'main' into api-keys-strategies
ersin-erdal f0f727e
encrypt uiamAPiKey in api_key_to_invalidate
ersin-erdal 1bcf94b
fix tests
ersin-erdal a896902
remove bulkMarkApiKeysForInvalidation
ersin-erdal 977b86c
Merge branch 'main' into api-keys-strategies
ersin-erdal 90fd09f
Merge branch 'main' into api-keys-strategies
ersin-erdal 784d41b
Merge branch 'main' into api-keys-strategies
ersin-erdal 27d5adf
Merge branch 'main' into api-keys-strategies
ersin-erdal 15a50b7
skip UIAM API key grant when request credential is not compatible
ersin-erdal 0ed22f2
add schedule/delete routes and Scout UIAM API key tests for task manager
ersin-erdal 3a9614e
tighten input validation on schedule route schema
ersin-erdal e49c309
add Scout test files to tsconfig and reference @kbn/scout
ersin-erdal 3eb2a70
address comments
ersin-erdal 636ff63
Changes from node scripts/regenerate_moon_projects.js --update
kibanamachine f30a8c4
remove noop task type, use existing task type with enabled: false
ersin-erdal 46d94d3
Merge branch 'main' into api-keys-strategies
ersin-erdal 98480d0
Merge branch 'main' into api-keys-strategies
ersin-erdal f8ec66c
Merge branch 'main' into api-keys-strategies
ersin-erdal 7180316
Merge branch 'main' into api-keys-strategies
ersin-erdal 156635c
Merge branch 'main' into api-keys-strategies
ersin-erdal 969f3f8
Merge branch 'main' into api-keys-strategies
ersin-erdal d230160
Move Task Manager schedule/delete FTR routes to ftr_apis plugin
ersin-erdal a49ab29
Merge branch 'main' into api-keys-strategies
ersin-erdal 316f3dd
Merge branch 'main' into api-keys-strategies
ersin-erdal 133d04a
use ['ftrApis'] privileges
ersin-erdal 5dfc7a6
Add xpack.task_manager.grant_uiam_api_keys rollout flag
ersin-erdal 81eec75
Fix Scout task_manager UIAM tests and address review feedback
ersin-erdal 4dec24d
Move task_api_keys Scout spec behind feature-flag config set
ersin-erdal f5cb90c
Changes from node scripts/regenerate_moon_projects.js --update
kibanamachine ca13796
Merge branch 'main' into api-keys-strategies
ersin-erdal d51be60
Merge branch 'main' into api-keys-strategies
ersin-erdal d5d6e29
Run Task Manager Scout API specs sequentially
ersin-erdal 468bf0c
add comments and some guardrails
ersin-erdal 7f39929
Changes from node scripts/eslint_all_files --no-cache --fix
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4734,6 +4734,9 @@ | |
| "properties": { | ||
| "apiKeyId": { | ||
| "type": "keyword" | ||
| }, | ||
| "uiamApiKeyId": { | ||
| "type": "keyword" | ||
| } | ||
| } | ||
| } | ||
|
|
||
59 changes: 59 additions & 0 deletions
59
packages/kbn-check-saved-objects-cli/src/migrations/__fixtures__/task/10.9.0.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| { | ||
| "10.8.0": [ | ||
| { | ||
| "taskType": "string", | ||
| "scheduledAt": "string", | ||
| "startedAt": "string?|null", | ||
| "retryAt": "string?|null", | ||
| "runAt": "string", | ||
| "params": "string", | ||
| "state": "string", | ||
| "stateVersion": "number?", | ||
| "traceparent": "string", | ||
| "user": "string?", | ||
| "scope": "array?", | ||
| "ownerId": "string?|null", | ||
| "enabled": "boolean?", | ||
| "timeoutOverride": "string?", | ||
| "attempts": "number", | ||
| "status": "idle|claiming|running|failed|unrecognized|dead_letter", | ||
| "version": "string?", | ||
| "partition": "number?", | ||
| "priority": "number?", | ||
| "interval": "string", | ||
| "apiKey": "string?", | ||
| "userScope": "object?", | ||
| "schedule": "object?", | ||
| "cost": "tiny?|normal?|extralarge?" | ||
| } | ||
| ], | ||
| "10.9.0": [ | ||
| { | ||
| "taskType": "string", | ||
| "scheduledAt": "string", | ||
| "startedAt": "string?|null", | ||
| "retryAt": "string?|null", | ||
| "runAt": "string", | ||
| "params": "string", | ||
| "state": "string", | ||
| "stateVersion": "number?", | ||
| "traceparent": "string", | ||
| "user": "string?", | ||
| "scope": "array?", | ||
| "ownerId": "string?|null", | ||
| "enabled": "boolean?", | ||
| "timeoutOverride": "string?", | ||
| "attempts": "number", | ||
| "status": "idle|claiming|running|failed|unrecognized|dead_letter", | ||
| "version": "string?", | ||
| "partition": "number?", | ||
| "priority": "number?", | ||
| "interval": "string", | ||
| "apiKey": "string?", | ||
| "uiamApiKey": "string?", | ||
| "userScope": "object?", | ||
| "schedule": "object?", | ||
| "cost": "tiny?|normal?|extralarge?" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
x-pack/platform/plugins/shared/task_manager/server/api_key_strategy/api_key_strategy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { | ||
| Logger, | ||
| SecurityServiceStart, | ||
| IBasePath, | ||
| KibanaRequest, | ||
| SavedObjectsClientContract, | ||
| } from '@kbn/core/server'; | ||
| import type { ApiKeyType } from '../config'; | ||
| import type { ConcreteTaskInstance, TaskInstance, TaskUserScope } from '../task'; | ||
| import { INVALIDATE_API_KEY_SO_NAME } from '../saved_objects'; | ||
|
|
||
| export type { ApiKeyType } from '../config'; | ||
|
|
||
| export interface ApiKeySOFields { | ||
| apiKey: string; | ||
| uiamApiKey?: string; | ||
| userScope: TaskUserScope; | ||
| } | ||
|
|
||
| export interface InvalidationTarget { | ||
| apiKeyId: string; | ||
| uiamApiKey?: string; | ||
| } | ||
|
|
||
| export interface ApiKeyStrategy { | ||
| readonly shouldGrantUiam: boolean; | ||
| readonly typeToUse: ApiKeyType; | ||
|
|
||
| grantApiKeys( | ||
| taskInstances: TaskInstance[], | ||
| request: KibanaRequest, | ||
| security: SecurityServiceStart, | ||
| basePath: IBasePath | ||
| ): Promise<Map<string, ApiKeySOFields>>; | ||
|
|
||
| getApiKeyForFakeRequest(taskInstance: ConcreteTaskInstance): string | undefined; | ||
|
|
||
| getApiKeyIdsForInvalidation(taskInstance: ConcreteTaskInstance): InvalidationTarget[]; | ||
|
|
||
| markForInvalidation( | ||
| targets: InvalidationTarget[], | ||
| logger: Logger, | ||
| savedObjectsClient: SavedObjectsClientContract | ||
| ): Promise<void>; | ||
| } | ||
|
|
||
| export const markApiKeysForInvalidation = async ( | ||
| targets: InvalidationTarget[], | ||
| logger: Logger, | ||
| savedObjectsClient: SavedObjectsClientContract | ||
| ): Promise<void> => { | ||
| if (targets.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await savedObjectsClient.bulkCreate( | ||
| targets.map((target) => ({ | ||
| attributes: { | ||
| apiKeyId: target.apiKeyId, | ||
| createdAt: new Date().toISOString(), | ||
| ...(target.uiamApiKey ? { uiamApiKey: target.uiamApiKey } : {}), | ||
| }, | ||
| type: INVALIDATE_API_KEY_SO_NAME, | ||
| })) | ||
| ); | ||
| } catch (e) { | ||
| logger.error(`Failed to bulk mark ${targets.length} API keys for invalidation: ${e.message}`); | ||
| } | ||
| }; | ||
46 changes: 46 additions & 0 deletions
46
...tform/plugins/shared/task_manager/server/api_key_strategy/create_api_key_strategy.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { coreMock } from '@kbn/core/server/mocks'; | ||
| import { ApiKeyType } from '../config'; | ||
| import { createApiKeyStrategy } from './create_api_key_strategy'; | ||
| import { EsApiKeyStrategy } from './es_api_key_strategy'; | ||
| import { EsAndUiamApiKeyStrategy } from './es_and_uiam_api_key_strategy'; | ||
|
|
||
| describe('createApiKeyStrategy', () => { | ||
| test('returns EsApiKeyStrategy when UIAM is not available', () => { | ||
| const coreStart = coreMock.createStart(); | ||
| coreStart.security.authc.apiKeys.uiam = null as never; | ||
|
|
||
| const strategy = createApiKeyStrategy(ApiKeyType.ES, coreStart.security); | ||
| expect(strategy).toBeInstanceOf(EsApiKeyStrategy); | ||
| }); | ||
|
|
||
| test('returns EsAndUiamApiKeyStrategy when UIAM is available', () => { | ||
| const coreStart = coreMock.createStart(); | ||
| coreStart.security.authc.apiKeys.uiam = { | ||
| grant: jest.fn(), | ||
| invalidate: jest.fn(), | ||
| convert: jest.fn(), | ||
| } as never; | ||
|
|
||
| const strategy = createApiKeyStrategy(ApiKeyType.ES, coreStart.security); | ||
| expect(strategy).toBeInstanceOf(EsAndUiamApiKeyStrategy); | ||
| }); | ||
|
|
||
| test('passes apiKeyType to EsAndUiamApiKeyStrategy', () => { | ||
| const coreStart = coreMock.createStart(); | ||
| coreStart.security.authc.apiKeys.uiam = { | ||
| grant: jest.fn(), | ||
| invalidate: jest.fn(), | ||
| convert: jest.fn(), | ||
| } as never; | ||
|
|
||
| const strategy = createApiKeyStrategy(ApiKeyType.UIAM, coreStart.security); | ||
| expect(strategy.typeToUse).toBe(ApiKeyType.UIAM); | ||
| }); | ||
| }); |
22 changes: 22 additions & 0 deletions
22
...k/platform/plugins/shared/task_manager/server/api_key_strategy/create_api_key_strategy.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { SecurityServiceStart } from '@kbn/core/server'; | ||
| import type { ApiKeyType } from '../config'; | ||
| import type { ApiKeyStrategy } from './api_key_strategy'; | ||
| import { EsApiKeyStrategy } from './es_api_key_strategy'; | ||
| import { EsAndUiamApiKeyStrategy } from './es_and_uiam_api_key_strategy'; | ||
|
|
||
| export const createApiKeyStrategy = ( | ||
| apiKeyType: ApiKeyType, | ||
| security: SecurityServiceStart | ||
| ): ApiKeyStrategy => { | ||
| if (security.authc.apiKeys.uiam) { | ||
| return new EsAndUiamApiKeyStrategy(apiKeyType, security); | ||
| } | ||
| return new EsApiKeyStrategy(); | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it okay to write an actual api key to a regular SO here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, no it is not, converted it to an encrypted field.