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

100% Statements 13/13
100% Branches 8/8
100% Functions 2/2
100% Lines 13/13

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