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
use crate::OPA_EXPLAIN;
use authzen_service_util::*;
use futures::future::try_join_all;
use hyper::{body::Bytes, http::header::*, Body, Method};
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
use std::borrow::Cow;
use std::fmt::Debug;
use uuid::Uuid;
const DEFAULT_DATA_PATH: &str = "app";
const DEFAULT_QUERY: &str = "authz";
pub async fn query_all<'a, Data: Clone + Debug + Send + Serialize + Sync + 'a>(
opa_client: &crate::OPAClient,
queries: impl IntoIterator<Item = OPAQuery<'a, Data>>,
) -> Result<(), anyhow::Error> {
let allowed: Vec<OPAQueryResult> = try_join_all(queries.into_iter().map(|query_input| async move {
let result: OPAQueryResult = query_input.query(opa_client).await?;
Ok::<OPAQueryResult, anyhow::Error>(result)
}))
.await?;
if !allowed.into_iter().all(Into::into) {
return Err(anyhow::Error::msg("Unauthorized"));
}
Ok(())
}
#[derive(Clone, Debug, Deserialize, Serialize, TypedBuilder)]
#[builder(field_defaults(setter(into)))]
#[skip_serializing_none]
pub struct OPAQuery<'a, Data: Clone = Value> {
#[serde(borrow)]
pub input: Cow<'a, OPAQueryInput<'a, Data>>,
#[builder(default)]
#[serde(skip_serializing)]
pub config: OPAQueryConfig<'a>,
#[builder(default)]
pub data: Option<Value>,
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, TypedBuilder)]
#[builder(field_defaults(default, setter(into)))]
#[skip_serializing_none]
pub struct OPAQueryConfig<'a> {
#[builder(default = DEFAULT_DATA_PATH)]
#[serde(skip_serializing)]
pub data_path: &'a str,
#[builder(default = DEFAULT_QUERY)]
#[serde(skip_serializing)]
pub query: &'a str,
#[builder(default = OPA_EXPLAIN.as_ref().map(|x| &**x))]
pub explain: Option<&'a str>,
pub pretty: Option<bool>,
pub instrument: Option<bool>,
pub metrics: Option<bool>,
}
impl Default for OPAQueryConfig<'_> {
fn default() -> Self {
Self::builder().build()
}
}
#[derive(Clone, Derivative, Deserialize, Serialize, TypedBuilder)]
#[derivative(Debug)]
#[skip_serializing_none]
pub struct OPAQueryInput<'a, Data = Value> {
#[derivative(Debug = "ignore")]
pub token: Option<&'a str>,
#[builder(setter(into))]
pub action: OPAQueryInputAction<'a, Data>,
#[builder(default)]
pub transaction_id: Option<Uuid>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "snake_case", tag = "type", content = "object")]
pub enum OPAQueryInputAction<'a, Data = Value> {
Create {
service: Cow<'a, str>,
entity: Cow<'a, str>,
records: Vec<Data>,
},
Delete {
service: Cow<'a, str>,
entity: Cow<'a, str>,
ids: Vec<Uuid>,
},
Read {
service: Cow<'a, str>,
entity: Cow<'a, str>,
ids: Vec<Uuid>,
},
Update {
service: Cow<'a, str>,
entity: Cow<'a, str>,
patches: Vec<Data>,
},
}
impl<Data: Clone + Serialize> Endpoint for OPAQuery<'_, Data> {
const METHOD: Method = Method::POST;
type Params<'a> = OPAQueryConfig<'a> where Self: 'a;
fn params(&self) -> Self::Params<'_> {
self.config
}
fn path(&self) -> Path {
format!("/v1/data/{}/{}", self.config.data_path, self.config.query).into()
}
fn headers(&self) -> HeaderMap {
HeaderMap::from_iter(vec![(CONTENT_TYPE, HeaderValue::from_str("application/json").unwrap())])
}
fn body(&self) -> Body {
let body = serde_json::to_string(&self).unwrap();
if *crate::OPA_DEBUG {
info!("OPA Request: {}", self.path());
info!("{body}");
}
Body::from(Bytes::copy_from_slice(body.as_bytes()))
}
}
#[derive(Clone, Copy, Debug, Deref, DerefMut, Eq, From, Into, Ord, PartialEq, PartialOrd, Serialize)]
pub struct OPAQueryResult(pub bool);
impl PartialEq<bool> for OPAQueryResult {
fn eq(&self, rhs: &bool) -> bool {
self.0 == *rhs
}
}
impl PartialEq<OPAQueryResult> for bool {
fn eq(&self, rhs: &OPAQueryResult) -> bool {
*self == rhs.0
}
}
#[derive(Clone, Debug, Deserialize)]
struct _OPAQueryResult {
result: Option<Value>,
}
impl<'de> Deserialize<'de> for OPAQueryResult {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let _simple_query_result = _OPAQueryResult::deserialize(deserializer)?;
Ok(OPAQueryResult(
_simple_query_result
.result
.map(|result| if let Value::Bool(bool) = result { bool } else { false })
.unwrap_or_default(),
))
}
}
impl OPAQueryResult {
pub fn ok_or<E>(self, e: E) -> Result<(), E> {
if self.0 {
Ok(())
} else {
Err(e)
}
}
pub fn ok_or_else<E, F: FnOnce() -> E>(self, f: F) -> Result<(), E> {
if self.0 {
Ok(())
} else {
Err(f())
}
}
}
impl<'a, Data: Clone> OPAQueryInput<'a, Data> {
pub fn map_data<T: Clone, F>(self, f: F) -> OPAQueryInput<'a, T>
where
F: FnMut(Data) -> T,
{
OPAQueryInput {
token: self.token,
action: self.action.map_data(f),
transaction_id: self.transaction_id,
}
}
}
impl<'a, Data> OPAQueryInputAction<'a, Data> {
pub fn map_data<T, F>(self, f: F) -> OPAQueryInputAction<'a, T>
where
F: FnMut(Data) -> T,
{
match self {
Self::Create {
service,
entity,
records,
} => OPAQueryInputAction::Create {
service,
entity,
records: records.into_iter().map(f).collect(),
},
Self::Delete { service, entity, ids } => OPAQueryInputAction::Delete { service, entity, ids },
Self::Read { service, entity, ids } => OPAQueryInputAction::Read { service, entity, ids },
Self::Update {
service,
entity,
patches,
} => OPAQueryInputAction::Update {
service,
entity,
patches: patches.into_iter().map(f).collect(),
},
}
}
}