1 parent
5138c1a702
commit
70dad4e00b
3 files changed
+44
-6
No files matched your search
@@ -205,6 +205,10 @@ function urlParamsRequireTls(dbType: DatabaseType, params: string): boolean {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isTidbCloudHost(host: string): boolean {
|
||||||
|
return host.toLowerCase().endsWith(".tidbcloud.com");
|
||||||
|
}
|
||||||
|
|
||||||
export function connectionProfileForScheme(scheme: string, preferredProfile?: string): ConnectionProfile | undefined {
|
export function connectionProfileForScheme(scheme: string, preferredProfile?: string): ConnectionProfile | undefined {
|
||||||
if ((scheme === "http" || scheme === "https") && preferredProfile) {
|
if ((scheme === "http" || scheme === "https") && preferredProfile) {
|
||||||
return HTTP_SELECTED_PROFILES[preferredProfile];
|
return HTTP_SELECTED_PROFILES[preferredProfile];
|
||||||
@@ -433,7 +437,11 @@ export function parseConnectionUrl(value: string, preferredProfile?: string): Pa
|
|||||||
password: mysqlCredentials?.password ?? decodeUrlPart(parsed.password),
|
password: mysqlCredentials?.password ?? decodeUrlPart(parsed.password),
|
||||||
database: databaseFromPath(parsed.pathname),
|
database: databaseFromPath(parsed.pathname),
|
||||||
urlParams: effectiveUrlParams,
|
urlParams: effectiveUrlParams,
|
||||||
ssl: scheme === "rediss" || scheme === "https" || urlParamsRequireTls(profile.type, effectiveUrlParams),
|
ssl:
|
||||||
|
scheme === "rediss" ||
|
||||||
|
scheme === "https" ||
|
||||||
|
urlParamsRequireTls(profile.type, effectiveUrlParams) ||
|
||||||
|
(profile.type === "mysql" && isTidbCloudHost(parsed.hostname)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -921,7 +921,9 @@ impl ConnectionConfig {
|
|||||||
return normalize_bare_mysql_url_params(value);
|
return normalize_bare_mysql_url_params(value);
|
||||||
}
|
}
|
||||||
match self.db_type {
|
match self.db_type {
|
||||||
DatabaseType::Mysql => normalize_mysql_url_params(value, self.ssl, self.ca_cert_path.trim().is_empty()),
|
DatabaseType::Mysql => {
|
||||||
|
normalize_mysql_url_params(value, self.mysql_uses_tls(), self.ca_cert_path.trim().is_empty())
|
||||||
|
}
|
||||||
DatabaseType::Doris | DatabaseType::StarRocks | DatabaseType::Databend => {
|
DatabaseType::Doris | DatabaseType::StarRocks | DatabaseType::Databend => {
|
||||||
normalize_bare_mysql_url_params(value)
|
normalize_bare_mysql_url_params(value)
|
||||||
}
|
}
|
||||||
@@ -935,6 +937,10 @@ impl ConnectionConfig {
|
|||||||
self.ssl || url_params_contains_flag(self.url_params.as_deref(), "secure", "true")
|
self.ssl || url_params_contains_flag(self.url_params.as_deref(), "secure", "true")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn mysql_uses_tls(&self) -> bool {
|
||||||
|
self.ssl || self.host.to_ascii_lowercase().ends_with(".tidbcloud.com")
|
||||||
|
}
|
||||||
|
|
||||||
fn redis_tls_insecure_fragment(&self) -> &'static str {
|
fn redis_tls_insecure_fragment(&self) -> &'static str {
|
||||||
if self.ssl && self.redis_tls_insecure() {
|
if self.ssl && self.redis_tls_insecure() {
|
||||||
"#insecure"
|
"#insecure"
|
||||||
@@ -987,10 +993,12 @@ fn normalize_mysql_url_params(value: &str, force_tls: bool, accept_invalid_certs
|
|||||||
let mut parts: Vec<String> = value.split('&').filter(|part| !part.is_empty()).map(str::to_string).collect();
|
let mut parts: Vec<String> = value.split('&').filter(|part| !part.is_empty()).map(str::to_string).collect();
|
||||||
|
|
||||||
if force_tls {
|
if force_tls {
|
||||||
parts.retain(|part| !url_param_key_is(part, "ssl-mode") && !url_param_key_is(part, "sslmode"));
|
parts.retain(|part| {
|
||||||
if !parts.iter().any(|part| url_param_key_is(part, "require_ssl")) {
|
!url_param_key_is(part, "ssl-mode")
|
||||||
parts.insert(0, "require_ssl=true".to_string());
|
&& !url_param_key_is(part, "sslmode")
|
||||||
}
|
&& !url_param_key_is(part, "require_ssl")
|
||||||
|
});
|
||||||
|
parts.insert(0, "require_ssl=true".to_string());
|
||||||
if accept_invalid_certs && !parts.iter().any(|part| url_param_key_is(part, "verify_ca")) {
|
if accept_invalid_certs && !parts.iter().any(|part| url_param_key_is(part, "verify_ca")) {
|
||||||
parts.push("verify_ca=false".to_string());
|
parts.push("verify_ca=false".to_string());
|
||||||
}
|
}
|
||||||
@@ -1668,6 +1676,19 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn tidb_cloud_mysql_url_requires_tls() {
|
||||||
|
let mut config = mysql_config("root", "secret", Some("test"));
|
||||||
|
config.host = "gateway01.us-west-2.prod.aws.tidbcloud.com".to_string();
|
||||||
|
config.port = 4000;
|
||||||
|
config.url_params = Some("require_ssl=false&charset=utf8mb4".to_string());
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
config.connection_url(),
|
||||||
|
"mysql://root:secret@gateway01.us-west-2.prod.aws.tidbcloud.com:4000/test?require_ssl=true&verify_ca=false&verify_identity=false&charset=utf8mb4"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn postgres_url_appends_custom_params() {
|
fn postgres_url_appends_custom_params() {
|
||||||
let mut config = mysql_config("postgres", "secret", Some("test"));
|
let mut config = mysql_config("postgres", "secret", Some("test"));
|
||||||
|
|||||||
@@ -50,6 +50,15 @@ test("parses mysql TLS URL params into the SSL switch state", () => {
|
|||||||
assert.equal(parseConnectionUrl("mysql://root@tidb.example.com:4000/test?require_ssl=true").ssl, true);
|
assert.equal(parseConnectionUrl("mysql://root@tidb.example.com:4000/test?require_ssl=true").ssl, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("parses TiDB Cloud MySQL URLs as TLS connections", () => {
|
||||||
|
const parsed = parseConnectionUrl(
|
||||||
|
"mysql://root:secret@gateway01.us-west-2.prod.aws.tidbcloud.com:4000/test",
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(parsed.dbType, "mysql");
|
||||||
|
assert.equal(parsed.ssl, true);
|
||||||
|
});
|
||||||
|
|
||||||
test("parses MySQL JDBC user and password URL params as credentials", () => {
|
test("parses MySQL JDBC user and password URL params as credentials", () => {
|
||||||
const parsed = parseConnectionUrl(
|
const parsed = parseConnectionUrl(
|
||||||
"jdbc:mysql://127.0.0.1:1234/example?user=admin&password=pwd&useUnicode=true&characterEncoding=UTF8&useSSL=false",
|
"jdbc:mysql://127.0.0.1:1234/example?user=admin&password=pwd&useUnicode=true&characterEncoding=UTF8&useSSL=false",
|
||||||
|
|||||||
Reference in new issue
Block a user