Skip to content

Commit 54926a2

Browse files
authored
fix(catalog/rest): Ensure token been reused correctly (#801)
* fix(catalog/rest): Ensure token been reused correctly Signed-off-by: Xuanwo <[email protected]> * Fix oauth test Signed-off-by: Xuanwo <[email protected]> * Fix tests Signed-off-by: Xuanwo <[email protected]> --------- Signed-off-by: Xuanwo <[email protected]>
1 parent 748d37c commit 54926a2

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

crates/catalog/rest/src/catalog.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,10 @@ impl RestCatalog {
256256
async fn context(&self) -> Result<&RestContext> {
257257
self.ctx
258258
.get_or_try_init(|| async {
259-
let catalog_config = RestCatalog::load_config(&self.user_config).await?;
259+
let client = HttpClient::new(&self.user_config)?;
260+
let catalog_config = RestCatalog::load_config(&client, &self.user_config).await?;
260261
let config = self.user_config.clone().merge_with_config(catalog_config);
261-
let client = HttpClient::new(&config)?;
262+
let client = client.update_with(&config)?;
262263

263264
Ok(RestContext { config, client })
264265
})
@@ -268,9 +269,10 @@ impl RestCatalog {
268269
/// Load the runtime config from the server by user_config.
269270
///
270271
/// It's required for a rest catalog to update it's config after creation.
271-
async fn load_config(user_config: &RestCatalogConfig) -> Result<CatalogConfig> {
272-
let client = HttpClient::new(user_config)?;
273-
272+
async fn load_config(
273+
client: &HttpClient,
274+
user_config: &RestCatalogConfig,
275+
) -> Result<CatalogConfig> {
274276
let mut request = client.request(Method::GET, user_config.config_endpoint());
275277

276278
if let Some(warehouse_location) = &user_config.warehouse {
@@ -280,6 +282,7 @@ impl RestCatalog {
280282
let config = client
281283
.query::<CatalogConfig, ErrorResponse, OK>(request.build()?)
282284
.await?;
285+
283286
Ok(config)
284287
}
285288

@@ -777,7 +780,7 @@ mod tests {
777780
"expires_in": 86400
778781
}"#,
779782
)
780-
.expect(2)
783+
.expect(1)
781784
.create_async()
782785
.await
783786
}
@@ -831,7 +834,7 @@ mod tests {
831834
"expires_in": 86400
832835
}"#,
833836
)
834-
.expect(2)
837+
.expect(1)
835838
.create_async()
836839
.await;
837840

crates/catalog/rest/src/client.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl Debug for HttpClient {
5454
}
5555

5656
impl HttpClient {
57+
/// Create a new http client.
5758
pub fn new(cfg: &RestCatalogConfig) -> Result<Self> {
5859
Ok(HttpClient {
5960
client: Client::new(),
@@ -66,6 +67,32 @@ impl HttpClient {
6667
})
6768
}
6869

70+
/// Update the http client with new configuration.
71+
///
72+
/// If cfg carries new value, we will use cfg instead.
73+
/// Otherwise, we will keep the old value.
74+
pub fn update_with(self, cfg: &RestCatalogConfig) -> Result<Self> {
75+
Ok(HttpClient {
76+
client: self.client,
77+
78+
token: Mutex::new(
79+
cfg.token()
80+
.or_else(|| self.token.into_inner().ok().flatten()),
81+
),
82+
token_endpoint: (!cfg.get_token_endpoint().is_empty())
83+
.then(|| cfg.get_token_endpoint())
84+
.unwrap_or(self.token_endpoint),
85+
credential: cfg.credential().or(self.credential),
86+
extra_headers: (!cfg.extra_headers()?.is_empty())
87+
.then(|| cfg.extra_headers())
88+
.transpose()?
89+
.unwrap_or(self.extra_headers),
90+
extra_oauth_params: (!cfg.extra_oauth_params().is_empty())
91+
.then(|| cfg.extra_oauth_params())
92+
.unwrap_or(self.extra_oauth_params),
93+
})
94+
}
95+
6996
/// This API is testing only to assert the token.
7097
#[cfg(test)]
7198
pub(crate) async fn token(&self) -> Option<String> {

0 commit comments

Comments
 (0)