Git Product home page Git Product logo

middy-rds's People

Contributors

jimmy89 avatar willfarrell avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

middy-rds's Issues

Integrating with @middy/secrets-manager using internalData

I'm trying to integrate this with @middy/secrets-manager, but setting internalData isn't working.

My DB conn is stored in secrets manager like:

{
	"host": "***.rds.amazonaws.com",
	"port": 3306,
	"user": "***",
	"password": "***",
	"database": "***"
}

Handler is:

const handler = middy()
	.use(
		secretsManager({
			fetchData: {
				dbConn: 'SECRET_NAME',
			},
		}),
	)
	.use(
		rdsMiddleware({
			client: knex,
			config: {
				client: 'mysql2',
			},
			internalData: 'dbConn',
		}),
	)
	.handler(async (event, context) => {
		const { sql } = context;

		// ...
	});

Issue is options.config.connection ends up being:

{
	"dbConn": {
		"host": "***.rds.amazonaws.com",
		"port": 3306,
		"user": "***",
		"password": "***",
		"database": "***"
	},
	"ssl": { ... }
}

Expected:

{
	"host": "***.rds.amazonaws.com",
	"port": 3306,
	"user": "***",
	"password": "***",
	"database": "***",
	"ssl": { ... }
}

const values = await getInternal(options.internalData, request)
options.config.connection = {
...defaultConnection,
...options.config.connection,
...values
}

If I change line 28 to this it works:

      ...values[options.internalData]

Dynamic require of \"crypto\" is not supported

I'm getting the error ""Dynamic require of "crypto" is not supported" when I try to use the middy-rds middleware in a JS module file (".mjs" extension). Is there a way around this, or am I stuck having to use this middlware only in ".js" and ".cjs" files? Here's the full stack trace for context.

{
  "errorType": "Error",
  "errorMessage": "Dynamic require of \"crypto\" is not supported",
  "stack": [
    "Error: Dynamic require of \"crypto\" is not supported",
    "    at file:///var/task/node_modules/middy-rds/clients/pg.mjs:12:9",
    "    at node_modules/uuid/dist/rng.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:2451:42)",
    "    at __require2 (file:///var/task/node_modules/middy-rds/clients/pg.mjs:15:50)",
    "    at node_modules/uuid/dist/v1.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:2536:39)",
    "    at __require2 (file:///var/task/node_modules/middy-rds/clients/pg.mjs:15:50)",
    "    at node_modules/uuid/dist/index.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:2919:37)",
    "    at __require2 (file:///var/task/node_modules/middy-rds/clients/pg.mjs:15:50)",
    "    at node_modules/@aws-sdk/middleware-retry/dist-cjs/StandardRetryStrategy.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:3027:18)",
    "    at __require2 (file:///var/task/node_modules/middy-rds/clients/pg.mjs:15:50)",
    "    at node_modules/@aws-sdk/middleware-retry/dist-cjs/AdaptiveRetryStrategy.js (file:///var/task/node_modules/middy-rds/clients/pg.mjs:3135:35)"
  ]
}

config.port is undefined

I think, it was a mistake in the code for the knex part.

At the example, all configuration (including database port) are inside options.config.connection. But, in the knex file, We have this mistake :

if (!options.config.port) {
throw new Error('port missing')
}

To avoid that, I have to set the port in options.config and options.config.connection

Impossible to load with typescript

Hello, I have a problem when I want to use your library using Typescript

When I try to use it, I have an error TS2307 (it will be fix with @ts-ignore) :
image

Update to use bundled certificate

In /var/runtime/bootstrap it contains:

# If NODE_EXTRA_CA_CERTS is being set by the customer, don't override. Else, include RDS CA
if [ -z "${NODE_EXTRA_CA_CERTS+set}" ];
then
  export NODE_EXTRA_CA_CERTS="/etc/pki/tls/certs/ca-bundle.crt"
fi

cacheExpiry set to 0 triggers db undefined error in middlewareAfter

Update:

Changing the line to await request.context.db.destroy(); resolves the problem.

Original:

Setting cacheExpiry to 0 triggers the error: "Cannot read property 'destroy' of undefined",
This occurs on line:

await promisify(request.context.db.destroy)()

Although the before middleware copies the db in the context, the afterMiddleware does not seems to be able to contain this connection.

The beforeMiddleware code:

Object.assign(request.context, { db: await value }) // await due to fetch being a promise

Below is the code I used for local testing (e.g. with lambda-node):

const mainHandler = middy(baseHandler).use(logger({
  awsContext: true
})).use(normalizer({
  canonical: true
})).use(jsonBodyParser())
  .use(httpErrorHandler());

const connection = {
  database: getEnvVar("dbDatabase"),
  password: getEnvVar("dbPassword"),
  host: getEnvVar("dbHost"),
  user: getEnvVar("dbUsername"),
  port: getEnvVar("dbPort") || 5432,
  ssl: false
}

handler = mainHandler.use(rds({
    cacheExpiry: 0,
    client: knex,
    config: {
      client: "pg",
      connection
    }
  }));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.