diff --git a/src/components/ImportForm/SecretSection/SecretSection.tsx b/src/components/ImportForm/SecretSection/SecretSection.tsx index a8988bd50..6fce65727 100644 --- a/src/components/ImportForm/SecretSection/SecretSection.tsx +++ b/src/components/ImportForm/SecretSection/SecretSection.tsx @@ -29,7 +29,7 @@ const SecretSection = () => { secrets && secretsLoaded ? secrets ?.filter((rs) => partnerTaskNames.includes(rs.metadata.name)) - ?.map((s) => s.metadata.name) || [] + ?.map((s) => s.metadata.name) : []; const onSubmit = React.useCallback( diff --git a/src/components/Secrets/SecretForm.tsx b/src/components/Secrets/SecretForm.tsx index 1024f6e4f..c240525f0 100644 --- a/src/components/Secrets/SecretForm.tsx +++ b/src/components/Secrets/SecretForm.tsx @@ -2,16 +2,14 @@ import React from 'react'; import { Form } from '@patternfly/react-core'; import { SelectVariant } from '@patternfly/react-core/deprecated'; import { useFormikContext } from 'formik'; +import { useSecrets } from '../../hooks/useSecrets'; import { DropdownItemObject, SelectInputField } from '../../shared'; import KeyValueFileInputField from '../../shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField'; import { SecretFormValues, SecretTypeDropdownLabel } from '../../types'; +import { useWorkspaceInfo } from '../../utils/workspace-context-utils'; import { RawComponentProps } from '../modal/createModalLauncher'; import SecretTypeSelector from './SecretTypeSelector'; -import { - getSupportedPartnerTaskKeyValuePairs, - isPartnerTask, - getSupportedPartnerTaskSecrets, -} from './utils/secret-utils'; +import { secretsList } from './utils/secret-utils'; type SecretFormProps = RawComponentProps & { existingSecrets: string[]; @@ -19,15 +17,28 @@ type SecretFormProps = RawComponentProps & { const SecretForm: React.FC> = ({ existingSecrets }) => { const { values, setFieldValue } = useFormikContext(); + const { namespace } = useWorkspaceInfo(); + const [secrets, secretsLoaded] = useSecrets(namespace); + const defaultKeyValues = [{ key: '', value: '', readOnlyKey: false }]; const defaultImageKeyValues = [{ key: '.dockerconfigjson', value: '', readOnlyKey: true }]; - const initialOptions = getSupportedPartnerTaskSecrets().filter( - (secret) => !existingSecrets.includes(secret.value), - ); + const sl = secretsLoaded ? secretsList(secrets) : []; + const initialOptions = Object.values(sl) + .map((secret) => ({ value: secret.name, label: secret.name })) + .filter((secret) => !existingSecrets.includes(secret.value)); const [options, setOptions] = React.useState(initialOptions); const currentTypeRef = React.useRef(values.type); + const isSecretTask = (secretName: string) => { + return !!Object.values(sl).find((secret) => secret.name === secretName); + }; + + const getSecretsTaskKeyValuePairs = (secretName?: string) => { + const secretTask = Object.values(sl).find((secret) => secret.name === secretName); + return secretTask ? secretTask.keyValuePairs : []; + }; + const clearKeyValues = () => { const newKeyValues = values.keyValues.filter((kv) => !kv.readOnlyKey); setFieldValue('keyValues', [...(newKeyValues.length ? newKeyValues : defaultKeyValues)]); @@ -57,9 +68,7 @@ const SecretForm: React.FC> = ({ existi currentTypeRef.current = type; if (type === SecretTypeDropdownLabel.image) { resetKeyValues(); - values.secretName && - isPartnerTask(values.secretName) && - setFieldValue('secretName', ''); + values.secretName && isSecretTask(values.secretName) && setFieldValue('secretName', ''); } else { setOptions(initialOptions); clearKeyValues(); @@ -80,15 +89,15 @@ const SecretForm: React.FC> = ({ existi toggleId="secret-name-toggle" toggleAriaLabel="secret-name-dropdown" onClear={() => { - if (currentTypeRef.current !== values.type || isPartnerTask(values.secretName)) { + if (currentTypeRef.current !== values.type || isSecretTask(values.secretName)) { clearKeyValues(); } }} onSelect={(e, value) => { - if (isPartnerTask(value)) { + if (isSecretTask(value)) { setFieldValue('keyValues', [ ...values.keyValues.filter((kv) => !kv.readOnlyKey && (!!kv.key || !!kv.value)), - ...getSupportedPartnerTaskKeyValuePairs(value), + ...getSecretsTaskKeyValuePairs(value), ]); } setFieldValue('secretName', value); diff --git a/src/components/Secrets/utils/secret-utils.ts b/src/components/Secrets/utils/secret-utils.ts index 2370ef0b2..f49c508c6 100644 --- a/src/components/Secrets/utils/secret-utils.ts +++ b/src/components/Secrets/utils/secret-utils.ts @@ -36,10 +36,34 @@ export const supportedPartnerTasksSecrets: { [key: string]: PartnerTask } = { name: 'snyk-secret', providerUrl: 'https://snyk.io/', tokenKeyName: 'snyk_token', - keyValuePairs: [{ key: 'snyk_token', value: '', readOnlyKey: true }], + keyValuePairs: [{ key: 'snyk_token', value: '', readOnlyKey: true, readOnlyValue: false }], }, }; +export const secretsList = (secrets) => { + const partnerSecretNames = { + 'snyk-secret': supportedPartnerTasksSecrets.snyk, + }; + secrets + .filter((secret) => secret.type === 'Opaque') + .forEach((secret) => { + partnerSecretNames[secret.metadata.name] = { + type: secret.type, + name: secret.metadata.name, + providerUrl: + secret.metadata.name === 'snyk-secret' ? 'https://snyk.io/' : 'https://kube.io', + tokenKeyName: + secret.metadata.name === 'snyk-secret' ? 'secret_token' : secret.metadata.name, + keyValuePairs: Object.keys(secret.data).map((key) => ({ + key, + value: Base64.decode(secret.data[key]), + readOnlyKey: true, + })), + }; + }); + return partnerSecretNames; +}; + export const getSupportedPartnerTaskSecrets = () => { return Object.values(supportedPartnerTasksSecrets).map((secret) => ({ label: secret.name, diff --git a/src/shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField.tsx b/src/shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField.tsx index d79e84c3c..7ea9a53e3 100644 --- a/src/shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField.tsx +++ b/src/shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField.tsx @@ -94,6 +94,7 @@ const KeyValueFileInputField: React.FC< label="Value" name={`${name}.${idx.toString()}.value`} filenamePlaceholder="Drag a file here or upload one" + isDisabled={v.readOnlyValue ?? v.readOnlyKey} onDataChange={(ev, data: string) => { setFieldValue(`${name}.${idx.toString()}.value`, data); onChange && onChange(data, `${name}.${idx.toString()}.value`);