Skip to content

Commit 450f906

Browse files
authored
[nexus] Split authn/authz and db-fixed-data into new crates (#5849)
As a part of the ongoing effort to split Nexus into smaller pieces, this PR splits out two new crates: - `nexus-auth` takes the contents of `nexus/db-queries/src/auth{n,z}`, as well as `nexus/db-queries/src/context.rs`, and separates this logic into a new bespoke crate. Although this crate **does** have a dependency on the datastore itself, it only actually invokes a single method, and can be abstracted via a new trait, defined in `nexus/auth/storage`. - `nexus-db-fixed-data` takes the contents of `nexus/db-queries/src/db/fixed-data` and separates this logic into a new crate.
1 parent 8df03b3 commit 450f906

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+800
-605
lines changed

Cargo.lock

Lines changed: 57 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ members = [
3939
"nexus",
4040
"nexus-config",
4141
"nexus/authz-macros",
42+
"nexus/auth",
43+
"nexus/db-fixed-data",
4244
"nexus/db-macros",
4345
"nexus/db-model",
4446
"nexus/db-queries",
@@ -123,9 +125,11 @@ default-members = [
123125
"nexus",
124126
"nexus-config",
125127
"nexus/authz-macros",
128+
"nexus/auth",
126129
"nexus/macros-common",
127130
"nexus/metrics-producer-gc",
128131
"nexus/networking",
132+
"nexus/db-fixed-data",
129133
"nexus/db-macros",
130134
"nexus/db-model",
131135
"nexus/db-queries",
@@ -317,8 +321,10 @@ newtype_derive = "0.1.6"
317321
mg-admin-client = { git = "https://github.com/oxidecomputer/maghemite", rev = "5630887d0373857f77cb264f84aa19bdec720ce3" }
318322
ddm-admin-client = { git = "https://github.com/oxidecomputer/maghemite", rev = "5630887d0373857f77cb264f84aa19bdec720ce3" }
319323
multimap = "0.10.0"
324+
nexus-auth = { path = "nexus/auth" }
320325
nexus-client = { path = "clients/nexus-client" }
321326
nexus-config = { path = "nexus-config" }
327+
nexus-db-fixed-data = { path = "nexus/db-fixed-data" }
322328
nexus-db-model = { path = "nexus/db-model" }
323329
nexus-db-queries = { path = "nexus/db-queries" }
324330
nexus-defaults = { path = "nexus/defaults" }

nexus/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ tough.workspace = true
8686
trust-dns-resolver.workspace = true
8787
uuid.workspace = true
8888

89+
nexus-auth.workspace = true
8990
nexus-defaults.workspace = true
9091
nexus-db-model.workspace = true
9192
nexus-db-queries.workspace = true

nexus/auth/Cargo.toml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
[package]
2+
name = "nexus-auth"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license = "MPL-2.0"
6+
7+
[lints]
8+
workspace = true
9+
10+
[build-dependencies]
11+
omicron-rpaths.workspace = true
12+
13+
[dependencies]
14+
anyhow.workspace = true
15+
async-trait.workspace = true
16+
base64.workspace = true
17+
chrono.workspace = true
18+
cookie.workspace = true
19+
dropshot.workspace = true
20+
futures.workspace = true
21+
headers.workspace = true
22+
http.workspace = true
23+
hyper.workspace = true
24+
newtype_derive.workspace = true
25+
# See omicron-rpaths for more about the "pq-sys" dependency.
26+
pq-sys = "*"
27+
once_cell.workspace = true
28+
openssl.workspace = true
29+
oso.workspace = true
30+
samael.workspace = true
31+
serde.workspace = true
32+
serde_urlencoded.workspace = true
33+
slog.workspace = true
34+
strum.workspace = true
35+
thiserror.workspace = true
36+
tokio = { workspace = true, features = ["full"] }
37+
uuid.workspace = true
38+
39+
authz-macros.workspace = true
40+
nexus-db-fixed-data.workspace = true
41+
nexus-db-model.workspace = true
42+
nexus-types.workspace = true
43+
omicron-common.workspace = true
44+
omicron-uuid-kinds.workspace = true
45+
omicron-workspace-hack.workspace = true
46+
47+
[dev-dependencies]
48+
omicron-test-utils.workspace = true

nexus/auth/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
// See omicron-rpaths for documentation.
6+
// NOTE: This file MUST be kept in sync with the other build.rs files in this
7+
// repository.
8+
fn main() {
9+
omicron_rpaths::configure_default_omicron_rpaths();
10+
}

nexus/db-queries/src/authn/external/cookies.rs renamed to nexus/auth/src/authn/external/cookies.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use dropshot::{
99
ApiEndpointBodyContentType, ExtensionMode, ExtractorMetadata, HttpError,
1010
RequestContext, ServerContext, SharedExtractor,
1111
};
12+
use newtype_derive::NewtypeDeref;
13+
use newtype_derive::NewtypeFrom;
1214

1315
pub fn parse_cookies(
1416
headers: &http::HeaderMap<http::HeaderValue>,

nexus/db-queries/src/authn/external/mod.rs renamed to nexus/auth/src/authn/external/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::SiloAuthnPolicy;
99
use crate::authn;
1010
use async_trait::async_trait;
1111
use authn::Reason;
12+
use slog::trace;
1213
use std::borrow::Borrow;
1314
use uuid::Uuid;
1415

nexus/db-queries/src/authn/external/session_cookie.rs renamed to nexus/auth/src/authn/external/session_cookie.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use async_trait::async_trait;
1313
use chrono::{DateTime, Duration, Utc};
1414
use dropshot::HttpError;
1515
use http::HeaderValue;
16+
use slog::debug;
1617
use uuid::Uuid;
1718

1819
// many parts of the implementation will reference this OWASP guide

nexus/db-queries/src/authn/external/spoof.rs renamed to nexus/auth/src/authn/external/spoof.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use async_trait::async_trait;
1717
use headers::authorization::{Authorization, Bearer};
1818
use headers::HeaderMapExt;
1919
use once_cell::sync::Lazy;
20+
use slog::debug;
2021
use uuid::Uuid;
2122

2223
// This scheme is intended for demos, development, and testing until we have a

nexus/db-queries/src/authn/mod.rs renamed to nexus/auth/src/authn/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,21 @@ pub mod external;
2828
pub mod saga;
2929
pub mod silos;
3030

31-
pub use crate::db::fixed_data::silo_user::USER_TEST_PRIVILEGED;
32-
pub use crate::db::fixed_data::silo_user::USER_TEST_UNPRIVILEGED;
33-
pub use crate::db::fixed_data::user_builtin::USER_DB_INIT;
34-
pub use crate::db::fixed_data::user_builtin::USER_EXTERNAL_AUTHN;
35-
pub use crate::db::fixed_data::user_builtin::USER_INTERNAL_API;
36-
pub use crate::db::fixed_data::user_builtin::USER_INTERNAL_READ;
37-
pub use crate::db::fixed_data::user_builtin::USER_SAGA_RECOVERY;
38-
pub use crate::db::fixed_data::user_builtin::USER_SERVICE_BALANCER;
39-
use crate::db::model::ConsoleSession;
31+
pub use nexus_db_fixed_data::silo_user::USER_TEST_PRIVILEGED;
32+
pub use nexus_db_fixed_data::silo_user::USER_TEST_UNPRIVILEGED;
33+
pub use nexus_db_fixed_data::user_builtin::USER_DB_INIT;
34+
pub use nexus_db_fixed_data::user_builtin::USER_EXTERNAL_AUTHN;
35+
pub use nexus_db_fixed_data::user_builtin::USER_INTERNAL_API;
36+
pub use nexus_db_fixed_data::user_builtin::USER_INTERNAL_READ;
37+
pub use nexus_db_fixed_data::user_builtin::USER_SAGA_RECOVERY;
38+
pub use nexus_db_fixed_data::user_builtin::USER_SERVICE_BALANCER;
4039

4140
use crate::authz;
42-
use crate::db;
43-
use crate::db::fixed_data::silo::DEFAULT_SILO;
44-
use crate::db::identity::Asset;
41+
use newtype_derive::NewtypeDisplay;
42+
use nexus_db_fixed_data::silo::DEFAULT_SILO;
4543
use nexus_types::external_api::shared::FleetRole;
4644
use nexus_types::external_api::shared::SiloRole;
45+
use nexus_types::identity::Asset;
4746
use omicron_common::api::external::LookupType;
4847
use serde::Deserialize;
4948
use serde::Serialize;
@@ -254,7 +253,6 @@ pub struct SiloAuthnPolicy {
254253
}
255254

256255
impl SiloAuthnPolicy {
257-
#[cfg(test)]
258256
pub fn new(
259257
mapped_fleet_roles: BTreeMap<SiloRole, BTreeSet<FleetRole>>,
260258
) -> SiloAuthnPolicy {
@@ -290,8 +288,8 @@ mod test {
290288
use super::USER_SERVICE_BALANCER;
291289
use super::USER_TEST_PRIVILEGED;
292290
use super::USER_TEST_UNPRIVILEGED;
293-
use crate::db::fixed_data::user_builtin::USER_EXTERNAL_AUTHN;
294-
use crate::db::identity::Asset;
291+
use nexus_db_fixed_data::user_builtin::USER_EXTERNAL_AUTHN;
292+
use nexus_types::identity::Asset;
295293

296294
#[test]
297295
fn test_internal_users() {
@@ -386,11 +384,13 @@ impl Actor {
386384
}
387385
}
388386

389-
impl From<&Actor> for db::model::IdentityType {
390-
fn from(actor: &Actor) -> db::model::IdentityType {
387+
impl From<&Actor> for nexus_db_model::IdentityType {
388+
fn from(actor: &Actor) -> nexus_db_model::IdentityType {
391389
match actor {
392-
Actor::UserBuiltin { .. } => db::model::IdentityType::UserBuiltin,
393-
Actor::SiloUser { .. } => db::model::IdentityType::SiloUser,
390+
Actor::UserBuiltin { .. } => {
391+
nexus_db_model::IdentityType::UserBuiltin
392+
}
393+
Actor::SiloUser { .. } => nexus_db_model::IdentityType::SiloUser,
394394
}
395395
}
396396
}
@@ -421,7 +421,7 @@ impl std::fmt::Debug for Actor {
421421
/// A console session with the silo id of the authenticated user
422422
#[derive(Clone, Debug)]
423423
pub struct ConsoleSessionWithSiloId {
424-
pub console_session: ConsoleSession,
424+
pub console_session: nexus_db_model::ConsoleSession,
425425
pub silo_id: Uuid,
426426
}
427427

File renamed without changes.

0 commit comments

Comments
 (0)