@@ -2,72 +2,44 @@ import type { DB } from '@matrixai/db';
22import type {
33 ClientRPCRequestParams ,
44 ClientRPCResponseResult ,
5- ContentWithErrorMessage ,
5+ ContentMessage ,
66 SecretIdentifierMessage ,
77} from '../types' ;
88import type VaultManager from '../../vaults/VaultManager' ;
9- import { DuplexHandler } from '@matrixai/rpc' ;
9+ import { ServerHandler } from '@matrixai/rpc' ;
1010import * as vaultsUtils from '../../vaults/utils' ;
1111import * as vaultsErrors from '../../vaults/errors' ;
1212import * as vaultOps from '../../vaults/VaultOps' ;
1313
14- class VaultsSecretsGet extends DuplexHandler <
14+ // This method only returns the contents of a single secret, and throws an error
15+ // if the secret couldn't be read. To read multiple secrets, refer to
16+ // `VaultsSecretsCat`.
17+ class VaultsSecretsGet extends ServerHandler <
1518 {
1619 db : DB ;
1720 vaultManager : VaultManager ;
1821 } ,
1922 ClientRPCRequestParams < SecretIdentifierMessage > ,
20- ClientRPCResponseResult < ContentWithErrorMessage >
23+ ClientRPCResponseResult < ContentMessage >
2124> {
2225 public handle = async function * (
23- input : AsyncIterable < ClientRPCRequestParams < SecretIdentifierMessage > > ,
24- _cancel ,
25- _meta ,
26- ctx ,
27- ) : AsyncGenerator < ClientRPCResponseResult < ContentWithErrorMessage > > {
28- if ( ctx . signal . aborted ) throw ctx . signal . reason ;
26+ input : ClientRPCRequestParams < SecretIdentifierMessage > ,
27+ ) : AsyncGenerator < ClientRPCResponseResult < ContentMessage > > {
2928 const { db, vaultManager } : { db : DB ; vaultManager : VaultManager } =
3029 this . container ;
31- yield * db . withTransactionG ( async function * ( tran ) : AsyncGenerator <
32- ClientRPCResponseResult < ContentWithErrorMessage >
33- > {
34- if ( ctx . signal . aborted ) throw ctx . signal . reason ;
35- // As we need to preserve the order of parameters, we need to loop over
36- // them individually, as grouping them would make them go out of order.
37- let metadata : any = undefined ;
38- for await ( const secretIdentiferMessage of input ) {
39- if ( ctx . signal . aborted ) throw ctx . signal . reason ;
40- if ( metadata == null ) metadata = secretIdentiferMessage . metadata ?? { } ;
41- const { nameOrId, secretName } = secretIdentiferMessage ;
42- const vaultIdFromName = await vaultManager . getVaultId ( nameOrId , tran ) ;
43- const vaultId = vaultIdFromName ?? vaultsUtils . decodeVaultId ( nameOrId ) ;
44- if ( vaultId == null ) throw new vaultsErrors . ErrorVaultsVaultUndefined ( ) ;
45- yield await vaultManager . withVaults (
46- [ vaultId ] ,
47- async ( vault ) => {
48- try {
49- const content = await vaultOps . getSecret ( vault , secretName ) ;
50- return { secretContent : content . toString ( 'binary' ) } ;
51- } catch ( e ) {
52- if ( metadata ?. options ?. continueOnError === true ) {
53- if ( e instanceof vaultsErrors . ErrorSecretsSecretUndefined ) {
54- return {
55- secretContent : '' ,
56- error : `${ e . name } : ${ secretName } : No such secret or directory\n` ,
57- } ;
58- } else if ( e instanceof vaultsErrors . ErrorSecretsIsDirectory ) {
59- return {
60- secretContent : '' ,
61- error : `${ e . name } : ${ secretName } : Is a directory\n` ,
62- } ;
63- }
64- }
65- throw e ;
66- }
67- } ,
68- tran ,
69- ) ;
70- }
30+ yield await db . withTransactionF ( async ( tran ) => {
31+ const vaultIdFromName = await vaultManager . getVaultId (
32+ input . nameOrId ,
33+ tran ,
34+ ) ;
35+ const vaultId =
36+ vaultIdFromName ?? vaultsUtils . decodeVaultId ( input . nameOrId ) ;
37+ if ( vaultId == null ) throw new vaultsErrors . ErrorVaultsVaultUndefined ( ) ;
38+ // Get the contents of the file
39+ return await vaultManager . withVaults ( [ vaultId ] , async ( vault ) => {
40+ const content = await vaultOps . getSecret ( vault , input . secretName ) ;
41+ return { secretContent : content . toString ( 'binary' ) } ;
42+ } ) ;
7143 } ) ;
7244 } ;
7345}
0 commit comments