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

feat: added support for client and frontend tokens to offline mode. #465

Merged
merged 9 commits into from
Sep 19, 2024
55 changes: 49 additions & 6 deletions server/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
use chrono::Duration;
use dashmap::DashMap;
use reqwest::Url;
use rustls::client;
Fixed Show fixed Hide fixed
use tracing::{debug, warn};
use unleash_types::client_features::ClientFeatures;
use unleash_yggdrasil::EngineState;

use crate::cli::RedisMode;
use crate::cli::{self, RedisMode};
Fixed Show fixed Hide fixed
use crate::offline::offline_hotload::{load_bootstrap, load_offline_engine_cache};
use crate::persistence::file::FilePersister;
use crate::persistence::redis::RedisPersister;
Expand Down Expand Up @@ -46,10 +47,7 @@
)
}

async fn hydrate_from_persistent_storage(
cache: CacheContainer,
storage: Arc<dyn EdgePersistence>,
) {
async fn hydrate_from_persistent_storage(cache: CacheContainer, storage: Arc<dyn EdgePersistence>) {
let (token_cache, features_cache, engine_cache) = cache;
let tokens = storage.load_tokens().await.unwrap_or_else(|error| {
warn!("Failed to load tokens from cache {error:?}");
Expand Down Expand Up @@ -80,14 +78,36 @@
pub(crate) fn build_offline_mode(
client_features: ClientFeatures,
tokens: Vec<String>,
client_tokens: Vec<String>,
frontend_tokens: Vec<String>,
) -> EdgeResult<CacheContainer> {
let (token_cache, features_cache, engine_cache) = build_caches();

let edge_tokens: Vec<EdgeToken> = tokens
.iter()
.map(|token| EdgeToken::from_str(token).unwrap_or_else(|_| EdgeToken::offline_token(token)))
.map(|mut token| {
token.token_type = Some(TokenType::Client);
token
})
.collect();

let edge_client_tokens: Vec<EdgeToken> = client_tokens
.iter()
.map(|token| EdgeToken::from_str(token).unwrap_or_else(|_| EdgeToken::offline_token(token)))
.map(|mut token| {
token.token_type = Some(TokenType::Client);
token
})
.collect();
let edge_frontend_tokens: Vec<EdgeToken> = frontend_tokens
.iter()
.map(|token| EdgeToken::from_str(token).unwrap_or_else(|_| EdgeToken::offline_token(token)))
.map(|mut token| {
token.token_type = Some(TokenType::Frontend);
token
})
.collect();
for edge_token in edge_tokens {
token_cache.insert(edge_token.token.clone(), edge_token.clone());

Expand All @@ -98,6 +118,24 @@
client_features.clone(),
);
}
for client_token in edge_client_tokens {
token_cache.insert(client_token.token.clone(), client_token.clone());
load_offline_engine_cache(
&client_token,
features_cache.clone(),
engine_cache.clone(),
client_features.clone(),
);
}
for frontend_token in edge_frontend_tokens {
token_cache.insert(frontend_token.token.clone(), frontend_token.clone());
load_offline_engine_cache(
&frontend_token,
features_cache.clone(),
engine_cache.clone(),
client_features.clone(),
)
}
Ok((token_cache, features_cache, engine_cache))
}

Expand All @@ -114,7 +152,12 @@

let client_features = load_bootstrap(&bootstrap)?;

build_offline_mode(client_features, offline_args.tokens)
build_offline_mode(
client_features,
offline_args.tokens,
offline_args.client_tokens,
offline_args.frontend_tokens,
)
} else {
Err(EdgeError::NoFeaturesFile)
}
Expand Down
6 changes: 6 additions & 0 deletions server/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ pub struct OfflineArgs {
/// Tokens that should be allowed to connect to Edge. Supports a comma separated list or multiple instances of the `--tokens` argument
#[clap(short, long, env, value_delimiter = ',')]
pub tokens: Vec<String>,
chriswk marked this conversation as resolved.
Show resolved Hide resolved
/// Client tokens that should be allowed to connect to Edge. Supports a comma separated list or multiple instances of the `--client-tokens` argument
#[clap(short, long, env, value_delimiter = ',')]
pub client_tokens: Vec<String>,
/// Frontend tokens that should be allowed to connect to Edge. Supports a comma separated list or multiple instances of the `--frontend-tokens` argument
#[clap(short, long, env, value_delimiter = ',')]
pub frontend_tokens: Vec<String>,
/// The interval in seconds between reloading the bootstrap file. Disabled if unset or 0
#[clap(short, long, env, default_value_t = 0)]
pub reload_interval: u64,
Expand Down
Loading