All files / modules/30-secrets/pages/secrets/views/SecretsListView SecretsList.tsx

95.12% Statements 78/82
68.06% Branches 49/72
94.74% Functions 18/19
95.06% Lines 77/81

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              9x 9x 9x 9x   9x                     9x 9x 9x   9x 9x 9x 9x 9x     9x 9x 9x 9x               9x 24x 24x 24x                                         9x 24x 24x                             9x 24x 24x               9x 26x 26x 26x 18x           8x 8x         1x 1x 1x                 9x 36x 36x 36x 36x 36x 36x         36x 36x   36x             36x               1x 1x 1x       1x           36x 1x 1x 1x     36x 1x 1x 1x                 36x         4x                 2x 2x                                           9x 6x 6x 6x 6x   6x 6x     24x             24x                           24x                 24x                     6x                                       9x  
/*
 * 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, { useMemo, useState } from 'react'
import { useParams, useHistory, useLocation } from 'react-router-dom'
import ReactTimeago from 'react-timeago'
import { Menu, Position, Classes, Intent } from '@blueprintjs/core'
import type { Column, Renderer, CellProps } from 'react-table'
import {
  Text,
  Layout,
  Icon,
  Button,
  Popover,
  TagsPopover,
  useConfirmationDialog,
  useToaster,
  TableV2
} from '@wings-software/uicore'
import { Color } from '@harness/design-system'
import { String, useStrings } from 'framework/strings'
import { SecretResponseWrapper, useDeleteSecretV2 } from 'services/cd-ng'
import type { PageSecretResponseWrapper, SecretTextSpecDTO } from 'services/cd-ng'
import { getStringForType } from '@secrets/utils/SSHAuthUtils'
import useCreateSSHCredModal from '@secrets/modals/CreateSSHCredModal/useCreateSSHCredModal'
import useCreateUpdateSecretModal from '@secrets/modals/CreateSecretModal/useCreateUpdateSecretModal'
import { useVerifyModal } from '@secrets/modals/CreateSSHCredModal/useVerifyModal'
import { PermissionIdentifier } from '@rbac/interfaces/PermissionIdentifier'
import type { SecretIdentifiers } from '@secrets/components/CreateUpdateSecret/CreateUpdateSecret'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import { ResourceType } from '@rbac/interfaces/ResourceType'
import RbacMenuItem from '@rbac/components/MenuItem/MenuItem'
import useRBACError from '@rbac/utils/useRBACError/useRBACError'
import css from './SecretsList.module.scss'
 
interface SecretsListProps {
  secrets?: PageSecretResponseWrapper
  gotoPage: (pageNumber: number) => void
  refetch?: () => void
}
 
const RenderColumnSecret: Renderer<CellProps<SecretResponseWrapper>> = ({ row }) => {
  const data = row.original.secret
  const { getString } = useStrings()
  return (
    <Layout.Horizontal>
      {data.type === 'SecretText' || data.type === 'SecretFile' ? (
        <Icon name="key" size={28} margin={{ top: 'xsmall', right: 'small' }} />
      ) : null}
      {data.type === 'SSHKey' ? <Icon name="secret-ssh" size={28} margin={{ top: 'xsmall', right: 'small' }} /> : null}
      <Layout.Vertical>
        <Layout.Horizontal spacing="small" width={230}>
          <Text color={Color.BLACK} lineClamp={1} className={css.secretName}>
            {data.name}
          </Text>
          {data.tags && Object.keys(data.tags).length ? <TagsPopover tags={data.tags} /> : null}
        </Layout.Horizontal>
        <Text color={Color.GREY_600} font={{ size: 'small' }} width={230} lineClamp={1}>
          {`${getString('common.ID')}: ${data.identifier}`}
        </Text>
      </Layout.Vertical>
    </Layout.Horizontal>
  )
}
 
const RenderColumnDetails: Renderer<CellProps<SecretResponseWrapper>> = ({ row }) => {
  const data = row.original.secret
  return (
    <>
      {data.type === 'SecretText' || data.type === 'SecretFile' ? (
        <Text color={Color.BLACK} lineClamp={1} width={230}>
          {(data.spec as SecretTextSpecDTO).secretManagerIdentifier}
        </Text>
      ) : null}
      {/* TODO {Abhinav} display SM name */}
      <Text color={Color.GREY_600} font={{ size: 'small' }}>
        {getStringForType(data.type)}
      </Text>
    </>
  )
}
 
const RenderColumnActivity: Renderer<CellProps<SecretResponseWrapper>> = ({ row }) => {
  const data = row.original
  return data.updatedAt ? (
    <Layout.Horizontal spacing="small" color={Color.GREY_600}>
      <Icon name="activity" />
      <ReactTimeago date={data.updatedAt} />
    </Layout.Horizontal>
  ) : null
}
 
const RenderColumnStatus: Renderer<CellProps<SecretResponseWrapper>> = ({ row }) => {
  const data = row.original.secret
  const { openVerifyModal } = useVerifyModal()
  if (data.type === 'SecretText' || data.type === 'SecretFile') {
    return row.original.draft ? (
      <Text icon="warning-sign" intent="warning">
        {<String stringID="secrets.incompleteSecret" />}
      </Text>
    ) : null
  }
  Eif (data.type === 'SSHKey')
    return (
      <Button
        font="small"
        text={<String stringID="common.labelTestConnection" />}
        onClick={e => {
          e.stopPropagation()
          openVerifyModal(data)
          return
        }}
        withoutBoxShadow
      />
    )
 
  return null
}
 
const RenderColumnAction: Renderer<CellProps<SecretResponseWrapper>> = ({ row, column }) => {
  const data = row.original.secret
  const { accountId, projectIdentifier, orgIdentifier } = useParams<ProjectPathProps>()
  const { getRBACErrorMessage } = useRBACError()
  const { showSuccess, showError } = useToaster()
  const [menuOpen, setMenuOpen] = useState(false)
  const { mutate: deleteSecret } = useDeleteSecretV2({
    queryParams: { accountIdentifier: accountId, projectIdentifier, orgIdentifier },
    requestOptions: { headers: { 'content-type': 'application/json' } }
  })
 
  const { openCreateSSHCredModal } = useCreateSSHCredModal({ onSuccess: (column as any).refreshSecrets })
  const { openCreateSecretModal } = useCreateUpdateSecretModal({ onSuccess: (column as any).refreshSecrets })
 
  const permissionRequest = {
    resource: {
      resourceType: ResourceType.SECRET,
      resourceIdentifier: data.identifier
    }
  }
 
  const { openDialog } = useConfirmationDialog({
    contentText: <String stringID="secrets.confirmDelete" vars={{ name: data.name }} />,
    titleText: <String stringID="secrets.confirmDeleteTitle" />,
    confirmButtonText: <String stringID="delete" />,
    cancelButtonText: <String stringID="cancel" />,
    intent: Intent.DANGER,
    buttonIntent: Intent.DANGER,
    onCloseDialog: async didConfirm => {
      Eif (didConfirm && data.identifier) {
        try {
          await deleteSecret(data.identifier)
          showSuccess(`Secret ${data.name} deleted`)
          ;(column as any).refreshSecrets?.()
        } catch (err) {
          showError(getRBACErrorMessage(err))
        }
      }
    }
  })
 
  const handleDelete = (e: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    e.stopPropagation()
    setMenuOpen(false)
    openDialog()
  }
 
  const handleEdit = (e: React.MouseEvent<HTMLElement, MouseEvent>): void => {
    e.stopPropagation()
    setMenuOpen(false)
    data.type === 'SSHKey'
      ? openCreateSSHCredModal(data)
      : openCreateSecretModal(data.type, {
          identifier: data.identifier,
          orgIdentifier: data.orgIdentifier,
          projectIdentifier: data.projectIdentifier
        } as SecretIdentifiers)
  }
 
  return (
    <Layout.Horizontal style={{ justifyContent: 'flex-end' }}>
      <Popover
        isOpen={menuOpen}
        onInteraction={nextOpenState => {
          setMenuOpen(nextOpenState)
        }}
        className={Classes.DARK}
        position={Position.RIGHT_TOP}
      >
        <Button
          minimal
          icon="Options"
          onClick={e => {
            e.stopPropagation()
            setMenuOpen(true)
          }}
        />
        <Menu>
          <RbacMenuItem
            icon="edit"
            text="Edit"
            onClick={handleEdit}
            permission={{ ...permissionRequest, permission: PermissionIdentifier.UPDATE_SECRET }}
          />
          <RbacMenuItem
            icon="trash"
            text="Delete"
            onClick={handleDelete}
            permission={{ ...permissionRequest, permission: PermissionIdentifier.DELETE_SECRET }}
          />
        </Menu>
      </Popover>
    </Layout.Horizontal>
  )
}
 
const SecretsList: React.FC<SecretsListProps> = ({ secrets, refetch, gotoPage }) => {
  const history = useHistory()
  const data: SecretResponseWrapper[] = useMemo(() => secrets?.content || [], [secrets?.content])
  const { pathname } = useLocation()
  const { getString } = useStrings()
 
  const columns: Column<SecretResponseWrapper>[] = useMemo(
    () => [
      {
        Header: getString('secretType'),
        accessor: row => row.secret.name,
        id: 'name',
        width: '30%',
        Cell: RenderColumnSecret
      },
      {
        Header: getString('details'),
        accessor: row => row.secret.description,
        id: 'details',
        width: '25%',
        Cell: RenderColumnDetails
      },
      {
        Header: getString('lastActivity'),
        accessor: 'updatedAt',
        id: 'activity',
        width: '20%',
        Cell: RenderColumnActivity
      },
      {
        Header: '',
        accessor: row => row.secret.type,
        id: 'status',
        width: '20%',
        Cell: RenderColumnStatus,
        refreshSecrets: refetch,
        disableSortBy: true
      },
      {
        Header: '',
        accessor: row => row.secret.identifier,
        id: 'action',
        width: '5%',
        Cell: RenderColumnAction,
        refreshSecrets: refetch,
        disableSortBy: true
      }
    ],
    [refetch]
  )
 
  return (
    <TableV2<SecretResponseWrapper>
      className={css.table}
      columns={columns}
      data={data}
      name="SecretsListView"
      onRowClick={secret => {
        history.push(`${pathname}/${secret.secret?.identifier}`)
      }}
      pagination={{
        itemCount: secrets?.totalItems || 0,
        pageSize: secrets?.pageSize || 10,
        pageCount: secrets?.totalPages || -1,
        pageIndex: secrets?.pageIndex || 0,
        gotoPage
      }}
    />
  )
}
 
export default SecretsList