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();
}
}
}
Aggregations