Skip to content

Commit

Permalink
add a unit test for status server with tls support
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
rleungx committed Mar 17, 2020
1 parent 34a2193 commit cc5411b
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 71 deletions.
54 changes: 53 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ git = "https://github.com/tikv/yatp.git"
[dev-dependencies]
panic_hook = { path = "components/panic_hook" }
test_sst_importer = { path = "components/test_sst_importer" }
test_util = { path = "components/test_util" }
tokio = { version = "0.2", features = ["macros", "rt-threaded", "time"] }
zipf = "5.0.1"
hyper-openssl = "0.7"

[patch.crates-io]
# TODO: remove this when new raft-rs is published.
Expand Down
20 changes: 0 additions & 20 deletions components/test_util/data/ca.crt

This file was deleted.

22 changes: 22 additions & 0 deletions components/test_util/data/ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDojCCAoqgAwIBAgIUdZFW8VQoZZzek8cA+5GGu6ZInjowDQYJKoZIhvcNAQEL
BQAwVzELMAkGA1UEBhMCQ04xEDAOBgNVBAgTB0JlaWppbmcxEDAOBgNVBAcTB0Jl
aWppbmcxEDAOBgNVBAoTB1BpbmdDQVAxEjAQBgNVBAMTCU15IG93biBDQTAeFw0x
OTA5MDIwNjEyMDBaFw0yNDA4MzEwNjEyMDBaMFcxCzAJBgNVBAYTAkNOMRAwDgYD
VQQIEwdCZWlqaW5nMRAwDgYDVQQHEwdCZWlqaW5nMRAwDgYDVQQKEwdQaW5nQ0FQ
MRIwEAYDVQQDEwlNeSBvd24gQ0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
AoIBAQDcDtQ7UX+xlVY0vpklp1uUmPoFsN0U6fqRzHU+LvYS5AM5RPJMVLiKBiSi
zGsB+XPmXZ8H7rZZ+osZsEmDIF3HdyiSNpPNzRJKxsz4KVRzfoKZXL9D41TpuE27
+7tN6qGytYrnAy8cHMA0S1TnQ0biOFTcXZrwh5lvlIcx7ceUamGuEl94tblxSSJl
2SkpHkKIDv0kcgoGmmh4y8SzAtmnwcCjkCSoITvvwKklp5830pFKOnpN9uZJzkXa
tuUSpSji/JG79nQfH91LtL7xMprORVtg9YAa3aJm0Uf33WFvaCTSrt//7CVK8nqK
xayS3u7dNH3GV9b81OGtlR76leFlAgMBAAGjZjBkMA4GA1UdDwEB/wQEAwIBBjAS
BgNVHRMBAf8ECDAGAQH/AgECMB0GA1UdDgQWBBS3hxTaN9B7eF8xr0DKLZ3b5vFn
rDAfBgNVHSMEGDAWgBS3hxTaN9B7eF8xr0DKLZ3b5vFnrDANBgkqhkiG9w0BAQsF
AAOCAQEAi9WiEvTQQjmb7ekXHf1tKwdLNu5akQXIwTKeZSWRSeMgqVQcoyTZMPBX
ythl6K3175RUIMtCwO4uZTOpRU1mTl0pIjoEcJGHYX91zyA5BjWahXZttvt7/hyX
UwJN9clBXLfZTCp1ysLCtarLcip4WxWNsxEwXFUisE2gbu3F9ELHAbRSVUe/CwC6
8BkY+G+fovazjGoTV4NadJVFRzTR/zsWkBNllBOBTrop8FH23ePVh3hXafzJlcip
bDbRxNqSzNtLr88mwswklgiIHXF6PY2TkyscsXVkHPAswZnrv4lLov7M3VjL8ITA
uYm4Me5Tmj+6pb+Foky15+ehmicQbA==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions components/test_util/data/key.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAsRpq/E/VC82YxsC5LlKFvI9HJuchMtKskn53anW4rNE3sfN0
WDS6qCyxNumUVBqO98J18xxbz/XkV7aP6TcXZrNgEqw07PZWTDoyZVi+n9HXyWwl
BeiE2WWrCESqsar+cXV5UE3oE7Y4CT56tMN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BgDly5E1P78rmhVl7qV5exYDkl2iMhnywYrPFtOUN
xDL2320csWz0l+F/S1rngYx/78KSUPMzsWgYKvuCPN+SQ5xNXzJXdzZLlqBN7/ZF
L/cMKJTP53FZxM2x8sjI09h1GPsG+quoVfL/yrLU1FF/FkyZ0QCKEooOfbaJoARe
YK+xAoGAfT0P200WsLKRl73XYJZNYQl5+h5s7Sk9J8QuPwFWqm/mGwYKTLI042jg
lsAym4krAR0c1CHTW3aHRimYpYbi7/kztZU1zUQgcGL+79afer3ZuFF7mGzR+I/r
yOQ2dEfmVASfl/fMh1qyExpcCaMuejaODWyILlxOwvnywHWMSCU=
-----END RSA PRIVATE KEY-----
19 changes: 0 additions & 19 deletions components/test_util/data/server.crt

This file was deleted.

49 changes: 22 additions & 27 deletions components/test_util/data/server.pem
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA2i5r+HQVIEbKr7wHemWCmbkkmbS0NSogGf9VgZuw033Qo3gc
gqL6NvYx7nIk+615UnFHUftQhnuWFjLIXHTtg0VgIg4uGU6gAw7DUygspQ+clGuD
66796ThF2UuDKDzWmgRzi8xzWiF3L9RhCv5dT+07b6+0QdLFUsjDKFqtfZ+YuE41
wVTZpmgxemayd1D0zrisAn0CMpUKZOY2btpW38K6oI5nq2PzZRU8GtEfCKLgKoFN
zR/RtGYz2mnnBvs4WufPSPILblNqB1k1zXJYzqLxy3rxhbAWxaXG2hYZxkwhSRIg
8yMH6Hkvehzm3fu2rgWOra+sR/D1ujMZNZWh4QIDAQABAoIBAEfb/EGzcfXUexNQ
OaJNZqtcuDpLswLDohkN6MqsTZwKlzoP6Ev6g7Cwe5eOTrH527iUiKnuvQHeGHut
NCKHfGa85cGxq+s34ym+pgRweevPbYHQu31XgFdc6lx8K3GIQCIwDyJfLyrjVM8T
AvdM+czGVMofM55uXgE7EFPtMbDq0JKHj/A0uAd8+3WKTWebshVxi2h7ejGQK5fO
xi3Wz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sE4M8ceGq58f7zPSq/suf+8SYEOFP8zfuXX1HfUrFVe
69ZQw8PZh4EYL0PYtE5BYfe9iJyFNNtZiejiribMQz/NtNkKM3M+Hm40ULGuwHXq
bKDjDq1PvmpVb/kKO/xADTIAbqproXETZ4W2keI3hwm6NxysvEbYV9+puQBXQqwT
KOt9Lo4ofSAF
-----END CERTIFICATE-----
8 changes: 4 additions & 4 deletions components/test_util/src/security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use tikv_util::security::SecurityConfig;
pub fn new_security_cfg() -> SecurityConfig {
let p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
SecurityConfig {
ca_path: format!("{}", p.join("data/ca.crt").display()),
cert_path: format!("{}", p.join("data/server.crt").display()),
key_path: format!("{}", p.join("data/server.pem").display()),
override_ssl_target: "example.com".to_owned(),
ca_path: format!("{}", p.join("data/ca.pem").display()),
cert_path: format!("{}", p.join("data/server.pem").display()),
key_path: format!("{}", p.join("data/key.pem").display()),
override_ssl_target: "".to_owned(),
cipher_file: "".to_owned(),
}
}
46 changes: 46 additions & 0 deletions src/server/status_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,12 +571,19 @@ fn handle_fail_points_request(
mod tests {
use futures::future::{lazy, Future};
use futures::Stream;
use hyper::client::HttpConnector;
use hyper::{Body, Client, Method, Request, StatusCode, Uri};
use hyper_openssl::HttpsConnector;
use openssl::ssl::{SslConnector, SslMethod};
use tokio_core::reactor::Handle;

use std::env;
use std::path::PathBuf;

use crate::config::TiKvConfig;
use crate::server::status_server::StatusServer;
use raftstore::store::PdTask;
use test_util::new_security_cfg;
use tikv_util::security::SecurityConfig;
use tikv_util::worker::{dummy_future_scheduler, FutureRunnable, FutureWorker};

Expand Down Expand Up @@ -606,6 +613,45 @@ mod tests {
status_server.stop();
}

#[test]
fn test_security_status_service() {
let mut status_server = StatusServer::new(1, dummy_future_scheduler());
let _ = status_server.start("127.0.0.1:0".to_string(), &new_security_cfg());
let p = PathBuf::from(env!("CARGO_MANIFEST_DIR"));

let mut connector = HttpConnector::new(1);
connector.enforce_http(false);
let mut ssl = SslConnector::builder(SslMethod::tls()).unwrap();
ssl.set_ca_file(format!(
"{}",
p.join("components/test_util/data/ca.pem").display()
))
.unwrap();

let ssl = HttpsConnector::with_connector(connector, ssl).unwrap();
let client = Client::builder().build::<_, Body>(ssl);

let uri = Uri::builder()
.scheme("https")
.authority(status_server.listening_addr().to_string().as_str())
.path_and_query("/metrics")
.build()
.unwrap();

let handle = status_server.thread_pool.spawn_handle(lazy(move || {
client
.get(uri)
.map(|res| {
assert_eq!(res.status(), StatusCode::OK);
})
.map_err(|err| {
panic!("response status is not OK: {:?}", err);
})
}));
handle.wait().unwrap();
status_server.stop();
}

#[test]
fn test_config_endpoint() {
struct Runner;
Expand Down

0 comments on commit cc5411b

Please sign in to comment.