@@ -54,6 +54,9 @@ interface DekClient {
54
54
registerDek ( kekName : string , subject : string , algorithm : string , version : number ,
55
55
encryptedKeyMaterial ?: string ) : Promise < Dek > ;
56
56
getDek ( kekName : string , subject : string , algorithm : string , version : number , deleted : boolean ) : Promise < Dek > ;
57
+ getDekEncryptedKeyMaterialBytes ( dek : Dek ) : Promise < Buffer | null > ;
58
+ getDekKeyMaterialBytes ( dek : Dek ) : Promise < Buffer | null > ;
59
+ setDekKeyMaterial ( dek : Dek , keyMaterialBytes : Buffer ) : Promise < void > ;
57
60
close ( ) : Promise < void > ;
58
61
}
59
62
@@ -90,53 +93,6 @@ class DekRegistryClient implements DekClient {
90
93
return new DekRegistryClient ( config )
91
94
}
92
95
93
- static getEncryptedKeyMaterialBytes ( dek : Dek ) : Buffer | null {
94
- if ( ! dek . encryptedKeyMaterial ) {
95
- return null ;
96
- }
97
-
98
- if ( ! dek . encryptedKeyMaterialBytes ) {
99
- try {
100
- const bytes = Buffer . from ( dek . encryptedKeyMaterial , 'base64' ) ;
101
- dek . encryptedKeyMaterialBytes = bytes ;
102
- } catch ( err ) {
103
- if ( err instanceof Error ) {
104
- throw new Error ( `Failed to decode base64 string: ${ err . message } ` ) ;
105
- }
106
- throw new Error ( `Unknown error: ${ err } ` ) ;
107
- }
108
- }
109
-
110
- return dek . encryptedKeyMaterialBytes ;
111
- }
112
-
113
- static getKeyMaterialBytes ( dek : Dek ) : Buffer | null {
114
- if ( ! dek . keyMaterial ) {
115
- return null ;
116
- }
117
-
118
- if ( ! dek . keyMaterialBytes ) {
119
- try {
120
- const bytes = Buffer . from ( dek . keyMaterial , 'base64' ) ;
121
- dek . keyMaterialBytes = bytes ;
122
- } catch ( err ) {
123
- if ( err instanceof Error ) {
124
- throw new Error ( `Failed to decode base64 string: ${ err . message } ` ) ;
125
- }
126
- throw new Error ( `Unknown error: ${ err } ` ) ;
127
- }
128
- }
129
-
130
- return dek . keyMaterialBytes ;
131
- }
132
-
133
- static setKeyMaterial ( dek : Dek , keyMaterialBytes : Buffer ) : void {
134
- if ( keyMaterialBytes ) {
135
- const str = keyMaterialBytes . toString ( 'base64' ) ;
136
- dek . keyMaterial = str ;
137
- }
138
- }
139
-
140
96
config ( ) : ClientConfig {
141
97
return this . clientConfig ;
142
98
}
@@ -238,6 +194,63 @@ class DekRegistryClient implements DekClient {
238
194
} ) ;
239
195
}
240
196
197
+ async getDekEncryptedKeyMaterialBytes ( dek : Dek ) : Promise < Buffer | null > {
198
+ if ( ! dek . encryptedKeyMaterial ) {
199
+ return null ;
200
+ }
201
+
202
+ if ( ! dek . encryptedKeyMaterialBytes ) {
203
+ await this . dekMutex . runExclusive ( async ( ) => {
204
+ if ( ! dek . encryptedKeyMaterialBytes ) {
205
+ try {
206
+ const bytes = Buffer . from ( dek . encryptedKeyMaterial ! , 'base64' ) ;
207
+ dek . encryptedKeyMaterialBytes = bytes ;
208
+ } catch ( err ) {
209
+ if ( err instanceof Error ) {
210
+ throw new Error ( `Failed to decode base64 string: ${ err . message } ` ) ;
211
+ }
212
+ throw new Error ( `Unknown error: ${ err } ` ) ;
213
+ }
214
+ }
215
+ } )
216
+ }
217
+
218
+ return dek . encryptedKeyMaterialBytes ! ;
219
+ }
220
+
221
+ async getDekKeyMaterialBytes ( dek : Dek ) : Promise < Buffer | null > {
222
+ if ( ! dek . keyMaterial ) {
223
+ return null ;
224
+ }
225
+
226
+ if ( ! dek . keyMaterialBytes ) {
227
+ await this . dekMutex . runExclusive ( async ( ) => {
228
+ if ( ! dek . keyMaterialBytes ) {
229
+ try {
230
+ const bytes = Buffer . from ( dek . keyMaterial ! , 'base64' ) ;
231
+ dek . keyMaterialBytes = bytes ;
232
+ } catch ( err ) {
233
+ if ( err instanceof Error ) {
234
+ throw new Error ( `Failed to decode base64 string: ${ err . message } ` ) ;
235
+ }
236
+ throw new Error ( `Unknown error: ${ err } ` ) ;
237
+ }
238
+ }
239
+ } )
240
+ }
241
+
242
+ return dek . keyMaterialBytes ! ;
243
+ }
244
+
245
+ async setDekKeyMaterial ( dek : Dek , keyMaterialBytes : Buffer ) : Promise < void > {
246
+ await this . dekMutex . runExclusive ( async ( ) => {
247
+ if ( keyMaterialBytes ) {
248
+ const str = keyMaterialBytes . toString ( 'base64' ) ;
249
+ dek . keyMaterial = str ;
250
+ }
251
+ } )
252
+ }
253
+
241
254
async close ( ) : Promise < void > {
242
255
return ;
243
256
}
0 commit comments