Skip to content

Commit 84f9bd1

Browse files
authored
1 parent cfb2dc8 commit 84f9bd1

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

.github/workflows/checks.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,30 @@ jobs:
169169
exit 1
170170
fi
171171
working-directory: cmdline
172+
173+
- name: Encrypt/Decrypt NanoTDF
174+
run: |
175+
echo 'here is some data to encrypt' > data
176+
177+
java -jar target/cmdline.jar \
178+
--client-id=opentdf-sdk \
179+
--client-secret=secret \
180+
--platform-endpoint=localhost:8080 \
181+
-i \
182+
encryptnano --kas-url=http://localhost:8080 -f data -m 'here is some metadata' > nano.ntdf
183+
184+
java -jar target/cmdline.jar \
185+
--client-id=opentdf-sdk \
186+
--client-secret=secret \
187+
--platform-endpoint=localhost:8080 \
188+
-i \
189+
decryptnano -f nano.ntdf > decrypted
190+
191+
if ! diff -q data decrypted; then
192+
printf 'decrypted data is incorrect [%s]' "$(< decrypted)"
193+
exit 1
194+
fi
195+
working-directory: cmdline
172196
ci:
173197
needs:
174198
- platform-integration

cmdline/src/main/java/io/opentdf/platform/Command.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package io.opentdf.platform;
22

3-
import io.opentdf.platform.sdk.Config;
4-
import io.opentdf.platform.sdk.SDK;
5-
import io.opentdf.platform.sdk.SDKBuilder;
3+
import io.opentdf.platform.sdk.*;
64
import io.opentdf.platform.sdk.TDF;
75
import picocli.CommandLine;
86
import picocli.CommandLine.Option;
@@ -18,6 +16,7 @@
1816
import java.io.IOException;
1917
import java.io.PrintWriter;
2018
import java.io.StringWriter;
19+
import java.nio.ByteBuffer;
2120
import java.nio.channels.FileChannel;
2221
import java.nio.file.Path;
2322
import java.nio.file.StandardOpenOption;
@@ -98,4 +97,43 @@ void readMetadata(@Option(names = {"-f", "--file"}, required = true) Path tdfPat
9897
}
9998
}
10099
}
100+
101+
@CommandLine.Command(name = "encryptnano")
102+
void createNanoTDF(
103+
@Option(names = {"-f", "--file"}, defaultValue = Option.NULL_VALUE) Optional<File> file,
104+
@Option(names = {"-k", "--kas-url"}, required = true) List<String> kas,
105+
@Option(names = {"-m", "--metadata"}, defaultValue = Option.NULL_VALUE) Optional<String> metadata) throws Exception {
106+
107+
var sdk = buildSDK();
108+
var kasInfos = kas.stream().map(k -> {
109+
var ki = new Config.KASInfo();
110+
ki.URL = k;
111+
return ki;
112+
}).toArray(Config.KASInfo[]::new);
113+
114+
List<Consumer<Config.NanoTDFConfig>> configs = new ArrayList<>();
115+
configs.add(Config.withNanoKasInformation(kasInfos));
116+
117+
var nanoTDFConfig = Config.newNanoTDFConfig(configs.toArray(Consumer[]::new));
118+
try (var in = file.isEmpty() ? new BufferedInputStream(System.in) : new FileInputStream(file.get())) {
119+
try (var out = new BufferedOutputStream(System.out)) {
120+
NanoTDF ntdf = new NanoTDF();
121+
ntdf.createNanoTDF(ByteBuffer.wrap(in.readAllBytes()), out, nanoTDFConfig, sdk.getServices().kas());
122+
}
123+
}
124+
}
125+
126+
@CommandLine.Command(name = "decryptnano")
127+
void readNanoTDF(@Option(names = {"-f", "--file"}, required = true) Path nanoTDFPath) throws Exception {
128+
var sdk = buildSDK();
129+
try (var in = FileChannel.open(nanoTDFPath, StandardOpenOption.READ)) {
130+
try (var stdout = new BufferedOutputStream(System.out)) {
131+
NanoTDF ntdf = new NanoTDF();
132+
ByteBuffer buffer = ByteBuffer.allocate((int) in.size());
133+
in.read(buffer);
134+
buffer.flip();
135+
ntdf.readNanoTDF(buffer, stdout, sdk.getServices().kas());
136+
}
137+
}
138+
}
101139
}

0 commit comments

Comments
 (0)