From dfb191093063d676323d840614cd3e381cb4aa8a Mon Sep 17 00:00:00 2001 From: Christopher Kolstad Date: Tue, 2 May 2023 11:31:33 +0200 Subject: [PATCH] fix: Handle both upper and lowercase of apitokentype. (#174) Unleash has suddenly started returning token type with uppercase. This PR makes us handle both UPPER and lower case for token type. The real fix would be for Unleash to obey its own contract with lowercase for token types, but this fix makes us more tolerant to mistakes in Unleash code. --- server/src/http/unleash_client.rs | 9 +++++---- server/src/types.rs | 29 ++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/server/src/http/unleash_client.rs b/server/src/http/unleash_client.rs index c97e232a..8e6b473f 100644 --- a/server/src/http/unleash_client.rs +++ b/server/src/http/unleash_client.rs @@ -15,6 +15,7 @@ use crate::types::{ use prometheus::{register_int_gauge_vec, IntGaugeVec, Opts}; use reqwest::{header, Client}; +use tracing::warn; use unleash_types::client_metrics::ClientApplication; use crate::error::FeatureError; @@ -239,10 +240,10 @@ impl UnleashClient { .map_err(|_| EdgeError::EdgeTokenError)?; match result.status() { StatusCode::OK => { - let token_response = result - .json::() - .await - .map_err(|_| EdgeError::EdgeTokenParseError)?; + let token_response = result.json::().await.map_err(|e| { + warn!("Failed to parse validation response with error: {e:?}"); + EdgeError::EdgeTokenParseError + })?; Ok(token_response .tokens .into_iter() diff --git a/server/src/types.rs b/server/src/types.rs index 456571ee..e024104c 100644 --- a/server/src/types.rs +++ b/server/src/types.rs @@ -23,8 +23,11 @@ pub type EdgeResult = Result; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, utoipa::ToSchema)] #[serde(rename_all = "lowercase")] pub enum TokenType { + #[serde(alias = "FRONTEND")] Frontend, + #[serde(alias = "CLIENT")] Client, + #[serde(alias = "ADMIN")] Admin, Invalid, } @@ -274,10 +277,12 @@ pub struct FeatureFilters { mod tests { use std::str::FromStr; + use crate::error::EdgeError::EdgeTokenParseError; + use crate::http::unleash_client::EdgeTokens; use test_case::test_case; use tracing::warn; - use crate::types::EdgeToken; + use crate::types::{EdgeResult, EdgeToken}; fn test_str(token: &str) -> EdgeToken { EdgeToken::from_str( @@ -360,4 +365,26 @@ mod tests { assert!(token1.subsumes(&token2)); assert!(!token2.subsumes(&token1)); } + + #[test] + fn token_type_should_be_case_insensitive() { + let json = r###"{ "tokens": [{ + "token": "chriswk-test:development.notusedsecret", + "type": "CLIENT", + "projects": [ + "chriswk-test" + ] + }, + { + "token": "demo-app:production.notusedsecret", + "type": "client", + "projects": [ + "demo-app" + ] + }] }"###; + let tokens: EdgeResult = + serde_json::from_str(json).map_err(|_| EdgeTokenParseError); + assert!(tokens.is_ok()); + assert_eq!(tokens.unwrap().tokens.len(), 2); + } }