From c1f08b56ba20bd3dc7e9084fc87a842996702ead Mon Sep 17 00:00:00 2001 From: Emma Casolin Date: Mon, 6 Jun 2022 14:53:12 +1000 Subject: [PATCH] fix: service handlers check for client error chains Sometimes an error is only considered a client error when part of the chain of another error (for example ErrorPolykeyRemote) so it is now possible to specify this array. #304 --- src/agent/service/nodesChainDataGet.ts | 2 +- .../service/nodesClosestLocalNodesGet.ts | 2 +- src/agent/service/nodesCrossSignClaim.ts | 2 +- .../service/nodesHolePunchMessageSend.ts | 2 +- src/agent/service/notificationsSend.ts | 2 +- src/agent/service/vaultsGitInfoGet.ts | 17 +++--- src/agent/service/vaultsGitPackGet.ts | 15 ++--- src/agent/service/vaultsScan.ts | 2 +- src/agent/types.ts | 10 +++- src/agent/utils.ts | 56 +++++++++++++++---- src/client/service/agentLockAll.ts | 2 +- src/client/service/agentStatus.ts | 2 +- src/client/service/agentStop.ts | 2 +- src/client/service/agentUnlock.ts | 2 +- .../service/gestaltsActionsGetByIdentity.ts | 2 +- .../service/gestaltsActionsGetByNode.ts | 2 +- .../service/gestaltsActionsSetByIdentity.ts | 2 +- .../service/gestaltsActionsSetByNode.ts | 2 +- .../service/gestaltsActionsUnsetByIdentity.ts | 2 +- .../service/gestaltsActionsUnsetByNode.ts | 2 +- .../service/gestaltsDiscoveryByIdentity.ts | 2 +- src/client/service/gestaltsDiscoveryByNode.ts | 2 +- .../service/gestaltsGestaltGetByIdentity.ts | 2 +- .../service/gestaltsGestaltGetByNode.ts | 2 +- src/client/service/gestaltsGestaltList.ts | 2 +- .../service/gestaltsGestaltTrustByIdentity.ts | 2 +- .../service/gestaltsGestaltTrustByNode.ts | 2 +- src/client/service/identitiesAuthenticate.ts | 5 +- .../service/identitiesAuthenticatedGet.ts | 2 +- src/client/service/identitiesClaim.ts | 2 +- .../service/identitiesInfoConnectedGet.ts | 2 +- src/client/service/identitiesInfoGet.ts | 2 +- src/client/service/identitiesProvidersList.ts | 2 +- src/client/service/identitiesTokenDelete.ts | 2 +- src/client/service/identitiesTokenGet.ts | 2 +- src/client/service/identitiesTokenPut.ts | 2 +- src/client/service/keysCertsChainGet.ts | 2 +- src/client/service/keysCertsGet.ts | 2 +- src/client/service/keysDecrypt.ts | 2 +- src/client/service/keysEncrypt.ts | 2 +- src/client/service/keysKeyPairRenew.ts | 2 +- src/client/service/keysKeyPairReset.ts | 2 +- src/client/service/keysKeyPairRoot.ts | 2 +- src/client/service/keysPasswordChange.ts | 2 +- src/client/service/keysSign.ts | 2 +- src/client/service/keysVerify.ts | 2 +- src/client/service/nodesAdd.ts | 2 +- src/client/service/nodesClaim.ts | 2 +- src/client/service/nodesFind.ts | 2 +- src/client/service/nodesPing.ts | 2 +- src/client/service/notificationsClear.ts | 2 +- src/client/service/notificationsRead.ts | 2 +- src/client/service/notificationsSend.ts | 2 +- src/client/service/vaultsClone.ts | 4 +- src/client/service/vaultsCreate.ts | 5 +- src/client/service/vaultsDelete.ts | 35 ++++-------- src/client/service/vaultsList.ts | 2 +- src/client/service/vaultsLog.ts | 32 +++-------- src/client/service/vaultsPermissionGet.ts | 35 ++++-------- src/client/service/vaultsPermissionSet.ts | 22 +++----- src/client/service/vaultsPermissionUnset.ts | 18 +++--- src/client/service/vaultsPull.ts | 21 ++++--- src/client/service/vaultsRename.ts | 31 +++------- src/client/service/vaultsScan.ts | 2 +- src/client/service/vaultsSecretsDelete.ts | 32 +++-------- src/client/service/vaultsSecretsEdit.ts | 32 +++-------- src/client/service/vaultsSecretsGet.ts | 32 +++-------- src/client/service/vaultsSecretsList.ts | 35 ++++-------- src/client/service/vaultsSecretsMkdir.ts | 32 +++-------- src/client/service/vaultsSecretsNew.ts | 32 +++-------- src/client/service/vaultsSecretsNewDir.ts | 35 ++++-------- src/client/service/vaultsSecretsRename.ts | 36 ++++-------- src/client/service/vaultsSecretsStat.ts | 32 +++-------- src/client/service/vaultsVersion.ts | 32 +++-------- src/client/types.ts | 8 ++- src/client/utils/utils.ts | 52 ++++++++++++++--- tests/bin/vaults/vaults.test.ts | 4 +- tests/vaults/VaultManager.test.ts | 3 +- 78 files changed, 345 insertions(+), 458 deletions(-) diff --git a/src/agent/service/nodesChainDataGet.ts b/src/agent/service/nodesChainDataGet.ts index 0e09ff35f5..e0f838b33b 100644 --- a/src/agent/service/nodesChainDataGet.ts +++ b/src/agent/service/nodesChainDataGet.ts @@ -51,7 +51,7 @@ function nodesChainDataGet({ return; } catch (e) { callback(grpcUtils.fromError(e, true)); - !agentUtils.isClientError(e) && logger.error(e); + !agentUtils.isAgentClientError(e) && logger.error(e); return; } }; diff --git a/src/agent/service/nodesClosestLocalNodesGet.ts b/src/agent/service/nodesClosestLocalNodesGet.ts index e28fbd191b..5c2c0e2049 100644 --- a/src/agent/service/nodesClosestLocalNodesGet.ts +++ b/src/agent/service/nodesClosestLocalNodesGet.ts @@ -67,7 +67,7 @@ function nodesClosestLocalNodesGet({ return; } catch (e) { callback(grpcUtils.fromError(e, true)); - !agentUtils.isClientError(e) && logger.error(e); + !agentUtils.isAgentClientError(e) && logger.error(e); return; } }; diff --git a/src/agent/service/nodesCrossSignClaim.ts b/src/agent/service/nodesCrossSignClaim.ts index 63b6304c9e..20d5ac575d 100644 --- a/src/agent/service/nodesCrossSignClaim.ts +++ b/src/agent/service/nodesCrossSignClaim.ts @@ -173,7 +173,7 @@ function nodesCrossSignClaim({ }); } catch (e) { await genClaims.throw(e); - !agentUtils.isClientError(e, [ + !agentUtils.isAgentClientError(e, [ claimsErrors.ErrorEmptyStream, claimsErrors.ErrorUndefinedSinglySignedClaim, claimsErrors.ErrorUndefinedSignature, diff --git a/src/agent/service/nodesHolePunchMessageSend.ts b/src/agent/service/nodesHolePunchMessageSend.ts index 1ec86a35c3..0887872646 100644 --- a/src/agent/service/nodesHolePunchMessageSend.ts +++ b/src/agent/service/nodesHolePunchMessageSend.ts @@ -74,7 +74,7 @@ function nodesHolePunchMessageSend({ return; } catch (e) { callback(grpcUtils.fromError(e, true)); - !agentUtils.isClientError(e) && logger.error(e); + !agentUtils.isAgentClientError(e) && logger.error(e); return; } }; diff --git a/src/agent/service/notificationsSend.ts b/src/agent/service/notificationsSend.ts index 6108cbe74a..eb336243cf 100644 --- a/src/agent/service/notificationsSend.ts +++ b/src/agent/service/notificationsSend.ts @@ -36,7 +36,7 @@ function notificationsSend({ return; } catch (e) { callback(grpcUtils.fromError(e, true)); - !agentUtils.isClientError(e, [ + !agentUtils.isAgentClientError(e, [ notificationsErrors.ErrorNotificationsInvalidType, notificationsErrors.ErrorNotificationsValidationFailed, notificationsErrors.ErrorNotificationsParse, diff --git a/src/agent/service/vaultsGitInfoGet.ts b/src/agent/service/vaultsGitInfoGet.ts index aecdde37ef..7fe9831fff 100644 --- a/src/agent/service/vaultsGitInfoGet.ts +++ b/src/agent/service/vaultsGitInfoGet.ts @@ -1,5 +1,5 @@ import type { DB } from '@matrixai/db'; -import type { VaultName, VaultId, VaultAction } from '../../vaults/types'; +import type { VaultName, VaultAction } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type ACL from '../../acl/ACL'; import type { ConnectionInfoGet } from '../../agent/types'; @@ -39,25 +39,24 @@ function vaultsGitInfoGet({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const { - vaultId, actionType, }: { - vaultId: VaultId; actionType: VaultAction; } = validateSync( (keyPath, value) => { return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], [['actionType'], () => validationUtils.parseVaultAction(value)], () => value, ); }, { - vaultId: call.request.getVault()?.getNameOrId(), actionType: call.request.getAction(), }, ); @@ -108,7 +107,7 @@ function vaultsGitInfoGet({ return; } catch (e) { await genWritable.throw(e); - !agentUtils.isClientError(e, [ + !agentUtils.isAgentClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, agentErrors.ErrorConnectionInfoMissing, vaultsErrors.ErrorVaultsPermissionDenied, diff --git a/src/agent/service/vaultsGitPackGet.ts b/src/agent/service/vaultsGitPackGet.ts index 276cec84fc..50db9da28a 100644 --- a/src/agent/service/vaultsGitPackGet.ts +++ b/src/agent/service/vaultsGitPackGet.ts @@ -1,6 +1,6 @@ import type * as grpc from '@grpc/grpc-js'; import type { DB } from '@matrixai/db'; -import type { VaultName, VaultId, VaultAction } from '../../vaults/types'; +import type { VaultName, VaultAction } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type { ConnectionInfoGet } from '../../agent/types'; import type ACL from '../../acl/ACL'; @@ -62,25 +62,22 @@ function vaultsGitPackGet({ nameOrId as VaultName, tran, ); + const vaultId = vaultIdFromName ?? vaultsUtils.decodeVaultId(nameOrId); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const { - vaultId, actionType, }: { - vaultId: VaultId; actionType: VaultAction; } = validateSync( (keyPath, value) => { return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], [['actionType'], () => validationUtils.parseVaultAction(value)], () => value, ); }, { - vaultId: nameOrId, actionType: meta.get('vaultAction').pop()!.toString(), }, ); @@ -123,7 +120,7 @@ function vaultsGitPackGet({ return; } catch (e) { await genDuplex.throw(e); - !agentUtils.isClientError(e, [ + !agentUtils.isAgentClientError(e, [ agentErrors.ErrorConnectionInfoMissing, vaultsErrors.ErrorVaultsPermissionDenied, vaultsErrors.ErrorVaultsVaultUndefined, diff --git a/src/agent/service/vaultsScan.ts b/src/agent/service/vaultsScan.ts index 42e6ca8eba..f1a94c2a97 100644 --- a/src/agent/service/vaultsScan.ts +++ b/src/agent/service/vaultsScan.ts @@ -53,7 +53,7 @@ function vaultsScan({ return; } catch (e) { await genWritable.throw(e); - !agentUtils.isClientError(e, [ + !agentUtils.isAgentClientError(e, [ agentErrors.ErrorConnectionInfoMissing, vaultsErrors.ErrorVaultsPermissionDenied, ]) && logger.error(e); diff --git a/src/agent/types.ts b/src/agent/types.ts index ced17bbf1b..b6d0e02598 100644 --- a/src/agent/types.ts +++ b/src/agent/types.ts @@ -1,8 +1,14 @@ -import type { ConnectionInfo } from 'network/types'; import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call'; +import type { Class } from '@matrixai/errors'; +import type { ConnectionInfo } from '../network/types'; +import type ErrorPolykey from '../ErrorPolykey'; type ConnectionInfoGet = ( call: ServerSurfaceCall, ) => ConnectionInfo | undefined; -export type { ConnectionInfoGet }; +type AgentClientErrors = Array< + Class> | Array>> +>; + +export type { ConnectionInfoGet, AgentClientErrors }; diff --git a/src/agent/utils.ts b/src/agent/utils.ts index 94b7d97d59..48f9ff59fd 100644 --- a/src/agent/utils.ts +++ b/src/agent/utils.ts @@ -1,15 +1,15 @@ import type { ServerSurfaceCall } from '@grpc/grpc-js/build/src/server-call'; -import type { Class } from '@matrixai/errors'; -import type { Host, Port } from 'network/types'; -import type Proxy from 'network/Proxy'; -import type { ConnectionInfoGet } from './types'; +import type ErrorPolykey from '../ErrorPolykey'; +import type { Host, Port } from '../network/types'; +import type Proxy from '../network/Proxy'; +import type { ConnectionInfoGet, AgentClientErrors } from './types'; import * as validationErrors from '../validation/errors'; /** * Array of errors that are always considered to be "client errors" * (4xx errors in HTTP) in the context of the agent service */ -const defaultClientErrors: Array> = [ +const defaultClientErrors: AgentClientErrors = [ validationErrors.ErrorValidation, ]; @@ -33,19 +33,53 @@ function connectionInfoGetter(proxy: Proxy): ConnectionInfoGet { * context of a given handler can be supplied in the `extraClientErrors` * argument */ -function isClientError( - e: Error, - extraClientErrors?: Array>, +function isAgentClientError( + thrownError: ErrorPolykey, + extraClientErrors?: AgentClientErrors, ): boolean { for (const error of defaultClientErrors) { - if (e instanceof error) return true; + if (Array.isArray(error)) { + let e = thrownError; + let matches = true; + for (const eType of error) { + if (e == null) { + matches = false; + break; + } + if (!(e instanceof eType)) { + matches = false; + break; + } + e = e.cause; + } + if (matches) return true; + } else if (thrownError instanceof error) { + return true; + } } if (extraClientErrors) { for (const error of extraClientErrors) { - if (e instanceof error) return true; + if (Array.isArray(error)) { + let e = thrownError; + let matches = true; + for (const eType of error) { + if (e == null) { + matches = false; + break; + } + if (!(e instanceof eType)) { + matches = false; + break; + } + e = e.cause; + } + if (matches) return true; + } else if (thrownError instanceof error) { + return true; + } } } return false; } -export { connectionInfoGetter, isClientError }; +export { connectionInfoGetter, isAgentClientError }; diff --git a/src/client/service/agentLockAll.ts b/src/client/service/agentLockAll.ts index 90ad0a93a6..29e64bcada 100644 --- a/src/client/service/agentLockAll.ts +++ b/src/client/service/agentLockAll.ts @@ -33,7 +33,7 @@ function agentLockAll({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/agentStatus.ts b/src/client/service/agentStatus.ts index 52d14144a2..110555cf8b 100644 --- a/src/client/service/agentStatus.ts +++ b/src/client/service/agentStatus.ts @@ -50,7 +50,7 @@ function agentStatus({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/agentStop.ts b/src/client/service/agentStop.ts index a745887e53..f88885f09c 100644 --- a/src/client/service/agentStop.ts +++ b/src/client/service/agentStop.ts @@ -33,7 +33,7 @@ function agentStop({ callback(null, response); } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } // Stop is called after GRPC resources are cleared diff --git a/src/client/service/agentUnlock.ts b/src/client/service/agentUnlock.ts index 1f726792d6..5147e27181 100644 --- a/src/client/service/agentUnlock.ts +++ b/src/client/service/agentUnlock.ts @@ -24,7 +24,7 @@ function agentUnlock({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/gestaltsActionsGetByIdentity.ts b/src/client/service/gestaltsActionsGetByIdentity.ts index bd0d8ba047..ef45d57e7f 100644 --- a/src/client/service/gestaltsActionsGetByIdentity.ts +++ b/src/client/service/gestaltsActionsGetByIdentity.ts @@ -63,7 +63,7 @@ function gestaltsActionsGetByIdentity({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/gestaltsActionsGetByNode.ts b/src/client/service/gestaltsActionsGetByNode.ts index 11187384b1..b5652d3bb6 100644 --- a/src/client/service/gestaltsActionsGetByNode.ts +++ b/src/client/service/gestaltsActionsGetByNode.ts @@ -57,7 +57,7 @@ function gestaltsActionsGetByNode({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/gestaltsActionsSetByIdentity.ts b/src/client/service/gestaltsActionsSetByIdentity.ts index 3776bbebba..0628b2a986 100644 --- a/src/client/service/gestaltsActionsSetByIdentity.ts +++ b/src/client/service/gestaltsActionsSetByIdentity.ts @@ -68,7 +68,7 @@ function gestaltsActionsSetByIdentity({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ gestaltsErrors.ErrorGestaltsGraphIdentityIdMissing, gestaltsErrors.ErrorGestaltsGraphNodeIdMissing, ]) && logger.error(e); diff --git a/src/client/service/gestaltsActionsSetByNode.ts b/src/client/service/gestaltsActionsSetByNode.ts index 91cef278f7..2aa9a7050d 100644 --- a/src/client/service/gestaltsActionsSetByNode.ts +++ b/src/client/service/gestaltsActionsSetByNode.ts @@ -54,7 +54,7 @@ function gestaltsActionsSetByNode({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ gestaltsErrors.ErrorGestaltsGraphNodeIdMissing, ]) && logger.error(e); return; diff --git a/src/client/service/gestaltsActionsUnsetByIdentity.ts b/src/client/service/gestaltsActionsUnsetByIdentity.ts index 1b20a56103..88745bc64b 100644 --- a/src/client/service/gestaltsActionsUnsetByIdentity.ts +++ b/src/client/service/gestaltsActionsUnsetByIdentity.ts @@ -68,7 +68,7 @@ function gestaltsActionsUnsetByIdentity({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ gestaltsErrors.ErrorGestaltsGraphIdentityIdMissing, gestaltsErrors.ErrorGestaltsGraphNodeIdMissing, ]) && logger.error(e); diff --git a/src/client/service/gestaltsActionsUnsetByNode.ts b/src/client/service/gestaltsActionsUnsetByNode.ts index 1f06d2c640..d7a62d68af 100644 --- a/src/client/service/gestaltsActionsUnsetByNode.ts +++ b/src/client/service/gestaltsActionsUnsetByNode.ts @@ -54,7 +54,7 @@ function gestaltsActionsUnsetByNode({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ gestaltsErrors.ErrorGestaltsGraphNodeIdMissing, ]) && logger.error(e); return; diff --git a/src/client/service/gestaltsDiscoveryByIdentity.ts b/src/client/service/gestaltsDiscoveryByIdentity.ts index b63bb83bf0..37efbf7d30 100644 --- a/src/client/service/gestaltsDiscoveryByIdentity.ts +++ b/src/client/service/gestaltsDiscoveryByIdentity.ts @@ -52,7 +52,7 @@ function gestaltsDiscoveryByIdentity({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/gestaltsDiscoveryByNode.ts b/src/client/service/gestaltsDiscoveryByNode.ts index b37bd777c9..0c8d1ea4cf 100644 --- a/src/client/service/gestaltsDiscoveryByNode.ts +++ b/src/client/service/gestaltsDiscoveryByNode.ts @@ -48,7 +48,7 @@ function gestaltsDiscoveryByNode({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/gestaltsGestaltGetByIdentity.ts b/src/client/service/gestaltsGestaltGetByIdentity.ts index 9396c3d6d2..81e3ffc549 100644 --- a/src/client/service/gestaltsGestaltGetByIdentity.ts +++ b/src/client/service/gestaltsGestaltGetByIdentity.ts @@ -60,7 +60,7 @@ function gestaltsGestaltGetByIdentity({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/gestaltsGestaltGetByNode.ts b/src/client/service/gestaltsGestaltGetByNode.ts index 2fbbb8447f..fb3e63f56c 100644 --- a/src/client/service/gestaltsGestaltGetByNode.ts +++ b/src/client/service/gestaltsGestaltGetByNode.ts @@ -56,7 +56,7 @@ function gestaltsGestaltGetByNode({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/gestaltsGestaltList.ts b/src/client/service/gestaltsGestaltList.ts index cbe2e0027c..7b4725892e 100644 --- a/src/client/service/gestaltsGestaltList.ts +++ b/src/client/service/gestaltsGestaltList.ts @@ -40,7 +40,7 @@ function gestaltsGestaltList({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/gestaltsGestaltTrustByIdentity.ts b/src/client/service/gestaltsGestaltTrustByIdentity.ts index 9fc7c4911b..36a0aa9825 100644 --- a/src/client/service/gestaltsGestaltTrustByIdentity.ts +++ b/src/client/service/gestaltsGestaltTrustByIdentity.ts @@ -84,7 +84,7 @@ function gestaltsGestaltTrustByIdentity({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ gestaltsErrors.ErrorGestaltsGraphIdentityIdMissing, gestaltsErrors.ErrorGestaltsGraphNodeIdMissing, ]) && logger.error(e); diff --git a/src/client/service/gestaltsGestaltTrustByNode.ts b/src/client/service/gestaltsGestaltTrustByNode.ts index 9be7fa6281..8fa7299a09 100644 --- a/src/client/service/gestaltsGestaltTrustByNode.ts +++ b/src/client/service/gestaltsGestaltTrustByNode.ts @@ -71,7 +71,7 @@ function gestaltsGestaltTrustByNode({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ gestaltsErrors.ErrorGestaltsGraphNodeIdMissing, ]) && logger.error(e); return; diff --git a/src/client/service/identitiesAuthenticate.ts b/src/client/service/identitiesAuthenticate.ts index 9aa0927e0b..d9c216c6cc 100644 --- a/src/client/service/identitiesAuthenticate.ts +++ b/src/client/service/identitiesAuthenticate.ts @@ -75,8 +75,9 @@ function identitiesAuthenticate({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e, [identitiesErrors.ErrorProviderMissing]) && - logger.error(e); + !clientUtils.isClientClientError(e, [ + identitiesErrors.ErrorProviderMissing, + ]) && logger.error(e); return; } }; diff --git a/src/client/service/identitiesAuthenticatedGet.ts b/src/client/service/identitiesAuthenticatedGet.ts index 64d0d72320..3ab4cc941f 100644 --- a/src/client/service/identitiesAuthenticatedGet.ts +++ b/src/client/service/identitiesAuthenticatedGet.ts @@ -64,7 +64,7 @@ function identitiesAuthenticatedGet({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/identitiesClaim.ts b/src/client/service/identitiesClaim.ts index 884a5ca1cb..de5e1df57e 100644 --- a/src/client/service/identitiesClaim.ts +++ b/src/client/service/identitiesClaim.ts @@ -93,7 +93,7 @@ function identitiesClaim({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ identitiesErrors.ErrorProviderMissing, identitiesErrors.ErrorProviderUnauthenticated, ]) && logger.error(e); diff --git a/src/client/service/identitiesInfoConnectedGet.ts b/src/client/service/identitiesInfoConnectedGet.ts index 7ac96810c6..63d6817004 100644 --- a/src/client/service/identitiesInfoConnectedGet.ts +++ b/src/client/service/identitiesInfoConnectedGet.ts @@ -120,7 +120,7 @@ function identitiesInfoConnectedGet({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ identitiesErrors.ErrorProviderMissing, identitiesErrors.ErrorProviderUnauthenticated, identitiesErrors.ErrorProviderUnimplemented, diff --git a/src/client/service/identitiesInfoGet.ts b/src/client/service/identitiesInfoGet.ts index bd29cf2f3e..e8df1153b6 100644 --- a/src/client/service/identitiesInfoGet.ts +++ b/src/client/service/identitiesInfoGet.ts @@ -113,7 +113,7 @@ function identitiesInfoGet({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ identitiesErrors.ErrorProviderMissing, identitiesErrors.ErrorProviderUnauthenticated, ]) && logger.error(e); diff --git a/src/client/service/identitiesProvidersList.ts b/src/client/service/identitiesProvidersList.ts index cd451d2f9b..70cbc9ccda 100644 --- a/src/client/service/identitiesProvidersList.ts +++ b/src/client/service/identitiesProvidersList.ts @@ -30,7 +30,7 @@ function identitiesProvidersList({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/identitiesTokenDelete.ts b/src/client/service/identitiesTokenDelete.ts index 9abe9d6240..779ffea475 100644 --- a/src/client/service/identitiesTokenDelete.ts +++ b/src/client/service/identitiesTokenDelete.ts @@ -57,7 +57,7 @@ function identitiesTokenDelete({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/identitiesTokenGet.ts b/src/client/service/identitiesTokenGet.ts index 193cf25533..5ca76f585e 100644 --- a/src/client/service/identitiesTokenGet.ts +++ b/src/client/service/identitiesTokenGet.ts @@ -57,7 +57,7 @@ function identitiesTokenGet({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/identitiesTokenPut.ts b/src/client/service/identitiesTokenPut.ts index c06ad81062..261b88a12f 100644 --- a/src/client/service/identitiesTokenPut.ts +++ b/src/client/service/identitiesTokenPut.ts @@ -67,7 +67,7 @@ function identitiesTokenPut({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysCertsChainGet.ts b/src/client/service/keysCertsChainGet.ts index 94418b7b69..fed99bf2a9 100644 --- a/src/client/service/keysCertsChainGet.ts +++ b/src/client/service/keysCertsChainGet.ts @@ -34,7 +34,7 @@ function keysCertsChainGet({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysCertsGet.ts b/src/client/service/keysCertsGet.ts index da8792aa1e..1b82226121 100644 --- a/src/client/service/keysCertsGet.ts +++ b/src/client/service/keysCertsGet.ts @@ -30,7 +30,7 @@ function keysCertsGet({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysDecrypt.ts b/src/client/service/keysDecrypt.ts index 092e7f67bf..b1a0f6bad7 100644 --- a/src/client/service/keysDecrypt.ts +++ b/src/client/service/keysDecrypt.ts @@ -31,7 +31,7 @@ function keysDecrypt({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysEncrypt.ts b/src/client/service/keysEncrypt.ts index df8f4ff625..b8c5c60bb4 100644 --- a/src/client/service/keysEncrypt.ts +++ b/src/client/service/keysEncrypt.ts @@ -31,7 +31,7 @@ function keysEncrypt({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysKeyPairRenew.ts b/src/client/service/keysKeyPairRenew.ts index 3660c9bc55..ada854cad8 100644 --- a/src/client/service/keysKeyPairRenew.ts +++ b/src/client/service/keysKeyPairRenew.ts @@ -31,7 +31,7 @@ function keysKeyPairRenew({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysKeyPairReset.ts b/src/client/service/keysKeyPairReset.ts index 3d04f12aad..af3b254264 100644 --- a/src/client/service/keysKeyPairReset.ts +++ b/src/client/service/keysKeyPairReset.ts @@ -31,7 +31,7 @@ function keysKeyPairReset({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysKeyPairRoot.ts b/src/client/service/keysKeyPairRoot.ts index ad1eb3e5a4..14f543d9f1 100644 --- a/src/client/service/keysKeyPairRoot.ts +++ b/src/client/service/keysKeyPairRoot.ts @@ -31,7 +31,7 @@ function keysKeyPairRoot({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysPasswordChange.ts b/src/client/service/keysPasswordChange.ts index b96a44ca10..b1d116db53 100644 --- a/src/client/service/keysPasswordChange.ts +++ b/src/client/service/keysPasswordChange.ts @@ -29,7 +29,7 @@ function keysPasswordChange({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysSign.ts b/src/client/service/keysSign.ts index bae99915f7..9c3aa2ab3d 100644 --- a/src/client/service/keysSign.ts +++ b/src/client/service/keysSign.ts @@ -32,7 +32,7 @@ function keysSign({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/keysVerify.ts b/src/client/service/keysVerify.ts index a7cfaae3aa..1c53fff74c 100644 --- a/src/client/service/keysVerify.ts +++ b/src/client/service/keysVerify.ts @@ -33,7 +33,7 @@ function keysVerify({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/nodesAdd.ts b/src/client/service/nodesAdd.ts index 8c089ff0b0..079d2eee2d 100644 --- a/src/client/service/nodesAdd.ts +++ b/src/client/service/nodesAdd.ts @@ -74,7 +74,7 @@ function nodesAdd({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/nodesClaim.ts b/src/client/service/nodesClaim.ts index 4022e10c7c..c9e1d6db9a 100644 --- a/src/client/service/nodesClaim.ts +++ b/src/client/service/nodesClaim.ts @@ -77,7 +77,7 @@ function nodesClaim({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ nodesErrors.ErrorNodeGraphNodeIdNotFound, ]) && logger.error(e); return; diff --git a/src/client/service/nodesFind.ts b/src/client/service/nodesFind.ts index 4826f9d96c..6c2061719c 100644 --- a/src/client/service/nodesFind.ts +++ b/src/client/service/nodesFind.ts @@ -59,7 +59,7 @@ function nodesFind({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ nodesErrors.ErrorNodeGraphNodeIdNotFound, ]) && logger.error(e); return; diff --git a/src/client/service/nodesPing.ts b/src/client/service/nodesPing.ts index f86c8c1fae..5adc83f541 100644 --- a/src/client/service/nodesPing.ts +++ b/src/client/service/nodesPing.ts @@ -53,7 +53,7 @@ function nodesPing({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ nodesErrors.ErrorNodeGraphNodeIdNotFound, ]) && logger.error(e); return; diff --git a/src/client/service/notificationsClear.ts b/src/client/service/notificationsClear.ts index 8d1d501c11..acc05724e4 100644 --- a/src/client/service/notificationsClear.ts +++ b/src/client/service/notificationsClear.ts @@ -33,7 +33,7 @@ function notificationsClear({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/notificationsRead.ts b/src/client/service/notificationsRead.ts index 60d55b7c71..4e6cbb92da 100644 --- a/src/client/service/notificationsRead.ts +++ b/src/client/service/notificationsRead.ts @@ -75,7 +75,7 @@ function notificationsRead({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/notificationsSend.ts b/src/client/service/notificationsSend.ts index c592aa476b..fcdb72606c 100644 --- a/src/client/service/notificationsSend.ts +++ b/src/client/service/notificationsSend.ts @@ -56,7 +56,7 @@ function notificationsSend({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ nodesErrors.ErrorNodeGraphNodeIdNotFound, ]) && logger.error(e); return; diff --git a/src/client/service/vaultsClone.ts b/src/client/service/vaultsClone.ts index 86129010df..ec60df21a0 100644 --- a/src/client/service/vaultsClone.ts +++ b/src/client/service/vaultsClone.ts @@ -6,6 +6,7 @@ import type * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; import * as grpcUtils from '../../grpc/utils'; +import * as grpcErrors from '../../grpc/errors'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; import * as nodesErrors from '../../nodes/errors'; import { validateSync } from '../../validation'; @@ -63,9 +64,10 @@ function vaultsClone({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ nodesErrors.ErrorNodeGraphNodeIdNotFound, vaultsErrors.ErrorVaultsNameConflict, + [grpcErrors.ErrorPolykeyRemote, vaultsErrors.ErrorVaultsVaultUndefined], ]) && logger.error(e); return; } diff --git a/src/client/service/vaultsCreate.ts b/src/client/service/vaultsCreate.ts index 4175a7de02..c181b4b768 100644 --- a/src/client/service/vaultsCreate.ts +++ b/src/client/service/vaultsCreate.ts @@ -39,8 +39,9 @@ function vaultsCreate({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [vaultsErrors.ErrorVaultsVaultDefined]) && - logger.error(e); + !clientUtils.isClientClientError(e, [ + vaultsErrors.ErrorVaultsVaultDefined, + ]) && logger.error(e); return; } }; diff --git a/src/client/service/vaultsDelete.ts b/src/client/service/vaultsDelete.ts index 6aea1c1c0a..61fbfbc272 100644 --- a/src/client/service/vaultsDelete.ts +++ b/src/client/service/vaultsDelete.ts @@ -1,5 +1,5 @@ import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type * as grpc from '@grpc/grpc-js'; import type { DB } from '@matrixai/db'; @@ -7,10 +7,8 @@ import type * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; import type Logger from '@matrixai/logger'; import * as grpcUtils from '../../grpc/utils'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as vaultsErrors from '../../vaults/errors'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsDelete({ @@ -37,24 +35,12 @@ function vaultsDelete({ call.request.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } await vaultManager.destroyVault(vaultId, tran); }); response.setSuccess(true); @@ -62,8 +48,9 @@ function vaultsDelete({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [vaultsErrors.ErrorVaultsVaultUndefined]) && - logger.error(e); + !clientUtils.isClientClientError(e, [ + vaultsErrors.ErrorVaultsVaultUndefined, + ]) && logger.error(e); return; } }; diff --git a/src/client/service/vaultsList.ts b/src/client/service/vaultsList.ts index 08646c59bb..2e9f1a79ae 100644 --- a/src/client/service/vaultsList.ts +++ b/src/client/service/vaultsList.ts @@ -40,7 +40,7 @@ function vaultsList({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e) && logger.error(e); return; } }; diff --git a/src/client/service/vaultsLog.ts b/src/client/service/vaultsLog.ts index 88873dca55..32d014b058 100644 --- a/src/client/service/vaultsLog.ts +++ b/src/client/service/vaultsLog.ts @@ -1,16 +1,14 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; import { Timestamp } from 'google-protobuf/google/protobuf/timestamp_pb'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as vaultsErrors from '../../vaults/errors'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsLog({ @@ -36,24 +34,12 @@ function vaultsLog({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } // Getting the log const depth = call.request.getLogDepth(); let commitId: string | undefined = call.request.getCommitId(); @@ -80,7 +66,7 @@ function vaultsLog({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorVaultReferenceInvalid, ]) && logger.error(e); diff --git a/src/client/service/vaultsPermissionGet.ts b/src/client/service/vaultsPermissionGet.ts index d612f5b8c8..86377f5314 100644 --- a/src/client/service/vaultsPermissionGet.ts +++ b/src/client/service/vaultsPermissionGet.ts @@ -2,7 +2,7 @@ import type * as grpc from '@grpc/grpc-js'; import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; import type VaultManager from '../../vaults/VaultManager'; -import type { VaultName, VaultId, VaultActions } from '../../vaults/types'; +import type { VaultName, VaultActions } from '../../vaults/types'; import type ACL from '../../acl/ACL'; import type { NodeId, NodeIdEncoded } from 'nodes/types'; import type Logger from '@matrixai/logger'; @@ -10,10 +10,9 @@ import { IdInternal } from '@matrixai/id'; import * as grpcUtils from '../../grpc/utils'; import * as nodesPB from '../../proto/js/polykey/v1/nodes/nodes_pb'; import * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; +import * as vaultsErrors from '../../vaults/errors'; import * as nodesUtils from '../../nodes/utils'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsPermissionGet({ @@ -43,24 +42,12 @@ function vaultsPermissionGet({ call.request.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } // Getting permissions return [await acl.getVaultPerm(vaultId, tran), vaultId]; }, @@ -86,7 +73,9 @@ function vaultsPermissionGet({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e) && logger.error(e); + !clientUtils.isClientClientError(e, [ + vaultsErrors.ErrorVaultsVaultUndefined, + ]) && logger.error(e); return; } }; diff --git a/src/client/service/vaultsPermissionSet.ts b/src/client/service/vaultsPermissionSet.ts index 335165ef7a..9bc2a3b8d1 100644 --- a/src/client/service/vaultsPermissionSet.ts +++ b/src/client/service/vaultsPermissionSet.ts @@ -1,12 +1,7 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; import type { NodeId } from '../../nodes/types'; -import type { - VaultName, - VaultId, - VaultAction, - VaultActions, -} from '../../vaults/types'; +import type { VaultName, VaultAction, VaultActions } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type GestaltGraph from '../../gestalts/GestaltGraph'; import type ACL from '../../acl/ACL'; @@ -55,29 +50,28 @@ function vaultsPermissionSet({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const { nodeId, - vaultId, actions, }: { nodeId: NodeId; - vaultId: VaultId; actions: Array; } = validateSync( (keyPath, value) => { return matchSync(keyPath)( [['nodeId'], () => validationUtils.parseNodeId(value)], - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], [['actions'], () => value.map(validationUtils.parseVaultAction)], () => value, ); }, { nodeId: call.request.getNode()?.getNodeId(), - vaultId: call.request.getVault()?.getNameOrId(), actions: call.request.getVaultPermissionsList(), }, ); @@ -105,7 +99,7 @@ function vaultsPermissionSet({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, aclErrors.ErrorACLNodeIdMissing, nodesErrors.ErrorNodeGraphNodeIdNotFound, diff --git a/src/client/service/vaultsPermissionUnset.ts b/src/client/service/vaultsPermissionUnset.ts index 9b561b1732..7906207a34 100644 --- a/src/client/service/vaultsPermissionUnset.ts +++ b/src/client/service/vaultsPermissionUnset.ts @@ -1,13 +1,14 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; import type { NodeId } from '../../nodes/types'; -import type { VaultName, VaultId, VaultAction } from '../../vaults/types'; +import type { VaultName, VaultAction } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type GestaltGraph from '../../gestalts/GestaltGraph'; import type ACL from '../../acl/ACL'; import type * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; +import * as vaultsUtils from '../../vaults/utils'; import * as vaultsErrors from '../../vaults/errors'; import { validateSync } from '../../validation'; import * as validationUtils from '../../validation/utils'; @@ -45,29 +46,28 @@ function vaultsPermissionUnset({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const { nodeId, - vaultId, actions, }: { nodeId: NodeId; - vaultId: VaultId; actions: Array; } = validateSync( (keyPath, value) => { return matchSync(keyPath)( [['nodeId'], () => validationUtils.parseNodeId(value)], - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], [['actions'], () => value.map(validationUtils.parseVaultAction)], () => value, ); }, { nodeId: call.request.getNode()?.getNodeId(), - vaultId: call.request.getVault()?.getNameOrId(), actions: call.request.getVaultPermissionsList(), }, ); @@ -99,7 +99,7 @@ function vaultsPermissionUnset({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, gestaltsErrors.ErrorGestaltsGraphNodeIdMissing, ]) && logger.error(e); diff --git a/src/client/service/vaultsPull.ts b/src/client/service/vaultsPull.ts index 6f7d191062..00fa448803 100644 --- a/src/client/service/vaultsPull.ts +++ b/src/client/service/vaultsPull.ts @@ -1,16 +1,18 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; import type VaultManager from '../../vaults/VaultManager'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type { NodeId } from '../../nodes/types'; import type * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; import * as grpcUtils from '../../grpc/utils'; +import * as grpcErrors from '../../grpc/errors'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; import { validateSync } from '../../validation'; import * as validationUtils from '../../validation/utils'; import * as vaultsUtils from '../../vaults/utils'; +import * as vaultsErrors from '../../vaults/errors'; import * as nodesErrors from '../../nodes/errors'; import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; @@ -48,19 +50,19 @@ function vaultsPull({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const { - vaultId, nodeId, }: { - vaultId: VaultId; nodeId: NodeId | undefined; } = validateSync( (keyPath, value) => { return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], [ ['nodeId'], () => (value ? validationUtils.parseNodeId(value) : undefined), @@ -69,7 +71,6 @@ function vaultsPull({ ); }, { - vaultId: call.request.getVault()?.getNameOrId(), nodeId: call.request.getNode()?.getNodeId(), }, ); @@ -85,8 +86,10 @@ function vaultsPull({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ + vaultsErrors.ErrorVaultsVaultUndefined, nodesErrors.ErrorNodeGraphNodeIdNotFound, + [grpcErrors.ErrorPolykeyRemote, vaultsErrors.ErrorVaultsVaultUndefined], ]) && logger.error(e); return; } diff --git a/src/client/service/vaultsRename.ts b/src/client/service/vaultsRename.ts index 9647d7ef1b..29d2bb04bd 100644 --- a/src/client/service/vaultsRename.ts +++ b/src/client/service/vaultsRename.ts @@ -1,16 +1,13 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsUtils from '../../vaults/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsRename({ @@ -37,24 +34,12 @@ function vaultsRename({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const newName = call.request.getNewName() as VaultName; await vaultManager.renameVault(vaultId, newName, tran); response.setNameOrId(vaultsUtils.encodeVaultId(vaultId)); @@ -63,7 +48,7 @@ function vaultsRename({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorVaultsVaultDefined, ]) && logger.error(e); diff --git a/src/client/service/vaultsScan.ts b/src/client/service/vaultsScan.ts index 48556105e6..c7a0def199 100644 --- a/src/client/service/vaultsScan.ts +++ b/src/client/service/vaultsScan.ts @@ -58,7 +58,7 @@ function vaultsScan({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ nodesErrors.ErrorNodeGraphNodeIdNotFound, ]) && logger.error(e); return; diff --git a/src/client/service/vaultsSecretsDelete.ts b/src/client/service/vaultsSecretsDelete.ts index 86a9a77b0e..625c03cabc 100644 --- a/src/client/service/vaultsSecretsDelete.ts +++ b/src/client/service/vaultsSecretsDelete.ts @@ -1,17 +1,15 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type * as secretsPB from '../../proto/js/polykey/v1/secrets/secrets_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsDelete({ @@ -38,24 +36,12 @@ function vaultsSecretsDelete({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const secretName = call.request.getSecretName(); await vaultManager.withVaults( [vaultId], @@ -70,7 +56,7 @@ function vaultsSecretsDelete({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorSecretsSecretUndefined, ]) && logger.error(e); diff --git a/src/client/service/vaultsSecretsEdit.ts b/src/client/service/vaultsSecretsEdit.ts index 374b5d9dd2..a40ea71e9a 100644 --- a/src/client/service/vaultsSecretsEdit.ts +++ b/src/client/service/vaultsSecretsEdit.ts @@ -1,17 +1,15 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type * as secretsPB from '../../proto/js/polykey/v1/secrets/secrets_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsEdit({ @@ -38,24 +36,12 @@ function vaultsSecretsEdit({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const secretName = call.request.getSecretName(); const secretContent = Buffer.from(call.request.getSecretContent()); await vaultManager.withVaults( @@ -71,7 +57,7 @@ function vaultsSecretsEdit({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorSecretsSecretUndefined, vaultsErrors.ErrorVaultRemoteDefined, diff --git a/src/client/service/vaultsSecretsGet.ts b/src/client/service/vaultsSecretsGet.ts index c5770db8e0..4f826551f0 100644 --- a/src/client/service/vaultsSecretsGet.ts +++ b/src/client/service/vaultsSecretsGet.ts @@ -1,17 +1,15 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; import * as secretsPB from '../../proto/js/polykey/v1/secrets/secrets_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsGet({ @@ -38,24 +36,12 @@ function vaultsSecretsGet({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const secretName = call.request.getSecretName(); const secretContent = await vaultManager.withVaults( [vaultId], @@ -70,7 +56,7 @@ function vaultsSecretsGet({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorSecretsSecretUndefined, ]) && logger.error(e); diff --git a/src/client/service/vaultsSecretsList.ts b/src/client/service/vaultsSecretsList.ts index b7c103cc76..a0774565f9 100644 --- a/src/client/service/vaultsSecretsList.ts +++ b/src/client/service/vaultsSecretsList.ts @@ -1,17 +1,15 @@ import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type * as grpc from '@grpc/grpc-js'; import type { DB } from '@matrixai/db'; import type * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; import type Logger from '@matrixai/logger'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; import * as secretsPB from '../../proto/js/polykey/v1/secrets/secrets_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsList({ @@ -37,24 +35,12 @@ function vaultsSecretsList({ call.request.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } return await vaultManager.withVaults( [vaultId], async (vault) => { @@ -73,8 +59,9 @@ function vaultsSecretsList({ return; } catch (e) { await genWritable.throw(e); - !clientUtils.isClientError(e, [vaultsErrors.ErrorVaultsVaultUndefined]) && - logger.error(e); + !clientUtils.isClientClientError(e, [ + vaultsErrors.ErrorVaultsVaultUndefined, + ]) && logger.error(e); return; } }; diff --git a/src/client/service/vaultsSecretsMkdir.ts b/src/client/service/vaultsSecretsMkdir.ts index eaa4e83242..623a28ec91 100644 --- a/src/client/service/vaultsSecretsMkdir.ts +++ b/src/client/service/vaultsSecretsMkdir.ts @@ -1,17 +1,15 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsMkdir({ @@ -38,24 +36,12 @@ function vaultsSecretsMkdir({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } await vaultManager.withVaults( [vaultId], async (vault) => { @@ -71,7 +57,7 @@ function vaultsSecretsMkdir({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorVaultsRecursive, ]) && logger.error(e); diff --git a/src/client/service/vaultsSecretsNew.ts b/src/client/service/vaultsSecretsNew.ts index d89064985b..c481a4cdad 100644 --- a/src/client/service/vaultsSecretsNew.ts +++ b/src/client/service/vaultsSecretsNew.ts @@ -1,17 +1,15 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type * as secretsPB from '../../proto/js/polykey/v1/secrets/secrets_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsNew({ @@ -38,24 +36,12 @@ function vaultsSecretsNew({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const secret = call.request.getSecretName(); const content = Buffer.from(call.request.getSecretContent()); await vaultManager.withVaults( @@ -71,7 +57,7 @@ function vaultsSecretsNew({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorSecretsSecretUndefined, ]) && logger.error(e); diff --git a/src/client/service/vaultsSecretsNewDir.ts b/src/client/service/vaultsSecretsNewDir.ts index 6cc7ad0f8d..e9a0d0878b 100644 --- a/src/client/service/vaultsSecretsNewDir.ts +++ b/src/client/service/vaultsSecretsNewDir.ts @@ -1,18 +1,16 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type { FileSystem } from '../../types'; import type * as secretsPB from '../../proto/js/polykey/v1/secrets/secrets_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsNewDir({ @@ -41,24 +39,12 @@ function vaultsSecretsNewDir({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const secretsPath = call.request.getSecretDirectory(); await vaultManager.withVaults( [vaultId], @@ -73,8 +59,9 @@ function vaultsSecretsNewDir({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [vaultsErrors.ErrorVaultsVaultUndefined]) && - logger.error(e); + !clientUtils.isClientClientError(e, [ + vaultsErrors.ErrorVaultsVaultUndefined, + ]) && logger.error(e); return; } }; diff --git a/src/client/service/vaultsSecretsRename.ts b/src/client/service/vaultsSecretsRename.ts index 1b17420ff7..9362c2b08e 100644 --- a/src/client/service/vaultsSecretsRename.ts +++ b/src/client/service/vaultsSecretsRename.ts @@ -1,17 +1,15 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type * as secretsPB from '../../proto/js/polykey/v1/secrets/secrets_pb'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; -import * as grpcUtils from '../../grpc/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as vaultsErrors from '../../vaults/errors'; +import * as grpcUtils from '../../grpc/utils'; import * as vaultOps from '../../vaults/VaultOps'; import * as utilsPB from '../../proto/js/polykey/v1/utils/utils_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsRename({ @@ -38,24 +36,14 @@ function vaultsSecretsRename({ call.request.getOldSecret()?.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getOldSecret()?.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId( + call.request.getOldSecret()?.getVault()?.getNameOrId(), + ); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const oldSecret = call.request.getOldSecret()?.getSecretName(); if (oldSecret == null) { throw new vaultsErrors.ErrorSecretsSecretUndefined(); @@ -74,7 +62,7 @@ function vaultsSecretsRename({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorSecretsSecretUndefined, ]) && logger.error(e); diff --git a/src/client/service/vaultsSecretsStat.ts b/src/client/service/vaultsSecretsStat.ts index fc173fe4be..78a7d2800b 100644 --- a/src/client/service/vaultsSecretsStat.ts +++ b/src/client/service/vaultsSecretsStat.ts @@ -1,16 +1,14 @@ import type { DB } from '@matrixai/db'; import type VaultManager from '../../vaults/VaultManager'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type { Authenticate } from '../types'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; import * as grpcUtils from '../../grpc/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultOps from '../../vaults/VaultOps'; import * as secretsPB from '../../proto/js/polykey/v1/secrets/secrets_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsSecretsStat({ @@ -37,24 +35,12 @@ function vaultsSecretsStat({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } const secretName = call.request.getSecretName(); const stat = await vaultManager.withVaults( [vaultId], @@ -69,7 +55,7 @@ function vaultsSecretsStat({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorSecretsSecretUndefined, ]) && logger.error(e); diff --git a/src/client/service/vaultsVersion.ts b/src/client/service/vaultsVersion.ts index 7c755aa55a..a3871b5261 100644 --- a/src/client/service/vaultsVersion.ts +++ b/src/client/service/vaultsVersion.ts @@ -1,15 +1,13 @@ import type { DB } from '@matrixai/db'; import type { Authenticate } from '../types'; -import type { VaultName, VaultId } from '../../vaults/types'; +import type { VaultName } from '../../vaults/types'; import type VaultManager from '../../vaults/VaultManager'; import type Logger from '@matrixai/logger'; import type * as grpc from '@grpc/grpc-js'; -import { validateSync } from '../../validation'; -import * as validationUtils from '../../validation/utils'; +import * as vaultsUtils from '../../vaults/utils'; import * as grpcUtils from '../../grpc/utils'; import * as vaultsErrors from '../../vaults/errors'; import * as vaultsPB from '../../proto/js/polykey/v1/vaults/vaults_pb'; -import { matchSync } from '../../utils'; import * as clientUtils from '../utils'; function vaultsVersion({ @@ -37,24 +35,12 @@ function vaultsVersion({ call.request.getVault()?.getNameOrId() as VaultName, tran, ); - const { - vaultId, - }: { - vaultId: VaultId; - } = validateSync( - (keyPath, value) => { - return matchSync(keyPath)( - [ - ['vaultId'], - () => vaultIdFromName ?? validationUtils.parseVaultId(value), - ], - () => value, - ); - }, - { - vaultId: call.request.getVault()?.getNameOrId(), - }, - ); + const vaultId = + vaultIdFromName ?? + vaultsUtils.decodeVaultId(call.request.getVault()?.getNameOrId()); + if (vaultId == null) { + throw new vaultsErrors.ErrorVaultsVaultUndefined(); + } // Doing the deed const versionId = call.request.getVersionId(); const [latestOid, currentVersionId] = await vaultManager.withVaults( @@ -78,7 +64,7 @@ function vaultsVersion({ return; } catch (e) { callback(grpcUtils.fromError(e)); - !clientUtils.isClientError(e, [ + !clientUtils.isClientClientError(e, [ vaultsErrors.ErrorVaultsVaultUndefined, vaultsErrors.ErrorVaultReferenceInvalid, vaultsErrors.ErrorVaultReferenceMissing, diff --git a/src/client/types.ts b/src/client/types.ts index dc642800f1..d06cdb11e2 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -1,8 +1,14 @@ import type * as grpc from '@grpc/grpc-js'; +import type { Class } from '@matrixai/errors'; +import type ErrorPolykey from '../ErrorPolykey'; type Authenticate = ( metadataClient: grpc.Metadata, metadataServer?: grpc.Metadata, ) => Promise; -export type { Authenticate }; +type ClientClientErrors = Array< + Class> | Array>> +>; + +export type { Authenticate, ClientClientErrors }; diff --git a/src/client/utils/utils.ts b/src/client/utils/utils.ts index b77ac0e76d..21bfaab208 100644 --- a/src/client/utils/utils.ts +++ b/src/client/utils/utils.ts @@ -3,12 +3,12 @@ import type { Interceptor, InterceptorOptions, } from '@grpc/grpc-js/build/src/client-interceptors'; -import type { Class } from '@matrixai/errors'; +import type ErrorPolykey from '../../ErrorPolykey'; import type KeyManager from '../../keys/KeyManager'; import type Session from '../../sessions/Session'; import type SessionManager from '../../sessions/SessionManager'; import type { SessionToken } from '../../sessions/types'; -import type { Authenticate } from '../types'; +import type { Authenticate, ClientClientErrors } from '../types'; import * as base64 from 'multiformats/bases/base64'; import * as grpc from '@grpc/grpc-js'; import * as validationErrors from '../../validation/errors'; @@ -18,7 +18,7 @@ import * as clientErrors from '../errors'; * Array of errors that are always considered to be "client errors" * (4xx errors in HTTP) in the context of the client service */ -const defaultClientErrors: Array> = [ +const defaultClientErrors: ClientClientErrors = [ validationErrors.ErrorValidation, clientErrors.ErrorClientAuthMissing, clientErrors.ErrorClientAuthFormat, @@ -154,16 +154,50 @@ function decodeAuthToSession( * context of a given handler can be supplied in the `extraClientErrors` * argument */ -function isClientError( - e: Error, - extraClientErrors?: Array>, +function isClientClientError( + thrownError: ErrorPolykey, + extraClientErrors?: ClientClientErrors, ): boolean { for (const error of defaultClientErrors) { - if (e instanceof error) return true; + if (Array.isArray(error)) { + let e = thrownError; + let matches = true; + for (const eType of error) { + if (e == null) { + matches = false; + break; + } + if (!(e instanceof eType)) { + matches = false; + break; + } + e = e.cause; + } + if (matches) return true; + } else if (thrownError instanceof error) { + return true; + } } if (extraClientErrors) { for (const error of extraClientErrors) { - if (e instanceof error) return true; + if (Array.isArray(error)) { + let e = thrownError; + let matches = true; + for (const eType of error) { + if (e == null) { + matches = false; + break; + } + if (!(e instanceof eType)) { + matches = false; + break; + } + e = e.cause; + } + if (matches) return true; + } else if (thrownError instanceof error) { + return true; + } } } return false; @@ -175,5 +209,5 @@ export { encodeAuthFromPassword, encodeAuthFromSession, decodeAuthToSession, - isClientError, + isClientClientError, }; diff --git a/tests/bin/vaults/vaults.test.ts b/tests/bin/vaults/vaults.test.ts index a8e6fa3df3..52b5f4e4c2 100644 --- a/tests/bin/vaults/vaults.test.ts +++ b/tests/bin/vaults/vaults.test.ts @@ -342,8 +342,8 @@ describe('CLI vaults', () => { targetNodeIdEncoded, ]; result = await testBinUtils.pkStdio([...command], {}, dataDir); - expect(result.exitCode).toBe(sysexits.DATAERR); - expect(result.stderr).toContain('ErrorValidation'); + expect(result.exitCode).toBe(sysexits.USAGE); + expect(result.stderr).toContain('ErrorVaultsVaultUndefined'); command = [ 'vaults', diff --git a/tests/vaults/VaultManager.test.ts b/tests/vaults/VaultManager.test.ts index c62824b1de..e4ed618aa8 100644 --- a/tests/vaults/VaultManager.test.ts +++ b/tests/vaults/VaultManager.test.ts @@ -23,7 +23,6 @@ import KeyManager from '@/keys/KeyManager'; import PolykeyAgent from '@/PolykeyAgent'; import VaultManager from '@/vaults/VaultManager'; import * as vaultsErrors from '@/vaults/errors'; -import * as validationErrors from '@/validation/errors'; import NodeGraph from '@/nodes/NodeGraph'; import * as nodesUtils from '@/nodes/utils'; import Proxy from '@/network/Proxy'; @@ -744,7 +743,7 @@ describe('VaultManager', () => { remoteKeynode1Id, 'not-existing' as VaultName, ), - validationErrors.ErrorValidation, + vaultsErrors.ErrorVaultsVaultUndefined, ); } finally { await vaultManager?.stop();