2024-01-02 20:07:11 +00:00
|
|
|
import { Secret, SecretList } from 'kubernetes-types/core/v1';
|
2023-06-11 21:46:48 +00:00
|
|
|
import { useMutation, useQuery } from 'react-query';
|
|
|
|
|
|
|
|
import { queryClient, withError } from '@/react-tools/react-query';
|
|
|
|
import axios from '@/portainer/services/axios';
|
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
|
|
|
import {
|
|
|
|
error as notifyError,
|
|
|
|
notifySuccess,
|
|
|
|
} from '@/portainer/services/notifications';
|
|
|
|
import { isFulfilled, isRejected } from '@/portainer/helpers/promise-utils';
|
2023-07-27 19:50:53 +00:00
|
|
|
import { pluralize } from '@/portainer/helpers/strings';
|
2023-06-11 21:46:48 +00:00
|
|
|
|
|
|
|
import { parseKubernetesAxiosError } from '../axiosError';
|
|
|
|
|
|
|
|
export const secretQueryKeys = {
|
|
|
|
secrets: (environmentId: EnvironmentId, namespace?: string) => [
|
|
|
|
'environments',
|
|
|
|
environmentId,
|
|
|
|
'kubernetes',
|
|
|
|
'secrets',
|
|
|
|
'namespaces',
|
|
|
|
namespace,
|
|
|
|
],
|
|
|
|
secretsForCluster: (environmentId: EnvironmentId) => [
|
|
|
|
'environments',
|
|
|
|
environmentId,
|
|
|
|
'kubernetes',
|
|
|
|
'secrets',
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
// returns a usequery hook for the list of secrets from the kubernetes API
|
|
|
|
export function useSecrets(environmentId: EnvironmentId, namespace?: string) {
|
|
|
|
return useQuery(
|
|
|
|
secretQueryKeys.secrets(environmentId, namespace),
|
|
|
|
() => (namespace ? getSecrets(environmentId, namespace) : []),
|
|
|
|
{
|
|
|
|
onError: (err) => {
|
|
|
|
notifyError(
|
|
|
|
'Failure',
|
|
|
|
err as Error,
|
|
|
|
`Unable to get secrets in namespace '${namespace}'`
|
|
|
|
);
|
|
|
|
},
|
|
|
|
enabled: !!namespace,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useSecretsForCluster(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespaces?: string[],
|
|
|
|
options?: { autoRefreshRate?: number }
|
|
|
|
) {
|
|
|
|
return useQuery(
|
|
|
|
secretQueryKeys.secretsForCluster(environmentId),
|
|
|
|
() => namespaces && getSecretsForCluster(environmentId, namespaces),
|
|
|
|
{
|
|
|
|
...withError('Unable to retrieve secrets for cluster'),
|
|
|
|
enabled: !!namespaces?.length,
|
|
|
|
refetchInterval() {
|
|
|
|
return options?.autoRefreshRate ?? false;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useMutationDeleteSecrets(environmentId: EnvironmentId) {
|
|
|
|
return useMutation(
|
|
|
|
async (secrets: { namespace: string; name: string }[]) => {
|
|
|
|
const promises = await Promise.allSettled(
|
|
|
|
secrets.map(({ namespace, name }) =>
|
|
|
|
deleteSecret(environmentId, namespace, name)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
const successfulSecrets = promises
|
|
|
|
.filter(isFulfilled)
|
|
|
|
.map((_, index) => secrets[index].name);
|
|
|
|
const failedSecrets = promises
|
|
|
|
.filter(isRejected)
|
|
|
|
.map(({ reason }, index) => ({
|
|
|
|
name: secrets[index].name,
|
|
|
|
reason,
|
|
|
|
}));
|
|
|
|
return { failedSecrets, successfulSecrets };
|
|
|
|
},
|
|
|
|
{
|
|
|
|
...withError('Unable to remove secrets'),
|
|
|
|
onSuccess: ({ failedSecrets, successfulSecrets }) => {
|
|
|
|
queryClient.invalidateQueries(
|
|
|
|
secretQueryKeys.secretsForCluster(environmentId)
|
|
|
|
);
|
|
|
|
// show an error message for each secret that failed to delete
|
|
|
|
failedSecrets.forEach(({ name, reason }) => {
|
|
|
|
notifyError(
|
|
|
|
`Failed to remove secret '${name}'`,
|
|
|
|
new Error(reason.message) as Error
|
|
|
|
);
|
|
|
|
});
|
|
|
|
// show one summary message for all successful deletes
|
|
|
|
if (successfulSecrets.length) {
|
|
|
|
notifySuccess(
|
2023-07-27 19:50:53 +00:00
|
|
|
`${pluralize(
|
|
|
|
successfulSecrets.length,
|
|
|
|
'Secret'
|
|
|
|
)} successfully removed`,
|
2023-06-11 21:46:48 +00:00
|
|
|
successfulSecrets.join(', ')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getSecretsForCluster(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespaces: string[]
|
|
|
|
) {
|
2023-10-26 21:56:03 +00:00
|
|
|
const secrets = await Promise.all(
|
|
|
|
namespaces.map((namespace) => getSecrets(environmentId, namespace))
|
|
|
|
);
|
|
|
|
return secrets.flat();
|
2023-06-11 21:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get all secrets for a namespace
|
|
|
|
async function getSecrets(environmentId: EnvironmentId, namespace: string) {
|
|
|
|
try {
|
|
|
|
const { data } = await axios.get<SecretList>(
|
|
|
|
buildUrl(environmentId, namespace)
|
|
|
|
);
|
2024-01-02 20:07:11 +00:00
|
|
|
const secretsWithKind: Secret[] = data.items.map((secret) => ({
|
|
|
|
...secret,
|
|
|
|
kind: 'Secret',
|
|
|
|
}));
|
|
|
|
return secretsWithKind;
|
2023-06-11 21:46:48 +00:00
|
|
|
} catch (e) {
|
2023-10-23 19:52:40 +00:00
|
|
|
throw parseKubernetesAxiosError(e, 'Unable to retrieve secrets');
|
2023-06-11 21:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteSecret(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
name: string
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
await axios.delete(buildUrl(environmentId, namespace, name));
|
|
|
|
} catch (e) {
|
2023-10-23 19:52:40 +00:00
|
|
|
throw parseKubernetesAxiosError(e, 'Unable to remove secret');
|
2023-06-11 21:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildUrl(environmentId: number, namespace: string, name?: string) {
|
|
|
|
const url = `/endpoints/${environmentId}/kubernetes/api/v1/namespaces/${namespace}/secrets`;
|
|
|
|
return name ? `${url}/${name}` : url;
|
|
|
|
}
|