Skip to content

Commit ef5f47a

Browse files
committed
added cookie auth to RemoteStateService
1 parent bd9c45f commit ef5f47a

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

Cargo.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -1863,9 +1863,9 @@ dependencies = [
18631863

18641864
[[package]]
18651865
name = "http"
1866-
version = "1.2.0"
1866+
version = "1.3.1"
18671867
source = "registry+https://github.com/rust-lang/crates.io-index"
1868-
checksum = "f16ca2af56261c99fba8bac40a10251ce8188205a4c448fbb745a2e4daa76fea"
1868+
checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565"
18691869
dependencies = [
18701870
"bytes",
18711871
"fnv",
@@ -6422,6 +6422,7 @@ dependencies = [
64226422
"hex",
64236423
"hex-literal",
64246424
"howudoin",
6425+
"http",
64256426
"human_bytes",
64266427
"humantime-serde",
64276428
"indexmap 2.7.1",

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ halo2 = "0.3.0"
6767
hex = "0.4.3"
6868
hex-literal = "0.4.1"
6969
howudoin = "0.1.2"
70+
http = "1.3.1"
7071
http-body-util = "0.1.2"
7172
human_bytes = { version = "0.4.3", default-features = false }
7273
humantime = "2.1.0"

zebra-state/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ elasticsearch = [
5151
# Remote support for the [`ReadStateService`].
5252
remote_read_state_service = [
5353
"dep:url",
54+
"dep:http",
5455
"dep:jsonrpsee",
5556
"jsonrpsee/http-client",
5657
"jsonrpsee/client-core",
@@ -86,6 +87,7 @@ tracing = { workspace = true }
8687

8788
# remote_read_state_service specific dependencies.
8889
url = { workspace = true, optional = true }
90+
http = { workspace = true, optional = true }
8991
jsonrpsee = { workspace = true, optional = true }
9092

9193
# elasticsearch specific dependencies.

zebra-state/src/service.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -2168,11 +2168,14 @@ pub fn init_test_services(
21682168
/// Holds functionality for remote use of the [`ReadStateService`] ([`RemoteStateService`]).
21692169
#[cfg(feature = "remote_read_state_service")]
21702170
pub mod remote {
2171+
use std::fs;
2172+
21712173
use super::*;
21722174

2175+
use http::header::COOKIE;
21732176
use jsonrpsee::{
21742177
core::client::ClientT,
2175-
http_client::{HttpClient, HttpClientBuilder},
2178+
http_client::{HeaderMap, HttpClient, HttpClientBuilder},
21762179
};
21772180
use url::Url;
21782181

@@ -2199,16 +2202,28 @@ pub mod remote {
21992202
}
22002203

22012204
impl RemoteStateService {
2202-
/// Creates a new remote, read-only state service client.
2205+
/// Creates a new remote, read-only state service client with optional cookie authentication.
22032206
#[allow(dead_code)]
2204-
pub fn new(url: Url) -> Result<Self, BoxError> {
2205-
let client = HttpClientBuilder::default()
2206-
.request_timeout(Duration::from_secs(5))
2207-
.build(url.as_str())?;
2208-
2209-
let remote_read_state_service = Self { client };
2207+
pub fn new(url: Url, cookie: Option<String>) -> Result<Self, BoxError> {
2208+
let mut builder = HttpClientBuilder::default().request_timeout(Duration::from_secs(5));
2209+
2210+
if let Some(cookie_path) = cookie {
2211+
let cookie_content =
2212+
fs::read_to_string(&cookie_path).map_err(|e| Box::new(e) as BoxError)?;
2213+
let cookie_content = cookie_content.trim();
2214+
2215+
let mut headers = HeaderMap::new();
2216+
headers.insert(
2217+
COOKIE,
2218+
cookie_content
2219+
.parse()
2220+
.map_err(|e| Box::new(e) as BoxError)?,
2221+
);
2222+
builder = builder.set_headers(headers);
2223+
}
22102224

2211-
Ok(remote_read_state_service)
2225+
let client = builder.build(url.as_str())?;
2226+
Ok(Self { client })
22122227
}
22132228

22142229
/// Sends a request to the server an waits on a response.

0 commit comments

Comments
 (0)