23
23
import java .util .Map ;
24
24
25
25
import org .springframework .util .Assert ;
26
- import org .springframework .util .ObjectUtils ;
27
26
import org .springframework .util .StringUtils ;
28
27
import org .springframework .vault .VaultException ;
29
28
import org .springframework .vault .support .TransformCiphertext ;
38
37
*
39
38
* @author Lauren Voswinkel
40
39
* @author Mark Paluch
40
+ * @author Roopesh Chandran
41
41
* @since 2.3
42
42
*/
43
43
public class VaultTransformTemplate implements VaultTransformOperations {
@@ -174,17 +174,11 @@ public List<VaultTransformDecodeResult> decode(String roleName, List<TransformCi
174
174
175
175
private static void applyTransformOptions (VaultTransformContext context , Map <String , String > request ) {
176
176
177
- if (!ObjectUtils .isEmpty (context .getTransformation ())) {
178
- request .put ("transformation" , context .getTransformation ());
179
- }
177
+ PropertyMapper mapper = PropertyMapper .get ();
180
178
181
- if (!ObjectUtils .isEmpty (context .getTweak ())) {
182
- request .put ("tweak" , Base64 .getEncoder ().encodeToString (context .getTweak ()));
183
- }
184
- // NEW: pass "reference" in each item, if present
185
- if (StringUtils .hasText (context .getReference ())) {
186
- request .put ("reference" , context .getReference ());
187
- }
179
+ mapper .from (context .getTransformation ()).whenNotEmpty ().to ("transformation" , request );
180
+ mapper .from (context .getTweak ()).whenNotEmpty ().as (Base64 .getEncoder ()::encodeToString ).to ("tweak" , request );
181
+ mapper .from (context .getReference ()).whenNotEmpty ().to ("reference" , request );
188
182
}
189
183
190
184
private static List <VaultTransformEncodeResult > toEncodedResults (VaultResponse vaultResponse ,
@@ -250,29 +244,10 @@ private static VaultTransformDecodeResult getDecryptionResult(Map<String, String
250
244
251
245
if (StringUtils .hasText (data .get ("decoded_value" ))) {
252
246
253
- // 1. Read reference from Vault's response (if present).
254
- String returnedRef = data .get ("reference" );
255
-
256
- // 2. Build an updated context that merges the existing transformation/tweak
257
- // with the newly-returned reference. If no reference is returned, keep the
258
- // old one. Note:- Relying on reference from originalContext is aimed at
259
- // providing a
260
- // fallback strategy, if vault does not return the reference, in any
261
- // circumstance.
262
- VaultTransformContext originalContext = ciphertext .getContext ();
263
- VaultTransformContext updatedContext = VaultTransformContext .builder ()
264
- .transformation (originalContext .getTransformation ())
265
- .tweak (originalContext .getTweak ())
266
- .reference (returnedRef != null ? returnedRef : originalContext .getReference ())
267
- .build ();
268
-
269
- // 3. Attach that updated context to the newly decoded plaintext.
247
+ VaultTransformContext updatedContext = postProcessTransformContext (data , ciphertext .getContext ());
270
248
TransformPlaintext decodedPlaintext = TransformPlaintext .of (data .get ("decoded_value" )).with (updatedContext );
271
249
272
250
return new VaultTransformDecodeResult (decodedPlaintext );
273
-
274
- // return new VaultTransformDecodeResult(
275
- // TransformPlaintext.of(data.get("decoded_value")).with(ciphertext.getContext()));
276
251
}
277
252
278
253
return new VaultTransformDecodeResult (TransformPlaintext .empty ().with (ciphertext .getContext ()));
@@ -281,24 +256,29 @@ private static VaultTransformDecodeResult getDecryptionResult(Map<String, String
281
256
private static TransformCiphertext toCiphertext (Map <String , ?> data , VaultTransformContext context ) {
282
257
283
258
String ciphertext = (String ) data .get ("encoded_value" );
284
-
285
- // if Vault returns "reference" in batch_results,capturing it for co-relation.
286
- String returnedRef = (String ) data .get ("reference" );
287
-
288
- VaultTransformContext contextToUse = context ;
289
- if (data .containsKey ("tweak" )) {
290
- byte [] tweak = Base64 .getDecoder ().decode ((String ) data .get ("tweak" ));
291
- contextToUse = VaultTransformContext .builder ()
292
- .transformation (context .getTransformation ())
293
- .tweak (tweak )
294
- .reference (returnedRef != null ? returnedRef : context .getReference ())
295
- .build ();
296
- }
259
+ VaultTransformContext contextToUse = postProcessTransformContext (data , context );
297
260
298
261
return contextToUse .isEmpty () ? TransformCiphertext .of (ciphertext )
299
262
: TransformCiphertext .of (ciphertext ).with (contextToUse );
300
263
}
301
264
265
+ private static VaultTransformContext postProcessTransformContext (Map <String , ?> data ,
266
+ VaultTransformContext context ) {
267
+
268
+ if (data .containsKey ("tweak" ) || data .containsKey ("reference" )) {
269
+
270
+ PropertyMapper mapper = PropertyMapper .get ();
271
+ VaultTransformContext .VaultTransformRequestBuilder builder = context .mutate ();
272
+
273
+ mapper .from ((String ) data .get ("tweak" )).whenNotEmpty ().as (Base64 .getDecoder ()::decode ).to (builder ::tweak );
274
+ mapper .from ((String ) data .get ("reference" )).whenNotEmpty ().to (builder ::reference );
275
+
276
+ return builder .build ();
277
+ }
278
+
279
+ return context ;
280
+ }
281
+
302
282
@ SuppressWarnings ("unchecked" )
303
283
private static List <Map <String , String >> getBatchData (VaultResponse vaultResponse ) {
304
284
return (List <Map <String , String >>) vaultResponse .getRequiredData ().get ("batch_results" );
0 commit comments