Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use token enclave public key when in privacy multi-tenancy mode #272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.hyperledger.besu.enclave.types.SendResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.priv.PrivGetPrivateTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionLegacyResult;
Expand Down Expand Up @@ -76,6 +77,8 @@ public class PrivGetPrivateTransactionIntegrationTest {

private static Vertx vertx = Vertx.vertx();

private EnclavePublicKeyProvider enclavePublicKeyProvider = (user) -> ENCLAVE_PUBLIC_KEY;

@BeforeClass
public static void setUpOnce() throws Exception {
folder.create();
Expand All @@ -89,7 +92,7 @@ public static void setUpOnce() throws Exception {
final EnclaveFactory factory = new EnclaveFactory(vertx);
enclave = factory.createVertxEnclave(testHarness.clientUrl());

privacyController = new PrivacyController(enclave, ENCLAVE_PUBLIC_KEY, null, null, null, null);
privacyController = new PrivacyController(enclave, null, null, null, null);
}

@AfterClass
Expand Down Expand Up @@ -148,7 +151,7 @@ public void before() {
public void returnsStoredPrivateTransaction() {

final PrivGetPrivateTransaction privGetPrivateTransaction =
new PrivGetPrivateTransaction(blockchain, privacyController);
new PrivGetPrivateTransaction(blockchain, privacyController, enclavePublicKeyProvider);

when(blockchain.transactionByHash(any(Hash.class)))
.thenReturn(Optional.of(returnedTransaction));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright ConsenSys AG.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods;

import java.util.Optional;

import io.vertx.ext.auth.User;

@FunctionalInterface
public interface EnclavePublicKeyProvider {
String getEnclaveKey(Optional<User> user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ public class PrivacySendTransaction {
private static final Logger LOG = LogManager.getLogger();

protected final PrivacyController privacyController;
private EnclavePublicKeyProvider enclavePublicKeyProvider;

public PrivacySendTransaction(final PrivacyController privacyController) {
public PrivacySendTransaction(
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.privacyController = privacyController;
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

public PrivateTransaction validateAndDecodeRequest(final JsonRpcRequestContext request)
Expand Down Expand Up @@ -74,7 +78,10 @@ public JsonRpcResponse validateAndExecute(
final String privacyGroupId,
final Supplier<JsonRpcResponse> successfulJsonRpcResponse) {
return privacyController
.validatePrivateTransaction(privateTransaction, privacyGroupId)
.validatePrivateTransaction(
privateTransaction,
privacyGroupId,
enclavePublicKeyProvider.getEnclaveKey(request.getUser()))
.either(
successfulJsonRpcResponse,
(errorReason) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction.ErrorResponseException;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand All @@ -33,14 +34,19 @@
public class EeaSendRawTransaction implements JsonRpcMethod {

private final PrivacySendTransaction privacySendTransaction;
private EnclavePublicKeyProvider enclavePublicKeyProvider;
private TransactionPool transactionPool;
private PrivacyController privacyController;

public EeaSendRawTransaction(
final TransactionPool transactionPool, final PrivacyController privacyController) {
final TransactionPool transactionPool,
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.transactionPool = transactionPool;
this.privacyController = privacyController;
this.privacySendTransaction = new PrivacySendTransaction(privacyController);
this.privacySendTransaction =
new PrivacySendTransaction(privacyController, enclavePublicKeyProvider);
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

@Override
Expand All @@ -59,7 +65,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

final SendTransactionResponse sendTransactionResponse;
try {
sendTransactionResponse = privacyController.sendTransaction(privateTransaction);
sendTransactionResponse =
privacyController.sendTransaction(
privateTransaction, enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
} catch (final Exception e) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.parameters.CreatePrivacyGroupParameter;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
Expand All @@ -33,9 +34,13 @@ public class PrivCreatePrivacyGroup implements JsonRpcMethod {

private static final Logger LOG = getLogger();
private PrivacyController privacyController;
private EnclavePublicKeyProvider enclavePublicKeyProvider;

public PrivCreatePrivacyGroup(final PrivacyController privacyController) {
public PrivCreatePrivacyGroup(
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.privacyController = privacyController;
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

@Override
Expand All @@ -59,7 +64,10 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
try {
response =
privacyController.createPrivacyGroup(
parameter.getAddresses(), parameter.getName(), parameter.getDescription());
parameter.getAddresses(),
parameter.getName(),
parameter.getDescription(),
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
} catch (Exception e) {
LOG.error("Failed to create privacy group", e);
return new JsonRpcErrorResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.privacy.PrivacyController;
Expand All @@ -30,9 +32,13 @@ public class PrivDeletePrivacyGroup implements JsonRpcMethod {

private static final Logger LOG = getLogger();
private PrivacyController privacyController;
private EnclavePublicKeyProvider enclavePublicKeyProvider;

public PrivDeletePrivacyGroup(final PrivacyController privacyController) {
public PrivDeletePrivacyGroup(
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.privacyController = privacyController;
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

@Override
Expand All @@ -48,10 +54,12 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

final String response;
try {
response = privacyController.deletePrivacyGroup(privacyGroupId);
response =
privacyController.deletePrivacyGroup(
privacyGroupId, enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
} catch (Exception e) {
LOG.error("Failed to fetch transaction", e);
return new JsonRpcSuccessResponse(
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(), JsonRpcError.DELETE_PRIVACY_GROUP_ERROR);
}
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.PrivacySendTransaction.ErrorResponseException;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
Expand All @@ -35,10 +36,15 @@ public class PrivDistributeRawTransaction implements JsonRpcMethod {

private final PrivacyController privacyController;
private final PrivacySendTransaction privacySendTransaction;
private EnclavePublicKeyProvider enclavePublicKeyProvider;

public PrivDistributeRawTransaction(final PrivacyController privacyController) {
public PrivDistributeRawTransaction(
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.privacyController = privacyController;
this.privacySendTransaction = new PrivacySendTransaction(privacyController);
this.privacySendTransaction =
new PrivacySendTransaction(privacyController, enclavePublicKeyProvider);
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

@Override
Expand All @@ -57,7 +63,9 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

final SendTransactionResponse sendTransactionResponse;
try {
sendTransactionResponse = privacyController.sendTransaction(privateTransaction);
sendTransactionResponse =
privacyController.sendTransaction(
privateTransaction, enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
} catch (final Exception e) {
return new JsonRpcErrorResponse(
requestContext.getRequest().getId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
Expand All @@ -34,9 +35,13 @@ public class PrivFindPrivacyGroup implements JsonRpcMethod {

private static final Logger LOG = getLogger();
private PrivacyController privacyController;
private EnclavePublicKeyProvider enclavePublicKeyProvider;

public PrivFindPrivacyGroup(final PrivacyController privacyController) {
public PrivFindPrivacyGroup(
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.privacyController = privacyController;
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

@Override
Expand All @@ -54,7 +59,10 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {

PrivacyGroup[] response;
try {
response = privacyController.findPrivacyGroup(Arrays.asList(addresses));
response =
privacyController.findPrivacyGroup(
Arrays.asList(addresses),
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
} catch (Exception e) {
LOG.error("Failed to fetch privacy group", e);
return new JsonRpcErrorResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
Expand All @@ -34,9 +35,13 @@ public class PrivGetEeaTransactionCount implements JsonRpcMethod {
private static final Logger LOG = getLogger();

private PrivacyController privacyController;
private EnclavePublicKeyProvider enclavePublicKeyProvider;

public PrivGetEeaTransactionCount(final PrivacyController privacyController) {
public PrivGetEeaTransactionCount(
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.privacyController = privacyController;
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

@Override
Expand All @@ -56,7 +61,12 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final String[] privateFor = requestContext.getRequiredParameter(2, String[].class);

try {
final long nonce = privacyController.determineNonce(privateFrom, privateFor, address);
final long nonce =
privacyController.determineNonce(
privateFrom,
privateFor,
address,
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
return new JsonRpcSuccessResponse(
requestContext.getRequest().getId(), Quantity.create(nonce));
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.privacy.PrivateTransactionGroupResult;
Expand All @@ -41,11 +42,15 @@ public class PrivGetPrivateTransaction implements JsonRpcMethod {

private final BlockchainQueries blockchain;
private final PrivacyController privacyController;
private EnclavePublicKeyProvider enclavePublicKeyProvider;

public PrivGetPrivateTransaction(
final BlockchainQueries blockchain, final PrivacyController privacyController) {
final BlockchainQueries blockchain,
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.blockchain = blockchain;
this.privacyController = privacyController;
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

@Override
Expand All @@ -68,7 +73,8 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
LOG.trace("Fetching transaction information");
final ReceiveResponse receiveResponse =
privacyController.retrieveTransaction(
resultTransaction.getTransaction().getPayload().toBase64String());
resultTransaction.getTransaction().getPayload().toBase64String(),
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
LOG.trace("Received transaction information");

final BytesValueRLPInput input =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.hyperledger.besu.ethereum.api.jsonrpc.RpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.privacy.methods.EnclavePublicKeyProvider;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcError;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcErrorResponse;
import org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcResponse;
Expand All @@ -28,9 +29,13 @@
public class PrivGetTransactionCount implements JsonRpcMethod {

private final PrivacyController privacyController;
private EnclavePublicKeyProvider enclavePublicKeyProvider;

public PrivGetTransactionCount(final PrivacyController privacyController) {
public PrivGetTransactionCount(
final PrivacyController privacyController,
final EnclavePublicKeyProvider enclavePublicKeyProvider) {
this.privacyController = privacyController;
this.enclavePublicKeyProvider = enclavePublicKeyProvider;
}

@Override
Expand All @@ -48,7 +53,11 @@ public JsonRpcResponse response(final JsonRpcRequestContext requestContext) {
final Address address = requestContext.getRequiredParameter(0, Address.class);
final String privacyGroupId = requestContext.getRequiredParameter(1, String.class);

final long nonce = privacyController.determineNonce(address, privacyGroupId);
final long nonce =
privacyController.determineNonce(
address,
privacyGroupId,
enclavePublicKeyProvider.getEnclaveKey(requestContext.getUser()));
return new JsonRpcSuccessResponse(requestContext.getRequest().getId(), Quantity.create(nonce));
}
}