Skip to main content

reqsign_azure_storage/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Azure Storage signing implementation for reqsign.
19//!
20//! This crate provides comprehensive signing support for Azure Storage services
21//! including Blob Storage, File Storage, Queue Storage, and Table Storage.
22//!
23//! ## Overview
24//!
25//! Azure Storage supports multiple authentication methods, and this crate
26//! implements all major ones:
27//!
28//! - **Shared Key**: Using storage account access keys
29//! - **SAS Token**: Pre-generated Shared Access Signature tokens
30//! - **Bearer Token**: OAuth2/Azure AD authentication
31//!
32//! ## Quick Start
33//!
34//! ```no_run
35//! use anyhow::Result;
36//! use reqsign_azure_storage::{DefaultCredentialProvider, RequestSigner};
37//! use reqsign_core::{Context, Signer};
38//! use reqsign_file_read_tokio::TokioFileRead;
39//! use reqsign_http_send_reqwest::ReqwestHttpSend;
40//! use reqwest::Client;
41//!
42//! #[tokio::main]
43//! async fn main() -> Result<()> {
44//!     // Create context
45//!     let ctx = Context::new()
46//!         .with_file_read(TokioFileRead::default())
47//!         .with_http_send(ReqwestHttpSend::default());
48//!
49//!     // Create credential loader (will try multiple methods)
50//!     let loader = DefaultCredentialProvider::new();
51//!
52//!     // Create request builder
53//!     let builder = RequestSigner::new();
54//!
55//!     // Create the signer
56//!     let signer = Signer::new(ctx, loader, builder);
57//!
58//!     // Sign requests
59//!     let mut req = http::Request::get("https://mystorageaccount.blob.core.windows.net/container/blob")
60//!         .body(())
61//!         .unwrap()
62//!         .into_parts()
63//!         .0;
64//!
65//!     signer.sign(&mut req, None).await?;
66//!     Ok(())
67//! }
68//! ```
69//!
70//! ## Credential Sources
71//!
72//! ### Environment Variables
73//!
74//! ```bash
75//! # For Shared Key authentication
76//! export AZURE_STORAGE_ACCOUNT_NAME=mystorageaccount
77//! export AZURE_STORAGE_ACCOUNT_KEY=base64key
78//!
79//! # For SAS Token authentication
80//! export AZURE_STORAGE_SAS_TOKEN=sv=2021-06-08&ss=b&srt=sco...
81//!
82//! # For Azure AD authentication
83//! export AZURE_CLIENT_ID=client-id
84//! export AZURE_CLIENT_SECRET=client-secret
85//! export AZURE_TENANT_ID=tenant-id
86//! ```
87//!
88//! ### Managed Identity
89//!
90//! When running on Azure services (VMs, App Service, AKS), the crate
91//! automatically uses managed identity:
92//!
93//! ```no_run
94//! use reqsign_azure_storage::DefaultCredentialProvider;
95//!
96//! // Create loader that will try managed identity
97//! let loader = DefaultCredentialProvider::new();
98//! ```
99//!
100//! Customize the default chain with slot-based builder APIs:
101//!
102//! ```no_run
103//! use reqsign_azure_storage::{
104//!     ClientSecretCredentialProvider, DefaultCredentialProvider,
105//! };
106//!
107//! let loader = DefaultCredentialProvider::builder()
108//!     .no_env()
109//!     .client_secret(ClientSecretCredentialProvider::new().with_tenant_id("tenant-id"))
110//!     .no_imds()
111//!     .build();
112//! ```
113//!
114//! ## Storage Services
115//!
116//! ### Blob Storage
117//!
118//! ```no_run
119//! # use http::Request;
120//! // List containers
121//! let req = Request::get("https://account.blob.core.windows.net/?comp=list")
122//!     .body(())
123//!     .unwrap();
124//!
125//! // Get blob
126//! let req = Request::get("https://account.blob.core.windows.net/container/blob.txt")
127//!     .body(())
128//!     .unwrap();
129//! ```
130//!
131//! ### File Storage
132//!
133//! ```no_run
134//! # use http::Request;
135//! // List shares
136//! let req = Request::get("https://account.file.core.windows.net/?comp=list")
137//!     .body(())
138//!     .unwrap();
139//! ```
140//!
141//! ## Advanced Features
142//!
143//! ### Account SAS Generation
144//!
145//! Generate SAS tokens for delegated access:
146//!
147//! ```text
148//! // Account SAS is not yet exposed in the public API
149//! // This is planned for future releases
150//! ```
151//!
152//! ### Using Specific Credential Providers
153//!
154//! ```no_run
155//! use reqsign_azure_storage::{StaticCredentialProvider, EnvCredentialProvider};
156//!
157//! // Use static credentials
158//! let static_loader = StaticCredentialProvider::new_shared_key(
159//!     "mystorageaccount",
160//!     "base64key"
161//! );
162//!
163//! // Or use environment variables
164//! let env_loader = EnvCredentialProvider::new();
165//! ```
166//!
167//! ## Examples
168//!
169//! Check out the examples directory for more detailed usage:
170//! - [Blob storage operations](examples/blob_storage.rs)
171//! - [SAS token generation](examples/sas_token.rs)
172
173mod constants;
174mod service_sas;
175mod user_delegation;
176mod user_delegation_sas;
177
178mod credential;
179pub use credential::Credential;
180
181pub use service_sas::{
182    SasIpRange, ServiceSasBlobPermissions, ServiceSasConfig, ServiceSasContainerPermissions,
183    ServiceSasGrant, ServiceSasGranter, ServiceSasResource, ServiceSasVersion,
184    ServiceSharedAccessSignature,
185};
186pub use user_delegation_sas::{
187    SasProtocol, UserDelegationSasGrant, UserDelegationSasGranter, UserDelegationSasPermissions,
188};
189
190mod sign_request;
191pub use sign_request::RequestSigner;
192
193mod provide_credential;
194pub use provide_credential::*;