Skip to content

Commit

Permalink
[Fleet] Add new index to fleet for artifacts being served out of flee…
Browse files Browse the repository at this point in the history
…t-server (#92860) (#92920)

* Added index definition for artifacts
  • Loading branch information
paul-tavares authored Feb 25, 2021
1 parent 3b0c951 commit 8f072aa
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 35 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/common/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof FLEET_SERVER_INDICES[number], string> = {
'.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 () => {
Expand All @@ -63,7 +62,6 @@ describe('setupFleetServerIndexes ', () => {
[params.index]: {
mappings: {
_meta: {
// @ts-expect-error
migrationHash: FLEET_INDEXES_MIGRATION_HASH[params.index.replace(/_1$/, '')],
},
},
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -137,7 +121,6 @@ describe('setupFleetServerIndexes ', () => {
[params.index]: {
mappings: {
_meta: {
// @ts-expect-error
migrationHash: FLEET_INDEXES_MIGRATION_HASH[params.index.replace(/_1$/, '')],
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}

0 comments on commit 8f072aa

Please sign in to comment.