All files / modules/30-secrets/utils SecretField.tsx

100% Statements 15/15
81.82% Branches 18/22
100% Functions 1/1
100% Lines 14/14

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              269x 269x                     269x       187x 124x   63x   63x   59x 59x 59x   1x     63x 63x         63x                
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Shield 1.0.0 license
 * that can be found in the licenses directory at the root of this repository, also available at
 * https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt.
 */
 
import { Scope } from '@common/interfaces/SecretsInterface'
import { getSecretV2Promise, GetSecretV2QueryParams } from 'services/cd-ng'
 
export interface SecretReferenceInterface {
  identifier: string
  name: string
  referenceString: string
  accountIdentifier: string
  orgIdentifier?: string
  projectIdentifier?: string
}
 
export const setSecretField = async (
  secretString: string,
  scopeQueryParams: GetSecretV2QueryParams
): Promise<SecretReferenceInterface | void> => {
  if (!secretString) {
    return undefined
  } else {
    const secretScope = secretString?.indexOf('.') < 0 ? Scope.PROJECT : secretString?.split('.')[0]
 
    switch (secretScope) {
      case Scope.ACCOUNT:
        delete scopeQueryParams.orgIdentifier
        delete scopeQueryParams.projectIdentifier
        break
      case Scope.ORG:
        delete scopeQueryParams.projectIdentifier
    }
 
    const identifier = secretString.indexOf('.') < 0 ? secretString : secretString.split('.')[1]
    const response = await getSecretV2Promise({
      identifier,
      queryParams: scopeQueryParams
    })
 
    return {
      identifier,
      name: response.data?.secret.name || secretString.split('.')[1],
      referenceString: secretString,
      ...scopeQueryParams
    }
  }
}