-
Notifications
You must be signed in to change notification settings - Fork 40
/
oso_generic.rs
322 lines (295 loc) · 9.99 KB
/
oso_generic.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
// 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/.
//! Oso integration
use super::actor::AnyActor;
use super::actor::AuthenticatedActor;
use super::api_resources::*;
use super::context::AuthorizedResource;
use super::roles::RoleSet;
use super::Authz;
use crate::authn;
use crate::context::OpContext;
use crate::db::DataStore;
use anyhow::ensure;
use anyhow::Context;
use futures::future::BoxFuture;
use futures::FutureExt;
use omicron_common::api::external::Error;
use oso::Oso;
use oso::PolarClass;
use std::collections::BTreeSet;
use std::fmt;
/// Base Polar configuration describing control plane authorization rules
const OMICRON_AUTHZ_CONFIG_BASE: &str = include_str!("omicron.polar");
/// Used to configure Polar resources
pub(super) struct Init {
/// snippet of Polar describing a resource
pub polar_snippet: &'static str,
/// description of Rust type implementing the Polar resource
pub polar_class: oso::Class,
}
/// Represents an initialized Oso environment
pub struct OsoInit {
pub oso: Oso,
pub class_names: BTreeSet<String>,
}
/// Manages initialization of Oso environment, including Polar snippets and
/// Polar classes
pub struct OsoInitBuilder {
log: slog::Logger,
oso: Oso,
class_names: BTreeSet<String>,
snippets: Vec<&'static str>,
}
impl OsoInitBuilder {
pub fn new(log: slog::Logger) -> OsoInitBuilder {
OsoInitBuilder {
log,
oso: Oso::new(),
class_names: BTreeSet::new(),
snippets: vec![OMICRON_AUTHZ_CONFIG_BASE],
}
}
/// Registers a class that either has no associated Polar snippet or whose
/// snippet is part of the base file
pub fn register_class(
mut self,
c: oso::Class,
) -> Result<Self, anyhow::Error> {
info!(self.log, "registering Oso class"; "class" => &c.name);
let name = c.name.clone();
let new_element = self.class_names.insert(name.clone());
ensure!(new_element, "Oso class was already registered: {:?}", &name);
self.oso
.register_class(c)
.with_context(|| format!("registering Oso class {:?}", name))?;
Ok(self)
}
/// Registers a class with associated Polar snippet
pub(super) fn register_class_with_snippet(
mut self,
init: Init,
) -> Result<Self, anyhow::Error> {
self.snippets.push(init.polar_snippet);
self.register_class(init.polar_class)
}
pub fn build(mut self) -> Result<OsoInit, anyhow::Error> {
let polar_config = self.snippets.join("\n");
info!(self.log, "full Oso configuration"; "config" => &polar_config);
self.oso
.load_str(&polar_config)
.context("loading Polar (Oso) config")?;
Ok(OsoInit { oso: self.oso, class_names: self.class_names })
}
}
/// Returns an Oso handle suitable for authorizing using Omicron's authorization
/// rules
pub fn make_omicron_oso(log: &slog::Logger) -> Result<OsoInit, anyhow::Error> {
let mut oso_builder = OsoInitBuilder::new(log.clone());
let classes = [
// Hand-written classes
Action::get_polar_class(),
AnyActor::get_polar_class(),
AuthenticatedActor::get_polar_class(),
Database::get_polar_class(),
Fleet::get_polar_class(),
IpPoolList::get_polar_class(),
GlobalImageList::get_polar_class(),
ConsoleSessionList::get_polar_class(),
DeviceAuthRequestList::get_polar_class(),
SiloIdentityProviderList::get_polar_class(),
SiloUserList::get_polar_class(),
];
for c in classes {
oso_builder = oso_builder.register_class(c)?;
}
// Generated by the `authz_resource!` macro
let generated_inits = [
Organization::init(),
Project::init(),
Disk::init(),
Snapshot::init(),
Instance::init(),
IpPool::init(),
NetworkInterface::init(),
Vpc::init(),
VpcRouter::init(),
RouterRoute::init(),
VpcSubnet::init(),
// Fleet-level resources
ConsoleSession::init(),
DeviceAuthRequest::init(),
DeviceAccessToken::init(),
Rack::init(),
RoleBuiltin::init(),
SshKey::init(),
Silo::init(),
SiloUser::init(),
SiloGroup::init(),
IdentityProvider::init(),
SamlIdentityProvider::init(),
Sled::init(),
UpdateAvailableArtifact::init(),
UserBuiltin::init(),
GlobalImage::init(),
];
for init in generated_inits {
oso_builder = oso_builder.register_class_with_snippet(init)?;
}
oso_builder.build()
}
/// Describes an action being authorized
///
/// There's currently just one enum of Actions for all of Omicron. We expect
/// most objects to support mostly the same set of actions.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(test, derive(strum::EnumIter))]
pub enum Action {
Query, // only used for [`Database`]
Read,
ListChildren,
ReadPolicy,
Modify,
ModifyPolicy,
CreateChild,
Delete,
}
impl oso::PolarClass for Action {
fn get_polar_class_builder() -> oso::ClassBuilder<Self> {
oso::Class::builder()
.with_equality_check()
.add_method("to_perm", |a: &Action| Perm::from(a).to_string())
}
}
/// A permission used in the Polar configuration
///
/// An authorization request starts by asking whether an actor can take some
/// _action_ on a resource. Most of the policy is written in terms of
/// traditional RBAC-style _permissions_. This type is used to help translate
/// from [`Action`] to permission.
///
/// Note that Polar appears to require that all permissions be strings. So in
/// practice, the [`Action`] is converted to a [`Perm`] only for long enough to
/// convert that to a string. Still, having a separate type here ensures that
/// not _any_ old string can be used as a permission.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Perm {
Query, // Only for [`Database`]
Read,
Modify,
ListChildren,
CreateChild,
}
impl From<&Action> for Perm {
fn from(a: &Action) -> Self {
match a {
Action::Query => Perm::Query,
Action::Read => Perm::Read,
Action::ReadPolicy => Perm::Read,
Action::Modify => Perm::Modify,
Action::ModifyPolicy => Perm::Modify,
Action::Delete => Perm::Modify,
Action::ListChildren => Perm::ListChildren,
Action::CreateChild => Perm::CreateChild,
}
}
}
impl fmt::Display for Perm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// This implementation MUST be kept in sync with the Polar configuration
// for Omicron, which uses literal strings for permissions.
f.write_str(match self {
Perm::Query => "query",
Perm::Read => "read",
Perm::Modify => "modify",
Perm::ListChildren => "list_children",
Perm::CreateChild => "create_child",
})
}
}
// Non-API resources that we want to protect with authorization
/// Represents the database itself to Polar
///
/// This exists so that we can have roles with no access to the database at all.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Database;
/// Singleton representing the [`Database`] itself for authz purposes
pub const DATABASE: Database = Database;
impl oso::PolarClass for Database {
fn get_polar_class_builder() -> oso::ClassBuilder<Self> {
oso::Class::builder().add_method(
"has_role",
|_d: &Database, _actor: AuthenticatedActor, _role: String| {
// There is an explicit rule in the Oso policy granting the
// appropriate roles on "Database" to the appropriate actors.
// We don't need to grant anything extra here.
false
},
)
}
}
impl AuthorizedResource for Database {
fn load_roles<'a, 'b, 'c, 'd, 'e, 'f>(
&'a self,
_: &'b OpContext,
_: &'c DataStore,
_: &'d authn::Context,
_: &'e mut RoleSet,
) -> BoxFuture<'f, Result<(), Error>>
where
'a: 'f,
'b: 'f,
'c: 'f,
'd: 'f,
'e: 'f,
{
// We don't use (database) roles to grant access to the database. The
// role assignment is hardcoded for all authenticated users. See the
// "has_role" Polar method above.
//
// Instead of this, we could modify this function to insert into
// `RoleSet` the "database user" role. However, this doesn't fit into
// the type signature of roles supported by RoleSet. RoleSet is really
// for roles on database objects -- it assumes they have a ResourceType
// and id, neither of which is true for `Database`.
futures::future::ready(Ok(())).boxed()
}
fn on_unauthorized(
&self,
_: &Authz,
error: Error,
_: AnyActor,
_: Action,
) -> Error {
error
}
fn polar_class(&self) -> oso::Class {
Self::get_polar_class()
}
}
#[cfg(test)]
mod test {
use super::OsoInitBuilder;
use crate::authz::Action;
use omicron_test_utils::dev;
use oso::PolarClass;
#[test]
fn test_duplicate_polar_classes() {
let logctx = dev::test_setup_log("test_duplicate_polar_classes");
let oso_builder = OsoInitBuilder::new(logctx.log.clone());
let oso_builder =
oso_builder.register_class(Action::get_polar_class()).unwrap();
match oso_builder.register_class(Action::get_polar_class()) {
Ok(_) => panic!("failed to detect duplicate class"),
Err(error) => {
println!("{:#}", error);
assert_eq!(
error.to_string(),
"Oso class was already registered: \"Action\""
);
}
};
logctx.cleanup_successful();
}
}