This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 130
Nc 2107 permissioning config toml file #643
Merged
macfarla
merged 51 commits into
PegaSysEng:master
from
macfarla:nc-2107-permissioning-config-toml-file
Jan 29, 2019
Merged
Changes from 36 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
c787dea
typo
macfarla 6ce9630
typos
macfarla a614ffa
typo
macfarla bfdf859
create permissioning config from toml file
macfarla 0239012
merged master
macfarla 58bfa1b
fixed merge issue
macfarla cc38ef6
don't throw
macfarla 260d417
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla 426b644
parse TOML file for permissioning config
macfarla 3aa9f1a
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla c5e5516
typo
macfarla 802279f
typos
macfarla 6c8dcdb
typo
macfarla a5a1dac
create permissioning config from toml file
macfarla 3e77b52
don't throw
macfarla c89768f
parse TOML file for permissioning config
macfarla 430b010
Merge branch 'nc-2107-permissioning-config-toml-file' of github.com:m…
macfarla 3aeb719
added a separate non-CLI toml parser
macfarla 3aad74e
formatting
macfarla 5fafd91
added tests
macfarla 6944ec1
formatting
macfarla fcfca80
avoid 2 x log messages
macfarla 8a6ee0f
ignore tests
macfarla 10d12d0
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla d5b7739
added CLI options for separately enabling node and account permissioning
macfarla 7e2398a
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla 5d9f833
tidying up
macfarla 73c8ed5
handle when default permissions config file does not exist
macfarla b9dce8f
handle when default permissions config file does not exist
macfarla 6e966d8
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla a46fc2f
added tests
macfarla cfb5531
moved check for bootnodes on whitelist out of CLI
macfarla 2867d41
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla 5d7296c
added tests
macfarla 2542d61
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla 39876ed
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla 5980e2a
added test and set to empty list if toml key/value is absent
macfarla d45e2de
PR comments
macfarla 586a820
PR comments
macfarla f29d7da
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla 2395ea7
copied code to convert from Peer to enode format
macfarla 81fb444
missed a
macfarla b08f6ca
Merge branch 'master' into nc-2107-permissioning-config-toml-file
mark-terry fc440e3
Merge branch 'master' of github.com:PegaSysEng/pantheon into nc-2107-…
macfarla dc26bdc
Merge branch 'nc-2107-permissioning-config-toml-file' of github.com:m…
macfarla 9f620ae
Merge branch 'master' into nc-2107-permissioning-config-toml-file
mark-terry 4ab5799
Merge branch 'master' into nc-2107-permissioning-config-toml-file
mark-terry 2a10a44
Merge branch 'master' into nc-2107-permissioning-config-toml-file
macfarla 0ce41e7
Merge branch 'master' into nc-2107-permissioning-config-toml-file
macfarla 2619368
Merge branch 'master' into nc-2107-permissioning-config-toml-file
macfarla 3302150
Merge branch 'master' into nc-2107-permissioning-config-toml-file
macfarla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
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,103 @@ | ||
/* | ||
* 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.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); | ||
} | ||
} | ||
if (permissionedNodeEnabled) { | ||
if (nodeWhitelistTomlArray != null) { | ||
List<URI> nodesWhitelistToml = | ||
nodeWhitelistTomlArray | ||
.toList() | ||
.stream() | ||
.map(Object::toString) | ||
.map(EnodeToURIPropertyConverter::convertToURI) | ||
.collect(Collectors.toList()); | ||
permissioningConfiguration.setNodeWhitelist(nodesWhitelistToml); | ||
} | ||
} | ||
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("Unable to load permissioning config file from location {}.", filename); | ||
return null; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
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,48 @@ | ||
/* | ||
* 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; | ||
result = Toml.parse(toml); | ||
mark-terry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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
52 changes: 52 additions & 0 deletions
52
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,52 @@ | ||
/* | ||
* 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.DefaultPeer; | ||
import tech.pegasys.pantheon.ethereum.p2p.peers.Peer; | ||
import tech.pegasys.pantheon.ethereum.permissioning.PermissioningConfiguration; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class PermissioningConfigurationValidator { | ||
|
||
public static void areAllBootnodesAreInWhitelist( | ||
final EthNetworkConfig ethNetworkConfig, | ||
final PermissioningConfiguration permissioningConfiguration) | ||
throws Exception { | ||
final List<Peer> bootnodesNotInWhitelist = new ArrayList<Peer>(); | ||
final List<Peer> bootnodes = | ||
DiscoveryConfiguration.getBootstrapPeersFromGenericCollection( | ||
ethNetworkConfig.getBootNodes()); | ||
if (permissioningConfiguration.isNodeWhitelistSet() && bootnodes != null) { | ||
final List<Peer> whitelist = | ||
permissioningConfiguration | ||
.getNodeWhitelist() | ||
.stream() | ||
.map(DefaultPeer::fromURI) | ||
.collect(Collectors.toList()); | ||
for (final Peer bootnode : bootnodes) { | ||
mark-terry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!whitelist.contains(bootnode)) { | ||
bootnodesNotInWhitelist.add(bootnode); | ||
} | ||
} | ||
if (!bootnodesNotInWhitelist.isEmpty()) { | ||
throw new Exception("Bootnode(s) not in nodes-whitelist " + bootnodesNotInWhitelist); | ||
mark-terry marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be more explicit that the file doesn't exist at the specified location? I feel like this could be interpreted as "something went wrong", not "it wasn't there."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍