Skip to content

Commit

Permalink
Merge pull request #118 from KittyCAD/add-dependabot-key-rotation
Browse files Browse the repository at this point in the history
Add option for updating dependabot secrets
  • Loading branch information
iterion authored Oct 12, 2022
2 parents 856dfb3 + a1df759 commit cb4aad5
Show file tree
Hide file tree
Showing 18 changed files with 5,687 additions and 328 deletions.
829 changes: 796 additions & 33 deletions dist/add-issues-to-project/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/add-issues-to-project/index.js.map

Large diffs are not rendered by default.

829 changes: 796 additions & 33 deletions dist/autogen-readme-list/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/autogen-readme-list/index.js.map

Large diffs are not rendered by default.

829 changes: 796 additions & 33 deletions dist/create-openapi-types/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/create-openapi-types/index.js.map

Large diffs are not rendered by default.

829 changes: 796 additions & 33 deletions dist/get-deployment-target-url/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/get-deployment-target-url/index.js.map

Large diffs are not rendered by default.

829 changes: 796 additions & 33 deletions dist/show-visual-diffs-in-comment/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/show-visual-diffs-in-comment/index.js.map

Large diffs are not rendered by default.

841 changes: 803 additions & 38 deletions dist/update-ts-machine-key/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/update-ts-machine-key/index.js.map

Large diffs are not rendered by default.

829 changes: 796 additions & 33 deletions dist/weekly-contributions/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/weekly-contributions/index.js.map

Large diffs are not rendered by default.

117 changes: 63 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions src/update-ts-machine-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ const re = /tskey-(?:auth-)?(?<keyID>.+)-.*/
async function run(): Promise<void> {
const org = github.context.repo.owner
const token = core.getInput('token')
const currentTSMachineKey = core.getInput('current-ts-machine-key')
const tsAPIKey = core.getInput('ts-api-key')
const currentTSMachineKey = core.getInput('current-ts-machine-key')
const tailnet = core.getInput('tailnet')
const secretName = core.getInput('org-secret-name')
const rotationLeadTimeInDays = core.getInput('rotation-lead-time')
const secretType = core.getInput('secret-type')

const rotationLeadTimeInMillis = parseInt(rotationLeadTimeInDays) * 24 * 3600 * 1000
const matches = currentTSMachineKey.match(re)
if (matches === null) {
Expand All @@ -28,6 +30,8 @@ async function run(): Promise<void> {

try {
const octokit = github.getOctokit(token || '')
const secretsClient = (secretType == 'actions') ? octokit.rest.actions : octokit.rest.dependabot

const headers = new Headers({
'Authorization': 'Basic ' + Buffer.from(tsAPIKey + ":").toString('base64'),
})
Expand All @@ -43,11 +47,11 @@ async function run(): Promise<void> {
const dateDiff = keyExpiry - Date.now()
// If we're not about to expire, log and continue
if (dateDiff > rotationLeadTimeInMillis) {
core.info(`Key is not about to expire, expiry: ${data.expires}`)
core.info(`Key is not about to expire (${data.expires})`)
return
}

core.info(`Key is about to expire (${keyExpiry}), creating and uploading a new key.`)
core.info(`Key is about to expire (${data.expires}), creating and uploading a new key.`)

// Reuse capabilities of the existing key
const newKeyCapabilities = { capabilities: data.capabilities }
Expand All @@ -61,7 +65,7 @@ async function run(): Promise<void> {
const machineKeyBytes = Buffer.from(data.key)
core.info(`Generated a new key, ID: ${data.id}`)

const pubKeyResponse = await octokit.rest.actions.getOrgPublicKey({ org, })
const pubKeyResponse = await secretsClient.getOrgPublicKey({ org, })
const pubKeyID = pubKeyResponse.data.key_id
const pubKey = Buffer.from(pubKeyResponse.data.key, 'base64')

Expand All @@ -74,7 +78,7 @@ async function run(): Promise<void> {
const encrypted = Buffer.from(encryptedBytes).toString('base64')

core.info(`Updating ${org} secret ${secretName} to new key`)
octokit.rest.actions.createOrUpdateOrgSecret({
secretsClient.createOrUpdateOrgSecret({
org: org,
secret_name: secretName,
key_id: pubKeyID,
Expand Down
4 changes: 4 additions & 0 deletions update-ts-machine-key/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ inputs:
required: false
default: 7
description: 'Number of days in advance to rotate a given key'
secret-type:
required: false
default: "actions"
description: "Update the machine key for either 'actions' or 'dependabot'"
runs:
using: 'node16'
main: '../dist/update-ts-machine-key/index.js'
Loading

0 comments on commit cb4aad5

Please sign in to comment.