From 46e12b59b6f361a60e013932076215b63e669c19 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 16 Dec 2019 16:14:34 -0500 Subject: [PATCH 1/4] [Fleet] Connect fleet to policy change update --- x-pack/legacy/plugins/fleet/index.ts | 12 ++++---- .../plugins/fleet/server/kibana.index.ts | 24 --------------- .../fleet/server/libs/compose/kibana.ts | 7 ++++- .../fleet/server/libs/policy_update.ts | 29 +++++++++++++++++++ .../enrollment_api_keys/default.ts | 6 ---- .../plugins/ingest/server/libs/policy.ts | 26 +++++++++++++++-- .../plugins/ingest/server/libs/types.ts | 7 +++++ 7 files changed, 72 insertions(+), 39 deletions(-) create mode 100644 x-pack/legacy/plugins/fleet/server/libs/policy_update.ts diff --git a/x-pack/legacy/plugins/fleet/index.ts b/x-pack/legacy/plugins/fleet/index.ts index 8af32cac2a7b7..1c5df5d8ae252 100644 --- a/x-pack/legacy/plugins/fleet/index.ts +++ b/x-pack/legacy/plugins/fleet/index.ts @@ -57,11 +57,13 @@ export function fleet(kibana: any) { config: () => config, configPrefix: CONFIG_PREFIX, init(server: any) { - server.newPlatform.setup.plugins.encryptedSavedObjects.registerType({ - type: 'enrollment_api_keys', - attributesToEncrypt: new Set(['api_key']), - attributesToExcludeFromAAD: new Set(['enrollment_rules']), - }); + // TODO https://github.com/elastic/kibana/issues/53199 + // server.newPlatform.setup.plugins.encryptedSavedObjects.registerType({ + // type: 'enrollment_api_keys', + // // attributesToEncrypt: new Set(['api_key']), + // attributesToEncrypt: new Set([]), + // attributesToExcludeFromAAD: new Set(['enrollment_rules']), + // }); server.plugins.xpack_main.registerFeature({ id: 'fleet', name: 'Fleet', diff --git a/x-pack/legacy/plugins/fleet/server/kibana.index.ts b/x-pack/legacy/plugins/fleet/server/kibana.index.ts index eb06594aa11ff..85263eb71c6d2 100644 --- a/x-pack/legacy/plugins/fleet/server/kibana.index.ts +++ b/x-pack/legacy/plugins/fleet/server/kibana.index.ts @@ -6,32 +6,8 @@ import { compose } from './libs/compose/kibana'; import { initRestApi } from './routes/init_api'; -import { FrameworkUser } from './adapters/framework/adapter_types'; -import { PolicyUpdatedEvent } from '../common/types/domain_data'; export const initServerWithKibana = (hapiServer: any) => { const libs = compose(hapiServer); initRestApi(hapiServer, libs); - // expose methods - libs.framework.expose('policyUpdated', async function handlePolicyUpdate( - event: PolicyUpdatedEvent, - user: FrameworkUser = { - kind: 'internal', - } - ) { - if (event.type === 'created') { - await libs.apiKeys.generateEnrollmentApiKey(user, { - policyId: event.policyId, - }); - } - - if (event.type === 'updated') { - await libs.agentsPolicy.updateAgentsForPolicyId(user, event.policyId); - } - - if (event.type === 'deleted') { - await libs.agents.unenrollForPolicy(user, event.policyId); - await libs.apiKeys.deleteEnrollmentApiKeyForPolicyId(user, event.policyId); - } - }); }; diff --git a/x-pack/legacy/plugins/fleet/server/libs/compose/kibana.ts b/x-pack/legacy/plugins/fleet/server/libs/compose/kibana.ts index 21bc6e9e94209..5f8c073a562a5 100644 --- a/x-pack/legacy/plugins/fleet/server/libs/compose/kibana.ts +++ b/x-pack/legacy/plugins/fleet/server/libs/compose/kibana.ts @@ -24,6 +24,7 @@ import { InstallLib } from '../install'; import { ElasticsearchAdapter } from '../../adapters/elasticsearch/default'; import { AgentPolicyLib } from '../agent_policy'; import { AgentEventLib } from '../agent_event'; +import { makePolicyUpdateHandler } from '../policy_update'; export function compose(server: any): FleetServerLib { const frameworkAdapter = new FrameworkAdapter(server); @@ -59,7 +60,7 @@ export function compose(server: any): FleetServerLib { const install = new InstallLib(framework); - return { + const libs = { agents, agentsPolicy, agentEvents, @@ -69,4 +70,8 @@ export function compose(server: any): FleetServerLib { install, framework, }; + const policyUpdateHandler = makePolicyUpdateHandler(libs); + server.plugins.ingest.policy.registerPolicyUpdateHandler(policyUpdateHandler); + + return libs; } diff --git a/x-pack/legacy/plugins/fleet/server/libs/policy_update.ts b/x-pack/legacy/plugins/fleet/server/libs/policy_update.ts new file mode 100644 index 0000000000000..35292ea7015d4 --- /dev/null +++ b/x-pack/legacy/plugins/fleet/server/libs/policy_update.ts @@ -0,0 +1,29 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { FrameworkUser } from '../adapters/framework/adapter_types'; +import { FleetServerLib } from './types'; + +export function makePolicyUpdateHandler(libs: FleetServerLib) { + return async function policyUpdateHandler(user: FrameworkUser, action: string, policyId: string) { + const internalUser = libs.framework.getInternalUser(); + + if (action === 'created') { + await libs.apiKeys.generateEnrollmentApiKey(internalUser, { + policyId, + }); + } + + if (action === 'updated') { + await libs.agentsPolicy.updateAgentsForPolicyId(internalUser, policyId); + } + + if (action === 'deleted') { + await libs.agents.unenrollForPolicy(internalUser, policyId); + await libs.apiKeys.deleteEnrollmentApiKeyForPolicyId(internalUser, policyId); + } + }; +} diff --git a/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/default.ts b/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/default.ts index 2cf798c3bc6fc..25723de330987 100644 --- a/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/default.ts +++ b/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/default.ts @@ -77,9 +77,6 @@ export class EnrollmentApiKeysRepository implements EnrollmentApiKeysRepositoryT name?: string; } ): Promise { - if (user.kind !== 'authenticated') { - throw new Error('Only authenticated user can create enrollment api keys.'); - } const so = await this.soAdapter.create(user, SAVED_OBJECT_TYPE, { created_at: moment().toISOString(), api_key: apiKey, @@ -122,9 +119,6 @@ export class EnrollmentApiKeysRepository implements EnrollmentApiKeysRepositoryT id: string, newData: Partial ): Promise { - if (user.kind !== 'authenticated') { - throw new Error('Only authenticated can update enrollment api keys'); - } const { error } = await this.soAdapter.update(user, SAVED_OBJECT_TYPE, id, newData); if (error) { diff --git a/x-pack/legacy/plugins/ingest/server/libs/policy.ts b/x-pack/legacy/plugins/ingest/server/libs/policy.ts index e96b269a9b011..c702cc3b67651 100644 --- a/x-pack/legacy/plugins/ingest/server/libs/policy.ts +++ b/x-pack/legacy/plugins/ingest/server/libs/policy.ts @@ -4,7 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import EventEmitter from 'events'; import { flatten, unique } from 'lodash'; import { DEFAULT_POLICY_ID } from '../../common/constants'; import { ReturnTypeBulkDelete } from '../../common/types/std_return_format'; @@ -15,9 +14,10 @@ import { DatasourcesLib } from './datasources'; import { BackendFrameworkLib } from './framework'; import { OutputsLib } from './outputs'; import { Status, Policy, Datasource } from '../../common/types/domain_data'; +import { PolicyUpdateHandler } from './types'; export class PolicyLib { - public events: EventEmitter = new EventEmitter(); + public eventsHandler: PolicyUpdateHandler[] = []; constructor( private readonly adapter: PolicyAdapter, private readonly libs: { @@ -121,6 +121,10 @@ export class PolicyLib { }; } + public registerPolicyUpdateHandler(handler: PolicyUpdateHandler) { + this.eventsHandler.push(handler); + } + // public async changeLog( // id: string, // page: number = 1, @@ -264,6 +268,12 @@ export class PolicyLib { await this.adapter.create(this.libs.framework.internalUser, newDefaultPolicy, { id: DEFAULT_POLICY_ID, }); + + await this._triggerPolicyUpdatedEvent( + this.libs.framework.internalUser, + 'created', + DEFAULT_POLICY_ID + ); } } @@ -273,11 +283,21 @@ export class PolicyLib { updated_on: new Date().toString(), updated_by: (user as FrameworkAuthenticatedUser).username || 'Fleet (system action)', }); - // TODO fire events for fleet that update was made + await this._triggerPolicyUpdatedEvent(user, 'updated', id); // TODO create audit/history log // const newPolicy = await this.adapter.create(policyData); return policy as Policy; } + + private async _triggerPolicyUpdatedEvent( + user: FrameworkUser, + action: 'created' | 'updated' | 'deleted', + policyId: string + ) { + for (const handler of this.eventsHandler) { + await handler(user, action, policyId); + } + } } diff --git a/x-pack/legacy/plugins/ingest/server/libs/types.ts b/x-pack/legacy/plugins/ingest/server/libs/types.ts index f0359c8cbc4fe..b481b2a0fe69c 100644 --- a/x-pack/legacy/plugins/ingest/server/libs/types.ts +++ b/x-pack/legacy/plugins/ingest/server/libs/types.ts @@ -9,6 +9,7 @@ import { DatasourcesLib } from './datasources'; import { BackendFrameworkLib } from './framework'; import { OutputsLib } from './outputs'; import { PolicyLib } from './policy'; +import { FrameworkUser } from './adapters/framework/adapter_types'; export interface ServerLibs { outputs: OutputsLib; @@ -17,3 +18,9 @@ export interface ServerLibs { framework: BackendFrameworkLib; database?: ESDatabaseAdapter; } + +export type PolicyUpdateHandler = ( + user: FrameworkUser, + action: 'created' | 'updated' | 'deleted', + policyId: string +) => Promise; From cb203ae8f0cfe53f7e9063f79612bb2d9096f7d5 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 16 Dec 2019 16:25:28 -0500 Subject: [PATCH 2/4] Send deleted event when a policy is updated --- x-pack/legacy/plugins/ingest/server/libs/policy.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/x-pack/legacy/plugins/ingest/server/libs/policy.ts b/x-pack/legacy/plugins/ingest/server/libs/policy.ts index c702cc3b67651..43c0abf8bc0b1 100644 --- a/x-pack/legacy/plugins/ingest/server/libs/policy.ts +++ b/x-pack/legacy/plugins/ingest/server/libs/policy.ts @@ -205,6 +205,7 @@ export class PolicyLib { for (const id of ids) { await this.adapter.delete(user, id); + await this._triggerPolicyUpdatedEvent(user, 'deleted', id); } return { From 0b0aa94b604b7788cae3bd326f3c93021299f201 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 16 Dec 2019 17:48:59 -0500 Subject: [PATCH 3/4] Fix api creation refresh --- .../pages/agent_list/components/agent_enrollment.tsx | 2 +- .../components/enrollment_api_keys/index.tsx | 12 +++++++++--- .../plugins/fleet/server/libs/policy_update.ts | 4 ++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/x-pack/legacy/plugins/fleet/public/pages/agent_list/components/agent_enrollment.tsx b/x-pack/legacy/plugins/fleet/public/pages/agent_list/components/agent_enrollment.tsx index d57e5106c9ead..93b7baec89336 100644 --- a/x-pack/legacy/plugins/fleet/public/pages/agent_list/components/agent_enrollment.tsx +++ b/x-pack/legacy/plugins/fleet/public/pages/agent_list/components/agent_enrollment.tsx @@ -131,7 +131,7 @@ export const AgentEnrollmentFlyout: React.FC = ({ onClose, policies {apiKeyListVisible && ( <> - + enrollmentApiKeys.refresh()} /> )} diff --git a/x-pack/legacy/plugins/fleet/public/pages/agent_list/components/enrollment_api_keys/index.tsx b/x-pack/legacy/plugins/fleet/public/pages/agent_list/components/enrollment_api_keys/index.tsx index 066b44597fa34..0f2b93c257c21 100644 --- a/x-pack/legacy/plugins/fleet/public/pages/agent_list/components/enrollment_api_keys/index.tsx +++ b/x-pack/legacy/plugins/fleet/public/pages/agent_list/components/enrollment_api_keys/index.tsx @@ -12,10 +12,11 @@ import { useLibs, usePagination } from '../../../../hooks'; import { useEnrollmentApiKeys, useEnrollmentApiKey } from './hooks'; import { ConfirmDeleteModal } from './confirm_delete_modal'; import { CreateApiKeyForm } from './create_api_key_form'; - export { useEnrollmentApiKeys, useEnrollmentApiKey } from './hooks'; -export const EnrollmentApiKeysTable: React.FC = () => { +export const EnrollmentApiKeysTable: React.FC<{ + onChange: () => void; +}> = ({ onChange }) => { const { enrollmentApiKeys } = useLibs(); const [confirmDeleteApiKeyId, setConfirmDeleteApiKeyId] = useState(null); const { pagination } = usePagination(); @@ -81,7 +82,12 @@ export const EnrollmentApiKeysTable: React.FC = () => { columns={columns} /> - refresh()} /> + { + refresh(); + onChange(); + }} + /> ); }; diff --git a/x-pack/legacy/plugins/fleet/server/libs/policy_update.ts b/x-pack/legacy/plugins/fleet/server/libs/policy_update.ts index 35292ea7015d4..f37b899665f02 100644 --- a/x-pack/legacy/plugins/fleet/server/libs/policy_update.ts +++ b/x-pack/legacy/plugins/fleet/server/libs/policy_update.ts @@ -12,6 +12,10 @@ export function makePolicyUpdateHandler(libs: FleetServerLib) { const internalUser = libs.framework.getInternalUser(); if (action === 'created') { + if (policyId === 'default') { + // TODO wait for #53111 to be fixed + return; + } await libs.apiKeys.generateEnrollmentApiKey(internalUser, { policyId, }); From 1522dfaea381723c737adac1ce457361d14f8376 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Tue, 7 Jan 2020 11:54:11 -0500 Subject: [PATCH 4/4] Disable tests --- .../default.contract.test.slap_snap | 690 ++++++++++++++++++ .../default.contract.test.ts | 7 +- 2 files changed, 695 insertions(+), 2 deletions(-) diff --git a/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/__memorize_snapshots__/default.contract.test.slap_snap b/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/__memorize_snapshots__/default.contract.test.slap_snap index 91ff2d47d80f0..c4731ba9c2979 100644 --- a/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/__memorize_snapshots__/default.contract.test.slap_snap +++ b/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/__memorize_snapshots__/default.contract.test.slap_snap @@ -1369,3 +1369,693 @@ exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey "saved_objects": [] } } + +exports['Enrollment api key Repository create allow to create an enrollment api key - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "a53e9600-316d-11ea-a75d-6346387f5db1", + "attributes": { + "created_at": "2020-01-07T16:49:18.128Z", + "api_key": "notencryptedkey", + "api_key_id": "key-id-123", + "policy_id": "policyId", + "active": true, + "enrollment_rules": [] + }, + "references": [], + "updated_at": "2020-01-07T16:49:18.175Z", + "version": "WzIsMV0=" + } +} + +exports['Enrollment api key Repository create allow to create an enrollment api key - get:enrollment_api_keys (2)'] = { + "results": { + "id": "a53e9600-316d-11ea-a75d-6346387f5db1", + "type": "enrollment_api_keys", + "updated_at": "2020-01-07T16:49:18.175Z", + "version": "WzIsMV0=", + "attributes": { + "created_at": "2020-01-07T16:49:18.128Z", + "api_key": "notencryptedkey", + "api_key_id": "key-id-123", + "policy_id": "policyId", + "active": true, + "enrollment_rules": [] + }, + "references": [] + } +} + +exports['Enrollment api key Repository create allow to create an enrollment api key - find:"enrollment_api_keys" (3)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 1, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "a53e9600-316d-11ea-a75d-6346387f5db1", + "attributes": { + "created_at": "2020-01-07T16:49:18.128Z", + "api_key": "notencryptedkey", + "api_key_id": "key-id-123", + "policy_id": "policyId", + "active": true, + "enrollment_rules": [] + }, + "references": [], + "updated_at": "2020-01-07T16:49:18.175Z", + "version": "WzIsMV0=" + } + ] + } +} + +exports['Enrollment api key Repository create allow to create an enrollment api key - delete (4)'] = { + "results": {} +} + +exports['Enrollment api key Repository update allow to update a key - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "a66effb0-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key": "qwerty", + "policy_id": "policyId" + }, + "references": [], + "updated_at": "2020-01-07T16:49:20.171Z", + "version": "WzQsMV0=" + } +} + +exports['Enrollment api key Repository update allow to update a key - update:enrollment_api_keys (2)'] = { + "results": { + "id": "a66effb0-316d-11ea-a75d-6346387f5db1", + "type": "enrollment_api_keys", + "updated_at": "2020-01-07T16:49:21.188Z", + "version": "WzUsMV0=", + "attributes": { + "active": false, + "api_key": "notencryptedapikey" + } + } +} + +exports['Enrollment api key Repository update allow to update a key - get:enrollment_api_keys (3)'] = { + "results": { + "id": "a66effb0-316d-11ea-a75d-6346387f5db1", + "type": "enrollment_api_keys", + "updated_at": "2020-01-07T16:49:21.188Z", + "version": "WzUsMV0=", + "attributes": { + "active": false, + "api_key": "notencryptedapikey", + "policy_id": "policyId" + }, + "references": [] + } +} + +exports['Enrollment api key Repository update allow to update a key - find:"enrollment_api_keys" (4)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 1, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "a66effb0-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": false, + "api_key": "notencryptedapikey", + "policy_id": "policyId" + }, + "references": [], + "updated_at": "2020-01-07T16:49:21.188Z", + "version": "WzUsMV0=" + } + ] + } +} + +exports['Enrollment api key Repository update allow to update a key - delete (5)'] = { + "results": {} +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "a8423910-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-1" + }, + "references": [], + "updated_at": "2020-01-07T16:49:23.233Z", + "version": "WzcsMV0=" + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - create:enrollment_api_keys (2)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "a842d550-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:49:23.237Z", + "version": "WzgsMV0=" + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - find:"enrollment_api_keys" (3)'] = { + "results": { + "page": 1, + "per_page": 20, + "total": 1, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "a842d550-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:49:23.237Z", + "version": "WzgsMV0=" + } + ] + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - getDecryptedAsInternalUser:enrollment_api_keys:a842d550-316d-11ea-a75d-6346387f5db1 (4)'] = { + "results": { + "id": "a842d550-316d-11ea-a75d-6346387f5db1", + "type": "enrollment_api_keys", + "updated_at": "2020-01-07T16:49:23.237Z", + "version": "WzgsMV0=", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [] + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - find:"enrollment_api_keys" (5)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 2, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "a8423910-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-1" + }, + "references": [], + "updated_at": "2020-01-07T16:49:23.233Z", + "version": "WzcsMV0=" + }, + { + "type": "enrollment_api_keys", + "id": "a842d550-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:49:23.237Z", + "version": "WzgsMV0=" + } + ] + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - delete (6)'] = { + "results": {} +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - delete (7)'] = { + "results": {} +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - create:enrollment_api_keys (2)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "aa157270-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:49:26.295Z", + "version": "WzExLDFd" + } +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "aa14d630-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-1" + }, + "references": [], + "updated_at": "2020-01-07T16:49:26.291Z", + "version": "WzEyLDFd" + } +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - find:"enrollment_api_keys" (3)'] = { + "results": { + "page": 1, + "per_page": 20, + "total": 0, + "saved_objects": [] + } +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - find:"enrollment_api_keys" (4)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 2, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "aa157270-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:49:26.295Z", + "version": "WzExLDFd" + }, + { + "type": "enrollment_api_keys", + "id": "aa14d630-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "api-key-1" + }, + "references": [], + "updated_at": "2020-01-07T16:49:26.291Z", + "version": "WzEyLDFd" + } + ] + } +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - delete (5)'] = { + "results": {} +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - delete (6)'] = { + "results": {} +} + +exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "abe9bd40-316d-11ea-a75d-6346387f5db1", + "attributes": { + "active": true, + "api_key_id": "qwerty" + }, + "references": [], + "updated_at": "2020-01-07T16:49:29.364Z", + "version": "WzE1LDFd" + } +} + +exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey - delete (2)'] = { + "results": {} +} + +exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey - get:enrollment_api_keys (3)'] = { + "results": null +} + +exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey - find:"enrollment_api_keys" (4)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 0, + "saved_objects": [] + } +} + +exports['Enrollment api key Repository create allow to create an enrollment api key - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "e84e3fe0-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "created_at": "2020-01-07T16:51:10.673Z", + "api_key": "notencryptedkey", + "api_key_id": "key-id-123", + "policy_id": "policyId", + "active": true, + "enrollment_rules": [] + }, + "references": [], + "updated_at": "2020-01-07T16:51:10.685Z", + "version": "WzIsMV0=" + } +} + +exports['Enrollment api key Repository create allow to create an enrollment api key - get:enrollment_api_keys (2)'] = { + "results": { + "id": "e84e3fe0-316d-11ea-9b52-c576e4085a4e", + "type": "enrollment_api_keys", + "updated_at": "2020-01-07T16:51:10.685Z", + "version": "WzIsMV0=", + "attributes": { + "created_at": "2020-01-07T16:51:10.673Z", + "api_key": "notencryptedkey", + "api_key_id": "key-id-123", + "policy_id": "policyId", + "active": true, + "enrollment_rules": [] + }, + "references": [] + } +} + +exports['Enrollment api key Repository create allow to create an enrollment api key - find:"enrollment_api_keys" (3)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 1, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "e84e3fe0-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "created_at": "2020-01-07T16:51:10.673Z", + "api_key": "notencryptedkey", + "api_key_id": "key-id-123", + "policy_id": "policyId", + "active": true, + "enrollment_rules": [] + }, + "references": [], + "updated_at": "2020-01-07T16:51:10.685Z", + "version": "WzIsMV0=" + } + ] + } +} + +exports['Enrollment api key Repository create allow to create an enrollment api key - delete (4)'] = { + "results": {} +} + +exports['Enrollment api key Repository update allow to update a key - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "e95d17d0-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key": "qwerty", + "policy_id": "policyId" + }, + "references": [], + "updated_at": "2020-01-07T16:51:12.461Z", + "version": "WzQsMV0=" + } +} + +exports['Enrollment api key Repository update allow to update a key - update:enrollment_api_keys (2)'] = { + "results": { + "id": "e95d17d0-316d-11ea-9b52-c576e4085a4e", + "type": "enrollment_api_keys", + "updated_at": "2020-01-07T16:51:13.471Z", + "version": "WzUsMV0=", + "attributes": { + "active": false, + "api_key": "notencryptedapikey" + } + } +} + +exports['Enrollment api key Repository update allow to update a key - get:enrollment_api_keys (3)'] = { + "results": { + "id": "e95d17d0-316d-11ea-9b52-c576e4085a4e", + "type": "enrollment_api_keys", + "updated_at": "2020-01-07T16:51:13.471Z", + "version": "WzUsMV0=", + "attributes": { + "active": false, + "api_key": "notencryptedapikey", + "policy_id": "policyId" + }, + "references": [] + } +} + +exports['Enrollment api key Repository update allow to update a key - find:"enrollment_api_keys" (4)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 1, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "e95d17d0-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": false, + "api_key": "notencryptedapikey", + "policy_id": "policyId" + }, + "references": [], + "updated_at": "2020-01-07T16:51:13.471Z", + "version": "WzUsMV0=" + } + ] + } +} + +exports['Enrollment api key Repository update allow to update a key - delete (5)'] = { + "results": {} +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "eb2f18b0-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-1" + }, + "references": [], + "updated_at": "2020-01-07T16:51:15.515Z", + "version": "WzcsMV0=" + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - create:enrollment_api_keys (2)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "eb2fdc00-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:51:15.520Z", + "version": "WzgsMV0=" + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - find:"enrollment_api_keys" (3)'] = { + "results": { + "page": 1, + "per_page": 20, + "total": 1, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "eb2fdc00-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:51:15.520Z", + "version": "WzgsMV0=" + } + ] + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - getDecryptedAsInternalUser:enrollment_api_keys:eb2fdc00-316d-11ea-9b52-c576e4085a4e (4)'] = { + "results": { + "id": "eb2fdc00-316d-11ea-9b52-c576e4085a4e", + "type": "enrollment_api_keys", + "updated_at": "2020-01-07T16:51:15.520Z", + "version": "WzgsMV0=", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [] + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - find:"enrollment_api_keys" (5)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 2, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "eb2f18b0-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-1" + }, + "references": [], + "updated_at": "2020-01-07T16:51:15.515Z", + "version": "WzcsMV0=" + }, + { + "type": "enrollment_api_keys", + "id": "eb2fdc00-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:51:15.520Z", + "version": "WzgsMV0=" + } + ] + } +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - delete (6)'] = { + "results": {} +} + +exports['Enrollment api key Repository getByApiKeyId allow to find a key - delete (7)'] = { + "results": {} +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "ed02c740-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-1" + }, + "references": [], + "updated_at": "2020-01-07T16:51:18.580Z", + "version": "WzExLDFd" + } +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - create:enrollment_api_keys (2)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "ed036380-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:51:18.584Z", + "version": "WzEyLDFd" + } +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - find:"enrollment_api_keys" (3)'] = { + "results": { + "page": 1, + "per_page": 20, + "total": 0, + "saved_objects": [] + } +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - find:"enrollment_api_keys" (4)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 2, + "saved_objects": [ + { + "type": "enrollment_api_keys", + "id": "ed036380-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-2" + }, + "references": [], + "updated_at": "2020-01-07T16:51:18.584Z", + "version": "WzEyLDFd" + }, + { + "type": "enrollment_api_keys", + "id": "ed02c740-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "api-key-1" + }, + "references": [], + "updated_at": "2020-01-07T16:51:18.580Z", + "version": "WzExLDFd" + } + ] + } +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - delete (5)'] = { + "results": {} +} + +exports['Enrollment api key Repository getByApiKeyId return null if the key does not exists - delete (6)'] = { + "results": {} +} + +exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey - create:enrollment_api_keys (1)'] = { + "results": { + "type": "enrollment_api_keys", + "id": "eeda4660-316d-11ea-9b52-c576e4085a4e", + "attributes": { + "active": true, + "api_key_id": "qwerty" + }, + "references": [], + "updated_at": "2020-01-07T16:51:21.670Z", + "version": "WzE1LDFd" + } +} + +exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey - delete (2)'] = { + "results": {} +} + +exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey - get:enrollment_api_keys (3)'] = { + "results": null +} + +exports['Enrollment api key Repository delete allow to delete a enrollmentApiKey - find:"enrollment_api_keys" (4)'] = { + "results": { + "page": 1, + "per_page": 1000, + "total": 0, + "saved_objects": [] + } +} diff --git a/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/default.contract.test.ts b/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/default.contract.test.ts index 3dc9dd9f4eed4..8c2a3748dd02f 100644 --- a/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/default.contract.test.ts +++ b/x-pack/legacy/plugins/fleet/server/repositories/enrollment_api_keys/default.contract.test.ts @@ -105,7 +105,8 @@ describe('Enrollment api key Repository', () => { expect(savedEnrollmentApiKey).toBeDefined(); expect(enrollmentApiKey.id).toBeDefined(); - expect(savedEnrollmentApiKey.attributes.apiKey !== 'notencryptedkey').toBe(true); + // TODO fix when we encrypt keys again + // expect(savedEnrollmentApiKey.attributes.apiKey !== 'notencryptedkey').toBe(true); expect(savedEnrollmentApiKey.attributes).toMatchObject({ active: true, @@ -141,7 +142,9 @@ describe('Enrollment api key Repository', () => { SAVED_OBJECT_TYPE, enrollmentApiKeyId )) as SavedObject; - expect(soEnrollmentKey.attributes.api_key !== 'notencryptedapikey').toBe(true); + + // TODO fix when we encrypt keys again + // expect(soEnrollmentKey.attributes.api_key !== 'notencryptedapikey').toBe(true); expect(soEnrollmentKey.attributes).toMatchObject({ active: false, });