iam-rs
USAGE NOTICE: This library is still in final phase of verification and may have breaking changes. Pin exact version. It is functional and ready to use, but it has not been externally verified. Small differences may exist compared to AWS's own internal implementation. Please report any discrepancies.
A comprehensive Rust library for parsing, validating, and evaluating AWS IAM (Identity and Access Management) policies. Provider-agnostic and designed for building flexible authorization systems with full AWS IAM compatibility.
๐ Key Features
- ๐ Complete IAM Policy Support: Full implementation of AWS IAM policy language including conditions, principals, actions, and resources
- โ๏ธ Policy Evaluation Engine: Production-ready authorization engine with proper AWS IAM precedence rules
- ๐ท๏ธ Advanced ARN Support: Comprehensive ARN parsing, validation, and wildcard pattern matching
- ๐ฏ Rich Condition Engine: Support for all AWS condition operators (String, Numeric, Date, Boolean, IP, ARN, Binary, Null)
- ๏ฟฝ Variable Interpolation: Dynamic policy variables with default fallback values (e.g.,
${aws:username, 'anonymous'}) - ๐ฆ Type-Safe APIs: Strong typing with comprehensive enums, builder patterns, and Serde integration
- โก High Performance: Zero-copy parsing, efficient evaluation, and minimal dependencies
- ๐งช Production Ready: Extensive test suite with 100+ tests covering real-world scenarios
๐ฆ Installation
๐ Quick Start
Simple Authorization Check
use ;
// Create a policy allowing S3 read access
let policy = new
.add_statement;
// Create an authorization request
let request = new;
// Evaluate the request
match evaluate_policy?
Policy with Conditions
use ;
use json;
// Create context for condition evaluation
let mut context = new;
context.insert;
context.insert;
context.insert;
// Policy with string and date conditions
let policy = new
.with_id
.add_statement;
let request = new_with_context;
let decision = evaluate_policy?;
๐ Core Components
IAM Policy Structure
use ;
let policy = new
.with_version // AWS standard version
.with_id
.add_statement;
Advanced Pattern Matching
ARN Wildcard Patterns
use Arn;
let arn = parse?;
// Test various wildcard patterns
let patterns = ;
for pattern in patterns
Action Wildcards
// Action wildcard matching
let actions = Multiple;
๐ง Variable Interpolation
IAM-rs supports AWS policy variables with default fallback values, enabling dynamic resource paths and conditions.
Basic Variable Usage
use ;
// Set up context
let mut context = new;
context.insert;
context.insert;
// Basic variable interpolation
let resource_pattern = "arn:aws:s3:::company-bucket/${aws:username}/*";
let resolved = interpolate_variables?;
// Result: "arn:aws:s3:::company-bucket/alice/*"
// Variable with default fallback
let team_pattern = "arn:aws:s3:::team-bucket-${aws:PrincipalTag/team, 'default'}/*";
let resolved = interpolate_variables?;
// Result: "arn:aws:s3:::team-bucket-red/*"
Variables with Default Values
// When context key is missing, use default value
let empty_context = new;
let pattern = "arn:aws:s3:::bucket-${aws:PrincipalTag/department, 'general'}/*";
let resolved = interpolate_variables?;
// Result: "arn:aws:s3:::bucket-general/*" (uses default)
// Common variable patterns
let patterns = ;
Dynamic Policy Example
// Policy that grants access to user-specific paths with team fallback
let policy = new
.add_statement;
๐ฏ Condition Operators
IAM-rs supports all AWS condition operators with full type safety:
String Conditions
use Operator;
// Basic string operations
StringEquals // Exact match
StringNotEquals // Not equal
StringEqualsIgnoreCase // Case-insensitive match
StringLike // Wildcard matching (*, ?)
StringNotLike // Inverse wildcard matching
// Set-based string operations
ForAnyValueStringEquals // At least one value matches
ForAllValuesStringEquals // All values match
Numeric and Date Conditions
// Numeric comparisons
NumericEquals
NumericNotEquals
NumericLessThan
NumericLessThanEquals
NumericGreaterThan
NumericGreaterThanEquals
// Date/time comparisons
DateEquals
DateNotEquals
DateLessThan
DateGreaterThan
DateLessThanEquals
DateGreaterThanEquals
Specialized Conditions
// Boolean conditions
Bool
// IP address conditions
IpAddress // IP within CIDR range
NotIpAddress // IP not in CIDR range
// ARN conditions
ArnEquals // Exact ARN match
ArnLike // ARN wildcard matching
ArnNotEquals
ArnNotLike
// Null checks
Null // Key exists/doesn't exist
// Binary data
BinaryEquals // Base64 binary comparison
Complex Condition Example
let statement = new
.with_action
.with_resource
// Must be from trusted IP range
.with_condition
// Must have MFA
.with_condition
// Must be during business hours
.with_condition
.with_condition
// User must have required tag
.with_condition;
โ๏ธ Policy Evaluation Engine
Advanced Evaluation Options
use ;
let evaluator = with_policies
.with_options;
let result = evaluator.evaluate?;
println!;
println!;
// Examine detailed results
for statement_match in result.statement_details
IAM Precedence Rules
The evaluation engine implements proper AWS IAM logic:
- Explicit Deny: Always takes precedence over Allow
- Explicit Allow: Required for access (no implicit allow)
- Implicit Deny: Default when no Allow statements match
- Conditions: Must be satisfied for statement to apply
- Multiple Policies: Combined with proper precedence
// Example demonstrating precedence
let allow_policy = new
.add_statement;
let deny_policy = new
.add_statement;
let policies = vec!;
let result = evaluate_policies?;
// Result: Decision::Deny (Explicit deny wins)
๐ JSON Policy Support
Parsing from JSON
let json_policy = r#"
{
"Version": "2012-10-17",
"Id": "S3BucketPolicy",
"Statement": [
{
"Sid": "AllowUserAccess",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:user/alice"
},
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::my-bucket/${aws:username}/*",
"Condition": {
"StringEquals": {
"s3:x-amz-server-side-encryption": "AES256"
},
"NumericLessThan": {
"s3:max-keys": "10"
}
}
}
]
}
"#;
let policy = from_json?;
println!;
Generating JSON
// Create policy programmatically
let policy = new
.with_id
.add_statement;
// Export to JSON
let json_output = policy.to_json?;
println!;
๐งช Examples
Run the comprehensive examples to see all features in action:
# ARN parsing and wildcard matching
# Policy validation and structure
# Complete evaluation engine with conditions
Example Scenarios Covered
- โ Basic Allow/Deny policies with simple action/resource matching
- โ Wildcard patterns for actions, resources, and principals
- โ Complex conditions with String, Numeric, Date, Boolean, IP, and ARN operators
- โ Variable interpolation with fallback values for dynamic policies
- โ Multi-policy evaluation with proper precedence handling
- โ Real-world scenarios like user folder access, time-based restrictions
- โ Resource-based policies for S3 buckets, Lambda functions, etc.
- โ Cross-account access with proper principal validation
๐ค Contributing
Contributions are welcome! This library aims to be the definitive Rust implementation of AWS IAM policy evaluation.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Add tests for new functionality
- Run the test suite (
cargo test) - Check code quality (
cargo clippy) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.