Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support allow-unauthorized in aem up #2352

Merged
merged 7 commits into from
May 3, 2024
2 changes: 1 addition & 1 deletion src/abstract-server.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class AbstractServerCommand extends AbstractCommand {
this.emit('stopped', this);
}

async initSeverOptions() {
async initServerOptions() {
if (this._cache) {
await fse.ensureDir(this._cache);
this._project.withCacheDirectory(this._cache);
Expand Down
6 changes: 3 additions & 3 deletions src/fetch-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ const httpProxyHandler = {
};

// create global context that is used by all commands and can be reset for CLI to terminate
export function getFetch(allowUnauthorized) {
const cacheName = allowUnauthorized ? 'insecure' : 'default';
export function getFetch(allowInsecure) {
const cacheName = allowInsecure ? 'insecure' : 'default';
let cache = CONTEXT_CACHE[cacheName];
if (!cache) {
const context = keepAlive({ rejectUnauthorized: !allowUnauthorized });
const context = keepAlive({ rejectUnauthorized: !allowInsecure });
cache = {
context,
fetch: new Proxy(context.fetch, httpProxyHandler),
Expand Down
11 changes: 9 additions & 2 deletions src/import.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export default class ImportCommand extends AbstractServerCommand {
constructor(logger) {
super(logger);
this._importerSubPath = 'tools/importer';
this._allowInsecure = true;
}

withAllowInsecure(value) {
this._allowInsecure = value;
return this;
}

withSkipUI(value) {
Expand Down Expand Up @@ -79,7 +85,8 @@ export default class ImportCommand extends AbstractServerCommand {
this._project = new HelixImportProject()
.withCwd(this.directory)
.withLogger(this._logger)
.withKill(this._kill);
.withKill(this._kill)
.withAllowInsecure(this._allowInsecure);
this.log.info(chalk`{yellow ___ ________ ___ __}`);
this.log.info(chalk`{yellow / | / ____/ |/ / (_)___ ___ ____ ____ _____/ /____ _____}`);
this.log.info(chalk`{yellow / /| | / __/ / /|_/ / / / __ \`__ \\/ __ \\/ __ \\/ ___/ __/ _ \\/ ___/}`);
Expand All @@ -88,7 +95,7 @@ export default class ImportCommand extends AbstractServerCommand {
this.log.info(chalk`{yellow /_/ v${pkgJson.version}}`);
this.log.info('');

await this.initSeverOptions();
await this.initServerOptions();

if (!this._skipUI) {
await this.setupImporterUI();
Expand Down
7 changes: 7 additions & 0 deletions src/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ export default function up() {
type: 'string',
})
.group(['open', 'no-open', 'cache', 'ui-repo', 'skip-ui'], 'AEM Importer Options')
.option('allow-insecure', {
alias: 'allowInsecure',
describe: 'Whether to allow insecure requests to the server',
type: 'boolean',
default: true,
})

.help();
},
Expand All @@ -100,6 +106,7 @@ export default function up() {
// this prevents the window to be opened during integration tests
.withOpen(path.basename(argv.$0) === 'aem' ? argv.open : false)
.withTLS(argv.tlsKey, argv.tlsCert)
.withAllowInsecure(argv.allowInsecure)
.withKill(argv.stopOther)
.withCache(argv.cache)
.withSkipUI(argv.skipUI)
Expand Down
2 changes: 1 addition & 1 deletion src/server/BaseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class BaseServer extends EventEmitter {
let retries = 1;
if (this._project.kill && await utils.checkPortInUse(this._port, this._addr)) {
try {
const res = await getFetch()(`${this._scheme}://${this._addr}:${this._port}/.kill`);
const res = await getFetch(true)(`${this._scheme}://${this._addr}:${this._port}/.kill`);
await res.text();
} catch (e) {
// ignore errors, in case the other server closes connection
Expand Down
7 changes: 5 additions & 2 deletions src/server/HeadHtmlSupport.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export default class HeadHtmlSupport {
.parse(html);
}

constructor({ proxyUrl, directory, log }) {
constructor({
proxyUrl, directory, allowInsecure, log,
}) {
this.remoteHtml = '';
this.remoteDom = null;
this.remoteStatus = 0;
Expand All @@ -85,6 +87,7 @@ export default class HeadHtmlSupport {
this.url = new URL(proxyUrl);
this.url.pathname = '/head.html';
this.filePath = resolve(directory, 'head.html');
this.allowInsecure = allowInsecure;
this.log = log;
}

Expand All @@ -94,7 +97,7 @@ export default class HeadHtmlSupport {
if (this.cookie) {
headers.cookie = this.cookie;
}
const resp = await getFetch()(this.url, {
const resp = await getFetch(this.allowInsecure)(this.url, {
cache: 'no-store',
headers,
});
Expand Down
11 changes: 11 additions & 0 deletions src/server/HelixImportProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import { BaseProject } from './BaseProject.js';
export class HelixImportProject extends BaseProject {
constructor() {
super(HelixImportServer);

this._allowInsecure = true;
}

withAllowInsecure(value) {
this._allowInsecure = value;
return this;
}

get allowInsecure() {
return this._allowInsecure;
}

async start() {
Expand Down
2 changes: 1 addition & 1 deletion src/server/HelixImportServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class HelixImportServer extends BaseServer {
delete headers.host;
delete headers.referer;

const ret = await getFetch(true)(url, {
const ret = await getFetch(ctx.config.allowInsecure)(url, {
method: req.method,
headers,
cache: 'no-store',
Expand Down
11 changes: 11 additions & 0 deletions src/server/HelixProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class HelixProject extends BaseProject {
this._headHtml = null;
this._indexer = null;
this._printIndex = false;
this._allowInsecure = false;
this._file404html = null;
}

Expand All @@ -41,10 +42,19 @@ export class HelixProject extends BaseProject {
return this;
}

withAllowInsecure(value) {
this._allowInsecure = value;
return this;
}

get proxyUrl() {
return this._proxyUrl;
}

get allowInsecure() {
return this._allowInsecure;
}

get indexer() {
return this._indexer;
}
Expand Down Expand Up @@ -77,6 +87,7 @@ export class HelixProject extends BaseProject {
directory: this.directory,
log: this.log,
proxyUrl: this.proxyUrl,
allowInsecure: this.allowInsecure,
});

// register local head in live-reload
Expand Down
4 changes: 2 additions & 2 deletions src/server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const utils = {
if (auth) {
headers.authorization = `Bearer ${auth}`;
}
const res = await getFetch()(uri, {
const res = await getFetch(ctx.config.allowInsecure)(uri, {
cache: 'no-store',
headers,
});
Expand Down Expand Up @@ -258,7 +258,7 @@ window.LiveReloadOptions = {
delete headers.connection;
delete headers['proxy-connection'];
delete headers.host;
const ret = await getFetch()(url, {
const ret = await getFetch(ctx.config.allowInsecure)(url, {
method: req.method,
headers,
cache: 'no-store',
Expand Down
12 changes: 9 additions & 3 deletions src/up.cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export default class UpCommand extends AbstractServerCommand {
return this;
}

withAllowInsecure(value) {
this._allowInsecure = value;
return this;
}

async doStop() {
await super.doStop();
if (this._watcher) {
Expand Down Expand Up @@ -65,7 +70,8 @@ export default class UpCommand extends AbstractServerCommand {
.withLiveReload(this._liveReload)
.withLogger(this._logger)
.withKill(this._kill)
.withPrintIndex(this._printIndex);
.withPrintIndex(this._printIndex)
.withAllowInsecure(this._allowInsecure);
this.log.info(chalk`{yellow ___ ________ ___ __ __ v${pkgJson.version}}`);
this.log.info(chalk`{yellow / | / ____/ |/ / _____(_)___ ___ __ __/ /___ _/ /_____ _____}`);
this.log.info(chalk`{yellow / /| | / __/ / /|_/ / / ___/ / __ \`__ \\/ / / / / __ \`/ __/ __ \\/ ___/}`);
Expand All @@ -79,7 +85,7 @@ export default class UpCommand extends AbstractServerCommand {
await this.verifyUrl(this._gitUrl, ref);
}
this._project.withProxyUrl(this._url);
await this.initSeverOptions();
await this.initServerOptions();

try {
await this._project.init();
Expand Down Expand Up @@ -111,7 +117,7 @@ export default class UpCommand extends AbstractServerCommand {
// "testProperty": "header";
// }
const configUrl = `https://admin.hlx.page/sidekick/${gitUrl.owner}/${gitUrl.repo}/main/config.json`;
const configResp = await getFetch()(configUrl);
const configResp = await getFetch(this._allowInsecure)(configUrl);
let previewHostBase = 'hlx.page';
if (configResp.ok) {
// this is best effort for now
Expand Down
7 changes: 7 additions & 0 deletions src/up.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ export default function up() {
type: 'string',
})
.group(['url', 'livereload', 'no-livereload', 'open', 'no-open', 'print-index', 'cache'], 'AEM Options')
.option('allow-insecure', {
alias: 'allowInsecure',
describe: 'Whether to allow insecure requests to the server',
type: 'boolean',
default: false,
})
Comment on lines +95 to +100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also add this to import.js ?


.help();
},
Expand All @@ -112,6 +118,7 @@ export default function up() {
.withLiveReload(argv.livereload)
.withUrl(argv.url)
.withPrintIndex(argv.printIndex)
.withAllowInsecure(argv.allowInsecure)
.withKill(argv.stopOther)
.withCache(argv.alphaCache)
.run();
Expand Down
1 change: 1 addition & 0 deletions test/import-cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('hlx import', () => {
mockImport.withCache.returnsThis();
mockImport.withSkipUI.returnsThis();
mockImport.withUIRepo.returnsThis();
mockImport.withAllowInsecure.returnsThis();
mockImport.run.returnsThis();
cli = (await new CLI().initCommands()).withCommandExecutor('import', mockImport);
});
Expand Down
7 changes: 7 additions & 0 deletions test/up-cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('hlx up', () => {
mockUp.withBindAddr.returnsThis();
mockUp.withUrl.returnsThis();
mockUp.withPrintIndex.returnsThis();
mockUp.withAllowInsecure.returnsThis();
mockUp.withKill.returnsThis();
mockUp.withCache.returnsThis();
mockUp.run.returnsThis();
Expand Down Expand Up @@ -121,6 +122,12 @@ describe('hlx up', () => {
sinon.assert.calledOnce(mockUp.run);
});

it('hlx up can enable allow insecure', async () => {
await cli.run(['up', '--allow-insecure']);
sinon.assert.calledWith(mockUp.withAllowInsecure, true);
sinon.assert.calledOnce(mockUp.run);
});

it('hlx up can enable cache', async () => {
await cli.run(['up', '--alpha-cache', '.cache/']);
sinon.assert.calledWith(mockUp.withCache, '.cache/');
Expand Down
Loading