2022-09-21 04:49:42 +00:00
|
|
|
import axios, { parseAxiosError } from '@/portainer/services/axios';
|
2022-10-23 06:53:25 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
2023-03-07 22:22:08 +00:00
|
|
|
import { Namespaces, SelfSubjectAccessReviewResponse } from './types';
|
2022-09-21 04:49:42 +00:00
|
|
|
|
2023-03-22 02:34:44 +00:00
|
|
|
// getNamespace is used to retrieve a namespace using the Portainer backend
|
2022-09-21 04:49:42 +00:00
|
|
|
export async function getNamespace(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string
|
|
|
|
) {
|
|
|
|
try {
|
2022-10-27 03:14:54 +00:00
|
|
|
const { data: ns } = await axios.get<Namespaces>(
|
2022-09-21 04:49:42 +00:00
|
|
|
buildUrl(environmentId, namespace)
|
|
|
|
);
|
2022-10-27 03:14:54 +00:00
|
|
|
return ns;
|
2022-09-21 04:49:42 +00:00
|
|
|
} catch (e) {
|
2022-10-27 03:14:54 +00:00
|
|
|
throw parseAxiosError(e as Error, 'Unable to retrieve namespace');
|
2022-09-21 04:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-22 02:34:44 +00:00
|
|
|
// getNamespaces is used to retrieve namespaces using the Portainer backend with caching
|
2022-09-21 04:49:42 +00:00
|
|
|
export async function getNamespaces(environmentId: EnvironmentId) {
|
|
|
|
try {
|
2022-10-27 03:14:54 +00:00
|
|
|
const { data: namespaces } = await axios.get<Namespaces>(
|
2022-09-21 04:49:42 +00:00
|
|
|
buildUrl(environmentId)
|
|
|
|
);
|
2022-10-27 03:14:54 +00:00
|
|
|
return namespaces;
|
2022-09-21 04:49:42 +00:00
|
|
|
} catch (e) {
|
2022-10-27 03:14:54 +00:00
|
|
|
throw parseAxiosError(e as Error, 'Unable to retrieve namespaces');
|
2022-09-21 04:49:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 22:22:08 +00:00
|
|
|
export async function getSelfSubjectAccessReview(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespaceName: string,
|
|
|
|
verb = 'list',
|
|
|
|
resource = 'deployments',
|
|
|
|
group = 'apps'
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const { data: accessReview } =
|
|
|
|
await axios.post<SelfSubjectAccessReviewResponse>(
|
|
|
|
`endpoints/${environmentId}/kubernetes/apis/authorization.k8s.io/v1/selfsubjectaccessreviews`,
|
|
|
|
{
|
|
|
|
spec: {
|
|
|
|
resourceAttributes: {
|
|
|
|
group,
|
|
|
|
resource,
|
|
|
|
verb,
|
|
|
|
namespace: namespaceName,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
apiVersion: 'authorization.k8s.io/v1',
|
|
|
|
kind: 'SelfSubjectAccessReview',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return accessReview;
|
|
|
|
} catch (e) {
|
|
|
|
throw parseAxiosError(
|
|
|
|
e as Error,
|
|
|
|
'Unable to retrieve self subject access review'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-21 04:49:42 +00:00
|
|
|
function buildUrl(environmentId: EnvironmentId, namespace?: string) {
|
|
|
|
let url = `kubernetes/${environmentId}/namespaces`;
|
|
|
|
|
|
|
|
if (namespace) {
|
|
|
|
url += `/${namespace}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|