|
| 1 | +package org.openmetadata.service.util.jdbi; |
| 2 | + |
| 3 | +import java.io.UnsupportedEncodingException; |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URI; |
| 6 | +import java.net.URL; |
| 7 | +import java.net.URLDecoder; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | +import java.util.Map; |
| 11 | +import java.util.Objects; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; |
| 14 | +import software.amazon.awssdk.regions.Region; |
| 15 | +import software.amazon.awssdk.services.rds.RdsUtilities; |
| 16 | +import software.amazon.awssdk.services.rds.model.GenerateAuthenticationTokenRequest; |
| 17 | + |
| 18 | +/** |
| 19 | + * {@link DatabaseAuthenticationProvider} implementation for AWS RDS IAM Auth. |
| 20 | + * |
| 21 | + * @see <a href="https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Enabling.html"></a> |
| 22 | + */ |
| 23 | +public class AwsRdsDatabaseAuthenticationProvider implements DatabaseAuthenticationProvider { |
| 24 | + |
| 25 | + public static final String AWS_REGION = "awsRegion"; |
| 26 | + public static final String ALLOW_PUBLIC_KEY_RETRIEVAL = "allowPublicKeyRetrieval"; |
| 27 | + public static final String PROTOCOL = "https://"; |
| 28 | + |
| 29 | + @Override |
| 30 | + public String authenticate(String jdbcUrl, String username, String password) { |
| 31 | + // !! |
| 32 | + try { |
| 33 | + // Prepare |
| 34 | + URI uri = URI.create(PROTOCOL + removeProtocolFrom(jdbcUrl)); |
| 35 | + Map<String, String> queryParams = parseQueryParams(uri.toURL()); |
| 36 | + |
| 37 | + // Set |
| 38 | + String awsRegion = queryParams.get(AWS_REGION); |
| 39 | + String allowPublicKeyRetrieval = queryParams.get(ALLOW_PUBLIC_KEY_RETRIEVAL); |
| 40 | + |
| 41 | + // Validate |
| 42 | + Objects.requireNonNull(awsRegion, "Parameter `awsRegion` shall be provided in the jdbc url."); |
| 43 | + Objects.requireNonNull( |
| 44 | + allowPublicKeyRetrieval, "Parameter `allowPublicKeyRetrieval` shall be provided in the jdbc url."); |
| 45 | + |
| 46 | + // Prepare request |
| 47 | + GenerateAuthenticationTokenRequest request = |
| 48 | + GenerateAuthenticationTokenRequest.builder() |
| 49 | + .credentialsProvider(DefaultCredentialsProvider.create()) |
| 50 | + .hostname(uri.getHost()) |
| 51 | + .port(uri.getPort()) |
| 52 | + .username(username) |
| 53 | + .build(); |
| 54 | + |
| 55 | + // Return token |
| 56 | + return RdsUtilities.builder().region(Region.of(awsRegion)).build().generateAuthenticationToken(request); |
| 57 | + |
| 58 | + } catch (MalformedURLException | UnsupportedEncodingException e) { |
| 59 | + // Throw |
| 60 | + throw new DatabaseAuthenticationProviderException(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @NotNull |
| 65 | + private static String removeProtocolFrom(String jdbcUrl) { |
| 66 | + return jdbcUrl.substring(jdbcUrl.indexOf("://") + 3); |
| 67 | + } |
| 68 | + |
| 69 | + private Map<String, String> parseQueryParams(URL url) throws UnsupportedEncodingException { |
| 70 | + // Prepare |
| 71 | + Map<String, String> query_pairs = new LinkedHashMap<>(); |
| 72 | + String query = url.getQuery(); |
| 73 | + String[] pairs = query.split("&"); |
| 74 | + |
| 75 | + // Loop |
| 76 | + for (String pair : pairs) { |
| 77 | + int idx = pair.indexOf("="); |
| 78 | + // Add |
| 79 | + query_pairs.put( |
| 80 | + URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8), |
| 81 | + URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8)); |
| 82 | + } |
| 83 | + // Return |
| 84 | + return query_pairs; |
| 85 | + } |
| 86 | +} |
0 commit comments