Skip to content

Commit dfe82ad

Browse files
authored
Merge branch 'main' into http
2 parents ee0db3e + 74962ba commit dfe82ad

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/query/service/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ regex = "1.6.0"
115115
semver = "1.0.14"
116116
serde = { workspace = true }
117117
serde_json = { workspace = true }
118+
serde_urlencoded = "0.7.1"
118119
tempfile = { version = "3.3.0", optional = true }
119120
thrift = { package = "databend-thrift", version = "0.17.0", optional = true }
120121
time = "0.3.14"

src/query/service/src/servers/http/middleware.rs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use std::collections::HashMap;
1516
use std::sync::Arc;
1617

1718
use common_exception::ErrorCode;
@@ -20,6 +21,7 @@ use headers::authorization::Basic;
2021
use headers::authorization::Bearer;
2122
use headers::authorization::Credentials;
2223
use http::header::AUTHORIZATION;
24+
use http::HeaderValue;
2325
use poem::error::Error as PoemError;
2426
use poem::error::Result as PoemResult;
2527
use poem::http::StatusCode;
@@ -49,8 +51,8 @@ impl HTTPSessionMiddleware {
4951
}
5052

5153
fn get_credential(req: &Request, kind: HttpHandlerKind) -> Result<Credential> {
52-
let auth_headers: Vec<_> = req.headers().get_all(AUTHORIZATION).iter().collect();
53-
if auth_headers.len() > 1 {
54+
let std_auth_headers: Vec<_> = req.headers().get_all(AUTHORIZATION).iter().collect();
55+
if std_auth_headers.len() > 1 {
5456
let msg = &format!("Multiple {} headers detected", AUTHORIZATION);
5557
return Err(ErrorCode::AuthenticateFailure(msg));
5658
}
@@ -59,26 +61,24 @@ fn get_credential(req: &Request, kind: HttpHandlerKind) -> Result<Credential> {
5961
Addr::Custom(..) => Some("127.0.0.1".to_string()),
6062
_ => None,
6163
};
62-
if auth_headers.is_empty() {
63-
if let HttpHandlerKind::Clickhouse = kind {
64-
let (user, key) = (
65-
req.headers().get("X-CLICKHOUSE-USER"),
66-
req.headers().get("X-CLICKHOUSE-KEY"),
67-
);
68-
if let (Some(name), Some(password)) = (user, key) {
69-
let c = Credential::Password {
70-
name: String::from_utf8(name.as_bytes().to_vec()).unwrap(),
71-
password: Some(password.as_bytes().to_vec()),
72-
hostname: client_ip,
73-
};
74-
return Ok(c);
75-
}
64+
if std_auth_headers.is_empty() {
65+
if matches!(kind, HttpHandlerKind::Clickhouse) {
66+
auth_clickhouse_name_password(req, client_ip)
67+
} else {
68+
Err(ErrorCode::AuthenticateFailure(
69+
"No authorization header detected",
70+
))
7671
}
77-
return Err(ErrorCode::AuthenticateFailure(
78-
"No authorization header detected",
79-
));
72+
} else {
73+
auth_by_header(&std_auth_headers, client_ip)
8074
}
81-
let value = auth_headers[0];
75+
}
76+
77+
fn auth_by_header(
78+
std_auth_headers: &[&HeaderValue],
79+
client_ip: Option<String>,
80+
) -> Result<Credential> {
81+
let value = &std_auth_headers[0];
8282
if value.as_bytes().starts_with(b"Basic ") {
8383
match Basic::decode(value) {
8484
Some(basic) => {
@@ -107,6 +107,37 @@ fn get_credential(req: &Request, kind: HttpHandlerKind) -> Result<Credential> {
107107
}
108108
}
109109

110+
fn auth_clickhouse_name_password(req: &Request, client_ip: Option<String>) -> Result<Credential> {
111+
let (user, key) = (
112+
req.headers().get("X-CLICKHOUSE-USER"),
113+
req.headers().get("X-CLICKHOUSE-KEY"),
114+
);
115+
if let (Some(name), Some(password)) = (user, key) {
116+
let c = Credential::Password {
117+
name: String::from_utf8(name.as_bytes().to_vec()).unwrap(),
118+
password: Some(password.as_bytes().to_vec()),
119+
hostname: client_ip,
120+
};
121+
Ok(c)
122+
} else {
123+
let query_str = req.uri().query().unwrap_or_default();
124+
let query_params = serde_urlencoded::from_str::<HashMap<String, String>>(query_str)
125+
.map_err(|e| ErrorCode::BadArguments(format!("{}", e)))?;
126+
let (user, key) = (query_params.get("user"), query_params.get("password"));
127+
if let (Some(name), Some(password)) = (user, key) {
128+
Ok(Credential::Password {
129+
name: name.clone(),
130+
password: Some(password.as_bytes().to_vec()),
131+
hostname: client_ip,
132+
})
133+
} else {
134+
Err(ErrorCode::AuthenticateFailure(
135+
"No header or query parameters for authorization detected",
136+
))
137+
}
138+
}
139+
}
140+
110141
impl<E: Endpoint> Middleware<E> for HTTPSessionMiddleware {
111142
type Output = HTTPSessionEndpoint<E>;
112143
fn transform(&self, ep: E) -> Self::Output {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
1
22
1
3+
1

tests/suites/0_stateless/14_clickhouse_http_handler/14_0003_http_auth.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ curl -s -u root: -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}"
99

1010
curl -s -u user1:abc123 -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}" -d 'select 1 FORMAT CSV'
1111

12-
## TODO(move this into separated port of clickhouse ?)
1312
curl -s -H 'X-ClickHouse-User: user1' -H 'X-ClickHouse-Key: abc123' -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}" -d 'select 1 FORMAT CSV'
1413

15-
14+
curl -s -XPOST "http://localhost:${QUERY_CLICKHOUSE_HTTP_HANDLER_PORT}?user=user1&password=abc123" -d 'select 1 FORMAT CSV'

0 commit comments

Comments
 (0)