forked from PegaSysEng/pantheon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nc 2107 permissioning config toml file (PegaSysEng#643)
* create permissioning config from toml file * added CLI options for separately enabling node and account permissioning * moved check for bootnodes on whitelist out of CLI
- Loading branch information
1 parent
b40f68b
commit a4b6045
Showing
20 changed files
with
525 additions
and
196 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
pantheon/src/main/java/tech/pegasys/pantheon/PermissioningConfigurationBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2019 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; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
import tech.pegasys.pantheon.cli.custom.EnodeToURIPropertyConverter; | ||
import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration; | ||
|
||
import java.io.File; | ||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.common.io.Resources; | ||
import net.consensys.cava.toml.TomlArray; | ||
import net.consensys.cava.toml.TomlParseResult; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class PermissioningConfigurationBuilder { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
// will be used to reload the config from a file while node is running | ||
public static PermissioningConfiguration permissioningConfigurationFromToml( | ||
final String configFilePath, | ||
final boolean permissionedNodeEnabled, | ||
final boolean permissionedAccountEnabled) { | ||
|
||
String permToml = permissioningConfigTomlAsString(permissioningConfigFile(configFilePath)); | ||
return permissioningConfiguration( | ||
TomlConfigFileParser.loadConfiguration(permToml), | ||
permissionedNodeEnabled, | ||
permissionedAccountEnabled); | ||
} | ||
|
||
public static PermissioningConfiguration permissioningConfiguration( | ||
final TomlParseResult permToml, | ||
final boolean permissionedNodeEnabled, | ||
final boolean permissionedAccountEnabled) { | ||
|
||
TomlArray accountWhitelistTomlArray = permToml.getArray("accounts-whitelist"); | ||
TomlArray nodeWhitelistTomlArray = permToml.getArray("nodes-whitelist"); | ||
|
||
final PermissioningConfiguration permissioningConfiguration = | ||
PermissioningConfiguration.createDefault(); | ||
if (permissionedAccountEnabled) { | ||
if (accountWhitelistTomlArray != null) { | ||
List<String> accountsWhitelistToml = | ||
accountWhitelistTomlArray | ||
.toList() | ||
.stream() | ||
.map(Object::toString) | ||
.collect(Collectors.toList()); | ||
permissioningConfiguration.setAccountWhitelist(accountsWhitelistToml); | ||
} else { | ||
permissioningConfiguration.setAccountWhitelist(new ArrayList<>()); | ||
} | ||
} | ||
if (permissionedNodeEnabled) { | ||
if (nodeWhitelistTomlArray != null) { | ||
List<URI> nodesWhitelistToml = | ||
nodeWhitelistTomlArray | ||
.toList() | ||
.stream() | ||
.map(Object::toString) | ||
.map(EnodeToURIPropertyConverter::convertToURI) | ||
.collect(Collectors.toList()); | ||
permissioningConfiguration.setNodeWhitelist(nodesWhitelistToml); | ||
} else { | ||
permissioningConfiguration.setNodeWhitelist(new ArrayList<>()); | ||
} | ||
} | ||
return permissioningConfiguration; | ||
} | ||
|
||
private static String permissioningConfigTomlAsString(final File file) { | ||
try { | ||
return Resources.toString(file.toURI().toURL(), UTF_8); | ||
} catch (final Exception e) { | ||
LOG.error("Unable to load permissioning config {}.", file, e); | ||
return null; | ||
} | ||
} | ||
|
||
private static File permissioningConfigFile(final String filename) { | ||
|
||
final File permissioningConfigFile = new File(filename); | ||
if (permissioningConfigFile.exists()) { | ||
return permissioningConfigFile; | ||
} else { | ||
LOG.error("File does not exist: permissioning config path: {}.", filename); | ||
return null; | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
pantheon/src/main/java/tech/pegasys/pantheon/TomlConfigFileParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2019 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; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
import net.consensys.cava.toml.Toml; | ||
import net.consensys.cava.toml.TomlParseError; | ||
import net.consensys.cava.toml.TomlParseResult; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
public class TomlConfigFileParser { | ||
|
||
private static final Logger LOG = LogManager.getLogger(); | ||
|
||
private static TomlParseResult checkConfigurationValidity( | ||
final TomlParseResult result, final String toml) { | ||
if (result == null || result.isEmpty()) { | ||
LOG.error("Unable to read TOML configuration file %s", toml); | ||
} | ||
return result; | ||
} | ||
|
||
public static TomlParseResult loadConfiguration(final String toml) { | ||
TomlParseResult result = Toml.parse(toml); | ||
|
||
if (result.hasErrors()) { | ||
final String errors = | ||
result.errors().stream().map(TomlParseError::toString).collect(Collectors.joining("%n")); | ||
; | ||
LOG.error("Invalid TOML configuration : %s", errors); | ||
} | ||
|
||
return checkConfigurationValidity(result, toml); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
pantheon/src/main/java/tech/pegasys/pantheon/util/PermissioningConfigurationValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2019 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.util; | ||
|
||
import tech.pegasys.pantheon.cli.EthNetworkConfig; | ||
import tech.pegasys.pantheon.ethereum.p2p.config.DiscoveryConfiguration; | ||
import tech.pegasys.pantheon.ethereum.p2p.peers.Endpoint; | ||
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer; | ||
import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration; | ||
|
||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.OptionalInt; | ||
import java.util.stream.Collectors; | ||
|
||
import org.bouncycastle.util.encoders.Hex; | ||
|
||
public class PermissioningConfigurationValidator { | ||
|
||
public static void areAllBootnodesAreInWhitelist( | ||
final EthNetworkConfig ethNetworkConfig, | ||
final PermissioningConfiguration permissioningConfiguration) | ||
throws Exception { | ||
List<Peer> bootnodesNotInWhitelist = new ArrayList<>(); | ||
final List<Peer> bootnodes = | ||
DiscoveryConfiguration.getBootstrapPeersFromGenericCollection( | ||
ethNetworkConfig.getBootNodes()); | ||
if (permissioningConfiguration.isNodeWhitelistSet() && bootnodes != null) { | ||
bootnodesNotInWhitelist = | ||
bootnodes | ||
.stream() | ||
.filter( | ||
node -> | ||
!permissioningConfiguration | ||
.getNodeWhitelist() | ||
.contains(URI.create(buildEnodeURI(node)))) | ||
.collect(Collectors.toList()); | ||
} | ||
if (!bootnodesNotInWhitelist.isEmpty()) { | ||
throw new Exception("Bootnode(s) not in nodes-whitelist " + bootnodesNotInWhitelist); | ||
} | ||
} | ||
|
||
private static String buildEnodeURI(final Peer s) { | ||
String url = Hex.toHexString(s.getId().extractArray()); | ||
Endpoint endpoint = s.getEndpoint(); | ||
String nodeIp = endpoint.getHost(); | ||
OptionalInt tcpPort = endpoint.getTcpPort(); | ||
int udpPort = endpoint.getUdpPort(); | ||
|
||
if (tcpPort.isPresent() && (tcpPort.getAsInt() != udpPort)) { | ||
return String.format( | ||
"enode://%s@%s:%d?discport=%d", url, nodeIp, tcpPort.getAsInt(), udpPort); | ||
} else { | ||
return String.format("enode://%s@%s:%d", url, nodeIp, udpPort); | ||
} | ||
} | ||
} |
Oops, something went wrong.