1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::{collections::HashMap, future::Future, pin::Pin, task::Poll};
use covert_types::auth::AuthPolicy;
use covert_types::error::ApiError;
use covert_types::request::{Operation, Request};
use covert_types::response::Response;
use tower::{util::BoxCloneService, Service};
use tower::{Layer, ServiceExt};
use covert_types::state::VaultState;
use super::handler::Handler;
#[derive(Debug, Clone)]
pub struct Route {
handler: BoxCloneService<Request, Response, ApiError>,
config: RouteConfig,
}
#[derive(Debug, Clone)]
pub struct RouteConfig {
pub policy: AuthPolicy,
pub state: Vec<VaultState>,
}
impl RouteConfig {
#[must_use]
pub fn unauthenticated() -> Self {
Self {
policy: AuthPolicy::Unauthenticated,
..Default::default()
}
}
}
impl Default for RouteConfig {
fn default() -> Self {
Self {
policy: AuthPolicy::Authenticated,
state: vec![VaultState::Unsealed],
}
}
}
impl Route {
#[must_use]
pub fn new(handler: BoxCloneService<Request, Response, ApiError>, config: RouteConfig) -> Self {
Self { handler, config }
}
}
impl Service<Request> for Route {
type Response = Response;
type Error = ApiError;
type Future =
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
fn poll_ready(&mut self, cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
self.handler.poll_ready(cx)
}
fn call(&mut self, req: Request) -> Self::Future {
let state = req
.extensions
.get::<VaultState>()
.expect("vault should always have a state");
if !self.config.state.contains(state) {
let state = *state;
return Box::pin(async move { Err(ApiError::invalid_state(state)) });
}
let policy = match req.extensions.get::<AuthPolicy>() {
Some(auth) => auth,
None => return Box::pin(async { Err(ApiError::unauthorized()) }),
};
let auth = match self.config.policy {
AuthPolicy::Root => *policy == AuthPolicy::Root,
AuthPolicy::Authenticated => {
matches!(policy, AuthPolicy::Root | AuthPolicy::Authenticated)
}
AuthPolicy::Unauthenticated => true,
};
if !auth {
return Box::pin(async { Err(ApiError::unauthorized()) });
}
self.handler.call(req)
}
}
#[derive(Debug, Clone)]
pub struct MethodRouter {
routes: HashMap<Operation, Route>,
}
impl Default for MethodRouter {
fn default() -> Self {
Self::new()
}
}
macro_rules! chained_handlers {
($operation:ident, $method:ident, $method_with_config:ident) => {
#[must_use]
pub fn $method<H, T>(mut self, handler: H) -> Self
where
H: Handler<T>,
T: Send + 'static,
{
let route = handler.into_route(RouteConfig::default());
self.routes.insert(Operation::$operation, route);
self
}
#[must_use]
pub fn $method_with_config<H, T>(mut self, handler: H, config: RouteConfig) -> Self
where
H: Handler<T>,
T: Send + 'static,
{
let route = handler.into_route(config);
self.routes.insert(Operation::$operation, route);
self
}
};
}
macro_rules! top_level_handlers {
($operation:ident, $method:ident, $method_with_config:ident) => {
#[must_use]
pub fn $method<H, T>(handler: H) -> MethodRouter
where
H: Handler<T>,
T: Send + 'static,
{
MethodRouter::new().on(Operation::$operation, handler, RouteConfig::default())
}
#[must_use]
pub fn $method_with_config<H, T>(handler: H, config: RouteConfig) -> MethodRouter
where
H: Handler<T>,
T: Send + 'static,
{
MethodRouter::new().on(Operation::$operation, handler, config)
}
};
}
top_level_handlers!(Create, create, create_with_config);
top_level_handlers!(Read, read, read_with_config);
top_level_handlers!(Update, update, update_with_config);
top_level_handlers!(Delete, delete, delete_with_config);
top_level_handlers!(Revoke, revoke, revoke_with_config);
top_level_handlers!(Renew, renew, renew_with_config);
impl MethodRouter {
#[must_use]
pub fn new() -> Self {
Self {
routes: HashMap::default(),
}
}
chained_handlers!(Create, create, create_with_config);
chained_handlers!(Read, read, read_with_config);
chained_handlers!(Update, update, update_with_config);
chained_handlers!(Delete, delete, delete_with_config);
chained_handlers!(Revoke, revoke, revoke_with_config);
chained_handlers!(Renew, renew, renew_with_config);
#[must_use]
pub fn on<H, T>(mut self, operation: Operation, handler: H, config: RouteConfig) -> Self
where
H: Handler<T>,
T: Send + 'static,
{
let route = handler.into_route(config);
self.routes.insert(operation, route);
self
}
#[must_use]
pub fn layer<L>(self, layer: L) -> Self
where
L: Layer<Route>,
L::Service:
Service<Request, Error = ApiError, Response = Response> + Clone + Send + 'static,
<L::Service as Service<Request>>::Future: Send + 'static,
{
let routes = self
.routes
.into_iter()
.map(|(op, route)| {
let config = route.config.clone();
let svc = layer.layer(route);
let svc = BoxCloneService::new(svc);
let route = Route::new(svc, config);
(op, route)
})
.collect();
Self { routes }
}
}
impl Service<Request> for MethodRouter {
type Response = Response;
type Error = ApiError;
type Future = Pin<Box<dyn Future<Output = Result<Response, ApiError>> + Send + 'static>>;
fn poll_ready(&mut self, _cx: &mut std::task::Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, req: Request) -> Self::Future {
let route = self.routes.get(&req.operation).map(Clone::clone);
Box::pin(async move {
match route {
Some(route) => route.oneshot(req).await,
None => Err(ApiError::not_found()),
}
})
}
}