Skip to content

Commit

Permalink
Additional type adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
madirey committed Jul 17, 2020
1 parent 87f8ee4 commit 807f80f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { NewPolicyData } from '../../common/endpoint/types';
import { ManifestManager } from './services/artifacts';
import { Manifest } from './lib/artifacts';
import { reportErrors, ManifestConstants } from './lib/artifacts/common';
import { InternalArtifactSchema } from './schemas/artifacts';
import { InternalArtifactCompleteSchema } from './schemas/artifacts';
import { manifestDispatchSchema } from '../../common/endpoint/schema/manifest';

const getManifest = async (logger: Logger, manifestManager: ManifestManager): Promise<Manifest> => {
Expand Down Expand Up @@ -39,7 +39,7 @@ const getManifest = async (logger: Logger, manifestManager: ManifestManager): Pr
// Persist new artifacts
const artifacts = adds
.map((artifactId) => newManifest.getArtifact(artifactId))
.filter((artifact) => artifact !== undefined) as InternalArtifactSchema[];
.filter((artifact): artifact is InternalArtifactCompleteSchema => artifact !== undefined);
if (artifacts.length !== adds.length) {
throw new Error('Invalid artifact encountered.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { Logger } from 'src/core/server';
import { InternalArtifactSchema } from '../../schemas/artifacts';
import {
InternalArtifactSchema,
InternalArtifactCompleteSchema,
internalArtifactCompleteSchema,
} from '../../schemas/artifacts';

export const ArtifactConstants = {
GLOBAL_ALLOWLIST_NAME: 'endpoint-exceptionlist',
Expand All @@ -22,6 +26,12 @@ export const getArtifactId = (artifact: InternalArtifactSchema) => {
return `${artifact.identifier}-${artifact.decodedSha256}`;
};

export const isCompleteArtifact = (
artifact: InternalArtifactSchema
): artifact is InternalArtifactCompleteSchema => {
return internalArtifactCompleteSchema.is(artifact);
};

export const reportErrors = (logger: Logger, errors: Error[]) => {
errors.forEach((err) => {
logger.error(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import { ManifestSchemaVersion } from '../../../../common/endpoint/schema/common';
import { InternalArtifactSchema } from '../../schemas';
import { InternalArtifactCompleteSchema } from '../../schemas';
import { ManifestConstants, getArtifactId } from './common';
import { Manifest } from './manifest';
import {
Expand All @@ -17,7 +17,7 @@ import {

describe('manifest', () => {
describe('Manifest object sanity checks', () => {
let artifacts: InternalArtifactSchema[] = [];
let artifacts: InternalArtifactCompleteSchema[] = [];
let manifest1: Manifest;
let manifest2: Manifest;
let emptyManifest: Manifest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
InternalArtifactSchema,
InternalManifestSchema,
internalArtifactCompleteSchema,
InternalArtifactCompleteSchema,
} from '../../schemas/artifacts';
import {
manifestSchemaVersion,
Expand Down Expand Up @@ -53,7 +54,7 @@ export class Manifest {
}

public static fromArtifacts(
artifacts: InternalArtifactSchema[],
artifacts: InternalArtifactCompleteSchema[],
schemaVersion: string,
oldManifest: Manifest
): Manifest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { InternalArtifactSchema } from '../../schemas/artifacts';
import { InternalArtifactCompleteSchema } from '../../schemas/artifacts';
import {
getInternalArtifactMock,
getInternalArtifactMockWithDiffs,
Expand All @@ -15,7 +15,7 @@ import { Manifest } from './manifest';

export const getMockArtifacts = async (opts?: { compress: boolean }) => {
return Promise.all(
ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.map<Promise<InternalArtifactSchema>>(
ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.map<Promise<InternalArtifactCompleteSchema>>(
async (os) => {
return getInternalArtifactMock(os, 'v1', opts);
}
Expand All @@ -25,7 +25,7 @@ export const getMockArtifacts = async (opts?: { compress: boolean }) => {

export const getMockArtifactsWithDiff = async (opts?: { compress: boolean }) => {
return Promise.all(
ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.map<Promise<InternalArtifactSchema>>(
ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.map<Promise<InternalArtifactCompleteSchema>>(
async (os) => {
if (os === 'linux') {
return getInternalArtifactMockWithDiffs(os, 'v1');
Expand All @@ -38,7 +38,7 @@ export const getMockArtifactsWithDiff = async (opts?: { compress: boolean }) =>

export const getEmptyMockArtifacts = async (opts?: { compress: boolean }) => {
return Promise.all(
ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.map<Promise<InternalArtifactSchema>>(
ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.map<Promise<InternalArtifactCompleteSchema>>(
async (os) => {
return getEmptyInternalArtifactMock(os, 'v1', opts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../../../../../task_manager/server';
import { EndpointAppContext } from '../../types';
import { reportErrors, ManifestConstants } from './common';
import { InternalArtifactSchema } from '../../schemas/artifacts';
import { InternalArtifactCompleteSchema } from '../../schemas/artifacts';

export const ManifestTaskConstants = {
TIMEOUT: '1m',
Expand Down Expand Up @@ -119,7 +119,7 @@ export class ManifestTask {
// Persist new artifacts
const artifacts = adds
.map((artifactId) => newManifest.getArtifact(artifactId))
.filter((artifact) => artifact !== undefined) as InternalArtifactSchema[];
.filter((artifact): artifact is InternalArtifactCompleteSchema => artifact !== undefined);
if (artifacts.length !== adds.length) {
throw new Error('Invalid artifact encountered.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
import { inflateSync } from 'zlib';
import { savedObjectsClientMock } from 'src/core/server/mocks';
import { createPackageConfigServiceMock } from '../../../../../../ingest_manager/server/mocks';
import { ArtifactConstants, ManifestConstants, ExceptionsCache } from '../../../lib/artifacts';
import {
ArtifactConstants,
ManifestConstants,
ExceptionsCache,
isCompleteArtifact,
} from '../../../lib/artifacts';
import { getManifestManagerMock } from './manifest_manager.mock';

describe('manifest_manager', () => {
Expand Down Expand Up @@ -61,7 +66,13 @@ describe('manifest_manager', () => {

const newArtifactId = diffs[1].id;
await newManifest.compressArtifact(newArtifactId);
await manifestManager.pushArtifacts([newManifest.getArtifact(newArtifactId)!]); // caches the artifact
const artifact = newManifest.getArtifact(newArtifactId)!;

if (isCompleteArtifact(artifact)) {
await manifestManager.pushArtifacts([artifact]); // caches the artifact
} else {
throw new Error('Artifact is missing a body.');
}

const entry = JSON.parse(inflateSync(cache.get(newArtifactId)! as Buffer).toString());
expect(entry).toEqual({
Expand Down Expand Up @@ -205,7 +216,14 @@ describe('manifest_manager', () => {
const oldArtifactId = diffs[0].id;
const newArtifactId = diffs[1].id;
await newManifest.compressArtifact(newArtifactId);
await manifestManager.pushArtifacts([newManifest.getArtifact(newArtifactId)!]);

const artifact = newManifest.getArtifact(newArtifactId)!;
if (isCompleteArtifact(artifact)) {
await manifestManager.pushArtifacts([artifact]);
} else {
throw new Error('Artifact is missing a body.');
}

await manifestManager.commit(newManifest);
await manifestManager.deleteArtifacts([oldArtifactId]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
getArtifactId,
} from '../../../lib/artifacts';
import {
InternalArtifactSchema,
InternalArtifactCompleteSchema,
internalArtifactCompleteSchema,
} from '../../../schemas/artifacts';
Expand Down Expand Up @@ -78,25 +77,24 @@ export class ManifestManager {
* state of exception-list-agnostic SOs.
*
* @param schemaVersion The schema version of the artifact
* @returns {Promise<InternalArtifactSchema[]>} An array of uncompressed artifacts built from exception-list-agnostic SOs.
* @returns {Promise<InternalArtifactCompleteSchema[]>} An array of uncompressed artifacts built from exception-list-agnostic SOs.
* @throws Throws/rejects if there are errors building the list.
*/
protected async buildExceptionListArtifacts(
schemaVersion: string
): Promise<InternalArtifactSchema[]> {
return ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.reduce<Promise<InternalArtifactSchema[]>>(
async (acc, os) => {
const exceptionList = await getFullEndpointExceptionList(
this.exceptionListClient,
os,
schemaVersion
);
const artifacts = await acc;
const artifact = await buildArtifact(exceptionList, os, schemaVersion);
return Promise.resolve([...artifacts, artifact]);
},
Promise.resolve([])
);
): Promise<InternalArtifactCompleteSchema[]> {
return ArtifactConstants.SUPPORTED_OPERATING_SYSTEMS.reduce<
Promise<InternalArtifactCompleteSchema[]>
>(async (acc, os) => {
const exceptionList = await getFullEndpointExceptionList(
this.exceptionListClient,
os,
schemaVersion
);
const artifacts = await acc;
const artifact = await buildArtifact(exceptionList, os, schemaVersion);
return Promise.resolve([...artifacts, artifact]);
}, Promise.resolve([]));
}

/**
Expand Down Expand Up @@ -130,7 +128,7 @@ export class ManifestManager {
* @param artifacts An InternalArtifactCompleteSchema array representing the artifacts.
* @returns {Promise<Error[]>} Any errors encountered.
*/
public async pushArtifacts(artifacts: InternalArtifactSchema[]): Promise<Error[]> {
public async pushArtifacts(artifacts: InternalArtifactCompleteSchema[]): Promise<Error[]> {
const errors: Error[] = [];
for (const artifact of artifacts) {
if (internalArtifactCompleteSchema.is(artifact)) {
Expand Down

0 comments on commit 807f80f

Please sign in to comment.