Skip to content

Commit

Permalink
Compose password prompt fix
Browse files Browse the repository at this point in the history
Password in compose command is only requested when necessary, currently in the odd case of setting up a custom fauceet.

fixes #181
  • Loading branch information
fboucquez committed Apr 9, 2021
1 parent 76d246e commit 649fa74
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
8 changes: 1 addition & 7 deletions src/commands/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ export default class Compose extends Command {
public async run(): Promise<void> {
const { flags } = this.parse(Compose);
BootstrapUtils.showBanner();
flags.password = await CommandUtils.resolvePassword(
flags.password,
flags.noPassword,
CommandUtils.passwordPromptDefaultMessage,
true,
);
await new BootstrapService(this.config.root).compose(flags);
await new BootstrapService(this.config.root).compose({ ...flags, composeResolvePassword: !flags.noPassword });
}
}
20 changes: 16 additions & 4 deletions src/service/ComposeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import Logger from '../logger/Logger';
import LoggerFactory from '../logger/LoggerFactory';
import { Addresses, ConfigPreset, DockerCompose, DockerComposeService, DockerServicePreset } from '../model';
import { BootstrapUtils } from './BootstrapUtils';
import { CommandUtils } from './CommandUtils';
import { ConfigLoader } from './ConfigLoader';
// eslint-disable-next-line @typescript-eslint/no-var-requires
export type ComposeParams = { target: string; user?: string; upgrade?: boolean; password?: string };
export type ComposeParams = { target: string; user?: string; upgrade?: boolean; password?: string; composeResolvePassword: boolean };

const logger: Logger = LoggerFactory.getLogger(LogType.System);

Expand All @@ -41,6 +42,7 @@ export interface PortConfiguration {

export class ComposeService {
public static defaultParams: ComposeParams = {
composeResolvePassword: false,
target: BootstrapUtils.defaultTargetFolder,
user: BootstrapUtils.CURRENT_USER,
upgrade: false,
Expand Down Expand Up @@ -383,7 +385,7 @@ export class ComposeService {
stop_signal: 'SIGINT',
environment: {
FAUCET_PRIVATE_KEY:
n.environment?.FAUCET_PRIVATE_KEY || this.getMainAccountPrivateKey(passedAddresses) || '',
n.environment?.FAUCET_PRIVATE_KEY || (await this.getNemesisAccountPrivateKey(passedAddresses)) || '',
NATIVE_CURRENCY_ID: BootstrapUtils.toSimpleHex(
n.environment?.NATIVE_CURRENCY_ID || presetData.currencyMosaicId || '',
),
Expand Down Expand Up @@ -424,8 +426,18 @@ export class ComposeService {
return dockerCompose;
}

private getMainAccountPrivateKey(passedAddresses: Addresses | undefined) {
const addresses = passedAddresses ?? this.configLoader.loadExistingAddresses(this.params.target, this.params.password);
private async getNemesisAccountPrivateKey(passedAddresses: Addresses | undefined) {
const addresses =
passedAddresses ??
this.configLoader.loadExistingAddresses(
this.params.target,
await CommandUtils.resolvePassword(
this.params.password,
!this.params.composeResolvePassword,
CommandUtils.passwordPromptDefaultMessage,
false,
),
);
return addresses?.mosaics?.[0]?.accounts[0].privateKey;
}
}
1 change: 1 addition & 0 deletions test/service/BootstrapServce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('BootstrapService', () => {
const service = new BootstrapService();
const config: StartParams = {
report: false,
composeResolvePassword: false,
preset: Preset.bootstrap,
reset: true,
upgrade: false,
Expand Down
29 changes: 2 additions & 27 deletions test/service/ComposeService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import { BootstrapUtils, ComposeService, ConfigLoader, ConfigService, LinkServic
describe('ComposeService', () => {
const password = '1234';

const assertDockerCompose = async (params: StartParams, expectedComposeFile: string) => {
const assertDockerCompose = async (partialParams: Partial<StartParams>, expectedComposeFile: string) => {
const params = { ...ConfigService.defaultParams, ...ComposeService.defaultParams, ...LinkService.defaultParams, ...partialParams };
const root = '.';
const presetData = new ConfigLoader().createPresetData({ root, password, ...params });
const dockerCompose = await new ComposeService(root, params).run(presetData);
Expand Down Expand Up @@ -68,8 +69,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose testnet dual', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
target: 'target/tests/testnet-dual',
password,
reset: false,
Expand All @@ -81,8 +80,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose testnet api', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
target: 'target/tests/testnet-api',
password,
reset: false,
Expand All @@ -94,8 +91,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose testnet peer', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
target: 'target/tests/testnet-peer',
password,
reset: false,
Expand All @@ -107,8 +102,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose mainnet dual', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
target: 'target/tests/mainnet-dual',
password,
reset: false,
Expand All @@ -120,8 +113,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose mainnet api', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
target: 'target/tests/mainnet-api',
password,
reset: false,
Expand All @@ -133,8 +124,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose mainnet peer', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
target: 'target/tests/mainnet-peer',
password,
reset: false,
Expand All @@ -146,8 +135,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose testnet supernode', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
target: 'target/tests/testnet-supernode',
password,
customPreset: './test/unit-test-profiles/supernode.yml',
Expand All @@ -160,8 +147,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose testnet dual voting', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
target: 'target/tests/ComposeService-testnet-voting',
password,
reset: false,
Expand All @@ -174,8 +159,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose bootstrap default', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
customPresetObject: {
faucets: [
{
Expand All @@ -192,8 +175,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose bootstrap custom compose', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
customPresetObject: {
faucets: [
{
Expand All @@ -212,8 +193,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose bootstrap custom preset', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
customPresetObject: {
faucets: [
{
Expand All @@ -231,8 +210,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose bootstrap full with debug on', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
customPresetObject: {
dockerComposeDebugMode: true,
faucets: [
Expand All @@ -252,8 +229,6 @@ ${BootstrapUtils.toYaml(dockerCompose)}

it('Compose bootstrap repeat', async () => {
const params = {
...ConfigService.defaultParams,
...LinkService.defaultParams,
customPresetObject: {
faucets: [
{
Expand Down
2 changes: 2 additions & 0 deletions test/service/RunService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('RunService', () => {
detached: true,
build: false,
user: 'current',
composeResolvePassword: false,
timeout: 1200,
};

Expand Down Expand Up @@ -62,6 +63,7 @@ describe('RunService', () => {
detached: true,
build: false,
user: 'current',
composeResolvePassword: false,
timeout: 1200,
};

Expand Down

0 comments on commit 649fa74

Please sign in to comment.