Skip to content

Added UI option to read password from environment variable and respec… #1495

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

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
79 changes: 23 additions & 56 deletions packages/driver.pg/connection.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@
"SQLTools Driver Credentials",
"Ask on connect",
"Use empty password",
"Save as plaintext in settings"
"Save as plaintext in settings",
"Retrieve from environ"
],
"default": "SQLTools Driver Credentials"
},
"password": {
"title": "Password",
"type": "string",
"minLength": 1
},
"environ": {
"title": "Environ variable name",
"type": "string",
"minLength": 1
}
},
"properties": {
Expand Down Expand Up @@ -122,12 +128,6 @@
]
}
}
},
"ssh": {
"title": "Over SSH",
"type": "string",
"enum": ["Enabled", "Disabled"],
"default": "Disabled"
}
},
"dependencies": {
Expand Down Expand Up @@ -259,63 +259,30 @@
"properties": {
"usePassword": {
"enum": [
"SQLTools Driver Credentials"
"Retrieve from environ"
]
},
"environ": {
"$ref": "#/definitions/environ"
}
}
}
]
},
"ssh": {
"oneOf": [
{
"properties": {
"ssh": {
"enum": ["Disabled"]
}
}
},
"required": [
"environ"
]
},
{
"properties": {
"ssh": {
"enum": ["Enabled"]
},
"sshOptions": {
"type": "object",
"title": "SSH Connection Options",
"properties": {
"host": {
"type": "string",
"title": "Server Address",
"minLength": 1
},
"port": {
"type": "integer",
"title": "Port",
"default": 22,
"minimum": 1
},
"username": {
"type": "string",
"title": "Username",
"minLength": 1
},
"password": {
"type": "string",
"title": "Password"
},
"privateKeyPath": {
"type": "string",
"title": "Private Key File Path"
}
},
"required": ["host", "username"]
"usePassword": {
"enum": [
"SQLTools Driver Credentials"
]
}
},
"required": ["sshOptions"]
}
}
]
}
},
"required": ["connectionMethod"]
"required": [
"connectionMethod"
]
}
15 changes: 14 additions & 1 deletion packages/driver.pg/src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IExtension, IExtensionPlugin, IDriverExtensionApi } from '@sqltools/types';
import { ExtensionContext, extensions, authentication } from 'vscode';
import { DRIVER_ALIASES } from './constants';
import { Connection } from 'pg';
const { publisher, name } = require('../package.json');
const driverName = 'PostgreSQL/Cockroach';
const AUTHENTICATION_PROVIDER = 'sqltools-driver-credentials';
Expand Down Expand Up @@ -54,14 +55,21 @@ export async function activate(extContext: ExtensionContext): Promise<IDriverExt
if (connInfo.usePassword.toString().toLowerCase().includes('ask')) {
connInfo.askForPassword = true;
propsToRemove.push('password');
propsToRemove.push('environ');
} else if (connInfo.usePassword.toString().toLowerCase().includes('empty')) {
connInfo.password = '';
propsToRemove.push('askForPassword');
propsToRemove.push('environ');
} else if (connInfo.usePassword.toString().toLowerCase().includes('save')) {
propsToRemove.push('askForPassword');
propsToRemove.push('environ');
} else if (connInfo.usePassword.toString().toLowerCase().includes('environ')) {
propsToRemove.push('password');
propsToRemove.push('askForPassword');
} else if (connInfo.usePassword.toString().toLowerCase().includes('secure')) {
propsToRemove.push('password');
propsToRemove.push('askForPassword');
propsToRemove.push('environ');
}
}
if (connInfo.connectString) {
Expand Down Expand Up @@ -102,6 +110,8 @@ export async function activate(extContext: ExtensionContext): Promise<IDriverExt
} else if (typeof connInfo.password === 'string') {
delete formData.askForPassword;
formData.usePassword = connInfo.password ? 'Save as plaintext in settings' : 'Use empty password';
} else if (connInfo.environ) {
formData.usePassword = "Retrieve from environ"
} else {
formData.usePassword = 'SQLTools Driver Credentials';
}
Expand All @@ -123,7 +133,7 @@ export async function activate(extContext: ExtensionContext): Promise<IDriverExt
* This hook is called after a connection definition has been fetched
* from settings and is about to be used to connect.
*/
if (connInfo.password === undefined && !connInfo.askForPassword && !connInfo.connectString) {
if (connInfo.password === undefined && !connInfo.askForPassword && !connInfo.connectString && !connInfo.environ) {
const scopes = [connInfo.name, (connInfo.username || "")];
let session = await authentication.getSession(
AUTHENTICATION_PROVIDER,
Expand All @@ -141,6 +151,9 @@ export async function activate(extContext: ExtensionContext): Promise<IDriverExt
connInfo.password = session.accessToken;
}
}
else if (connInfo.password === undefined && !connInfo.askForPassword && !connInfo.connectString && connInfo.environ) {
connInfo.password = process.env[connInfo.environ]
}
return connInfo;
},
driverAliases: DRIVER_ALIASES,
Expand Down