postgresql-jdbc.git / summary / log / commit / refs

commit    de239cc09aee3916daa6cdb77abfa486675efbbd
Author:   Dave Cramer <davecramer@gmail.com>
Commit:   GitHub <noreply@github.com>
Date:     Thu Jul 30 15:54:14 2020 +0000

    use connection encoding instead of hard coding ISO-8859-1 (#1833)
    
    * use connection encoding instead of hard coding ISO-8859-1
    
    * sleep 2 seconds to wait for pg to start
    
    * set canonicalize = true


.github/workflows/main.yml | 5 +++- .../main/java/org/postgresql/jdbc/PgResultSet.java | 4 +-- test-gss/src/main/groovy/Kerberos.groovy | 4 ++- test-gss/src/main/groovy/PgJDBC.groovy | 2 +- test-gss/src/main/groovy/Postgres.groovy | 3 +++ test-gss/src/main/groovy/TestPostgres.groovy | 29 ++++++++++++++++------ test-gss/src/main/groovy/Util.groovy | 3 +++ 7 files changed, 38 insertions(+), 12 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f87daa7d..e37c8fb0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -123,7 +123,10 @@ jobs: - name: 'Install software' run: sudo apt -y install krb5-kdc krb5-admin-server libkrb5-dev postgresql-12 - name: 'Update hosts' - run: sudo -- sh -c "echo 127.0.0.1 auth-test-localhost.postgresql.example.com >> /etc/hosts" + run: | + sudo -- sh -c "echo 127.0.0.1 localhost auth-test-localhost.postgresql.example.com > /etc/hosts" + cat /etc/hosts + - uses: actions/cache@v2 name: Cache ./gradle/caches/ with: diff --git a/pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java b/pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java index f2f4a07f..51331909 100644 --- a/pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java +++ b/pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java @@ -1808,10 +1808,10 @@ public class PgResultSet implements ResultSet, org.postgresql.PGRefCursorResultS } else { try { rowBuffer.set(columnIndex, - PGbytea.toPGString((byte[]) valueObject).getBytes("ISO-8859-1")); + PGbytea.toPGString((byte[]) valueObject).getBytes(connection.getEncoding().name())); } catch (UnsupportedEncodingException e) { throw new PSQLException( - GT.tr("The JVM claims not to support the encoding: {0}", "ISO-8859-1"), + GT.tr("The JVM claims not to support the encoding: {0}", connection.getEncoding().name()), PSQLState.UNEXPECTED_ERROR, e); } } diff --git a/test-gss/src/main/groovy/Kerberos.groovy b/test-gss/src/main/groovy/Kerberos.groovy index a04c7fee..826062e0 100644 --- a/test-gss/src/main/groovy/Kerberos.groovy +++ b/test-gss/src/main/groovy/Kerberos.groovy @@ -105,6 +105,8 @@ kdc = FILE:$kdcLog [libdefaults] default_realm = $realm +canonicalize = true + [realms] $realm = { @@ -181,7 +183,7 @@ $realm = { return krb5Process } - + public void mkdir(String newDir){ diff --git a/test-gss/src/main/groovy/PgJDBC.groovy b/test-gss/src/main/groovy/PgJDBC.groovy index c73b40f9..d69241ff 100644 --- a/test-gss/src/main/groovy/PgJDBC.groovy +++ b/test-gss/src/main/groovy/PgJDBC.groovy @@ -31,7 +31,7 @@ public class PgJDBC { } - public Connection tryConnect(String dataBase, String host, int port, String user, String password) { + public Connection tryConnect(String dataBase, String host, int port, String user, String password) throws Exception { String url = "jdbc:postgresql://$host:$port/$dataBase" PGProperty.USER.set(properties,user) PGProperty.PASSWORD.set(properties,password) diff --git a/test-gss/src/main/groovy/Postgres.groovy b/test-gss/src/main/groovy/Postgres.groovy index 585e5ba9..327dc1b8 100644 --- a/test-gss/src/main/groovy/Postgres.groovy +++ b/test-gss/src/main/groovy/Postgres.groovy @@ -65,6 +65,9 @@ class Postgres { public void writePgHBA(String text) { Util.appendToFile("$dataPath/pg_hba.conf", text, true) } + public String readPgHBA() { + Util.readFile("$dataPath/pg_hba.conf") + } public void writePgConf(String text) { Util.appendToFile("$dataPath/postgresql.conf", text, false) } diff --git a/test-gss/src/main/groovy/TestPostgres.groovy b/test-gss/src/main/groovy/TestPostgres.groovy index c91fe2d6..16bc82f6 100644 --- a/test-gss/src/main/groovy/TestPostgres.groovy +++ b/test-gss/src/main/groovy/TestPostgres.groovy @@ -30,8 +30,13 @@ class TestPostgres { currentEnvironment[environment.size() + 1] = kerberos.env[1] Postgres postgres = new Postgres('/usr/lib/postgresql/12/bin/', '/tmp/pggss') + /* + make sure we can connect + */ + postgres.writePgHBA("host all all 127.0.0.1/32 trust") if (postgres.waitForHBA(5000) ) { Process p = postgres.startPostgres(currentEnvironment) + sleep(2000) pgJDBC = new PgJDBC(host, postgres.getPort()); pgJDBC.addProperty(PGProperty.GSS_ENC_MODE, GSSEncMode.DISABLE.value) pgJDBC.createUser(superUser, superPass, 'test1', 'secret1') @@ -40,21 +45,25 @@ class TestPostgres { postgres.enableMyMap('EXAMPLE.COM') postgres.setKeyTabLocation(kerberos.getKeytab()) postgres.reload() - pgJDBC.addProperty(PGProperty.GSS_ENC_MODE, GSSEncMode.PREFER.value) + pgJDBC.addProperty(PGProperty.GSS_ENC_MODE, GSSEncMode.REQUIRE.value) pgJDBC.addProperty(PGProperty.JAAS_LOGIN, true) pgJDBC.addProperty(PGProperty.JAAS_APPLICATION_NAME, "pgjdbc") + try { Connection connection; try { connection = pgJDBC.tryConnect('test', 'auth-test-localhost.postgresql.example.com', postgres.getPort(), 'test1', 'secret1') - if (pgJDBC.select(connection, "SELECT gss_authenticated AND encrypted from pg_stat_gssapi where pid = pg_backend_pid()")) { System.err.println 'GSS authenticated and encrypted Connection succeeded' } else { Assert.fail 'GSS authenticated and encrypted Connection failed' } + } catch( Exception ex ) { + System.err.println "PG HBA.conf: \n ${postgres.readPgHBA()}" + ex.printStackTrace() } finally { - connection.close() + connection?.close() + } postgres.enableGSS('127.0.0.1', 'hostnogssenc', 'map=mymap') @@ -63,17 +72,23 @@ class TestPostgres { try { connection = pgJDBC.tryConnect('test', 'auth-test-localhost.postgresql.example.com', postgres.getPort(), 'test1', 'secret1') + if (pgJDBC.select(connection, "SELECT gss_authenticated AND not encrypted from pg_stat_gssapi where pid = pg_backend_pid()")) { System.err.println 'GSS authenticated and not encrypted Connection succeeded' } else { Assert.fail 'GSS authenticated and not encrypted Connection failed' } - } finally { - connection.close() - } - + }catch( Exception ex ) { + System.err.println "PG HBA.conf: \n ${postgres.readPgHBA()}" + ex.printStackTrace() + } finally { + if (!connection) { + System.err.println "PG HBA.conf: \n ${postgres.readPgHBA()}" + } + connection?.close() + } } finally { !p.destroy() !kerberos.destroy() diff --git a/test-gss/src/main/groovy/Util.groovy b/test-gss/src/main/groovy/Util.groovy index 5094f00c..44256407 100644 --- a/test-gss/src/main/groovy/Util.groovy +++ b/test-gss/src/main/groovy/Util.groovy @@ -20,4 +20,7 @@ public class Util { f.append("$text\n ") } } + public static String readFile(String fileName) { + new FileInputStream(fileName).text + } } [parent: a40052c9176a]