Runway 1.1.0
This commit is contained in:
1 parent
ce217fc952
commit
07d5bd7e9a
223 files changed
+21147
-255
No files matched your search
+79
@@ -0,0 +1,79 @@
|
||||
package mx.quest.database.postgresql;
|
||||
|
||||
import java.util.Objects;
|
||||
import javax.sql.DataSource;
|
||||
import mx.quest.database.spi.ConnectionSettings;
|
||||
import mx.quest.database.spi.DatabaseProvider;
|
||||
import org.postgresql.ds.PGSimpleDataSource;
|
||||
import org.postgresql.util.DriverInfo;
|
||||
|
||||
/**
|
||||
* PostgreSQL through pgjdbc. Builds an unpooled {@link PGSimpleDataSource} from the database plugin's checked
|
||||
* settings: one host, the account as properties, TLS exactly as configured, and nothing taken from free text. The
|
||||
* database plugin pools it and runs every statement.
|
||||
*
|
||||
* <p>TLS:
|
||||
* <ul>
|
||||
* <li>{@code DISABLE}: {@code sslmode=disable}.</li>
|
||||
* <li>{@code REQUIRE}: {@code sslmode=require} with the non-validating factory: encrypted, the server not
|
||||
* checked, no client certificate (the factory initialises its TLS context with no key manager).</li>
|
||||
* <li>{@code VERIFY_FULL}: {@code sslmode=verify-full}, checking the chain and the host name, against the
|
||||
* configured CA file when there is one ({@code sslrootcert}) and the JVM's trust store otherwise.</li>
|
||||
* </ul>
|
||||
*/
|
||||
public final class PostgresProvider implements DatabaseProvider {
|
||||
|
||||
static final int DEFAULT_PORT = 5432;
|
||||
static final String NON_VALIDATING = "org.postgresql.ssl.NonValidatingFactory";
|
||||
static final String JVM_TRUST = "org.postgresql.ssl.DefaultJavaSSLFactory";
|
||||
static final String CA_FILE = "org.postgresql.ssl.LibPQFactory";
|
||||
|
||||
@Override
|
||||
public String id() {
|
||||
return "postgresql";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String driverVersion() {
|
||||
return "pgjdbc " + DriverInfo.DRIVER_VERSION;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataSource dataSource(ConnectionSettings settings) {
|
||||
Objects.requireNonNull(settings, "settings");
|
||||
PGSimpleDataSource ds = new PGSimpleDataSource();
|
||||
String host = settings.host().indexOf(':') >= 0 ? "[" + settings.host() + "]" : settings.host();
|
||||
ds.setServerNames(new String[] {host});
|
||||
ds.setPortNumbers(new int[] {settings.portOr(DEFAULT_PORT)});
|
||||
ds.setDatabaseName(settings.database());
|
||||
ds.setUser(settings.username());
|
||||
ds.setPassword(settings.password());
|
||||
ds.setApplicationName("runway");
|
||||
ds.setConnectTimeout(seconds(settings.connectTimeout().toSeconds()));
|
||||
ds.setLoginTimeout(seconds(settings.connectTimeout().toSeconds()));
|
||||
ds.setSocketTimeout(seconds(settings.socketTimeout().toSeconds()));
|
||||
ds.setTcpKeepAlive(true);
|
||||
switch (settings.tls()) {
|
||||
case DISABLE -> ds.setSslMode("disable");
|
||||
case REQUIRE -> {
|
||||
ds.setSslMode("require");
|
||||
ds.setSslfactory(NON_VALIDATING);
|
||||
}
|
||||
case VERIFY_FULL -> {
|
||||
ds.setSslMode("verify-full");
|
||||
if (settings.caCertificate() != null) {
|
||||
ds.setSslfactory(CA_FILE);
|
||||
ds.setSslRootCert(settings.caCertificate().toAbsolutePath().toString());
|
||||
} else {
|
||||
ds.setSslfactory(JVM_TRUST);
|
||||
}
|
||||
}
|
||||
default -> throw new IllegalArgumentException("unknown TLS policy " + settings.tls());
|
||||
}
|
||||
return ds;
|
||||
}
|
||||
|
||||
private static int seconds(long value) {
|
||||
return (int) Math.max(1, Math.min(Integer.MAX_VALUE, value));
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package mx.quest.database.postgresql;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/** Registers the PostgreSQL provider; the database plugin collects it by type. */
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class PostgresProviderConfig {
|
||||
|
||||
@Bean
|
||||
public PostgresProvider databasePostgresqlProvider() {
|
||||
return new PostgresProvider();
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package mx.quest.database.postgresql;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.Statement;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Stream;
|
||||
import mx.quest.database.spi.ConnectionSettings;
|
||||
import mx.quest.database.spi.ConnectionSettings.Tls;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.postgresql.ds.PGSimpleDataSource;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
class PostgresProviderTest {
|
||||
|
||||
static ConnectionSettings settings(String host, int port, String password, Tls tls, Path ca) {
|
||||
return new ConnectionSettings(host, port, "runway", "runway", password, tls, ca, Duration.ofSeconds(5),
|
||||
Duration.ofSeconds(40));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tlsIsExactlyAsConfiguredAndTimeoutsArePinned() {
|
||||
PostgresProvider p = new PostgresProvider();
|
||||
PGSimpleDataSource off = (PGSimpleDataSource) p.dataSource(settings("db", 0, "pw", Tls.DISABLE, null));
|
||||
assertThat(off.getSslMode()).isEqualTo("disable");
|
||||
assertThat(off.getPortNumbers()).containsExactly(5432);
|
||||
assertThat(off.getConnectTimeout()).isEqualTo(5);
|
||||
assertThat(off.getSocketTimeout()).isEqualTo(40);
|
||||
assertThat(off.getTcpKeepAlive()).isTrue();
|
||||
PGSimpleDataSource req = (PGSimpleDataSource) p.dataSource(settings("db", 1, "pw", Tls.REQUIRE, null));
|
||||
assertThat(req.getSslMode()).isEqualTo("require");
|
||||
assertThat(req.getSslfactory()).isEqualTo(PostgresProvider.NON_VALIDATING);
|
||||
PGSimpleDataSource jvm = (PGSimpleDataSource) p.dataSource(settings("db", 1, "pw", Tls.VERIFY_FULL, null));
|
||||
assertThat(jvm.getSslMode()).isEqualTo("verify-full");
|
||||
assertThat(jvm.getSslfactory()).isEqualTo(PostgresProvider.JVM_TRUST);
|
||||
PGSimpleDataSource ca = (PGSimpleDataSource) p.dataSource(settings("db", 1, "pw", Tls.VERIFY_FULL,
|
||||
Path.of("/x/ca.pem")));
|
||||
assertThat(ca.getSslfactory()).isEqualTo(PostgresProvider.CA_FILE);
|
||||
assertThat(ca.getSslRootCert()).isEqualTo("/x/ca.pem");
|
||||
assertThat(ca.getUrl()).doesNotContain("pw").doesNotContain("password");
|
||||
PGSimpleDataSource v6 = (PGSimpleDataSource) p.dataSource(settings("::1", 1, "pw", Tls.DISABLE, null));
|
||||
assertThat(v6.getServerNames()).containsExactly("[::1]");
|
||||
}
|
||||
|
||||
@Test
|
||||
void itConnectsToARealServer() throws Exception {
|
||||
String hostPort = System.getProperty("db.it.postgresql");
|
||||
String file = System.getProperty("db.it.password-file");
|
||||
assumeTrue(hostPort != null && file != null, "no PostgreSQL server configured");
|
||||
String[] hp = hostPort.split(":");
|
||||
try (Connection c = new PostgresProvider().dataSource(settings(hp[0], Integer.parseInt(hp[1]),
|
||||
Files.readString(Path.of(file)).strip(), Tls.DISABLE, null)).getConnection();
|
||||
Statement s = c.createStatement(); ResultSet rs = s.executeQuery("SELECT current_setting('application_name')")) {
|
||||
rs.next();
|
||||
assertThat(rs.getString(1)).isEqualTo("runway");
|
||||
}
|
||||
assertThat(new PostgresProvider().driverVersion()).isEqualTo("pgjdbc 42.7.10");
|
||||
}
|
||||
|
||||
@Test
|
||||
void theConfigurationRegistersOneProviderNamedForThePlugin() {
|
||||
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
|
||||
PostgresProviderConfig.class)) {
|
||||
assertThat(ctx.getBeanNamesForType(mx.quest.database.spi.DatabaseProvider.class))
|
||||
.containsExactly("databasePostgresqlProvider");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCompiledClassesLinkNoHostInternals() throws Exception {
|
||||
Path classes = Path.of("target", "classes");
|
||||
Pattern host = Pattern.compile("mx[/.]quest[/.]runway[/.](?!spi[/.])[\\w/.$]*");
|
||||
List<String> problems = new ArrayList<>();
|
||||
try (Stream<Path> files = Files.walk(classes)) {
|
||||
for (Path f : files.filter(x -> x.toString().endsWith(".class")).toList()) {
|
||||
Matcher m = host.matcher(new String(Files.readAllBytes(f), StandardCharsets.ISO_8859_1));
|
||||
while (m.find()) {
|
||||
problems.add(f + " → " + m.group());
|
||||
}
|
||||
}
|
||||
}
|
||||
assertThat(problems).isEmpty();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user