Skip to content

Commit

Permalink
Add compression type to manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
madirey committed Jul 8, 2020
1 parent 0809b77 commit 2bf4acb
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const compressionAlgorithm = t.keyof({
none: null,
zlib: null,
});
export type CompressionAlgorithm = t.TypeOf<typeof compressionAlgorithm>;

export const encryptionAlgorithm = t.keyof({
none: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,7 @@ describe('manifest_entry', () => {
'/api/endpoint/artifacts/download/endpoint-exceptionlist-windows-v1/5f16e5e338c53e77cfa945c17c11b175c3967bf109aa87131de41fb93b149735',
});
});

// TODO: add test for entry with compression
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import { InternalArtifactSchema } from '../../schemas/artifacts';
import { CompressionAlgorithm } from '../../../../common/endpoint/schema/common';
import { ManifestEntrySchema } from '../../../../common/endpoint/schema/manifest';

export class ManifestEntry {
Expand All @@ -22,6 +23,10 @@ export class ManifestEntry {
return this.artifact.identifier;
}

public getCompressionAlgorithm(): CompressionAlgorithm {
return this.artifact.compressionAlgorithm;
}

public getEncodedSha256(): string {
return this.artifact.encodedSha256;
}
Expand All @@ -48,7 +53,7 @@ export class ManifestEntry {

public getRecord(): ManifestEntrySchema {
return {
compression_algorithm: 'none',
compression_algorithm: this.getCompressionAlgorithm(),
encryption_algorithm: 'none',
decoded_sha256: this.getDecodedSha256(),
decoded_size: this.getDecodedSize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ export class ManifestManager {
manifestSo.version
);

await Promise.all(
manifestSo.attributes.ids.map(async (id) => {
const artifactSo = await this.artifactClient.getArtifact(id);
manifest.addEntry(artifactSo.attributes);
})
);
for (const id of manifestSo.attributes.ids) {
const artifactSo = await this.artifactClient.getArtifact(id);
manifest.addEntry(artifactSo.attributes);
}
return manifest;
} catch (err) {
if (err.output.statusCode !== 404) {
Expand Down Expand Up @@ -202,38 +200,32 @@ export class ManifestManager {
return diff.type === 'delete';
});

await Promise.all(
adds.map(async (diff) => {
const artifact = snapshot.manifest.getArtifact(diff.id);
const compressedArtifact = await compressExceptionList(
Buffer.from(artifact.body, 'base64')
);
artifact.body = compressedArtifact.toString('base64');
artifact.encodedSize = compressedArtifact.byteLength;
artifact.compressionAlgorithm = 'zlib';
artifact.encodedSha256 = createHash('sha256').update(compressedArtifact).digest('hex');

try {
await this.artifactClient.createArtifact(artifact);
} catch (err) {
if (err.status === 409) {
this.logger.debug(`Tried to create artifact ${diff.id}, but it already exists.`);
} else {
throw err;
}
for (const diff of adds) {
const artifact = snapshot.manifest.getArtifact(diff.id);
const compressedArtifact = await compressExceptionList(Buffer.from(artifact.body, 'base64'));
artifact.body = compressedArtifact.toString('base64');
artifact.encodedSize = compressedArtifact.byteLength;
artifact.compressionAlgorithm = 'zlib';
artifact.encodedSha256 = createHash('sha256').update(compressedArtifact).digest('hex');

try {
await this.artifactClient.createArtifact(artifact);
} catch (err) {
if (err.status === 409) {
this.logger.debug(`Tried to create artifact ${diff.id}, but it already exists.`);
} else {
throw err;
}
// Cache the body of the artifact
this.cache.set(diff.id, Buffer.from(artifact.body, 'base64'));
})
);
}
// Cache the body of the artifact
this.cache.set(diff.id, Buffer.from(artifact.body, 'base64'));
}

await Promise.all(
deletes.map(async (diff) => {
await this.artifactClient.deleteArtifact(diff.id);
// TODO: should we delete the cache entry here?
this.logger.info(`Cleaned up artifact ${diff.id}`);
})
);
for (const diff of deletes) {
await this.artifactClient.deleteArtifact(diff.id);
// TODO: should we delete the cache entry here?
this.logger.info(`Cleaned up artifact ${diff.id}`);
}
}

/**
Expand All @@ -252,35 +244,30 @@ export class ManifestManager {
kuery: 'ingest-package-configs.package.name:endpoint',
});

await Promise.all(
items.map(async (packageConfig) => {
const { id, revision, updated_at, updated_by, ...newPackageConfig } = packageConfig;
if (
newPackageConfig.inputs.length > 0 &&
newPackageConfig.inputs[0].config !== undefined
) {
const artifactManifest = newPackageConfig.inputs[0].config.artifact_manifest ?? {
value: {},
};
artifactManifest.value = manifest.toEndpointFormat();
newPackageConfig.inputs[0].config.artifact_manifest = artifactManifest;

try {
await this.packageConfigService.update(this.savedObjectsClient, id, newPackageConfig);
this.logger.debug(
`Updated package config ${id} with manifest version ${manifest.getVersion()}`
);
} catch (err) {
success = false;
this.logger.debug(`Error updating package config ${id}`);
this.logger.error(err);
}
} else {
for (const packageConfig of items) {
const { id, revision, updated_at, updated_by, ...newPackageConfig } = packageConfig;
if (newPackageConfig.inputs.length > 0 && newPackageConfig.inputs[0].config !== undefined) {
const artifactManifest = newPackageConfig.inputs[0].config.artifact_manifest ?? {
value: {},
};
artifactManifest.value = manifest.toEndpointFormat();
newPackageConfig.inputs[0].config.artifact_manifest = artifactManifest;

try {
await this.packageConfigService.update(this.savedObjectsClient, id, newPackageConfig);
this.logger.debug(
`Updated package config ${id} with manifest version ${manifest.getVersion()}`
);
} catch (err) {
success = false;
this.logger.debug(`Package config ${id} has no config.`);
this.logger.debug(`Error updating package config ${id}`);
this.logger.error(err);
}
})
);
} else {
success = false;
this.logger.debug(`Package config ${id} has no config.`);
}
}

paging = page * items.length < total;
page++;
Expand Down

0 comments on commit 2bf4acb

Please sign in to comment.