-
Notifications
You must be signed in to change notification settings - Fork 40
/
context.rs
532 lines (493 loc) · 18.2 KB
/
context.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// 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/.
//! Shared state used by API request handlers
use super::authn;
use super::authz;
use super::config;
use super::db;
use super::Nexus;
use crate::authn::external::session_cookie::{Session, SessionStore};
use crate::authn::Actor;
use crate::authz::AuthorizedResource;
use crate::db::model::ConsoleSession;
use crate::db::DataStore;
use crate::saga_interface::SagaContext;
use async_trait::async_trait;
use authn::external::session_cookie::HttpAuthnSessionCookie;
use authn::external::spoof::HttpAuthnSpoof;
use authn::external::HttpAuthnScheme;
use chrono::{DateTime, Duration, Utc};
use omicron_common::api::external::Error;
use oximeter::types::ProducerRegistry;
use oximeter_instruments::http::{HttpService, LatencyTracker};
use slog::Logger;
use std::collections::BTreeMap;
use std::env;
use std::fmt::Debug;
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Instant;
use std::time::SystemTime;
use uuid::Uuid;
/// Shared state available to all API request handlers
pub struct ServerContext {
/// reference to the underlying nexus
pub nexus: Arc<Nexus>,
/// debug log
pub log: Logger,
/// authenticator for external HTTP requests
pub external_authn: authn::external::Authenticator<Arc<ServerContext>>,
/// authentication context used for internal HTTP requests
pub internal_authn: Arc<authn::Context>,
/// authorizer
pub authz: Arc<authz::Authz>,
/// internal API request latency tracker
pub internal_latencies: LatencyTracker,
/// external API request latency tracker
pub external_latencies: LatencyTracker,
/// registry of metric producers
pub producer_registry: ProducerRegistry,
/// tunable settings needed for the console at runtime
pub console_config: ConsoleConfig,
}
pub struct ConsoleConfig {
/// how long a session can be idle before expiring
pub session_idle_timeout: Duration,
/// how long a session can exist before expiring
pub session_absolute_timeout: Duration,
/// how long browsers can cache static assets
pub cache_control_max_age: Duration,
/// directory containing static file to serve
pub static_dir: Option<PathBuf>,
}
impl ServerContext {
/// Create a new context with the given rack id and log. This creates the
/// underlying nexus as well.
pub fn new(
rack_id: Uuid,
log: Logger,
pool: db::Pool,
config: &config::Config,
) -> Result<Arc<ServerContext>, String> {
let nexus_schemes = config
.authn
.schemes_external
.iter()
.map::<Box<dyn HttpAuthnScheme<Arc<ServerContext>>>, _>(|name| {
match name {
config::SchemeName::Spoof => Box::new(HttpAuthnSpoof),
config::SchemeName::SessionCookie => {
Box::new(HttpAuthnSessionCookie)
}
}
})
.collect();
let external_authn = authn::external::Authenticator::new(nexus_schemes);
let internal_authn = Arc::new(authn::Context::internal_api());
let authz = Arc::new(authz::Authz::new());
let create_tracker = |name: &str| {
let target = HttpService { name: name.to_string(), id: config.id };
const START_LATENCY_DECADE: i8 = -6;
const END_LATENCY_DECADE: i8 = 3;
LatencyTracker::with_latency_decades(
target,
START_LATENCY_DECADE,
END_LATENCY_DECADE,
)
.unwrap()
};
let internal_latencies = create_tracker("nexus-internal");
let external_latencies = create_tracker("nexus-external");
let producer_registry = ProducerRegistry::with_id(config.id);
producer_registry
.register_producer(internal_latencies.clone())
.unwrap();
producer_registry
.register_producer(external_latencies.clone())
.unwrap();
// Support both absolute and relative paths. If configured dir is
// absolute, use it directly. If not, assume it's relative to the
// current working directory.
let static_dir = if config.console.static_dir.is_absolute() {
Some(config.console.static_dir.to_owned())
} else {
env::current_dir()
.map(|root| root.join(config.console.static_dir.to_owned()))
.ok()
};
// We don't want to fail outright yet, but we do want to try to make
// problems slightly easier to debug. The only way it's None is if
// current_dir() fails.
if static_dir.is_none() {
error!(log, "No assets directory configured. All console page and asset requests will 404.");
}
// TODO: check that asset directory exists, check for particular assets
// like console index.html. leaving that out for now so we don't break
// nexus in dev for everyone
Ok(Arc::new(ServerContext {
nexus: Nexus::new_with_id(
rack_id,
log.new(o!("component" => "nexus")),
pool,
config,
Arc::clone(&authz),
),
log,
external_authn,
internal_authn,
authz,
internal_latencies,
external_latencies,
producer_registry,
console_config: ConsoleConfig {
session_idle_timeout: Duration::minutes(
config.console.session_idle_timeout_minutes.into(),
),
session_absolute_timeout: Duration::minutes(
config.console.session_absolute_timeout_minutes.into(),
),
static_dir,
cache_control_max_age: Duration::minutes(
config.console.cache_control_max_age_minutes.into(),
),
},
}))
}
}
/// Provides general facilities scoped to whatever operation Nexus is currently
/// doing
///
/// The idea is that whatever code path you're looking at in Nexus, it should
/// eventually have an OpContext that allows it to:
///
/// - log a message (with relevant operation-specific metadata)
/// - bump a counter (exported via Oximeter)
/// - emit tracing data
/// - do an authorization check
///
/// OpContexts are constructed when Nexus begins doing something. This is often
/// when it starts handling an API request, but it could be when starting a
/// background operation or something else.
// Not all of these fields are used yet, but they may still prove useful for
// debugging.
#[allow(dead_code)]
pub struct OpContext {
pub log: slog::Logger,
pub authn: Arc<authn::Context>,
authz: authz::Context,
created_instant: Instant,
created_walltime: SystemTime,
metadata: BTreeMap<String, String>,
kind: OpKind,
}
enum OpKind {
/// Handling an external API request
ExternalApiRequest,
/// Handling an internal API request
InternalApiRequest,
/// Executing a saga activity
Saga,
/// Background operations in Nexus
Background,
/// Automated testing (unit tests and integration tests)
Test,
}
impl OpContext {
/// Authenticates an incoming request to the external API and produces a new
/// operation context for it
pub async fn for_external_api(
rqctx: &dropshot::RequestContext<Arc<ServerContext>>,
) -> Result<OpContext, dropshot::HttpError> {
let created_instant = Instant::now();
let created_walltime = SystemTime::now();
let apictx = rqctx.context();
let authn = Arc::new(apictx.external_authn.authn_request(rqctx).await?);
let datastore = Arc::clone(apictx.nexus.datastore());
let authz = authz::Context::new(
Arc::clone(&authn),
Arc::clone(&apictx.authz),
datastore,
);
let (log, mut metadata) =
OpContext::log_and_metadata_for_authn(&rqctx.log, &authn);
OpContext::load_request_metadata(rqctx, &mut metadata).await;
Ok(OpContext {
log,
authz,
authn,
created_instant,
created_walltime,
metadata,
kind: OpKind::ExternalApiRequest,
})
}
/// Returns a context suitable for use in handling internal API operations
// TODO-security this should eventually do some kind of authentication
pub async fn for_internal_api(
rqctx: &dropshot::RequestContext<Arc<ServerContext>>,
) -> OpContext {
let created_instant = Instant::now();
let created_walltime = SystemTime::now();
let apictx = rqctx.context();
let authn = Arc::clone(&apictx.internal_authn);
let datastore = Arc::clone(apictx.nexus.datastore());
let authz = authz::Context::new(
Arc::clone(&authn),
Arc::clone(&apictx.authz),
datastore,
);
let (log, mut metadata) =
OpContext::log_and_metadata_for_authn(&rqctx.log, &authn);
OpContext::load_request_metadata(rqctx, &mut metadata).await;
OpContext {
log,
authz,
authn,
created_instant,
created_walltime,
metadata,
kind: OpKind::InternalApiRequest,
}
}
fn log_and_metadata_for_authn(
log: &slog::Logger,
authn: &authn::Context,
) -> (slog::Logger, BTreeMap<String, String>) {
let mut metadata = BTreeMap::new();
let log = if let Some(Actor(actor_id)) = authn.actor() {
metadata
.insert(String::from("authenticated"), String::from("true"));
metadata.insert(String::from("actor"), actor_id.to_string());
log.new(
o!("authenticated" => true, "actor" => actor_id.to_string()),
)
} else {
metadata
.insert(String::from("authenticated"), String::from("false"));
log.new(o!("authenticated" => false))
};
(log, metadata)
}
async fn load_request_metadata<T: Send + Sync + 'static>(
rqctx: &dropshot::RequestContext<T>,
metadata: &mut BTreeMap<String, String>,
) {
let request = rqctx.request.lock().await;
metadata.insert(String::from("request_id"), rqctx.request_id.clone());
metadata
.insert(String::from("http_method"), request.method().to_string());
metadata.insert(String::from("http_uri"), request.uri().to_string());
}
pub fn for_saga_action<T>(
sagactx: &steno::ActionContext<T>,
serialized_authn: &authn::saga::Serialized,
) -> OpContext
where
T: steno::SagaType<ExecContextType = Arc<SagaContext>>,
{
let created_instant = Instant::now();
let created_walltime = SystemTime::now();
let osagactx = sagactx.user_data();
let nexus = osagactx.nexus();
let datastore = Arc::clone(nexus.datastore());
let authn = Arc::new(serialized_authn.to_authn());
let authz = authz::Context::new(
Arc::clone(&authn),
Arc::clone(&osagactx.authz()),
datastore,
);
let (log, mut metadata) =
OpContext::log_and_metadata_for_authn(osagactx.log(), &authn);
// TODO-debugging This would be a good place to put the saga template
// name, but we don't have it available here. This log maybe should
// come from steno, prepopulated with useful metadata similar to the
// way dropshot::RequestContext does.
let log = log.new(o!(
"saga_node" => sagactx.node_label().to_string(),
));
metadata.insert(
String::from("saga_node"),
sagactx.node_label().to_string(),
);
OpContext {
log,
authz,
authn,
created_instant,
created_walltime,
metadata,
kind: OpKind::Saga,
}
}
/// Returns a context suitable for use in background operations in Nexus
pub fn for_background(
log: slog::Logger,
authz: Arc<authz::Authz>,
authn: authn::Context,
datastore: Arc<DataStore>,
) -> OpContext {
let created_instant = Instant::now();
let created_walltime = SystemTime::now();
let authn = Arc::new(authn);
let authz = authz::Context::new(
Arc::clone(&authn),
Arc::clone(&authz),
Arc::clone(&datastore),
);
OpContext {
log,
authz,
authn,
created_instant,
created_walltime,
metadata: BTreeMap::new(),
kind: OpKind::Background,
}
}
/// Returns a context suitable for automated tests where an OpContext is
/// needed outside of a Dropshot context
pub fn for_tests(
log: slog::Logger,
datastore: Arc<DataStore>,
) -> OpContext {
let created_instant = Instant::now();
let created_walltime = SystemTime::now();
let authn = Arc::new(authn::Context::internal_test_user());
let authz = authz::Context::new(
Arc::clone(&authn),
Arc::new(authz::Authz::new()),
Arc::clone(&datastore),
);
OpContext {
log,
authz,
authn,
created_instant,
created_walltime,
metadata: BTreeMap::new(),
kind: OpKind::Test,
}
}
/// Check whether the actor performing this request is authorized for
/// `action` on `resource`.
pub async fn authorize<Resource>(
&self,
action: authz::Action,
resource: &Resource,
) -> Result<(), Error>
where
Resource: AuthorizedResource + Debug + Clone,
{
// TODO-cleanup In an ideal world, Oso would consume &Action and
// &Resource. Instead, it consumes owned types. As a result, they're
// not available to us (even for logging) after we make the authorize()
// call. We work around this by cloning.
trace!(self.log, "authorize begin";
"actor" => ?self.authn.actor(),
"action" => ?action,
"resource" => ?*resource
);
let result = self.authz.authorize(self, action, resource.clone()).await;
debug!(self.log, "authorize result";
"actor" => ?self.authn.actor(),
"action" => ?action,
"resource" => ?*resource,
"result" => ?result,
);
result
}
}
#[cfg(test)]
mod test {
use super::OpContext;
use crate::authn;
use crate::authz;
use authz::Action;
use nexus_test_utils::db::test_setup_database;
use omicron_common::api::external::Error;
use omicron_test_utils::dev;
use std::sync::Arc;
#[tokio::test]
async fn test_background_context() {
let logctx = dev::test_setup_log("test_background_context");
let mut db = test_setup_database(&logctx.log).await;
let (_, datastore) =
crate::db::datastore::datastore_test(&logctx, &db).await;
let opctx = OpContext::for_background(
logctx.log.new(o!()),
Arc::new(authz::Authz::new()),
authn::Context::internal_unauthenticated(),
datastore,
);
// This is partly a test of the authorization policy. Today, background
// contexts should have no privileges. That's misleading because in
// fact they do a bunch of privileged things, but we haven't yet added
// privilege checks to those code paths. Eventually we'll probably want
// to define a particular internal user (maybe even a different one for
// different background contexts) with specific privileges and test
// those here.
//
// For now, we check what we currently expect, which is that this
// context has no official privileges.
let error = opctx
.authorize(Action::Query, &authz::DATABASE)
.await
.expect_err("expected authorization error");
assert!(matches!(error, Error::Unauthenticated { .. }));
db.cleanup().await.unwrap();
logctx.cleanup_successful();
}
#[tokio::test]
async fn test_test_context() {
let logctx = dev::test_setup_log("test_background_context");
let mut db = test_setup_database(&logctx.log).await;
let (_, datastore) =
crate::db::datastore::datastore_test(&logctx, &db).await;
let opctx = OpContext::for_tests(logctx.log.new(o!()), datastore);
// Like in test_background_context(), this is essentially a test of the
// authorization policy. The unit tests assume this user can do
// basically everything. We don't need to verify that -- the tests
// themselves do that -- but it's useful to have a basic santiy test
// that we can construct such a context it's authorized to do something.
opctx
.authorize(Action::Query, &authz::DATABASE)
.await
.expect("expected authorization to succeed");
db.cleanup().await.unwrap();
logctx.cleanup_successful();
}
}
#[async_trait]
impl SessionStore for Arc<ServerContext> {
type SessionModel = ConsoleSession;
async fn session_fetch(&self, token: String) -> Option<Self::SessionModel> {
self.nexus.session_fetch(token).await.ok()
}
async fn session_update_last_used(
&self,
token: String,
) -> Option<Self::SessionModel> {
self.nexus.session_update_last_used(token).await.ok()
}
async fn session_expire(&self, token: String) -> Option<()> {
self.nexus.session_hard_delete(token).await.ok()
}
fn session_idle_timeout(&self) -> Duration {
self.console_config.session_idle_timeout
}
fn session_absolute_timeout(&self) -> Duration {
self.console_config.session_absolute_timeout
}
}
impl Session for ConsoleSession {
fn user_id(&self) -> Uuid {
self.user_id
}
fn time_last_used(&self) -> DateTime<Utc> {
self.time_last_used
}
fn time_created(&self) -> DateTime<Utc> {
self.time_created
}
}