|
| 1 | +/* |
| 2 | + * TLS-Scanner - A TLS configuration and analysis tool based on TLS-Attacker |
| 3 | + * |
| 4 | + * Copyright 2017-2023 Ruhr University Bochum, Paderborn University, Technology Innovation Institute, and Hackmanit GmbH |
| 5 | + * |
| 6 | + * Licensed under Apache License, Version 2.0 |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0.txt |
| 8 | + */ |
| 9 | +package de.rub.nds.tlsscanner.serverscanner.probe; |
| 10 | + |
| 11 | +import static org.junit.Assume.assumeNotNull; |
| 12 | + |
| 13 | +import com.github.dockerjava.api.exception.DockerException; |
| 14 | +import com.github.dockerjava.api.model.Image; |
| 15 | +import de.rub.nds.scanner.core.constants.TestResult; |
| 16 | +import de.rub.nds.tls.subject.ConnectionRole; |
| 17 | +import de.rub.nds.tls.subject.TlsImplementationType; |
| 18 | +import de.rub.nds.tls.subject.constants.TransportType; |
| 19 | +import de.rub.nds.tls.subject.docker.DockerClientManager; |
| 20 | +import de.rub.nds.tls.subject.docker.DockerTlsInstance; |
| 21 | +import de.rub.nds.tls.subject.docker.DockerTlsManagerFactory; |
| 22 | +import de.rub.nds.tls.subject.docker.DockerTlsServerInstance; |
| 23 | +import de.rub.nds.tlsattacker.core.config.delegate.GeneralDelegate; |
| 24 | +import de.rub.nds.tlsattacker.core.workflow.ParallelExecutor; |
| 25 | +import de.rub.nds.tlsscanner.core.constants.TlsAnalyzedProperty; |
| 26 | +import de.rub.nds.tlsscanner.serverscanner.config.ServerScannerConfig; |
| 27 | +import de.rub.nds.tlsscanner.serverscanner.report.ServerReport; |
| 28 | +import de.rub.nds.tlsscanner.serverscanner.selector.ConfigSelector; |
| 29 | +import java.security.Security; |
| 30 | +import java.util.List; |
| 31 | +import java.util.UUID; |
| 32 | +import org.apache.logging.log4j.LogManager; |
| 33 | +import org.apache.logging.log4j.Logger; |
| 34 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 35 | +import org.junit.Assume; |
| 36 | +import org.junit.jupiter.api.AfterEach; |
| 37 | +import org.junit.jupiter.api.BeforeAll; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.api.TestInstance; |
| 41 | + |
| 42 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 43 | +public abstract class AbstractProbeIT { |
| 44 | + |
| 45 | + private static final Logger LOGGER = LogManager.getLogger(); |
| 46 | + private static final int MAX_ATTEMPTS = 3; |
| 47 | + private static List<Image> localImages; |
| 48 | + |
| 49 | + private final TlsImplementationType implementation; |
| 50 | + private final String version; |
| 51 | + private final String additionalParameters; |
| 52 | + private final TransportType transportType; |
| 53 | + |
| 54 | + private DockerTlsInstance dockerInstance; |
| 55 | + protected ServerReport report; |
| 56 | + protected ParallelExecutor parallelExecutor; |
| 57 | + protected ConfigSelector configSelector; |
| 58 | + private ServerScannerConfig config; |
| 59 | + |
| 60 | + public AbstractProbeIT( |
| 61 | + TlsImplementationType implementation, String version, String additionalParameters) { |
| 62 | + this.implementation = implementation; |
| 63 | + this.version = version; |
| 64 | + this.additionalParameters = additionalParameters; |
| 65 | + this.transportType = TransportType.TCP; |
| 66 | + } |
| 67 | + |
| 68 | + public AbstractProbeIT( |
| 69 | + TlsImplementationType implementation, |
| 70 | + String version, |
| 71 | + String additionalParameters, |
| 72 | + TransportType transportType) { |
| 73 | + this.implementation = implementation; |
| 74 | + this.version = version; |
| 75 | + this.additionalParameters = additionalParameters; |
| 76 | + this.transportType = transportType; |
| 77 | + } |
| 78 | + |
| 79 | + @BeforeAll |
| 80 | + public void loadList() { |
| 81 | + try { |
| 82 | + DockerClientManager.getDockerClient().listContainersCmd().exec(); |
| 83 | + } catch (Exception ex) { |
| 84 | + Assume.assumeNoException(ex); |
| 85 | + } |
| 86 | + localImages = DockerTlsManagerFactory.getAllImages(); |
| 87 | + } |
| 88 | + |
| 89 | + @BeforeEach |
| 90 | + public void setUp() throws InterruptedException { |
| 91 | + Security.addProvider(new BouncyCastleProvider()); |
| 92 | + DockerClientManager.setDockerServerUsername(System.getenv("DOCKER_USERNAME")); |
| 93 | + DockerClientManager.setDockerServerPassword(System.getenv("DOCKER_PASSWORD")); |
| 94 | + prepareContainer(); |
| 95 | + } |
| 96 | + |
| 97 | + private void prepareContainer() throws DockerException, InterruptedException { |
| 98 | + Image image = |
| 99 | + DockerTlsManagerFactory.getMatchingImage( |
| 100 | + localImages, implementation, version, ConnectionRole.SERVER); |
| 101 | + getDockerInstance(image); |
| 102 | + } |
| 103 | + |
| 104 | + private void getDockerInstance(Image image) throws DockerException, InterruptedException { |
| 105 | + DockerTlsManagerFactory.TlsServerInstanceBuilder serverInstanceBuilder; |
| 106 | + if (image != null) { |
| 107 | + serverInstanceBuilder = |
| 108 | + new DockerTlsManagerFactory.TlsServerInstanceBuilder(image, transportType); |
| 109 | + } else { |
| 110 | + serverInstanceBuilder = |
| 111 | + new DockerTlsManagerFactory.TlsServerInstanceBuilder( |
| 112 | + implementation, version, transportType) |
| 113 | + .pull(); |
| 114 | + localImages = DockerTlsManagerFactory.getAllImages(); |
| 115 | + assumeNotNull( |
| 116 | + image, |
| 117 | + String.format( |
| 118 | + "TLS implementation %s %s not available", |
| 119 | + implementation.name(), version)); |
| 120 | + } |
| 121 | + serverInstanceBuilder |
| 122 | + .containerName("server-scanner-test-server-" + UUID.randomUUID()) |
| 123 | + .additionalParameters(additionalParameters); |
| 124 | + dockerInstance = serverInstanceBuilder.build(); |
| 125 | + dockerInstance.start(); |
| 126 | + } |
| 127 | + |
| 128 | + @AfterEach |
| 129 | + public void tearDown() { |
| 130 | + killContainer(); |
| 131 | + } |
| 132 | + |
| 133 | + private void killContainer() { |
| 134 | + if (dockerInstance != null && dockerInstance.getId() != null) { |
| 135 | + dockerInstance.kill(); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testProbe() throws InterruptedException { |
| 141 | + LOGGER.info("Testing: " + getProbe().getProbeName()); |
| 142 | + for (int i = 0; i < MAX_ATTEMPTS; i++) { |
| 143 | + try { |
| 144 | + executeProbe(); |
| 145 | + } catch (Exception ignored) { |
| 146 | + LOGGER.info( |
| 147 | + "Encountered exception during scanner execution (" |
| 148 | + + ignored.getMessage() |
| 149 | + + ")"); |
| 150 | + } |
| 151 | + if (!executedAsPlanned()) { |
| 152 | + LOGGER.info("Failed to complete scan, reexecuting..."); |
| 153 | + killContainer(); |
| 154 | + prepareContainer(); |
| 155 | + } else { |
| 156 | + return; |
| 157 | + } |
| 158 | + } |
| 159 | + LOGGER.error("Failed"); |
| 160 | + } |
| 161 | + |
| 162 | + private void executeProbe() { |
| 163 | + // Preparing config, executor, config selector, and report |
| 164 | + config = new ServerScannerConfig(new GeneralDelegate()); |
| 165 | + config.getClientDelegate() |
| 166 | + .setHost("localhost:" + ((DockerTlsServerInstance) dockerInstance).getPort()); |
| 167 | + parallelExecutor = new ParallelExecutor(1, 3); |
| 168 | + configSelector = new ConfigSelector(config, parallelExecutor); |
| 169 | + configSelector.findWorkingConfigs(); |
| 170 | + report = new ServerReport(); |
| 171 | + prepareReport(); |
| 172 | + // Executing probe |
| 173 | + TlsServerProbe probe = getProbe(); |
| 174 | + probe.adjustConfig(report); |
| 175 | + probe.executeTest(); |
| 176 | + probe.merge(report); |
| 177 | + } |
| 178 | + |
| 179 | + protected abstract TlsServerProbe getProbe(); |
| 180 | + |
| 181 | + protected abstract void prepareReport(); |
| 182 | + |
| 183 | + protected abstract boolean executedAsPlanned(); |
| 184 | + |
| 185 | + protected boolean verifyProperty(TlsAnalyzedProperty property, TestResult result) { |
| 186 | + return report.getResult(property) == result; |
| 187 | + } |
| 188 | +} |
0 commit comments