Skip to content

Commit

Permalink
Preserve compressed artifacts when updating manifest (#71196) (#71286)
Browse files Browse the repository at this point in the history
Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
madirey and elasticmachine authored Jul 9, 2020
1 parent c1fd354 commit 95234bc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ describe('manifest', () => {
});

test('Manifest can be created from list of artifacts', async () => {
const manifest = Manifest.fromArtifacts(artifacts, 'v1', ManifestConstants.INITIAL_VERSION);
const oldManifest = new Manifest(
new Date(),
ManifestConstants.SCHEMA_VERSION,
ManifestConstants.INITIAL_VERSION
);
const manifest = Manifest.fromArtifacts(artifacts, 'v1', oldManifest);
expect(
manifest.contains(
'endpoint-exceptionlist-linux-v1-5f16e5e338c53e77cfa945c17c11b175c3967bf109aa87131de41fb93b149735'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,17 @@ export class Manifest {
public static fromArtifacts(
artifacts: InternalArtifactSchema[],
schemaVersion: string,
version: string
oldManifest: Manifest
): Manifest {
const manifest = new Manifest(new Date(), schemaVersion, version);
const manifest = new Manifest(new Date(), schemaVersion, oldManifest.getVersion());
artifacts.forEach((artifact) => {
manifest.addEntry(artifact);
const id = `${artifact.identifier}-${artifact.decodedSha256}`;
const existingArtifact = oldManifest.getArtifact(id);
if (existingArtifact) {
manifest.addEntry(existingArtifact);
} else {
manifest.addEntry(artifact);
}
});
return manifest;
}
Expand Down Expand Up @@ -81,8 +87,8 @@ export class Manifest {
return this.entries;
}

public getArtifact(artifactId: string): InternalArtifactSchema {
return this.entries[artifactId].getArtifact();
public getArtifact(artifactId: string): InternalArtifactSchema | undefined {
return this.entries[artifactId]?.getArtifact();
}

public diff(manifest: Manifest): ManifestDiff[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class ManifestManager {
const newManifest = Manifest.fromArtifacts(
artifacts,
ManifestConstants.SCHEMA_VERSION,
oldManifest.getVersion()
oldManifest
);

// Get diffs
Expand Down Expand Up @@ -202,6 +202,11 @@ export class ManifestManager {

for (const diff of adds) {
const artifact = snapshot.manifest.getArtifact(diff.id);
if (artifact === undefined) {
throw new Error(
`Corrupted manifest detected. Diff contained artifact ${diff.id} not in manifest.`
);
}
const compressedArtifact = await compressExceptionList(Buffer.from(artifact.body, 'base64'));
artifact.body = compressedArtifact.toString('base64');
artifact.encodedSize = compressedArtifact.byteLength;
Expand Down

0 comments on commit 95234bc

Please sign in to comment.