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

Feature: Adding Command Line Support for Password Retrieval #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions src/commands/addConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ interface SSLQuickPickItem extends vscode.QuickPickItem {
ssl: boolean
}

interface PasswordTypeQuickPickItem extends vscode.QuickPickItem {
passwordType: string
}

interface DatabaseQuickPickItem extends vscode.QuickPickItem {
dbname?: string
}
Expand All @@ -23,11 +27,15 @@ const sslOptions: SSLQuickPickItem[] = [
{label: 'Use Secure Connection', ssl: true},
{label: 'Standard Connection', ssl: false}
]
const passwordTypeOptions: PasswordTypeQuickPickItem[] = [
{label: 'Plain text', passwordType: "PLAIN"},
{label: 'Password from command line', passwordType: "CMD"}
]

export class addConnectionCommand extends BaseCommand {

readonly TITLE: string = 'Add Database Connection';
readonly TotalSteps: number = 7;
readonly TotalSteps: number = 8;

async run() {
const state = {port: 5432} as Partial<ConnectionState>;
Expand All @@ -49,7 +57,8 @@ export class addConnectionCommand extends BaseCommand {
user: state.user,
port: state.port,
ssl: state.secure,
database: state.database
database: state.database,
passwordGenerationCommand: state.passwordGenerationCommand
};

connections[id].hasPassword = !!state.password;
Expand Down Expand Up @@ -87,7 +96,20 @@ export class addConnectionCommand extends BaseCommand {
value: (typeof state.user === 'string') ? state.user : '',
validate: async (value) => (!value || !value.trim()) ? 'Username is required' : ''
});
return (input: MultiStepInput) => this.setPassword(input, state);
return (input: MultiStepInput) => this.passwordType(input, state);
}

async passwordType(input: MultiStepInput, state: Partial<ConnectionState>) {
let passwordType = await input.showQuickPick({
title: this.TITLE,
step: input.CurrentStepNumber,
totalSteps: this.TotalSteps,
placeholder: 'Select Passward Type',
ignoreFocusOut: true,
items: passwordTypeOptions,
convert: async (value: PasswordTypeQuickPickItem) => value.passwordType
});
return (input: MultiStepInput) => passwordType === "PLAIN"? this.setPassword(input,state): this.setPasswordGeneratorCommand(input, state) ;
}

async setPassword(input: MultiStepInput, state: Partial<ConnectionState>) {
Expand All @@ -102,6 +124,20 @@ export class addConnectionCommand extends BaseCommand {
value: (typeof state.password === 'string') ? state.password : '',
validate: async (value) => ''
});
return (input: MultiStepInput) => this.setPasswordGeneratorCommand(input, state);
}

async setPasswordGeneratorCommand(input: MultiStepInput, state: Partial<ConnectionState>) {
state.passwordGenerationCommand = await input.showInputBox({
title: this.TITLE,
step: input.CurrentStepNumber,
totalSteps: this.TotalSteps,
prompt: 'If the password is generated by a command, enter the command here. The command should output the password to stdout',
placeholder: 'ex. cat /path/to/password.txt',
ignoreFocusOut: true,
value: (typeof state.passwordGenerationCommand === 'string') ? state.passwordGenerationCommand : '',
validate: async (value) => ''
});
return (input: MultiStepInput) => this.setPort(input, state);
}

Expand Down Expand Up @@ -153,7 +189,8 @@ export class addConnectionCommand extends BaseCommand {
user: state.user,
password: state.password,
port: state.port,
ssl: state.secure
ssl: state.secure,
passwordGenerationCommand: state.passwordGenerationCommand
}, 'postgres');
const res = await connection.query('SELECT datname FROM pg_database WHERE datistemplate = false;');
databases = res.rows.map<DatabaseQuickPickItem>(database => ({label: database.datname, dbname: database.datname}));
Expand Down Expand Up @@ -194,7 +231,8 @@ export class addConnectionCommand extends BaseCommand {
user: state.user,
password: state.password,
port: state.port,
ssl: state.secure
ssl: state.secure,
passwordGenerationCommand: state.passwordGenerationCommand
}, databaseToTry);
connectionOK = true;
} catch(err) {
Expand Down Expand Up @@ -253,5 +291,6 @@ interface ConnectionState {
password: string;
port: number;
database: string;
secure: boolean
secure: boolean;
passwordGenerationCommand: string;
}
1 change: 1 addition & 0 deletions src/common/IConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface IConnection {
multipleStatements?: boolean;
readonly certPath?: string;
ssl?: any;
readonly passwordGenerationCommand?: string;
}
20 changes: 18 additions & 2 deletions src/common/database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as vscode from 'vscode';
import * as path from 'path';
import {execSync} from 'child_process';
// import { Pool, Client, types, ClientConfig } from 'pg';
import { PgClient } from './connection';
import { IConnection } from "./IConnection";
Expand Down Expand Up @@ -65,7 +66,8 @@ export class Database {
port: connection.port,
database: dbname,
multipleStatements: connection.multipleStatements,
certPath: connection.certPath
certPath: connection.certPath,
passwordGenerationCommand: connection.passwordGenerationCommand,
};
}

Expand All @@ -84,7 +86,21 @@ export class Database {
}

let client = new PgClient(connectionOptions);
await client.connect();
try {
await client.connect();
} catch (err) {
if (connection.passwordGenerationCommand) {
try {
const password = execSync(connection.passwordGenerationCommand, { encoding: 'utf-8' });
connectionOptions.password = password.trim();
client = new PgClient(connectionOptions);
await client.connect();
} catch (error) {
throw error
}
}

}
const versionRes = await client.query(`SELECT current_setting('server_version_num') as ver_num;`);
/*
return res.rows.map<ColumnNode>(column => {
Expand Down