From 8dfc6525638bf5b567722417fb842657b7babca2 Mon Sep 17 00:00:00 2001 From: Tom Forbes Date: Wed, 16 Jun 2021 14:29:20 +0100 Subject: [PATCH] Script to recover cancellations --- scripts/recover-cancellations/README.md | 20 ++++++++ scripts/recover-cancellations/decode.sh | 5 ++ .../recover-cancellations/getReminderCode.js | 47 +++++++++++++++++++ scripts/recover-cancellations/package.json | 10 ++++ 4 files changed, 82 insertions(+) create mode 100644 scripts/recover-cancellations/README.md create mode 100755 scripts/recover-cancellations/decode.sh create mode 100644 scripts/recover-cancellations/getReminderCode.js create mode 100644 scripts/recover-cancellations/package.json diff --git a/scripts/recover-cancellations/README.md b/scripts/recover-cancellations/README.md new file mode 100644 index 0000000..09f41d7 --- /dev/null +++ b/scripts/recover-cancellations/README.md @@ -0,0 +1,20 @@ +#### 1. Download logs from cloudwatch + +E.g. + +`export AWS_DEFAULT_REGION=eu-west-1 && awslogs get /aws/lambda/support-reminders-cancel-reminders-PROD --start='2021-06-15 16:00' --end='2021-06-16 09:00' --filter-pattern="invalid input syntax for type uuid" --profile membership > filtered-dump.log` + +#### 2. Extract the base64 codes from the error logs and decode + +`grep "Error: error" filtered-dump.log > errors` + +You can just use vim block edit mode to remove everything except the codes. + +`cat errors | uniq > deduped` + +`./decode.sh deduped` + +#### 3. Lookup actual reminder_codes from postgres and send to cancellation endpoint + +Setup the ssh tunnel to the database bastion, then run: +`node getReminderCode.js ` diff --git a/scripts/recover-cancellations/decode.sh b/scripts/recover-cancellations/decode.sh new file mode 100755 index 0000000..a58c8dd --- /dev/null +++ b/scripts/recover-cancellations/decode.sh @@ -0,0 +1,5 @@ +#!/bin/bash +while IFS= read -r line; do + echo `echo $line | base64 -d` +done < "$1" + diff --git a/scripts/recover-cancellations/getReminderCode.js b/scripts/recover-cancellations/getReminderCode.js new file mode 100644 index 0000000..6c22413 --- /dev/null +++ b/scripts/recover-cancellations/getReminderCode.js @@ -0,0 +1,47 @@ +const pg = require('pg'); +const fs = require('fs'); +const fetch = require('node-fetch'); + +const database = process.argv[2]; +const user = process.argv[3]; +const password = process.argv[4]; +const path = process.argv[5]; + +const pool = new pg.Pool({ + host: 'localhost', + database, + user, + password, + port: 5431, +}); + +const data = fs.readFileSync(path).toString() +data.split('\n').forEach(prefix => { + const query = { + text: ` + WITH codes AS ( + SELECT reminder_code + FROM recurring_reminder_signups + UNION + SELECT reminder_code + FROM one_off_reminder_signups + ) + SELECT reminder_code FROM codes + WHERE + reminder_code::text LIKE($1) + `, + values: [`${prefix}%`], + }; + + pool.query(query) + .then(result => { + console.log(prefix, result.rows[0].reminder_code) + return fetch('https://reminders.support.guardianapis.com/cancel', + { + method: 'POST', + body: JSON.stringify({ reminderCode: result.rows[0].reminder_code} ) + }) + }).then(response => { + console.log('api response', response.status) + }) +}) diff --git a/scripts/recover-cancellations/package.json b/scripts/recover-cancellations/package.json new file mode 100644 index 0000000..15e56be --- /dev/null +++ b/scripts/recover-cancellations/package.json @@ -0,0 +1,10 @@ +{ + "name": "support-reminders", + "dependencies": { + "node-fetch": "^2.1.2", + "pg": "^7.9.0", + "pg-format": "^1.0.4" + }, + "devDependencies": {}, + "scripts": {} +}