Skip to content

Add credential using a base64 encoded API key #238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api_generator/src/generator/code_gen/url/enum_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ mod tests {
methods: vec![HttpMethod::Get, HttpMethod::Post],
parts: {
let mut map = BTreeMap::new();
map.insert("index".to_string(), Type {i
map.insert("index".to_string(), Type {
ty: TypeKind::List,
description: Some("A comma-separated list of index names to search".to_string()),
options: vec![],
Expand Down
4 changes: 3 additions & 1 deletion elasticsearch/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ pub enum Credentials {
/// This requires the `native-tls` or `rustls-tls` feature to be enabled.
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
Certificate(ClientCertificate),
/// An id and api_key to use for API key authentication
/// An API key id and secret to use for API key authentication
ApiKey(String, String),
/// An API key as a base64-encoded id and secret
EncodedApiKey(String),
}

#[cfg(any(feature = "native-tls", feature = "rustls-tls"))]
Expand Down
13 changes: 9 additions & 4 deletions elasticsearch/src/http/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use lazy_static::lazy_static;
use serde::Serialize;
use serde_json::Value;
use std::{
convert::TryFrom,
error, fmt,
fmt::Debug,
io::{self, Write},
Expand Down Expand Up @@ -492,10 +493,14 @@ impl Transport {
write!(encoder, "{}:", i).unwrap();
write!(encoder, "{}", k).unwrap();
}
request_builder.header(
AUTHORIZATION,
HeaderValue::from_bytes(&header_value).unwrap(),
)
let mut header_value = HeaderValue::from_bytes(&header_value).unwrap();
header_value.set_sensitive(true);
request_builder.header(AUTHORIZATION, header_value)
}
Credentials::EncodedApiKey(k) => {
let mut header_value = HeaderValue::try_from(format!("ApiKey {}", k)).unwrap();
header_value.set_sensitive(true);
request_builder.header(AUTHORIZATION, header_value)
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions elasticsearch/tests/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ async fn api_key_header() -> Result<(), failure::Error> {
Ok(())
}

#[tokio::test]
async fn encoded_api_key_header() -> Result<(), failure::Error> {
let server = server::http(move |req| async move {
let mut header_value = b"ApiKey ".to_vec();
{
let mut encoder = EncoderWriter::new(&mut header_value, &BASE64_STANDARD);
write!(encoder, "id:api_key").unwrap();
}

assert_eq!(
req.headers()["authorization"],
String::from_utf8(header_value).unwrap()
);
http::Response::default()
});

let builder = client::create_builder(format!("http://{}", server.addr()).as_ref())
// result of `echo -n "id:api_key" | base64`
.auth(Credentials::EncodedApiKey("aWQ6YXBpX2tleQ==".into()));

let client = client::create(builder);
let _response = client.ping().send().await?;

Ok(())
}

#[tokio::test]
async fn bearer_header() -> Result<(), failure::Error> {
let server = server::http(move |req| async move {
Expand Down