|
| 1 | +package org.tarantool.jdbc; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.tarantool.TestAssumptions.assumeMinimalServerVersion; |
| 6 | + |
| 7 | +import org.tarantool.ServerVersion; |
| 8 | +import org.tarantool.TarantoolTestHelper; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.AfterAll; |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.BeforeAll; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | + |
| 16 | +import java.security.Permission; |
| 17 | +import java.sql.Connection; |
| 18 | +import java.sql.DriverManager; |
| 19 | +import java.sql.SQLException; |
| 20 | +import java.util.EnumSet; |
| 21 | +import java.util.concurrent.Executors; |
| 22 | + |
| 23 | +public class JdbcSecurityIT { |
| 24 | + |
| 25 | + private static TarantoolTestHelper testHelper; |
| 26 | + |
| 27 | + private Connection connection; |
| 28 | + private SecurityManager originalSecurityManager; |
| 29 | + |
| 30 | + @BeforeAll |
| 31 | + public static void setupEnv() { |
| 32 | + testHelper = new TarantoolTestHelper("jdbc-security-it"); |
| 33 | + testHelper.createInstance(); |
| 34 | + testHelper.startInstance(); |
| 35 | + } |
| 36 | + |
| 37 | + @AfterAll |
| 38 | + public static void teardownEnv() { |
| 39 | + testHelper.stopInstance(); |
| 40 | + } |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + public void setUpTest() throws SQLException { |
| 44 | + assumeMinimalServerVersion(testHelper.getInstanceVersion(), ServerVersion.V_2_1); |
| 45 | + connection = DriverManager.getConnection(SqlTestUtils.makeDefaultJdbcUrl()); |
| 46 | + originalSecurityManager = System.getSecurityManager(); |
| 47 | + } |
| 48 | + |
| 49 | + @AfterEach |
| 50 | + public void tearDownTest() throws SQLException { |
| 51 | + assumeMinimalServerVersion(testHelper.getInstanceVersion(), ServerVersion.V_2_1); |
| 52 | + if (connection != null) { |
| 53 | + connection.close(); |
| 54 | + } |
| 55 | + System.setSecurityManager(originalSecurityManager); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void testDeniedConnectionAbort() { |
| 60 | + EnumSet<JdbcPermission> exclusions = EnumSet.of(JdbcPermission.CALL_ABORT); |
| 61 | + System.setSecurityManager(new JdbcSecurityManager(true, exclusions)); |
| 62 | + |
| 63 | + SecurityException securityException = assertThrows( |
| 64 | + SecurityException.class, |
| 65 | + () -> connection.abort(Executors.newSingleThreadExecutor()) |
| 66 | + ); |
| 67 | + assertEquals(securityException.getMessage(), "Permission callAbort is not allowed"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void testDeniedSetConnectionTimeout() { |
| 72 | + EnumSet<JdbcPermission> exclusions = EnumSet.of(JdbcPermission.SET_NETWORK_TIMEOUT); |
| 73 | + System.setSecurityManager(new JdbcSecurityManager(true, exclusions)); |
| 74 | + |
| 75 | + SecurityException securityException = assertThrows( |
| 76 | + SecurityException.class, |
| 77 | + () -> connection.setNetworkTimeout(Executors.newSingleThreadExecutor(), 1000) |
| 78 | + ); |
| 79 | + assertEquals(securityException.getMessage(), "Permission setNetworkTimeout is not allowed"); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Lists permissions supported by JDBC API. |
| 84 | + * |
| 85 | + * <ul> |
| 86 | + * <li>setLog</li> |
| 87 | + * <li>callAbort</li> |
| 88 | + * <li>setSyncFactory<</li> |
| 89 | + * <li>setNetworkTimeout</li> |
| 90 | + * <li>deregisterDriver</li> |
| 91 | + * </ul> |
| 92 | + * |
| 93 | + * @see java.sql.SQLPermission |
| 94 | + */ |
| 95 | + private enum JdbcPermission { |
| 96 | + SET_LOG("setLog"), |
| 97 | + CALL_ABORT("callAbort"), |
| 98 | + SET_SYNC_FACTORY("setSyncFactory"), |
| 99 | + SET_NETWORK_TIMEOUT("setNetworkTimeout"), |
| 100 | + DEREGISTER_DRIVER("deregisterDriver"); |
| 101 | + |
| 102 | + private final String permissionName; |
| 103 | + |
| 104 | + JdbcPermission(String permissionName) { |
| 105 | + this.permissionName = permissionName; |
| 106 | + } |
| 107 | + |
| 108 | + public String getPermissionName() { |
| 109 | + return permissionName; |
| 110 | + } |
| 111 | + |
| 112 | + public static JdbcPermission fromName(String name) { |
| 113 | + for (JdbcPermission values : JdbcPermission.values()) { |
| 114 | + if (values.permissionName.equals(name)) { |
| 115 | + return values; |
| 116 | + } |
| 117 | + } |
| 118 | + return null; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private static class JdbcSecurityManager extends SecurityManager { |
| 123 | + private final boolean allowAll; |
| 124 | + private final EnumSet<JdbcPermission> exclusions; |
| 125 | + |
| 126 | + /** |
| 127 | + * Configures a new {@link SecurityManager} that follows the custom rules. |
| 128 | + * |
| 129 | + * @param allowAll whether permissions are allowed by default or not |
| 130 | + * @param exclusions optional set of exclusions |
| 131 | + */ |
| 132 | + private JdbcSecurityManager(boolean allowAll, EnumSet<JdbcPermission> exclusions) { |
| 133 | + this.exclusions = exclusions; |
| 134 | + this.allowAll = allowAll; |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void checkPermission(Permission permission) { |
| 139 | + JdbcPermission jdbcPermission = JdbcPermission.fromName(permission.getName()); |
| 140 | + if (jdbcPermission == null) { |
| 141 | + return; |
| 142 | + } |
| 143 | + boolean allowed = allowAll ^ exclusions.contains(jdbcPermission); |
| 144 | + if (!allowed) { |
| 145 | + throw new SecurityException("Permission " + jdbcPermission.getPermissionName() + " is not allowed"); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
0 commit comments