From 6c86cfe5d2929535d221841beab73a48672e1385 Mon Sep 17 00:00:00 2001 From: David Lick Date: Fri, 24 Nov 2023 17:42:21 -0500 Subject: [PATCH] get nsec from ssm --- Makefile | 13 ++++++++++--- README.md | 19 +++++++++++++++++++ index.mjs | 40 ++++++++++++++++++++++++++-------------- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index c11e3d9b..c692016b 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ setup: export EAGER_SERVICE_LOADING=1 \ - SERVICES=lambda,ssm \ + SERVICES=lambda,ssm,kms \ + LAMBDA_EXECUTOR=docker \ AWS_ACCESS_KEY_ID=test \ AWS_SECRET_ACCESS_KEY=test dockerd & @@ -17,6 +18,12 @@ setup: awslocal ssm put-parameter \ --name /rss-nostr-lambda/last-run-time \ --value "2023-11-23T14:46:56Z" \ + --type "String" \ + --overwrite + awslocal ssm put-parameter \ + --name /rss-nostr-lambda/nsec \ + --value "nsec1eg458xl7d3eywld94rpfx8daa6h2hqfqxylmk43dz6ep8q388e2sfe4thg" \ + --type "SecureString" \ --overwrite rm handler.zip @@ -25,7 +32,7 @@ invoke: --region us-east-1 \ --function-name rss-nostr-lambda \ --cli-binary-format raw-in-base64-out \ - --payload '{"feedUrl":"https://nitter.1d4.us/culturaltutor/rss","nostrNsec":"nsec1eg458xl7d3eywld94rpfx8daa6h2hqfqxylmk43dz6ep8q388e2sfe4thg","lastRunTimeParam":"/rss-nostr-lambda/last-run-time"}' \ + --payload '{"feedUrl":"https://nitter.1d4.us/culturaltutor/rss","nostrNsecParam":"/rss-nostr-lambda/nsec","lastRunTimeParam":"/rss-nostr-lambda/last-run-time"}' \ response.json invoke-dryrun: @@ -33,7 +40,7 @@ invoke-dryrun: --region us-east-1 \ --function-name rss-nostr-lambda \ --cli-binary-format raw-in-base64-out \ - --payload '{"dryRun":true,"feedUrl":"https://nitter.1d4.us/culturaltutor/rss","nostrNsec":"nsec1eg458xl7d3eywld94rpfx8daa6h2hqfqxylmk43dz6ep8q388e2sfe4thg","lastRunTimeParam":"/rss-nostr-lambda/last-run-time"}' \ + --payload '{"dryRun":true,"feedUrl":"https://nitter.1d4.us/culturaltutor/rss","nostrNsecParam":"/rss-nostr-lambda/nsec","lastRunTimeParam":"/rss-nostr-lambda/last-run-time"}' \ response.json recreate: diff --git a/README.md b/README.md index 1bd32f71..1c5e9bf6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,22 @@ # rss-nostr-lambda An AWS Lambda function that posts content found in an RSS feed to Nostr. + +## Running Locally + +### Local environment +A [nix-shell development environment](https://nixos.wiki/wiki/Development_environment_with_nix-shell) is configured in this repo. You can use it by running `nix-shell` from within the project directory. This is the easiest way to get started working with this application. + +If you are not running [nix](https://nixos.wiki/wiki/Nix_package_manager) you will need to configure your environment manually. You will need the following dependencies: + +- [awscliv2](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) is used for creating and updating resources in AWS. +- [LocalStack](https://docs.localstack.cloud/getting-started/) is a service that mocks AWS services locally on your machine. +- [Docker](https://docs.docker.com/engine/install/) is used by LocalStack under the hood. You'll need the Docker daemon up and running to use LocalStack. +- [awscli-local](https://github.com/localstack/awscli-local) is a thin wrapper around the awscli which provides the `awslocal` command and allows you to call awscli commands without needing to provide the `--endpoint=http://localhost:xxxx` for LocalStack on each call. + +### Makefile +A [Makefile](https://www.gnu.org/software/make/manual/make.html) has been set up to bundle some of the commands used to set up and use this Lambda. + +## Running in AWS + +This function runs in AWS Lambda and uses SSM to store state like the `last-run-time`. The function will read and write parameters under the `/rss-nostr-lambda` path in Parameter Store; be sure to [grant the proper permissions](https://docs.aws.amazon.com/systems-manager/latest/userguide/ps-integration-lambda-extensions.html) or this function will fail. diff --git a/index.mjs b/index.mjs index c099c3bf..e6807703 100644 --- a/index.mjs +++ b/index.mjs @@ -27,7 +27,7 @@ const htmlToAscii = html => { export const handler = async (event) => { const region = process.env.AWS_REGION || "us-east-1" const feed = event.feedUrl - const privkey = toHexString(event.nostrNsec) + const nostrNsecParam = event.nostrNsecParam const lastRunTimeParam = event.lastRunTimeParam const dryrun = event.dryRun @@ -42,13 +42,12 @@ export const handler = async (event) => { } const ssmClient = new SSMClient({ region }) - const params = { - Name: lastRunTimeParam - } - const ssmGetCommand = new GetParameterCommand(params); + const ssmGetLastRunCommand = new GetParameterCommand({ + Name: lastRunTimeParam + }); let since = new Date() - await ssmClient.send(ssmGetCommand) + await ssmClient.send(ssmGetLastRunCommand) .then(data => { since = data.Parameter.Value }).catch(err => { @@ -56,7 +55,21 @@ export const handler = async (event) => { response.errors.push(err) return response }) - + + const ssmGetNsecCommand = new GetParameterCommand({ + Name: nostrNsecParam, + WithDecryption: true + }) + + let privkey = '' + await ssmClient.send(ssmGetNsecCommand) + .then(data => { + privkey = toHexString(data.Parameter.Value) + }).catch(err => { + console.dir(err) + response.errors.push(err) + return response + }) if (feed === "") { console.dir("feedUrl was not set.") @@ -100,7 +113,6 @@ export const handler = async (event) => { }) if (dryrun) { - console.dir(ndkEvent) response.successes++ continue } @@ -111,12 +123,12 @@ export const handler = async (event) => { if (published) { response.successes++ - params.Name = lastRunTimeParam - params.Value = new Date().toISOString() - params.Type = "String" - params.Overwrite = true - - const ssmPutCommand = new PutParameterCommand(params) + const ssmPutCommand = new PutParameterCommand({ + Name: lastRunTimeParam, + Value: new Date().toISOString(), + Type: "String", + Overwrite: true + }) await ssmClient.send(ssmPutCommand) .catch(err => { console.dir(err)