RustWebアプリケーションの承認メカニズム

アプリケーションのセキュリティを確保するために、認証や承認などのメカニズムを使用しています。多くの方がこれらの概念に精通していると思います。この記事では、承認の概念と関連するアクセス制御モデルに焦点を当てます。





この記事で使用されている用語の定義

      承認と認証の違いを理解することが重要です。





– , ( , ).





– , .





– , .





– , , .





(Crate) – Rust.





承認プロセスには、アクセス制御ポリシーの概念含まれています。これに従って、システムリソース(アクセスオブジェクトに対する特定のユーザー(アクセスサブジェクト)の一連の許可されたアクション決定されます。





また、アクセス制御モデル-さまざまな要因やシステム要件に応じて選択する、ユーザーポリシーを介してアクセスを区切るための一般的なスキーム。





基本的なアクセス制御モデルを見てみましょう。





  • DAC随意アクセス制御)-選択的(随意)アクセス制御





他のユーザーへの権利の譲渡
他のユーザーへの権利の譲渡

- , (ACL).



       , .





, .





  • MAC (Mandatory access-control) –





プライバシーラベル

(, ), .



( ), . .



, MAC , ( , ).





  • RBAC (Role-Based access-control) –





, - . DAC, .





, .



, RBAC PBAC (Permission-Based access-control) , (: READ_DOCUMENT



, WRITE_DOCUMENT



, DELETE_DOCUMENT



) , – .





  • ABAC (Attribute-Based access-control) –





属性の例

, , .  





, , , , .., .





ABAC , ( ) ( ).





OWASP (Open Web Application Security Project) IBM.





, , .





- Rust?

, - (, actix-web, Rocket tide), Middleware, FromRequest Guard (Filter warp).





, . , .





, . , , .





casbin-rs

Casbinproduction-ready , , ( ACL, RBAC, ABAC) .





casbin - PERM (Policy, Effect, Request, Matchers) , , .





# Request definition
[request_definition]
r = sub, obj, act

# Policy definition
[policy_definition]
p = sub, obj, act

# Policy effect
[policy_effect]
e = some(where (p.eft == allow))

# Matchers
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
      
      



, -





( ), PERM .





p, alice, data1, read
p, bob, data2, write
      
      



, .





use casbin::prelude::*;

#[tokio::main]
async fn main() -> () {
    let mut e = Enforcer::new("examples/acl_model.conf", "examples/acl_policy.csv").await?;
    e.enable_log(true);

    let sub = "alice"; // the user that wants to access a resource.
    let obj = "data1"; // the resource that is going to be accessed.
    let act = "read"; // the operation that the user performs on the resource.

    if let Ok(authorized) = e.enforce((sub, obj, act)) {
        if authorized {
            // permit alice to read data1
        } else {
            // deny the request
        }
    } else {
        // error occurs
    }
}
      
      



. , !





, , , , , , , .





, backend Rust. PBAC -, ACL/RBAC.





: actix-web-grants.





actix-web-grants

Middleware



.





, , , : (ACL), (RBAC/PBAC).





       , :





// Sample application with grant protection based on extracting by your custom function
#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let auth = GrantsMiddleware::with_extractor(extract);
        App::new()
            .wrap(auth)
            .service(index)
    })
    .bind("localhost:8081")?
    .run()
    .await
}

async fn extract(_req: &ServiceRequest) -> Result<Vec<String>, Error> {
    // Here is a place for your code to get user permissions/grants/permissions from a request
    // For example from a token or database
    
    // Stub example
    Ok(vec![ROLE_ADMIN.to_string()])
}
      
      



: JWT-, , .



:





use actix_web_grants::proc_macro::{has_roles};

#[get("/secure")]
#[has_roles("ROLE_ADMIN")]
async fn macro_secured() -> HttpResponse {
    HttpResponse::Ok().body("ADMIN_RESPONSE")
}
      
      



actix-web-grants, .





, ( wrk) .





RBAC - : , . . GitHub: actix-web-authz-benchmark ( ).





:







Benchmark





casbin-rs





actix-web-grants





Latency





Req/Sec





Latency





Req/Sec





Allowed Endpoint





6.18 ms





16.27k





4.41 ms





22.69k





Denied Endpoint





6.70 ms





14.98k





4.94 ms





20.23k





rustc: v1.52.0 (stable); CPU: 2,6 GHz 6-Core Intel Core i7; RAM: 16 GB





, , actix-web-grants (endpoints), casbin-rs.





Post Scriptum

このライブラリはまだ多くのWebフレームワークと統合されていませんが、いくつかの抽象化を導入し、他のフレームワーク用のモジュールを作成し、いくつかの改善を行う予定です(たとえば、役割を継承し、カスタムタイプをサポートする機能)。どんな提案や貢献も歓迎します!








All Articles