From 13bf073c77ca9fcf75ebd31098a9bf538e96c9c9 Mon Sep 17 00:00:00 2001 From: Janicki Artur Date: Wed, 21 Nov 2018 11:14:45 +0100 Subject: [PATCH] move ssh errors from common resource to auth keys service Feature #14479_ssh_keys Arvados-DCO-1.1-Signed-off-by: Janicki Artur --- .../authorized-keys-service.ts | 24 +++++++++++++++++-- .../common-service/common-resource-service.ts | 6 ----- src/store/auth/auth-action.ts | 16 ++++++------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/services/authorized-keys-service/authorized-keys-service.ts b/src/services/authorized-keys-service/authorized-keys-service.ts index b51704a0..c952f425 100644 --- a/src/services/authorized-keys-service/authorized-keys-service.ts +++ b/src/services/authorized-keys-service/authorized-keys-service.ts @@ -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 { 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 diff --git a/src/services/common-service/common-resource-service.ts b/src/services/common-service/common-resource-service.ts index 02a3379d..70c1df0e 100644 --- a/src/services/common-service/common-resource-service.ts +++ b/src/services/common-service/common-resource-service.ts @@ -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; } diff --git a/src/store/auth/auth-action.ts b/src/store/auth/auth-action.ts index 1a4ba0c4..00e372a2 100644 --- a/src/store/auth/auth-action.ts +++ b/src/store/auth/auth-action.ts @@ -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(), @@ -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' })); } } -- 2.30.2