get nsec from ssm

This commit is contained in:
2023-11-24 17:42:21 -05:00
parent a74d7b91b8
commit 6c86cfe5d2
3 changed files with 55 additions and 17 deletions
+10 -3
View File
@@ -1,6 +1,7 @@
setup: setup:
export EAGER_SERVICE_LOADING=1 \ export EAGER_SERVICE_LOADING=1 \
SERVICES=lambda,ssm \ SERVICES=lambda,ssm,kms \
LAMBDA_EXECUTOR=docker \
AWS_ACCESS_KEY_ID=test \ AWS_ACCESS_KEY_ID=test \
AWS_SECRET_ACCESS_KEY=test AWS_SECRET_ACCESS_KEY=test
dockerd & dockerd &
@@ -17,6 +18,12 @@ setup:
awslocal ssm put-parameter \ awslocal ssm put-parameter \
--name /rss-nostr-lambda/last-run-time \ --name /rss-nostr-lambda/last-run-time \
--value "2023-11-23T14:46:56Z" \ --value "2023-11-23T14:46:56Z" \
--type "String" \
--overwrite
awslocal ssm put-parameter \
--name /rss-nostr-lambda/nsec \
--value "nsec1eg458xl7d3eywld94rpfx8daa6h2hqfqxylmk43dz6ep8q388e2sfe4thg" \
--type "SecureString" \
--overwrite --overwrite
rm handler.zip rm handler.zip
@@ -25,7 +32,7 @@ invoke:
--region us-east-1 \ --region us-east-1 \
--function-name rss-nostr-lambda \ --function-name rss-nostr-lambda \
--cli-binary-format raw-in-base64-out \ --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 response.json
invoke-dryrun: invoke-dryrun:
@@ -33,7 +40,7 @@ invoke-dryrun:
--region us-east-1 \ --region us-east-1 \
--function-name rss-nostr-lambda \ --function-name rss-nostr-lambda \
--cli-binary-format raw-in-base64-out \ --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 response.json
recreate: recreate:
+19
View File
@@ -1,3 +1,22 @@
# rss-nostr-lambda # rss-nostr-lambda
An AWS Lambda function that posts content found in an RSS feed to Nostr. 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.
+26 -14
View File
@@ -27,7 +27,7 @@ const htmlToAscii = html => {
export const handler = async (event) => { export const handler = async (event) => {
const region = process.env.AWS_REGION || "us-east-1" const region = process.env.AWS_REGION || "us-east-1"
const feed = event.feedUrl const feed = event.feedUrl
const privkey = toHexString(event.nostrNsec) const nostrNsecParam = event.nostrNsecParam
const lastRunTimeParam = event.lastRunTimeParam const lastRunTimeParam = event.lastRunTimeParam
const dryrun = event.dryRun const dryrun = event.dryRun
@@ -42,13 +42,12 @@ export const handler = async (event) => {
} }
const ssmClient = new SSMClient({ region }) const ssmClient = new SSMClient({ region })
const params = { const ssmGetLastRunCommand = new GetParameterCommand({
Name: lastRunTimeParam Name: lastRunTimeParam
} });
const ssmGetCommand = new GetParameterCommand(params);
let since = new Date() let since = new Date()
await ssmClient.send(ssmGetCommand) await ssmClient.send(ssmGetLastRunCommand)
.then(data => { .then(data => {
since = data.Parameter.Value since = data.Parameter.Value
}).catch(err => { }).catch(err => {
@@ -56,7 +55,21 @@ export const handler = async (event) => {
response.errors.push(err) response.errors.push(err)
return response 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 === "") { if (feed === "") {
console.dir("feedUrl was not set.") console.dir("feedUrl was not set.")
@@ -100,7 +113,6 @@ export const handler = async (event) => {
}) })
if (dryrun) { if (dryrun) {
console.dir(ndkEvent)
response.successes++ response.successes++
continue continue
} }
@@ -111,12 +123,12 @@ export const handler = async (event) => {
if (published) { if (published) {
response.successes++ response.successes++
params.Name = lastRunTimeParam const ssmPutCommand = new PutParameterCommand({
params.Value = new Date().toISOString() Name: lastRunTimeParam,
params.Type = "String" Value: new Date().toISOString(),
params.Overwrite = true Type: "String",
Overwrite: true
const ssmPutCommand = new PutParameterCommand(params) })
await ssmClient.send(ssmPutCommand) await ssmClient.send(ssmPutCommand)
.catch(err => { .catch(err => {
console.dir(err) console.dir(err)