Merge branch '20272-description-label' refs #20272
[arvados.git] / services / workbench2 / src / store / auth / auth-action-ssh.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { dialogActions } from "store/dialog/dialog-actions";
6 import { Dispatch } from "redux";
7 import { RootState } from "store/store";
8 import { getUserUuid } from "common/getuser";
9 import { ServiceRepository } from "services/services";
10 import { snackbarActions, SnackbarKind } from "store/snackbar/snackbar-actions";
11 import { FormErrors, reset, startSubmit, stopSubmit } from "redux-form";
12 import { KeyType } from "models/ssh-key";
13 import {
14     AuthorizedKeysServiceError,
15     getAuthorizedKeysServiceError
16 } from "services/authorized-keys-service/authorized-keys-service";
17 import { setBreadcrumbs } from "store/breadcrumbs/breadcrumbs-actions";
18 import { authActions } from "store/auth/auth-action";
19
20 export const SSH_KEY_CREATE_FORM_NAME = 'sshKeyCreateFormName';
21 export const SSH_KEY_PUBLIC_KEY_DIALOG = 'sshKeyPublicKeyDialog';
22 export const SSH_KEY_REMOVE_DIALOG = 'sshKeyRemoveDialog';
23 export const SSH_KEY_ATTRIBUTES_DIALOG = 'sshKeyAttributesDialog';
24
25 export interface SshKeyCreateFormDialogData {
26     publicKey: string;
27     name: string;
28 }
29
30 export const openSshKeyCreateDialog = () => dialogActions.OPEN_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME, data: {} });
31
32 export const openPublicKeyDialog = (name: string, publicKey: string) =>
33     dialogActions.OPEN_DIALOG({ id: SSH_KEY_PUBLIC_KEY_DIALOG, data: { name, publicKey } });
34
35 export const openSshKeyAttributesDialog = (uuid: string) =>
36     (dispatch: Dispatch, getState: () => RootState) => {
37         const sshKey = getState().auth.sshKeys.find(it => it.uuid === uuid);
38         dispatch(dialogActions.OPEN_DIALOG({ id: SSH_KEY_ATTRIBUTES_DIALOG, data: { sshKey } }));
39     };
40
41 export const openSshKeyRemoveDialog = (uuid: string) =>
42     (dispatch: Dispatch, getState: () => RootState) => {
43         dispatch(dialogActions.OPEN_DIALOG({
44             id: SSH_KEY_REMOVE_DIALOG,
45             data: {
46                 title: 'Remove public key',
47                 text: 'Are you sure you want to remove this public key?',
48                 confirmButtonLabel: 'Remove',
49                 uuid
50             }
51         }));
52     };
53
54 export const removeSshKey = (uuid: string) =>
55     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Removing ...', kind: SnackbarKind.INFO }));
57         await services.authorizedKeysService.delete(uuid);
58         dispatch(authActions.REMOVE_SSH_KEY(uuid));
59         dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'Public Key has been successfully removed.', hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
60     };
61
62 export const createSshKey = (data: SshKeyCreateFormDialogData) =>
63     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
64         const userUuid = getUserUuid(getState());
65         if (!userUuid) { return; }
66         const { name, publicKey } = data;
67         dispatch(startSubmit(SSH_KEY_CREATE_FORM_NAME));
68         try {
69             const newSshKey = await services.authorizedKeysService.create({
70                 name,
71                 publicKey,
72                 keyType: KeyType.SSH,
73                 authorizedUserUuid: userUuid
74             });
75             dispatch(authActions.ADD_SSH_KEY(newSshKey));
76             dispatch(dialogActions.CLOSE_DIALOG({ id: SSH_KEY_CREATE_FORM_NAME }));
77             dispatch(reset(SSH_KEY_CREATE_FORM_NAME));
78             dispatch(snackbarActions.OPEN_SNACKBAR({
79                 message: "Public key has been successfully created.",
80                 hideDuration: 2000,
81                 kind: SnackbarKind.SUCCESS
82             }));
83         } catch (e) {
84             const error = getAuthorizedKeysServiceError(e);
85             if (error === AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY) {
86                 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key already exists.' } as FormErrors));
87             } else if (error === AuthorizedKeysServiceError.INVALID_PUBLIC_KEY) {
88                 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key is invalid' } as FormErrors));
89             }
90         }
91     };
92
93 export const loadSshKeysPanel = () =>
94     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
95         try {
96             dispatch(setBreadcrumbs([{ label: 'SSH Keys' }]));
97             const response = await services.authorizedKeysService.list();
98             dispatch(authActions.SET_SSH_KEYS(response.items));
99         } catch (e) {
100             return;
101         }
102     };