[plugin cli] Fix file:/// paths on Windows (#10540)

Backports PR #10083

**Commit 1:**
[plugin cli] Fix file:/// paths on Windows

* Original sha: f47bc1e802
* Authored by Jonathan Budzenski <jon@jbudz.me> on 2017-01-26T17:02:24Z

**Commit 2:**
[plugin cli] Stricter path checking, keeps support for file://

* Original sha: 6bb880165a
* Authored by Jonathan Budzenski <jon@jbudz.me> on 2017-01-27T16:04:38Z

**Commit 3:**
[plugin cli] Add deprecation warning for file://

* Original sha: 051fcb1412
* Authored by Jonathan Budzenski <jon@jbudz.me> on 2017-02-14T19:49:19Z
This commit is contained in:
jasper 2017-02-23 13:05:42 -05:00 committed by Jonathan Budzenski
parent eb9fb9231d
commit be5b20e540
2 changed files with 55 additions and 2 deletions

View file

@ -6,7 +6,7 @@ import rimraf from 'rimraf';
import mkdirp from 'mkdirp';
import Logger from '../../lib/logger';
import { UnsupportedProtocolError } from '../../lib/errors';
import { download, _downloadSingle } from '../download';
import { download, _downloadSingle, _getFilePath, _checkFilePathDeprecation } from '../download';
import { join } from 'path';
describe('kibana cli', function () {
@ -133,6 +133,37 @@ describe('kibana cli', function () {
});
describe('_getFilePath', function () {
it('should decode paths', function () {
expect(_getFilePath('Test%20folder/file.zip')).to.equal('Test folder/file.zip');
});
it('should remove the leading slash from windows paths', function () {
const platform = Object.getOwnPropertyDescriptor(process, 'platform');
Object.defineProperty(process, 'platform', { value: 'win32' });
expect(_getFilePath('/C:/foo/bar')).to.equal('C:/foo/bar');
Object.defineProperty(process, 'platform', platform);
});
});
describe('Windows file:// deprecation', function () {
it('should log a warning if a file:// path is used', function () {
const platform = Object.getOwnPropertyDescriptor(process, 'platform');
Object.defineProperty(process, 'platform', { value: 'win32' });
const logger = {
log: sinon.spy()
};
_checkFilePathDeprecation('file://foo/bar', logger);
_checkFilePathDeprecation('file:///foo/bar', logger);
expect(logger.log.callCount).to.be(1);
expect(logger.log.calledWith('Install paths with file:// are deprecated, use file:/// instead')).to.be(true);
Object.defineProperty(process, 'platform', platform);
});
});
describe('download', function () {
it('should loop through bad urls until it finds a good one.', function () {
const filePath = join(__dirname, 'replies/test_plugin.zip');

View file

@ -3,12 +3,34 @@ import downloadLocalFile from './downloaders/file';
import { UnsupportedProtocolError } from '../lib/errors';
import { parse } from 'url';
function _isWindows() {
return /^win/.test(process.platform);
}
export function _getFilePath(filePath, sourceUrl) {
const decodedPath = decodeURI(filePath);
const prefixedDrive = /^\/[a-zA-Z]:/.test(decodedPath);
if (_isWindows() && prefixedDrive) {
return decodedPath.slice(1);
}
return decodedPath;
}
export function _checkFilePathDeprecation(sourceUrl, logger) {
const twoSlashes = /^file:\/\/(?!\/)/.test(sourceUrl);
if (_isWindows() && twoSlashes) {
logger.log('Install paths with file:// are deprecated, use file:/// instead');
}
}
export function _downloadSingle(settings, logger, sourceUrl) {
const urlInfo = parse(sourceUrl);
let downloadPromise;
if (/^file/.test(urlInfo.protocol)) {
downloadPromise = downloadLocalFile(logger, decodeURI(urlInfo.path), settings.tempArchiveFile);
_checkFilePathDeprecation(sourceUrl, logger);
downloadPromise = downloadLocalFile(logger, _getFilePath(urlInfo.path, sourceUrl), settings.tempArchiveFile);
} else if (/^https?/.test(urlInfo.protocol)) {
downloadPromise = downloadHttpFile(logger, sourceUrl, settings.tempArchiveFile, settings.timeout);
} else {