All files / utils apiKeyCleanup.js

100% Statements 19/19
100% Branches 4/4
100% Functions 4/4
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77        6x 6x                                 8x     8x 9x                   8x 8x     7x 7x                         5x     5x 6x                 6x 6x     5x 5x     6x  
/**
 * @implements UC-APIKEY-006.1
 */
 
const { QueryCommand } = require('@aws-sdk/lib-dynamodb')
const { batchDelete } = require('./batchWrite')
 
/**
 * Permanently deletes the API keys a user created in one flow. A key acts with
 * the roles it was given, independently of its creator's membership, so
 * without this a member who leaves (or is removed from) a flow would leave
 * behind keys that keep working there — and keep being billed to them.
 *
 * Keys are found through `createdByIndex` (all keys of that one user, a small
 * bounded set), not by scanning; the flow filter is applied to that set. Keys
 * they created in other flows are untouched.
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {{ apiKeysTableName: string, userId: string, flowId: string }} params
 * @returns {Promise<number>} how many keys were deleted
 */
async function deleteUserApiKeysInFlow(docClient, { apiKeysTableName, userId, flowId }) {
  const keys = []
  let lastKey
 
  do {
    const result = await docClient.send(
      new QueryCommand({
        TableName: apiKeysTableName,
        IndexName: 'createdByIndex',
        KeyConditionExpression: 'createdBy = :userId',
        FilterExpression: 'flowId = :flowId',
        ExpressionAttributeValues: { ':userId': userId, ':flowId': flowId },
        ExclusiveStartKey: lastKey
      })
    )
    keys.push(...(result.Items ?? []).map((item) => ({ id: item.id })))
    lastKey = result.LastEvaluatedKey
  } while (lastKey)
 
  await batchDelete(docClient, apiKeysTableName, keys)
  return keys.length
}
 
/**
 * Permanently deletes every API key bound to a flow, whoever created it — used
 * when the flow itself is deleted, so no key outlives the flow it was made for.
 * Keys are found through `flowIndex` (a Query, never a Scan).
 *
 * @param {import('@aws-sdk/lib-dynamodb').DynamoDBDocumentClient} docClient
 * @param {{ apiKeysTableName: string, flowId: string }} params
 * @returns {Promise<number>} how many keys were deleted
 */
async function deleteApiKeysForFlow(docClient, { apiKeysTableName, flowId }) {
  const keys = []
  let lastKey
 
  do {
    const result = await docClient.send(
      new QueryCommand({
        TableName: apiKeysTableName,
        IndexName: 'flowIndex',
        KeyConditionExpression: 'flowId = :flowId',
        ExpressionAttributeValues: { ':flowId': flowId },
        ExclusiveStartKey: lastKey
      })
    )
    keys.push(...(result.Items ?? []).map((item) => ({ id: item.id })))
    lastKey = result.LastEvaluatedKey
  } while (lastKey)
 
  await batchDelete(docClient, apiKeysTableName, keys)
  return keys.length
}
 
module.exports = { deleteUserApiKeysInFlow, deleteApiKeysForFlow }