use of org.apache.tuweni.toml.TomlParseResult in project couchbase-elasticsearch-connector by couchbase.
the class ConfigHelper method readPassword.
public static String readPassword(ConfigTable parent, String parentName, String keyName) {
final String pathToPassword = parent.getString(keyName).orElseThrow(() -> new ConfigException(parentName + "." + keyName + " must not be null"));
final File passwordFile = resolveIfRelative(pathToPassword);
try (InputStream is = new FileInputStream(passwordFile)) {
final TomlParseResult config = Toml.parse(resolveVariables(is));
if (config.hasErrors()) {
// DO NOT REPORT ERRORS, AS THAT MIGHT LEAK THE CONTENTS OF THE FILE TO AN ATTACKER
// trying to use the connector's elevated privileges to read a completely
// unrelated file.
}
final String password = config.getString("password");
if (password == null) {
throw new ConfigException("Failed to parse " + passwordFile + " : Expected a TOML file with contents like: password = 'swordfish'");
}
return password;
} catch (FileNotFoundException e) {
throw new ConfigException("Error reading config at " + parent.inputPositionOf(keyName) + "; File not found: " + passwordFile);
} catch (IOException e) {
LOGGER.error("Failed to read password from file {}", passwordFile, e);
throw new ConfigException(e.getMessage());
}
}
use of org.apache.tuweni.toml.TomlParseResult in project signers by ConsenSys.
the class SigningMetadataTomlConfigLoader method getMetadataInfo.
private Optional<SigningMetadataFile> getMetadataInfo(final Path file) {
final String filename = file.getFileName().toString();
try {
final TomlParseResult result = TomlConfigFileParser.loadConfigurationFromFile(file.toAbsolutePath().toString());
final Optional<TomlTableAdapter> signingTable = getSigningTableFrom(file.getFileName().toString(), result);
if (signingTable.isEmpty()) {
return Optional.empty();
}
final String type = signingTable.get().getString("type");
if (SignerType.fromString(type).equals(SignerType.FILE_BASED_SIGNER)) {
return getFileBasedSigningMetadataFromToml(filename, result);
} else if (SignerType.fromString(type).equals(SignerType.AZURE_SIGNER)) {
return getAzureBasedSigningMetadataFromToml(file.getFileName().toString(), result);
} else if (SignerType.fromString(type).equals(SignerType.HASHICORP_SIGNER)) {
return getHashicorpMetadataFromToml(file, result);
} else if (SignerType.fromString(type).equals(SignerType.RAW_SIGNER)) {
return getRawMetadataFromToml(filename, result);
} else {
LOG.error("Unknown signing type in metadata: " + type);
return Optional.empty();
}
} catch (final IllegalArgumentException | TomlInvalidTypeException e) {
final String errorMsg = String.format("%s failed to decode: %s", filename, e.getMessage());
LOG.error(errorMsg);
return Optional.empty();
} catch (final Exception e) {
LOG.error("Could not load TOML file " + file, e);
return Optional.empty();
}
}
use of org.apache.tuweni.toml.TomlParseResult in project besu by hyperledger.
the class RpcAuthFileValidator method validate.
public static String validate(final CommandLine commandLine, final String filename, final String type) {
final File authfile = new File(filename);
if (!authfile.exists()) {
throw new ParameterException(commandLine, "The specified RPC " + type + " authentication credential file '" + filename + "' does not exist");
}
final TomlParseResult tomlParseResult;
try {
tomlParseResult = TomlConfigFileParser.loadConfigurationFromFile(filename);
} catch (IOException e) {
throw new ParameterException(commandLine, "An error occurred while opening the specified RPC " + type + " authentication configuration file.");
} catch (Exception e) {
throw new ParameterException(commandLine, "Invalid RPC " + type + " authentication credentials file: " + e.getMessage());
}
if (tomlParseResult.hasErrors()) {
throw new ParameterException(commandLine, "An error occurred while parsing the specified RPC authentication configuration file.");
}
if (!verifyAllUsersHavePassword(tomlParseResult)) {
throw new ParameterException(commandLine, "RPC user specified without password.");
}
if (!verifyAllEntriesHaveValues(tomlParseResult)) {
throw new ParameterException(commandLine, "RPC authentication configuration file contains invalid values.");
}
return filename;
}
use of org.apache.tuweni.toml.TomlParseResult in project besu by hyperledger.
the class BesuCommandTest method tomlThatConfiguresEverythingExceptPermissioningToml.
@Test
public void tomlThatConfiguresEverythingExceptPermissioningToml() throws IOException {
// Load a TOML that configures literally everything (except permissioning TOML config)
final URL configFile = this.getClass().getResource("/everything_config.toml");
final Path toml = createTempFile("toml", Resources.toByteArray(configFile));
// Parse it.
final CommandLine.Model.CommandSpec spec = parseCommand("--config-file", toml.toString()).spec;
final TomlParseResult tomlResult = Toml.parse(toml);
// Verify we configured everything
final HashSet<CommandLine.Model.OptionSpec> options = new HashSet<>(spec.options());
// Except for meta-options
options.remove(spec.optionsMap().get("--config-file"));
options.remove(spec.optionsMap().get("--help"));
options.remove(spec.optionsMap().get("--version"));
for (final String tomlKey : tomlResult.keySet()) {
final CommandLine.Model.OptionSpec optionSpec = spec.optionsMap().get("--" + tomlKey);
assertThat(optionSpec).describedAs("Option '%s' should be a configurable option.", tomlKey).isNotNull();
// Verify TOML stores it by the appropriate type
if (optionSpec.type().equals(Boolean.class)) {
tomlResult.getBoolean(tomlKey);
} else if (optionSpec.isMultiValue() || optionSpec.arity().max > 1) {
tomlResult.getArray(tomlKey);
} else if (optionSpec.type().equals(Double.class)) {
tomlResult.getDouble(tomlKey);
} else if (Number.class.isAssignableFrom(optionSpec.type())) {
tomlResult.getLong(tomlKey);
} else if (Wei.class.isAssignableFrom(optionSpec.type())) {
tomlResult.getLong(tomlKey);
} else if (Fraction.class.isAssignableFrom(optionSpec.type())) {
tomlResult.getDouble(tomlKey);
} else if (Percentage.class.isAssignableFrom(optionSpec.type())) {
tomlResult.getLong(tomlKey);
} else {
tomlResult.getString(tomlKey);
}
options.remove(optionSpec);
}
assertThat(options.stream().filter(optionSpec -> !optionSpec.hidden()).map(CommandLine.Model.OptionSpec::longestName)).isEmpty();
}
use of org.apache.tuweni.toml.TomlParseResult in project besu by hyperledger.
the class TomlAuth method authenticate.
@Override
public void authenticate(final JsonObject authInfo, final Handler<AsyncResult<User>> resultHandler) {
final String username = authInfo.getString("username");
if (username == null) {
resultHandler.handle(Future.failedFuture("No username provided"));
return;
}
final String password = authInfo.getString("password");
if (password == null) {
resultHandler.handle(Future.failedFuture("No password provided"));
return;
}
vertx.executeBlocking(f -> {
TomlParseResult parseResult;
try {
parseResult = Toml.parse(options.getTomlPath());
} catch (IOException e) {
f.fail(e);
return;
}
final TomlTable userData = parseResult.getTableOrEmpty("Users." + username);
if (userData.isEmpty()) {
f.fail("User not found");
return;
}
final TomlUser tomlUser = readTomlUserFromTable(username, userData);
if ("".equals(tomlUser.getPassword())) {
f.fail("No password set for user");
return;
}
checkPasswordHash(password, tomlUser.getPassword(), rs -> {
if (rs.succeeded()) {
f.complete(tomlUser);
} else {
f.fail(rs.cause());
}
});
}, false, res -> {
if (res.succeeded()) {
resultHandler.handle(Future.succeededFuture((User) res.result()));
} else {
resultHandler.handle(Future.failedFuture(res.cause()));
}
});
}
Aggregations