-
Notifications
You must be signed in to change notification settings - Fork 885
/
artifact.js
471 lines (393 loc) · 14.2 KB
/
artifact.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
const fetch = require('node-fetch');
const AbortController = require('abort-controller');
const fs = require('fs');
const { promisify } = require('util');
const { pipeline, Transform } = require('stream');
const chalk = require('chalk');
const { createHash } = require('crypto');
const path = require('path');
const asyncPipeline = promisify(pipeline);
const SUPPORTED_PLATFORMS = ['linux', 'windows', 'darwin'];
const DAILY_SNAPSHOTS_BASE_URL = 'https://artifacts.opensearch.org/snapshots/core/opensearch';
// TODO: [RENAMEME] currently do not have an existing replacement
// issue: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/475
const PERMANENT_SNAPSHOTS_BASE_URL = '';
// Since we do not have a manifest URL, limiting how many RC checks to 5 should be more than enough
const MAX_RC_CHECK = 5;
const { cache } = require('./utils');
const { resolveCustomSnapshotUrl } = require('./custom_snapshots');
const { createCliError, isCliError } = require('./errors');
function getChecksumType(checksumUrl) {
if (checksumUrl.endsWith('.sha512')) {
return 'sha512';
}
throw new Error(`unable to determine checksum type: ${checksumUrl}`);
}
function headersToString(headers, indent = '') {
return [...headers.entries()].reduce(
(acc, [key, value]) => `${acc}\n${indent}${key}: ${value}`,
''
);
}
async function retry(log, fn) {
async function doAttempt(attempt) {
try {
return await fn();
} catch (error) {
if (isCliError(error) || attempt >= 5) {
throw error;
}
log.warning('...failure, retrying in 5 seconds:', error.message);
await new Promise((resolve) => setTimeout(resolve, 5000));
log.info('...retrying');
return await doAttempt(attempt + 1);
}
}
return await doAttempt(1);
}
// Setting this flag provides an easy way to run the latest un-promoted snapshot without having to look it up
function shouldUseUnverifiedSnapshot() {
return !!process.env.OSD_OPENSEARCH_SNAPSHOT_USE_UNVERIFIED;
}
// Setting this flag provides an easy way to skip comparing the checksum
function skipVerifyChecksum() {
return !!process.env.OSD_SNAPSHOT_SKIP_VERIFY_CHECKSUM;
}
async function fetchSnapshotManifest(url, log) {
log.info('Downloading snapshot manifest from %s', chalk.bold(url));
const abc = new AbortController();
const resp = await retry(log, async () => await fetch(url, { signal: abc.signal }));
const json = await resp.text();
return { abc, resp, json };
}
async function verifySnapshotUrl(url, log) {
log.info('Verifying snapshot URL at %s', chalk.bold(url));
const abc = new AbortController();
const resp = await retry(
log,
async () => await fetch(url, { signal: abc.signal }, { method: 'HEAD' })
);
return { abc, resp };
}
/**
* @deprecated This method should not be used, uses logic for resources we do not have access to.
*/
async function getArtifactSpecForSnapshot(urlVersion, license, log) {
const desiredVersion = urlVersion.replace('-SNAPSHOT', '');
const desiredLicense = license === 'oss' ? 'oss' : 'default';
const customManifestUrl = process.env.OPENSEARCH_SNAPSHOT_MANIFEST;
const primaryManifestUrl = `${DAILY_SNAPSHOTS_BASE_URL}/${desiredVersion}/manifest-latest${
shouldUseUnverifiedSnapshot() ? '' : '-verified'
}.json`;
const secondaryManifestUrl = `${PERMANENT_SNAPSHOTS_BASE_URL}/${desiredVersion}/manifest.json`;
let { abc, resp, json } = await fetchSnapshotManifest(
customManifestUrl || primaryManifestUrl,
log
);
if (!customManifestUrl && !shouldUseUnverifiedSnapshot() && resp.status === 404) {
log.info('Daily snapshot manifest not found, falling back to permanent manifest');
({ abc, resp, json } = await fetchSnapshotManifest(secondaryManifestUrl, log));
}
if (resp.status === 404) {
abc.abort();
throw createCliError(`Snapshots for ${desiredVersion} are not available`);
}
if (!resp.ok) {
abc.abort();
throw new Error(`Unable to read snapshot manifest: ${resp.statusText}\n ${json}`);
}
const manifest = JSON.parse(json);
const platform = process.platform === 'win32' ? 'windows' : process.platform;
const arch = process.arch === 'arm64' ? 'arm64' : 'x64';
const archive = manifest.archives.find(
(archive) =>
archive.version === desiredVersion &&
archive.platform === platform &&
archive.license === desiredLicense &&
archive.architecture === arch
);
if (!archive) {
throw createCliError(
`Snapshots for ${desiredVersion} are available, but couldn't find an artifact in the manifest for [${desiredVersion}, ${desiredLicense}, ${platform}]`
);
}
return {
url: archive.url,
checksumUrl: archive.url + '.sha512',
checksumType: 'sha512',
filename: archive.filename,
};
}
async function getArtifactSpecForSnapshotFromUrl(urlVersion, log) {
const desiredVersion = urlVersion.replace('-SNAPSHOT', '');
if (process.env.OPENSEARCH_SNAPSHOT_MANIFEST) {
return await getArtifactSpecForSnapshot(urlVersion, 'oss', log);
}
// [RENAMEME] Need replacement for other platforms.
// issue: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/475
const platform = process.platform === 'win32' ? 'windows' : process.platform;
const arch = process.arch === 'arm64' ? 'arm64' : 'x64';
const extension = process.platform === 'win32' ? 'zip' : 'tar.gz';
if (!SUPPORTED_PLATFORMS.includes(platform)) {
throw createCliError(`Snapshots are only available for Linux, Windows, and Darwin`);
}
const latestUrl = `${DAILY_SNAPSHOTS_BASE_URL}/${desiredVersion}-SNAPSHOT`;
const latestFile = `opensearch-min-${desiredVersion}-SNAPSHOT-${platform}-${arch}-latest.${extension}`;
const completeLatestUrl = `${latestUrl}/${latestFile}`;
let { abc, resp } = await verifySnapshotUrl(completeLatestUrl, log);
if (resp.ok) {
return {
url: completeLatestUrl,
checksumUrl: completeLatestUrl + '.sha512',
checksumType: 'sha512',
filename: latestFile,
};
}
log.info(
'Daily general-availability snapshot URL not found for current version, falling back to release-candidate snapshot URL.'
);
let completeUrl = null;
let snapshotFile = null;
/**
* TODO: This might not be relevant anymore. After a few iterations if RC builds are no longer being built
* then we can remove this logic.
*
* This checks and uses an RC if a RC exists at a higher increment than RC1 or it tries to use RC1
* This is in replacement of having a manifest URL, so the exact RC number is unknown but expect it not to be a large number
*/
let rcCheck = MAX_RC_CHECK;
do {
const secondaryLatestUrl = `${DAILY_SNAPSHOTS_BASE_URL}/${desiredVersion}-rc${rcCheck}`;
const secondaryLatestFile = `opensearch-${desiredVersion}-rc${rcCheck}-SNAPSHOT-${platform}-${arch}-latest.tar.gz`;
const completeSecondaryLatestUrl = `${secondaryLatestUrl}/${secondaryLatestFile}`;
({ abc, resp } = await verifySnapshotUrl(completeSecondaryLatestUrl, log));
if (resp.ok) {
completeUrl = completeSecondaryLatestUrl;
snapshotFile = secondaryLatestFile;
break;
}
} while (rcCheck-- >= 1);
if (resp.status === 404 || !completeUrl || !snapshotFile) {
abc.abort();
throw createCliError(`Snapshots for ${desiredVersion} are not available`);
}
if (!resp.ok) {
abc.abort();
throw new Error(`Unable to read snapshot url: ${resp.statusText}`);
}
return {
url: completeUrl,
checksumUrl: completeUrl + '.sha512',
checksumType: 'sha512',
filename: snapshotFile,
};
}
exports.Artifact = class Artifact {
/**
* Fetch an Artifact from the Artifact API for a license level and version.
* Only OSS license should be used but the param was left to mitigate impact
* until a later iteration.
*
* TODO: [RENAMEME] remove license param
* issue: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/475
*
* @param {('oss')} license
* @param {string} version
* @param {ToolingLog} log
*/
static async getSnapshot(license, version, log) {
const urlVersion = `${encodeURIComponent(version)}-SNAPSHOT`;
const customSnapshotArtifactSpec = resolveCustomSnapshotUrl(urlVersion, license);
if (customSnapshotArtifactSpec) {
return new Artifact(customSnapshotArtifactSpec, log);
}
const artifactSpec = await getArtifactSpecForSnapshotFromUrl(urlVersion, log);
return new Artifact(artifactSpec, log);
}
/**
* Fetch an Artifact from the OpenSearch past releases url
* @param {string} url
* @param {ToolingLog} log
*/
static async getArchive(url, log) {
const shaUrl = `${url}.sha512`;
const artifactSpec = {
url: url,
filename: path.basename(url),
checksumUrl: shaUrl,
checksumType: getChecksumType(shaUrl),
};
return new Artifact(artifactSpec, log);
}
constructor(spec, log) {
this._spec = spec;
this._log = log;
}
getUrl() {
return this._spec.url;
}
getChecksumUrl() {
return this._spec.checksumUrl;
}
getChecksumType() {
return this._spec.checksumType;
}
getFilename() {
return this._spec.filename;
}
/**
* Download the artifact to disk, skips the download if the cache is
* up-to-date, verifies checksum when downloaded
* @param {string} dest
* @return {Promise<void>}
*/
async download(dest) {
await retry(this._log, async () => {
const cacheMeta = cache.readMeta(dest);
const tmpPath = `${dest}.tmp`;
const artifactResp = await this._download(tmpPath, cacheMeta.etag, cacheMeta.ts);
if (artifactResp.cached) {
return;
}
if (!skipVerifyChecksum()) {
await this._verifyChecksum(artifactResp);
}
// cache the etag for future downloads
cache.writeMeta(dest, { etag: artifactResp.etag });
// rename temp download to the destination location
fs.renameSync(tmpPath, dest);
});
}
/**
* Fetch the artifact with an etag
* @param {string} tmpPath
* @param {string} etag
* @param {string} ts
* @return {{ cached: true }|{ checksum: string, etag: string, first500Bytes: Buffer }}
*/
async _download(tmpPath, etag, ts) {
const url = this.getUrl();
if (etag) {
this._log.info('verifying cache of %s', chalk.bold(url));
} else {
this._log.info('downloading artifact from %s', chalk.bold(url));
}
const abc = new AbortController();
const resp = await fetch(url, {
signal: abc.signal,
headers: {
'If-None-Match': etag,
},
});
if (resp.status === 304) {
this._log.info('etags match, reusing cache from %s', chalk.bold(ts));
abc.abort();
return {
cached: true,
};
}
if (!resp.ok) {
abc.abort();
throw new Error(
`Unable to download opensearch snapshot: ${resp.statusText}${headersToString(
resp.headers,
' '
)}`
);
}
if (etag) {
this._log.info('cache invalid, redownloading');
}
const hash = createHash(this.getChecksumType());
let first500Bytes = Buffer.alloc(0);
let contentLength = 0;
fs.mkdirSync(path.dirname(tmpPath), { recursive: true });
await asyncPipeline(
resp.body,
new Transform({
transform(chunk, encoding, cb) {
contentLength += Buffer.byteLength(chunk);
if (first500Bytes.length < 500) {
first500Bytes = Buffer.concat(
[first500Bytes, chunk],
first500Bytes.length + chunk.length
).slice(0, 500);
}
hash.update(chunk, encoding);
cb(null, chunk);
},
}),
fs.createWriteStream(tmpPath)
);
return {
checksum: hash.digest('hex'),
etag: resp.headers.get('etag'),
contentLength,
first500Bytes,
headers: resp.headers,
};
}
/**
* Verify the checksum of the downloaded artifact with the checksum at checksumUrl
* @param {{ checksum: string, contentLength: number, first500Bytes: Buffer }} artifactResp
* @return {Promise<void>}
*/
async _verifyChecksum(artifactResp) {
this._log.info('downloading artifact checksum from %s', chalk.bold(this.getChecksumUrl()));
const abc = new AbortController();
const resp = await fetch(this.getChecksumUrl(), {
signal: abc.signal,
});
if (!resp.ok) {
abc.abort();
throw new Error(
`Unable to download opensearch checksum: ${resp.statusText}${headersToString(
resp.headers,
' '
)}`
);
}
// in format of stdout from `shasum` cmd, which is `<checksum> <filename>`
const [expectedChecksum] = (await resp.text()).split(' ');
if (artifactResp.checksum !== expectedChecksum) {
const len = Buffer.byteLength(artifactResp.first500Bytes);
const lenString = `${len} / ${artifactResp.contentLength}`;
throw createCliError(
`artifact downloaded from ${this.getUrl()} does not match expected checksum\n` +
` expected: ${expectedChecksum}\n` +
` received: ${artifactResp.checksum}\n` +
` headers: ${headersToString(artifactResp.headers, ' ')}\n` +
` content[${lenString} base64]: ${artifactResp.first500Bytes.toString('base64')}`
);
}
this._log.info('checksum verified');
}
};