mirror of https://github.com/hyperledger/besu
added --nodes-whitelist param to CLI and NodeWhitelistController (#346)
* added --nodes-whitelist param to CLI and NodeWhitelistController
parent
665ae97b84
commit
e3a58b6b7b
@ -0,0 +1,43 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 ConsenSys AG. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
*/ |
||||||
|
package tech.pegasys.pantheon.ethereum.permissioning; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.Collection; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
public class PermissioningConfiguration { |
||||||
|
private List<String> nodeWhitelist; |
||||||
|
private boolean nodeWhitelistSet; |
||||||
|
|
||||||
|
public List<String> getNodeWhitelist() { |
||||||
|
return nodeWhitelist; |
||||||
|
} |
||||||
|
|
||||||
|
public static PermissioningConfiguration createDefault() { |
||||||
|
final PermissioningConfiguration config = new PermissioningConfiguration(); |
||||||
|
config.nodeWhitelist = new ArrayList<>(); |
||||||
|
return config; |
||||||
|
} |
||||||
|
|
||||||
|
public void setNodeWhitelist(final Collection<String> nodeWhitelist) { |
||||||
|
if (nodeWhitelist != null) { |
||||||
|
this.nodeWhitelist.addAll(nodeWhitelist); |
||||||
|
this.nodeWhitelistSet = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public boolean isNodeWhitelistSet() { |
||||||
|
return nodeWhitelistSet; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,46 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 ConsenSys AG. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
*/ |
||||||
|
package tech.pegasys.pantheon.ethereum.permissioning; |
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||||
|
|
||||||
|
import java.util.Arrays; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
|
||||||
|
public class PermissioningConfigurationTest { |
||||||
|
|
||||||
|
@Test |
||||||
|
public void defaultConfiguration() { |
||||||
|
final PermissioningConfiguration configuration = PermissioningConfiguration.createDefault(); |
||||||
|
assertThat(configuration.getNodeWhitelist()).isEmpty(); |
||||||
|
assertThat(configuration.isNodeWhitelistSet()).isFalse(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void setNodeWhitelist() { |
||||||
|
final String[] nodes = {"enode://001@123:4567", "enode://002@123:4567", "enode://003@123:4567"}; |
||||||
|
final PermissioningConfiguration configuration = PermissioningConfiguration.createDefault(); |
||||||
|
configuration.setNodeWhitelist(Arrays.asList(nodes)); |
||||||
|
assertThat(configuration.getNodeWhitelist()).containsExactlyInAnyOrder(nodes); |
||||||
|
assertThat(configuration.isNodeWhitelistSet()).isTrue(); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void setNodeWhiteListPassingNull() { |
||||||
|
final PermissioningConfiguration configuration = PermissioningConfiguration.createDefault(); |
||||||
|
configuration.setNodeWhitelist(null); |
||||||
|
assertThat(configuration.getNodeWhitelist()).isEmpty(); |
||||||
|
assertThat(configuration.isNodeWhitelistSet()).isFalse(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,49 @@ |
|||||||
|
/* |
||||||
|
* Copyright 2018 ConsenSys AG. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
||||||
|
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
||||||
|
* specific language governing permissions and limitations under the License. |
||||||
|
*/ |
||||||
|
package tech.pegasys.pantheon.controller; |
||||||
|
|
||||||
|
import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager; |
||||||
|
import org.apache.logging.log4j.Logger; |
||||||
|
|
||||||
|
public class NodeWhitelistController { |
||||||
|
|
||||||
|
private static final Logger LOG = LogManager.getLogger(); |
||||||
|
|
||||||
|
private static List<String> nodeWhitelist; |
||||||
|
private static boolean nodeWhitelistSet = false; |
||||||
|
|
||||||
|
public NodeWhitelistController(final PermissioningConfiguration configuration) { |
||||||
|
nodeWhitelist = new ArrayList<>(); |
||||||
|
if (configuration != null && configuration.getNodeWhitelist() != null) { |
||||||
|
nodeWhitelist.addAll(configuration.getNodeWhitelist()); |
||||||
|
nodeWhitelistSet = true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public boolean addNode(final String nodeId) { |
||||||
|
return nodeWhitelist.add(nodeId); |
||||||
|
} |
||||||
|
|
||||||
|
public boolean removeNode(final String nodeId) { |
||||||
|
return nodeWhitelist.remove(nodeId); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isNodeWhitelistSet() { |
||||||
|
return nodeWhitelistSet; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue