All files / modules/30-secrets/components/SecretReference SecretReference.tsx

89.66% Statements 52/58
80% Branches 84/105
66.67% Functions 10/15
89.66% Lines 52/58

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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301              285x 285x 285x 285x 285x 285x 285x                 285x 285x 285x 285x 285x 285x 285x 285x 285x   285x   285x         285x 285x 285x 285x                                       285x                         13x               13x 4x     13x                           13x 13x 13x 13x 13x 18x           13x                 285x 10x 10x                     10x             32x                                       285x 35x 34x 34x 34x   34x                   34x       8x 8x                   13x 13x                                                               35x                                                                                                                                                   285x  
/*
 * 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 React, { useState } from 'react'
import { Icon, SelectOption, Text, Button, Container, Layout } from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { Select } from '@blueprintjs/select'
import { MenuItem } from '@blueprintjs/core'
import cx from 'classnames'
import {
  ListSecretsV2QueryParams,
  Failure,
  listSecretsV2Promise,
  SecretDTOV2,
  SecretTextSpecDTO,
  ResponsePageSecretResponseWrapper,
  ConnectorInfoDTO
} from 'services/cd-ng'
import { EntityReference } from '@common/exports'
import { EntityReferenceResponse, getScopeFromDTO } from '@common/components/EntityReference/EntityReference'
import { Scope } from '@common/interfaces/SecretsInterface'
import { useStrings } from 'framework/strings'
import useCreateUpdateSecretModal from '@secrets/modals/CreateSecretModal/useCreateUpdateSecretModal'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import RbacButton from '@rbac/components/Button/Button'
import SecretEmptyState from '../../pages/secrets/secrets-empty-state.png'
import type { SecretFormData, SecretIdentifiers } from '../CreateUpdateSecret/CreateUpdateSecret'
import css from './SecretReference.module.scss'
 
const CustomSelect = Select.ofType<SelectOption>()
export interface SecretRef extends SecretDTOV2 {
  scope: Scope
}
 
export const enum SecretTypeEnum {
  SECRET_TEXT = 'SecretText',
  SSH_KEY = 'SSHKey',
  SECRET_FILE = 'SecretFile'
}
 
interface SceretTypeDropDownProps {
  secretType?: SelectOption
  setSecretType?: (val: SelectOption) => void
}
export interface SecretReferenceProps extends SceretTypeDropDownProps {
  onSelect: (secret: SecretRef) => void
  accountIdentifier: string
  projectIdentifier?: string
  orgIdentifier?: string
  defaultScope?: Scope
  type?: ListSecretsV2QueryParams['type']
  mock?: ResponsePageSecretResponseWrapper
  connectorTypeContext?: ConnectorInfoDTO['type']
  onCancel?: () => void
  handleInlineSSHSecretCreation?: () => void
}
 
const fetchRecords = (
  pageIndex: number,
  setPagedSecretData: (data: ResponsePageSecretResponseWrapper) => void,
  scope: Scope,
  search: string | undefined,
  done: (records: EntityReferenceResponse<SecretRef>[]) => void,
  type: ListSecretsV2QueryParams['type'],
  accountIdentifier: string,
  projectIdentifier?: string,
  orgIdentifier?: string,
  mock?: ResponsePageSecretResponseWrapper,
  connectorTypeContext?: ConnectorInfoDTO['type']
): void => {
  const secretManagerTypes: ConnectorInfoDTO['type'][] = [
    'AwsKms',
    'AzureKeyVault',
    'Vault',
    'AwsSecretManager',
    'GcpKms'
  ]
  let sourceCategory: ListSecretsV2QueryParams['source_category'] | undefined
  if (connectorTypeContext && secretManagerTypes.includes(connectorTypeContext)) {
    sourceCategory = 'SECRET_MANAGER'
  }
 
  listSecretsV2Promise({
    queryParams: {
      accountIdentifier,
      type,
      searchTerm: search?.trim(),
      projectIdentifier: scope === Scope.PROJECT ? projectIdentifier : undefined,
      orgIdentifier: scope === Scope.PROJECT || scope === Scope.ORG ? orgIdentifier : undefined,
      source_category: sourceCategory,
      pageIndex: pageIndex,
      pageSize: 10
    },
    mock
  })
    .then(responseData => {
      Eif (responseData?.data?.content) {
        const secrets = responseData.data.content
        setPagedSecretData(responseData)
        const response: EntityReferenceResponse<SecretRef>[] = []
        secrets.forEach(secret => {
          response.push({
            name: secret.secret.name || '',
            identifier: secret.secret.identifier || '',
            record: { ...secret.secret, scope }
          })
        })
        done(response)
      } else {
        done([])
      }
    })
    .catch((err: Failure) => {
      throw err.message
    })
}
const SelectTypeDropdown: React.FC<SceretTypeDropDownProps> = props => {
  const { getString } = useStrings()
  const secretTypeOptions: SelectOption[] = [
    {
      label: getString('secrets.secret.labelText'),
      value: 'SecretText'
    },
    {
      label: getString('secrets.secret.labelFile'),
      value: 'SecretFile'
    }
  ]
 
  return (
    <Container flex={{ alignItems: 'baseline' }}>
      <Text margin={{ left: 'medium', right: 'xsmall' }}>{getString('secrets.secret.labelSecretType')}</Text>
      <CustomSelect
        items={secretTypeOptions}
        filterable={false}
        itemRenderer={(item, { handleClick }) => (
          <MenuItem className={css.popoverWidth} key={item.value as string} text={item.label} onClick={handleClick} />
        )}
        onItemSelect={item => {
          props.setSecretType?.(item)
        }}
        popoverProps={{ minimal: true, popoverClassName: css.popoverWidth }}
      >
        <Button
          className={css.selectButton}
          width={60}
          inline
          minimal
          rightIcon="chevron-down"
          text={props.secretType?.label}
        />
      </CustomSelect>
    </Container>
  )
}
 
const SecretReference: React.FC<SecretReferenceProps> = props => {
  const { defaultScope, accountIdentifier, projectIdentifier, orgIdentifier, type, mock, connectorTypeContext } = props
  const { getString } = useStrings()
  const [pagedSecretData, setPagedSecretData] = useState<ResponsePageSecretResponseWrapper>({})
  const [pageNo, setPageNo] = useState(0)
 
  const { openCreateSecretModal } = useCreateUpdateSecretModal({
    onSuccess: data => {
      props.onSelect({
        ...data,
        spec: {},
        scope: getScopeFromDTO<SecretFormData>(data)
      })
    }
  })
 
  return (
    <Container className={css.secretRefContainer}>
      <EntityReference<SecretRef>
        onSelect={(secret, scope) => {
          secret.scope = scope
          props.onSelect(secret)
        }}
        defaultScope={defaultScope}
        noDataCard={{
          image: SecretEmptyState,
          message: getString('secrets.secret.noSecretsFound'),
          containerClassName: css.noDataCardContainerSecret,
          className: css.noDataCardContainerSecret
        }}
        fetchRecords={(scope, done, search, page = 0) => {
          const selectedType = type || (props.secretType?.value as SecretDTOV2['type'])
          fetchRecords(
            page,
            setPagedSecretData,
            scope,
            search,
            done,
            selectedType,
            accountIdentifier,
            projectIdentifier,
            orgIdentifier,
            mock,
            connectorTypeContext
          )
        }}
        projectIdentifier={projectIdentifier}
        orgIdentifier={orgIdentifier}
        onCancel={props.onCancel}
        noRecordsText={getString('secrets.secret.noSecretsFound')}
        searchInlineComponent={
          !type ? <SelectTypeDropdown secretType={props.secretType} setSecretType={props.setSecretType} /> : undefined
        }
        input={!type ? type || (props.secretType?.value as SecretDTOV2['type']) : undefined}
        renderTabSubHeading
        pagination={{
          itemCount: pagedSecretData?.data?.totalItems || 0,
          pageSize: pagedSecretData?.data?.pageSize || 10,
          pageCount: pagedSecretData?.data?.totalPages || -1,
          pageIndex: pageNo || 0,
          gotoPage: pageIndex => setPageNo(pageIndex)
        }}
        disableCollapse={true}
        recordRender={({ item, selectedScope, selected }) => {
          return (
            <>
              <Layout.Horizontal className={css.item} flex={{ alignItems: 'center', justifyContent: 'space-between' }}>
                <Layout.Horizontal spacing="medium" className={css.leftInfo}>
                  <Icon
                    className={cx(css.iconCheck, { [css.iconChecked]: selected })}
                    size={14}
                    name="pipeline-approval"
                  />
                  <Layout.Horizontal flex={{ alignItems: 'center' }}>
                    {item.record.type === SecretTypeEnum.SECRET_TEXT ||
                    item.record.type === SecretTypeEnum.SECRET_FILE ? (
                      <Icon
                        name={item.record.type === SecretTypeEnum.SECRET_TEXT ? 'text' : 'file'}
                        size={24}
                        className={css.secretIcon}
                      />
                    ) : null}
                    <Layout.Vertical padding={{ left: 'small' }}>
                      <Text lineClamp={1} font={{ weight: 'bold' }} color={Color.BLACK}>
                        {item.record.name}
                      </Text>
                      {item.record.type === SecretTypeEnum.SECRET_TEXT ||
                      item.record.type === SecretTypeEnum.SECRET_FILE ? (
                        <Text lineClamp={1} font={{ size: 'small', weight: 'light' }} color={Color.GREY_600}>
                          {`${getString('common.ID')}: ${item.identifier}.${
                            (item.record.spec as SecretTextSpecDTO).secretManagerIdentifier
                          }`}
                        </Text>
                      ) : null}
                      {item.record.type === SecretTypeEnum.SSH_KEY ? (
                        <Text lineClamp={1} font={{ size: 'small', weight: 'light' }} color={Color.GREY_600}>
                          {`${getString('common.ID')}: ${item.identifier}`}
                        </Text>
                      ) : null}
                    </Layout.Vertical>
                  </Layout.Horizontal>
                </Layout.Horizontal>
 
                <RbacButton
                  minimal
                  className={css.editBtn}
                  data-testid={`${name}-edit`}
                  onClick={() =>
                    openCreateSecretModal(item.record.type, {
                      identifier: item.identifier,
                      projectIdentifier: item.record.projectIdentifier,
                      orgIdentifier: item.record.orgIdentifier
                    } as SecretIdentifiers)
                  }
                  permission={{
                    permission: PermissionIdentifier.UPDATE_SECRET,
                    resource: {
                      resourceType: ResourceType.SECRET,
                      resourceIdentifier: item.identifier
                    },
                    resourceScope: {
                      accountIdentifier,
                      orgIdentifier:
                        selectedScope === Scope.ORG || selectedScope === Scope.PROJECT ? orgIdentifier : undefined,
                      projectIdentifier: selectedScope === Scope.PROJECT ? projectIdentifier : undefined
                    }
                  }}
                  text={<Icon size={16} name={'Edit'} color={Color.GREY_600} />}
                />
              </Layout.Horizontal>
            </>
          )
        }}
      />
    </Container>
  )
}
 
export default SecretReference