Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add authenticationPluginClassName option to provide passwords a…
…t runtime Adds authenticationPluginClassName connection property that allows end users to specify a class that will provide the connection passwords at runtime. This allows configuring a connection with a password that must be generated on the fly or periodically changes. Custom implementations have access to the full connection properties and the type of authentication that is being requested by the server, e.g. MD5 or SCRAM.
- Loading branch information
Showing
10 changed files
with
258 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
pgjdbc/src/main/java/org/postgresql/core/AuthenticationPluginManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2021, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.core; | ||
|
||
import org.postgresql.PGProperty; | ||
import org.postgresql.plugin.AuthenticationPlugin; | ||
import org.postgresql.plugin.AuthenticationRequestType; | ||
import org.postgresql.util.GT; | ||
import org.postgresql.util.ObjectFactory; | ||
import org.postgresql.util.PSQLException; | ||
import org.postgresql.util.PSQLState; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Properties; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
public class AuthenticationPluginManager { | ||
private static final Logger LOGGER = Logger.getLogger(AuthenticationPluginManager.class.getName()); | ||
|
||
private AuthenticationPluginManager() { | ||
} | ||
|
||
/** | ||
* If a password is requested by the server during connection initiation, this | ||
* method will be invoked to supply the password. This method will only be | ||
* invoked if the server actually requests a password, e.g. trust authentication | ||
* will skip it entirely. | ||
* | ||
* @param type The authentication type that is being requested | ||
* @param info The connection properties for the connection | ||
* @return The password to use for authentication or null if none is available | ||
* @throws PSQLException Throws a PSQLException if the plugin class cannot be instantiated | ||
*/ | ||
public static @Nullable String getPassword(AuthenticationRequestType type, Properties info) throws PSQLException { | ||
String authPluginClassName = PGProperty.AUTHENTICATION_PLUGIN_CLASS_NAME.get(info); | ||
|
||
if (authPluginClassName == null || authPluginClassName.equals("")) { | ||
// Default auth plugin simply pulls password directly from connection properties | ||
return PGProperty.PASSWORD.get(info); | ||
} | ||
|
||
AuthenticationPlugin authPlugin; | ||
try { | ||
authPlugin = (AuthenticationPlugin) ObjectFactory.instantiate(authPluginClassName, info, | ||
false, null); | ||
} catch (Exception ex) { | ||
LOGGER.log(Level.FINE, "Unable to load Authentication Plugin " + ex.toString()); | ||
throw new PSQLException(ex.getMessage(), PSQLState.UNEXPECTED_ERROR); | ||
} | ||
return authPlugin.getPassword(type); | ||
} | ||
|
||
/** | ||
* Helper that wraps getPassword(...), checks that it is not-null, and encodes | ||
* it as a byte array. Used by internal code paths that require an encoded password that may be an | ||
* empty string, but not null. | ||
* | ||
* @param type The authentication type that is being requested | ||
* @param info The connection properties for the connection | ||
* @return The password to use for authentication encoded as a byte array | ||
* @throws PSQLException Throws a PSQLException if the plugin class cannot be instantiated or if the retrieved password is null. | ||
*/ | ||
public static byte[] getEncodedPassword(AuthenticationRequestType type, Properties info) throws PSQLException { | ||
String password = getPassword(type, info); | ||
|
||
if (password == null) { | ||
throw new PSQLException( | ||
GT.tr("The server requested password-based authentication, but no password was provided."), | ||
PSQLState.CONNECTION_REJECTED); | ||
} | ||
|
||
return password.getBytes(StandardCharsets.UTF_8); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
pgjdbc/src/main/java/org/postgresql/plugin/AuthenticationPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* Copyright (c) 2021, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.plugin; | ||
|
||
import org.postgresql.util.PSQLException; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
public interface AuthenticationPlugin { | ||
@Nullable | ||
String getPassword(AuthenticationRequestType type) throws PSQLException; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
pgjdbc/src/main/java/org/postgresql/plugin/AuthenticationRequestType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* Copyright (c) 2021, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.plugin; | ||
|
||
public enum AuthenticationRequestType { | ||
CLEARTEXT_PASSWORD, | ||
GSS, | ||
MD5_PASSWORD, | ||
SASL, | ||
} |
86 changes: 86 additions & 0 deletions
86
pgjdbc/src/test/java/org/postgresql/test/plugin/AuthenticationPluginTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2021, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.test.plugin; | ||
|
||
import org.postgresql.PGProperty; | ||
import org.postgresql.core.ServerVersion; | ||
import org.postgresql.plugin.AuthenticationPlugin; | ||
import org.postgresql.plugin.AuthenticationRequestType; | ||
import org.postgresql.test.TestUtil; | ||
import org.postgresql.util.PSQLException; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import java.util.Properties; | ||
import java.util.function.Consumer; | ||
|
||
public class AuthenticationPluginTest { | ||
@BeforeClass | ||
public static void setUp() throws SQLException { | ||
TestUtil.assumeHaveMinimumServerVersion(ServerVersion.v10); | ||
} | ||
|
||
public static class DummyAuthenticationPlugin implements AuthenticationPlugin { | ||
private static Consumer<AuthenticationRequestType> onGetPassword; | ||
|
||
@Override | ||
public @Nullable String getPassword(AuthenticationRequestType type) throws PSQLException { | ||
onGetPassword.accept(type); | ||
|
||
// Ex: "MD5" => "DUMMY-MD5" | ||
return "DUMMY-" + type.toString(); | ||
} | ||
} | ||
|
||
private void testAuthPlugin(String username, String passwordEncryption, AuthenticationRequestType expectedType) throws SQLException { | ||
createRole(username, passwordEncryption, "DUMMY-" + expectedType.toString()); | ||
try { | ||
Properties props = new Properties(); | ||
props.setProperty(PGProperty.AUTHENTICATION_PLUGIN_CLASS_NAME.getName(), DummyAuthenticationPlugin.class.getName()); | ||
props.setProperty("username", username); | ||
|
||
boolean[] wasCalled = { false }; | ||
DummyAuthenticationPlugin.onGetPassword = type -> { | ||
wasCalled[0] = true; | ||
Assert.assertEquals("The authentication type should match", expectedType, type); | ||
}; | ||
try (Connection conn = TestUtil.openDB(props)) { | ||
Assert.assertTrue("The custom authentication plugin should be invoked", wasCalled[0]); | ||
} | ||
} finally { | ||
dropRole(username); | ||
} | ||
} | ||
|
||
@Test | ||
public void testAuthPluginMD5() throws Exception { | ||
testAuthPlugin("auth_plugin_test_md5", "md5", AuthenticationRequestType.MD5_PASSWORD); | ||
} | ||
|
||
@Test | ||
public void testAuthPluginSASL() throws Exception { | ||
testAuthPlugin("auth_plugin_test_sasl", "scram-sha-256", AuthenticationRequestType.SASL); | ||
} | ||
|
||
private static void createRole(String username, String passwordEncryption, String password) throws SQLException { | ||
try (Connection conn = TestUtil.openPrivilegedDB()) { | ||
TestUtil.execute("SET password_encryption='" + passwordEncryption + "'", conn); | ||
TestUtil.execute("DROP ROLE IF EXISTS " + username, conn); | ||
TestUtil.execute("CREATE USER " + username + " WITH PASSWORD '" + password + "'", conn); | ||
} | ||
} | ||
|
||
private static void dropRole(String username) throws SQLException { | ||
try (Connection conn = TestUtil.openPrivilegedDB()) { | ||
TestUtil.execute("DROP ROLE IF EXISTS " + username, conn); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
pgjdbc/src/test/java/org/postgresql/test/plugin/PluginTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Copyright (c) 2021, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.test.plugin; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
import org.junit.runners.Suite.SuiteClasses; | ||
|
||
@RunWith(Suite.class) | ||
@SuiteClasses({ | ||
AuthenticationPluginTest.class, | ||
}) | ||
public class PluginTestSuite { | ||
|
||
} |