All files / modules/30-secrets/modals/CreateSSHCredModal useCreateSSHCredModal.tsx

90% Statements 27/30
77.27% Branches 68/88
83.33% Functions 5/6
90% Lines 27/30

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              232x 232x 232x 232x   232x 232x                 232x   232x 232x                     232x 232x 232x     232x 84x 84x 84x   84x   2x                                                   84x   2x 2x 2x                                             2x   2x       84x 2x         232x  
/*
 * 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, { useCallback, useState } from 'react'
import { Dialog } from '@blueprintjs/core'
import { Button } from '@wings-software/uicore'
import { useModalHook } from '@harness/use-modal'
 
import { pick } from 'lodash-es'
import { useParams } from 'react-router-dom'
import type {
  KerberosConfigDTO,
  SecretDTOV2,
  SSHConfigDTO,
  SSHKeyPathCredentialDTO,
  SSHKeyReferenceCredentialDTO,
  SSHKeySpecDTO
} from 'services/cd-ng'
import { getSecretReferencesforSSH } from '@secrets/utils/SSHAuthUtils'
import type { ProjectPathProps } from '@common/interfaces/RouteInterfaces'
import CreateSSHCredWizard, { SSHCredSharedObj } from './CreateSSHCredWizard'
import css from './useCreateSSHCredModal.module.scss'
 
export interface UseCreateSSHCredModalProps {
  onSuccess?: (secret: SecretDTOV2) => void
}
 
export interface UseCreateSSHCredModalReturn {
  openCreateSSHCredModal: (secret?: SecretDTOV2) => void
  closeCreateSSHCredModal: () => void
}
 
export enum Views {
  CREATE,
  EDIT
}
 
const useCreateSSHCredModal = (props: UseCreateSSHCredModalProps): UseCreateSSHCredModalReturn => {
  const { accountId, orgIdentifier, projectIdentifier } = useParams<ProjectPathProps>()
  const [view, setView] = useState(Views.CREATE)
  const [sshData, setSSHData] = useState<SSHCredSharedObj>()
 
  const [showModal, hideModal] = useModalHook(
    () => (
      <Dialog
        className={css.dialog}
        enforceFocus={false}
        isOpen={true}
        onClose={() => {
          setView(Views.CREATE)
          hideModal()
        }}
      >
        {view === Views.CREATE ? (
          <CreateSSHCredWizard {...props} hideModal={hideModal} />
        ) : (
          <CreateSSHCredWizard
            {...props}
            isEdit={true}
            detailsData={sshData?.detailsData}
            authData={sshData?.authData}
            hideModal={hideModal}
          />
        )}
        <Button minimal icon="cross" iconProps={{ size: 18 }} onClick={hideModal} className={css.crossIcon} />
      </Dialog>
    ),
    [view, sshData]
  )
 
  const open = useCallback(
    async (_sshData?: SecretDTOV2) => {
      Eif (_sshData) {
        const response = await getSecretReferencesforSSH(_sshData, accountId, orgIdentifier, projectIdentifier)
        setSSHData({
          detailsData: {
            ...pick(_sshData, 'name', 'identifier', 'description', 'tags')
          },
          authData: {
            authScheme: (_sshData.spec as SSHKeySpecDTO)?.auth.type,
            credentialType: ((_sshData.spec as SSHKeySpecDTO)?.auth.spec as SSHConfigDTO)?.credentialType,
            tgtGenerationMethod:
              ((_sshData.spec as SSHKeySpecDTO)?.auth.spec as KerberosConfigDTO).tgtGenerationMethod || 'None',
            userName: (
              ((_sshData.spec as SSHKeySpecDTO)?.auth.spec as SSHConfigDTO)?.spec as SSHKeyReferenceCredentialDTO
            )?.userName,
            principal: ((_sshData.spec as SSHKeySpecDTO)?.auth.spec as KerberosConfigDTO)?.principal,
            realm: ((_sshData.spec as SSHKeySpecDTO)?.auth.spec as KerberosConfigDTO)?.realm,
            keyPath:
              ((_sshData.spec as SSHKeySpecDTO)?.auth.spec as KerberosConfigDTO)?.keyPath ||
              (((_sshData.spec as SSHKeySpecDTO)?.auth.spec as SSHConfigDTO)?.spec as SSHKeyPathCredentialDTO)?.keyPath,
            port: (_sshData.spec as SSHKeySpecDTO)?.port || 22,
            key: response.keySecret,
            password: response.passwordSecret,
            encryptedPassphrase: response.encryptedPassphraseSecret
          }
        })
        setView(Views.EDIT)
      } else setView(Views.CREATE)
      showModal()
    },
    [showModal]
  )
  return {
    openCreateSSHCredModal: (secret?: SecretDTOV2) => open(secret),
    closeCreateSSHCredModal: hideModal
  }
}
 
export default useCreateSSHCredModal