Format AttestationCleint.java
package com.google.oak.functions.client;
import com.google.common.base.VerifyException;
-import com.google.oak.functions.client.AeadEncryptor;
-import com.google.oak.functions.client.KeyNegotiator;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;
-import java.lang.RuntimeException;
import java.security.GeneralSecurityException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
@@ -24,6 +21,10 @@ import oak.functions.server.RemoteAttestationGrpc;
import oak.functions.server.RemoteAttestationGrpc.RemoteAttestationStub;
// TODO(#2121): Implement a protocol independent state machine.
+/**
+ * Client with remote attestation support for sending requests to an Oak Functions loader
+ * application.
+ */
public class AttestationClient {
private static final Logger logger = Logger.getLogger(AttestationClient.class.getName());
private final ManagedChannel channel;
@@ -38,7 +39,8 @@ public class AttestationClient {
// Create server response handler.
messageQueue = new ArrayBlockingQueue<>(1);
- StreamObserver<AttestedInvokeResponse> responseObserver = new StreamObserver<AttestedInvokeResponse>() {
+ StreamObserver<AttestedInvokeResponse> responseObserver =
+ new StreamObserver<AttestedInvokeResponse>() {
@Override
public void onNext(AttestedInvokeResponse response) {
try {
@@ -58,8 +60,7 @@ public class AttestationClient {
}
@Override
- public void onCompleted() {
- }
+ public void onCompleted() {}
};
requestObserver = stub.attestedInvoke(responseObserver);
@@ -69,8 +70,10 @@ public class AttestationClient {
ByteString publicKeyBytes = ByteString.copyFrom(publicKey);
// Send client public key to the server.
- AttestedInvokeRequest request = AttestedInvokeRequest.newBuilder()
- .setClientIdentity(ClientIdentity.newBuilder().setPublicKey(publicKeyBytes).build()).build();
+ AttestedInvokeRequest request =
+ AttestedInvokeRequest.newBuilder()
+ .setClientIdentity(ClientIdentity.newBuilder().setPublicKey(publicKeyBytes).build())
+ .build();
requestObserver.onNext(request);
AttestedInvokeResponse response = messageQueue.take();
@@ -96,22 +99,23 @@ public class AttestationClient {
channel.shutdown();
}
- /**
- * Encrypts and sends `message` via an attested gRPC channel to the server.
- */
+ /** Encrypts and sends `message` via an attested gRPC channel to the server. */
+ @SuppressWarnings("ProtoParseWithRegistry")
public Response send(Request request)
throws GeneralSecurityException, InterruptedException, InvalidProtocolBufferException {
byte[] encryptedMessage = encryptor.encrypt(request.getBody().toByteArray());
- oak.functions.server.Request serverRequest = oak.functions.server.Request.newBuilder()
- .setEncryptedPayload(ByteString.copyFrom(encryptedMessage)).build();
- AttestedInvokeRequest attestedRequest = AttestedInvokeRequest.newBuilder().setRequest(serverRequest).build();
+ oak.functions.server.Request serverRequest =
+ oak.functions.server.Request.newBuilder()
+ .setEncryptedPayload(ByteString.copyFrom(encryptedMessage))
+ .build();
+ AttestedInvokeRequest attestedRequest =
+ AttestedInvokeRequest.newBuilder().setRequest(serverRequest).build();
requestObserver.onNext(attestedRequest);
AttestedInvokeResponse attestedResponse = messageQueue.take();
byte[] responsePayload = attestedResponse.getEncryptedPayload().toByteArray();
byte[] decryptedResponse = encryptor.decrypt(responsePayload);
- Response response = Response.parseFrom(decryptedResponse);
- return response;
+ return Response.parseFrom(decryptedResponse);
}
}
