Skip to content

Commit fdb4c1c

Browse files
fix: cn and gov endpoint support (#244)
1 parent 8e62769 commit fdb4c1c

File tree

6 files changed

+385
-136
lines changed

6 files changed

+385
-136
lines changed

common/lib/host_list_provider/rds_host_list_provider.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
7777
this.initialHost = this.initialHostList[0];
7878
this.hostListProviderService.setInitialConnectionHostInfo(this.initialHost);
7979
this.refreshRateNano = WrapperProperties.CLUSTER_TOPOLOGY_REFRESH_RATE_MS.get(this.properties) * 1000000;
80-
this.rdsUrlType = this.rdsHelper.identifyRdsType(this.originalUrl);
80+
this.rdsUrlType = this.rdsHelper.identifyRdsType(this.initialHost.host);
8181
}
8282

8383
init(): void {
@@ -104,12 +104,12 @@ export class RdsHostListProvider implements DynamicHostListProvider {
104104
// identification
105105
this.clusterId = this.initialHost.url;
106106
} else if (this.rdsUrlType.isRds) {
107-
const clusterSuggestedResult: ClusterSuggestedResult | null = this.getSuggestedClusterId(this.initialHost.url);
107+
const clusterSuggestedResult: ClusterSuggestedResult | null = this.getSuggestedClusterId(this.initialHost.host);
108108
if (clusterSuggestedResult && clusterSuggestedResult.clusterId) {
109109
this.clusterId = clusterSuggestedResult.clusterId;
110110
this.isPrimaryClusterId = clusterSuggestedResult.isPrimaryClusterId;
111111
} else {
112-
const clusterRdsHostUrl: string | null = this.rdsHelper.getRdsClusterHostUrl(this.initialHost.url);
112+
const clusterRdsHostUrl: string | null = this.rdsHelper.getRdsClusterHostUrl(this.initialHost.host);
113113
if (clusterRdsHostUrl) {
114114
this.clusterId = this.clusterInstanceTemplate.isPortSpecified()
115115
? `${clusterRdsHostUrl}:${this.clusterInstanceTemplate.port}`
@@ -143,7 +143,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
143143
}
144144

145145
if (client.targetClient) {
146-
return dialect.getHostRole(client.targetClient, this.properties);
146+
return dialect.getHostRole(client.targetClient);
147147
} else {
148148
throw new AwsWrapperError(Messages.get("AwsClient targetClient not defined."));
149149
}
@@ -153,7 +153,7 @@ export class RdsHostListProvider implements DynamicHostListProvider {
153153
if (!this.isTopologyAwareDatabaseDialect(dialect)) {
154154
throw new TypeError(Messages.get("RdsHostListProvider.incorrectDialect"));
155155
}
156-
const instanceName = await dialect.identifyConnection(targetClient, this.properties);
156+
const instanceName = await dialect.identifyConnection(targetClient);
157157

158158
return this.refresh(targetClient).then((topology) => {
159159
const matches = topology.filter((host) => host.hostId === instanceName);
@@ -215,17 +215,17 @@ export class RdsHostListProvider implements DynamicHostListProvider {
215215
}
216216
}
217217

218-
private getSuggestedClusterId(url: string): ClusterSuggestedResult | null {
218+
private getSuggestedClusterId(host: string): ClusterSuggestedResult | null {
219219
for (const [key, hosts] of RdsHostListProvider.topologyCache.getEntries()) {
220220
const isPrimaryCluster: boolean = RdsHostListProvider.primaryClusterIdCache.get(key, false, this.suggestedClusterIdRefreshRateNano) ?? false;
221-
if (key === url) {
222-
return new ClusterSuggestedResult(url, isPrimaryCluster);
221+
if (key === host) {
222+
return new ClusterSuggestedResult(host, isPrimaryCluster);
223223
}
224224

225225
if (hosts) {
226-
for (const host of hosts) {
227-
if (host.url === url) {
228-
logger.debug(Messages.get("RdsHostListProvider.suggestedClusterId", key, url));
226+
for (const hostInfo of hosts) {
227+
if (hostInfo.host === host) {
228+
logger.debug(Messages.get("RdsHostListProvider.suggestedClusterId", key, host));
229229
return new ClusterSuggestedResult(key, isPrimaryCluster);
230230
}
231231
}

common/lib/utils/connection_url_parser.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export abstract class ConnectionUrlParser {
3535

3636
private getHostInfo(host: string, port: string | undefined, role: HostRole, builder: HostInfoBuilder): HostInfo {
3737
const hostId = ConnectionUrlParser.rdsUtils.getRdsInstanceId(host);
38-
3938
builder = builder.withHost(host).withRole(role);
4039

4140
if (hostId) {

common/lib/utils/rds_utils.ts

Lines changed: 166 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
import { RdsUrlType } from "./rds_url_type";
18+
import { equalsIgnoreCase } from "./utils";
1819

1920
export class RdsUtils {
2021
// Aurora DB clusters support different endpoints. More details about Aurora RDS endpoints
@@ -53,27 +54,49 @@ export class RdsUtils {
5354
//
5455
// Instance Endpoint: <instance-name>.<xyz>.rds.<aws-region>.amazonaws.com.cn
5556
// Example: test-postgres-instance-1.123456789012.rds.cn-northwest-1.amazonaws.com.cn
57+
//
58+
//
59+
// Governmental endpoints
60+
// https://aws.amazon.com/compliance/fips/#FIPS_Endpoints_by_Service
61+
// https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/Region.html
5662

5763
private static readonly AURORA_DNS_PATTERN =
58-
/(?<instance>.+)\.(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)/i;
59-
private static readonly AURORA_INSTANCE_PATTERN = /(?<instance>.+)\.(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)/i;
64+
/^(?<instance>.+)\.(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)$/i;
65+
private static readonly AURORA_INSTANCE_PATTERN = /^(?<instance>.+)\.(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)$/i;
6066
private static readonly AURORA_CLUSTER_PATTERN =
61-
/(?<instance>.+)\.(?<dns>cluster-|cluster-ro-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)/i;
67+
/^(?<instance>.+)\.(?<dns>cluster-|cluster-ro-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)$/i;
6268
private static readonly AURORA_CUSTOM_CLUSTER_PATTERN =
63-
/(?<instance>.+)\.(?<dns>cluster-custom-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)/i;
69+
/^(?<instance>.+)\.(?<dns>cluster-custom-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)$/i;
6470
private static readonly AURORA_PROXY_DNS_PATTERN =
65-
/(?<instance>.+)\.(?<dns>proxy-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)/i;
71+
/^(?<instance>.+)\.(?<dns>proxy-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com)$/i;
6672
private static readonly AURORA_CHINA_DNS_PATTERN =
67-
/(?<instance>.+)\.(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.amazonaws\.com\.cn)/i;
73+
/^(?<instance>.+)\.(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.amazonaws\.com\.cn)$/i;
74+
private static readonly AURORA_OLD_CHINA_DNS_PATTERN =
75+
/^(?<instance>.+)\.(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-)?(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com\.cn)$/i;
6876
private static readonly AURORA_CHINA_INSTANCE_PATTERN =
69-
/(?<instance>.+)\.(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com\.cn)/i;
77+
/^(?<instance>.+)\.(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.amazonaws\.com\.cn)$/i;
78+
private static readonly AURORA_OLD_CHINA_INSTANCE_PATTERN =
79+
/^(?<instance>.+)\.(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com\.cn)$/i;
7080
private static readonly AURORA_CHINA_CLUSTER_PATTERN =
71-
/(?<instance>.+)\.(?<dns>cluster-|cluster-ro-)+(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.amazonaws\.com\.cn)/i;
81+
/^(?<instance>.+)\.(?<dns>cluster-|cluster-ro-)+(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.amazonaws\.com\.cn)$/i;
82+
private static readonly AURORA_OLD_CHINA_CLUSTER_PATTERN =
83+
/^(?<instance>.+)\.(?<dns>cluster-|cluster-ro-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com\.cn)$/i;
7284
private static readonly AURORA_CHINA_CUSTOM_CLUSTER_PATTERN =
73-
/(?<instance>.+)\.(?<dns>cluster-custom-)+(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.amazonaws\.com\.cn)/i;
85+
/^(?<instance>.+)\.(?<dns>cluster-custom-)+(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.amazonaws\.com\.cn)$/i;
86+
private static readonly AURORA_OLD_CHINA_CUSTOM_CLUSTER_PATTERN =
87+
/^(?<instance>.+)\.(?<dns>cluster-custom-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-]+)\.rds\.amazonaws\.com\.cn)$/i;
7488
private static readonly AURORA_CHINA_PROXY_DNS_PATTERN =
75-
/(?<instance>.+)\.(?<dns>proxy-)+(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-])+\.amazonaws\.com\.cn)/i;
76-
private static readonly ELB_PATTERN = /(?<instance>.+)\.elb\.((?<region>[a-zA-Z0-9-]+)\.amazonaws\.com)/i;
89+
/^(?<instance>.+)\.(?<dns>proxy-)+(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-])+\.amazonaws\.com\.cn)$/i;
90+
private static readonly AURORA_OLD_CHINA_PROXY_DNS_PATTERN =
91+
/^(?<instance>.+)\.(?<dns>proxy-)+(?<domain>[a-zA-Z0-9]+\.(?<region>[a-zA-Z0-9-])+\.rds\.amazonaws\.com\.cn)$/i;
92+
93+
private static readonly AURORA_GOV_DNS_PATTERN =
94+
/^(?<instance>.+)\.(?<dns>proxy-|cluster-|cluster-ro-|cluster-custom-|shardgrp-)?(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.(amazonaws\.com|c2s\.ic\.gov|sc2s\.sgov\.gov))$/i;
95+
96+
private static readonly AURORA_GOV_CLUSTER_PATTERN =
97+
/^(?<instance>.+)\.(?<dns>cluster-|cluster-ro-)+(?<domain>[a-zA-Z0-9]+\.rds\.(?<region>[a-zA-Z0-9-]+)\.(amazonaws\.com|c2s\.ic\.gov|sc2s\.sgov\.gov))$/i;
98+
99+
private static readonly ELB_PATTERN = /^(?<instance>.+)\.elb\.((?<region>[a-zA-Z0-9-]+)\.amazonaws\.com)$/i;
77100
private static readonly IP_V4 =
78101
/^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){1}(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/i;
79102
private static readonly IP_V6 = /^[0-9a-fA-F]{1,4}(:[0-9a-fA-F]{1,4}){7}$/i;
@@ -85,109 +108,136 @@ export class RdsUtils {
85108
static readonly DOMAIN_GROUP = "domain";
86109
static readonly REGION_GROUP = "region";
87110

88-
public isRdsClusterDns(host: string): boolean | null {
89-
return host.match(RdsUtils.AURORA_CLUSTER_PATTERN) != null || host.match(RdsUtils.AURORA_CHINA_CLUSTER_PATTERN) != null;
111+
private static readonly cachedPatterns = new Map();
112+
private static readonly cachedDnsPatterns = new Map();
113+
114+
public isRdsClusterDns(host: string): boolean {
115+
const dnsGroup = this.getDnsGroup(host);
116+
return equalsIgnoreCase(dnsGroup, "cluster-") || equalsIgnoreCase(dnsGroup, "cluster-ro-");
90117
}
91118

92119
public isRdsCustomClusterDns(host: string): boolean {
93-
return host.match(RdsUtils.AURORA_CUSTOM_CLUSTER_PATTERN) != null || host.match(RdsUtils.AURORA_CHINA_CUSTOM_CLUSTER_PATTERN) != null;
120+
const dnsGroup = this.getDnsGroup(host);
121+
return equalsIgnoreCase(dnsGroup, "cluster-custom-");
94122
}
95123

96124
public isRdsDns(host: string) {
97-
return host.match(RdsUtils.AURORA_DNS_PATTERN) || host.match(RdsUtils.AURORA_CHINA_DNS_PATTERN);
125+
const matcher = this.cacheMatcher(
126+
host,
127+
RdsUtils.AURORA_DNS_PATTERN,
128+
RdsUtils.AURORA_CHINA_DNS_PATTERN,
129+
RdsUtils.AURORA_OLD_CHINA_DNS_PATTERN,
130+
RdsUtils.AURORA_GOV_DNS_PATTERN
131+
);
132+
const group = this.getRegexGroup(matcher, RdsUtils.DNS_GROUP);
133+
134+
if (group) {
135+
RdsUtils.cachedDnsPatterns.set(host, group);
136+
}
137+
138+
return matcher != null;
98139
}
99140

100141
public isRdsInstance(host: string): boolean {
101-
return host.match(RdsUtils.AURORA_INSTANCE_PATTERN) !== null || host.match(RdsUtils.AURORA_CHINA_INSTANCE_PATTERN) !== null;
142+
return !this.getDnsGroup(host) && this.isRdsDns(host);
102143
}
103144

104145
isRdsProxyDns(host: string) {
105-
return host.match(RdsUtils.AURORA_PROXY_DNS_PATTERN) || host.match(RdsUtils.AURORA_CHINA_PROXY_DNS_PATTERN);
106-
}
107-
108-
public isElbUrl(host: string) {
109-
return host.match(RdsUtils.ELB_PATTERN);
146+
const dnsGroup = this.getDnsGroup(host);
147+
return dnsGroup && dnsGroup.startsWith("proxy-");
110148
}
111149

112150
public getRdsInstanceId(host: string) {
113151
if (!host) {
114152
return null;
115153
}
116154

117-
const instanceId = (host.match(RdsUtils.AURORA_INSTANCE_PATTERN) || host.match(RdsUtils.AURORA_CHINA_INSTANCE_PATTERN))?.groups?.[
118-
RdsUtils.INSTANCE_GROUP
119-
];
120-
return instanceId ? instanceId : null;
155+
const matcher = this.cacheMatcher(
156+
host,
157+
RdsUtils.AURORA_DNS_PATTERN,
158+
RdsUtils.AURORA_CHINA_DNS_PATTERN,
159+
RdsUtils.AURORA_OLD_CHINA_DNS_PATTERN,
160+
RdsUtils.AURORA_GOV_DNS_PATTERN
161+
);
162+
if (this.getRegexGroup(matcher, RdsUtils.DNS_GROUP)) {
163+
return this.getRegexGroup(matcher, RdsUtils.INSTANCE_GROUP);
164+
}
165+
166+
return null;
121167
}
122168

123169
public getRdsInstanceHostPattern(host: string): string {
124-
if (host == null) {
170+
if (!host) {
125171
return "?";
126172
}
127173

128-
const matcher = host.match(RdsUtils.AURORA_DNS_PATTERN);
129-
if (matcher !== null && matcher.groups !== undefined) {
130-
return "?." + matcher.groups[RdsUtils.DOMAIN_GROUP];
131-
}
132-
const chinaMatcher = host.match(RdsUtils.AURORA_CHINA_DNS_PATTERN);
133-
if (chinaMatcher !== null && chinaMatcher.groups !== undefined) {
134-
return "?." + chinaMatcher.groups[RdsUtils.DOMAIN_GROUP];
135-
}
136-
return "?";
174+
const matcher = this.cacheMatcher(
175+
host,
176+
RdsUtils.AURORA_DNS_PATTERN,
177+
RdsUtils.AURORA_CHINA_DNS_PATTERN,
178+
RdsUtils.AURORA_OLD_CHINA_DNS_PATTERN,
179+
RdsUtils.AURORA_GOV_DNS_PATTERN
180+
);
181+
const group = this.getRegexGroup(matcher, RdsUtils.DOMAIN_GROUP);
182+
return group ? `?.${group}` : "?";
137183
}
138184

139185
public getRdsRegion(host: string): string | null {
140-
const matcher = host.match(RdsUtils.AURORA_DNS_PATTERN);
141-
if (matcher !== null && matcher.groups !== undefined) {
142-
return matcher.groups[RdsUtils.REGION_GROUP];
186+
if (!host) {
187+
return null;
143188
}
144-
const chinaMatcher = host.match(RdsUtils.AURORA_CHINA_DNS_PATTERN);
145-
if (chinaMatcher !== null && chinaMatcher.groups !== undefined) {
146-
return chinaMatcher.groups[RdsUtils.REGION_GROUP];
189+
190+
const matcher = this.cacheMatcher(
191+
host,
192+
RdsUtils.AURORA_DNS_PATTERN,
193+
RdsUtils.AURORA_CHINA_DNS_PATTERN,
194+
RdsUtils.AURORA_OLD_CHINA_DNS_PATTERN,
195+
RdsUtils.AURORA_GOV_DNS_PATTERN
196+
);
197+
198+
const group = this.getRegexGroup(matcher, RdsUtils.REGION_GROUP);
199+
if (group) {
200+
return group;
147201
}
202+
148203
const elbMatcher = host.match(RdsUtils.ELB_PATTERN);
149-
if (elbMatcher !== null && elbMatcher.groups !== undefined) {
150-
return elbMatcher.groups[RdsUtils.REGION_GROUP];
204+
if (elbMatcher && elbMatcher.length > 0) {
205+
return this.getRegexGroup(elbMatcher, RdsUtils.REGION_GROUP);
151206
}
207+
152208
return null;
153209
}
154210

155-
public isWriterClusterDns(host: string) {
156-
if (host === undefined) {
157-
return false;
158-
}
159-
160-
const matcher = host.match(RdsUtils.AURORA_CLUSTER_PATTERN);
161-
if (matcher !== null && matcher.groups !== undefined) {
162-
return "cluster-" === matcher.groups[RdsUtils.DNS_GROUP];
163-
}
164-
const chinaMatcher = host.match(RdsUtils.AURORA_CHINA_CLUSTER_PATTERN);
165-
if (chinaMatcher !== null && chinaMatcher.groups !== undefined) {
166-
return "cluster-" == chinaMatcher.groups[RdsUtils.DNS_GROUP];
167-
}
168-
return false;
211+
public isWriterClusterDns(host: string): boolean {
212+
const dnsGroup = this.getDnsGroup(host);
213+
return equalsIgnoreCase(dnsGroup, "cluster-");
169214
}
170215

171216
public isReaderClusterDns(host: string): boolean {
172-
const matcher = host.match(RdsUtils.AURORA_CLUSTER_PATTERN);
173-
if (matcher !== null && matcher.groups !== undefined) {
174-
return "cluster-ro-" == matcher.groups[RdsUtils.DNS_GROUP];
175-
}
176-
const chinaMatcher = host.match(RdsUtils.AURORA_CHINA_CLUSTER_PATTERN);
177-
if (chinaMatcher !== null && chinaMatcher.groups !== undefined) {
178-
return "cluster-ro-" == chinaMatcher.groups[RdsUtils.DNS_GROUP];
179-
}
180-
return false;
217+
const dnsGroup = this.getDnsGroup(host);
218+
return equalsIgnoreCase(dnsGroup, "cluster-ro-");
181219
}
182220

183221
public getRdsClusterHostUrl(host: string): string | null {
222+
if (!host) {
223+
return null;
224+
}
225+
184226
const matcher = host.match(RdsUtils.AURORA_CLUSTER_PATTERN);
185227
if (matcher) {
186228
return host.replace(RdsUtils.AURORA_CLUSTER_PATTERN, "$<instance>.cluster-$<domain>");
187229
}
188230
const chinaMatcher = host.match(RdsUtils.AURORA_CHINA_CLUSTER_PATTERN);
189231
if (chinaMatcher) {
190-
return host.replace(RdsUtils.AURORA_CHINA_CLUSTER_PATTERN, "${<instance>.cluster-$<domain>");
232+
return host.replace(RdsUtils.AURORA_CHINA_CLUSTER_PATTERN, "$<instance>.cluster-$<domain>");
233+
}
234+
const oldChinaMatcher = host.match(RdsUtils.AURORA_OLD_CHINA_CLUSTER_PATTERN);
235+
if (oldChinaMatcher) {
236+
return host.replace(RdsUtils.AURORA_OLD_CHINA_CLUSTER_PATTERN, "$<instance>.cluster-$<domain>");
237+
}
238+
const govMatcher = host.match(RdsUtils.AURORA_GOV_CLUSTER_PATTERN);
239+
if (govMatcher) {
240+
return host.replace(RdsUtils.AURORA_GOV_CLUSTER_PATTERN, "$<instance>.cluster-$<domain>");
191241
}
192242
return null;
193243
}
@@ -259,4 +309,53 @@ export class RdsUtils {
259309
}
260310
return hostAndPort.substring(0, index);
261311
}
312+
313+
private getDnsGroup(host: string): string | null {
314+
if (!host) {
315+
return null;
316+
}
317+
318+
const group = RdsUtils.cachedDnsPatterns.get(host);
319+
if (group) {
320+
return group;
321+
}
322+
323+
const matcher = this.cacheMatcher(
324+
host,
325+
RdsUtils.AURORA_DNS_PATTERN,
326+
RdsUtils.AURORA_CHINA_DNS_PATTERN,
327+
RdsUtils.AURORA_OLD_CHINA_DNS_PATTERN,
328+
RdsUtils.AURORA_GOV_DNS_PATTERN
329+
);
330+
return this.getRegexGroup(matcher, RdsUtils.DNS_GROUP);
331+
}
332+
333+
private getRegexGroup(matcher: RegExpMatchArray, groupName: string): string | null {
334+
if (!matcher) {
335+
return null;
336+
}
337+
338+
return matcher.groups?.[groupName] ?? null;
339+
}
340+
341+
private cacheMatcher(host: string, ...patterns: RegExp[]) {
342+
let matcher = null;
343+
for (const pattern of patterns) {
344+
matcher = RdsUtils.cachedPatterns.get(host);
345+
if (matcher) {
346+
return matcher;
347+
}
348+
matcher = host.match(pattern);
349+
if (matcher && matcher.length > 0) {
350+
RdsUtils.cachedPatterns.set(host, matcher);
351+
return matcher;
352+
}
353+
}
354+
return null;
355+
}
356+
357+
static clearCache() {
358+
RdsUtils.cachedPatterns.clear();
359+
RdsUtils.cachedDnsPatterns.clear();
360+
}
262361
}

common/lib/utils/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,7 @@ export function logAndThrowError(message: string) {
7272
logger.error(message);
7373
throw new AwsWrapperError(message);
7474
}
75+
76+
export function equalsIgnoreCase(value1: string | null, value2: string | null): boolean {
77+
return value1 != null && value2 != null && value1.localeCompare(value2, undefined, { sensitivity: "accent" }) === 0;
78+
}

0 commit comments

Comments
 (0)