-
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
8 changed files
with
122 additions
and
3 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
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,44 @@ | ||
const { Site } = require('../models'); | ||
const CFApiClient = require('../utils/cfApiClient'); | ||
|
||
async function rotateBucketKey(site) { | ||
const cfApi = new CFApiClient(); | ||
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); | ||
} | ||
|
||
async function rotateSitesBucketKeys(limit = 20) { | ||
const sites = await Site.findAll({ | ||
order: [['awsBucketKeyUpdatedAt', 'ASC']], | ||
limit, | ||
}); | ||
|
||
return Promise.allSettled(sites.map(site => rotateBucketKey(site))); | ||
} | ||
|
||
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
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/admin-client | ||
path: bash | ||
args: [-c, yarn rotate-bucket-keys] |
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,15 @@ | ||
const { | ||
rotateSitesBucketKeys, | ||
} = require('../api/services/SiteBucketKeyRotator'); | ||
|
||
async function main() { | ||
const rotatedKeyServices = await rotateSitesBucketKeys(); | ||
|
||
// eslint-disable-next-line | ||
rotatedKeyServices.map(result => { | ||
// eslint-disable-next-line | ||
console.log(JSON.stringify(result, null, 2)); | ||
}); | ||
} | ||
|
||
main(); |