|
| 1 | +/* |
| 2 | + * AWS JDBC Proxy Driver |
| 3 | + * Copyright Amazon.com Inc. or affiliates. |
| 4 | + * See the LICENSE file in the project root for more information. |
| 5 | + */ |
| 6 | + |
| 7 | +package integration.util; |
| 8 | + |
| 9 | +import java.io.BufferedReader; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.net.URL; |
| 12 | +import java.net.UnknownHostException; |
| 13 | +import java.time.Duration; |
| 14 | +import java.util.Optional; |
| 15 | +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; |
| 16 | +import software.amazon.awssdk.core.waiters.WaiterResponse; |
| 17 | +import software.amazon.awssdk.regions.Region; |
| 18 | +import software.amazon.awssdk.services.ec2.Ec2Client; |
| 19 | +import software.amazon.awssdk.services.ec2.model.Ec2Exception; |
| 20 | +import software.amazon.awssdk.services.rds.RdsClient; |
| 21 | +import software.amazon.awssdk.services.rds.model.CreateDbClusterRequest; |
| 22 | +import software.amazon.awssdk.services.rds.model.CreateDbInstanceRequest; |
| 23 | +import software.amazon.awssdk.services.rds.model.DeleteDbInstanceRequest; |
| 24 | +import software.amazon.awssdk.services.rds.model.DescribeDbInstancesResponse; |
| 25 | +import software.amazon.awssdk.services.rds.model.Filter; |
| 26 | +import software.amazon.awssdk.services.rds.model.Tag; |
| 27 | +import software.amazon.awssdk.services.rds.waiters.RdsWaiter; |
| 28 | +import software.aws.rds.jdbc.proxydriver.util.StringUtils; |
| 29 | + |
| 30 | +/** |
| 31 | + * Creates and destroys AWS RDS Clusters and Instances. |
| 32 | + * To use this functionality the following environment variables must be defined: |
| 33 | + * - AWS_ACCESS_KEY_ID |
| 34 | + * - AWS_SECRET_ACCESS_KEY |
| 35 | + */ |
| 36 | +public class AuroraTestUtility { |
| 37 | + // Default values |
| 38 | + private String dbUsername = "my_test_username"; |
| 39 | + private String dbPassword = "my_test_password"; |
| 40 | + private String dbName = "test"; |
| 41 | + private String dbIdentifier = "test-identifier"; |
| 42 | + private String dbEngine = "aurora-postgresql"; |
| 43 | + private String dbInstanceClass = "db.r5.large"; |
| 44 | + private final Region dbRegion; |
| 45 | + private final String dbSecGroup = "default"; |
| 46 | + private int numOfInstances = 5; |
| 47 | + |
| 48 | + private final RdsClient rdsClient; |
| 49 | + private final Ec2Client ec2Client; |
| 50 | + |
| 51 | + private static final String DUPLICATE_IP_ERROR_CODE = "InvalidPermission.Duplicate"; |
| 52 | + |
| 53 | + /** |
| 54 | + * Initializes an AmazonRDS & AmazonEC2 client. RDS client used to create/destroy clusters & |
| 55 | + * instances. EC2 client used to add/remove IP from security group. |
| 56 | + */ |
| 57 | + public AuroraTestUtility() { |
| 58 | + this(Region.US_EAST_1, DefaultCredentialsProvider.create()); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Initializes an AmazonRDS & AmazonEC2 client. |
| 63 | + * |
| 64 | + * @param region define AWS Regions, refer to |
| 65 | + * https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html |
| 66 | + */ |
| 67 | + public AuroraTestUtility(Region region) { |
| 68 | + this(region, DefaultCredentialsProvider.create()); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Initializes an AmazonRDS & AmazonEC2 client. |
| 73 | + * |
| 74 | + * @param region define AWS Regions, refer to |
| 75 | + * https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html |
| 76 | + */ |
| 77 | + public AuroraTestUtility(String region) { |
| 78 | + this(getRegionInternal(region), DefaultCredentialsProvider.create()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Initializes an AmazonRDS & AmazonEC2 client. |
| 83 | + * |
| 84 | + * @param region define AWS Regions, refer to |
| 85 | + * https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.RegionsAndAvailabilityZones.html |
| 86 | + * @param credentials Specific AWS credential provider |
| 87 | + */ |
| 88 | + public AuroraTestUtility(Region region, DefaultCredentialsProvider credentials) { |
| 89 | + dbRegion = region; |
| 90 | + |
| 91 | + rdsClient = RdsClient.builder().region(dbRegion).credentialsProvider(credentials).build(); |
| 92 | + |
| 93 | + ec2Client = Ec2Client.builder().region(dbRegion).credentialsProvider(credentials).build(); |
| 94 | + } |
| 95 | + |
| 96 | + public Region getRegion(String rdsRegion) { |
| 97 | + return getRegionInternal(rdsRegion); |
| 98 | + } |
| 99 | + |
| 100 | + protected static Region getRegionInternal(String rdsRegion) { |
| 101 | + Optional<Region> regionOptional = |
| 102 | + Region.regions().stream().filter(r -> r.id().equalsIgnoreCase(rdsRegion)).findFirst(); |
| 103 | + |
| 104 | + if (regionOptional.isPresent()) { |
| 105 | + return regionOptional.get(); |
| 106 | + } |
| 107 | + throw new IllegalArgumentException(String.format("Unknown AWS region '%s'.", rdsRegion)); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Creates RDS Cluster/Instances and waits until they are up, and proper IP whitelisting for |
| 112 | + * databases. |
| 113 | + * |
| 114 | + * @param username Master username for access to database |
| 115 | + * @param password Master password for access to database |
| 116 | + * @param name Database name |
| 117 | + * @param identifier Database cluster identifier |
| 118 | + * @param engine Database engine to use, refer to |
| 119 | + * https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Welcome.html |
| 120 | + * @param instanceClass instance class, refer to |
| 121 | + * https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Concepts.DBInstanceClass.html |
| 122 | + * @param instances number of instances to spin up |
| 123 | + * @return An endpoint for one of the instances |
| 124 | + * @throws InterruptedException when clusters have not started after 30 minutes |
| 125 | + */ |
| 126 | + public String createCluster( |
| 127 | + String username, |
| 128 | + String password, |
| 129 | + String name, |
| 130 | + String identifier, |
| 131 | + String engine, |
| 132 | + String instanceClass, |
| 133 | + int instances) |
| 134 | + throws InterruptedException { |
| 135 | + dbUsername = username; |
| 136 | + dbPassword = password; |
| 137 | + dbName = name; |
| 138 | + dbIdentifier = identifier; |
| 139 | + dbEngine = engine; |
| 140 | + dbInstanceClass = instanceClass; |
| 141 | + numOfInstances = instances; |
| 142 | + return createCluster(); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Creates RDS Cluster/Instances and waits until they are up, and proper IP whitelisting for |
| 147 | + * databases. |
| 148 | + * |
| 149 | + * @param username Master username for access to database |
| 150 | + * @param password Master password for access to database |
| 151 | + * @param name Database name |
| 152 | + * @param identifier Database identifier |
| 153 | + * @return An endpoint for one of the instances |
| 154 | + * @throws InterruptedException when clusters have not started after 30 minutes |
| 155 | + */ |
| 156 | + public String createCluster(String username, String password, String name, String identifier) |
| 157 | + throws InterruptedException { |
| 158 | + dbUsername = username; |
| 159 | + dbPassword = password; |
| 160 | + dbName = name; |
| 161 | + dbIdentifier = identifier; |
| 162 | + return createCluster(); |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Creates RDS Cluster/Instances and waits until they are up, and proper IP whitelisting for |
| 167 | + * databases. |
| 168 | + * |
| 169 | + * @return An endpoint for one of the instances |
| 170 | + * @throws InterruptedException when clusters have not started after 30 minutes |
| 171 | + */ |
| 172 | + public String createCluster() throws InterruptedException { |
| 173 | + // Create Cluster |
| 174 | + final Tag testRunnerTag = Tag.builder().key("env").value("test-runner").build(); |
| 175 | + |
| 176 | + final CreateDbClusterRequest dbClusterRequest = |
| 177 | + CreateDbClusterRequest.builder() |
| 178 | + .dbClusterIdentifier(dbIdentifier) |
| 179 | + .databaseName(dbName) |
| 180 | + .masterUsername(dbUsername) |
| 181 | + .masterUserPassword(dbPassword) |
| 182 | + .sourceRegion(dbRegion.id()) |
| 183 | + .enableIAMDatabaseAuthentication(true) |
| 184 | + .engine(dbEngine) |
| 185 | + .storageEncrypted(true) |
| 186 | + .tags(testRunnerTag) |
| 187 | + .build(); |
| 188 | + |
| 189 | + rdsClient.createDBCluster(dbClusterRequest); |
| 190 | + |
| 191 | + // Create Instances |
| 192 | + for (int i = 1; i <= numOfInstances; i++) { |
| 193 | + rdsClient.createDBInstance( |
| 194 | + CreateDbInstanceRequest.builder() |
| 195 | + .dbClusterIdentifier(dbIdentifier) |
| 196 | + .dbInstanceIdentifier(dbIdentifier + "-" + i) |
| 197 | + .dbClusterIdentifier(dbIdentifier) |
| 198 | + .dbInstanceClass(dbInstanceClass) |
| 199 | + .engine(dbEngine) |
| 200 | + .publiclyAccessible(true) |
| 201 | + .tags(testRunnerTag) |
| 202 | + .build()); |
| 203 | + } |
| 204 | + |
| 205 | + // Wait for all instances to be up |
| 206 | + final RdsWaiter waiter = rdsClient.waiter(); |
| 207 | + WaiterResponse<DescribeDbInstancesResponse> waiterResponse = |
| 208 | + waiter.waitUntilDBInstanceAvailable( |
| 209 | + (requestBuilder) -> |
| 210 | + requestBuilder.filters( |
| 211 | + Filter.builder().name("db-cluster-id").values(dbIdentifier).build()), |
| 212 | + (configurationBuilder) -> configurationBuilder.waitTimeout(Duration.ofMinutes(30))); |
| 213 | + |
| 214 | + if (waiterResponse.matched().exception().isPresent()) { |
| 215 | + deleteCluster(); |
| 216 | + throw new InterruptedException( |
| 217 | + "Unable to start AWS RDS Cluster & Instances after waiting for 30 minutes"); |
| 218 | + } |
| 219 | + |
| 220 | + final DescribeDbInstancesResponse dbInstancesResult = |
| 221 | + rdsClient.describeDBInstances( |
| 222 | + (builder) -> |
| 223 | + builder.filters( |
| 224 | + Filter.builder().name("db-cluster-id").values(dbIdentifier).build())); |
| 225 | + final String endpoint = dbInstancesResult.dbInstances().get(0).endpoint().address(); |
| 226 | + return endpoint.substring(endpoint.indexOf('.') + 1); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Gets public IP. |
| 231 | + * |
| 232 | + * @return public IP of user |
| 233 | + * @throws UnknownHostException when checkip host isn't available |
| 234 | + */ |
| 235 | + public String getPublicIPAddress() throws UnknownHostException { |
| 236 | + String ip; |
| 237 | + try { |
| 238 | + URL ipChecker = new URL("http://checkip.amazonaws.com"); |
| 239 | + BufferedReader reader = new BufferedReader(new InputStreamReader(ipChecker.openStream())); |
| 240 | + ip = reader.readLine(); |
| 241 | + } catch (Exception e) { |
| 242 | + throw new UnknownHostException("Unable to get IP"); |
| 243 | + } |
| 244 | + return ip; |
| 245 | + } |
| 246 | + |
| 247 | + /** Authorizes IP to EC2 Security groups for RDS access. */ |
| 248 | + public void ec2AuthorizeIP(String ipAddress) { |
| 249 | + if (StringUtils.isNullOrEmpty(ipAddress)) { |
| 250 | + return; |
| 251 | + } |
| 252 | + try { |
| 253 | + ec2Client.authorizeSecurityGroupIngress( |
| 254 | + (builder) -> |
| 255 | + builder |
| 256 | + .groupName(dbSecGroup) |
| 257 | + .cidrIp(ipAddress + "/32") |
| 258 | + .ipProtocol("-1") // All protocols |
| 259 | + .fromPort(0) // For all ports |
| 260 | + .toPort(65535)); |
| 261 | + } catch (Ec2Exception exception) { |
| 262 | + if (!DUPLICATE_IP_ERROR_CODE.equalsIgnoreCase(exception.awsErrorDetails().errorCode())) { |
| 263 | + throw exception; |
| 264 | + } |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + /** De-authorizes IP from EC2 Security groups. */ |
| 269 | + public void ec2DeauthorizesIP(String ipAddress) { |
| 270 | + if (StringUtils.isNullOrEmpty(ipAddress)) { |
| 271 | + return; |
| 272 | + } |
| 273 | + try { |
| 274 | + ec2Client.revokeSecurityGroupIngress( |
| 275 | + (builder) -> |
| 276 | + builder |
| 277 | + .groupName(dbSecGroup) |
| 278 | + .cidrIp(ipAddress + "/32") |
| 279 | + .ipProtocol("-1") // All protocols |
| 280 | + .fromPort(0) // For all ports |
| 281 | + .toPort(65535)); |
| 282 | + } catch (Ec2Exception exception) { |
| 283 | + // Ignore |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + /** |
| 288 | + * Destroys all instances and clusters. Removes IP from EC2 whitelist. |
| 289 | + * |
| 290 | + * @param identifier database identifier to delete |
| 291 | + */ |
| 292 | + public void deleteCluster(String identifier) { |
| 293 | + dbIdentifier = identifier; |
| 294 | + deleteCluster(); |
| 295 | + } |
| 296 | + |
| 297 | + /** Destroys all instances and clusters. Removes IP from EC2 whitelist. */ |
| 298 | + public void deleteCluster() { |
| 299 | + // Tear down instances |
| 300 | + for (int i = 1; i <= numOfInstances; i++) { |
| 301 | + rdsClient.deleteDBInstance( |
| 302 | + DeleteDbInstanceRequest.builder() |
| 303 | + .dbInstanceIdentifier(dbIdentifier + "-" + i) |
| 304 | + .skipFinalSnapshot(true) |
| 305 | + .build()); |
| 306 | + } |
| 307 | + |
| 308 | + // Tear down cluster |
| 309 | + rdsClient.deleteDBCluster( |
| 310 | + (builder -> builder.skipFinalSnapshot(true).dbClusterIdentifier(dbIdentifier))); |
| 311 | + } |
| 312 | +} |
0 commit comments