From c8c8101c1c45992287f886aef92c7907ed8c9cc9 Mon Sep 17 00:00:00 2001 From: Paul Tavares Date: Thu, 25 Feb 2021 10:28:17 -0500 Subject: [PATCH 1/2] Added index definition for artifacts --- .../plugins/fleet/common/constants/index.ts | 1 + .../services/fleet_server/elastic_index.ts | 2 + .../elasticsearch/fleet_artifacts.json | 47 +++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 x-pack/plugins/fleet/server/services/fleet_server/elasticsearch/fleet_artifacts.json diff --git a/x-pack/plugins/fleet/common/constants/index.ts b/x-pack/plugins/fleet/common/constants/index.ts index d95bc9cf736a6..abf6b3e1cbbd7 100644 --- a/x-pack/plugins/fleet/common/constants/index.ts +++ b/x-pack/plugins/fleet/common/constants/index.ts @@ -27,6 +27,7 @@ export const FLEET_SERVER_INDICES_VERSION = 1; export const FLEET_SERVER_INDICES = [ '.fleet-actions', '.fleet-agents', + '.fleet-artifacts', '.fleet-enrollment-api-keys', '.fleet-policies', '.fleet-policies-leader', diff --git a/x-pack/plugins/fleet/server/services/fleet_server/elastic_index.ts b/x-pack/plugins/fleet/server/services/fleet_server/elastic_index.ts index 15672be756fe2..4b85f753740e3 100644 --- a/x-pack/plugins/fleet/server/services/fleet_server/elastic_index.ts +++ b/x-pack/plugins/fleet/server/services/fleet_server/elastic_index.ts @@ -16,10 +16,12 @@ import ESFleetPoliciesLeaderIndex from './elasticsearch/fleet_policies_leader.js import ESFleetServersIndex from './elasticsearch/fleet_servers.json'; import ESFleetEnrollmentApiKeysIndex from './elasticsearch/fleet_enrollment_api_keys.json'; import EsFleetActionsIndex from './elasticsearch/fleet_actions.json'; +import EsFleetArtifactsIndex from './elasticsearch/fleet_artifacts.json'; const FLEET_INDEXES: Array<[typeof FLEET_SERVER_INDICES[number], any]> = [ ['.fleet-actions', EsFleetActionsIndex], ['.fleet-agents', ESFleetAgentIndex], + ['.fleet-artifacts', EsFleetArtifactsIndex], ['.fleet-enrollment-api-keys', ESFleetEnrollmentApiKeysIndex], ['.fleet-policies', ESFleetPoliciesIndex], ['.fleet-policies-leader', ESFleetPoliciesLeaderIndex], diff --git a/x-pack/plugins/fleet/server/services/fleet_server/elasticsearch/fleet_artifacts.json b/x-pack/plugins/fleet/server/services/fleet_server/elasticsearch/fleet_artifacts.json new file mode 100644 index 0000000000000..01a2c82b71861 --- /dev/null +++ b/x-pack/plugins/fleet/server/services/fleet_server/elasticsearch/fleet_artifacts.json @@ -0,0 +1,47 @@ +{ + "settings": {}, + "mappings": { + "dynamic": false, + "properties": { + "identifier": { + "type": "keyword" + }, + "compressionAlgorithm": { + "type": "keyword", + "index": false + }, + "encryptionAlgorithm": { + "type": "keyword", + "index": false + }, + "encodedSha256": { + "type": "keyword" + }, + "encodedSize": { + "type": "long", + "index": false + }, + "decodedSha256": { + "type": "keyword", + "index": false + }, + "decodedSize": { + "type": "long", + "index": false + }, + "created": { + "type": "date", + "index": false + }, + "packageName": { + "type": "keyword" + }, + "type": { + "type": "keyword" + }, + "body": { + "type": "binary" + } + } + } +} From 045e1504cbfbe76224a6631d04e0cc32cb45bc4d Mon Sep 17 00:00:00 2001 From: Paul Tavares Date: Thu, 25 Feb 2021 11:01:51 -0500 Subject: [PATCH 2/2] Adjusted tests to include the artifacts index --- .../fleet_server/elastic_index.test.ts | 53 +++++++------------ 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/fleet_server/elastic_index.test.ts b/x-pack/plugins/fleet/server/services/fleet_server/elastic_index.test.ts index 96e642ba9884e..310db24b8184d 100644 --- a/x-pack/plugins/fleet/server/services/fleet_server/elastic_index.test.ts +++ b/x-pack/plugins/fleet/server/services/fleet_server/elastic_index.test.ts @@ -14,42 +14,41 @@ import ESFleetPoliciesLeaderIndex from './elasticsearch/fleet_policies_leader.js import ESFleetServersIndex from './elasticsearch/fleet_servers.json'; import ESFleetEnrollmentApiKeysIndex from './elasticsearch/fleet_enrollment_api_keys.json'; import EsFleetActionsIndex from './elasticsearch/fleet_actions.json'; +import EsFleetArtifactsIndex from './elasticsearch/fleet_artifacts.json'; +import { FLEET_SERVER_INDICES } from '../../../common'; -const FLEET_INDEXES_MIGRATION_HASH = { +const FLEET_INDEXES_MIGRATION_HASH: Record = { '.fleet-actions': hash(EsFleetActionsIndex), '.fleet-agents': hash(ESFleetAgentIndex), + '.fleet-artifacts': hash(EsFleetArtifactsIndex), '.fleet-enrollment-apy-keys': hash(ESFleetEnrollmentApiKeysIndex), '.fleet-policies': hash(ESFleetPoliciesIndex), '.fleet-policies-leader': hash(ESFleetPoliciesLeaderIndex), '.fleet-servers': hash(ESFleetServersIndex), }; +const getIndexList = (returnAliases: boolean = false): string[] => { + const response = [...FLEET_SERVER_INDICES]; + + if (returnAliases) { + return response.sort(); + } + + return response.map((index) => `${index}_1`).sort(); +}; + describe('setupFleetServerIndexes ', () => { it('should create all the indices and aliases if nothings exists', async () => { const esMock = elasticsearchServiceMock.createInternalClient(); await setupFleetServerIndexes(esMock); const indexesCreated = esMock.indices.create.mock.calls.map((call) => call[0].index).sort(); - expect(indexesCreated).toEqual([ - '.fleet-actions_1', - '.fleet-agents_1', - '.fleet-enrollment-api-keys_1', - '.fleet-policies-leader_1', - '.fleet-policies_1', - '.fleet-servers_1', - ]); + expect(indexesCreated).toEqual(getIndexList()); const aliasesCreated = esMock.indices.updateAliases.mock.calls .map((call) => (call[0].body as any)?.actions[0].add.alias) .sort(); - expect(aliasesCreated).toEqual([ - '.fleet-actions', - '.fleet-agents', - '.fleet-enrollment-api-keys', - '.fleet-policies', - '.fleet-policies-leader', - '.fleet-servers', - ]); + expect(aliasesCreated).toEqual(getIndexList(true)); }); it('should not create any indices and create aliases if indices exists but not the aliases', async () => { @@ -63,7 +62,6 @@ describe('setupFleetServerIndexes ', () => { [params.index]: { mappings: { _meta: { - // @ts-expect-error migrationHash: FLEET_INDEXES_MIGRATION_HASH[params.index.replace(/_1$/, '')], }, }, @@ -79,14 +77,7 @@ describe('setupFleetServerIndexes ', () => { .map((call) => (call[0].body as any)?.actions[0].add.alias) .sort(); - expect(aliasesCreated).toEqual([ - '.fleet-actions', - '.fleet-agents', - '.fleet-enrollment-api-keys', - '.fleet-policies', - '.fleet-policies-leader', - '.fleet-servers', - ]); + expect(aliasesCreated).toEqual(getIndexList(true)); }); it('should put new indices mapping if the mapping has been updated ', async () => { @@ -115,14 +106,7 @@ describe('setupFleetServerIndexes ', () => { .map((call) => call[0].index) .sort(); - expect(indexesMappingUpdated).toEqual([ - '.fleet-actions_1', - '.fleet-agents_1', - '.fleet-enrollment-api-keys_1', - '.fleet-policies-leader_1', - '.fleet-policies_1', - '.fleet-servers_1', - ]); + expect(indexesMappingUpdated).toEqual(getIndexList()); }); it('should not create any indices or aliases if indices and aliases already exists', async () => { @@ -137,7 +121,6 @@ describe('setupFleetServerIndexes ', () => { [params.index]: { mappings: { _meta: { - // @ts-expect-error migrationHash: FLEET_INDEXES_MIGRATION_HASH[params.index.replace(/_1$/, '')], }, },