Skip to content

Commit

Permalink
[chore] upgrade wreck (#36527) (#36552)
Browse files Browse the repository at this point in the history
wreck has been deprecated and will receive no further security updates
in favor of @hapi/wreck
  • Loading branch information
toddself committed May 15, 2019
1 parent 7448f64 commit e9bfc7f
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 161 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -110,6 +110,7 @@
"@elastic/good": "8.1.1-kibana2",
"@elastic/numeral": "2.3.3",
"@elastic/ui-ace": "0.2.3",
"@hapi/wreck": "^15.0.1",
"@kbn/babel-code-parser": "1.0.0",
"@kbn/babel-preset": "1.0.0",
"@kbn/config-schema": "1.0.0",
Expand Down Expand Up @@ -252,7 +253,6 @@
"webpack": "4.23.1",
"webpack-merge": "4.1.4",
"whatwg-fetch": "^3.0.0",
"wreck": "^14.0.2",
"yauzl": "2.7.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/cli_plugin/install/downloaders/http.js
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import Wreck from 'wreck';
import Wreck from '@hapi/wreck';
import Progress from '../progress';
import { createWriteStream } from 'fs';
import HttpProxyAgent from 'http-proxy-agent';
Expand Down
32 changes: 19 additions & 13 deletions src/dev/build/tasks/nodejs/__tests__/download.js
Expand Up @@ -24,7 +24,7 @@ import { readFileSync } from 'fs';
import del from 'del';
import sinon from 'sinon';
import expect from '@kbn/expect';
import Wreck from 'wreck';
import Wreck from '@hapi/wreck';

import { ToolingLog } from '@kbn/dev-utils';
import { download } from '../download';
Expand All @@ -45,12 +45,12 @@ describe('src/dev/build/tasks/nodejs/download', () => {
const log = new ToolingLog({
level: 'verbose',
writeTo: {
write: onLogLine
}
write: onLogLine,
},
});

const FOO_SHA256 = '2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae';
const createSendHandler = (send) => (req, res) => {
const createSendHandler = send => (req, res) => {
res.statusCode = 200;
res.end(send);
};
Expand All @@ -62,7 +62,7 @@ describe('src/dev/build/tasks/nodejs/download', () => {
let server;
let serverUrl;
let nextHandler;
afterEach(() => nextHandler = null);
afterEach(() => (nextHandler = null));

before(async () => {
server = createServer((req, res) => {
Expand All @@ -79,9 +79,9 @@ describe('src/dev/build/tasks/nodejs/download', () => {
new Promise((resolve, reject) => {
server.once('error', reject);
}),
new Promise((resolve) => {
new Promise(resolve => {
server.listen(resolve);
})
}),
]);

serverUrl = `http://localhost:${server.address().port}/`;
Expand All @@ -98,7 +98,7 @@ describe('src/dev/build/tasks/nodejs/download', () => {
log,
url: serverUrl,
destination: TMP_DESTINATION,
sha256: FOO_SHA256
sha256: FOO_SHA256,
});
expect(readFileSync(TMP_DESTINATION, 'utf8')).to.be('foo');
});
Expand All @@ -111,11 +111,13 @@ describe('src/dev/build/tasks/nodejs/download', () => {
log,
url: serverUrl,
destination: TMP_DESTINATION,
sha256: 'bar'
sha256: 'bar',
});
throw new Error('Expected download() to reject');
} catch (error) {
expect(error).to.have.property('message').contain('does not match the expected sha256 checksum');
expect(error)
.to.have.property('message')
.contain('does not match the expected sha256 checksum');
}

try {
Expand Down Expand Up @@ -192,7 +194,9 @@ describe('src/dev/build/tasks/nodejs/download', () => {
});
throw new Error('Expected download() to reject');
} catch (error) {
expect(error).to.have.property('message').contain('Unexpected status code 500');
expect(error)
.to.have.property('message')
.contain('Unexpected status code 500');
expect(reqCount).to.be(6);
}
});
Expand All @@ -211,12 +215,14 @@ describe('src/dev/build/tasks/nodejs/download', () => {
await download({
log,
url: 'http://google.com',
destination: TMP_DESTINATION
destination: TMP_DESTINATION,
});

throw new Error('expected download() to reject');
} catch (error) {
expect(error).to.have.property('message').contain('refusing to download');
expect(error)
.to.have.property('message')
.contain('refusing to download');
}
});
});
Expand Down
10 changes: 3 additions & 7 deletions src/dev/build/tasks/nodejs/download.js
Expand Up @@ -22,7 +22,7 @@ import { dirname } from 'path';

import chalk from 'chalk';
import { createHash } from 'crypto';
import wreck from 'wreck';
import wreck from '@hapi/wreck';
import mkdirp from 'mkdirp';

function tryUnlink(path) {
Expand All @@ -39,9 +39,7 @@ export async function download(options) {
const { log, url, destination, sha256, retries = 0 } = options;

if (!sha256) {
throw new Error(
`sha256 checksum of ${url} not provided, refusing to download.`
);
throw new Error(`sha256 checksum of ${url} not provided, refusing to download.`);
}

// mkdirp and open file outside of try/catch, we don't retry for those errors
Expand All @@ -55,9 +53,7 @@ export async function download(options) {
const response = await wreck.request('GET', url);

if (response.statusCode !== 200) {
throw new Error(
`Unexpected status code ${response.statusCode} when downloading ${url}`
);
throw new Error(`Unexpected status code ${response.statusCode} when downloading ${url}`);
}

const hash = createHash('sha256');
Expand Down
2 changes: 1 addition & 1 deletion src/dev/build/tasks/nodejs/node_shasums.js
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import wreck from 'wreck';
import wreck from '@hapi/wreck';

export async function getNodeShasums(nodeVersion) {
const url = `https://nodejs.org/dist/v${nodeVersion}/SHASUMS256.txt`;
Expand Down
34 changes: 16 additions & 18 deletions src/dev/build/tasks/patch_native_modules_task.js
Expand Up @@ -20,7 +20,7 @@
import { scanCopy, untar, deleteAll } from '../lib';
import { createWriteStream } from 'fs';
import { binaryInfo } from '../../../../x-pack/plugins/code/tasks/nodegit_info';
import wreck from 'wreck';
import wreck from '@hapi/wreck';
import mkdirp from 'mkdirp';
import { dirname, join, basename } from 'path';
import { createPromiseFromStreams } from '../../../legacy/utils/streams';
Expand All @@ -29,15 +29,10 @@ async function download(url, destination, log) {
const response = await wreck.request('GET', url);

if (response.statusCode !== 200) {
throw new Error(
`Unexpected status code ${response.statusCode} when downloading ${url}`
);
throw new Error(`Unexpected status code ${response.statusCode} when downloading ${url}`);
}
mkdirp.sync(dirname(destination));
await createPromiseFromStreams([
response,
createWriteStream(destination)
]);
await createPromiseFromStreams([response, createWriteStream(destination)]);
log.debug('Downloaded ', url);
}

Expand All @@ -46,7 +41,7 @@ async function downloadAndExtractTarball(url, dest, log, retry) {
await download(url, dest, log);
const extractDir = join(dirname(dest), basename(dest, '.tar.gz'));
await untar(dest, extractDir, {
strip: 1
strip: 1,
});
return extractDir;
} catch (e) {
Expand All @@ -66,7 +61,10 @@ async function patchNodeGit(config, log, build, platform) {
const downloadPath = build.resolvePathForPlatform(platform, '.nodegit_binaries', packageName);
const extractDir = await downloadAndExtractTarball(downloadUrl, downloadPath, log, 3);

const destination = build.resolvePathForPlatform(platform, 'node_modules/@elastic/nodegit/build/Release');
const destination = build.resolvePathForPlatform(
platform,
'node_modules/@elastic/nodegit/build/Release'
);
log.debug('Replacing nodegit binaries from ', extractDir);
await deleteAll([destination], log);
await scanCopy({
Expand All @@ -77,15 +75,15 @@ async function patchNodeGit(config, log, build, platform) {
await deleteAll([extractDir, downloadPath], log);
}



export const PatchNativeModulesTask = {
description: 'Patching platform-specific native modules directories',
async run(config, log, build) {
await Promise.all(config.getTargetPlatforms().map(async platform => {
if (!build.isOss()) {
await patchNodeGit(config, log, build, platform);
}
}));
}
await Promise.all(
config.getTargetPlatforms().map(async platform => {
if (!build.isOss()) {
await patchNodeGit(config, log, build, platform);
}
})
);
},
};
77 changes: 39 additions & 38 deletions src/es_archiver/lib/indices/kibana_index.js
@@ -1,28 +1,28 @@
/*
* 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.
*/
* 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.
*/

import _ from 'lodash';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import { toArray } from 'rxjs/operators';
import wreck from 'wreck';
import wreck from '@hapi/wreck';

import { deleteIndex } from './delete_index';
import { collectUiExports } from '../../../legacy/ui/ui_exports';
Expand All @@ -33,10 +33,10 @@ import { findPluginSpecs } from '../../../legacy/plugin_discovery';
* Load the uiExports for a Kibana instance, only load uiExports from xpack if
* it is enabled in the Kibana server.
*/
const getUiExports = async (kibanaUrl) => {
const getUiExports = async kibanaUrl => {
const xpackEnabled = await getKibanaPluginEnabled({
kibanaUrl,
pluginId: 'xpack_main'
pluginId: 'xpack_main',
});

const { spec$ } = await findPluginSpecs({
Expand Down Expand Up @@ -98,7 +98,7 @@ export async function migrateKibanaIndex({ client, log, kibanaUrl }) {

const server = {
log: ([logType, messageType], ...args) => log[logType](`[${messageType}] ${args.join(' ')}`),
config: () => ({ get: (path) => config[path] }),
config: () => ({ get: path => config[path] }),
plugins: { elasticsearch },
};

Expand All @@ -121,21 +121,22 @@ async function loadElasticVersion() {
export async function isSpacesEnabled({ kibanaUrl }) {
return await getKibanaPluginEnabled({
kibanaUrl,
pluginId: 'spaces'
pluginId: 'spaces',
});
}

async function getKibanaPluginEnabled({ pluginId, kibanaUrl }) {
try {
const { payload } = await wreck.get('/api/status', {
baseUrl: kibanaUrl,
json: true
json: true,
});

return payload.status.statuses
.some(({ id }) => id.includes(`plugin:${pluginId}@`));
return payload.status.statuses.some(({ id }) => id.includes(`plugin:${pluginId}@`));
} catch (error) {
throw new Error(`Unable to fetch Kibana status API response from Kibana at ${kibanaUrl}: ${error}`);
throw new Error(
`Unable to fetch Kibana status API response from Kibana at ${kibanaUrl}: ${error}`
);
}
}

Expand All @@ -151,9 +152,9 @@ export async function createDefaultSpace({ index, client }) {
name: 'Default Space',
description: 'This is the default space',
disabledFeatures: [],
_reserved: true
}
}
_reserved: true,
},
},
});
}

Expand All @@ -167,12 +168,12 @@ export async function createDefaultSpace({ index, client }) {
*/
async function fetchKibanaIndices(client) {
const kibanaIndices = await client.cat.indices({ index: '.kibana*', format: 'json' });
const isKibanaIndex = (index) => (/^\.kibana(:?_\d*)?$/).test(index);
const isKibanaIndex = index => /^\.kibana(:?_\d*)?$/.test(index);
return kibanaIndices.map(x => x.index).filter(isKibanaIndex);
}

export async function cleanKibanaIndices({ client, stats, log, kibanaUrl }) {
if (!await isSpacesEnabled({ kibanaUrl })) {
if (!(await isSpacesEnabled({ kibanaUrl }))) {
return await deleteKibanaIndices({
client,
stats,
Expand All @@ -188,17 +189,17 @@ export async function cleanKibanaIndices({ client, stats, log, kibanaUrl }) {
must_not: {
ids: {
type: '_doc',
values: ['space:default']
}
}
}
}
}
values: ['space:default'],
},
},
},
},
},
});

log.warning(
`since spaces are enabled, all objects other than the default space were deleted from ` +
`.kibana rather than deleting the whole index`
`.kibana rather than deleting the whole index`
);

stats.deletedIndex('.kibana');
Expand Down

0 comments on commit e9bfc7f

Please sign in to comment.