Repository navigation
Expand file tree
/
Copy pathactions.rs
More file actions
267 lines (251 loc) · 8.66 KB
/
Copy pathactions.rs
File metadata and controls
267 lines (251 loc) · 8.66 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//! Checked single-attempt and durable transactional action dispatch.
pub(super) mod durable;
mod eligibility;
pub(super) mod footprint;
mod receipts;
mod tickets;
pub(in crate::project) use tickets::Submission;
use super::{CheckedProject, Module, input, rpc, storage::PostgresStore, wire};
use crate::error::{Error, Result};
use crate::value::{Type, Value, ValueKind};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub(super) struct Binding {
pub function: String,
#[serde(default = "transactional")]
pub replay: String,
pub resource: Option<String>,
pub parent: Option<String>,
pub label: Option<String>,
}
fn transactional() -> String {
"transactional".into()
}
impl Binding {
pub(super) fn durable(&self) -> bool {
self.replay == "transactional"
}
}
pub(super) fn schema(store: &mut PostgresStore, initialize: bool) -> Result<()> {
tickets::schema(store, initialize)?;
receipts::schema(store, initialize)
}
pub(super) fn checked(project: &CheckedProject) -> Result<()> {
if !project.manifest.actions.is_empty()
&& (project.manifest.storage.is_none() || project.manifest.identity.is_none())
{
return Err(input(
"LP2 actions require relational-v1 storage and accounts-v1 identity",
));
}
let declarations = project
.modules
.values()
.flat_map(|m| m.parsed.actions.iter().cloned())
.collect::<std::collections::BTreeSet<_>>();
for function in project.server.functions.values() {
check_body(&function.body, &declarations)?;
}
let mut bound = std::collections::BTreeSet::new();
for (name, binding) in &project.manifest.actions {
if !["never", "transactional"].contains(&binding.replay.as_str()) {
return Err(input("action replay must be transactional or never"));
}
eligibility::checked(project, name, binding)?;
let op = &project.rpc[name];
if !matches!(op.result, Type::AppResult(..)) {
return Err(input(
"actions require Result<Value, declared application enum>",
));
}
if project.server.functions[&op.function]
.effects
.iter()
.any(|effect| {
!matches!(
effect,
crate::authority::Effect::Read(_) | crate::authority::Effect::Write(_)
)
})
{
return Err(input(
"transactional and single-attempt actions support database effects only; external effects cannot gain replay",
));
}
if !bound.insert(op.function.clone()) {
return Err(input(
"each declared action function requires one unique actions binding; API operations may alias that binding",
));
}
}
for module in project.modules.values() {
for function in &module.parsed.actions {
if !bound.contains(function) {
return Err(input(
"each declared action needs an explicit actions binding",
));
}
}
}
Ok(())
}
fn check_body(
body: &[crate::syntax::Statement],
actions: &std::collections::BTreeSet<String>,
) -> Result<()> {
for statement in body {
match &statement.kind {
crate::syntax::StatementKind::Let(_, value)
| crate::syntax::StatementKind::Expression(value)
| crate::syntax::StatementKind::Return(value)
| crate::syntax::StatementKind::Redirect(value) => check_expression(value, actions)?,
crate::syntax::StatementKind::Transaction(body) => check_body(body, actions)?,
}
}
Ok(())
}
fn check_expression(
expression: &crate::syntax::Expr,
actions: &std::collections::BTreeSet<String>,
) -> Result<()> {
use crate::syntax::ExprKind;
match &expression.kind {
ExprKind::Name(name) if actions.contains(name) => {
return Err(Error::diagnostic(
"D333",
expression.location,
"actions are dispatcher boundaries and cannot be called as ordinary source functions",
));
}
ExprKind::Unary(_, value) | ExprKind::Field(value, _) | ExprKind::Try(value) => {
check_expression(value, actions)?;
}
ExprKind::Binary(a, _, b) => {
check_expression(a, actions)?;
check_expression(b, actions)?;
}
ExprKind::Call {
callee, arguments, ..
} => {
check_expression(callee, actions)?;
for (_, value) in arguments {
check_expression(value, actions)?;
}
}
ExprKind::List(values) => {
for value in values {
check_expression(value, actions)?;
}
}
ExprKind::For(_, values, body) => {
check_expression(values, actions)?;
check_body(body, actions)?;
}
ExprKind::Match(value, arms) => {
check_expression(value, actions)?;
for arm in arms {
check_body(&arm.body, actions)?;
}
}
ExprKind::Html(nodes) => check_html(nodes, actions)?,
_ => {}
}
Ok(())
}
fn check_html(
nodes: &[crate::syntax::HtmlNode],
actions: &std::collections::BTreeSet<String>,
) -> Result<()> {
for node in nodes {
match node {
crate::syntax::HtmlNode::Expression(expression) => {
check_expression(expression, actions)?;
}
crate::syntax::HtmlNode::Element {
attributes,
children,
..
} => {
for (_, attribute) in attributes {
if let crate::syntax::HtmlAttribute::Expression(expression) = attribute {
check_expression(expression, actions)?;
}
}
check_html(children, actions)?;
}
crate::syntax::HtmlNode::Text(_) => {}
}
}
Ok(())
}
pub(super) fn declared(modules: &BTreeMap<String, Module>, function: &str) -> bool {
modules
.values()
.any(|module| module.parsed.actions.contains(function))
}
pub(super) fn finish(
store: &mut PostgresStore,
result: Result<Value>,
) -> Result<serde_json::Value> {
let result = result.and_then(|value| match value.0 {
ValueKind::AppErr(error) => Err(Error::Application(*error)),
_ => Ok(value),
});
// A caught policy/validation failure still poisons the enclosing action.
let result = match store.action_failure() {
Some(error) => Err(error),
None => result,
};
let outcome = match store.finish_request(result) {
Ok(Value(ValueKind::Ok(value))) => crate::action::value("ActionSucceeded", vec![*value]),
Ok(_) => return Err(input("action did not return its checked Result shape")),
Err(Error::Application(error)) => crate::action::value("ActionRejected", vec![error]),
Err(Error::Denied | Error::Unauthenticated) => crate::action::value("ActionDenied", vec![]),
Err(Error::Input(_)) => crate::action::value("ActionInvalid", vec![]),
Err(Error::Aborted | Error::Conflict) => crate::action::value("ActionAborted", vec![]),
Err(_) => crate::action::value("ActionUnknown", vec![]),
};
wire::encode(&outcome)
}
pub(super) fn path<'a>(
path: &'a str,
operations: &BTreeMap<String, rpc::Operation>,
) -> Option<&'a str> {
endpoint(path, operations).map(|endpoint| endpoint.name)
}
#[derive(Clone, Copy, Debug)]
pub(super) struct Endpoint<'a> {
pub name: &'a str,
pub control: durable::Control,
}
pub(super) fn split(path: &str) -> (&str, durable::Control) {
for (suffix, control) in [
("/prepare", durable::Control::Prepare),
("/status", durable::Control::Status),
("/ack", durable::Control::Ack),
] {
if let Some(name) = path.strip_suffix(suffix) {
return (name, control);
}
}
(path, durable::Control::Execute)
}
pub(super) fn endpoint<'a>(
path: &'a str,
operations: &BTreeMap<String, rpc::Operation>,
) -> Option<Endpoint<'a>> {
let mut control = durable::Control::Execute;
let (name, action) = if let Some(name) = path.strip_prefix("/_diamond/actions/") {
let (operation, mode) = split(name);
control = mode;
(operation, true)
} else {
(path.strip_prefix("/_diamond/rpc/")?, false)
};
operations
.get(name)
.filter(|op| op.action == action)
.map(|_| Endpoint { name, control })
}