Skip to content

Commit

Permalink
HttpFrontend::route has type Option<ClusterId>
Browse files Browse the repository at this point in the history
move type Route to sozu_lib::router
  • Loading branch information
Keksoj committed Mar 27, 2023
1 parent 31d689a commit f03aac9
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 125 deletions.
6 changes: 3 additions & 3 deletions bin/src/acme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sozu_command_lib::{
request::{
AddBackend, AddCertificate, RemoveBackend, ReplaceCertificate, Request, RequestHttpFrontend,
},
response::{PathRule, Response, ResponseStatus, Route, RulePosition},
response::{PathRule, Response, ResponseStatus, RulePosition},
};

use crate::util;
Expand Down Expand Up @@ -288,7 +288,7 @@ fn set_up_proxying(
server_address: &SocketAddr,
) -> anyhow::Result<()> {
let add_http_front = Request::AddHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(cluster_id.to_owned()),
route: Some(cluster_id.to_owned()),
hostname: String::from(hostname),
address: frontend.to_string(),
path: PathRule::prefix(path_begin.to_owned()),
Expand Down Expand Up @@ -321,7 +321,7 @@ fn remove_proxying(
server_address: SocketAddr,
) -> anyhow::Result<()> {
let remove_http_front = Request::RemoveHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(cluster_id.to_owned()),
route: Some(cluster_id.to_owned()),
address: frontend.to_string(),
hostname: String::from(hostname),
path: PathRule::prefix(path_begin.to_owned()),
Expand Down
12 changes: 7 additions & 5 deletions bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::{collections::BTreeMap, net::SocketAddr};

use clap::{Parser, Subcommand};

use sozu_command_lib::{certificate::TlsVersion, request::LoadBalancingAlgorithms};
use sozu_command_lib::{
certificate::TlsVersion, request::LoadBalancingAlgorithms, state::ClusterId,
};

#[derive(Parser, PartialEq, Eq, Clone, Debug)]
#[clap(author, version, about)]
Expand Down Expand Up @@ -434,11 +436,11 @@ pub enum Route {
}

#[allow(clippy::from_over_into)]
impl std::convert::Into<sozu_command_lib::response::Route> for Route {
fn into(self) -> sozu_command_lib::response::Route {
impl std::convert::Into<Option<ClusterId>> for Route {
fn into(self) -> Option<ClusterId> {
match self {
Route::Deny => sozu_command_lib::response::Route::Deny,
Route::Id { id } => sozu_command_lib::response::Route::ClusterId(id),
Route::Deny => None,
Route::Id { id } => Some(id),
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions bin/src/ctl/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use prettytable::{Row, Table};

use sozu_command_lib::response::{
AggregatedMetrics, AvailableMetrics, ClusterMetrics, FilteredMetrics, ListedFrontends,
ListenersList, ResponseContent, Route, WorkerInfo, WorkerMetrics,
ListenersList, ResponseContent, WorkerInfo, WorkerMetrics,
};

pub fn print_listeners(listeners_list: ListenersList) {
Expand Down Expand Up @@ -137,7 +137,7 @@ pub fn print_frontend_list(frontends: ListedFrontends) {
]);
for http_frontend in frontends.http_frontends.iter() {
table.add_row(row!(
http_frontend.route,
http_frontend.route.clone().unwrap_or("Deny".to_owned()),
http_frontend.address.to_string(),
http_frontend.hostname.to_string(),
format!("{:?}", http_frontend.path),
Expand All @@ -159,7 +159,7 @@ pub fn print_frontend_list(frontends: ListedFrontends) {
]);
for https_frontend in frontends.https_frontends.iter() {
table.add_row(row!(
https_frontend.route,
https_frontend.route.clone().unwrap_or("Deny".to_owned()),
https_frontend.address.to_string(),
https_frontend.hostname.to_string(),
format!("{:?}", https_frontend.path),
Expand Down Expand Up @@ -481,8 +481,8 @@ pub fn print_query_response_data(
for (key, values) in frontend_data.iter() {
let mut row = Vec::new();
match &key.route {
Route::ClusterId(cluster_id) => row.push(cell!(cluster_id)),
Route::Deny => row.push(cell!("-")),
Some(cluster_id) => row.push(cell!(cluster_id)),
None => row.push(cell!("-")),
}
row.push(cell!(key.hostname));
row.push(cell!(key.path));
Expand All @@ -505,8 +505,8 @@ pub fn print_query_response_data(
for (key, values) in https_frontend_data.iter() {
let mut row = Vec::new();
match &key.route {
Route::ClusterId(cluster_id) => row.push(cell!(cluster_id)),
Route::Deny => row.push(cell!("-")),
Some(cluster_id) => row.push(cell!(cluster_id)),
None => row.push(cell!("-")),
}
row.push(cell!(key.hostname));
row.push(cell!(key.path));
Expand Down
4 changes: 1 addition & 3 deletions command/assets/add_http_front.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"type": "ADD_HTTP_FRONTEND",
"content": {
"route": {
"CLUSTER_ID": "xxx"
},
"route": "xxx",
"address": "0.0.0.0:8080",
"hostname": "yyy",
"path": {
Expand Down
4 changes: 1 addition & 3 deletions command/assets/add_https_front.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"type": "ADD_HTTPS_FRONTEND",
"content": {
"route": {
"CLUSTER_ID": "xxx"
},
"route": "xxx",
"address": "0.0.0.0:8443",
"hostname": "yyy",
"path": {
Expand Down
4 changes: 1 addition & 3 deletions command/assets/remove_http_front.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"type": "REMOVE_HTTP_FRONTEND",
"content": {
"route": {
"CLUSTER_ID": "xxx"
},
"route": "xxx",
"address": "0.0.0.0:8080",
"hostname": "yyy",
"path": {
Expand Down
4 changes: 1 addition & 3 deletions command/assets/remove_https_front.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"type": "REMOVE_HTTPS_FRONTEND",
"content": {
"route": {
"CLUSTER_ID": "xxx"
},
"route": "xxx",
"address": "0.0.0.0:8443",
"hostname": "yyy",
"path": {
Expand Down
6 changes: 3 additions & 3 deletions command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
RequestTcpFrontend, WorkerRequest,
},
response::{
HttpListenerConfig, HttpsListenerConfig, PathRule, Route, RulePosition, TcpListenerConfig,
HttpListenerConfig, HttpsListenerConfig, PathRule, RulePosition, TcpListenerConfig,
},
};

Expand Down Expand Up @@ -737,7 +737,7 @@ impl HttpFrontendConfig {
}));

v.push(Request::AddHttpsFrontend(RequestHttpFrontend {
route: Route::ClusterId(cluster_id.to_string()),
route: Some(cluster_id.to_string()),
address: self.address.to_string(),
hostname: self.hostname.clone(),
path: self.path.clone(),
Expand All @@ -748,7 +748,7 @@ impl HttpFrontendConfig {
} else {
//create the front both for HTTP and HTTPS if possible
v.push(Request::AddHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(cluster_id.to_string()),
route: Some(cluster_id.to_string()),
address: self.address.to_string(),
hostname: self.hostname.clone(),
path: self.path.clone(),
Expand Down
35 changes: 18 additions & 17 deletions command/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
config::ProxyProtocolConfig,
response::{
is_default_path_rule, HttpFrontend, HttpListenerConfig, HttpsListenerConfig, MessageId,
PathRule, Route, RulePosition, TcpListenerConfig,
PathRule, RulePosition, TcpListenerConfig,
},
state::{ClusterId, RouteKey},
};
Expand Down Expand Up @@ -315,7 +315,8 @@ pub struct RequestTcpFrontend {
/// A frontend as requested from the client, with a string SocketAddress
#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RequestHttpFrontend {
pub route: Route,
/// the route is DENY if none
pub route: Option<ClusterId>,
pub address: String,
pub hostname: String,
#[serde(default)]
Expand Down Expand Up @@ -458,19 +459,19 @@ mod tests {
use super::*;
use crate::certificate::{split_certificate_chain, TlsVersion};
use crate::config::ProxyProtocolConfig;
use crate::response::{HttpFrontend, PathRule, Route, RulePosition};
use crate::response::{HttpFrontend, PathRule, RulePosition};
use hex::FromHex;
use serde_json;

#[test]
fn config_message_test() {
let raw_json = r#"{ "id": "ID_TEST", "version": 0, "type": "ADD_HTTP_FRONTEND", "content":{"route": {"CLUSTER_ID": "xxx"}, "hostname": "yyy", "path": {"kind": "PREFIX", "value": "xxx"}, "address": "0.0.0.0:8080"}}"#;
let raw_json = r#"{ "id": "ID_TEST", "version": 0, "type": "ADD_HTTP_FRONTEND", "content":{"route": "xxx", "hostname": "yyy", "path": {"kind": "PREFIX", "value": "xxx"}, "address": "0.0.0.0:8080"}}"#;
let message: Request = serde_json::from_str(raw_json).unwrap();
println!("{message:?}");
assert_eq!(
message,
Request::AddHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(String::from("xxx")),
route: Some(String::from("xxx")),
hostname: String::from("yyy"),
path: PathRule::prefix(String::from("xxx")),
method: None,
Expand Down Expand Up @@ -524,7 +525,7 @@ mod tests {
add_http_front,
"../assets/add_http_front.json",
Request::AddHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(String::from("xxx")),
route: Some(String::from("xxx")),
hostname: String::from("yyy"),
path: PathRule::prefix(String::from("xxx")),
method: None,
Expand All @@ -538,7 +539,7 @@ mod tests {
remove_http_front,
"../assets/remove_http_front.json",
Request::RemoveHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(String::from("xxx")),
route: Some(String::from("xxx")),
hostname: String::from("yyy"),
path: PathRule::prefix(String::from("xxx")),
method: None,
Expand All @@ -558,7 +559,7 @@ mod tests {
add_https_front,
"../assets/add_https_front.json",
Request::AddHttpsFrontend(RequestHttpFrontend {
route: Route::ClusterId(String::from("xxx")),
route: Some(String::from("xxx")),
hostname: String::from("yyy"),
path: PathRule::prefix(String::from("xxx")),
method: None,
Expand All @@ -572,7 +573,7 @@ mod tests {
remove_https_front,
"../assets/remove_https_front.json",
Request::RemoveHttpsFrontend(RequestHttpFrontend {
route: Route::ClusterId(String::from("xxx")),
route: Some(String::from("xxx")),
hostname: String::from("yyy"),
path: PathRule::prefix(String::from("xxx")),
method: None,
Expand Down Expand Up @@ -689,13 +690,13 @@ mod tests {

#[test]
fn add_front_test() {
let raw_json = r#"{"type": "ADD_HTTP_FRONTEND", "content": {"route": { "CLUSTER_ID": "xxx"}, "hostname": "yyy", "path": {"kind": "PREFIX", "value": "xxx"}, "address": "127.0.0.1:4242", "sticky_session": false}}"#;
let raw_json = r#"{"type": "ADD_HTTP_FRONTEND", "content": {"route": "xxx", "hostname": "yyy", "path": {"kind": "PREFIX", "value": "xxx"}, "address": "127.0.0.1:4242", "sticky_session": false}}"#;
let command: Request = serde_json::from_str(raw_json).expect("could not parse json");
println!("{command:?}");
assert!(
command
== Request::AddHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(String::from("xxx")),
route: Some(String::from("xxx")),
hostname: String::from("yyy"),
path: PathRule::prefix(String::from("xxx")),
method: None,
Expand All @@ -708,13 +709,13 @@ mod tests {

#[test]
fn remove_front_test() {
let raw_json = r#"{"type": "REMOVE_HTTP_FRONTEND", "content": {"route": {"CLUSTER_ID": "xxx"}, "hostname": "yyy", "path": {"kind": "PREFIX", "value": "xxx"}, "address": "127.0.0.1:4242", "tags": { "owner": "John", "id": "some-long-id" }}}"#;
let raw_json = r#"{"type": "REMOVE_HTTP_FRONTEND", "content": {"route": "xxx", "hostname": "yyy", "path": {"kind": "PREFIX", "value": "xxx"}, "address": "127.0.0.1:4242", "tags": { "owner": "John", "id": "some-long-id" }}}"#;
let command: Request = serde_json::from_str(raw_json).expect("could not parse json");
println!("{command:?}");
assert!(
command
== Request::RemoveHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(String::from("xxx")),
route: Some(String::from("xxx")),
hostname: String::from("yyy"),
path: PathRule::prefix(String::from("xxx")),
method: None,
Expand Down Expand Up @@ -763,13 +764,13 @@ mod tests {

#[test]
fn http_front_crash_test() {
let raw_json = r#"{"type": "ADD_HTTP_FRONTEND", "content": {"route": {"CLUSTER_ID": "aa"}, "hostname": "cltdl.fr", "path": {"kind": "PREFIX", "value": ""}, "address": "127.0.0.1:4242", "tags": { "owner": "John", "id": "some-long-id" }}}"#;
let raw_json = r#"{"type": "ADD_HTTP_FRONTEND", "content": {"route": "aa", "hostname": "cltdl.fr", "path": {"kind": "PREFIX", "value": ""}, "address": "127.0.0.1:4242", "tags": { "owner": "John", "id": "some-long-id" }}}"#;
let command: Request = serde_json::from_str(raw_json).expect("could not parse json");
println!("{command:?}");
assert!(
command
== Request::AddHttpFrontend(RequestHttpFrontend {
route: Route::ClusterId(String::from("aa")),
route: Some(String::from("aa")),
hostname: String::from("cltdl.fr"),
path: PathRule::prefix(String::from("")),
method: None,
Expand All @@ -785,13 +786,13 @@ mod tests {

#[test]
fn http_front_crash_test2() {
let raw_json = r#"{"route": {"CLUSTER_ID": "aa"}, "hostname": "cltdl.fr", "path": {"kind": "PREFIX", "value": ""}, "address": "127.0.0.1:4242" }"#;
let raw_json = r#"{"route": "aa", "hostname": "cltdl.fr", "path": {"kind": "PREFIX", "value": ""}, "address": "127.0.0.1:4242" }"#;
let front: HttpFrontend = serde_json::from_str(raw_json).expect("could not parse json");
println!("{front:?}");
assert!(
front
== HttpFrontend {
route: Route::ClusterId(String::from("aa")),
route: Some(String::from("aa")),
hostname: String::from("cltdl.fr"),
path: PathRule::prefix(String::from("")),
method: None,
Expand Down
24 changes: 3 additions & 21 deletions command/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
default_sticky_name, is_false, AddBackend, Cluster, LoadBalancingParams,
RequestHttpFrontend, RequestTcpFrontend, PROTOCOL_VERSION,
},
state::{ClusterId, ConfigState},
state::{ConfigState, ClusterId},
};

/// Responses of the main process to the CLI (or other client)
Expand Down Expand Up @@ -107,7 +107,8 @@ pub struct AvailableMetrics {

#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct HttpFrontend {
pub route: Route,
/// The route is DENY if None
pub route: Option<ClusterId>,
pub address: SocketAddr,
pub hostname: String,
#[serde(default)]
Expand Down Expand Up @@ -135,25 +136,6 @@ impl Into<RequestHttpFrontend> for HttpFrontend {
}
}

/// The cluster to which the traffic will be redirected
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum Route {
/// send a 401 default answer
Deny,
/// the cluster to which the frontend belongs
ClusterId(ClusterId),
}

impl std::fmt::Display for Route {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Route::Deny => write!(f, "deny"),
Route::ClusterId(string) => write!(f, "{string}"),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RulePosition {
Expand Down

0 comments on commit f03aac9

Please sign in to comment.