-
Notifications
You must be signed in to change notification settings - Fork 40
/
role.rs
346 lines (312 loc) · 13.7 KB
/
role.rs
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! [`DataStore`] methods related to roles.
use super::DataStore;
use crate::authz;
use crate::authz::AuthorizedResource;
use crate::context::OpContext;
use crate::db;
use crate::db::datastore::RunnableQuery;
use crate::db::datastore::RunnableQueryNoReturn;
use crate::db::error::public_error_from_diesel;
use crate::db::error::ErrorHandler;
use crate::db::error::TransactionError;
use crate::db::fixed_data::role_assignment::BUILTIN_ROLE_ASSIGNMENTS;
use crate::db::fixed_data::role_builtin::BUILTIN_ROLES;
use crate::db::model::DatabaseString;
use crate::db::model::IdentityType;
use crate::db::model::RoleAssignment;
use crate::db::model::RoleBuiltin;
use crate::db::pagination::paginated_multicolumn;
use crate::db::pool::DbConnection;
use async_bb8_diesel::AsyncConnection;
use async_bb8_diesel::AsyncRunQueryDsl;
use diesel::prelude::*;
use nexus_types::external_api::shared;
use omicron_common::api::external::DataPageParams;
use omicron_common::api::external::Error;
use omicron_common::api::external::ListResultVec;
use omicron_common::api::external::ResourceType;
use omicron_common::bail_unless;
use uuid::Uuid;
impl DataStore {
/// List built-in roles
pub async fn roles_builtin_list_by_name(
&self,
opctx: &OpContext,
pagparams: &DataPageParams<'_, (String, String)>,
) -> ListResultVec<RoleBuiltin> {
use db::schema::role_builtin::dsl;
opctx.authorize(authz::Action::ListChildren, &authz::FLEET).await?;
let conn = self.pool_connection_authorized(opctx).await?;
paginated_multicolumn(
dsl::role_builtin,
(dsl::resource_type, dsl::role_name),
pagparams,
)
.select(RoleBuiltin::as_select())
.load_async::<RoleBuiltin>(&*conn)
.await
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))
}
/// Load built-in roles into the database
pub async fn load_builtin_roles(
&self,
opctx: &OpContext,
) -> Result<(), Error> {
use db::schema::role_builtin::dsl;
opctx.authorize(authz::Action::Modify, &authz::DATABASE).await?;
let builtin_roles = BUILTIN_ROLES
.iter()
.map(|role_config| {
RoleBuiltin::new(
role_config.resource_type,
&role_config.role_name,
&role_config.description,
)
})
.collect::<Vec<RoleBuiltin>>();
debug!(opctx.log, "attempting to create built-in roles");
let conn = self.pool_connection_authorized(opctx).await?;
let count = diesel::insert_into(dsl::role_builtin)
.values(builtin_roles)
.on_conflict((dsl::resource_type, dsl::role_name))
.do_nothing()
.execute_async(&*conn)
.await
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?;
info!(opctx.log, "created {} built-in roles", count);
Ok(())
}
/// Load role assignments for built-in users and built-in roles into the
/// database
pub async fn load_builtin_role_asgns(
&self,
opctx: &OpContext,
) -> Result<(), Error> {
use db::schema::role_assignment::dsl;
opctx.authorize(authz::Action::Modify, &authz::DATABASE).await?;
debug!(opctx.log, "attempting to create built-in role assignments");
let conn = self.pool_connection_authorized(opctx).await?;
let count = diesel::insert_into(dsl::role_assignment)
.values(&*BUILTIN_ROLE_ASSIGNMENTS)
.on_conflict((
dsl::identity_type,
dsl::identity_id,
dsl::resource_type,
dsl::resource_id,
dsl::role_name,
))
.do_nothing()
.execute_async(&*conn)
.await
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))?;
info!(opctx.log, "created {} built-in role assignments", count);
Ok(())
}
/// Return the built-in roles that the given built-in user has for the given
/// resource
pub async fn role_asgn_list_for(
&self,
opctx: &OpContext,
identity_type: IdentityType,
identity_id: Uuid,
resource_type: ResourceType,
resource_id: Uuid,
) -> Result<Vec<RoleAssignment>, Error> {
use db::schema::role_assignment::dsl;
// There is no resource-specific authorization check because all
// authenticated users need to be able to list their own roles --
// otherwise we can't do any authorization checks.
// TODO-security rethink this -- how do we know the user is looking up
// their own roles? Maybe this should use an internal authz context.
// TODO-scalability TODO-security This needs to be paginated. It's not
// exposed via an external API right now but someone could still put us
// into some hurt by assigning loads of roles to someone and having that
// person attempt to access anything.
self.pool_connection_authorized(opctx).await?
.transaction_async(|conn| async move {
let mut role_assignments = dsl::role_assignment
.filter(dsl::identity_type.eq(identity_type.clone()))
.filter(dsl::identity_id.eq(identity_id))
.filter(dsl::resource_type.eq(resource_type.to_string()))
.filter(dsl::resource_id.eq(resource_id))
.select(RoleAssignment::as_select())
.load_async::<RoleAssignment>(&conn)
.await?;
// Return the roles that a silo user has from their group memberships
if identity_type == IdentityType::SiloUser {
use db::schema::silo_group_membership;
let mut group_role_assignments = dsl::role_assignment
.filter(dsl::identity_type.eq(IdentityType::SiloGroup))
.filter(dsl::identity_id.eq_any(
silo_group_membership::dsl::silo_group_membership
.filter(silo_group_membership::dsl::silo_user_id.eq(identity_id))
.select(silo_group_membership::dsl::silo_group_id)
))
.filter(dsl::resource_type.eq(resource_type.to_string()))
.filter(dsl::resource_id.eq(resource_id))
.select(RoleAssignment::as_select())
.load_async::<RoleAssignment>(&conn)
.await?;
role_assignments.append(&mut group_role_assignments);
}
Ok(role_assignments)
})
.await
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))
}
/// Fetches all of the externally-visible role assignments for the specified
/// resource
///
/// Role assignments for internal identities (e.g., built-in users) are not
/// included in this list.
///
/// This function is generic over all resources that can accept roles (e.g.,
/// Fleet, Silo, etc.).
// TODO-scalability In an ideal world, this would be paginated. The impact
// is mitigated because we cap the number of role assignments per resource
// pretty tightly.
pub async fn role_assignment_fetch_visible<
T: authz::ApiResourceWithRoles + AuthorizedResource + Clone,
>(
&self,
opctx: &OpContext,
authz_resource: &T,
) -> ListResultVec<db::model::RoleAssignment> {
let conn = self.pool_connection_authorized(opctx).await?;
self.role_assignment_fetch_visible_conn(opctx, authz_resource, &conn)
.await
}
pub async fn role_assignment_fetch_visible_conn<
T: authz::ApiResourceWithRoles + AuthorizedResource + Clone,
>(
&self,
opctx: &OpContext,
authz_resource: &T,
conn: &async_bb8_diesel::Connection<DbConnection>,
) -> ListResultVec<db::model::RoleAssignment> {
opctx.authorize(authz::Action::ReadPolicy, authz_resource).await?;
let resource_type = authz_resource.resource_type();
let resource_id = authz_resource.resource_id();
use db::schema::role_assignment::dsl;
dsl::role_assignment
.filter(dsl::resource_type.eq(resource_type.to_string()))
.filter(dsl::resource_id.eq(resource_id))
.filter(dsl::identity_type.ne(IdentityType::UserBuiltin))
.order(dsl::role_name.asc())
.then_order_by(dsl::identity_id.asc())
.select(RoleAssignment::as_select())
.load_async::<RoleAssignment>(conn)
.await
.map_err(|e| public_error_from_diesel(e, ErrorHandler::Server))
}
/// Removes all existing externally-visble role assignments on
/// `authz_resource` and adds those specified by `new_assignments`
///
/// Role assignments for internal identities (e.g., built-in users) are not
/// affected.
///
/// The expectation is that the caller will have just fetched the role
/// assignments, modified them, and is giving us the complete new list.
///
/// This function is generic over all resources that can accept roles (e.g.,
/// Fleet, Silo, etc.).
// TODO-correctness As with the rest of the API, we're lacking an ability
// for an ETag precondition check here.
// TODO-scalability In an ideal world, this would update in batches. That's
// tricky without first-classing the Policy in the database. The impact is
// mitigated because we cap the number of role assignments per resource
// pretty tightly.
pub async fn role_assignment_replace_visible<T>(
&self,
opctx: &OpContext,
authz_resource: &T,
new_assignments: &[shared::RoleAssignment<T::AllowedRoles>],
) -> ListResultVec<db::model::RoleAssignment>
where
T: authz::ApiResourceWithRolesType + AuthorizedResource + Clone,
{
opctx.authorize(authz::Action::ModifyPolicy, authz_resource).await?;
let authz_resource = authz_resource.clone();
let new_assignments = new_assignments.to_vec().clone();
let queries = DataStore::role_assignment_replace_visible_queries(
opctx,
&authz_resource,
&new_assignments,
)
.await?;
let (delete_old_query, insert_new_query) = queries;
// TODO-scalability: Ideally this would be a batched transaction so we
// don't need to hold a transaction open across multiple roundtrips from
// the database, but for now we're using a transaction due to the
// severely decreased legibility of CTEs via diesel right now.
// We might instead want to first-class the idea of Policies in the
// database so that we can build up a whole new Policy in batches and
// then flip the resource over to using it.
self.pool_connection_authorized(opctx)
.await?
.transaction_async(|conn| async move {
delete_old_query.execute_async(&conn).await?;
Ok(insert_new_query.get_results_async(&conn).await?)
})
.await
.map_err(|e| match e {
TransactionError::CustomError(e) => e,
TransactionError::Database(e) => {
public_error_from_diesel(e, ErrorHandler::Server)
}
})
}
pub async fn role_assignment_replace_visible_queries<T>(
opctx: &OpContext,
authz_resource: &T,
new_assignments: &[shared::RoleAssignment<T::AllowedRoles>],
) -> Result<
(
impl RunnableQueryNoReturn,
impl RunnableQuery<db::model::RoleAssignment>,
),
Error,
>
where
T: authz::ApiResourceWithRolesType + AuthorizedResource + Clone,
{
opctx.authorize(authz::Action::ModifyPolicy, authz_resource).await?;
bail_unless!(
new_assignments.len() <= shared::MAX_ROLE_ASSIGNMENTS_PER_RESOURCE
);
let resource_type = authz_resource.resource_type();
let resource_id = authz_resource.resource_id();
// Sort the records in the same order that we would return them when
// listing them. This is because we're going to use RETURNING to return
// the inserted rows from the database and we want them to come back in
// the same order that we would normally list them.
let mut new_assignments = new_assignments
.iter()
.map(|r| {
db::model::RoleAssignment::new(
db::model::IdentityType::from(r.identity_type),
r.identity_id,
resource_type,
resource_id,
&r.role_name.to_database_string(),
)
})
.collect::<Vec<_>>();
new_assignments.sort_by(|r1, r2| {
(&r1.role_name, r1.identity_id)
.cmp(&(&r2.role_name, r2.identity_id))
});
use db::schema::role_assignment::dsl;
let delete_old_query = diesel::delete(dsl::role_assignment)
.filter(dsl::resource_id.eq(resource_id))
.filter(dsl::resource_type.eq(resource_type.to_string()))
.filter(dsl::identity_type.ne(IdentityType::UserBuiltin));
let insert_new_query = diesel::insert_into(dsl::role_assignment)
.values(new_assignments)
.returning(RoleAssignment::as_returning());
Ok((delete_old_query, insert_new_query))
}
}