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

feat(graphql-model-transformer): add dsql support for sql lambda #3117

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { DsqlSigner } from "@aws-sdk/dsql-signer";

export const generateDSQLAuthToken = async (endpoint: string): Promise<string> => {
const signer = new DsqlSigner({
hostname: endpoint,
});
try {
const token = await signer.getDbConnectAdminAuthToken();
return token;
} catch (error) {
throw error;
sundersc marked this conversation as resolved.
Show resolved Hide resolved
}
}

export const isDSQLHostname = (endpoint: string): boolean => {
return endpoint.includes(".dsql.") && endpoint.endsWith(".on.aws");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SSMClient, GetParameterCommand, GetParameterCommandOutput } from '@aws-
import { GetSecretValueCommand, SecretsManagerClient, GetSecretValueCommandOutput } from '@aws-sdk/client-secrets-manager';
// @ts-ignore
import { DBAdapter, DBConfig, getDBAdapter } from 'rds-query-processor';
import { generateDSQLAuthToken, isDSQLHostname } from './dsql-helpers';

let adapter: DBAdapter;
let ssmClient: SSMClient;
Expand Down Expand Up @@ -52,7 +53,9 @@ const isRetryableError = (error: Error & {code?: string, errno?: string}): boole
// https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html
const mysqlRetryableError = error.errno === '1045';

return postgresRetryableError || mysqlRetryableError;
const dsqlRetryableError = error.code === '08006' && error.message?.includes('access denied');

return postgresRetryableError || mysqlRetryableError || dsqlRetryableError;
}

const createSSMClient = (): void => {
Expand Down Expand Up @@ -195,7 +198,23 @@ const getDBConfig = async (): DBConfig => {

const jsonConnectionString = process.env.connectionString;
if (jsonConnectionString) {
config.connectionString = await retrieveSsmValueFromEnvPaths(jsonConnectionString);
const connectionString = await retrieveSsmValueFromEnvPaths(jsonConnectionString);

// If the host is a DSQL hostname, generate an auth token
const { hostname } = new URL(connectionString);
config.host = decodeURIComponent(hostname);
if (isDSQLHostname(config.host)) {
config.username = 'admin';
config.password = await generateDSQLAuthToken(config.host);
config.engine = 'postgres';
config.port = 5432;
config.database = 'postgres';
return config;
}

// Fall back to the connection string if the host is not a DSQL hostname
delete config.host;
config.connectionString = connectionString;
return config;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dependencies": {
"@aws-sdk/client-secrets-manager": "3.462.0",
"@aws-sdk/client-ssm": "3.624.0",
"@aws-sdk/dsql-signer": "^3.726.1",
"babel-jest": "^29.1.2",
"bestzip": "^2.1.5",
"jest": "^29.1.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ export const createRdsLambdaRole = (
new PolicyStatement({
effect: Effect.ALLOW,
resources: ['*'],
actions: ['ec2:CreateNetworkInterface', 'ec2:DescribeNetworkInterfaces', 'ec2:DeleteNetworkInterface'],
actions: ['ec2:CreateNetworkInterface', 'ec2:DescribeNetworkInterfaces', 'ec2:DeleteNetworkInterface', 'dsql:DbConnectAdmin'],
}),
);

Expand Down
Loading