Skip to content

Commit

Permalink
Add ssl support
Browse files Browse the repository at this point in the history
Allow disabling ssl certificate verification.
  • Loading branch information
foxylion committed Mar 31, 2017
1 parent 48609a2 commit 85a8446
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ Usage: java -jar build/libs/psql-multi-all.jar [options]
Default: 127.0.0.1:5432
--include, -i
Include databases with the following regex pattern
--no-ssl-verify
Verify SSL certificate
Default: true
--pass, -p
Password to authenticate
Default: postgres
Expand All @@ -35,6 +38,9 @@ Usage: java -jar build/libs/psql-multi-all.jar [options]
When using a SELECT statement as the command results will be printed to
console
Default: false
--ssl
Use a SSL connection to the database server
Default: false
--user, -u
User to authenticate
Default: postgres
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/de/foxylion/psql/multi/CommandlineParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ class CommandlineParams {

@Parameter(names = { "--force", "-f" }, description = "Force to continue when executed query fails on a database")
boolean force = false;
}

@Parameter(names = { "--ssl" }, description = "Use a SSL connection to the database server")
boolean ssl = false;

@Parameter(names = { "--no-ssl-verify" }, description = "Verify SSL certificate")
boolean noSslVerify = false;
}
4 changes: 2 additions & 2 deletions src/main/java/de/foxylion/psql/multi/PsqlFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class PsqlFactory {

public Psql getConnection(String host, String database, String user, String password) {
return new PsqlImpl(host, database, user, password);
public Psql getConnection(String host, String database, String user, String password, boolean ssl, boolean verifySsl) {
return new PsqlImpl(host, database, user, password, ssl, verifySsl);
}
}
5 changes: 3 additions & 2 deletions src/main/java/de/foxylion/psql/multi/PsqlImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ public class PsqlImpl implements Psql {

private final Connection connection;

public PsqlImpl(String host, String db, String user, String pass) {
public PsqlImpl(String host, String db, String user, String pass, boolean ssl, boolean noSslVerify) {
try {
connection = DriverManager.getConnection("jdbc:postgresql://" + host + "/" + db, user, pass);
String sslFactory = noSslVerify ? "&sslfactory=org.postgresql.ssl.NonValidatingFactory" : "";
connection = DriverManager.getConnection("jdbc:postgresql://" + host + "/" + db + "?ssl=" + ssl + sslFactory, user, pass);
} catch (SQLException e) {
throw new RuntimeException("Failed to open database connection: " + e.getMessage());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/foxylion/psql/multi/PsqlMulti.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void run() {
}

private void runQuery(String db) {
try (Psql psql = psqlFactory.getConnection(params.host, db, params.user, params.pass)) {
try (Psql psql = psqlFactory.getConnection(params.host, db, params.user, params.pass, params.ssl, params.noSslVerify)) {
if (!params.showResults) {
psql.execute(params.command);
} else {
Expand All @@ -44,7 +44,7 @@ private void runQuery(String db) {
}

private List<String> collectDatabases() {
try (Psql psql = psqlFactory.getConnection(params.host, "postgres", params.user, params.pass)) {
try (Psql psql = psqlFactory.getConnection(params.host, "postgres", params.user, params.pass, params.ssl, params.noSslVerify)) {
return psql.fetch("SELECT datname FROM pg_database WHERE datistemplate = false;").stream()
.map((row) -> row.get("datname")) //
.collect(Collectors.toList());
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/de/foxylion/psql/multi/PsqlMultiTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.foxylion.psql.multi;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

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

psqlFactory = mock(PsqlFactory.class);
when(psqlFactory.getConnection(any(), any(), any(), any())).then((a) -> {
when(psqlFactory.getConnection(any(), any(), any(), any(), anyBoolean(), anyBoolean())).then((a) -> {
String dbName = a.getArgumentAt(2, String.class);
if(!psqlMocks.containsKey(dbName)) {
Psql psql = mock(Psql.class);
Expand Down

0 comments on commit 85a8446

Please sign in to comment.