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

Add createHandler method for programmatic configuration #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Nevon
Copy link

@Nevon Nevon commented Sep 9, 2020

This PR proposes the addition of a new public method, createHandler. The main use-case for this is to allow the user to inject their own configuration without having to rely on environment variables.

In my particular case, I am storing my secrets in AWS Parameter Store as SecretStrings, as I prefer to not to have to manually do KMS encryption and maintain (and pay for) yet another KMS key, and I don't want to keep my webhook url unencrypted as I'm using this in an open source project where anyone can see the build logs.

Example would be something like this for my case:

const { createHandler, config } = require('lambda-cloudwatch-slack');
const { SSM } = require('aws-sdk')

const parameterStore = new SSM()

let handler
const setupHandler = async () => {
  if (handler != null {
    return handler
  }

  const { Value } = await parameterStore.getParameter({
    Name: process.env.WEBHOOK_URL_PARAMETER,
    WithDecryption: true
  }).promise()

  handler = createHandler({
    ...config,
    unencryptedHookUrl
  })

  return handler
}

exports.handler = async (event, context) => {
  const handler = await setupHandler()
  return handler(event, context)
}

Considered alternatives

I considered writing to process.env['UNENCRYPTED_HOOK_URL'] after looking up the decrypted value, but since config.js is imported immediately, the value will have already been read from the environment before I can look it up.

Note

Since this project doesn't have a test suite, I'm not 100% sure that my code actually works. I haven't had a chance to manually test it yet, as it requires quite a lot of setup. If this PR would be considered to be merged, it will first have to actually be tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant