Skip to content

Commit af8be6d

Browse files
authored
Provide native-tls and rustls-tls features (#68)
This commit passes through the following features of the reqwest dependency: - native-tls (enabled by default): Enables TLS functionality provided by native-tls. passes through to reqwest/native-tls and is set as a default feature. - rustls-tls: Enables TLS functionality provided by rustls. passes through to reqwest/rustls-tls allowing end users more control over dependencies Attribute source code and tests that require features of the elasticsearch package to be enabled. Introduce ClientCertificate enum to differentiate between PKCS#12 archives and PEM certs, which are available only when native-tls and rustls-tls features are enabled, respectively. ClientCertificate enum takes bytes representing a certificate as opposed to a reqwest Identity because Identity does not implement Clone, and cannot be dereferenced from a Credentials::Certificate variant when building a Transport because other variants are passed to the built Transport. Closes #44
1 parent b53f5c7 commit af8be6d

File tree

14 files changed

+226
-45
lines changed

14 files changed

+226
-45
lines changed

.ci/DockerFile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ RUN mkdir src && echo "// dummy file" > src/lib.rs && cargo build --tests
1212
# install app dependencies and build
1313
COPY README.md ./../
1414
COPY elasticsearch .
15-
RUN cargo build --tests
15+
RUN cargo build --tests --all-features

.ci/run-elasticsearch.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,12 @@ if ($DETACH) {
278278
while((!$(container_running -Name $NODE_NAME)) -or ((container_running -Name $NODE_NAME) -and ($(docker inspect -f '{{.State.Health.Status}}' $NODE_NAME) -eq "starting"))) {
279279
Start-Sleep 2;
280280
$logs = docker inspect -f '{{json .State.Health.Log}}' $NODE_NAME | ConvertFrom-Json
281-
$lastLog = $logs[$logs.Length-1]
282-
Write-Output $lastLog.Output
281+
if ($logs) {
282+
$lastLog = $logs[$logs.Length-1]
283+
Write-Output $lastLog.Output
284+
} else {
285+
Write-Output "No logs from docker inspect"
286+
}
283287
log "waiting for node $NODE_NAME to be up"
284288
}
285289

.ci/run-tests.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@ param (
44
$ELASTICSEARCH_VERSION,
55

66
[string]
7+
[Parameter(Mandatory = $false)]
78
[ValidateSet("oss", "xpack")]
8-
$TEST_SUITE = "oss",
9+
$TEST_SUITE = "xpack",
910

1011
# TODO: move to stable once elasticsearch-rs compiles on stable
1112
[string]
13+
[Parameter(Mandatory = $false)]
1214
$RUST_VERSION = "nightly",
1315

1416
[string]
1517
[ValidateSet("1", "full")]
16-
$RUST_BACKTRACE
18+
[Parameter(Mandatory = $false)]
19+
$RUST_BACKTRACE,
20+
21+
[string]
22+
[Parameter(Mandatory = $false)]
23+
$CARGO_TEST_FLAGS = "--all-features"
1724
)
1825

1926
trap {
@@ -117,7 +124,7 @@ docker run `
117124
--name elasticsearch-rs `
118125
--rm `
119126
elastic/elasticsearch-rs `
120-
cargo test
127+
cargo test $CARGO_TEST_FLAGS
121128

122129
if ($LASTEXITCODE) {
123130
docker rm elasticsearch-rs

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Cargo.lock
55
.idea
66
.vscode/
77
*.log
8+
yaml_test_runner/

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ functions on the client will be compatible.
4040
Elasticsearch**. Major differences likely exist between major versions of Elasticsearch, particularly
4141
around request and response object formats, but also around API urls and behaviour.
4242

43+
## Features
44+
45+
The following are a list of Cargo features that can be enabled or disabled:
46+
47+
- **native-tls** *(enabled by default)*: Enables TLS functionality provided by `native-tls`.
48+
- **rustls-tls**: Enables TLS functionality provided by `rustls`.
49+
4350
## Getting started
4451

4552
The client exposes all Elasticsearch APIs as associated functions, either on

api_generator/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ lazy_static = "1.4.0"
1616
quote = "~0.3"
1717
reduce = "0.1.2"
1818
regex = "1.3.1"
19-
reqwest = "~0.9"
19+
reqwest = { version = "~0.10", features = ["blocking", "json", "gzip"] }
2020
rustfmt-nightly = "~1"
2121
semver = "0.9.0"
2222
serde = "~1"

api_generator/src/rest_spec/mod.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ pub fn download_specs(branch: &str, download_dir: &PathBuf) {
6161
}
6262

6363
fn download_endpoints(spec: &GitHubSpec, download_dir: &PathBuf) {
64-
let mut response = reqwest::get(&spec.url).unwrap();
65-
let rest_api_specs: Vec<RestApiSpec> = response.json().unwrap();
64+
let client = reqwest::blocking::ClientBuilder::new()
65+
.user_agent(concat!("RustApiGenerator/", env!("CARGO_PKG_VERSION")))
66+
.build()
67+
.unwrap();
6668

69+
let response = client.get(&spec.url).send().unwrap();
70+
let rest_api_specs: Vec<RestApiSpec> = response.json().unwrap();
6771
println!("Downloading {} specs from {}", spec.dir, spec.branch);
68-
download_specs_to_dir(rest_api_specs.as_slice(), download_dir).unwrap();
72+
download_specs_to_dir(client,rest_api_specs.as_slice(), download_dir).unwrap();
6973
println!("Done downloading {} specs from {}", spec.dir, spec.branch);
7074
}

api_generator/src/rest_spec/parallel_downloading.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ pub(super) enum DownloadSpecsErrors {
1919
/// Downloads the given specs to the provided director in parallel, displaying progress bars for
2020
/// each file.
2121
pub(super) fn download_specs_to_dir(
22+
client: reqwest::blocking::Client,
2223
specs: &[RestApiSpec],
2324
download_dir: &PathBuf,
2425
) -> Result<(), DownloadSpecsErrors> {
25-
let client = reqwest::Client::new();
2626
let sty = ProgressStyle::default_bar()
2727
.template("{spinner:.green} {msg} [{elapsed_precise} (ETA: {eta})] [{bar:40.cyan/blue}] {bytes}/{total_bytes}")
2828
.progress_chars("#>-");

elasticsearch/Cargo.toml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@ authors = ["Elastic and Contributors"]
66
description = "Official Elasticsearch Rust client"
77
repository = "https://github.com/elastic/elasticsearch-rs"
88
keywords = ["elasticsearch", "elastic", "search", "lucene"]
9+
categories = ["api-bindings", "database"]
910
documentation = "https://docs.rs/elasticsearch/"
1011
license = "Apache-2.0"
1112
readme = "../README.md"
1213

14+
[package.metadata."docs.rs"]
15+
all-features = true
16+
17+
[features]
18+
default = ["native-tls"]
19+
20+
# optional TLS
21+
native-tls = ["reqwest/native-tls"]
22+
rustls-tls = ["reqwest/rustls-tls"]
23+
1324
[dependencies]
1425
base64 = "^0.11"
1526
bytes = "^0.5"
1627
dyn-clone = "~1"
17-
reqwest = { version = "^0.10", features = ["gzip", "json", "native-tls"] }
28+
reqwest = { version = "~0.10", default-features = false, features = ["default-tls", "gzip", "json"] }
1829
url = "^2.1"
1930
serde = { version = "~1", features = ["derive"] }
2031
serde_json = "~1"

elasticsearch/src/auth.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,45 @@ pub enum Credentials {
77
Basic(String, String),
88
/// An access_token to use for Bearer authentication
99
Bearer(String),
10-
/// Bytes of a DER-formatted PKCS#12 archive and password to use for
11-
/// PKI (Client Certificate) authentication.
10+
/// A client certificate to use for PKI (Client Certificate) authentication.
11+
/// # Optional
1212
///
13-
/// The archive should contain a leaf certificate and its private key, as well any intermediate
14-
/// certificates that allow clients to build a chain to a trusted root. The chain certificates
15-
/// should be in order from the leaf certificate towards the root.
16-
Cert(Vec<u8>, String),
13+
/// This requires the `native-tls` or `rustls-tls` feature to be enabled.
14+
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
15+
Certificate(ClientCertificate),
1716
/// An id and api_key to use for API key authentication
1817
ApiKey(String, String),
1918
}
19+
20+
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
21+
#[derive(Debug, Clone)]
22+
pub enum ClientCertificate {
23+
/// Bytes of a DER-formatted PKCS#12 archive and optional passphrase.
24+
///
25+
/// The archive should contain a leaf certificate and its private key,
26+
/// as well any intermediate certificates that allow clients to build a chain to
27+
/// a trusted root. The chain certificates
28+
/// should be in order from the leaf certificate towards the root.
29+
///
30+
/// # Optional
31+
///
32+
/// This requires the `native-tls` feature to be enabled.
33+
#[cfg(feature = "native-tls")]
34+
Pkcs12(Vec<u8>, Option<String>),
35+
36+
/// Bytes of a PEM encoded private key and
37+
/// at least one PEM encoded certificate.
38+
///
39+
/// # Optional
40+
///
41+
/// This requires the `rustls-tls` feature to be enabled.
42+
#[cfg(feature = "rustls-tls")]
43+
Pem(Vec<u8>)
44+
}
45+
46+
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
47+
impl From<ClientCertificate> for Credentials {
48+
fn from(cert: ClientCertificate) -> Self {
49+
Credentials::Certificate(cert)
50+
}
51+
}

0 commit comments

Comments
 (0)