|
| 1 | +package org.testcontainers.mssqlserver; |
| 2 | + |
| 3 | +import org.testcontainers.containers.JdbcDatabaseContainer; |
| 4 | +import org.testcontainers.utility.DockerImageName; |
| 5 | +import org.testcontainers.utility.LicenseAcceptance; |
| 6 | + |
| 7 | +import java.util.Set; |
| 8 | +import java.util.regex.Pattern; |
| 9 | +import java.util.stream.Stream; |
| 10 | + |
| 11 | +/** |
| 12 | + * Testcontainers implementation for Microsoft SQL Server. |
| 13 | + * <p> |
| 14 | + * Supported image: {@code mcr.microsoft.com/mssql/server} |
| 15 | + * <p> |
| 16 | + * Exposed ports: 1433 |
| 17 | + */ |
| 18 | +public class MSSQLServerContainer extends JdbcDatabaseContainer<MSSQLServerContainer> { |
| 19 | + |
| 20 | + private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mcr.microsoft.com/mssql/server"); |
| 21 | + |
| 22 | + public static final String NAME = "sqlserver"; |
| 23 | + |
| 24 | + public static final String IMAGE = DEFAULT_IMAGE_NAME.getUnversionedPart(); |
| 25 | + |
| 26 | + public static final Integer MS_SQL_SERVER_PORT = 1433; |
| 27 | + |
| 28 | + static final String DEFAULT_USER = "sa"; |
| 29 | + |
| 30 | + static final String DEFAULT_PASSWORD = "A_Str0ng_Required_Password"; |
| 31 | + |
| 32 | + private String password = DEFAULT_PASSWORD; |
| 33 | + |
| 34 | + private static final int DEFAULT_STARTUP_TIMEOUT_SECONDS = 240; |
| 35 | + |
| 36 | + private static final int DEFAULT_CONNECT_TIMEOUT_SECONDS = 240; |
| 37 | + |
| 38 | + private static final Pattern[] PASSWORD_CATEGORY_VALIDATION_PATTERNS = new Pattern[] { |
| 39 | + Pattern.compile("[A-Z]+"), |
| 40 | + Pattern.compile("[a-z]+"), |
| 41 | + Pattern.compile("[0-9]+"), |
| 42 | + Pattern.compile("[^a-zA-Z0-9]+", Pattern.CASE_INSENSITIVE), |
| 43 | + }; |
| 44 | + |
| 45 | + public MSSQLServerContainer(final String dockerImageName) { |
| 46 | + this(DockerImageName.parse(dockerImageName)); |
| 47 | + } |
| 48 | + |
| 49 | + public MSSQLServerContainer(final DockerImageName dockerImageName) { |
| 50 | + super(dockerImageName); |
| 51 | + dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME); |
| 52 | + |
| 53 | + withStartupTimeoutSeconds(DEFAULT_STARTUP_TIMEOUT_SECONDS); |
| 54 | + withConnectTimeoutSeconds(DEFAULT_CONNECT_TIMEOUT_SECONDS); |
| 55 | + addExposedPort(MS_SQL_SERVER_PORT); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public Set<Integer> getLivenessCheckPortNumbers() { |
| 60 | + return super.getLivenessCheckPortNumbers(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void configure() { |
| 65 | + // If license was not accepted programmatically, check if it was accepted via resource file |
| 66 | + if (!getEnvMap().containsKey("ACCEPT_EULA")) { |
| 67 | + LicenseAcceptance.assertLicenseAccepted(this.getDockerImageName()); |
| 68 | + acceptLicense(); |
| 69 | + } |
| 70 | + |
| 71 | + addEnv("MSSQL_SA_PASSWORD", password); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Accepts the license for the SQLServer container by setting the ACCEPT_EULA=Y |
| 76 | + * variable as described at <a href="https://hub.docker.com/_/microsoft-mssql-server">https://hub.docker.com/_/microsoft-mssql-server</a> |
| 77 | + */ |
| 78 | + public MSSQLServerContainer acceptLicense() { |
| 79 | + addEnv("ACCEPT_EULA", "Y"); |
| 80 | + return self(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String getDriverClassName() { |
| 85 | + return "com.microsoft.sqlserver.jdbc.SQLServerDriver"; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected String constructUrlForConnection(String queryString) { |
| 90 | + // The JDBC driver of MS SQL Server enables encryption by default for versions > 10.1.0. |
| 91 | + // We need to disable it by default to be able to use the container without having to pass extra params. |
| 92 | + // See https://github.com/microsoft/mssql-jdbc/releases/tag/v10.1.0 |
| 93 | + if (urlParameters.keySet().stream().map(String::toLowerCase).noneMatch("encrypt"::equals)) { |
| 94 | + urlParameters.put("encrypt", "false"); |
| 95 | + } |
| 96 | + return super.constructUrlForConnection(queryString); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String getJdbcUrl() { |
| 101 | + String additionalUrlParams = constructUrlParameters(";", ";"); |
| 102 | + return "jdbc:sqlserver://" + getHost() + ":" + getMappedPort(MS_SQL_SERVER_PORT) + additionalUrlParams; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String getUsername() { |
| 107 | + return DEFAULT_USER; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getPassword() { |
| 112 | + return password; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String getTestQueryString() { |
| 117 | + return "SELECT 1"; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public MSSQLServerContainer withPassword(final String password) { |
| 122 | + checkPasswordStrength(password); |
| 123 | + this.password = password; |
| 124 | + return self(); |
| 125 | + } |
| 126 | + |
| 127 | + private void checkPasswordStrength(String password) { |
| 128 | + if (password == null) { |
| 129 | + throw new IllegalArgumentException("Null password is not allowed"); |
| 130 | + } |
| 131 | + |
| 132 | + if (password.length() < 8) { |
| 133 | + throw new IllegalArgumentException("Password should be at least 8 characters long"); |
| 134 | + } |
| 135 | + |
| 136 | + if (password.length() > 128) { |
| 137 | + throw new IllegalArgumentException("Password can be up to 128 characters long"); |
| 138 | + } |
| 139 | + |
| 140 | + long satisfiedCategories = Stream |
| 141 | + .of(PASSWORD_CATEGORY_VALIDATION_PATTERNS) |
| 142 | + .filter(p -> p.matcher(password).find()) |
| 143 | + .count(); |
| 144 | + |
| 145 | + if (satisfiedCategories < 3) { |
| 146 | + throw new IllegalArgumentException( |
| 147 | + "Password must contain characters from three of the following four categories:\n" + |
| 148 | + " - Latin uppercase letters (A through Z)\n" + |
| 149 | + " - Latin lowercase letters (a through z)\n" + |
| 150 | + " - Base 10 digits (0 through 9)\n" + |
| 151 | + " - Non-alphanumeric characters such as: exclamation point (!), dollar sign ($), number sign (#), " + |
| 152 | + "or percent (%)." |
| 153 | + ); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments