Skip to content

Commit fd078df

Browse files
committed
Add crypto
Signed-off-by: sirivarma <siri.varma@outlook.com>
1 parent 9fe698b commit fd078df

File tree

4 files changed

+445
-0
lines changed

4 files changed

+445
-0
lines changed

sdk/src/main/java/io/dapr/client/DaprClientImpl.java

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@
4848
import io.dapr.client.domain.ConversationTools;
4949
import io.dapr.client.domain.ConversationToolsFunction;
5050
import io.dapr.client.domain.DaprMetadata;
51+
import io.dapr.client.domain.DecryptRequestAlpha1;
5152
import io.dapr.client.domain.DeleteJobRequest;
5253
import io.dapr.client.domain.DeleteStateRequest;
5354
import io.dapr.client.domain.DropFailurePolicy;
55+
import io.dapr.client.domain.EncryptRequestAlpha1;
5456
import io.dapr.client.domain.ExecuteStateTransactionRequest;
5557
import io.dapr.client.domain.FailurePolicy;
5658
import io.dapr.client.domain.FailurePolicyType;
@@ -2108,4 +2110,194 @@ private AppConnectionPropertiesHealthMetadata getAppConnectionPropertiesHealth(
21082110
return new AppConnectionPropertiesHealthMetadata(healthCheckPath, healthProbeInterval, healthProbeTimeout,
21092111
healthThreshold);
21102112
}
2113+
2114+
/**
2115+
* {@inheritDoc}
2116+
*/
2117+
@Override
2118+
public Flux<byte[]> encrypt(EncryptRequestAlpha1 request) {
2119+
try {
2120+
if (request == null) {
2121+
throw new IllegalArgumentException("EncryptRequestAlpha1 cannot be null.");
2122+
}
2123+
if (request.getComponentName() == null || request.getComponentName().trim().isEmpty()) {
2124+
throw new IllegalArgumentException("Component name cannot be null or empty.");
2125+
}
2126+
if (request.getKeyName() == null || request.getKeyName().trim().isEmpty()) {
2127+
throw new IllegalArgumentException("Key name cannot be null or empty.");
2128+
}
2129+
if (request.getKeyWrapAlgorithm() == null || request.getKeyWrapAlgorithm().trim().isEmpty()) {
2130+
throw new IllegalArgumentException("Key wrap algorithm cannot be null or empty.");
2131+
}
2132+
if (request.getPlainTextStream() == null) {
2133+
throw new IllegalArgumentException("Plaintext stream cannot be null.");
2134+
}
2135+
2136+
return Flux.create(sink -> {
2137+
// Create response observer to receive encrypted data
2138+
StreamObserver<DaprProtos.EncryptResponse> responseObserver = new StreamObserver<DaprProtos.EncryptResponse>() {
2139+
@Override
2140+
public void onNext(DaprProtos.EncryptResponse response) {
2141+
if (response.hasPayload()) {
2142+
byte[] data = response.getPayload().getData().toByteArray();
2143+
if (data.length > 0) {
2144+
sink.next(data);
2145+
}
2146+
}
2147+
}
2148+
2149+
@Override
2150+
public void onError(Throwable t) {
2151+
sink.error(DaprException.propagate(new DaprException("ENCRYPT_ERROR",
2152+
"Error during encryption: " + t.getMessage(), t)));
2153+
}
2154+
2155+
@Override
2156+
public void onCompleted() {
2157+
sink.complete();
2158+
}
2159+
};
2160+
2161+
// Get the request stream observer from gRPC
2162+
StreamObserver<DaprProtos.EncryptRequest> requestObserver =
2163+
intercept(null, asyncStub).encryptAlpha1(responseObserver);
2164+
2165+
// Build options for the first message
2166+
DaprProtos.EncryptRequestOptions.Builder optionsBuilder = DaprProtos.EncryptRequestOptions.newBuilder()
2167+
.setComponentName(request.getComponentName())
2168+
.setKeyName(request.getKeyName())
2169+
.setKeyWrapAlgorithm(request.getKeyWrapAlgorithm());
2170+
2171+
if (request.getDataEncryptionCipher() != null && !request.getDataEncryptionCipher().isEmpty()) {
2172+
optionsBuilder.setDataEncryptionCipher(request.getDataEncryptionCipher());
2173+
}
2174+
optionsBuilder.setOmitDecryptionKeyName(request.isOmitDecryptionKeyName());
2175+
if (request.getDecryptionKeyName() != null && !request.getDecryptionKeyName().isEmpty()) {
2176+
optionsBuilder.setDecryptionKeyName(request.getDecryptionKeyName());
2177+
}
2178+
2179+
final DaprProtos.EncryptRequestOptions options = optionsBuilder.build();
2180+
final long[] sequenceNumber = {0};
2181+
final boolean[] firstMessage = {true};
2182+
2183+
// Subscribe to the plaintext stream and send chunks
2184+
request.getPlainTextStream()
2185+
.doOnNext(chunk -> {
2186+
DaprProtos.EncryptRequest.Builder reqBuilder = DaprProtos.EncryptRequest.newBuilder()
2187+
.setPayload(CommonProtos.StreamPayload.newBuilder()
2188+
.setData(ByteString.copyFrom(chunk))
2189+
.setSeq(sequenceNumber[0]++)
2190+
.build());
2191+
2192+
// Include options only in the first message
2193+
if (firstMessage[0]) {
2194+
reqBuilder.setOptions(options);
2195+
firstMessage[0] = false;
2196+
}
2197+
2198+
requestObserver.onNext(reqBuilder.build());
2199+
})
2200+
.doOnError(error -> {
2201+
requestObserver.onError(error);
2202+
sink.error(DaprException.propagate(new DaprException("ENCRYPT_ERROR",
2203+
"Error reading plaintext stream: " + error.getMessage(), error)));
2204+
})
2205+
.doOnComplete(() -> {
2206+
requestObserver.onCompleted();
2207+
})
2208+
.subscribe();
2209+
});
2210+
} catch (Exception ex) {
2211+
return DaprException.wrapFlux(ex);
2212+
}
2213+
}
2214+
2215+
/**
2216+
* {@inheritDoc}
2217+
*/
2218+
@Override
2219+
public Flux<byte[]> decrypt(DecryptRequestAlpha1 request) {
2220+
try {
2221+
if (request == null) {
2222+
throw new IllegalArgumentException("DecryptRequestAlpha1 cannot be null.");
2223+
}
2224+
if (request.getComponentName() == null || request.getComponentName().trim().isEmpty()) {
2225+
throw new IllegalArgumentException("Component name cannot be null or empty.");
2226+
}
2227+
if (request.getCipherTextStream() == null) {
2228+
throw new IllegalArgumentException("Ciphertext stream cannot be null.");
2229+
}
2230+
2231+
return Flux.create(sink -> {
2232+
// Create response observer to receive decrypted data
2233+
StreamObserver<DaprProtos.DecryptResponse> responseObserver = new StreamObserver<DaprProtos.DecryptResponse>() {
2234+
@Override
2235+
public void onNext(DaprProtos.DecryptResponse response) {
2236+
if (response.hasPayload()) {
2237+
byte[] data = response.getPayload().getData().toByteArray();
2238+
if (data.length > 0) {
2239+
sink.next(data);
2240+
}
2241+
}
2242+
}
2243+
2244+
@Override
2245+
public void onError(Throwable t) {
2246+
sink.error(DaprException.propagate(new DaprException("DECRYPT_ERROR",
2247+
"Error during decryption: " + t.getMessage(), t)));
2248+
}
2249+
2250+
@Override
2251+
public void onCompleted() {
2252+
sink.complete();
2253+
}
2254+
};
2255+
2256+
// Get the request stream observer from gRPC
2257+
StreamObserver<DaprProtos.DecryptRequest> requestObserver =
2258+
intercept(null, asyncStub).decryptAlpha1(responseObserver);
2259+
2260+
// Build options for the first message
2261+
DaprProtos.DecryptRequestOptions.Builder optionsBuilder = DaprProtos.DecryptRequestOptions.newBuilder()
2262+
.setComponentName(request.getComponentName());
2263+
2264+
if (request.getKeyName() != null && !request.getKeyName().isEmpty()) {
2265+
optionsBuilder.setKeyName(request.getKeyName());
2266+
}
2267+
2268+
final DaprProtos.DecryptRequestOptions options = optionsBuilder.build();
2269+
final long[] sequenceNumber = {0};
2270+
final boolean[] firstMessage = {true};
2271+
2272+
// Subscribe to the ciphertext stream and send chunks
2273+
request.getCipherTextStream()
2274+
.doOnNext(chunk -> {
2275+
DaprProtos.DecryptRequest.Builder reqBuilder = DaprProtos.DecryptRequest.newBuilder()
2276+
.setPayload(CommonProtos.StreamPayload.newBuilder()
2277+
.setData(ByteString.copyFrom(chunk))
2278+
.setSeq(sequenceNumber[0]++)
2279+
.build());
2280+
2281+
// Include options only in the first message
2282+
if (firstMessage[0]) {
2283+
reqBuilder.setOptions(options);
2284+
firstMessage[0] = false;
2285+
}
2286+
2287+
requestObserver.onNext(reqBuilder.build());
2288+
})
2289+
.doOnError(error -> {
2290+
requestObserver.onError(error);
2291+
sink.error(DaprException.propagate(new DaprException("DECRYPT_ERROR",
2292+
"Error reading ciphertext stream: " + error.getMessage(), error)));
2293+
})
2294+
.doOnComplete(() -> {
2295+
requestObserver.onCompleted();
2296+
})
2297+
.subscribe();
2298+
});
2299+
} catch (Exception ex) {
2300+
return DaprException.wrapFlux(ex);
2301+
}
2302+
}
21112303
}

sdk/src/main/java/io/dapr/client/DaprPreviewClient.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import io.dapr.client.domain.ConversationRequestAlpha2;
2323
import io.dapr.client.domain.ConversationResponse;
2424
import io.dapr.client.domain.ConversationResponseAlpha2;
25+
import io.dapr.client.domain.DecryptRequestAlpha1;
2526
import io.dapr.client.domain.DeleteJobRequest;
27+
import io.dapr.client.domain.EncryptRequestAlpha1;
2628
import io.dapr.client.domain.GetJobRequest;
2729
import io.dapr.client.domain.GetJobResponse;
2830
import io.dapr.client.domain.LockRequest;
@@ -339,4 +341,24 @@ <T> Subscription subscribeToEvents(
339341
* @return {@link ConversationResponseAlpha2}.
340342
*/
341343
public Mono<ConversationResponseAlpha2> converseAlpha2(ConversationRequestAlpha2 conversationRequestAlpha2);
344+
345+
/**
346+
* Encrypt data using the Dapr cryptography building block.
347+
* This method uses streaming to handle large payloads efficiently.
348+
*
349+
* @param request The encryption request containing component name, key information, and plaintext stream.
350+
* @return A Flux of encrypted byte arrays (ciphertext chunks).
351+
* @throws IllegalArgumentException if required parameters are missing.
352+
*/
353+
Flux<byte[]> encrypt(EncryptRequestAlpha1 request);
354+
355+
/**
356+
* Decrypt data using the Dapr cryptography building block.
357+
* This method uses streaming to handle large payloads efficiently.
358+
*
359+
* @param request The decryption request containing component name, optional key name, and ciphertext stream.
360+
* @return A Flux of decrypted byte arrays (plaintext chunks).
361+
* @throws IllegalArgumentException if required parameters are missing.
362+
*/
363+
Flux<byte[]> decrypt(DecryptRequestAlpha1 request);
342364
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2024 The Dapr Authors
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package io.dapr.client.domain;
15+
16+
import reactor.core.publisher.Flux;
17+
18+
/**
19+
* Request to decrypt data using the Dapr Cryptography building block.
20+
* Uses streaming to handle large payloads efficiently.
21+
*/
22+
public class DecryptRequestAlpha1 {
23+
24+
private final String componentName;
25+
private final Flux<byte[]> cipherTextStream;
26+
private String keyName;
27+
28+
/**
29+
* Constructor for DecryptRequestAlpha1.
30+
*
31+
* @param componentName Name of the cryptography component. Required.
32+
* @param cipherTextStream Stream of ciphertext data to decrypt. Required.
33+
*/
34+
public DecryptRequestAlpha1(String componentName, Flux<byte[]> cipherTextStream) {
35+
this.componentName = componentName;
36+
this.cipherTextStream = cipherTextStream;
37+
}
38+
39+
/**
40+
* Gets the cryptography component name.
41+
*
42+
* @return the component name
43+
*/
44+
public String getComponentName() {
45+
return componentName;
46+
}
47+
48+
/**
49+
* Gets the ciphertext data stream to decrypt.
50+
*
51+
* @return the ciphertext stream as Flux of byte arrays
52+
*/
53+
public Flux<byte[]> getCipherTextStream() {
54+
return cipherTextStream;
55+
}
56+
57+
/**
58+
* Gets the key name (or name/version) to use for decryption.
59+
*
60+
* @return the key name, or null if using the key embedded in the ciphertext
61+
*/
62+
public String getKeyName() {
63+
return keyName;
64+
}
65+
66+
/**
67+
* Sets the key name (or name/version) to decrypt the message.
68+
* This overrides any key reference included in the message if present.
69+
* This is required if the message doesn't include a key reference
70+
* (i.e., was created with omitDecryptionKeyName set to true).
71+
*
72+
* @param keyName the key name to use for decryption
73+
* @return this request instance for method chaining
74+
*/
75+
public DecryptRequestAlpha1 setKeyName(String keyName) {
76+
this.keyName = keyName;
77+
return this;
78+
}
79+
}

0 commit comments

Comments
 (0)