Search in sources :

Example 6 with PasswordExpiredException

use of com.mysql.cj.exceptions.PasswordExpiredException in project ABC by RuiPinto96274.

the class ConnectionRegressionTest method testExpiredPassword.

@Test
public void testExpiredPassword() throws Exception {
    assumeTrue(versionMeetsMinimum(5, 6, 10), "MySQL 5.6.10+ is required to run this test.");
    Connection testConn = null;
    Statement testSt = null;
    ResultSet testRs = null;
    Properties urlProps = getPropertiesFromTestsuiteUrl();
    String dbname = urlProps.getProperty(PropertyKey.DBNAME.getKeyName());
    try {
        createUser("'must_change1'@'%'", "IDENTIFIED BY 'aha'");
        this.stmt.executeUpdate("grant all on `" + dbname + "`.* to 'must_change1'@'%'");
        createUser("'must_change2'@'%'", "IDENTIFIED BY 'aha'");
        this.stmt.executeUpdate("grant all on `" + dbname + "`.* to 'must_change2'@'%'");
        // workaround for Bug#77732
        if (versionMeetsMinimum(5, 7, 6) && !versionMeetsMinimum(8, 0, 5)) {
            this.stmt.executeUpdate("GRANT SELECT ON `performance_schema`.`session_variables` TO 'must_change1'@'%'");
            this.stmt.executeUpdate("GRANT SELECT ON `performance_schema`.`session_variables` TO 'must_change2'@'%'");
        }
        this.stmt.executeUpdate(versionMeetsMinimum(5, 7, 6) ? "ALTER USER 'must_change1'@'%', 'must_change2'@'%' PASSWORD EXPIRE" : "ALTER USER 'must_change1'@'%' PASSWORD EXPIRE, 'must_change2'@'%' PASSWORD EXPIRE");
        Properties props = new Properties();
        props.setProperty(PropertyKey.sslMode.getKeyName(), "DISABLED");
        props.setProperty(PropertyKey.allowPublicKeyRetrieval.getKeyName(), "true");
        // ALTER USER can be prepared as of 5.6.8 (BUG#14646014)
        if (versionMeetsMinimum(5, 6, 8)) {
            props.setProperty(PropertyKey.useServerPrepStmts.getKeyName(), "true");
            testConn = getConnectionWithProps(props);
            this.pstmt = testConn.prepareStatement(versionMeetsMinimum(5, 7, 6) ? "ALTER USER 'must_change1'@'%', 'must_change2'@'%' PASSWORD EXPIRE" : "ALTER USER 'must_change1'@'%' PASSWORD EXPIRE, 'must_change2'@'%' PASSWORD EXPIRE");
            this.pstmt.executeUpdate();
            this.pstmt.close();
            this.pstmt = testConn.prepareStatement(versionMeetsMinimum(5, 7, 6) ? "ALTER USER ?, 'must_change2'@'%' PASSWORD EXPIRE" : "ALTER USER ? PASSWORD EXPIRE, 'must_change2'@'%' PASSWORD EXPIRE");
            this.pstmt.setString(1, "must_change1");
            this.pstmt.executeUpdate();
            this.pstmt.close();
            testConn.close();
        }
        props.setProperty(PropertyKey.USER.getKeyName(), "must_change1");
        props.setProperty(PropertyKey.PASSWORD.getKeyName(), "aha");
        try {
            testConn = getConnectionWithProps(props);
            fail("SQLException expected due to password expired");
        } catch (PasswordExpiredException | ClosedOnExpiredPasswordException | SQLException e1) {
            if ((e1 instanceof SQLException && (((SQLException) e1).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD || ((SQLException) e1).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD_LOGIN)) || e1 instanceof PasswordExpiredException || e1 instanceof ClosedOnExpiredPasswordException) {
                props.setProperty(PropertyKey.disconnectOnExpiredPasswords.getKeyName(), "false");
                try {
                    testConn = getConnectionWithProps(props);
                    testSt = testConn.createStatement();
                    testRs = testSt.executeQuery("SHOW VARIABLES LIKE 'disconnect_on_expired_password'");
                    testRs.close();
                    fail("SQLException expected due to password expired");
                } catch (PasswordExpiredException | ClosedOnExpiredPasswordException | SQLException e3) {
                    if (e3 instanceof SQLException && (((SQLException) e3).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD_LOGIN) || e3 instanceof ClosedOnExpiredPasswordException) {
                        testSt.close();
                        testConn = getConnectionWithProps(props);
                        testSt = testConn.createStatement();
                    }
                    if (testSt == null) {
                        testSt = testConn.createStatement();
                    }
                    testSt.executeUpdate(versionMeetsMinimum(5, 7, 6) ? "ALTER USER USER() IDENTIFIED BY 'newpwd'" : "SET PASSWORD = PASSWORD('newpwd')");
                    testConn.close();
                    props.setProperty(PropertyKey.USER.getKeyName(), "must_change1");
                    props.setProperty(PropertyKey.PASSWORD.getKeyName(), "newpwd");
                    props.setProperty(PropertyKey.disconnectOnExpiredPasswords.getKeyName(), "true");
                    testConn = getConnectionWithProps(props);
                    testSt = testConn.createStatement();
                    testRs = testSt.executeQuery("SHOW VARIABLES LIKE 'disconnect_on_expired_password'");
                    assertTrue(testRs.next());
                    // change user
                    try {
                        ((JdbcConnection) testConn).changeUser("must_change2", "aha");
                        fail("SQLException expected due to password expired");
                    } catch (PasswordExpiredException | ClosedOnExpiredPasswordException | SQLException e4) {
                        if (e4 instanceof SQLException && (((SQLException) e4).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD || ((SQLException) e4).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD_LOGIN) || e4 instanceof PasswordExpiredException || e4 instanceof ClosedOnExpiredPasswordException) {
                            props.setProperty(PropertyKey.disconnectOnExpiredPasswords.getKeyName(), "false");
                            testConn = getConnectionWithProps(props);
                            try {
                                ((JdbcConnection) testConn).changeUser("must_change2", "aha");
                                testSt = testConn.createStatement();
                                testRs = testSt.executeQuery("SHOW VARIABLES LIKE 'disconnect_on_expired_password'");
                                fail("SQLException expected due to password expired");
                            } catch (PasswordExpiredException | ClosedOnExpiredPasswordException | SQLException e5) {
                                if (e5 instanceof SQLException && ((SQLException) e5).getErrorCode() == MysqlErrorNumbers.ER_MUST_CHANGE_PASSWORD_LOGIN || e5 instanceof ClosedOnExpiredPasswordException) {
                                    testConn = getConnectionWithProps(props);
                                    testSt = testConn.createStatement();
                                }
                                testSt = testConn.createStatement();
                                testSt.executeUpdate(versionMeetsMinimum(5, 7, 6) ? "ALTER USER USER() IDENTIFIED BY 'newpwd'" : "SET PASSWORD = PASSWORD('newpwd')");
                                testConn.close();
                                props.setProperty(PropertyKey.USER.getKeyName(), "must_change2");
                                props.setProperty(PropertyKey.PASSWORD.getKeyName(), "newpwd");
                                props.setProperty(PropertyKey.disconnectOnExpiredPasswords.getKeyName(), "true");
                                testConn = getConnectionWithProps(props);
                                testSt = testConn.createStatement();
                                testRs = testSt.executeQuery("SHOW VARIABLES LIKE 'disconnect_on_expired_password'");
                                assertTrue(testRs.next());
                            }
                        } else {
                            throw e4;
                        }
                    }
                }
            } else {
                throw e1;
            }
        }
    } finally {
        if (testRs != null) {
            testRs.close();
        }
        if (testSt != null) {
            testSt.close();
        }
        if (testConn != null) {
            testConn.close();
        }
    }
}
Also used : PasswordExpiredException(com.mysql.cj.exceptions.PasswordExpiredException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerPreparedStatement(com.mysql.cj.jdbc.ServerPreparedStatement) ClientPreparedStatement(com.mysql.cj.jdbc.ClientPreparedStatement) ReplicationConnection(com.mysql.cj.jdbc.ha.ReplicationConnection) MysqlPooledConnection(com.mysql.cj.jdbc.MysqlPooledConnection) SuspendableXAConnection(com.mysql.cj.jdbc.SuspendableXAConnection) Connection(java.sql.Connection) XAConnection(javax.sql.XAConnection) PooledConnection(javax.sql.PooledConnection) MysqlXAConnection(com.mysql.cj.jdbc.MysqlXAConnection) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) MysqlConnection(com.mysql.cj.MysqlConnection) ResultSet(java.sql.ResultSet) ClosedOnExpiredPasswordException(com.mysql.cj.exceptions.ClosedOnExpiredPasswordException) JdbcConnection(com.mysql.cj.jdbc.JdbcConnection) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Aggregations

PasswordExpiredException (com.mysql.cj.exceptions.PasswordExpiredException)6 SQLException (java.sql.SQLException)6 MysqlConnection (com.mysql.cj.MysqlConnection)3 CJException (com.mysql.cj.exceptions.CJException)3 ClosedOnExpiredPasswordException (com.mysql.cj.exceptions.ClosedOnExpiredPasswordException)3 UnableToConnectException (com.mysql.cj.exceptions.UnableToConnectException)3 ClientPreparedStatement (com.mysql.cj.jdbc.ClientPreparedStatement)3 JdbcConnection (com.mysql.cj.jdbc.JdbcConnection)3 MysqlPooledConnection (com.mysql.cj.jdbc.MysqlPooledConnection)3 MysqlXAConnection (com.mysql.cj.jdbc.MysqlXAConnection)3 ServerPreparedStatement (com.mysql.cj.jdbc.ServerPreparedStatement)3 SuspendableXAConnection (com.mysql.cj.jdbc.SuspendableXAConnection)3 ReplicationConnection (com.mysql.cj.jdbc.ha.ReplicationConnection)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLClientInfoException (java.sql.SQLClientInfoException)3 Savepoint (java.sql.Savepoint)3 Statement (java.sql.Statement)3 Properties (java.util.Properties)3