Skip to content

Commit

Permalink
feat(s3s/access): check op input
Browse files Browse the repository at this point in the history
  • Loading branch information
Nugine committed Oct 10, 2024
1 parent f59a330 commit d72e907
Show file tree
Hide file tree
Showing 6 changed files with 1,180 additions and 115 deletions.
56 changes: 56 additions & 0 deletions codegen/src/access.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::ops::Operations;

use codegen_writer::g;
use codegen_writer::glines;
use heck::ToSnakeCase;

pub fn codegen(ops: &Operations) {
glines![
"//! Auto generated by `codegen/src/access.rs`"
"use super::S3AccessContext;"
""
"use crate::dto::*;"
"use crate::error::S3Result;"
"use crate::request::S3Request;"
""
"#[async_trait::async_trait]"
"pub trait S3Access: Send + Sync + 'static {"
""
];

glines![
"/// Checks whether the current request has accesses to the resources."
"///"
"/// This method is called before deserializing the operation input."
"///"
"/// By default, this method rejects all anonymous requests"
"/// and returns [`AccessDenied`](crate::S3ErrorCode::AccessDenied) error."
"///"
"/// An access control provider can override this method to implement custom logic."
"///"
"/// Common fields in the context:"
"/// + [`cx.credentials()`](S3AccessContext::credentials)"
"/// + [`cx.s3_path()`](S3AccessContext::s3_path)"
"/// + [`cx.s3_op().name()`](crate::S3Operation::name)"
"/// + [`cx.extensions_mut()`](S3AccessContext::extensions_mut)"
"async fn check(&self, cx: &mut S3AccessContext<'_>) -> S3Result<()> {"
" super::default_check(cx)"
"}"
];

for op in ops.values() {
let method_name = op.name.to_snake_case();
let input = &op.input;

g!("/// Checks whether the {} request has accesses to the resources.", op.name);
g!("/// ");
g!("/// This method returns `Ok(())` by default.");
g!("async fn {method_name}(&self, _req: &mut S3Request<{input}>) -> S3Result<()> {{");
g!("Ok(())");
g!("}}");
g!();
}

g!("}}");
g!();
}
7 changes: 7 additions & 0 deletions codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
mod rust;
mod smithy;

mod access;
mod dto;
mod error;
mod headers;
Expand Down Expand Up @@ -79,6 +80,12 @@ fn main() {
codegen_writer::scoped(gen, || ops::codegen(&ops, &rust_types));
}

{
let path = "crates/s3s/src/access/generated.rs";
let gen = Codegen::create_file(path).unwrap();
codegen_writer::scoped(gen, || access::codegen(&ops));
}

{
let path = "crates/s3s-aws/src/conv/generated.rs";
let gen = Codegen::create_file(path).unwrap();
Expand Down
6 changes: 5 additions & 1 deletion codegen/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,13 @@ fn codegen_op_http_call(op: &Operation) {
let method = op.name.to_snake_case();

g!("let input = Self::deserialize_http(req)?;");
g!("let s3_req = super::build_s3_request(input, req);");
g!("let mut s3_req = super::build_s3_request(input, req);");
g!("let s3 = ccx.s3;");

g!("if let Some(access) = ccx.access {{");
g!(" access.{method}(&mut s3_req).await?;");
g!("}}");

if op.name == "GetObject" {
g!("let overrided_headers = super::get_object::extract_overrided_response_headers(&s3_req)?;");
}
Expand Down
Loading

0 comments on commit d72e907

Please sign in to comment.