2021-12-07 17:50:58 +00:00
|
|
|
use std::collections::HashMap;
|
2021-12-06 22:08:22 +00:00
|
|
|
use std::net::SocketAddr;
|
2021-12-07 15:37:22 +00:00
|
|
|
use std::sync::{atomic, Arc};
|
2021-12-06 22:08:22 +00:00
|
|
|
use std::{cmp, time::Duration};
|
|
|
|
|
2021-12-07 16:56:15 +00:00
|
|
|
use anyhow::Result;
|
2022-12-05 17:43:48 +00:00
|
|
|
use opentelemetry::{metrics, KeyValue};
|
2021-12-07 16:56:15 +00:00
|
|
|
|
2021-12-07 17:50:58 +00:00
|
|
|
use futures::future::BoxFuture;
|
2021-12-07 16:56:15 +00:00
|
|
|
use futures::stream::{FuturesUnordered, StreamExt};
|
|
|
|
|
2021-12-06 22:08:22 +00:00
|
|
|
use log::*;
|
2022-01-24 18:28:18 +00:00
|
|
|
use tokio::{select, sync::watch, time::sleep};
|
2021-12-06 22:08:22 +00:00
|
|
|
|
|
|
|
use crate::consul::*;
|
|
|
|
|
|
|
|
// ---- Extract proxy config from Consul catalog ----
|
|
|
|
|
2021-12-08 10:11:22 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum HostDescription {
|
|
|
|
Hostname(String),
|
|
|
|
Pattern(glob::Pattern),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl HostDescription {
|
|
|
|
fn new(desc: &str) -> Result<Self> {
|
|
|
|
if desc.chars().any(|x| matches!(x, '*' | '?' | '[' | ']')) {
|
|
|
|
Ok(Self::Pattern(glob::Pattern::new(desc)?))
|
|
|
|
} else {
|
|
|
|
Ok(Self::Hostname(desc.to_string()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn matches(&self, v: &str) -> bool {
|
|
|
|
match self {
|
|
|
|
Self::Pattern(p) => p.matches(v),
|
|
|
|
Self::Hostname(s) => s == v,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-05 17:43:48 +00:00
|
|
|
impl std::fmt::Display for HostDescription {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
HostDescription::Hostname(h) => write!(f, "{}", h),
|
|
|
|
HostDescription::Pattern(p) => write!(f, "Pattern('{}')", p.as_str()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:08:22 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ProxyEntry {
|
2022-01-13 10:31:08 +00:00
|
|
|
/// Publicly exposed TLS hostnames for matching this rule
|
2021-12-08 10:11:22 +00:00
|
|
|
pub host: HostDescription,
|
2022-01-13 10:31:08 +00:00
|
|
|
/// Path prefix for matching this rule
|
2021-12-06 22:08:22 +00:00
|
|
|
pub path_prefix: Option<String>,
|
2022-01-13 10:31:08 +00:00
|
|
|
/// Priority with which this rule is considered (highest first)
|
2021-12-06 22:08:22 +00:00
|
|
|
pub priority: u32,
|
2022-01-13 10:31:08 +00:00
|
|
|
|
2022-12-05 17:43:48 +00:00
|
|
|
/// Consul service name
|
|
|
|
pub service_name: String,
|
2022-01-13 10:31:08 +00:00
|
|
|
/// Node address (ip+port) to handle requests that match this entry
|
|
|
|
pub target_addr: SocketAddr,
|
|
|
|
/// Is the target serving HTTPS instead of HTTP?
|
|
|
|
pub https_target: bool,
|
|
|
|
|
|
|
|
/// Is the target the same node as we are running on?
|
|
|
|
/// (if yes priorize it over other matching targets)
|
|
|
|
pub same_node: bool,
|
|
|
|
/// Is the target the same site as this node?
|
|
|
|
/// (if yes priorize it over other matching targets)
|
|
|
|
pub same_site: bool,
|
|
|
|
|
|
|
|
/// Add the following headers to all responses returned
|
|
|
|
/// when matching this rule
|
2021-12-07 17:19:51 +00:00
|
|
|
pub add_headers: Vec<(String, String)>,
|
2021-12-07 15:37:22 +00:00
|
|
|
|
2022-12-06 13:02:32 +00:00
|
|
|
/// Number of calls in progress, used to deprioritize slow back-ends
|
|
|
|
pub calls_in_progress: atomic::AtomicI64,
|
|
|
|
/// Time of last call, used for round-robin selection
|
|
|
|
pub last_call: atomic::AtomicI64,
|
2021-12-06 22:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 10:24:25 +00:00
|
|
|
impl std::fmt::Display for ProxyEntry {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2021-12-08 21:58:19 +00:00
|
|
|
if self.https_target {
|
|
|
|
write!(f, "https://")?;
|
|
|
|
}
|
2021-12-08 10:24:25 +00:00
|
|
|
write!(f, "{} ", self.target_addr)?;
|
2021-12-08 11:02:39 +00:00
|
|
|
write!(
|
|
|
|
f,
|
2022-12-05 17:43:48 +00:00
|
|
|
"{}{} {}",
|
|
|
|
self.host,
|
|
|
|
self.path_prefix.as_deref().unwrap_or_default(),
|
2021-12-08 11:02:39 +00:00
|
|
|
self.priority
|
|
|
|
)?;
|
2022-01-13 10:31:08 +00:00
|
|
|
if self.same_node {
|
|
|
|
write!(f, " OURSELF")?;
|
|
|
|
} else if self.same_site {
|
|
|
|
write!(f, " SAME_SITE")?;
|
|
|
|
}
|
2021-12-08 10:24:25 +00:00
|
|
|
if !self.add_headers.is_empty() {
|
2021-12-08 11:27:47 +00:00
|
|
|
write!(f, " +Headers: {:?}", self.add_headers)?;
|
2021-12-08 10:24:25 +00:00
|
|
|
}
|
2022-12-06 13:02:32 +00:00
|
|
|
Ok(())
|
2021-12-08 10:24:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:08:22 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ProxyConfig {
|
|
|
|
pub entries: Vec<ProxyEntry>,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn retry_to_time(retries: u32, max_time: Duration) -> Duration {
|
|
|
|
// 1.2^x seems to be a good value to exponentially increase time at a good pace
|
|
|
|
// eg. 1.2^32 = 341 seconds ~= 5 minutes - ie. after 32 retries we wait 5
|
|
|
|
// minutes
|
2021-12-09 11:20:37 +00:00
|
|
|
Duration::from_secs(cmp::min(
|
2021-12-06 22:08:22 +00:00
|
|
|
max_time.as_secs(),
|
|
|
|
1.2f64.powf(retries as f64) as u64,
|
2021-12-09 11:20:37 +00:00
|
|
|
))
|
2021-12-06 22:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 17:50:58 +00:00
|
|
|
fn parse_tricot_tag(
|
2022-12-05 17:43:48 +00:00
|
|
|
service_name: String,
|
2021-12-07 17:50:58 +00:00
|
|
|
tag: &str,
|
|
|
|
target_addr: SocketAddr,
|
|
|
|
add_headers: &[(String, String)],
|
2022-01-13 10:31:08 +00:00
|
|
|
same_node: bool,
|
|
|
|
same_site: bool,
|
2021-12-07 17:50:58 +00:00
|
|
|
) -> Option<ProxyEntry> {
|
2021-12-06 22:08:22 +00:00
|
|
|
let splits = tag.split(' ').collect::<Vec<_>>();
|
2021-12-08 21:58:19 +00:00
|
|
|
if (splits.len() != 2 && splits.len() != 3)
|
2021-12-09 11:18:23 +00:00
|
|
|
|| (splits[0] != "tricot" && splits[0] != "tricot-https")
|
|
|
|
{
|
2021-12-06 22:08:22 +00:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:36:01 +00:00
|
|
|
let (host, path_prefix) = match splits[1].find('/') {
|
|
|
|
Some(i) => {
|
|
|
|
let (host, pp) = splits[1].split_at(i);
|
|
|
|
(host, Some(pp.to_string()))
|
2021-12-08 16:50:40 +00:00
|
|
|
}
|
2021-12-06 22:08:22 +00:00
|
|
|
None => (splits[1], None),
|
|
|
|
};
|
|
|
|
|
|
|
|
let priority = match splits.len() {
|
|
|
|
3 => splits[2].parse().ok()?,
|
|
|
|
_ => 100,
|
|
|
|
};
|
|
|
|
|
2021-12-08 10:11:22 +00:00
|
|
|
let host = match HostDescription::new(host) {
|
|
|
|
Ok(h) => h,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Invalid hostname pattern {}: {}", host, e);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-12-06 22:08:22 +00:00
|
|
|
Some(ProxyEntry {
|
2022-12-05 17:43:48 +00:00
|
|
|
service_name,
|
2021-12-06 22:08:22 +00:00
|
|
|
target_addr,
|
2021-12-08 21:58:19 +00:00
|
|
|
https_target: (splits[0] == "tricot-https"),
|
2021-12-08 10:11:22 +00:00
|
|
|
host,
|
2022-01-13 10:31:08 +00:00
|
|
|
same_node,
|
|
|
|
same_site,
|
2021-12-06 22:08:22 +00:00
|
|
|
path_prefix,
|
|
|
|
priority,
|
2021-12-07 17:19:51 +00:00
|
|
|
add_headers: add_headers.to_vec(),
|
2022-12-06 13:02:32 +00:00
|
|
|
last_call: atomic::AtomicI64::from(0),
|
|
|
|
calls_in_progress: atomic::AtomicI64::from(0),
|
2021-12-06 22:08:22 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:19:51 +00:00
|
|
|
fn parse_tricot_add_header_tag(tag: &str) -> Option<(String, String)> {
|
2022-05-04 07:14:39 +00:00
|
|
|
let splits = tag.splitn(3, ' ').collect::<Vec<_>>();
|
2021-12-07 17:19:51 +00:00
|
|
|
if splits.len() == 3 && splits[0] == "tricot-add-header" {
|
|
|
|
Some((splits[1].to_string(), splits[2].to_string()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 10:31:08 +00:00
|
|
|
fn parse_consul_catalog(
|
|
|
|
catalog: &ConsulNodeCatalog,
|
|
|
|
same_node: bool,
|
|
|
|
same_site: bool,
|
|
|
|
) -> Vec<ProxyEntry> {
|
2021-12-08 12:28:07 +00:00
|
|
|
trace!("Parsing node catalog: {:#?}", catalog);
|
|
|
|
|
2021-12-06 22:08:22 +00:00
|
|
|
let mut entries = vec![];
|
|
|
|
|
2022-12-05 18:10:15 +00:00
|
|
|
for (_, svc) in catalog.services.iter() {
|
2021-12-06 22:08:22 +00:00
|
|
|
let ip_addr = match svc.address.parse() {
|
|
|
|
Ok(ip) => ip,
|
2021-12-07 17:31:04 +00:00
|
|
|
_ => match catalog.node.address.parse() {
|
|
|
|
Ok(ip) => ip,
|
|
|
|
_ => {
|
2021-12-07 17:50:58 +00:00
|
|
|
warn!(
|
|
|
|
"Could not get address for service {} at node {}",
|
|
|
|
svc.service, catalog.node.node
|
|
|
|
);
|
2021-12-07 17:31:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-07 17:50:58 +00:00
|
|
|
},
|
2021-12-06 22:08:22 +00:00
|
|
|
};
|
|
|
|
let addr = SocketAddr::new(ip_addr, svc.port);
|
2021-12-07 17:19:51 +00:00
|
|
|
|
2022-01-13 11:24:25 +00:00
|
|
|
let (same_node, same_site) = if svc.tags.contains(&"tricot-global-lb".into()) {
|
|
|
|
(false, false)
|
|
|
|
} else if svc.tags.contains(&"tricot-site-lb".into()) {
|
|
|
|
(false, same_site)
|
|
|
|
} else {
|
|
|
|
(same_node, same_site)
|
|
|
|
};
|
|
|
|
|
2021-12-07 17:19:51 +00:00
|
|
|
let mut add_headers = vec![];
|
|
|
|
for tag in svc.tags.iter() {
|
|
|
|
if let Some(pair) = parse_tricot_add_header_tag(tag) {
|
|
|
|
add_headers.push(pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:08:22 +00:00
|
|
|
for tag in svc.tags.iter() {
|
2022-12-05 17:43:48 +00:00
|
|
|
if let Some(ent) = parse_tricot_tag(
|
2022-12-05 18:10:15 +00:00
|
|
|
svc.service.clone(),
|
2022-12-05 17:43:48 +00:00
|
|
|
tag,
|
|
|
|
addr,
|
|
|
|
&add_headers[..],
|
|
|
|
same_node,
|
|
|
|
same_site,
|
|
|
|
) {
|
2021-12-06 22:08:22 +00:00
|
|
|
entries.push(ent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 12:28:07 +00:00
|
|
|
trace!("Result of parsing catalog:");
|
|
|
|
for ent in entries.iter() {
|
|
|
|
trace!(" {}", ent);
|
|
|
|
}
|
|
|
|
|
2021-12-07 16:56:15 +00:00
|
|
|
entries
|
2021-12-06 22:08:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 16:56:15 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
struct NodeWatchState {
|
|
|
|
last_idx: Option<usize>,
|
|
|
|
last_catalog: Option<ConsulNodeCatalog>,
|
|
|
|
retries: u32,
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:28:18 +00:00
|
|
|
pub fn spawn_proxy_config_task(
|
|
|
|
consul: Consul,
|
2022-12-07 13:28:29 +00:00
|
|
|
local_node: String,
|
2022-01-24 18:28:18 +00:00
|
|
|
mut must_exit: watch::Receiver<bool>,
|
|
|
|
) -> watch::Receiver<Arc<ProxyConfig>> {
|
2021-12-07 12:50:44 +00:00
|
|
|
let (tx, rx) = watch::channel(Arc::new(ProxyConfig {
|
2021-12-06 22:08:22 +00:00
|
|
|
entries: Vec::new(),
|
2021-12-07 12:50:44 +00:00
|
|
|
}));
|
2021-12-07 17:50:58 +00:00
|
|
|
|
2022-12-05 17:43:48 +00:00
|
|
|
let metrics = ProxyConfigMetrics::new(rx.clone());
|
2021-12-07 16:56:15 +00:00
|
|
|
let consul = Arc::new(consul);
|
2021-12-06 22:08:22 +00:00
|
|
|
|
|
|
|
tokio::spawn(async move {
|
2021-12-07 16:56:15 +00:00
|
|
|
let mut nodes = HashMap::new();
|
|
|
|
let mut watches = FuturesUnordered::<BoxFuture<'static, (String, Result<_>)>>::new();
|
2021-12-06 22:08:22 +00:00
|
|
|
|
2022-01-13 10:31:08 +00:00
|
|
|
let mut node_site = HashMap::new();
|
|
|
|
|
2022-01-24 18:28:18 +00:00
|
|
|
while !*must_exit.borrow() {
|
|
|
|
let list_nodes = select! {
|
|
|
|
ln = consul.list_nodes() => ln,
|
|
|
|
_ = must_exit.changed() => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
match list_nodes {
|
2021-12-07 16:56:15 +00:00
|
|
|
Ok(consul_nodes) => {
|
|
|
|
info!("Watched consul nodes: {:?}", consul_nodes);
|
2022-01-13 10:31:08 +00:00
|
|
|
for consul_node in consul_nodes {
|
|
|
|
let node = &consul_node.node;
|
|
|
|
if !nodes.contains_key(node) {
|
2021-12-07 16:56:15 +00:00
|
|
|
nodes.insert(node.clone(), NodeWatchState::default());
|
|
|
|
|
|
|
|
let node = node.to_string();
|
|
|
|
let consul = consul.clone();
|
|
|
|
|
|
|
|
watches.push(Box::pin(async move {
|
|
|
|
let res = consul.watch_node(&node, None).await;
|
|
|
|
(node, res)
|
|
|
|
}));
|
|
|
|
}
|
2022-01-13 10:31:08 +00:00
|
|
|
if let Some(site) = consul_node.meta.get("site") {
|
|
|
|
node_site.insert(node.clone(), site.clone());
|
|
|
|
}
|
2021-12-07 16:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
warn!("Could not get Consul node list: {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:28:18 +00:00
|
|
|
let next_watch = select! {
|
|
|
|
nw = watches.next() => nw,
|
|
|
|
_ = must_exit.changed() => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let (node, res): (String, Result<_>) = match next_watch {
|
2021-12-07 16:56:15 +00:00
|
|
|
Some(v) => v,
|
|
|
|
None => {
|
|
|
|
warn!("No nodes currently watched in proxy_config.rs");
|
|
|
|
sleep(Duration::from_secs(10)).await;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok((catalog, new_idx)) => {
|
|
|
|
let mut watch_state = nodes.get_mut(&node).unwrap();
|
|
|
|
watch_state.last_idx = Some(new_idx);
|
|
|
|
watch_state.last_catalog = Some(catalog);
|
|
|
|
watch_state.retries = 0;
|
|
|
|
|
|
|
|
let idx = watch_state.last_idx;
|
|
|
|
let consul = consul.clone();
|
|
|
|
watches.push(Box::pin(async move {
|
|
|
|
let res = consul.watch_node(&node, idx).await;
|
|
|
|
(node, res)
|
|
|
|
}));
|
|
|
|
}
|
2021-12-06 22:08:22 +00:00
|
|
|
Err(e) => {
|
2021-12-07 16:56:15 +00:00
|
|
|
let mut watch_state = nodes.get_mut(&node).unwrap();
|
|
|
|
watch_state.retries += 1;
|
|
|
|
watch_state.last_idx = None;
|
|
|
|
|
2021-12-07 17:50:58 +00:00
|
|
|
let will_retry_in =
|
|
|
|
retry_to_time(watch_state.retries, Duration::from_secs(600));
|
2021-12-06 22:08:22 +00:00
|
|
|
error!(
|
2021-12-07 17:31:04 +00:00
|
|
|
"Failed to query consul for node {}. Will retry in {}s. {}",
|
|
|
|
node,
|
2021-12-06 22:08:22 +00:00
|
|
|
will_retry_in.as_secs(),
|
|
|
|
e
|
|
|
|
);
|
2021-12-07 16:56:15 +00:00
|
|
|
|
|
|
|
let consul = consul.clone();
|
|
|
|
watches.push(Box::pin(async move {
|
|
|
|
sleep(will_retry_in).await;
|
|
|
|
let res = consul.watch_node(&node, None).await;
|
|
|
|
(node, res)
|
|
|
|
}));
|
2021-12-06 22:08:22 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-07 16:56:15 +00:00
|
|
|
}
|
2021-12-06 22:08:22 +00:00
|
|
|
|
2021-12-07 16:56:15 +00:00
|
|
|
let mut entries = vec![];
|
2022-01-13 10:31:08 +00:00
|
|
|
for (node_name, watch_state) in nodes.iter() {
|
2021-12-07 16:56:15 +00:00
|
|
|
if let Some(catalog) = &watch_state.last_catalog {
|
2022-12-07 13:28:29 +00:00
|
|
|
let same_node = *node_name == local_node;
|
|
|
|
let same_site = match (node_site.get(node_name), node_site.get(&local_node)) {
|
|
|
|
(Some(s1), Some(s2)) => s1 == s2,
|
|
|
|
_ => false,
|
|
|
|
};
|
2022-01-13 10:31:08 +00:00
|
|
|
|
|
|
|
entries.extend(parse_consul_catalog(catalog, same_node, same_site));
|
2021-12-07 16:56:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
let config = ProxyConfig { entries };
|
2021-12-06 22:08:22 +00:00
|
|
|
|
2021-12-07 12:50:44 +00:00
|
|
|
tx.send(Arc::new(config)).expect("Internal error");
|
2021-12-06 22:08:22 +00:00
|
|
|
}
|
2022-12-05 17:43:48 +00:00
|
|
|
|
|
|
|
drop(metrics); // ensure Metrics lives up to here
|
2021-12-06 22:08:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
rx
|
|
|
|
}
|
2022-05-04 07:14:39 +00:00
|
|
|
|
2022-12-05 17:43:48 +00:00
|
|
|
// ----
|
|
|
|
|
|
|
|
struct ProxyConfigMetrics {
|
|
|
|
_proxy_config_entries: metrics::ValueObserver<u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProxyConfigMetrics {
|
|
|
|
fn new(rx: watch::Receiver<Arc<ProxyConfig>>) -> Self {
|
|
|
|
let meter = opentelemetry::global::meter("tricot");
|
|
|
|
Self {
|
|
|
|
_proxy_config_entries: meter
|
|
|
|
.u64_value_observer("proxy_config_entries", move |observer| {
|
|
|
|
let mut patterns = HashMap::new();
|
|
|
|
for ent in rx.borrow().entries.iter() {
|
2022-12-05 18:10:15 +00:00
|
|
|
let attrs = (
|
|
|
|
ent.host.to_string(),
|
|
|
|
ent.path_prefix.clone().unwrap_or_default(),
|
|
|
|
ent.service_name.clone(),
|
2022-12-05 17:43:48 +00:00
|
|
|
);
|
2022-12-05 18:10:15 +00:00
|
|
|
*patterns.entry(attrs).or_default() += 1;
|
2022-12-05 17:43:48 +00:00
|
|
|
}
|
2022-12-05 18:10:15 +00:00
|
|
|
for ((host, prefix, svc), num) in patterns {
|
|
|
|
observer.observe(
|
|
|
|
num,
|
|
|
|
&[
|
|
|
|
KeyValue::new("host", host),
|
|
|
|
KeyValue::new("path_prefix", prefix),
|
|
|
|
KeyValue::new("service", svc),
|
|
|
|
],
|
|
|
|
);
|
2022-12-05 17:43:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.with_description("Number of proxy entries (back-ends) configured in Tricot")
|
|
|
|
.init(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----
|
|
|
|
|
2022-05-04 07:14:39 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-05-06 10:21:15 +00:00
|
|
|
use super::*;
|
2022-05-04 07:14:39 +00:00
|
|
|
|
2022-05-06 10:21:15 +00:00
|
|
|
#[test]
|
|
|
|
fn test_parse_tricot_add_header_tag() {
|
|
|
|
match parse_tricot_add_header_tag("tricot-add-header Content-Security-Policy default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'") {
|
2022-05-04 07:14:39 +00:00
|
|
|
Some((name, value)) => {
|
|
|
|
assert_eq!(name, "Content-Security-Policy");
|
|
|
|
assert_eq!(value, "default-src 'none'; img-src 'self'; script-src 'self'; style-src 'self'");
|
|
|
|
}
|
|
|
|
_ => panic!("Passed a valid tag but the function says it is not valid")
|
|
|
|
}
|
2022-05-06 10:21:15 +00:00
|
|
|
}
|
2022-05-04 07:14:39 +00:00
|
|
|
}
|