Skip to content

Commit

Permalink
[plugin cli] Fix file:/// paths on Windows (#10083)
Browse files Browse the repository at this point in the history
* [plugin cli] Fix file:/// paths on Windows

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

* [plugin cli] Add deprecation warning for file://
  • Loading branch information
jbudz authored Feb 16, 2017
1 parent 7c02f3b commit 9120d17
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
33 changes: 32 additions & 1 deletion src/cli_plugin/install/__tests__/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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');
Expand Down
24 changes: 23 additions & 1 deletion src/cli_plugin/install/download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9120d17

Please sign in to comment.