Skip to content

Commit

Permalink
chore: merge changes from alternative branch
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoPedroPP committed Oct 17, 2024
2 parents dbbdf42 + 8998222 commit 673818a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 19 deletions.
8 changes: 4 additions & 4 deletions src/components/ImportForm/SecretSection/SecretSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -76,9 +76,9 @@ const SecretSection = () => {
variant="link"
data-testid="add-secret-button"
icon={<PlusCircleIcon />}
onClick={() =>
showModal(SecretModalLauncher([...partnerTaskSecrets, ...values.newSecrets], onSubmit))
}
onClick={() => {
showModal(SecretModalLauncher([...partnerTaskSecrets, ...values.newSecrets], onSubmit));
}}
isDisabled={!canCreateSecret}
tooltip="You don't have access to add a secret"
>
Expand Down
37 changes: 23 additions & 14 deletions src/components/Secrets/SecretForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,43 @@ 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[];
};

const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ existingSecrets }) => {
const { values, setFieldValue } = useFormikContext<SecretFormValues>();
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)]);
Expand Down Expand Up @@ -57,9 +68,7 @@ const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ 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();
Expand All @@ -80,15 +89,15 @@ const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ 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);
Expand Down
26 changes: 25 additions & 1 deletion src/components/Secrets/utils/secret-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand Down

0 comments on commit 673818a

Please sign in to comment.