Skip to content
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

chore: raise error message when failing to validate tokens and url has url postfix #588

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ utoipa-swagger-ui = { version = "8", features = ["actix-web"] }
actix-http = "3.9.0"
actix-http-test = "3.2.0"
actix-service = "2.0.2"
capture-logger = "0.1.1"
env_logger = "0.11.5"
maplit = "1.0.2"
rand = "0.8.5"
Expand Down
45 changes: 37 additions & 8 deletions server/src/http/unleash_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use reqwest::header::{HeaderMap, HeaderName};
use reqwest::{header, Client};
use reqwest::{ClientBuilder, Identity, RequestBuilder, StatusCode, Url};
use serde::{Deserialize, Serialize};
use tracing::error;
use tracing::{info, trace, warn};
use unleash_types::client_features::ClientFeatures;
use unleash_types::client_metrics::ClientApplication;
Expand Down Expand Up @@ -178,11 +179,7 @@ pub struct EdgeTokens {
}

impl UnleashClient {
pub fn from_url(
server_url: Url,
token_header: String,
backing_client: Client,
) -> Self {
pub fn from_url(server_url: Url, token_header: String, backing_client: Client) -> Self {
Self {
urls: UnleashUrls::from_base_url(server_url),
backing_client,
Expand Down Expand Up @@ -434,7 +431,7 @@ impl UnleashClient {
let check_api_suffix = || {
let base_url = self.urls.base_url.to_string();
if base_url.ends_with("/api") || base_url.ends_with("/api/") {
info!("Try passing the instance URL without '/api'.");
error!("Try passing the instance URL without '/api'.");
}
};

Expand All @@ -447,7 +444,6 @@ impl UnleashClient {
.await
.map_err(|e| {
info!("Failed to validate tokens: [{e:?}]");
check_api_suffix();
EdgeError::EdgeTokenError
})?;
match result.status() {
Expand Down Expand Up @@ -476,7 +472,7 @@ impl UnleashClient {
TOKEN_VALIDATION_FAILURES
.with_label_values(&[result.status().as_str()])
.inc();
warn!(
error!(
"Failed to validate tokens. Requested url: [{}]. Got status: {:?}",
self.urls.edge_validate_url.to_string(),
s
Expand Down Expand Up @@ -504,6 +500,7 @@ mod tests {
http::header::EntityTag,
web, App, HttpResponse,
};
use capture_logger::{begin_capture, pop_captured};
use chrono::Duration;
use unleash_types::client_features::{ClientFeature, ClientFeatures};

Expand Down Expand Up @@ -576,6 +573,10 @@ mod tests {
.service(
web::resource("/edge/validate")
.route(web::post().to(return_validate_tokens)),
)
.service(
web::resource("/api/edge/validate")
.route(web::post().to(|| HttpResponse::Forbidden())),
),
|_| AppConfig::default(),
))
Expand Down Expand Up @@ -723,6 +724,34 @@ mod tests {
}
}

#[actix_web::test]
async fn url_with_api_postfix_logs_a_useful_error_message() {
begin_capture();

let srv = test_features_server().await;
let client = UnleashClient::new(srv.url("/api").as_str(), None).unwrap();

let validate_result = client
.validate_tokens(ValidateTokensRequest {
tokens: vec![TEST_TOKEN.to_string()],
})
.await;

assert!(validate_result.is_err());

let mut captured = false;
while let Some(message) = pop_captured() {
if message.level() == capture_logger::Level::Error {
captured = true;
assert_eq!(
message.message(),
"Try passing the instance URL without '/api'."
);
}
}
assert!(captured);
}

#[test]
pub fn can_parse_entity_tag() {
let etag = EntityTag::from_str("W/\"b5e6-DPC/1RShRw1J/jtxvRtTo1jf4+o\"").unwrap();
Expand Down
Loading