88import io .opentdf .platform .sdk .Config ;
99import io .opentdf .platform .sdk .KeyType ;
1010import io .opentdf .platform .sdk .Config .AssertionVerificationKeys ;
11- import io .opentdf .platform .sdk .NanoTDF ;
1211import io .opentdf .platform .sdk .SDK ;
1312import io .opentdf .platform .sdk .SDKBuilder ;
1413import io .opentdf .platform .sdk .TDF ;
@@ -192,8 +191,8 @@ void encrypt(
192191 } catch (JsonSyntaxException e ) {
193192 // try it as a file path
194193 try {
195- String fielJson = new String (Files .readAllBytes (Paths .get (assertionConfig )));
196- assertionConfigs = gson .fromJson (fielJson , AssertionConfig [].class );
194+ String fileJson = new String (Files .readAllBytes (Paths .get (assertionConfig )));
195+ assertionConfigs = gson .fromJson (fileJson , AssertionConfig [].class );
197196 } catch (JsonSyntaxException e2 ) {
198197 throw new RuntimeException ("Failed to parse assertion from file, expects an list of assertions" , e2 );
199198 } catch (Exception e3 ) {
@@ -221,9 +220,7 @@ void encrypt(
221220 var tdfConfig = Config .newTDFConfig (configs .toArray (Consumer []::new ));
222221 try (var in = file .isEmpty () ? new BufferedInputStream (System .in ) : new FileInputStream (file .get ())) {
223222 try (var out = new BufferedOutputStream (System .out )) {
224- new TDF ().createTDF (in , out , tdfConfig ,
225- sdk .getServices ().kas (),
226- sdk .getServices ().attributes ());
223+ sdk .createTDF (in , out , tdfConfig );
227224 }
228225 }
229226 }
@@ -249,56 +246,53 @@ void decrypt(@Option(names = { "-f", "--file" }, required = true) Path tdfPath,
249246 @ Option (names = { "--with-assertion-verification-keys" }, defaultValue = Option .NULL_VALUE ) Optional <String > assertionVerification ,
250247 @ Option (names = { "--kas-allowlist" }, defaultValue = Option .NULL_VALUE ) Optional <String > kasAllowlistStr ,
251248 @ Option (names = { "--ignore-kas-allowlist" }, defaultValue = Option .NULL_VALUE ) Optional <Boolean > ignoreAllowlist )
252- throws IOException , TDF .FailedToCreateGMAC , JOSEException , ParseException , NoSuchAlgorithmException , DecoderException , InterruptedException , ExecutionException , URISyntaxException {
253- var sdk = buildSDK ();
254- var opts = new ArrayList <Consumer <Config .TDFReaderConfig >>();
255- try (var in = FileChannel .open (tdfPath , StandardOpenOption .READ )) {
256- try (var stdout = new BufferedOutputStream (System .out )) {
257- if (assertionVerification .isPresent ()) {
258- var assertionVerificationInput = assertionVerification .get ();
259- Gson gson = new Gson ();
260-
261- AssertionVerificationKeys assertionVerificationKeys ;
262- try {
263- assertionVerificationKeys = gson .fromJson (assertionVerificationInput , AssertionVerificationKeys .class );
264- } catch (JsonSyntaxException e ) {
265- // try it as a file path
249+ throws Exception {
250+ try (var sdk = buildSDK ()) {
251+ var opts = new ArrayList <Consumer <Config .TDFReaderConfig >>();
252+ try (var in = FileChannel .open (tdfPath , StandardOpenOption .READ )) {
253+ try (var stdout = new BufferedOutputStream (System .out )) {
254+ if (assertionVerification .isPresent ()) {
255+ var assertionVerificationInput = assertionVerification .get ();
256+ Gson gson = new Gson ();
257+
258+ AssertionVerificationKeys assertionVerificationKeys ;
266259 try {
267- String fileJson = new String (Files .readAllBytes (Paths .get (assertionVerificationInput )));
268- assertionVerificationKeys = gson .fromJson (fileJson , AssertionVerificationKeys .class );
269- } catch (JsonSyntaxException e2 ) {
270- throw new RuntimeException ("Failed to parse assertion verification keys from file" , e2 );
271- } catch (Exception e3 ) {
272- throw new RuntimeException ("Could not parse assertion verification keys as json string or path to file" , e3 );
260+ assertionVerificationKeys = gson .fromJson (assertionVerificationInput , AssertionVerificationKeys .class );
261+ } catch (JsonSyntaxException e ) {
262+ // try it as a file path
263+ try {
264+ String fileJson = new String (Files .readAllBytes (Paths .get (assertionVerificationInput )));
265+ assertionVerificationKeys = gson .fromJson (fileJson , AssertionVerificationKeys .class );
266+ } catch (JsonSyntaxException e2 ) {
267+ throw new RuntimeException ("Failed to parse assertion verification keys from file" , e2 );
268+ } catch (Exception e3 ) {
269+ throw new RuntimeException ("Could not parse assertion verification keys as json string or path to file" , e3 );
270+ }
273271 }
274- }
275272
276- for (Map .Entry <String , AssertionConfig .AssertionKey > entry : assertionVerificationKeys .keys .entrySet ()){
277- try {
278- Object correctedKey = correctKeyType (entry .getValue ().alg , entry .getValue ().key , true );
279- entry .setValue (new AssertionConfig .AssertionKey (entry .getValue ().alg , correctedKey ));
280- } catch (Exception e ) {
281- throw new RuntimeException ("Error with assertion verification key: " + e .getMessage (), e );
273+ for (Map .Entry <String , AssertionConfig .AssertionKey > entry : assertionVerificationKeys .keys .entrySet ()) {
274+ try {
275+ Object correctedKey = correctKeyType (entry .getValue ().alg , entry .getValue ().key , true );
276+ entry .setValue (new AssertionConfig .AssertionKey (entry .getValue ().alg , correctedKey ));
277+ } catch (Exception e ) {
278+ throw new RuntimeException ("Error with assertion verification key: " + e .getMessage (), e );
279+ }
282280 }
281+ opts .add (Config .withAssertionVerificationKeys (assertionVerificationKeys ));
283282 }
284- opts .add (Config .withAssertionVerificationKeys (assertionVerificationKeys ));
285- }
286283
287- if (disableAssertionVerification ) {
288- opts .add (Config .withDisableAssertionVerification (true ));
289- }
290- rewrapKeyType .map (Config ::WithSessionKeyType ).ifPresent (opts ::add );
284+ if (disableAssertionVerification ) {
285+ opts .add (Config .withDisableAssertionVerification (true ));
286+ }
287+ rewrapKeyType .map (Config ::WithSessionKeyType ).ifPresent (opts ::add );
291288
292- if (ignoreAllowlist .isPresent ()) {
293- opts .add (Config .WithIgnoreKasAllowlist (ignoreAllowlist .get ()));
294- }
295- if (kasAllowlistStr .isPresent ()) {
296- opts .add (Config .WithKasAllowlist (kasAllowlistStr .get ().split ("," )));
297- }
289+ ignoreAllowlist .ifPresent (aBoolean -> opts .add (Config .WithIgnoreKasAllowlist (aBoolean )));
290+ kasAllowlistStr .ifPresent (s -> opts .add (Config .WithKasAllowlist (s .split ("," ))));
298291
299- var readerConfig = Config .newTDFReaderConfig (opts .toArray (new Consumer [0 ]));
300- var reader = new TDF ().loadTDF (in , sdk .getServices ().kas (), readerConfig , sdk .getServices ().kasRegistry (), sdk .getPlatformUrl ());
301- reader .readPayload (stdout );
292+ var readerConfig = Config .newTDFReaderConfig (opts .toArray (new Consumer [0 ]));
293+ var reader = sdk .loadTDF (in , readerConfig );
294+ reader .readPayload (stdout );
295+ }
302296 }
303297 }
304298 }
@@ -313,15 +307,11 @@ void readMetadata(@Option(names = { "-f", "--file" }, required = true) Path tdfP
313307 try (var in = FileChannel .open (tdfPath , StandardOpenOption .READ )) {
314308 try (var stdout = new PrintWriter (System .out )) {
315309
316- if (ignoreAllowlist .isPresent ()) {
317- opts .add (Config .WithIgnoreKasAllowlist (ignoreAllowlist .get ()));
318- }
319- if (kasAllowlistStr .isPresent ()) {
320- opts .add (Config .WithKasAllowlist (kasAllowlistStr .get ().split ("," )));
321- }
310+ ignoreAllowlist .map (Config ::WithIgnoreKasAllowlist ).ifPresent (opts ::add );
311+ kasAllowlistStr .map (s -> s .split ("," )).map (Config ::WithKasAllowlist ).ifPresent (opts ::add );
322312
323313 var readerConfig = Config .newTDFReaderConfig (opts .toArray (new Consumer [0 ]));
324- var reader = new TDF () .loadTDF (in , sdk . getServices (). kas (), readerConfig , sdk . getServices (). kasRegistry (), sdk . getPlatformUrl () );
314+ var reader = sdk .loadTDF (in , readerConfig );
325315 stdout .write (reader .getMetadata () == null ? "" : reader .getMetadata ());
326316 }
327317 }
@@ -351,8 +341,7 @@ void createNanoTDF(
351341 var nanoTDFConfig = Config .newNanoTDFConfig (configs .toArray (Consumer []::new ));
352342 try (var in = file .isEmpty () ? new BufferedInputStream (System .in ) : new FileInputStream (file .get ())) {
353343 try (var out = new BufferedOutputStream (System .out )) {
354- NanoTDF ntdf = new NanoTDF ();
355- ntdf .createNanoTDF (ByteBuffer .wrap (in .readAllBytes ()), out , nanoTDFConfig , sdk .getServices ().kas ());
344+ sdk .createNanoTDF (ByteBuffer .wrap (in .readAllBytes ()), out , nanoTDFConfig );
356345 }
357346 }
358347 }
@@ -364,19 +353,14 @@ void readNanoTDF(@Option(names = { "-f", "--file" }, required = true) Path nanoT
364353 var sdk = buildSDK ();
365354 try (var in = FileChannel .open (nanoTDFPath , StandardOpenOption .READ )) {
366355 try (var stdout = new BufferedOutputStream (System .out )) {
367- NanoTDF ntdf = new NanoTDF ();
368356 ByteBuffer buffer = ByteBuffer .allocate ((int ) in .size ());
369357 in .read (buffer );
370358 buffer .flip ();
371359 var opts = new ArrayList <Consumer <Config .NanoTDFReaderConfig >>();
372- if (ignoreAllowlist .isPresent ()) {
373- opts .add (Config .WithNanoIgnoreKasAllowlist (ignoreAllowlist .get ()));
374- }
375- if (kasAllowlistStr .isPresent ()) {
376- opts .add (Config .WithNanoKasAllowlist (kasAllowlistStr .get ().split ("," )));
377- }
360+ ignoreAllowlist .map (Config ::WithNanoIgnoreKasAllowlist ).ifPresent (opts ::add );
361+ kasAllowlistStr .map (s -> s .split ("," )).map (Config ::WithNanoKasAllowlist ).ifPresent (opts ::add );
378362 var readerConfig = Config .newNanoTDFReaderConfig (opts .toArray (new Consumer [0 ]));
379- ntdf .readNanoTDF (buffer , stdout , sdk . getServices (). kas (), readerConfig , sdk . getServices (). kasRegistry (), sdk . getPlatformUrl () );
363+ sdk .readNanoTDF (buffer , stdout , readerConfig );
380364 }
381365 }
382366 }
0 commit comments