Skip to content

new internal lookup API #798

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 57 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
4610d1a
first cut
davepacheco Mar 20, 2022
ef932a1
rename Lookup
davepacheco Mar 20, 2022
9cc52b0
formatting nit
davepacheco Mar 20, 2022
5845149
formatting
davepacheco Mar 20, 2022
49a10e3
fix
davepacheco Mar 20, 2022
491f30c
some notes, PoC diesel impl
davepacheco Mar 20, 2022
037b78f
get rid of Root
davepacheco Mar 21, 2022
ebaefb1
make LookupPath reachable through the whole path
davepacheco Mar 21, 2022
ce4b362
some cleanup
davepacheco Mar 21, 2022
aae2da4
flesh out macros
davepacheco Mar 21, 2022
73f0f4b
more macro cleanup
davepacheco Mar 21, 2022
68b51cf
more cleanup
davepacheco Mar 21, 2022
6d265bb
more commonizing
davepacheco Mar 21, 2022
e4a5844
cleanup
davepacheco Mar 21, 2022
0e73f25
more commonizing
davepacheco Mar 21, 2022
a414c1f
lookup done enough to start trying it out
davepacheco Mar 21, 2022
c7941b3
prototype a few call sites in Nexus
davepacheco Mar 21, 2022
5690db3
some basic tests are working by accident
davepacheco Mar 21, 2022
295182b
cleanup
davepacheco Mar 21, 2022
bcf57a6
fix up prototyping
davepacheco Mar 21, 2022
43d0102
replace one XXX with a comment
davepacheco Mar 21, 2022
0a494ae
in-progress update to return entire path -- cannot work with macro_ru…
davepacheco Mar 22, 2022
013e981
it works with macro_rules!
davepacheco Mar 22, 2022
785eda2
half-finished effort at proc macro approah
davepacheco Mar 25, 2022
a48722a
more progress
davepacheco Mar 25, 2022
dd5ce40
more work
davepacheco Mar 26, 2022
1f1615c
closer to compiling
davepacheco Mar 26, 2022
4fede01
closer to compiling
davepacheco Mar 26, 2022
30ecdc8
nearly fixed
davepacheco Mar 28, 2022
9f557db
compiles!
davepacheco Mar 28, 2022
064e64d
generate child lookup functions
davepacheco Mar 28, 2022
f2d5770
rip out dead code, fix test
davepacheco Mar 28, 2022
c222c7b
clippy
davepacheco Mar 29, 2022
019d15f
make it a function-like macro
davepacheco Mar 29, 2022
de35510
remove dead code
davepacheco Mar 29, 2022
872ea9a
re-add Nexus changes
davepacheco Mar 29, 2022
44acb43
add some docs
davepacheco Mar 29, 2022
51b0af8
documentation for `LookupPath`
davepacheco Mar 30, 2022
9897ca2
more docs
davepacheco Mar 30, 2022
af6fca3
more docs
davepacheco Mar 30, 2022
977b9c0
more documentation
davepacheco Mar 30, 2022
58b03ed
more cleanup
davepacheco Mar 30, 2022
d68e1c5
macro impl cleanup: part 1 (naming)
davepacheco Mar 30, 2022
cf6591e
more extraction
davepacheco Mar 30, 2022
718b0a0
more cleanup
davepacheco Mar 30, 2022
ce6066d
more cleanup
davepacheco Mar 30, 2022
2d5353d
macro impl: commonize Resource
davepacheco Mar 30, 2022
a71e1a1
macro impl: no need to special-case authz names
davepacheco Mar 30, 2022
e25e9aa
macro impl: minor cleanup
davepacheco Mar 30, 2022
7230871
macro impl: no need to first-class model type
davepacheco Mar 30, 2022
07eaab8
macro impl: authz type need not be first-classed
davepacheco Mar 30, 2022
ea67303
clippy
davepacheco Mar 30, 2022
62e5eea
macro impl: add comment
davepacheco Mar 30, 2022
32eab6e
Merge branch 'main' into new-lookup
davepacheco Mar 30, 2022
fa0f1d6
fix mismerge
davepacheco Mar 30, 2022
a599546
better explain use of input.name
davepacheco Mar 31, 2022
f7ff93a
cargo doc nit
davepacheco Mar 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions nexus/src/authz/api_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ impl Project {
lookup_type,
}
}

pub fn organization(&self) -> &Organization {
&self.parent
}
}

impl Eq for Project {}
Expand Down
4 changes: 2 additions & 2 deletions nexus/src/db/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ impl DataStore {
// the database. Eventually, this function should only be used for doing
// authentication in the first place (since we can't do an authz check in
// that case).
fn pool(&self) -> &bb8::Pool<ConnectionManager<DbConnection>> {
pub(super) fn pool(&self) -> &bb8::Pool<ConnectionManager<DbConnection>> {
self.pool.pool()
}

async fn pool_authorized(
pub(super) async fn pool_authorized(
&self,
opctx: &OpContext,
) -> Result<&bb8::Pool<ConnectionManager<DbConnection>>, Error> {
Expand Down
3 changes: 3 additions & 0 deletions nexus/src/db/db-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ license = "MPL-2.0"
proc-macro = true

[dependencies]
heck = "0.4"
proc-macro2 = { version = "1.0" }
quote = { version = "1.0" }
serde = { version = "1.0", features = [ "derive" ] }
serde_tokenstream = "0.1"
syn = { version = "1.0", features = [ "full", "derive", "extra-traits" ] }
56 changes: 56 additions & 0 deletions nexus/src/db/db-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,62 @@ use quote::{format_ident, quote};
use syn::spanned::Spanned;
use syn::{Data, DataStruct, DeriveInput, Error, Fields, Ident, Lit, Meta};

mod lookup;

/// Defines a structure and helper functions for looking up resources
///
/// # Examples
///
/// ```ignore
/// lookup_resource! {
/// name = "Organization",
/// ancestors = [],
/// children = [ "Project" ],
/// authz_kind = Typed
/// }
/// ```
///
/// See [`lookup::Input`] for documentation on the named arguments.
///
/// This defines a struct `Organization<'a>` with functions `fetch()`,
/// `fetch_for(authz::Action)`, and `lookup_for(authz::Action)` for looking up
/// an Organization in the database. These functions are all protected by
/// access controls.
///
/// Building on that, we have:
///
/// ```ignore
/// lookup_resource! {
/// name = "Organization",
/// ancestors = [],
/// children = [ "Project" ],
/// authz_kind = Typed
/// }
///
/// lookup_resource! {
/// name = "Instance",
/// ancestors = [ "Organization", "Project" ],
/// children = [],
/// authz_kind = Generic
/// }
/// ```
///
/// These define `Project<'a>` and `Instance<'a>`. For more on these structs
/// and how they're used, see nexus/src/db/lookup.rs.
// Allow private intra-doc links. This is useful because the `Input` struct
// cannot be exported (since we're a proc macro crate, and we can't expose
// a struct), but its documentation is very useful.
#[allow(rustdoc::private_intra_doc_links)]
#[proc_macro]
pub fn lookup_resource(
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
match lookup::lookup_resource(input.into()) {
Ok(output) => output.into(),
Err(error) => error.to_compile_error().into(),
}
}

/// Looks for a Meta-style attribute with a particular identifier.
///
/// As an example, for an attribute like `#[foo = "bar"]`, we can find this
Expand Down
Loading