Skip to content

Commit

Permalink
fix cert update route url bug (#6311)
Browse files Browse the repository at this point in the history
* fix path bug

* fix cert crud
  • Loading branch information
jackkav committed Aug 17, 2023
1 parent 7ba00d5 commit 74960ec
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 49 deletions.
Expand Up @@ -132,7 +132,8 @@ export const WorkspaceSettingsModal = ({ workspace, workspaceMeta, clientCertifi
const _handleCreateCertificate = async (event: React.SyntheticEvent<HTMLFormElement>) => {
event.preventDefault();
const { pfxPath, crtPath, keyPath, host, passphrase, isPrivate } = state;
const certificate = {

newClientCert({
host,
isPrivate,
parentId: workspace._id,
Expand All @@ -141,11 +142,6 @@ export const WorkspaceSettingsModal = ({ workspace, workspaceMeta, clientCertifi
cert: crtPath || null,
key: keyPath || null,
pfx: pfxPath || null,
};
workspaceFetcher.submit(certificate, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/clientcert/new`,
method: 'post',
encType: 'application/json',
});
_handleToggleCertificateForm();
};
Expand Down Expand Up @@ -175,12 +171,7 @@ export const WorkspaceSettingsModal = ({ workspace, workspaceMeta, clientCertifi
<button
className="btn btn--super-compact width-auto"
title="Enable or disable certificate"
onClick={() =>
workspaceFetcher.submit({ parentId: certificate._id, disabled: !certificate.disabled }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/clientcert/update`,
method: 'post',
encType: 'application/json',
})}
onClick={() => toggleClientCert(certificate)}
>
{certificate.disabled ? (
<i className="fa fa-square-o" />
Expand All @@ -191,11 +182,7 @@ export const WorkspaceSettingsModal = ({ workspace, workspaceMeta, clientCertifi
<PromptButton
className="btn btn--super-compact width-auto"
confirmMessage=""
onClick={() => workspaceFetcher.submit({ certificateId: certificate._id }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/clientcert/delete`,
method: 'post',
encType: 'application/json',
})}
onClick={() => deleteClientCert(certificate)}
>
<i className="fa fa-trash-o" />
</PromptButton>
Expand All @@ -215,6 +202,49 @@ export const WorkspaceSettingsModal = ({ workspace, workspaceMeta, clientCertifi
defaultPreviewMode,
} = state;

const newCaCert = (path: string) => {
workspaceFetcher.submit({ parentId: workspace._id, path }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/cacert/new`,
method: 'post',
encType: 'application/json',
});
};
const deleteCaCert = () => {
workspaceFetcher.submit({}, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/cacert/delete`,
method: 'post',
encType: 'application/json',
});
};
const toggleCaCert = (caCert: CaCertificate) => {
workspaceFetcher.submit({ _id: caCert._id, disabled: !caCert.disabled }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/cacert/update`,
method: 'post',
encType: 'application/json',
});
};
const newClientCert = (certificate: Partial<ClientCertificate>) => {
workspaceFetcher.submit(certificate, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/clientcert/new`,
method: 'post',
encType: 'application/json',
});
};
const deleteClientCert = (certificate: ClientCertificate) => {
workspaceFetcher.submit({ _id: certificate._id }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/clientcert/delete`,
method: 'post',
encType: 'application/json',
});
};
const toggleClientCert = (certificate: ClientCertificate) => {
workspaceFetcher.submit({ _id: certificate._id, disabled: !certificate.disabled }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/clientcert/update`,
method: 'post',
encType: 'application/json',
});
};

return (
<OverlayContainer>
<Modal ref={modalRef} onHide={onHide}>
Expand Down Expand Up @@ -298,13 +328,7 @@ export const WorkspaceSettingsModal = ({ workspace, workspaceMeta, clientCertifi
disabled={caCertificate !== null}
className="btn btn--clicky"
name="PEM file"
onChange={async path => {
workspaceFetcher.submit({ parentId: workspace._id, path }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/cacert/new`,
method: 'post',
encType: 'application/json',
});
}}
onChange={newCaCert}
path={caCertificate?.path || ''}
showFileName
showFileIcon
Expand All @@ -314,14 +338,7 @@ export const WorkspaceSettingsModal = ({ workspace, workspaceMeta, clientCertifi
disabled={caCertificate === null}
className="btn btn--super-compact width-auto"
title="Enable or disable certificate"
onClick={async () => {
invariant(caCertificate, 'CA cert should exist');
workspaceFetcher.submit({ parentId: caCertificate._id, disabled: !caCertificate.disabled }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/clientcert/update`,
method: 'post',
encType: 'application/json',
});
}}
onClick={() => caCertificate && toggleCaCert(caCertificate)}
>
{caCertificate?.disabled !== false ? (
<i className="fa fa-square-o" />
Expand All @@ -334,14 +351,7 @@ export const WorkspaceSettingsModal = ({ workspace, workspaceMeta, clientCertifi
className="btn btn--super-compact width-auto"
confirmMessage=""
doneMessage=""
onClick={() => {
invariant(caCertificate, 'CA cert should exist');
workspaceFetcher.submit({ certificateId: caCertificate._id }, {
action: `/organization/${organizationId}/project/${projectId}/workspace/${workspace._id}/cacert/delete`,
method: 'post',
encType: 'application/json',
});
}}
onClick={deleteCaCert}
>
<i className="fa fa-trash-o" />
</PromptButton>
Expand Down
19 changes: 10 additions & 9 deletions packages/insomnia/src/ui/routes/actions.tsx
Expand Up @@ -900,17 +900,18 @@ export const createNewCaCertificateAction: ActionFunction = async ({ request })

export const updateCaCertificateAction: ActionFunction = async ({ request }) => {
const patch = await request.json();
const caCertificate = await models.caCertificate.getById(patch.parentId);
const caCertificate = await models.caCertificate.getById(patch._id);
invariant(caCertificate, 'CA Certificate not found');
await models.caCertificate.update(caCertificate, patch);
return null;
};

export const deleteCaCertificateAction: ActionFunction = async ({ request }) => {
const { certificateId } = await request.json();
const caCertificate = await models.caCertificate.getById(certificateId);
export const deleteCaCertificateAction: ActionFunction = async ({ params }) => {
const { workspaceId } = params;
invariant(typeof workspaceId === 'string', 'Workspace ID is required');
const caCertificate = await models.caCertificate.findByParentId(workspaceId);
invariant(caCertificate, 'CA Certificate not found');
await models.caCertificate.removeWhere(certificateId);
await models.caCertificate.removeWhere(workspaceId);
return null;
};

Expand All @@ -922,17 +923,17 @@ export const createNewClientCertificateAction: ActionFunction = async ({ request

export const updateClientCertificateAction: ActionFunction = async ({ request }) => {
const patch = await request.json();
const clientCertificate = await models.clientCertificate.getById(patch.parentId);
const clientCertificate = await models.clientCertificate.getById(patch._id);
invariant(clientCertificate, 'CA Certificate not found');
await models.clientCertificate.update(clientCertificate, patch);
return null;
};

export const deleteClientCertificateAction: ActionFunction = async ({ request }) => {
const { certificateId } = await request.json();
const clientCertificate = await models.clientCertificate.getById(certificateId);
const { _id } = await request.json();
const clientCertificate = await models.clientCertificate.getById(_id);
invariant(clientCertificate, 'CA Certificate not found');
await models.clientCertificate.remove(certificateId);
await models.clientCertificate.remove(clientCertificate);
return null;
};

Expand Down

0 comments on commit 74960ec

Please sign in to comment.