forwardKeys = keyPairConverter.convert(config.getAlwaysSendTo());
- LoggerFactory.getLogger(EnclaveFactory.class).debug("Creating enclave");
+ LOGGER.debug("Creating enclave");
Enclave enclave = new EnclaveImpl(encryptor, new KeyManagerImpl(keys, forwardKeys));
- LoggerFactory.getLogger(EnclaveFactory.class).debug("Created enclave {}", enclave);
+ LOGGER.debug("Created enclave {}", enclave);
return enclave;
}
-
- /**
- * Determines from the provided configuration whether to construct a client to a remote service,
- * or to create a local instance.
- *
- * If a remote instance is requested, it is constructed from a {@link EnclaveClientFactory}.
- *
- * @param config the global configuration to use to create a remote enclave connection
- * @return the {@link Enclave}, which may be either local or remote
- */
- default Enclave create(Config config) {
- EnclaveHolder enclaveHolder = EnclaveHolder.getInstance();
- Optional enclave = enclaveHolder.getEnclave();
- if (enclave.isPresent()) {
- return enclave.get();
- }
-
- LoggerFactory.getLogger(EnclaveFactory.class).info("Creating enclave");
- try {
- final Optional enclaveServerConfig =
- config.getServerConfigs().stream().filter(sc -> sc.getApp() == AppType.ENCLAVE).findAny();
-
- if (enclaveServerConfig.isPresent()) {
- LoggerFactory.getLogger(EnclaveFactory.class).info("Creating remoted enclave");
- return enclaveHolder.setEnclave(EnclaveClientFactory.create().create(config));
- }
- return enclaveHolder.setEnclave(createServer(config));
- } catch (Throwable ex) {
- LoggerFactory.getLogger(EnclaveFactory.class).error("", ex);
- throw ex;
- }
- }
-
- default Optional enclave() {
- EnclaveHolder enclaveHolder = EnclaveHolder.getInstance();
- return enclaveHolder.getEnclave();
- }
-
- static EnclaveFactory create() {
- LoggerFactory.getLogger(EnclaveFactory.class).debug("Creating EnclaveFactory");
- return ServiceLoaderUtil.load(EnclaveFactory.class).orElseGet(() -> new EnclaveFactory() {});
- }
}
diff --git a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveHolder.java b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveHolder.java
index dae5ddf917..6e1b32ce16 100644
--- a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveHolder.java
+++ b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveHolder.java
@@ -1,6 +1,5 @@
package com.quorum.tessera.enclave;
-import com.quorum.tessera.ServiceLoaderUtil;
import java.util.Optional;
public interface EnclaveHolder {
@@ -8,8 +7,4 @@ public interface EnclaveHolder {
Optional getEnclave();
Enclave setEnclave(Enclave enclave);
-
- static EnclaveHolder getInstance() {
- return ServiceLoaderUtil.load(EnclaveHolder.class).orElse(DefaultEnclaveHolder.INSTANCE);
- }
}
diff --git a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveImpl.java b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveImpl.java
index d7fabc3c01..442a8ec9c7 100644
--- a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveImpl.java
+++ b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveImpl.java
@@ -158,8 +158,7 @@ public Set findInvalidSecurityHashes(
return encodedPayload.getAffectedContractTransactions().entrySet().stream()
.filter(
entry -> {
- // TODO - remove extra logs
- LOGGER.info("Verifying hash for TxKey {}", entry.getKey().encodeToBase64());
+ LOGGER.debug("Verifying hash for TxKey {}", entry.getKey().encodeToBase64());
TxHash txHash = entry.getKey();
final Optional affectedTransaction =
diff --git a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveProvider.java b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveProvider.java
new file mode 100644
index 0000000000..560cef53e4
--- /dev/null
+++ b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveProvider.java
@@ -0,0 +1,28 @@
+package com.quorum.tessera.enclave;
+
+import com.quorum.tessera.config.Config;
+import com.quorum.tessera.config.ConfigFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class EnclaveProvider {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(EnclaveProvider.class);
+
+ public static Enclave provider() {
+ EnclaveHolder enclaveHolder = DefaultEnclaveHolder.INSTANCE;
+ if (enclaveHolder.getEnclave().isPresent()) {
+ return enclaveHolder.getEnclave().get();
+ }
+
+ Config config = ConfigFactory.create().getConfig();
+
+ EnclaveFactoryImpl enclaveFactory = new EnclaveFactoryImpl(config);
+
+ LOGGER.debug("Found config {}", config);
+
+ Enclave enclave = enclaveFactory.createEnclave();
+
+ return enclaveHolder.setEnclave(enclave);
+ }
+}
diff --git a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServer.java b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServer.java
new file mode 100644
index 0000000000..5707daf555
--- /dev/null
+++ b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServer.java
@@ -0,0 +1,12 @@
+package com.quorum.tessera.enclave;
+
+import com.quorum.tessera.service.Service;
+import com.quorum.tessera.serviceloader.ServiceLoaderUtil;
+import java.util.ServiceLoader;
+
+public interface EnclaveServer extends Enclave, Service {
+
+ static EnclaveServer create() {
+ return ServiceLoaderUtil.loadSingle(ServiceLoader.load(EnclaveServer.class));
+ }
+}
diff --git a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServerImpl.java b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServerImpl.java
new file mode 100644
index 0000000000..698da6079e
--- /dev/null
+++ b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServerImpl.java
@@ -0,0 +1,89 @@
+package com.quorum.tessera.enclave;
+
+import com.quorum.tessera.encryption.PublicKey;
+import com.quorum.tessera.service.Service;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+class EnclaveServerImpl implements EnclaveServer {
+
+ private Enclave enclave;
+
+ EnclaveServerImpl(Enclave enclave) {
+ this.enclave = Objects.requireNonNull(enclave);
+ }
+
+ @Override
+ public PublicKey defaultPublicKey() {
+ return enclave.defaultPublicKey();
+ }
+
+ @Override
+ public Set getForwardingKeys() {
+ return enclave.getForwardingKeys();
+ }
+
+ @Override
+ public Set getPublicKeys() {
+ return enclave.getPublicKeys();
+ }
+
+ @Override
+ public EncodedPayload encryptPayload(
+ byte[] message,
+ PublicKey senderPublicKey,
+ List recipientPublicKeys,
+ PrivacyMetadata privacyMetadata) {
+ return enclave.encryptPayload(message, senderPublicKey, recipientPublicKeys, privacyMetadata);
+ }
+
+ @Override
+ public EncodedPayload encryptPayload(
+ RawTransaction rawTransaction,
+ List recipientPublicKeys,
+ PrivacyMetadata privacyMetadata) {
+ return enclave.encryptPayload(rawTransaction, recipientPublicKeys, privacyMetadata);
+ }
+
+ @Override
+ public Set findInvalidSecurityHashes(
+ EncodedPayload encodedPayload, List affectedContractTransactions) {
+ return enclave.findInvalidSecurityHashes(encodedPayload, affectedContractTransactions);
+ }
+
+ @Override
+ public RawTransaction encryptRawPayload(byte[] message, PublicKey sender) {
+ return enclave.encryptRawPayload(message, sender);
+ }
+
+ @Override
+ public byte[] unencryptTransaction(EncodedPayload payload, PublicKey providedKey) {
+ return enclave.unencryptTransaction(payload, providedKey);
+ }
+
+ @Override
+ public byte[] unencryptRawPayload(RawTransaction payload) {
+ return enclave.unencryptRawPayload(payload);
+ }
+
+ @Override
+ public byte[] createNewRecipientBox(EncodedPayload payload, PublicKey recipientKey) {
+ return enclave.createNewRecipientBox(payload, recipientKey);
+ }
+
+ @Override
+ public void start() {
+ enclave.start();
+ }
+
+ @Override
+ public void stop() {
+ enclave.stop();
+ }
+
+ @Override
+ public Service.Status status() {
+ return enclave.status();
+ }
+}
diff --git a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServerProvider.java b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServerProvider.java
new file mode 100644
index 0000000000..a3496f5d1e
--- /dev/null
+++ b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/EnclaveServerProvider.java
@@ -0,0 +1,18 @@
+package com.quorum.tessera.enclave;
+
+import com.quorum.tessera.config.Config;
+import com.quorum.tessera.config.ConfigFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class EnclaveServerProvider {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(EnclaveServerProvider.class);
+
+ public static EnclaveServer provider() {
+ Config config = ConfigFactory.create().getConfig();
+ Enclave enclave = EnclaveFactoryImpl.createServer(config);
+ LOGGER.debug("Creating server with {}", enclave);
+ return new EnclaveServerImpl(enclave);
+ }
+}
diff --git a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/KeyPairConverter.java b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/KeyPairConverter.java
index 8f8100a28a..2ca61f2466 100644
--- a/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/KeyPairConverter.java
+++ b/enclave/enclave-api/src/main/java/com/quorum/tessera/enclave/KeyPairConverter.java
@@ -7,19 +7,12 @@
import com.quorum.tessera.config.keypairs.ConfigKeyPair;
import com.quorum.tessera.config.keypairs.HashicorpVaultKeyPair;
import com.quorum.tessera.config.util.EnvironmentVariableProvider;
-import com.quorum.tessera.config.vault.data.AWSGetSecretData;
-import com.quorum.tessera.config.vault.data.AzureGetSecretData;
-import com.quorum.tessera.config.vault.data.GetSecretData;
-import com.quorum.tessera.config.vault.data.HashicorpGetSecretData;
import com.quorum.tessera.encryption.KeyPair;
import com.quorum.tessera.encryption.PrivateKey;
import com.quorum.tessera.encryption.PublicKey;
import com.quorum.tessera.key.vault.KeyVaultService;
import com.quorum.tessera.key.vault.KeyVaultServiceFactory;
-import java.util.Base64;
-import java.util.Collection;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
import java.util.stream.Collectors;
public class KeyPairConverter {
@@ -38,8 +31,8 @@ public Collection convert(Collection configKeyPairs) {
}
private KeyPair convert(ConfigKeyPair configKeyPair) {
- String base64PublicKey;
- String base64PrivateKey;
+ final String base64PublicKey;
+ final String base64PrivateKey;
if (configKeyPair instanceof AzureVaultKeyPair) {
@@ -50,10 +43,13 @@ private KeyPair convert(ConfigKeyPair configKeyPair) {
AzureVaultKeyPair akp = (AzureVaultKeyPair) configKeyPair;
- GetSecretData getPublicKeyData =
- new AzureGetSecretData(akp.getPublicKeyId(), akp.getPublicKeyVersion());
- GetSecretData getPrivateKeyData =
- new AzureGetSecretData(akp.getPrivateKeyId(), akp.getPrivateKeyVersion());
+ Map getPublicKeyData =
+ new HashMap<>(Map.of("secretName", akp.getPublicKeyId()));
+ getPublicKeyData.put("secretVersion", akp.getPublicKeyVersion());
+
+ Map getPrivateKeyData =
+ new HashMap<>(Map.of("secretName", akp.getPrivateKeyId()));
+ getPrivateKeyData.put("secretVersion", akp.getPrivateKeyVersion());
base64PublicKey = keyVaultService.getSecret(getPublicKeyData);
base64PrivateKey = keyVaultService.getSecret(getPrivateKeyData);
@@ -66,18 +62,19 @@ private KeyPair convert(ConfigKeyPair configKeyPair) {
HashicorpVaultKeyPair hkp = (HashicorpVaultKeyPair) configKeyPair;
- GetSecretData getPublicKeyData =
- new HashicorpGetSecretData(
- hkp.getSecretEngineName(),
- hkp.getSecretName(),
- hkp.getPublicKeyId(),
- hkp.getSecretVersion());
- GetSecretData getPrivateKeyData =
- new HashicorpGetSecretData(
- hkp.getSecretEngineName(),
- hkp.getSecretName(),
- hkp.getPrivateKeyId(),
- hkp.getSecretVersion());
+ Map getPublicKeyData =
+ Map.of(
+ "secretEngineName", hkp.getSecretEngineName(),
+ "secretName", hkp.getSecretName(),
+ "secretId", hkp.getPublicKeyId(),
+ "secretVersion", Objects.toString(hkp.getSecretVersion()));
+
+ Map