Skip to content

Commit 753b55a

Browse files
macfarlausmansaleem
andcommitted
[ES-57] Modify EthSigner to use TLS connection to Hashicorp Vault (Consensys#204)
* Introduced following options to subcommand hashicorp-signer `--tls-enabled` To enable TLS when connecting to Hashicorp Vault. True by default `--tls-known-server-file` (Optional) Path to the file containing Hashicorp Vault's host, port and self-signed certificate fingerprint * Acceptance tests using Dockerized Hashicorp Vault in TLS mode Signed-off-by: Sally MacFarlane <sally.macfarlane@consensys.net> Co-authored-by: Usman Saleem <usman@usmans.info>
1 parent fca49d0 commit 753b55a

File tree

43 files changed

+1962
-745
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1962
-745
lines changed

acceptance-tests/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ dependencies {
1616
testRuntimeOnly 'javax.activation:activation'
1717
testRuntimeOnly 'org.apache.logging.log4j:log4j-core'
1818
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
19+
testRuntimeOnly 'org.bouncycastle:bcpkix-jdk15on'
1920

2021
testImplementation project(':ethsigner:core')
22+
testImplementation project(path: ':ethsigner:signer:multikey', configuration: 'testSupportArtifacts')
2123

2224
testImplementation 'com.github.docker-java:docker-java'
2325
testImplementation 'org.junit.jupiter:junit-jupiter-api'
@@ -39,5 +41,7 @@ task acceptanceTest(type: Test) {
3941
group = 'verification'
4042

4143
useJUnitPlatform()
44+
// toggle to show standard out and standard error of the test JVM(s) on the console
45+
testLogging.showStandardStreams = false
4246
}
4347
acceptanceTest.dependsOn(rootProject.installDist)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2020 ConsenSys AG.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package tech.pegasys.ethsigner.tests.dsl.hashicorp;
14+
15+
import java.io.IOException;
16+
import java.io.UncheckedIOException;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
19+
import java.util.Optional;
20+
21+
import com.github.dockerjava.api.DockerClient;
22+
import org.apache.tuweni.net.tls.TLS;
23+
24+
public class HashicorpNode {
25+
private final HashicorpVaultDockerCertificate hashicorpVaultDockerCertificate;
26+
private final DockerClient dockerClient;
27+
private HashicorpVaultDocker hashicorpVaultDocker;
28+
private Optional<Path> knownServerFile = Optional.empty();
29+
30+
private HashicorpNode(final DockerClient dockerClient) {
31+
this(dockerClient, null);
32+
}
33+
34+
private HashicorpNode(
35+
final DockerClient dockerClient,
36+
final HashicorpVaultDockerCertificate hashicorpVaultDockerCertificate) {
37+
this.dockerClient = dockerClient;
38+
this.hashicorpVaultDockerCertificate = hashicorpVaultDockerCertificate;
39+
}
40+
41+
public static HashicorpNode createAndStartHashicorp(
42+
final DockerClient dockerClient, final boolean withTls) {
43+
final HashicorpNode hashicorpNode =
44+
withTls
45+
? new HashicorpNode(dockerClient, HashicorpVaultDockerCertificate.create())
46+
: new HashicorpNode(dockerClient);
47+
hashicorpNode.start();
48+
return hashicorpNode;
49+
}
50+
51+
private void start() {
52+
hashicorpVaultDocker =
53+
HashicorpVaultDocker.createVaultDocker(dockerClient, hashicorpVaultDockerCertificate);
54+
Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown));
55+
56+
if (isTlsEnabled()) {
57+
knownServerFile = Optional.of(createKnownServerFile());
58+
}
59+
}
60+
61+
public void shutdown() {
62+
if (hashicorpVaultDocker != null) {
63+
hashicorpVaultDocker.shutdown();
64+
}
65+
}
66+
67+
public String getVaultToken() {
68+
return hashicorpVaultDocker.getHashicorpRootToken();
69+
}
70+
71+
public String getHost() {
72+
return hashicorpVaultDocker.getIpAddress();
73+
}
74+
75+
public String getSigningKeyPath() {
76+
return hashicorpVaultDocker.getVaultSigningKeyPath();
77+
}
78+
79+
public int getPort() {
80+
return hashicorpVaultDocker.getPort();
81+
}
82+
83+
public boolean isTlsEnabled() {
84+
return hashicorpVaultDockerCertificate != null;
85+
}
86+
87+
public Optional<Path> getKnownServerFilePath() {
88+
return knownServerFile;
89+
}
90+
91+
private Path createKnownServerFile() {
92+
try {
93+
final Path tempFile = Files.createTempFile("knownServer", ".txt");
94+
final String hexFingerprint =
95+
TLS.certificateHexFingerprint(hashicorpVaultDockerCertificate.getTlsCertificate());
96+
Files.writeString(tempFile, String.format("%s:%d %s", getHost(), getPort(), hexFingerprint));
97+
return tempFile;
98+
} catch (final IOException e) {
99+
throw new UncheckedIOException(e);
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)