TrustedCertEntityBuilder implements EntityBuilder { @Override public List<Entity> build(Bundle bundle, BundleType bundleType, Document document) { if (bundle instanceof AnnotatedBundle) { Map<String, TrustedCert> trustedCertMap = Optional.ofNullable(bundle.getTrustedCerts()).orElse(Collections.emptyMap()); return buildEntities(trustedCertMap, ((AnnotatedBundle)bundle).getFullBundle(), bundleType, document); } else { return buildEntities(bundle.getTrustedCerts(), bundle, bundleType, document); } } @Inject  TrustedCertEntityBuilder(IdGenerator idGenerator, SSLSocketFactory acceptAllSocketFactory, CertificateFactory certFactory); @Override List<Entity> build(Bundle bundle, BundleType bundleType, Document document); @Override @NotNull Integer getOrder();  }
@Test void buildNoTrustedCerts() { final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), createAcceptAllSocketFactory(), certFact); final Bundle bundle = new Bundle(); final List<Entity> trustedCertEntities = builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument()); assertEquals(0, trustedCertEntities.size()); }
@Test void buildTrustedCertFromNonExistentFile() { final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), null, certFact); final Bundle bundle = new Bundle(); final TrustedCert trustedCert = new TrustedCert(ImmutableMap.of(VERIFY_HOSTNAME, true), null); bundle.putAllTrustedCerts(ImmutableMap.of(CERT_NAME, trustedCert)); assertThrows(EntityBuilderException.class, () -> builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument())); }
@Test void buildTrustedCertFromUrl() throws Exception { SSLSocketFactory sf = mock(SSLSocketFactory.class); SSLSocket socket = mock(SSLSocket.class); SSLSession sslSession = mock(SSLSession.class); when(sf.createSocket(anyString(), anyInt())).thenReturn(socket); when(socket.getSession()).thenReturn(sslSession); when(sslSession.getPeerCertificates()).thenReturn(new X509Certificate[]{testCert}); final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), sf, certFact); final Bundle bundle = new Bundle(); final TrustedCert trustedCert = new TrustedCert(ImmutableMap.of(VERIFY_HOSTNAME, true), null); bundle.putAllTrustedCerts(ImmutableMap.of(URL_NAME, trustedCert)); final List<Entity> trustedCerts = builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument()); assertEquals(1, trustedCerts.size()); final Element trustedCertEntityXml = verifyTrustedCertElement(trustedCerts, true); verifyProperties(trustedCertEntityXml); verifyCertDetails(trustedCertEntityXml); }
@Test void tryBuildTrustedCertFromUrlReturningEmpty() throws Exception { SSLSocketFactory sf = mock(SSLSocketFactory.class); SSLSocket socket = mock(SSLSocket.class); SSLSession sslSession = mock(SSLSession.class); when(sf.createSocket(anyString(), anyInt())).thenReturn(socket); when(socket.getSession()).thenReturn(sslSession); when(sslSession.getPeerCertificates()).thenReturn(new X509Certificate[]{}); final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), sf, certFact); final Bundle bundle = new Bundle(); final TrustedCert trustedCert = new TrustedCert(ImmutableMap.of(VERIFY_HOSTNAME, true), null); bundle.putAllTrustedCerts(ImmutableMap.of(URL_NAME, trustedCert)); assertThrows(EntityBuilderException.class, () -> builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument())); }
@Test void tryBuildTrustedCertFromUrlInvalidCertType() throws Exception { SSLSocketFactory sf = mock(SSLSocketFactory.class); SSLSocket socket = mock(SSLSocket.class); SSLSession sslSession = mock(SSLSession.class); when(sf.createSocket(anyString(), anyInt())).thenReturn(socket); when(socket.getSession()).thenReturn(sslSession); when(sslSession.getPeerCertificates()).thenReturn(new Certificate[]{ new Certificate("MockCertificate") { @Override public byte[] getEncoded() { return new byte[0]; } @Override public void verify(PublicKey key) { } @Override public void verify(PublicKey key, String sigProvider) { } @Override public String toString() { return null; } @Override public PublicKey getPublicKey() { return null; } }}); final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), sf, certFact); final Bundle bundle = new Bundle(); final TrustedCert trustedCert = new TrustedCert(ImmutableMap.of(VERIFY_HOSTNAME, true), null); bundle.putAllTrustedCerts(ImmutableMap.of(URL_NAME, trustedCert)); assertThrows(EntityBuilderException.class, () -> builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument())); }
@Test void tryBuildTrustedCertFromUrlThrowingException() throws Exception { SSLSocketFactory sf = mock(SSLSocketFactory.class); SSLSocket socket = mock(SSLSocket.class); SSLSession sslSession = mock(SSLSession.class); when(sf.createSocket(anyString(), anyInt())).thenReturn(socket); when(socket.getSession()).thenReturn(sslSession); doThrow(IOException.class).when(socket).startHandshake(); final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), sf, certFact); final Bundle bundle = new Bundle(); final TrustedCert trustedCert = new TrustedCert(ImmutableMap.of(VERIFY_HOSTNAME, true), null); bundle.putAllTrustedCerts(ImmutableMap.of(URL_NAME, trustedCert)); assertThrows(EntityBuilderException.class, () -> builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument())); }
@Test void buildTrustedCertUsingPem() throws Exception { final File trustedCertLocation = new File(getClass().getClassLoader().getResource(CERT_NAME + ".pem").getFile()); final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), null, certFact); final Bundle bundle = new Bundle(); final TrustedCert trustedCert = new TrustedCert(ImmutableMap.of(VERIFY_HOSTNAME, true), null); bundle.putAllTrustedCerts(ImmutableMap.of(CERT_NAME, trustedCert)); bundle.putAllCertificateFiles(ImmutableMap.of(CERT_NAME, () -> new FileInputStream(trustedCertLocation))); final List<Entity> trustedCerts = builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument()); assertEquals(1, trustedCerts.size()); final Element trustedCertEntityXml = verifyTrustedCertElement(trustedCerts, false); verifyProperties(trustedCertEntityXml); verifyCertDetails(trustedCertEntityXml); }
@Test void buildTrustedCertUsingDer() throws Exception { final File trustedCertLocation = new File(getClass().getClassLoader().getResource(CERT_NAME + ".der").getFile()); final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), null, certFact); final Bundle bundle = new Bundle(); final TrustedCert trustedCert = new TrustedCert(ImmutableMap.of(VERIFY_HOSTNAME, true), null); bundle.putAllTrustedCerts(ImmutableMap.of(CERT_NAME, trustedCert)); bundle.putAllCertificateFiles(ImmutableMap.of(CERT_NAME, () -> new FileInputStream(trustedCertLocation))); final List<Entity> trustedCerts = builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument()); assertEquals(1, trustedCerts.size()); final Element trustedCertEntityXml = verifyTrustedCertElement(trustedCerts, false); verifyProperties(trustedCertEntityXml); verifyCertDetails(trustedCertEntityXml); }
@Test void buildTrustedCertUsingCertData() throws Exception { final String EXPECT_ISSUER = "issuer"; final BigInteger EXPECT_BIG_INT = new BigInteger("1234"); final String EXPECT_SUB_NAME = "subName"; final String EXPECT_DATA = "data"; final TrustedCertEntityBuilder builder = new TrustedCertEntityBuilder(new IdGenerator(), null, certFact); final Bundle bundle = new Bundle(); final CertificateData certData = new CertificateData(EXPECT_ISSUER, EXPECT_BIG_INT, EXPECT_SUB_NAME, EXPECT_DATA); final TrustedCert trustedCert = new TrustedCert(ImmutableMap.of(VERIFY_HOSTNAME, true), certData); bundle.putAllTrustedCerts(ImmutableMap.of(CERT_NAME, trustedCert)); final List<Entity> trustedCerts = builder.build(bundle, EntityBuilder.BundleType.ENVIRONMENT, DocumentTools.INSTANCE.getDocumentBuilder().newDocument()); assertEquals(1, trustedCerts.size()); final Element trustedCertEntityXml = verifyTrustedCertElement(trustedCerts, false); verifyProperties(trustedCertEntityXml); final Element certDataXml = getSingleElement(trustedCertEntityXml, CERT_DATA); assertEquals(EXPECT_ISSUER, getSingleChildElementTextContent(certDataXml, ISSUER_NAME)); assertEquals(EXPECT_BIG_INT, new BigInteger(getSingleChildElementTextContent(certDataXml, SERIAL_NUMBER))); assertEquals(EXPECT_SUB_NAME, getSingleChildElementTextContent(certDataXml, SUBJECT_NAME)); assertEquals(EXPECT_DATA, getSingleChildElementTextContent(certDataXml, ENCODED)); }