|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package software.amazon.jdbc.plugin.iam; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | +import static org.mockito.ArgumentMatchers.anyString; |
| 25 | +import static org.mockito.ArgumentMatchers.eq; |
| 26 | +import static org.mockito.Mockito.doReturn; |
| 27 | +import static software.amazon.jdbc.plugin.iam.DsqlTokenUtilityTest.ADMIN_USER; |
| 28 | +import static software.amazon.jdbc.plugin.iam.DsqlTokenUtilityTest.REGULAR_USER; |
| 29 | + |
| 30 | +import java.sql.Connection; |
| 31 | +import java.sql.SQLException; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Properties; |
| 34 | +import org.junit.jupiter.api.AfterEach; |
| 35 | +import org.junit.jupiter.api.BeforeEach; |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.params.ParameterizedTest; |
| 38 | +import org.junit.jupiter.params.provider.ValueSource; |
| 39 | +import org.mockito.Mock; |
| 40 | +import org.mockito.Mockito; |
| 41 | +import org.mockito.MockitoAnnotations; |
| 42 | +import software.amazon.awssdk.regions.Region; |
| 43 | +import software.amazon.jdbc.ConnectionPlugin; |
| 44 | +import software.amazon.jdbc.ConnectionPluginChainBuilder; |
| 45 | +import software.amazon.jdbc.ConnectionProvider; |
| 46 | +import software.amazon.jdbc.HostSpec; |
| 47 | +import software.amazon.jdbc.HostSpecBuilder; |
| 48 | +import software.amazon.jdbc.JdbcCallable; |
| 49 | +import software.amazon.jdbc.PluginManagerService; |
| 50 | +import software.amazon.jdbc.PluginService; |
| 51 | +import software.amazon.jdbc.PropertyDefinition; |
| 52 | +import software.amazon.jdbc.dialect.Dialect; |
| 53 | +import software.amazon.jdbc.hostavailability.SimpleHostAvailabilityStrategy; |
| 54 | +import software.amazon.jdbc.plugin.TokenInfo; |
| 55 | +import software.amazon.jdbc.util.FullServicesContainer; |
| 56 | +import software.amazon.jdbc.util.IamAuthUtils; |
| 57 | +import software.amazon.jdbc.util.telemetry.TelemetryContext; |
| 58 | +import software.amazon.jdbc.util.telemetry.TelemetryFactory; |
| 59 | +import software.amazon.jdbc.util.telemetry.TelemetryTraceLevel; |
| 60 | + |
| 61 | +class DsqlIamConnectionPluginTest { |
| 62 | + |
| 63 | + private static final Region TEST_REGION = Region.US_EAST_1; |
| 64 | + private static final String TEST_HOSTNAME = String.format("foo0bar1baz2quux3quuux4.dsql.%s.on.aws", TEST_REGION); |
| 65 | + private static final int TEST_PORT = 5432; |
| 66 | + |
| 67 | + private static final String DRIVER_PROTOCOL = "jdbc:postgresql:"; |
| 68 | + private static final HostSpec HOST_SPEC = new HostSpecBuilder(new SimpleHostAvailabilityStrategy()) |
| 69 | + .host(TEST_HOSTNAME).port(TEST_PORT).build(); |
| 70 | + |
| 71 | + private static final String DEFAULT_USERNAME = "admin"; |
| 72 | + |
| 73 | + private final Properties props = new Properties(); |
| 74 | + |
| 75 | + static void assertTokenContainsProperties(final String token, final String hostname, final String username) { |
| 76 | + assertNotNull(token); |
| 77 | + assertFalse(token.isEmpty()); |
| 78 | + |
| 79 | + assertTrue(token.contains(hostname)); |
| 80 | + |
| 81 | + final String expectedAction; |
| 82 | + if (username.equals("admin")) { |
| 83 | + expectedAction = "DbConnectAdmin"; |
| 84 | + } else { |
| 85 | + expectedAction = "DbConnect"; |
| 86 | + } |
| 87 | + |
| 88 | + // Include the ampersand to ensure the complete action is compared. |
| 89 | + assertTrue(token.contains("Action=" + expectedAction + "&")); |
| 90 | + } |
| 91 | + |
| 92 | + private AutoCloseable cleanMocksCallback; |
| 93 | + @Mock private Connection mockConnection; |
| 94 | + @Mock private PluginService mockPluginService; |
| 95 | + @Mock private FullServicesContainer mockServicesContainer; |
| 96 | + @Mock private Dialect mockDialect; |
| 97 | + @Mock private TelemetryFactory mockTelemetryFactory; |
| 98 | + @Mock private TelemetryContext mockTelemetryContext; |
| 99 | + @Mock private JdbcCallable<Connection, SQLException> mockLambda; |
| 100 | + @Mock private ConnectionProvider mockConnectionProvider; |
| 101 | + @Mock private PluginManagerService mockPluginManagerService; |
| 102 | + |
| 103 | + @BeforeEach |
| 104 | + public void init() { |
| 105 | + cleanMocksCallback = MockitoAnnotations.openMocks(this); |
| 106 | + |
| 107 | + IamAuthConnectionPlugin.clearCache(); |
| 108 | + |
| 109 | + props.setProperty(PropertyDefinition.USER.name, DEFAULT_USERNAME); |
| 110 | + props.setProperty("iamRegion", Region.US_EAST_1.toString()); |
| 111 | + props.setProperty(PropertyDefinition.PLUGINS.name, "iamDsql"); |
| 112 | + |
| 113 | + doReturn(mockPluginService).when(mockServicesContainer).getPluginService(); |
| 114 | + doReturn(mockDialect).when(mockPluginService).getDialect(); |
| 115 | + doReturn(TEST_PORT).when(mockDialect).getDefaultPort(); |
| 116 | + doReturn(mockTelemetryFactory).when(mockPluginService).getTelemetryFactory(); |
| 117 | + doReturn(mockTelemetryContext).when(mockTelemetryFactory) |
| 118 | + .openTelemetryContext(anyString(), eq(TelemetryTraceLevel.NESTED)); |
| 119 | + } |
| 120 | + |
| 121 | + @AfterEach |
| 122 | + public void cleanup() throws Exception { |
| 123 | + cleanMocksCallback.close(); |
| 124 | + } |
| 125 | + |
| 126 | + @SuppressWarnings("resource") // Prevent Mockito warning when mocking closeable return type. |
| 127 | + private void assertPluginProvidesDsqlTokens(final ConnectionPlugin plugin, final String username) |
| 128 | + throws SQLException { |
| 129 | + Mockito.doReturn(mockConnection).when(mockLambda).call(); |
| 130 | + |
| 131 | + plugin |
| 132 | + .connect(DRIVER_PROTOCOL, HOST_SPEC, props, true, mockLambda) |
| 133 | + .close(); |
| 134 | + |
| 135 | + final String cacheKey = IamAuthUtils.getCacheKey( |
| 136 | + username, |
| 137 | + TEST_HOSTNAME, |
| 138 | + TEST_PORT, |
| 139 | + TEST_REGION); |
| 140 | + |
| 141 | + final TokenInfo info = IamAuthCacheHolder.tokenCache.get(cacheKey); |
| 142 | + final String token = info.getToken(); |
| 143 | + |
| 144 | + assertTokenContainsProperties(token, TEST_HOSTNAME, username); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void testDsqlPluginRegistration() throws SQLException { |
| 149 | + ConnectionPluginChainBuilder builder = new ConnectionPluginChainBuilder(); |
| 150 | + |
| 151 | + final List<ConnectionPlugin> result = builder.getPlugins( |
| 152 | + mockServicesContainer, |
| 153 | + mockConnectionProvider, |
| 154 | + null, |
| 155 | + mockPluginManagerService, |
| 156 | + props, |
| 157 | + null); |
| 158 | + |
| 159 | + // 2 because default plugin is always included. |
| 160 | + assertEquals(2, result.size()); |
| 161 | + final ConnectionPlugin plugin = result.get(0); |
| 162 | + |
| 163 | + assertInstanceOf(IamAuthConnectionPlugin.class, plugin); |
| 164 | + assertPluginProvidesDsqlTokens(plugin, DEFAULT_USERNAME); |
| 165 | + } |
| 166 | + |
| 167 | + @ParameterizedTest |
| 168 | + @ValueSource(strings = {REGULAR_USER, ADMIN_USER}) |
| 169 | + public void testDsqlTokenGeneratedBasedOnUser(final String username) throws SQLException { |
| 170 | + props.setProperty(PropertyDefinition.USER.name, username); |
| 171 | + |
| 172 | + final DsqlIamConnectionPluginFactory factory = new DsqlIamConnectionPluginFactory(); |
| 173 | + final ConnectionPlugin plugin = factory.getInstance(mockPluginService, props); |
| 174 | + assertPluginProvidesDsqlTokens(plugin, username); |
| 175 | + } |
| 176 | +} |
0 commit comments