Skip to content

Commit

Permalink
fix: Handle both upper and lowercase of apitokentype. (#174)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Christopher Kolstad authored May 2, 2023
1 parent b26ad4a commit dfb1910
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
9 changes: 5 additions & 4 deletions server/src/http/unleash_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -239,10 +240,10 @@ impl UnleashClient {
.map_err(|_| EdgeError::EdgeTokenError)?;
match result.status() {
StatusCode::OK => {
let token_response = result
.json::<EdgeTokens>()
.await
.map_err(|_| EdgeError::EdgeTokenParseError)?;
let token_response = result.json::<EdgeTokens>().await.map_err(|e| {
warn!("Failed to parse validation response with error: {e:?}");
EdgeError::EdgeTokenParseError
})?;
Ok(token_response
.tokens
.into_iter()
Expand Down
29 changes: 28 additions & 1 deletion server/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ pub type EdgeResult<T> = Result<T, EdgeError>;
#[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,
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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<EdgeTokens> =
serde_json::from_str(json).map_err(|_| EdgeTokenParseError);
assert!(tokens.is_ok());
assert_eq!(tokens.unwrap().tokens.len(), 2);
}
}

0 comments on commit dfb1910

Please sign in to comment.