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

fix: make fe tokens respect token cache #241

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 44 additions & 4 deletions server/src/http/feature_refresher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ use crate::{
types::{ClientFeaturesRequest, ClientFeaturesResponse, EdgeToken, TokenRefresh},
};

fn frontend_token_is_covered_by_tokens(
frontend_token: &EdgeToken,
tokens_to_refresh: Arc<DashMap<String, TokenRefresh>>,
) -> bool {
tokens_to_refresh.iter().any(|client_token| {
client_token
.token
.same_environment_and_broader_or_equal_project_access(frontend_token)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the magic, this flips fe token and present tokens - we previously did the check the wrong way around

})
}

#[derive(Clone)]
pub struct FeatureRefresher {
pub unleash_client: Arc<UnleashClient>,
Expand Down Expand Up @@ -119,9 +130,7 @@ impl FeatureRefresher {
&self,
frontend_token: &EdgeToken,
) -> bool {
self.tokens_to_refresh.iter().any(|client_token| {
frontend_token.same_environment_and_broader_or_equal_project_access(&client_token.token)
})
frontend_token_is_covered_by_tokens(frontend_token, self.tokens_to_refresh.clone())
}

pub(crate) async fn register_and_hydrate_token(
Expand Down Expand Up @@ -364,7 +373,7 @@ mod tests {
types::{EdgeToken, TokenRefresh},
};

use super::FeatureRefresher;
use super::{frontend_token_is_covered_by_tokens, FeatureRefresher};

impl PartialEq for TokenRefresh {
fn eq(&self, other: &Self) -> bool {
Expand Down Expand Up @@ -998,4 +1007,35 @@ mod tests {
7
);
}

#[test]
fn front_end_token_is_properly_covered_by_current_tokens() {
let fe_token = EdgeToken {
projects: vec!["a".into(), "b".into()],
environment: Some("development".into()),
..Default::default()
};

let wildcard_token = EdgeToken {
projects: vec!["*".into()],
environment: Some("development".into()),
..Default::default()
};

let current_tokens = DashMap::new();
let token_refresh = TokenRefresh {
token: wildcard_token.clone(),
etag: None,
last_refreshed: None,
last_check: None,
};

current_tokens.insert(wildcard_token.token, token_refresh);

let current_tokens_arc = Arc::new(current_tokens);
assert!(frontend_token_is_covered_by_tokens(
&fe_token,
current_tokens_arc
));
}
}
78 changes: 77 additions & 1 deletion server/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl EdgeToken {
mod tests {
use crate::{
tokens::simplify,
types::{EdgeToken, TokenRefresh},
types::{EdgeToken, TokenRefresh, TokenType},
};
use ulid::Ulid;

Expand Down Expand Up @@ -311,4 +311,80 @@ mod tests {

assert_eq!(actual, expected);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These aren't all that relevant but they were missing tests and nice to add while we're in this space

#[test]
fn test_single_project_token_is_covered_by_wildcard() {
let self_token = EdgeToken {
projects: vec!["*".into()],
environment: Some("development".into()),
..Default::default()
};

let other_token = EdgeToken {
projects: vec!["A".into()],
environment: Some("development".into()),
..Default::default()
};

let is_covered =
self_token.same_environment_and_broader_or_equal_project_access(&other_token);
assert!(is_covered);
}

#[test]
fn test_multi_project_token_is_covered_by_wildcard() {
let self_token = EdgeToken {
projects: vec!["*".into()],
environment: Some("development".into()),
..Default::default()
};

let other_token = EdgeToken {
projects: vec!["A".into(), "B".into()],
environment: Some("development".into()),
..Default::default()
};

let is_covered =
self_token.same_environment_and_broader_or_equal_project_access(&other_token);
assert!(is_covered);
}

#[test]
fn test_multi_project_tokens_cover_each_other() {
let self_token = EdgeToken {
projects: vec!["A".into(), "B".into()],
environment: Some("development".into()),
..Default::default()
};

let fe_token = EdgeToken {
projects: vec!["A".into()],
environment: Some("development".into()),
token_type: Some(TokenType::Frontend),
..Default::default()
};

let is_covered = self_token.same_environment_and_broader_or_equal_project_access(&fe_token);
assert!(is_covered);
}

#[test]
fn test_multi_project_tokens_do_not_cover_each_other_when_they_do_not_overlap() {
let self_token = EdgeToken {
projects: vec!["A".into(), "B".into()],
environment: Some("development".into()),
..Default::default()
};

let fe_token = EdgeToken {
projects: vec!["A".into(), "C".into()],
environment: Some("development".into()),
token_type: Some(TokenType::Frontend),
..Default::default()
};

let is_covered = self_token.same_environment_and_broader_or_equal_project_access(&fe_token);
assert!(!is_covered);
}
}
Loading