mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-24 06:06:00 +00:00
AWS IAM auth for PostgreSQL
This commit is contained in:
@@ -13,6 +13,8 @@ const {
|
|||||||
extractErrorLogData,
|
extractErrorLogData,
|
||||||
} = global.DBGATE_PACKAGES['dbgate-tools'];
|
} = global.DBGATE_PACKAGES['dbgate-tools'];
|
||||||
|
|
||||||
|
let authProxy;
|
||||||
|
|
||||||
const logger = getLogger('postreDriver');
|
const logger = getLogger('postreDriver');
|
||||||
|
|
||||||
pg.types.setTypeParser(1082, 'text', val => val); // date
|
pg.types.setTypeParser(1082, 'text', val => val); // date
|
||||||
@@ -40,22 +42,27 @@ const drivers = driverBases.map(driverBase => ({
|
|||||||
...driverBase,
|
...driverBase,
|
||||||
analyserClass: Analyser,
|
analyserClass: Analyser,
|
||||||
|
|
||||||
async connect({
|
async connect(props) {
|
||||||
engine,
|
const {
|
||||||
server,
|
engine,
|
||||||
port,
|
server,
|
||||||
user,
|
port,
|
||||||
password,
|
user,
|
||||||
database,
|
password,
|
||||||
databaseUrl,
|
database,
|
||||||
useDatabaseUrl,
|
databaseUrl,
|
||||||
ssl,
|
useDatabaseUrl,
|
||||||
isReadOnly,
|
ssl,
|
||||||
authType,
|
isReadOnly,
|
||||||
socketPath,
|
authType,
|
||||||
}) {
|
socketPath,
|
||||||
|
} = props;
|
||||||
let options = null;
|
let options = null;
|
||||||
|
|
||||||
|
if (authType == 'awsIam') {
|
||||||
|
awsIamToken = await authProxy.getAwsIamToken(props);
|
||||||
|
}
|
||||||
|
|
||||||
if (engine == 'redshift@dbgate-plugin-postgres') {
|
if (engine == 'redshift@dbgate-plugin-postgres') {
|
||||||
let url = databaseUrl;
|
let url = databaseUrl;
|
||||||
if (url && url.startsWith('jdbc:redshift://')) {
|
if (url && url.startsWith('jdbc:redshift://')) {
|
||||||
@@ -82,9 +89,9 @@ const drivers = driverBases.map(driverBase => ({
|
|||||||
host: authType == 'socket' ? socketPath || driverBase.defaultSocketPath : server,
|
host: authType == 'socket' ? socketPath || driverBase.defaultSocketPath : server,
|
||||||
port: authType == 'socket' ? null : port,
|
port: authType == 'socket' ? null : port,
|
||||||
user,
|
user,
|
||||||
password,
|
password: awsIamToken || password,
|
||||||
database: extractDbNameFromComposite(database) || 'postgres',
|
database: extractDbNameFromComposite(database) || 'postgres',
|
||||||
ssl,
|
ssl: authType == 'awsIam' ? ssl || { rejectUnauthorized: false } : ssl,
|
||||||
application_name: 'DbGate',
|
application_name: 'DbGate',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -276,7 +283,7 @@ const drivers = driverBases.map(driverBase => ({
|
|||||||
},
|
},
|
||||||
|
|
||||||
getAuthTypes() {
|
getAuthTypes() {
|
||||||
return [
|
const res = [
|
||||||
{
|
{
|
||||||
title: 'Host and port',
|
title: 'Host and port',
|
||||||
name: 'hostPort',
|
name: 'hostPort',
|
||||||
@@ -286,6 +293,13 @@ const drivers = driverBases.map(driverBase => ({
|
|||||||
name: 'socket',
|
name: 'socket',
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
if (authProxy.supportsAwsIam()) {
|
||||||
|
res.push({
|
||||||
|
title: 'AWS IAM',
|
||||||
|
name: 'awsIam',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return res;
|
||||||
},
|
},
|
||||||
|
|
||||||
async listSchemas(dbhan) {
|
async listSchemas(dbhan) {
|
||||||
@@ -313,4 +327,8 @@ const drivers = driverBases.map(driverBase => ({
|
|||||||
},
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
drivers.initialize = dbgateEnv => {
|
||||||
|
authProxy = dbgateEnv.authProxy;
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = drivers;
|
module.exports = drivers;
|
||||||
|
|||||||
@@ -3,4 +3,7 @@ const drivers = require('./drivers');
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
packageName: 'dbgate-plugin-postgres',
|
packageName: 'dbgate-plugin-postgres',
|
||||||
drivers,
|
drivers,
|
||||||
|
initialize(dbgateEnv) {
|
||||||
|
drivers.initialize(dbgateEnv);
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -135,24 +135,31 @@ const postgresDriverBase = {
|
|||||||
databaseUrlPlaceholder: 'e.g. postgresql://user:password@localhost:5432/default_database',
|
databaseUrlPlaceholder: 'e.g. postgresql://user:password@localhost:5432/default_database',
|
||||||
|
|
||||||
showConnectionField: (field, values) => {
|
showConnectionField: (field, values) => {
|
||||||
if (field == 'useDatabaseUrl') return true;
|
const allowedFields = ['useDatabaseUrl', 'authType', 'user', 'isReadOnly', 'useSeparateSchemas'];
|
||||||
if (values.useDatabaseUrl) {
|
|
||||||
return ['databaseUrl', 'isReadOnly'].includes(field);
|
if (values.authType == 'awsIam') {
|
||||||
|
allowedFields.push('awsRegion', 'secretAccessKey', 'accessKeyId');
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
if (values.authType == 'socket') {
|
||||||
[
|
allowedFields.push('socketPath');
|
||||||
'authType',
|
} else {
|
||||||
'user',
|
if (values.useDatabaseUrl) {
|
||||||
'password',
|
allowedFields.push('databaseUrl');
|
||||||
'defaultDatabase',
|
} else {
|
||||||
'singleDatabase',
|
allowedFields.push('server', 'port');
|
||||||
'isReadOnly',
|
}
|
||||||
'useSeparateSchemas',
|
}
|
||||||
].includes(field) ||
|
|
||||||
(values.authType == 'socket' && ['socketPath'].includes(field)) ||
|
if (values.authType != 'awsIam' && values.authType != 'socket') {
|
||||||
(values.authType != 'socket' && ['server', 'port'].includes(field))
|
allowedFields.push('password');
|
||||||
);
|
}
|
||||||
|
|
||||||
|
if (!values.useDatabaseUrl) {
|
||||||
|
allowedFields.push('defaultDatabase', 'singleDatabase');
|
||||||
|
}
|
||||||
|
|
||||||
|
return allowedFields.includes(field);
|
||||||
},
|
},
|
||||||
|
|
||||||
beforeConnectionSave: connection => {
|
beforeConnectionSave: connection => {
|
||||||
@@ -162,6 +169,7 @@ const postgresDriverBase = {
|
|||||||
return {
|
return {
|
||||||
...connection,
|
...connection,
|
||||||
singleDatabase: !!m,
|
singleDatabase: !!m,
|
||||||
|
|
||||||
defaultDatabase: m ? m[1] : null,
|
defaultDatabase: m ? m[1] : null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user