|
| 1 | +package io.opentdf.platform; |
| 2 | + |
| 3 | +import io.opentdf.platform.sdk.Config; |
| 4 | +import io.opentdf.platform.sdk.SDK; |
| 5 | +import io.opentdf.platform.sdk.SDKBuilder; |
| 6 | +import io.opentdf.platform.sdk.TDF; |
| 7 | +import picocli.CommandLine; |
| 8 | +import picocli.CommandLine.Option; |
| 9 | + |
| 10 | +import javax.crypto.BadPaddingException; |
| 11 | +import javax.crypto.IllegalBlockSizeException; |
| 12 | +import javax.crypto.NoSuchPaddingException; |
| 13 | +import java.io.BufferedInputStream; |
| 14 | +import java.io.BufferedOutputStream; |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileInputStream; |
| 18 | +import java.io.IOException; |
| 19 | +import java.io.PrintWriter; |
| 20 | +import java.io.StringWriter; |
| 21 | +import java.nio.channels.FileChannel; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.StandardOpenOption; |
| 24 | +import java.security.InvalidAlgorithmParameterException; |
| 25 | +import java.security.InvalidKeyException; |
| 26 | +import java.security.NoSuchAlgorithmException; |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Optional; |
| 30 | +import java.util.function.Consumer; |
| 31 | + |
| 32 | +@CommandLine.Command(name = "tdf") |
| 33 | +class Command { |
| 34 | + |
| 35 | + @Option(names = {"--client-secret"}, required = true) |
| 36 | + private String clientSecret; |
| 37 | + |
| 38 | + @Option(names = {"-i", "--insecure-connection"}, defaultValue = "false") |
| 39 | + private boolean insecure; |
| 40 | + |
| 41 | + @Option(names = {"--client-id"}, required = true) |
| 42 | + private String clientId; |
| 43 | + |
| 44 | + @Option(names = {"-p", "--platform-endpoint"}, required = true) |
| 45 | + private String platformEndpoint; |
| 46 | + |
| 47 | + @CommandLine.Command(name = "encrypt") |
| 48 | + void encrypt( |
| 49 | + @Option(names = {"-f", "--file"}, defaultValue = Option.NULL_VALUE) Optional<File> file, |
| 50 | + @Option(names = {"-k", "--kas-url"}, required = true) List<String> kas, |
| 51 | + @Option(names = {"-m", "--metadata"}, defaultValue = Option.NULL_VALUE) Optional<String> metadata) throws IOException { |
| 52 | + |
| 53 | + var sdk = buildSDK(); |
| 54 | + var kasInfos = kas.stream().map(k -> { |
| 55 | + var ki = new Config.KASInfo(); |
| 56 | + ki.URL = k; |
| 57 | + return ki; |
| 58 | + }).toArray(Config.KASInfo[]::new); |
| 59 | + |
| 60 | + List<Consumer<Config.TDFConfig>> configs = new ArrayList<>(); |
| 61 | + configs.add(Config.withKasInformation(kasInfos)); |
| 62 | + metadata.map(Config::withMetaData).ifPresent(configs::add); |
| 63 | + |
| 64 | + var tdfConfig = Config.newTDFConfig(configs.toArray(Consumer[]::new)); |
| 65 | + try (var in = file.isEmpty() ? new BufferedInputStream(System.in) : new FileInputStream(file.get())) { |
| 66 | + try (var out = new BufferedOutputStream(System.out)) { |
| 67 | + new TDF().createTDF(in, out, tdfConfig, sdk.getServices().kas()); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private SDK buildSDK() { |
| 73 | + return new SDKBuilder() |
| 74 | + .platformEndpoint(platformEndpoint) |
| 75 | + .clientSecret(clientId, clientSecret) |
| 76 | + .useInsecurePlaintextConnection(insecure) |
| 77 | + .build(); |
| 78 | + } |
| 79 | + |
| 80 | + @CommandLine.Command(name = "decrypt") |
| 81 | + void decrypt(@Option(names = {"-f", "--file"}, required = true) Path tdfPath) throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { |
| 82 | + var sdk = buildSDK(); |
| 83 | + try (var in = FileChannel.open(tdfPath, StandardOpenOption.READ)) { |
| 84 | + try (var stdout = new BufferedOutputStream(System.out)) { |
| 85 | + var reader = new TDF().loadTDF(in, sdk.getServices().kas()); |
| 86 | + reader.readPayload(stdout); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + @CommandLine.Command(name = "metadata") |
| 91 | + void readMetadata(@Option(names = {"-f", "--file"}, required = true) Path tdfPath) throws IOException, InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException { |
| 92 | + var sdk = buildSDK(); |
| 93 | + |
| 94 | + try (var in = FileChannel.open(tdfPath, StandardOpenOption.READ)) { |
| 95 | + try (var stdout = new PrintWriter(System.out)) { |
| 96 | + var reader = new TDF().loadTDF(in, sdk.getServices().kas()); |
| 97 | + stdout.write(reader.getMetadata() == null ? "" : reader.getMetadata()); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments