@Test public void applyOverrides() throws Exception { final int portOverride = 9999; final String unixSocketFileOverride = "unixSocketFileOverride.ipc"; final String workdirOverride = "workdirOverride"; final List<Peer> overridePeers = Arrays.asList(new Peer("http: final List<Path> privateKeyPaths = Arrays.asList( Files.createTempFile("applyOverrides1", ".txt"), Files.createTempFile("applyOverrides2", ".txt")); final byte[] privateKeyData = FixtureUtil.createLockedPrivateKey().toString().getBytes(); for (Path p : privateKeyPaths) { Files.write(p, privateKeyData); } final List<String> privateKeyPasswords = Arrays.asList("SECRET1", "SECRET2"); final Path privateKeyPasswordFile = Files.createTempFile("applyOverridesPasswords", ".txt"); Files.write(privateKeyPasswordFile, privateKeyPasswords); final LegacyOverridesMixin mixin = new LegacyOverridesMixin(); mixin.url = "http: mixin.port = portOverride; mixin.socket = unixSocketFileOverride; mixin.workdir = workdirOverride; mixin.othernodes = overridePeers.stream().map(Peer::getUrl).collect(toList()); mixin.publickeys = Stream.of("ONE", "TWO").collect(toList()); mixin.storage = "sqlite:somepath"; mixin.tlsservertrust = SslTrustMode.WHITELIST; mixin.tlsclienttrust = SslTrustMode.CA; mixin.tlsservercert = "tlsservercert.cert"; mixin.tlsclientcert = "tlsclientcert.cert"; mixin.tlsserverchain = Stream.of("server1.crt", "server2.crt", "server3.crt").collect(toList()); mixin.tlsclientchain = Stream.of("client1.crt", "client2.crt", "client3.crt").collect(toList()); mixin.tlsserverkey = "tlsserverkey.key"; mixin.tlsclientkey = "tlsclientkey.key"; mixin.privatekeys = privateKeyPaths.stream().map(Path::toString).collect(toList()); mixin.passwords = privateKeyPasswordFile.toString(); mixin.tlsknownclients = "tlsknownclients.file"; mixin.tlsknownservers = "tlsknownservers.file"; mixin.alwayssendto = new ArrayList<>(); instance.setOverrides(mixin); final Config result = instance.applyOverrides(builderWithValidValues, KeyDataBuilder.create()).build(); assertThat(result).isNotNull(); final ServerConfig serverConfig = result.getP2PServerConfig(); final SslConfig sslConfig = serverConfig.getSslConfig(); assertThat(serverConfig.getBindingAddress()).isEqualTo("http: assertThat(serverConfig.getServerAddress()).isEqualTo("http: assertThat(this.getUnixSocketServerAddress(result)) .isEqualTo("unix:" + Paths.get(workdirOverride, unixSocketFileOverride).toAbsolutePath()); assertThat(result.getPeers()).containsExactly(overridePeers.toArray(new Peer[0])); assertThat(result.getKeys().getKeyData()).hasSize(2); assertThat(result.getJdbcConfig()).isNotNull(); assertThat(result.getJdbcConfig().getUrl()).isEqualTo("jdbc:sqlite:somepath"); assertThat(sslConfig.getServerTrustMode()).isEqualTo(SslTrustMode.WHITELIST); assertThat(sslConfig.getClientTrustMode()).isEqualTo(SslTrustMode.CA); assertThat(sslConfig.getClientTlsCertificatePath()).isEqualTo(Paths.get("workdirOverride/tlsclientcert.cert")); assertThat(sslConfig.getServerTlsCertificatePath()).isEqualTo(Paths.get("workdirOverride/tlsservercert.cert")); assertThat(sslConfig.getServerTrustCertificates()) .containsExactly( Paths.get(workdirOverride, "server1.crt"), Paths.get(workdirOverride, "server2.crt"), Paths.get(workdirOverride, "server3.crt")); assertThat(sslConfig.getClientTrustCertificates()) .containsExactly( Paths.get(workdirOverride, "client1.crt"), Paths.get(workdirOverride, "client2.crt"), Paths.get(workdirOverride, "client3.crt")); assertThat(sslConfig.getServerKeyStore()).isEqualTo(Paths.get("workdirOverride/sslServerKeyStorePath")); assertThat(sslConfig.getClientKeyStore()).isEqualTo(Paths.get("workdirOverride/sslClientKeyStorePath")); assertThat(sslConfig.getKnownServersFile()).isEqualTo(Paths.get("workdirOverride/tlsknownservers.file")); assertThat(sslConfig.getKnownClientsFile()).isEqualTo(Paths.get(workdirOverride, "tlsknownclients.file")); }
ConfigBuilder applyOverrides(ConfigBuilder configBuilder, KeyDataBuilder keyDataBuilder) { Optional.ofNullable(overrides.workdir).ifPresent(configBuilder::workdir); Optional.ofNullable(overrides.workdir).ifPresent(keyDataBuilder::withWorkingDirectory); Optional.ofNullable(overrides.url) .map( url -> { try { return new URL(url); } catch (MalformedURLException e) { throw new RuntimeException("Bad server url given: " + e.getMessage()); } }) .map(uri -> uri.getProtocol() + ": .ifPresent(configBuilder::serverHostname); Optional.ofNullable(overrides.port).ifPresent(configBuilder::serverPort); Optional.ofNullable(overrides.socket).ifPresent(configBuilder::unixSocketFile); Optional.ofNullable(overrides.othernodes).ifPresent(configBuilder::peers); Optional.ofNullable(overrides.publickeys).ifPresent(keyDataBuilder::withPublicKeys); Optional.ofNullable(overrides.privatekeys).ifPresent(keyDataBuilder::withPrivateKeys); Optional.ofNullable(overrides.alwayssendto).ifPresent(configBuilder::alwaysSendTo); Optional.ofNullable(overrides.passwords).ifPresent(keyDataBuilder::withPrivateKeyPasswordFile); Optional.ofNullable(overrides.storage) .map(JdbcConfigFactory::fromLegacyStorageString) .ifPresent(configBuilder::jdbcConfig); if (overrides.whitelist) { configBuilder.useWhiteList(true); } Optional.ofNullable(overrides.tls).ifPresent(configBuilder::sslAuthenticationMode); Optional.ofNullable(overrides.tlsservertrust).ifPresent(configBuilder::sslServerTrustMode); Optional.ofNullable(overrides.tlsclienttrust).ifPresent(configBuilder::sslClientTrustMode); Optional.ofNullable(overrides.tlsservercert).ifPresent(configBuilder::sslServerTlsCertificatePath); Optional.ofNullable(overrides.tlsclientcert).ifPresent(configBuilder::sslClientTlsCertificatePath); Optional.ofNullable(overrides.tlsserverchain).ifPresent(configBuilder::sslServerTrustCertificates); Optional.ofNullable(overrides.tlsclientchain).ifPresent(configBuilder::sslClientTrustCertificates); Optional.ofNullable(overrides.tlsserverkey).ifPresent(configBuilder::sslServerTlsKeyPath); Optional.ofNullable(overrides.tlsclientkey).ifPresent(configBuilder::sslClientTlsKeyPath); Optional.ofNullable(overrides.tlsknownservers).ifPresent(configBuilder::sslKnownServersFile); Optional.ofNullable(overrides.tlsknownclients).ifPresent(configBuilder::sslKnownClientsFile); final KeyConfiguration keyConfiguration = keyDataBuilder.build(); if (!keyConfiguration.getKeyData().isEmpty()) { configBuilder.keyData(keyConfiguration); } else if (overrides.passwords != null) { SystemAdapter.INSTANCE .err() .println( "Info: Public/Private key data not provided in overrides. Overriden password file has not been added to config."); } return configBuilder; }
LegacyCliAdapter implements CliAdapter, Callable<CliResult> { ConfigBuilder applyOverrides(ConfigBuilder configBuilder, KeyDataBuilder keyDataBuilder) { Optional.ofNullable(overrides.workdir).ifPresent(configBuilder::workdir); Optional.ofNullable(overrides.workdir).ifPresent(keyDataBuilder::withWorkingDirectory); Optional.ofNullable(overrides.url) .map( url -> { try { return new URL(url); } catch (MalformedURLException e) { throw new RuntimeException("Bad server url given: " + e.getMessage()); } }) .map(uri -> uri.getProtocol() + ": .ifPresent(configBuilder::serverHostname); Optional.ofNullable(overrides.port).ifPresent(configBuilder::serverPort); Optional.ofNullable(overrides.socket).ifPresent(configBuilder::unixSocketFile); Optional.ofNullable(overrides.othernodes).ifPresent(configBuilder::peers); Optional.ofNullable(overrides.publickeys).ifPresent(keyDataBuilder::withPublicKeys); Optional.ofNullable(overrides.privatekeys).ifPresent(keyDataBuilder::withPrivateKeys); Optional.ofNullable(overrides.alwayssendto).ifPresent(configBuilder::alwaysSendTo); Optional.ofNullable(overrides.passwords).ifPresent(keyDataBuilder::withPrivateKeyPasswordFile); Optional.ofNullable(overrides.storage) .map(JdbcConfigFactory::fromLegacyStorageString) .ifPresent(configBuilder::jdbcConfig); if (overrides.whitelist) { configBuilder.useWhiteList(true); } Optional.ofNullable(overrides.tls).ifPresent(configBuilder::sslAuthenticationMode); Optional.ofNullable(overrides.tlsservertrust).ifPresent(configBuilder::sslServerTrustMode); Optional.ofNullable(overrides.tlsclienttrust).ifPresent(configBuilder::sslClientTrustMode); Optional.ofNullable(overrides.tlsservercert).ifPresent(configBuilder::sslServerTlsCertificatePath); Optional.ofNullable(overrides.tlsclientcert).ifPresent(configBuilder::sslClientTlsCertificatePath); Optional.ofNullable(overrides.tlsserverchain).ifPresent(configBuilder::sslServerTrustCertificates); Optional.ofNullable(overrides.tlsclientchain).ifPresent(configBuilder::sslClientTrustCertificates); Optional.ofNullable(overrides.tlsserverkey).ifPresent(configBuilder::sslServerTlsKeyPath); Optional.ofNullable(overrides.tlsclientkey).ifPresent(configBuilder::sslClientTlsKeyPath); Optional.ofNullable(overrides.tlsknownservers).ifPresent(configBuilder::sslKnownServersFile); Optional.ofNullable(overrides.tlsknownclients).ifPresent(configBuilder::sslKnownClientsFile); final KeyConfiguration keyConfiguration = keyDataBuilder.build(); if (!keyConfiguration.getKeyData().isEmpty()) { configBuilder.keyData(keyConfiguration); } else if (overrides.passwords != null) { SystemAdapter.INSTANCE .err() .println( "Info: Public/Private key data not provided in overrides. Overriden password file has not been added to config."); } return configBuilder; } }
LegacyCliAdapter implements CliAdapter, Callable<CliResult> { ConfigBuilder applyOverrides(ConfigBuilder configBuilder, KeyDataBuilder keyDataBuilder) { Optional.ofNullable(overrides.workdir).ifPresent(configBuilder::workdir); Optional.ofNullable(overrides.workdir).ifPresent(keyDataBuilder::withWorkingDirectory); Optional.ofNullable(overrides.url) .map( url -> { try { return new URL(url); } catch (MalformedURLException e) { throw new RuntimeException("Bad server url given: " + e.getMessage()); } }) .map(uri -> uri.getProtocol() + ": .ifPresent(configBuilder::serverHostname); Optional.ofNullable(overrides.port).ifPresent(configBuilder::serverPort); Optional.ofNullable(overrides.socket).ifPresent(configBuilder::unixSocketFile); Optional.ofNullable(overrides.othernodes).ifPresent(configBuilder::peers); Optional.ofNullable(overrides.publickeys).ifPresent(keyDataBuilder::withPublicKeys); Optional.ofNullable(overrides.privatekeys).ifPresent(keyDataBuilder::withPrivateKeys); Optional.ofNullable(overrides.alwayssendto).ifPresent(configBuilder::alwaysSendTo); Optional.ofNullable(overrides.passwords).ifPresent(keyDataBuilder::withPrivateKeyPasswordFile); Optional.ofNullable(overrides.storage) .map(JdbcConfigFactory::fromLegacyStorageString) .ifPresent(configBuilder::jdbcConfig); if (overrides.whitelist) { configBuilder.useWhiteList(true); } Optional.ofNullable(overrides.tls).ifPresent(configBuilder::sslAuthenticationMode); Optional.ofNullable(overrides.tlsservertrust).ifPresent(configBuilder::sslServerTrustMode); Optional.ofNullable(overrides.tlsclienttrust).ifPresent(configBuilder::sslClientTrustMode); Optional.ofNullable(overrides.tlsservercert).ifPresent(configBuilder::sslServerTlsCertificatePath); Optional.ofNullable(overrides.tlsclientcert).ifPresent(configBuilder::sslClientTlsCertificatePath); Optional.ofNullable(overrides.tlsserverchain).ifPresent(configBuilder::sslServerTrustCertificates); Optional.ofNullable(overrides.tlsclientchain).ifPresent(configBuilder::sslClientTrustCertificates); Optional.ofNullable(overrides.tlsserverkey).ifPresent(configBuilder::sslServerTlsKeyPath); Optional.ofNullable(overrides.tlsclientkey).ifPresent(configBuilder::sslClientTlsKeyPath); Optional.ofNullable(overrides.tlsknownservers).ifPresent(configBuilder::sslKnownServersFile); Optional.ofNullable(overrides.tlsknownclients).ifPresent(configBuilder::sslKnownClientsFile); final KeyConfiguration keyConfiguration = keyDataBuilder.build(); if (!keyConfiguration.getKeyData().isEmpty()) { configBuilder.keyData(keyConfiguration); } else if (overrides.passwords != null) { SystemAdapter.INSTANCE .err() .println( "Info: Public/Private key data not provided in overrides. Overriden password file has not been added to config."); } return configBuilder; } LegacyCliAdapter(); }
LegacyCliAdapter implements CliAdapter, Callable<CliResult> { ConfigBuilder applyOverrides(ConfigBuilder configBuilder, KeyDataBuilder keyDataBuilder) { Optional.ofNullable(overrides.workdir).ifPresent(configBuilder::workdir); Optional.ofNullable(overrides.workdir).ifPresent(keyDataBuilder::withWorkingDirectory); Optional.ofNullable(overrides.url) .map( url -> { try { return new URL(url); } catch (MalformedURLException e) { throw new RuntimeException("Bad server url given: " + e.getMessage()); } }) .map(uri -> uri.getProtocol() + ": .ifPresent(configBuilder::serverHostname); Optional.ofNullable(overrides.port).ifPresent(configBuilder::serverPort); Optional.ofNullable(overrides.socket).ifPresent(configBuilder::unixSocketFile); Optional.ofNullable(overrides.othernodes).ifPresent(configBuilder::peers); Optional.ofNullable(overrides.publickeys).ifPresent(keyDataBuilder::withPublicKeys); Optional.ofNullable(overrides.privatekeys).ifPresent(keyDataBuilder::withPrivateKeys); Optional.ofNullable(overrides.alwayssendto).ifPresent(configBuilder::alwaysSendTo); Optional.ofNullable(overrides.passwords).ifPresent(keyDataBuilder::withPrivateKeyPasswordFile); Optional.ofNullable(overrides.storage) .map(JdbcConfigFactory::fromLegacyStorageString) .ifPresent(configBuilder::jdbcConfig); if (overrides.whitelist) { configBuilder.useWhiteList(true); } Optional.ofNullable(overrides.tls).ifPresent(configBuilder::sslAuthenticationMode); Optional.ofNullable(overrides.tlsservertrust).ifPresent(configBuilder::sslServerTrustMode); Optional.ofNullable(overrides.tlsclienttrust).ifPresent(configBuilder::sslClientTrustMode); Optional.ofNullable(overrides.tlsservercert).ifPresent(configBuilder::sslServerTlsCertificatePath); Optional.ofNullable(overrides.tlsclientcert).ifPresent(configBuilder::sslClientTlsCertificatePath); Optional.ofNullable(overrides.tlsserverchain).ifPresent(configBuilder::sslServerTrustCertificates); Optional.ofNullable(overrides.tlsclientchain).ifPresent(configBuilder::sslClientTrustCertificates); Optional.ofNullable(overrides.tlsserverkey).ifPresent(configBuilder::sslServerTlsKeyPath); Optional.ofNullable(overrides.tlsclientkey).ifPresent(configBuilder::sslClientTlsKeyPath); Optional.ofNullable(overrides.tlsknownservers).ifPresent(configBuilder::sslKnownServersFile); Optional.ofNullable(overrides.tlsknownclients).ifPresent(configBuilder::sslKnownClientsFile); final KeyConfiguration keyConfiguration = keyDataBuilder.build(); if (!keyConfiguration.getKeyData().isEmpty()) { configBuilder.keyData(keyConfiguration); } else if (overrides.passwords != null) { SystemAdapter.INSTANCE .err() .println( "Info: Public/Private key data not provided in overrides. Overriden password file has not been added to config."); } return configBuilder; } LegacyCliAdapter(); @Override CliType getType(); @Override CliResult call(); @Override CliResult execute(String... args); void setOverrides(final LegacyOverridesMixin overrides); }
LegacyCliAdapter implements CliAdapter, Callable<CliResult> { ConfigBuilder applyOverrides(ConfigBuilder configBuilder, KeyDataBuilder keyDataBuilder) { Optional.ofNullable(overrides.workdir).ifPresent(configBuilder::workdir); Optional.ofNullable(overrides.workdir).ifPresent(keyDataBuilder::withWorkingDirectory); Optional.ofNullable(overrides.url) .map( url -> { try { return new URL(url); } catch (MalformedURLException e) { throw new RuntimeException("Bad server url given: " + e.getMessage()); } }) .map(uri -> uri.getProtocol() + ": .ifPresent(configBuilder::serverHostname); Optional.ofNullable(overrides.port).ifPresent(configBuilder::serverPort); Optional.ofNullable(overrides.socket).ifPresent(configBuilder::unixSocketFile); Optional.ofNullable(overrides.othernodes).ifPresent(configBuilder::peers); Optional.ofNullable(overrides.publickeys).ifPresent(keyDataBuilder::withPublicKeys); Optional.ofNullable(overrides.privatekeys).ifPresent(keyDataBuilder::withPrivateKeys); Optional.ofNullable(overrides.alwayssendto).ifPresent(configBuilder::alwaysSendTo); Optional.ofNullable(overrides.passwords).ifPresent(keyDataBuilder::withPrivateKeyPasswordFile); Optional.ofNullable(overrides.storage) .map(JdbcConfigFactory::fromLegacyStorageString) .ifPresent(configBuilder::jdbcConfig); if (overrides.whitelist) { configBuilder.useWhiteList(true); } Optional.ofNullable(overrides.tls).ifPresent(configBuilder::sslAuthenticationMode); Optional.ofNullable(overrides.tlsservertrust).ifPresent(configBuilder::sslServerTrustMode); Optional.ofNullable(overrides.tlsclienttrust).ifPresent(configBuilder::sslClientTrustMode); Optional.ofNullable(overrides.tlsservercert).ifPresent(configBuilder::sslServerTlsCertificatePath); Optional.ofNullable(overrides.tlsclientcert).ifPresent(configBuilder::sslClientTlsCertificatePath); Optional.ofNullable(overrides.tlsserverchain).ifPresent(configBuilder::sslServerTrustCertificates); Optional.ofNullable(overrides.tlsclientchain).ifPresent(configBuilder::sslClientTrustCertificates); Optional.ofNullable(overrides.tlsserverkey).ifPresent(configBuilder::sslServerTlsKeyPath); Optional.ofNullable(overrides.tlsclientkey).ifPresent(configBuilder::sslClientTlsKeyPath); Optional.ofNullable(overrides.tlsknownservers).ifPresent(configBuilder::sslKnownServersFile); Optional.ofNullable(overrides.tlsknownclients).ifPresent(configBuilder::sslKnownClientsFile); final KeyConfiguration keyConfiguration = keyDataBuilder.build(); if (!keyConfiguration.getKeyData().isEmpty()) { configBuilder.keyData(keyConfiguration); } else if (overrides.passwords != null) { SystemAdapter.INSTANCE .err() .println( "Info: Public/Private key data not provided in overrides. Overriden password file has not been added to config."); } return configBuilder; } LegacyCliAdapter(); @Override CliType getType(); @Override CliResult call(); @Override CliResult execute(String... args); void setOverrides(final LegacyOverridesMixin overrides);  }