Skip to content

Commit

Permalink
Read secure option from config
Browse files Browse the repository at this point in the history
  • Loading branch information
azat committed Dec 18, 2023
1 parent 0778a29 commit 36a8331
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/interpreter/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ struct ClickHouseClientConfigConnectionsCredentials {
port: Option<u16>,
user: Option<String>,
password: Option<String>,
secure: Option<bool>,
}
#[derive(Deserialize, Default)]
struct ClickHouseClientConfig {
user: Option<String>,
password: Option<String>,
secure: Option<bool>,
connections_credentials: Vec<ClickHouseClientConfigConnectionsCredentials>,
}

Expand All @@ -37,13 +39,15 @@ struct XmlClickHouseClientConfigConnectionsCredentialsConnection {
struct XmlClickHouseClientConfig {
user: Option<String>,
password: Option<String>,
secure: Option<bool>,
connections_credentials: Option<XmlClickHouseClientConfigConnectionsCredentialsConnection>,
}

#[derive(Deserialize)]
struct YamlClickHouseClientConfig {
user: Option<String>,
password: Option<String>,
secure: Option<bool>,
connections_credentials: Option<HashMap<String, ClickHouseClientConfigConnectionsCredentials>>,
}

Expand Down Expand Up @@ -137,6 +141,7 @@ fn read_yaml_clickhouse_client_config(path: &str) -> Result<ClickHouseClientConf
let mut config = ClickHouseClientConfig::default();
config.user = yaml_config.user;
config.password = yaml_config.password;
config.secure = yaml_config.secure;
config.connections_credentials = yaml_config
.connections_credentials
.unwrap_or_default()
Expand All @@ -154,6 +159,7 @@ fn read_xml_clickhouse_client_config(path: &str) -> Result<ClickHouseClientConfi
let mut config = ClickHouseClientConfig::default();
config.user = xml_config.user;
config.password = xml_config.password;
config.secure = xml_config.secure;
config.connections_credentials = xml_config
.connections_credentials
.unwrap_or_default()
Expand Down Expand Up @@ -223,6 +229,14 @@ fn clickhouse_url_defaults(options: &mut ChDigOptions) {
let mut url = parse_url(&options.clickhouse.url.clone().unwrap_or_default());
let config: Option<ClickHouseClientConfig> = read_clickhouse_client_config();
let connection = &options.clickhouse.connection;
let mut has_secure: Option<bool> = None;

{
let pairs: HashMap<_, _> = url.query_pairs().into_owned().collect();
if pairs.contains_key("secure") {
has_secure = Some(true);
}
}

// host should be set first, since url crate does not allow to set user/password without host.
let has_host = url.host().is_some();
Expand All @@ -243,6 +257,11 @@ fn clickhouse_url_defaults(options: &mut ChDigOptions) {
url.set_password(Some(env_password.as_str())).unwrap();
}
}
if url.password().is_none() {
if let Ok(env_password) = env::var("CLICKHOUSE_PASSWORD") {
url.set_password(Some(env_password.as_str())).unwrap();
}
}

//
// config
Expand All @@ -258,6 +277,11 @@ fn clickhouse_url_defaults(options: &mut ChDigOptions) {
url.set_password(Some(password.as_str())).unwrap();
}
}
if has_secure.is_none() {
if let Some(secure) = &config.secure {
has_secure = Some(*secure);
}
}

//
// connections_credentials section from config
Expand Down Expand Up @@ -293,6 +317,11 @@ fn clickhouse_url_defaults(options: &mut ChDigOptions) {
url.set_password(Some(password.as_str())).unwrap();
}
}
if has_secure.is_none() {
if let Some(secure) = &c.secure {
has_secure = Some(*secure);
}
}
}

if !connection_found {
Expand All @@ -303,6 +332,17 @@ fn clickhouse_url_defaults(options: &mut ChDigOptions) {
panic!("No client config had been read, while --connection was set");
}

// - 9000 for non secure
// - 9440 for secure
if url.port().is_none() {
url.set_port(Some(if has_secure.unwrap_or_default() {
9440
} else {
9000
}))
.unwrap();
}

let mut url_safe = url.clone();

// url_safe
Expand Down Expand Up @@ -330,6 +370,9 @@ fn clickhouse_url_defaults(options: &mut ChDigOptions) {
if !pairs.contains_key("query_timeout") {
mut_pairs.append_pair("query_timeout", "600s");
}
if let Some(secure) = has_secure {
mut_pairs.append_pair("secure", secure.to_string().as_str());
}
}

options.clickhouse.url = Some(url.to_string());
Expand Down

0 comments on commit 36a8331

Please sign in to comment.