All files / proxy/utils/customer-variables/secrets-manager validate-secret.ts

100% Statements 11/11
100% Branches 11/11
100% Functions 3/3
100% Lines 11/11

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      8x     18x 1x         19x       10x 2x     8x 19x 1x 1x     18x      
import type { CustomerVariablesRecord, CustomerVariableReturn } from '../types.ts'
import { CustomerVariableName } from '../types.ts'
 
const allowedKeys = Object.values<string>(CustomerVariableName)
 
function assertIsCustomerVariableValue(value: unknown, key: string): asserts value is CustomerVariableReturn {
  if (typeof value !== 'string' && value !== null && value !== undefined) {
    throw new TypeError(`Secrets Manager secret contains an invalid value for ${key}`)
  }
}
 
function isCustomerVariableName(key: string): key is CustomerVariableName {
  return allowedKeys.includes(key)
}
 
export function validateSecret(obj: unknown): asserts obj is CustomerVariablesRecord {
  if (obj === null || typeof obj !== 'object') {
    throw new TypeError('Secrets Manager secret is not an object')
  }
 
  for (const [key, value] of Object.entries(obj)) {
    if (!isCustomerVariableName(key)) {
      console.warn(`Secrets Manager secret contains an invalid key: ${key}`)
      continue
    }
 
    assertIsCustomerVariableValue(value, key)
  }
}