PIE-1526: Normalize account permissioning addresses in whitelist (#1320)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Lucas Saldanha 6 years ago committed by GitHub
parent d5da0eb47b
commit 7d28dd37fb
  1. 25
      ethereum/permissioning/src/main/java/tech/pegasys/pantheon/ethereum/permissioning/AccountWhitelistController.java
  2. 43
      ethereum/permissioning/src/test/java/tech/pegasys/pantheon/ethereum/permissioning/AccountWhitelistControllerTest.java

@ -17,8 +17,10 @@ import tech.pegasys.pantheon.util.bytes.BytesValue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -55,18 +57,20 @@ public class AccountWhitelistController {
}
public WhitelistOperationResult addAccounts(final List<String> accounts) {
final WhitelistOperationResult inputValidationResult = inputValidation(accounts);
final List<String> normalizedAccounts = normalizeAccounts(accounts);
final WhitelistOperationResult inputValidationResult = inputValidation(normalizedAccounts);
if (inputValidationResult != WhitelistOperationResult.SUCCESS) {
return inputValidationResult;
}
boolean inputHasExistingAccount = accounts.stream().anyMatch(accountWhitelist::contains);
boolean inputHasExistingAccount =
normalizedAccounts.stream().anyMatch(accountWhitelist::contains);
if (inputHasExistingAccount) {
return WhitelistOperationResult.ERROR_EXISTING_ENTRY;
}
final List<String> oldWhitelist = new ArrayList<>(this.accountWhitelist);
this.accountWhitelist.addAll(accounts);
this.accountWhitelist.addAll(normalizedAccounts);
try {
verifyConfigurationFileState(oldWhitelist);
updateConfigurationFile(accountWhitelist);
@ -81,18 +85,19 @@ public class AccountWhitelistController {
}
public WhitelistOperationResult removeAccounts(final List<String> accounts) {
final WhitelistOperationResult inputValidationResult = inputValidation(accounts);
final List<String> normalizedAccounts = normalizeAccounts(accounts);
final WhitelistOperationResult inputValidationResult = inputValidation(normalizedAccounts);
if (inputValidationResult != WhitelistOperationResult.SUCCESS) {
return inputValidationResult;
}
if (!accountWhitelist.containsAll(accounts)) {
if (!accountWhitelist.containsAll(normalizedAccounts)) {
return WhitelistOperationResult.ERROR_ABSENT_ENTRY;
}
final List<String> oldWhitelist = new ArrayList<>(this.accountWhitelist);
this.accountWhitelist.removeAll(accounts);
this.accountWhitelist.removeAll(normalizedAccounts);
try {
verifyConfigurationFileState(oldWhitelist);
updateConfigurationFile(accountWhitelist);
@ -187,4 +192,12 @@ public class AccountWhitelistController {
throw new RuntimeException(e);
}
}
private List<String> normalizeAccounts(final List<String> accounts) {
if (accounts != null) {
return accounts.parallelStream().map(String::toLowerCase).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
}

@ -59,6 +59,17 @@ public class AccountWhitelistControllerTest {
.contains("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73");
}
@Test
public void whenLoadingAccountsFromConfigShouldNormalizeAccountsToLowerCase() {
when(permissioningConfig.isAccountWhitelistEnabled()).thenReturn(true);
when(permissioningConfig.getAccountWhitelist())
.thenReturn(singletonList("0xFE3B557E8Fb62b89F4916B721be55cEb828dBd73"));
controller = new AccountWhitelistController(permissioningConfig, whitelistPersistor);
assertThat(controller.getAccountWhitelist())
.containsExactly("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73");
}
@Test
public void whenPermConfigContainsEmptyListOfAccountsContainsShouldReturnFalse() {
when(permissioningConfig.isAccountWhitelistEnabled()).thenReturn(true);
@ -87,6 +98,17 @@ public class AccountWhitelistControllerTest {
.containsExactly("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73");
}
@Test
public void addExistingAccountWithDifferentCasingShouldReturnExistingEntryResult() {
controller.addAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"));
WhitelistOperationResult addResult =
controller.addAccounts(Arrays.asList("0xFE3B557E8Fb62b89F4916B721be55cEb828dBd73"));
assertThat(addResult).isEqualTo(WhitelistOperationResult.ERROR_EXISTING_ENTRY);
assertThat(controller.getAccountWhitelist())
.containsExactly("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73");
}
@Test
public void addValidAccountsShouldReturnSuccessResult() {
WhitelistOperationResult addResult =
@ -97,6 +119,16 @@ public class AccountWhitelistControllerTest {
.containsExactly("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73");
}
@Test
public void addAccountsShouldAddAccountNormalizedToLowerCase() {
WhitelistOperationResult addResult =
controller.addAccounts(Arrays.asList("0xFE3B557E8Fb62b89F4916B721be55cEb828dBd73"));
assertThat(addResult).isEqualTo(WhitelistOperationResult.SUCCESS);
assertThat(controller.getAccountWhitelist())
.containsExactly("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73");
}
@Test
public void removeExistingAccountShouldReturnSuccessResult() {
controller.addAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"));
@ -108,6 +140,17 @@ public class AccountWhitelistControllerTest {
assertThat(controller.getAccountWhitelist()).isEmpty();
}
@Test
public void removeAccountShouldNormalizeAccountToLowerCAse() {
controller.addAccounts(Arrays.asList("0xfe3b557e8fb62b89f4916b721be55ceb828dbd73"));
WhitelistOperationResult removeResult =
controller.removeAccounts(Arrays.asList("0xFE3B557E8Fb62b89F4916B721be55cEb828dBd73"));
assertThat(removeResult).isEqualTo(WhitelistOperationResult.SUCCESS);
assertThat(controller.getAccountWhitelist()).isEmpty();
}
@Test
public void removeAbsentAccountShouldReturnAbsentEntryResult() {
WhitelistOperationResult removeResult =

Loading…
Cancel
Save