move ssh errors from common resource to auth keys service
authorJanicki Artur <artur.janicki@contractors.roche.com>
Wed, 21 Nov 2018 10:14:45 +0000 (11:14 +0100)
committerJanicki Artur <artur.janicki@contractors.roche.com>
Wed, 21 Nov 2018 10:14:45 +0000 (11:14 +0100)
Feature #14479_ssh_keys

Arvados-DCO-1.1-Signed-off-by: Janicki Artur <artur.janicki@contractors.roche.com>

src/services/authorized-keys-service/authorized-keys-service.ts
src/services/common-service/common-resource-service.ts
src/store/auth/auth-action.ts

index b51704a0223ddd84e7f23ae2176973d0ca304ed6..c952f4255244b52526cdf8c8148ec6c7660e35f0 100644 (file)
@@ -4,11 +4,31 @@
 
 import { AxiosInstance } from "axios";
 import { SshKeyResource } from '~/models/ssh-key';
-import { CommonResourceService } from "~/services/common-service/common-resource-service";
+import { CommonResourceService, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
 import { ApiActions } from "~/services/api/api-actions";
 
+export enum AuthorizedKeysServiceError {
+    UNIQUE_PUBLIC_KEY = 'UniquePublicKey',
+    INVALID_PUBLIC_KEY = 'InvalidPublicKey',
+}
+
 export class AuthorizedKeysService extends CommonResourceService<SshKeyResource> {
     constructor(serverApi: AxiosInstance, actions: ApiActions) {
         super(serverApi, "authorized_keys", actions);
     }
-}
\ No newline at end of file
+}
+
+export const getAuthorizedKeysServiceError = (errorResponse: any) => {
+    if ('errors' in errorResponse && 'errorToken' in errorResponse) {
+        const error = errorResponse.errors.join('');
+        switch (true) {
+            case /Public key does not appear to be a valid ssh-rsa or dsa public key/.test(error):
+                return AuthorizedKeysServiceError.INVALID_PUBLIC_KEY;
+            case /Public key already exists in the database, use a different key./.test(error):
+                return AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY;
+            default:
+                return CommonResourceServiceError.UNKNOWN;
+        }
+    }
+    return CommonResourceServiceError.NONE;
+};
\ No newline at end of file
index 02a3379dc2dde5369d31998e275105a11e7b8196..70c1df0e2bc5e665a3c62c05b39b7a50a6705c62 100644 (file)
@@ -35,8 +35,6 @@ export enum CommonResourceServiceError {
     UNIQUE_VIOLATION = 'UniqueViolation',
     OWNERSHIP_CYCLE = 'OwnershipCycle',
     MODIFYING_CONTAINER_REQUEST_FINAL_STATE = 'ModifyingContainerRequestFinalState',
-    UNIQUE_PUBLIC_KEY = 'UniquePublicKey',
-    INVALID_PUBLIC_KEY = 'InvalidPublicKey',
     UNKNOWN = 'Unknown',
     NONE = 'None'
 }
@@ -152,10 +150,6 @@ export const getCommonResourceServiceError = (errorResponse: any) => {
                 return CommonResourceServiceError.OWNERSHIP_CYCLE;
             case /Mounts cannot be modified in state 'Final'/.test(error):
                 return CommonResourceServiceError.MODIFYING_CONTAINER_REQUEST_FINAL_STATE;
-            case /Public key does not appear to be a valid ssh-rsa or dsa public key/.test(error):
-                return CommonResourceServiceError.INVALID_PUBLIC_KEY;
-            case /Public key already exists in the database, use a different key./.test(error):
-                return CommonResourceServiceError.UNIQUE_PUBLIC_KEY;
             default:
                 return CommonResourceServiceError.UNKNOWN;
         }
index 1a4ba0c447d7b25677cbfcf62acb21ca8979812c..00e372a2ecd5206852790da4e32ac2b5bb239c94 100644 (file)
@@ -5,15 +5,15 @@
 import { ofType, unionize, UnionOf } from '~/common/unionize';
 import { Dispatch } from "redux";
 import { reset, stopSubmit } from 'redux-form';
-import { User } from "~/models/user";
-import { RootState } from "../store";
-import { ServiceRepository } from "~/services/services";
-import { getCommonResourceServiceError, CommonResourceServiceError } from '~/services/common-service/common-resource-service';
 import { AxiosInstance } from "axios";
+import { RootState } from "../store";
 import { snackbarActions } from '~/store/snackbar/snackbar-actions';
 import { dialogActions } from '~/store/dialog/dialog-actions';
+import { setBreadcrumbs } from '~/store/breadcrumbs/breadcrumbs-actions';
+import { ServiceRepository } from "~/services/services";
+import { getAuthorizedKeysServiceError, AuthorizedKeysServiceError } from '~/services/authorized-keys-service/authorized-keys-service';
 import { SshKeyCreateFormDialogData, KeyType, SshKeyResource } from '~/models/ssh-key';
-import { setBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
+import { User } from "~/models/user";
 
 export const authActions = unionize({
     SAVE_API_TOKEN: ofType<string>(),
@@ -101,10 +101,10 @@ export const createSshKey = (data: SshKeyCreateFormDialogData) =>
                 hideDuration: 2000
             }));
         } catch (e) {
-            const error = getCommonResourceServiceError(e);
-            if (error === CommonResourceServiceError.UNIQUE_PUBLIC_KEY) {
+            const error = getAuthorizedKeysServiceError(e);
+            if (error === AuthorizedKeysServiceError.UNIQUE_PUBLIC_KEY) {
                 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key already exists.' }));
-            } else if (error === CommonResourceServiceError.INVALID_PUBLIC_KEY) {
+            } else if (error === AuthorizedKeysServiceError.INVALID_PUBLIC_KEY) {
                 dispatch(stopSubmit(SSH_KEY_CREATE_FORM_NAME, { publicKey: 'Public key is invalid' }));
             }
         }