Skip to content

Commit

Permalink
Implement DirectoryAuthorityFactory
Browse files Browse the repository at this point in the history
Other changes:
- Set identity and relay key fingerprints
  • Loading branch information
alvasw committed Jun 19, 2023
1 parent c04b50e commit 8aceaf8
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.tor.local_network;

import bisq.common.util.NetworkUtils;
import bisq.tor.local_network.torrc.DirectoryAuthorityTorrcGenerator;
import bisq.tor.local_network.torrc.TorrcFileGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;

public class DirectoryAuthorityTests {

@Test
public void createOneDA(@TempDir Path tempDir) throws IOException, InterruptedException {
var firstDirectoryAuthority = DirectoryAuthority.builder()
.nickname("DA_1")
.dataDir(tempDir)
.controlPort(NetworkUtils.findFreeSystemPort())
.orPort(NetworkUtils.findFreeSystemPort())
.dirPort(NetworkUtils.findFreeSystemPort())
.build();
DirectoryAuthorityFactory.createDirectoryAuthority(firstDirectoryAuthority, "my_passphrase");

assertThat(tempDir).isNotEmptyDirectory();
assertThat(tempDir.resolve("keys")).isNotEmptyDirectory();
}

@Test
public void createThreeDA(@TempDir Path tempDir) throws IOException, InterruptedException {
Path firstDaDataDir = tempDir.resolve("da_1");
var firstDirectoryAuthority = DirectoryAuthority.builder()
.nickname("DA_1")
.dataDir(firstDaDataDir)
.controlPort(NetworkUtils.findFreeSystemPort())
.orPort(NetworkUtils.findFreeSystemPort())
.dirPort(NetworkUtils.findFreeSystemPort())
.build();

Path secondDaDataDir = tempDir.resolve("da_2");
var secondDirectoryAuthority = DirectoryAuthority.builder()
.nickname("DA_2")
.dataDir(secondDaDataDir)
.controlPort(NetworkUtils.findFreeSystemPort())
.orPort(NetworkUtils.findFreeSystemPort())
.dirPort(NetworkUtils.findFreeSystemPort())
.build();

Path thirdDaDataDir = tempDir.resolve("da_3");
var thirdDirectoryAuthority = DirectoryAuthority.builder()
.nickname("DA_3")
.dataDir(thirdDaDataDir)
.controlPort(NetworkUtils.findFreeSystemPort())
.orPort(NetworkUtils.findFreeSystemPort())
.dirPort(NetworkUtils.findFreeSystemPort())
.build();

Set<DirectoryAuthority> allDAs = Set.of(
firstDirectoryAuthority,
secondDirectoryAuthority,
thirdDirectoryAuthority);

// Generate all keys to have fingerprints
for (DirectoryAuthority da : allDAs) {
DirectoryAuthorityFactory.createDirectoryAuthority(da, "my_passphrase");
}

// Fingerprints are now available
for (DirectoryAuthority da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
var torrcFileGenerator = new TorrcFileGenerator(torDaTorrcGenerator, allDAs);
torrcFileGenerator.generate();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

import java.nio.file.Path;
import java.util.Optional;

@Builder
@Getter
public class DirectoryAuthority {
private final String nickname;
Expand All @@ -33,11 +34,22 @@ public class DirectoryAuthority {
private final int orPort;
private final int dirPort;

private final String v3LongTermSigningKeyFingerprint;
private final String torKeyFingerprint;

private final String exitPolicy = "ExitPolicy accept *:*";

@Setter
private Optional<String> identityKeyFingerprint = Optional.empty();
@Setter
private Optional<String> relayKeyFingerprint = Optional.empty();

@Builder
public DirectoryAuthority(String nickname, Path dataDir, int controlPort, int orPort, int dirPort) {
this.nickname = nickname;
this.dataDir = dataDir;
this.controlPort = controlPort;
this.orPort = orPort;
this.dirPort = dirPort;
}

public Path getTorrcPath() {
return dataDir.resolve("torrc");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.tor.local_network;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

public class DirectoryAuthorityFactory {
public static void createDirectoryAuthority(DirectoryAuthority directoryAuthority,
String passphrase) throws IOException, InterruptedException {
Path dataDir = directoryAuthority.getDataDir();
createDataDirIfNotPresent(dataDir);

Path keysPath = dataDir.resolve("keys");
boolean isSuccess = keysPath.toFile().mkdirs();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create keys folder in data directory for directory authority.");
}

var relayKeyGenProcess = new RelayKeyGenProcess(directoryAuthority);
String firstDirectoryAuthorityAddress = "127.0.0.1:" + directoryAuthority.getDirPort();
var torDAKeyGenProcess = new DirectoryIdentityKeyGenProcess(keysPath, firstDirectoryAuthorityAddress);

var directoryAuthorityKeyGenerator = new DirectoryAuthorityKeyGenerator(torDAKeyGenProcess, relayKeyGenProcess);
directoryAuthorityKeyGenerator.generate(passphrase);

directoryAuthority.setIdentityKeyFingerprint(
directoryAuthorityKeyGenerator.getIdentityKeyFingerprint()
);
directoryAuthority.setRelayKeyFingerprint(
directoryAuthorityKeyGenerator.getRelayKeyFingerprint()
);
}

private static void createDataDirIfNotPresent(Path dataDir) {
File dataDirFile = dataDir.toFile();
if (!dataDirFile.exists()) {
boolean isSuccess = dataDir.toFile().mkdir();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create data directory for directory authority.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package bisq.tor.local_network;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

@Slf4j
public class DirectoryAuthorityKeyGenerator {
Expand All @@ -32,6 +34,11 @@ public class DirectoryAuthorityKeyGenerator {
private final DirectoryIdentityKeyGenProcess identityKeyGenProcess;
private final RelayKeyGenProcess relayKeyGenProcess;

@Getter
private Optional<String> identityKeyFingerprint = Optional.empty();
@Getter
private Optional<String> relayKeyFingerprint = Optional.empty();

public DirectoryAuthorityKeyGenerator(DirectoryIdentityKeyGenProcess identityKeyGenProcess,
RelayKeyGenProcess relayKeyGenProcess) {
this.identityKeyGenProcess = identityKeyGenProcess;
Expand All @@ -40,7 +47,10 @@ public DirectoryAuthorityKeyGenerator(DirectoryIdentityKeyGenProcess identityKey

public void generate(String passphrase) throws IOException, InterruptedException {
String identityKeyFingerprint = generateIdentityKeys(passphrase);
relayKeyGenProcess.generateKeys(identityKeyFingerprint);
this.identityKeyFingerprint = Optional.of(identityKeyFingerprint);

String relayKeyFingerprint = relayKeyGenProcess.generateKeys(identityKeyFingerprint);
this.relayKeyFingerprint = Optional.of(relayKeyFingerprint);
}

private String generateIdentityKeys(String passphrase) throws IOException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public void generate() throws IOException {
allDirAuthorities.forEach(dirAuthority ->
torrcStringBuilder.append("DirAuthority ").append(dirAuthority.getNickname())
.append(" orport=").append(dirAuthority.getOrPort())
.append(" v3ident=").append(dirAuthority.getV3LongTermSigningKeyFingerprint())
.append(" v3ident=").append(dirAuthority.getIdentityKeyFingerprint().orElseThrow())
.append(" 127.0.0.1:").append(dirAuthority.getDirPort())
.append(" ").append(dirAuthority.getTorKeyFingerprint())
.append(" ").append(dirAuthority.getRelayKeyFingerprint().orElseThrow())
.append("\n"));

DirectoryAuthority thisDirectoryAuthority = commonTorrcGenerator.getThisDirectoryAuthority();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -42,10 +43,11 @@ void basicTest(@TempDir Path tempDir) throws IOException {
.orPort(2)
.dirPort(3)

.v3LongTermSigningKeyFingerprint("AAAA_v3")
.torKeyFingerprint("AAAA_fp")
.build();

firstDirAuth.setIdentityKeyFingerprint(Optional.of("AAAA_fp"));
firstDirAuth.setRelayKeyFingerprint(Optional.of("AAAA_v3"));

DirectoryAuthority secondDirAuth = DirectoryAuthority.builder()
.nickname("B")
.dataDir(tempDir.resolve("DA_B"))
Expand All @@ -54,10 +56,11 @@ void basicTest(@TempDir Path tempDir) throws IOException {
.orPort(2)
.dirPort(3)

.v3LongTermSigningKeyFingerprint("BBBB_v3")
.torKeyFingerprint("BBBB_fp")
.build();

secondDirAuth.setIdentityKeyFingerprint(Optional.of("BBBB_fp"));
secondDirAuth.setRelayKeyFingerprint(Optional.of("BBBB_v3"));

var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(firstDirAuth);
var allDirAuthorities = Set.of(firstDirAuth, secondDirAuth);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -42,10 +43,11 @@ void basicTest(@TempDir Path tempDir) throws IOException {
.orPort(2)
.dirPort(3)

.v3LongTermSigningKeyFingerprint("AAAA_v3")
.torKeyFingerprint("AAAA_fp")
.build();

firstDirAuth.setIdentityKeyFingerprint(Optional.of("AAAA_fp"));
firstDirAuth.setRelayKeyFingerprint(Optional.of("AAAA_v3"));

DirectoryAuthority secondDirAuth = DirectoryAuthority.builder()
.nickname("B")
.dataDir(tempDir.resolve("DA_B"))
Expand All @@ -54,10 +56,11 @@ void basicTest(@TempDir Path tempDir) throws IOException {
.orPort(2)
.dirPort(3)

.v3LongTermSigningKeyFingerprint("BBBB_v3")
.torKeyFingerprint("BBBB_fp")
.build();

secondDirAuth.setIdentityKeyFingerprint(Optional.of("BBBB_fp"));
secondDirAuth.setRelayKeyFingerprint(Optional.of("BBBB_v3"));

var relayTorrcGenerator = new RelayTorrcGenerator(firstDirAuth);
var allDirAuthorities = Set.of(firstDirAuth, secondDirAuth);

Expand Down

0 comments on commit 8aceaf8

Please sign in to comment.