Repository navigation
Expand file tree
/
Copy pathdefinitions_api.rs
More file actions
154 lines (144 loc) · 4.77 KB
/
Copy pathdefinitions_api.rs
File metadata and controls
154 lines (144 loc) · 4.77 KB
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
use crate::{definition::Definition, error, inventory::AppState, store, ApiError};
use axum::{
extract::{Path, State},
http::{header, HeaderMap, StatusCode},
response::{IntoResponse, Response},
Json,
};
use serde::Serialize;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Validated {
name: String,
hash: String,
yaml: String,
}
type Problem = (StatusCode, Json<serde_json::Value>);
fn problem(status: StatusCode, code: &str, message: &str) -> Problem {
(
status,
Json(serde_json::json!({"error":{"code":code,"message":message}})),
)
}
fn parse(
headers: &HeaderMap,
body: Result<String, axum::extract::rejection::StringRejection>,
) -> Result<Definition, Problem> {
if headers.contains_key(header::ORIGIN) {
return Err(problem(
StatusCode::FORBIDDEN,
"origin_forbidden",
"Browser origins cannot modify local definitions.",
));
}
if headers
.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.and_then(|v| v.split(';').next())
.map(str::trim)
!= Some("application/yaml")
{
return Err(problem(
StatusCode::UNSUPPORTED_MEDIA_TYPE,
"unsupported_media_type",
"Send YAML using Content-Type: application/yaml.",
));
}
let yaml = body.map_err(|e| {
problem(
e.status(),
"invalid_body",
"YAML body is invalid or exceeds 64 KiB.",
)
})?;
Definition::parse(&yaml).map_err(|issue| {
(
StatusCode::UNPROCESSABLE_ENTITY,
Json(serde_json::json!({
"error": {"code":"invalid_definition", "message":issue.message, "field":issue.field}
})),
)
})
}
pub async fn validate(
headers: HeaderMap,
yaml: Result<String, axum::extract::rejection::StringRejection>,
) -> Response {
let definition = match parse(&headers, yaml) {
Ok(d) => d,
Err(e) => return e.into_response(),
};
match (definition.hash(), definition.canonical_yaml()) {
(Ok(hash), Ok(yaml)) => Json(Validated {
name: definition.metadata.name,
hash,
yaml,
})
.into_response(),
_ => storage_error(store::Error::InvalidDefinition).into_response(),
}
}
pub async fn create(
State(state): State<AppState>,
headers: HeaderMap,
yaml: Result<String, axum::extract::rejection::StringRejection>,
) -> Response {
let definition = match parse(&headers, yaml) {
Ok(d) => d,
Err(e) => return e.into_response(),
};
match tokio::task::spawn_blocking(move || state.store.create(&definition)).await {
Ok(Ok(record)) => (StatusCode::CREATED, Json(record)).into_response(),
Ok(Err(e)) => storage_error(e).into_response(),
Err(_) => storage_error(store::Error::Unavailable).into_response(),
}
}
pub async fn list(State(state): State<AppState>) -> Response {
match tokio::task::spawn_blocking(move || state.store.list()).await {
Ok(Ok(records)) => Json(records).into_response(),
Ok(Err(e)) => storage_error(e).into_response(),
Err(_) => storage_error(store::Error::Unavailable).into_response(),
}
}
// The static validation route takes precedence over /definitions/{name}.
pub async fn get_validate(state: State<AppState>) -> Response {
get(state, Path("validate".to_owned())).await
}
pub async fn get(State(state): State<AppState>, Path(name): Path<String>) -> Response {
match tokio::task::spawn_blocking(move || state.store.get(&name)).await {
Ok(Ok(Some(record))) => Json(record).into_response(),
Ok(Ok(None)) => error(
StatusCode::NOT_FOUND,
"definition_not_found",
"Definition not found.",
)
.into_response(),
Ok(Err(e)) => storage_error(e).into_response(),
Err(_) => storage_error(store::Error::Unavailable).into_response(),
}
}
fn storage_error(e: store::Error) -> (StatusCode, Json<ApiError>) {
let (status, code, message) = match e {
store::Error::Conflict => (
StatusCode::CONFLICT,
"definition_exists",
"A definition with this name already exists.",
),
store::Error::InvalidDefinition => (
StatusCode::UNPROCESSABLE_ENTITY,
"invalid_definition",
"Definition is invalid.",
),
store::Error::InvalidOperation => (
StatusCode::BAD_REQUEST,
"invalid_operation",
"Invalid operation.",
),
store::Error::Unavailable => (
StatusCode::SERVICE_UNAVAILABLE,
"storage_unavailable",
"Definition storage is unavailable.",
),
};
error(status, code, message)
}