XConfig { @VisibleForTesting static TlsConfig buildTlsConfig(Config config) { TlsConfig.Builder configBuilder = TlsConfig.builderFrom(config); if (!config.hasPath("privateKeyPath") || !config.hasPath("x509CertPath")) { log.info( "Private key path or x509 certificate path not defined. " + "Generating self signed certificate."); SelfSignedCertificate selfSignedCert; try { selfSignedCert = new SelfSignedCertificate("localhost"); } catch (CertificateException ce) { throw new IllegalStateException("Error creating a self-signed cert", ce); } configBuilder.privateKey(selfSignedCert.key()).certificate(selfSignedCert.cert()); } return configBuilder.build(); } XConfig();  XConfig(Config configOverrides); Map<String, List<Double>> getClientRateLimitOverride();  }
@Test void buildTlsConfig_shouldUseCorrectTlsConfigValues() { TlsConfig tlsConfig = XConfig.buildTlsConfig( ConfigFactory.parseResources(XConfig.class, "xrpc.conf").getConfig("tls")); List<String> defaultSupportedProtocols = tlsConfig.getCiphers(); assertEquals(6, defaultSupportedProtocols.size()); for (String protocol : defaultSupportedProtocols) { assertTrue(SUPPORTED_PROTOCOLS.contains(protocol)); } ApplicationProtocolConfig applicationProtocolConfig = tlsConfig.getAlpnConfig(); assertEquals( SUPPORTED_PROTOCOLS_IN_PREFERENCE_ORDER, applicationProtocolConfig.supportedProtocols()); assertEquals(NO_ADVERTISE, applicationProtocolConfig.selectorFailureBehavior()); assertEquals(ACCEPT, applicationProtocolConfig.selectedListenerFailureBehavior()); assertEquals(ALPN, applicationProtocolConfig.protocol()); assertTrue(tlsConfig.isLogInsecureConfig()); assertTrue(tlsConfig.isUseSsl()); assertEquals(ClientAuth.OPTIONAL, tlsConfig.getClientAuth()); assertFalse(tlsConfig.isEnableOcsp()); assertEquals(0, tlsConfig.getSessionTimeout()); assertEquals(0, tlsConfig.getSessionCacheSize()); assertEquals(SslProvider.OPENSSL, tlsConfig.getSslProvider()); }
@Test void buildTlsConfig_shouldUseUserSuppliedCertificateWhenDefined() throws Exception { Config defaultConfig = ConfigFactory.parseResources(XConfigTest.class, "xrpc.conf").getConfig("tls"); Config configWithCertPaths = ConfigFactory.parseMap( ImmutableMap.of( "privateKeyPath", getResourceAbsolutePath("testing-purposes-only-key.pem"), "x509CertPath", getResourceAbsolutePath("testing-purposes-only-certificate.crt"))); TlsConfig tlsConfig = XConfig.buildTlsConfig(configWithCertPaths.withFallback(defaultConfig)); String privateKey = getFileContent("testing-purposes-only-key.pem"); String certificate = getFileContent("testing-purposes-only-certificate.crt"); assertThat(tlsConfig.getPrivateKey(), is(parsePrivateKeyFromPem(privateKey))); assertThat(tlsConfig.getCertificateAndChain().length, is(1)); assertThat(tlsConfig.getCertificateAndChain()[0], is(parseX509CertificateFromPem(certificate))); }