Skip to content

Commit 85a8446

Browse files
committed
Add ssl support
Allow disabling ssl certificate verification.
1 parent 48609a2 commit 85a8446

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Usage: java -jar build/libs/psql-multi-all.jar [options]
2626
Default: 127.0.0.1:5432
2727
--include, -i
2828
Include databases with the following regex pattern
29+
--no-ssl-verify
30+
Verify SSL certificate
31+
Default: true
2932
--pass, -p
3033
Password to authenticate
3134
Default: postgres
@@ -35,6 +38,9 @@ Usage: java -jar build/libs/psql-multi-all.jar [options]
3538
When using a SELECT statement as the command results will be printed to
3639
console
3740
Default: false
41+
--ssl
42+
Use a SSL connection to the database server
43+
Default: false
3844
--user, -u
3945
User to authenticate
4046
Default: postgres

src/main/java/de/foxylion/psql/multi/CommandlineParams.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,10 @@ class CommandlineParams {
3131

3232
@Parameter(names = { "--force", "-f" }, description = "Force to continue when executed query fails on a database")
3333
boolean force = false;
34-
}
34+
35+
@Parameter(names = { "--ssl" }, description = "Use a SSL connection to the database server")
36+
boolean ssl = false;
37+
38+
@Parameter(names = { "--no-ssl-verify" }, description = "Verify SSL certificate")
39+
boolean noSslVerify = false;
40+
}

src/main/java/de/foxylion/psql/multi/PsqlFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class PsqlFactory {
44

5-
public Psql getConnection(String host, String database, String user, String password) {
6-
return new PsqlImpl(host, database, user, password);
5+
public Psql getConnection(String host, String database, String user, String password, boolean ssl, boolean verifySsl) {
6+
return new PsqlImpl(host, database, user, password, ssl, verifySsl);
77
}
88
}

src/main/java/de/foxylion/psql/multi/PsqlImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class PsqlImpl implements Psql {
1414

1515
private final Connection connection;
1616

17-
public PsqlImpl(String host, String db, String user, String pass) {
17+
public PsqlImpl(String host, String db, String user, String pass, boolean ssl, boolean noSslVerify) {
1818
try {
19-
connection = DriverManager.getConnection("jdbc:postgresql://" + host + "/" + db, user, pass);
19+
String sslFactory = noSslVerify ? "&sslfactory=org.postgresql.ssl.NonValidatingFactory" : "";
20+
connection = DriverManager.getConnection("jdbc:postgresql://" + host + "/" + db + "?ssl=" + ssl + sslFactory, user, pass);
2021
} catch (SQLException e) {
2122
throw new RuntimeException("Failed to open database connection: " + e.getMessage());
2223
}

src/main/java/de/foxylion/psql/multi/PsqlMulti.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void run() {
2727
}
2828

2929
private void runQuery(String db) {
30-
try (Psql psql = psqlFactory.getConnection(params.host, db, params.user, params.pass)) {
30+
try (Psql psql = psqlFactory.getConnection(params.host, db, params.user, params.pass, params.ssl, params.noSslVerify)) {
3131
if (!params.showResults) {
3232
psql.execute(params.command);
3333
} else {
@@ -44,7 +44,7 @@ private void runQuery(String db) {
4444
}
4545

4646
private List<String> collectDatabases() {
47-
try (Psql psql = psqlFactory.getConnection(params.host, "postgres", params.user, params.pass)) {
47+
try (Psql psql = psqlFactory.getConnection(params.host, "postgres", params.user, params.pass, params.ssl, params.noSslVerify)) {
4848
return psql.fetch("SELECT datname FROM pg_database WHERE datistemplate = false;").stream()
4949
.map((row) -> row.get("datname")) //
5050
.collect(Collectors.toList());

src/test/java/de/foxylion/psql/multi/PsqlMultiTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.foxylion.psql.multi;
22

33
import static org.mockito.Matchers.any;
4+
import static org.mockito.Matchers.anyBoolean;
45
import static org.mockito.Mockito.mock;
56
import static org.mockito.Mockito.when;
67

@@ -19,7 +20,7 @@ void setup() {
1920
psqlMocks = new HashMap<>();
2021

2122
psqlFactory = mock(PsqlFactory.class);
22-
when(psqlFactory.getConnection(any(), any(), any(), any())).then((a) -> {
23+
when(psqlFactory.getConnection(any(), any(), any(), any(), anyBoolean(), anyBoolean())).then((a) -> {
2324
String dbName = a.getArgumentAt(2, String.class);
2425
if(!psqlMocks.containsKey(dbName)) {
2526
Psql psql = mock(Psql.class);

0 commit comments

Comments
 (0)