Finish preliminary loop on SSH certificates

This commit is contained in:
Tuan Dang
2024-12-02 22:37:23 -08:00
parent 82c3e943eb
commit 4fc8c509ac
65 changed files with 3685 additions and 26 deletions
+3 -1
View File
@@ -1,3 +1,5 @@
import { SshCaStatus } from "@app/hooks/api/ssh-ca";
import { CaStatus, CaType } from "./enums";
export const caTypeToNameMap: { [K in CaType]: string } = {
@@ -11,7 +13,7 @@ export const caStatusToNameMap: { [K in CaStatus]: string } = {
[CaStatus.PENDING_CERTIFICATE]: "Pending Certificate"
};
export const getCaStatusBadgeVariant = (status: CaStatus) => {
export const getCaStatusBadgeVariant = (status: CaStatus | SshCaStatus) => {
switch (status) {
case CaStatus.ACTIVE:
return "success";
+2
View File
@@ -38,6 +38,8 @@ export * from "./secretSharing";
export * from "./secretSnapshots";
export * from "./serverDetails";
export * from "./serviceTokens";
export * from "./ssh-ca";
export * from "./sshCertificateTemplates";
export * from "./ssoConfig";
export * from "./subscriptions";
export * from "./tags";
+2 -2
View File
@@ -19,6 +19,6 @@ export {
useGetOrgPmtMethods,
useGetOrgTaxIds,
useGetOrgTrialUrl,
useListOrgSshCas,
useUpdateOrg,
useUpdateOrgBillingDetails
} from "./queries";
useUpdateOrgBillingDetails} from "./queries";
@@ -4,6 +4,7 @@ import { apiRequest } from "@app/config/request";
import { OrderByDirection } from "@app/hooks/api/generic/types";
import { TGroupOrgMembership } from "../groups/types";
import { TSshCertificateAuthority } from "../ssh-ca/types";
import { IntegrationAuth } from "../types";
import {
BillingDetails,
@@ -41,7 +42,8 @@ export const organizationKeys = {
}: TListOrgIdentitiesDTO) =>
[...organizationKeys.getOrgIdentityMemberships(orgId), params] as const,
getOrgGroups: (orgId: string) => [{ orgId }, "organization-groups"] as const,
getOrgIntegrationAuths: (orgId: string) => [{ orgId }, "integration-auths"] as const
getOrgIntegrationAuths: (orgId: string) => [{ orgId }, "integration-auths"] as const,
getOrgSshCas: ({ orgId }: { orgId: string }) => [{ orgId }, "org-ssh-cas"] as const
};
export const fetchOrganizations = async () => {
@@ -495,3 +497,18 @@ export const useGetOrgIntegrationAuths = <TData = IntegrationAuth[],>(
select
});
};
export const useListOrgSshCas = ({ orgId }: { orgId: string }) => {
return useQuery({
queryKey: organizationKeys.getOrgSshCas({ orgId }),
queryFn: async () => {
const {
data: { cas }
} = await apiRequest.get<{ cas: TSshCertificateAuthority[] }>(
`/api/v1/organization/${orgId}/ssh-cas`
);
return cas;
},
enabled: Boolean(orgId)
});
};
+4
View File
@@ -0,0 +1,4 @@
export enum SshCaStatus {
ACTIVE = "active",
DISABLED = "disabled"
}
+3
View File
@@ -0,0 +1,3 @@
export { SshCaStatus } from "./enums";
export { useCreateSshCa, useDeleteSshCa,useUpdateSshCa } from "./mutations";
export { useGetSshCaById, useGetSshCaCertTemplates } from "./queries";
@@ -0,0 +1,61 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { organizationKeys } from "../organization/queries";
import {
TCreateSshCaDTO,
TDeleteSshCaDTO,
TSshCertificateAuthority,
TUpdateSshCaDTO} from "./types";
export const sshCaKeys = {
getSshCaById: (caId: string) => [{ caId }, "ssh-ca"]
};
export const useCreateSshCa = () => {
const queryClient = useQueryClient();
return useMutation<TSshCertificateAuthority, {}, TCreateSshCaDTO>({
mutationFn: async (body) => {
const {
data: { ca }
} = await apiRequest.post<{ ca: TSshCertificateAuthority }>("/api/v1/ssh/ca/", body);
return ca;
},
onSuccess: ({ orgId }) => {
queryClient.invalidateQueries(organizationKeys.getOrgSshCas({ orgId }));
}
});
};
export const useUpdateSshCa = () => {
const queryClient = useQueryClient();
return useMutation<TSshCertificateAuthority, {}, TUpdateSshCaDTO>({
mutationFn: async ({ caId, ...body }) => {
const {
data: { ca }
} = await apiRequest.patch<{ ca: TSshCertificateAuthority }>(`/api/v1/ssh/ca/${caId}`, body);
return ca;
},
onSuccess: ({ orgId }, { caId }) => {
queryClient.invalidateQueries(organizationKeys.getOrgSshCas({ orgId }));
queryClient.invalidateQueries(sshCaKeys.getSshCaById(caId));
}
});
};
export const useDeleteSshCa = () => {
const queryClient = useQueryClient();
return useMutation<TSshCertificateAuthority, {}, TDeleteSshCaDTO>({
mutationFn: async ({ caId }) => {
const {
data: { ca }
} = await apiRequest.delete<{ ca: TSshCertificateAuthority }>(`/api/v1/ssh/ca/${caId}`);
return ca;
},
onSuccess: ({ orgId }) => {
queryClient.invalidateQueries(organizationKeys.getOrgSshCas({ orgId }));
}
});
};
+37
View File
@@ -0,0 +1,37 @@
import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { TSshCertificateTemplate } from "../sshCertificateTemplates/types";
import { TSshCertificateAuthority } from "./types";
export const sshCaKeys = {
getSshCaById: (caId: string) => [{ caId }, "ssh-ca"],
getSshCaCertTemplates: (caId: string) => [{ caId }, "ssh-ca-cert-templates"]
};
export const useGetSshCaById = (caId: string) => {
return useQuery({
queryKey: sshCaKeys.getSshCaById(caId),
queryFn: async () => {
const {
data: { ca }
} = await apiRequest.get<{ ca: TSshCertificateAuthority }>(`/api/v1/ssh/ca/${caId}`);
return ca;
},
enabled: Boolean(caId)
});
};
export const useGetSshCaCertTemplates = (caId: string) => {
return useQuery({
queryKey: sshCaKeys.getSshCaCertTemplates(caId),
queryFn: async () => {
const { data } = await apiRequest.get<{
certificateTemplates: TSshCertificateTemplate[];
}>(`/api/v1/ssh/ca/${caId}/certificate-templates`);
return data;
},
enabled: Boolean(caId)
});
};
+26
View File
@@ -0,0 +1,26 @@
import { CertKeyAlgorithm } from "../certificates/enums";
import { SshCaStatus } from "./enums";
export type TSshCertificateAuthority = {
id: string;
orgId: string;
status: SshCaStatus;
friendlyName: string;
keyAlgorithm: CertKeyAlgorithm;
createdAt: string;
updatedAt: string;
};
export type TCreateSshCaDTO = {
friendlyName?: string;
keyAlgorithm: CertKeyAlgorithm;
};
export type TUpdateSshCaDTO = {
caId: string;
status?: SshCaStatus;
};
export type TDeleteSshCaDTO = {
caId: string;
};
@@ -0,0 +1,5 @@
export {
useCreateSshCertTemplate,
useDeleteSshCertTemplate,
useUpdateSshCertTemplate} from "./mutations";
export { useGetSshCertTemplate } from "./queries";
@@ -0,0 +1,59 @@
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { sshCaKeys } from "../ssh-ca/queries";
import {
TCreateSshCertificateTemplateDTO,
TDeleteSshCertificateTemplateDTO,
TSshCertificateTemplate,
TUpdateSshCertificateTemplateDTO
} from "./types";
export const useCreateSshCertTemplate = () => {
const queryClient = useQueryClient();
return useMutation<TSshCertificateTemplate, {}, TCreateSshCertificateTemplateDTO>({
mutationFn: async (data) => {
const { data: certificateTemplate } = await apiRequest.post<TSshCertificateTemplate>(
"/api/v1/ssh/certificate-templates",
data
);
return certificateTemplate;
},
onSuccess: ({ sshCaId }) => {
queryClient.invalidateQueries(sshCaKeys.getSshCaCertTemplates(sshCaId));
}
});
};
export const useUpdateSshCertTemplate = () => {
const queryClient = useQueryClient();
return useMutation<TSshCertificateTemplate, {}, TUpdateSshCertificateTemplateDTO>({
mutationFn: async (data) => {
const { data: certificateTemplate } = await apiRequest.patch<TSshCertificateTemplate>(
`/api/v1/ssh/certificate-templates/${data.id}`,
data
);
return certificateTemplate;
},
onSuccess: ({ sshCaId }) => {
queryClient.invalidateQueries(sshCaKeys.getSshCaCertTemplates(sshCaId));
}
});
};
export const useDeleteSshCertTemplate = () => {
const queryClient = useQueryClient();
return useMutation<TSshCertificateTemplate, {}, TDeleteSshCertificateTemplateDTO>({
mutationFn: async (data) => {
const { data: certificateTemplate } = await apiRequest.delete<TSshCertificateTemplate>(
`/api/v1/ssh/certificate-templates/${data.id}`
);
return certificateTemplate;
},
onSuccess: ({ sshCaId }) => {
queryClient.invalidateQueries(sshCaKeys.getSshCaCertTemplates(sshCaId));
}
});
};
@@ -0,0 +1,22 @@
import { useQuery } from "@tanstack/react-query";
import { apiRequest } from "@app/config/request";
import { TSshCertificateTemplate } from "./types";
export const certTemplateKeys = {
getSshCertTemplateById: (id: string) => [{ id }, "ssh-cert-template"]
};
export const useGetSshCertTemplate = (id: string) => {
return useQuery({
queryKey: certTemplateKeys.getSshCertTemplateById(id),
queryFn: async () => {
const { data: certificateTemplate } = await apiRequest.get<TSshCertificateTemplate>(
`/api/v1/ssh/certificate-templates/${id}`
);
return certificateTemplate;
},
enabled: Boolean(id)
});
};
@@ -0,0 +1,40 @@
export type TSshCertificateTemplate = {
id: string;
sshCaId: string;
name: string;
ttl: string;
maxTTL: string;
allowedUsers: string[];
allowedHosts: string[];
allowUserCertificates: boolean;
allowHostCertificates: boolean;
allowCustomKeyIds: boolean;
};
export type TCreateSshCertificateTemplateDTO = {
sshCaId: string;
name: string;
ttl: string;
maxTTL: string;
allowedUsers: string[];
allowedHosts: string[];
allowUserCertificates: boolean;
allowHostCertificates: boolean;
allowCustomKeyIds: boolean;
};
export type TUpdateSshCertificateTemplateDTO = {
id: string;
name?: string;
ttl?: string;
maxTTL?: string;
allowedUsers?: string[];
allowedHosts?: string[];
allowUserCertificates?: boolean;
allowHostCertificates?: boolean;
allowCustomKeyIds?: boolean;
};
export type TDeleteSshCertificateTemplateDTO = {
id: string;
};