Skip to content

Commit

Permalink
Allow empty password files to be read (#116)
Browse files Browse the repository at this point in the history
Allow empty password files to be read

It's possible to create web3 wallets with no password so it seems reasonable to allow this case

Relates to Consensys/ethsigner#372
  • Loading branch information
siladu committed Sep 24, 2021
1 parent 747576c commit ba977fe
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#Changelog

## 1.0.19
### Features Added
- Allow empty password files to be read when creating a Signer

## 1.0.18
### Breaking Changes
- Upgrade to Azure Key Vault 4.3.3 removes support for previously deprecated SECP256K1 curve.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

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

import com.google.common.io.Files;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class PasswordFileUtil {
private static final Logger LOG = LogManager.getLogger();

/**
* Read password from the first line of specified file
Expand All @@ -33,11 +31,6 @@ public class PasswordFileUtil {
* @throws SignerInitializationException If password file is empty
*/
public static String readPasswordFromFile(final Path path) throws IOException {
final String password = Files.asCharSource(path.toFile(), UTF_8).readFirstLine();
if (password == null || password.isEmpty()) {
LOG.error("Cannot read password from empty file: " + path);
throw new SignerInitializationException("Cannot read password from empty file: " + path);
}
return password;
return Optional.ofNullable(Files.asCharSource(path.toFile(), UTF_8).readFirstLine()).orElse("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -41,19 +40,11 @@ void canReadSpaceOnlyPasswordFromFile(@TempDir final Path tempDir) throws IOExce
assertThat(PasswordFileUtil.readPasswordFromFile(passwordFile)).isEqualTo(" ");
}

@Test
void emptyFileThrowsException(@TempDir final Path tempDir) throws IOException {
final Path passwordFile = Files.write(tempDir.resolve("password.txt"), new byte[0]);
assertThatExceptionOfType(SignerInitializationException.class)
.isThrownBy(() -> PasswordFileUtil.readPasswordFromFile(passwordFile))
.withMessage("Cannot read password from empty file: %s", passwordFile);
}

@Test
void fileWithEolOnlyThrowsException(@TempDir final Path tempDir) throws IOException {
final Path passwordFile = Files.write(tempDir.resolve("password.txt"), "\n".getBytes(UTF_8));
assertThatExceptionOfType(SignerInitializationException.class)
.isThrownBy(() -> PasswordFileUtil.readPasswordFromFile(passwordFile))
.withMessage("Cannot read password from empty file: %s", passwordFile);
@ParameterizedTest
@ValueSource(strings = {"", "\n"})
void canReadPasswordFromEmptyStringFile(final String content, @TempDir final Path tempDir)
throws IOException {
final Path passwordFile = Files.write(tempDir.resolve("password.txt"), content.getBytes(UTF_8));
assertThat(PasswordFileUtil.readPasswordFromFile(passwordFile)).isEqualTo("");
}
}

0 comments on commit ba977fe

Please sign in to comment.