-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Auto rotate site bucket keys #153
- Loading branch information
Showing
22 changed files
with
223 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
18.12 | ||
20.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM node:18.15 | ||
FROM node:20.9-bullseye | ||
|
||
WORKDIR /app | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM node:18.15 | ||
FROM node:20.9-bullseye | ||
|
||
WORKDIR /app | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
18.12 | ||
20.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM node:18 | ||
FROM node:20.9-bullseye | ||
|
||
WORKDIR /app | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
const { Site } = require('../models'); | ||
const CFApiClient = require('../utils/cfApiClient'); | ||
const CloudFoundryAuthClient = require('../utils/cfAuthClient'); | ||
|
||
async function rotateBucketKey(site, cfApi) { | ||
const serviceName = site.s3ServiceName; | ||
const serviceBindingName = `${serviceName}-key`; | ||
const serviceInstance = await cfApi.fetchServiceInstance(serviceName); | ||
const credentials = await cfApi | ||
.fetchCredentialBindingsInstance(serviceBindingName) | ||
.catch((error) => { | ||
// Return null to skip credentials delete if not found | ||
// fetchCredentialBindingsInstance throws error | ||
// with name string starting "Not found" when credentials | ||
// do not exist | ||
if (error.name.toLowerCase().trim().startswith('not found')) { | ||
return null; | ||
} | ||
|
||
throw error; | ||
}); | ||
|
||
if (credentials) { | ||
// Delete existing credential service if they exist | ||
await cfApi.deleteServiceInstanceCredentials(credentials.guid); | ||
await cfApi.sleep('3000'); | ||
} | ||
|
||
await cfApi.createServiceKey(serviceInstance.name, serviceInstance.guid); | ||
|
||
const now = new Date(); | ||
await site.update({ awsBucketKeyUpdatedAt: now }); | ||
|
||
return site; | ||
} | ||
|
||
async function rotateSitesBucketKeys({ | ||
limit = 20, username, password, tokenUrl, apiUrl, | ||
}) { | ||
const authClient = new CloudFoundryAuthClient({ username, password, tokenUrl }); | ||
const cfApi = new CFApiClient({ apiUrl, authClient }); | ||
|
||
const sites = await Site.findAll({ | ||
order: [['awsBucketKeyUpdatedAt', 'ASC']], | ||
limit, | ||
}); | ||
|
||
return Promise.allSettled(sites.map(site => rotateBucketKey(site, cfApi))); | ||
} | ||
|
||
module.exports = { | ||
rotateBucketKey, | ||
rotateSitesBucketKeys, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
"license": "CC0-1.0", | ||
"author": "[email protected]", | ||
"engines": { | ||
"node": "^18.x.x" | ||
"node": "^20.x.x" | ||
}, | ||
"scripts": { | ||
"dev": "NODE_ENV=development npx nodemon -r dotenv/config src/index.js", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
platform: linux | ||
inputs: [name: src] | ||
outputs: [name: src] | ||
run: | ||
dir: src | ||
path: bash | ||
args: [-c, node --env-file=.env scripts/rotate-bucket-keys.js] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
migrations/20231027154728-site-aws-bucket-key-updated-at.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const TABLE = 'site'; | ||
const COLUMN_NAME = 'awsBucketKeyUpdatedAt'; | ||
const COLUMN_TYPE = { | ||
type: 'timestamp', | ||
notNull: true, | ||
defaultValue: new String('now()'), | ||
}; | ||
|
||
exports.up = async (db) => { | ||
await db.addColumn(TABLE, COLUMN_NAME, COLUMN_TYPE); | ||
}; | ||
|
||
exports.down = async (db) => { | ||
await db.removeColumn(TABLE, COLUMN_NAME); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const cfenv = require('cfenv'); | ||
const { | ||
rotateSitesBucketKeys, | ||
} = require('../api/services/SiteBucketKeyRotator'); | ||
|
||
const appDeployEnv = process.env.APP_ENV; | ||
|
||
async function main() { | ||
const appEnv = cfenv.getAppEnv(); | ||
const uaaCredentials = appEnv.getServiceCreds(`app-${appDeployEnv}-uaa-client`); | ||
const rotatedKeyServices = await rotateSitesBucketKeys({ | ||
username: uaaCredentials.clientID, | ||
password: uaaCredentials.clientSecret, | ||
tokenUrl: uaaCredentials.tokenURL, | ||
apiUrl: appEnv.app.cf_api, | ||
}); | ||
|
||
const rejected = rotatedKeyServices.filter( | ||
result => result.status === 'rejected' | ||
); | ||
|
||
if (rejected.length > 0) { | ||
// eslint-disable-next-line | ||
console.log('Failed to rotate site keys'); | ||
// eslint-disable-next-line | ||
console.log(JSON.stringify(rejected, null, 2)); | ||
process.exit(1); | ||
} | ||
|
||
process.exit(0); | ||
} | ||
|
||
main(); |
Oops, something went wrong.