All files / modules/35-connectors/components/CreateConnector/AWSSecretManager/views AwsSecretManagerConfig.tsx

96.97% Statements 32/33
65% Branches 13/20
90.91% Functions 10/11
96.97% Lines 32/33

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              219x 219x 219x                           219x 219x 219x 219x           219x 219x 219x   219x   219x               6x 6x   6x                           6x                       6x 6x   6x 2x 1x 1x 1x         6x                           2x       2x         2x       2x               2x                 1x       6x                                                                                   219x  
/*
 * Copyright 2021 Harness Inc. All rights reserved.
 * Use of this source code is governed by the PolyForm Free Trial 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/05/PolyForm-Free-Trial-1.0.0.txt.
 */
 
import React, { useState } from 'react'
import * as Yup from 'yup'
import {
  StepProps,
  Container,
  Text,
  SelectOption,
  FormInput,
  Formik,
  FormikForm,
  Layout,
  Button,
  ModalErrorHandlerBinding,
  ModalErrorHandler,
  ButtonVariation
} from '@wings-software/uicore'
import { FontVariation } from '@harness/design-system'
import { useStrings } from 'framework/strings'
import { setupAwsSecretManagerFormData } from '@connectors/pages/connectors/utils/ConnectorUtils'
import {
  AwsSecretManagerConfigFormData,
  ConnectorDetailsProps,
  CredTypeValues,
  StepDetailsProps
} from '@connectors/interfaces/ConnectorInterface'
import { PageSpinner } from '@common/components'
import AwsSecretManagerAccessKeyForm from './AwsSecretManagerAccessKeyForm'
import css from '../CreateAwsSecretManagerConnector.module.scss'
 
const externalIdRegExpression = /^\S*$/
 
const AwsSecretManagerConfig: React.FC<StepProps<StepDetailsProps> & ConnectorDetailsProps> = ({
  accountId,
  prevStepData,
  previousStep,
  nextStep,
  isEditMode,
  connectorInfo
}) => {
  const { getString } = useStrings()
  const [modalErrorHandler, setModalErrorHandler] = useState<ModalErrorHandlerBinding | undefined>()
 
  const credTypeOptions: SelectOption[] = [
    {
      label: getString('connectors.aws.awsAccessKey'),
      value: CredTypeValues.ManualConfig
    },
    {
      label: getString('connectors.aws.assumeIAMRole'),
      value: CredTypeValues.AssumeIAMRole
    },
    {
      label: getString('connectors.awsKms.awsSTS'),
      value: CredTypeValues.AssumeRoleSTS
    }
  ]
  const defaultInitialFormData: AwsSecretManagerConfigFormData = {
    accessKey: undefined,
    secretKey: undefined,
    secretNamePrefix: undefined,
    region: undefined,
    credType: credTypeOptions[0].value as string,
    roleArn: undefined,
    externalId: undefined,
    assumeStsRoleDuration: undefined,
    default: false
  }
 
  const [initialValues, setInitialValues] = useState(defaultInitialFormData)
  const [loadingFormData, setLoadingFormData] = useState(isEditMode)
 
  React.useEffect(() => {
    if (isEditMode && connectorInfo) {
      setupAwsSecretManagerFormData(connectorInfo, accountId).then(data => {
        setInitialValues(data as AwsSecretManagerConfigFormData)
        setLoadingFormData(false)
      })
    }
  }, [isEditMode, connectorInfo, accountId])
 
  return loadingFormData ? (
    <PageSpinner />
  ) : (
    <Container padding={{ top: 'medium' }} width="64%">
      <Text font={{ variation: FontVariation.H3 }} padding={{ bottom: 'xlarge' }}>
        {getString('details')}
      </Text>
      <ModalErrorHandler bind={setModalErrorHandler} />
      <Formik
        enableReinitialize
        initialValues={{ ...initialValues, ...prevStepData }}
        formName="awsSMConfigForm"
        validationSchema={Yup.object().shape({
          accessKey: Yup.object().when(['credType'], {
            is: credentials => credentials === credTypeOptions[0].value,
            then: Yup.object().required(getString('connectors.aws.validation.accessKey'))
          }),
          secretKey: Yup.object().when(['credType'], {
            is: credentials => credentials === credTypeOptions[0].value,
            then: Yup.object().required(getString('connectors.aws.validation.secretKeyRef'))
          }),
          region: Yup.string().trim().required(getString('connectors.awsKms.validation.selectRegion')),
          roleArn: Yup.string().when(['credType'], {
            is: credentials => credentials === credTypeOptions[2].value,
            then: Yup.string().trim().required(getString('connectors.aws.validation.crossAccountRoleArn'))
          }),
          externalId: Yup.string().when(['credType'], {
            is: credentials => credentials === credTypeOptions[2].value,
            then: Yup.string()
              .trim()
              .min(2, getString('connectors.awsKms.validation.externalIdLengthError'))
              .max(1224, getString('connectors.awsKms.validation.externalIdLengthError'))
              .matches(externalIdRegExpression, getString('connectors.awsKms.validation.externalIdRegexError'))
          }),
          assumeStsRoleDuration: Yup.number().when(['credType'], {
            is: credentials => credentials === credTypeOptions[2].value,
            then: Yup.number()
              .integer(getString('connectors.awsKms.validation.durationError'))
              .min(900, getString('connectors.awsKms.validation.durationNumber'))
              .max(43200, getString('connectors.awsKms.validation.durationNumber'))
              .typeError(getString('connectors.awsKms.validation.durationError'))
          })
        })}
        onSubmit={formData => {
          nextStep?.({ ...connectorInfo, ...prevStepData, ...formData } as StepDetailsProps)
        }}
      >
        {formik => {
          return (
            <FormikForm>
              <Container margin={{ top: 'medium', bottom: 'xxlarge' }} className={css.container}>
                <FormInput.Select name="credType" label={getString('credType')} items={credTypeOptions} />
                <AwsSecretManagerAccessKeyForm
                  formik={formik}
                  accountId={accountId}
                  modalErrorHandler={modalErrorHandler}
                />
                {formik.values?.credType === credTypeOptions[2].value && (
                  <>
                    <FormInput.Text name="roleArn" label={getString('connectors.awsKms.roleArnLabel')} />
                    <FormInput.Text name="externalId" label={getString('connectors.aws.externalId')} />
                    <FormInput.Text
                      name="assumeStsRoleDuration"
                      label={getString('connectors.awsKms.assumedRoleDuration')}
                    />
                  </>
                )}
                <FormInput.CheckBox
                  name="default"
                  label={getString('connectors.hashiCorpVault.defaultVault')}
                  padding={{ left: 'xxlarge' }}
                />
              </Container>
              <Layout.Horizontal spacing="medium">
                <Button
                  variation={ButtonVariation.SECONDARY}
                  icon="chevron-left"
                  text={getString('back')}
                  onClick={() => previousStep?.(prevStepData)}
                />
                <Button type="submit" intent="primary" rightIcon="chevron-right" text={getString('continue')} />
              </Layout.Horizontal>
            </FormikForm>
          )
        }}
      </Formik>
    </Container>
  )
}
 
export default AwsSecretManagerConfig