Skip to content

Commit

Permalink
Update to latest crates, fix tests, fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
phyber committed Oct 14, 2024
1 parent 8393b9f commit 95a1540
Show file tree
Hide file tree
Showing 8 changed files with 976 additions and 804 deletions.
1,672 changes: 917 additions & 755 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ exclude = [

[dependencies]
anyhow = "1.0"
aws-config = "0.55.0"
aws-sdk-s3 = "0.25.0"
aws-types = "0.55.0"
aws-config = "1.5.8"
aws-sdk-s3 = "1.56.0"
aws-types = "1.3.3"
colored = "2"
csv = "1.2"
log = "0.4"
pretty_env_logger = "0.4"
pretty_env_logger = "0.5"
serde_json = "1.0"

[dependencies.clap]
Expand Down
10 changes: 5 additions & 5 deletions src/s3/acl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ pub enum BucketAcl {

impl From<GetBucketAclOutput> for BucketAcl {
fn from(output: GetBucketAclOutput) -> Self {
let Some(grants) = output.grants() else {
return Self::Private;
};
let grants = output.grants();

// Might have no grants
if grants.is_empty() {
Expand Down Expand Up @@ -90,7 +88,8 @@ mod tests {
.id("lovelace")
.r#type(Type::from("N/A"))
.uri(PRIVATE_GROUP)
.build();
.build()
.unwrap();

let grant = Grant::builder()
.grantee(grantee)
Expand Down Expand Up @@ -122,7 +121,8 @@ mod tests {
.id("lovelace")
.r#type(Type::from("N/A"))
.uri(PUBLIC_GROUP)
.build();
.build()
.unwrap();

let grant = Grant::builder()
.grantee(grantee)
Expand Down
38 changes: 19 additions & 19 deletions src/s3/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::s3::{
Reports,
};
use anyhow::Result;
use aws_config::BehaviorVersion;
use aws_config::meta::region::RegionProviderChain;
use aws_sdk_s3::client::Client as S3Client;
use aws_sdk_s3::error::SdkError;
Expand Down Expand Up @@ -52,15 +53,15 @@ impl Client {
.or_default_provider()
.or_else("us-east-1");

let config = aws_config::from_env()
let config = aws_config::defaults(BehaviorVersion::latest())
.region(region_provider)
.load()
.await;

let client = S3Client::new(&config);

Self {
client: client,
client,
}
}

Expand All @@ -75,21 +76,20 @@ impl Client {
.send()
.await?;

let bucket_names = output.buckets().map_or_else(Vec::new, |buckets| {
buckets
.iter()
.filter_map(aws_sdk_s3::types::Bucket::name)
.collect()
});
let bucket_names: Vec<&str> = output
.buckets()
.iter()
.filter_map(aws_sdk_s3::types::Bucket::name)
.collect();

let mut buckets: Vec<Bucket> = Vec::new();

for bucket in bucket_names {
let region = self.get_bucket_region(bucket).await?;

let bucket = Bucket {
region,
name: bucket.to_string(),
region: region,
};

buckets.push(bucket);
Expand Down Expand Up @@ -323,7 +323,7 @@ impl Client {

// Both of these come from the Versioning API, so enabled either of
// these needs to get the bucket versioning.
let versioning_audits = vec![
let versioning_audits = [
Audit::MfaDelete,
Audit::Versioning,
];
Expand All @@ -349,14 +349,14 @@ impl Client {
};

let report = Report {
name: bucket.into(),
acl: acl,
encryption: encryption,
logging: logging,
policy: policy,
public_access_block: public_access_block,
versioning: versioning,
website: website,
acl,
encryption,
logging,
policy,
public_access_block,
versioning,
website,
name: bucket.into(),
};

Ok(report)
Expand All @@ -373,8 +373,8 @@ impl Client {
Some(bucket) => {
let region = self.get_bucket_region(&bucket).await?;
let bucket = Bucket {
region,
name: bucket,
region: region,
};

vec![bucket]
Expand Down
28 changes: 18 additions & 10 deletions src/s3/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type EncryptionResult = Result<
// Could probably replace a log of this with some .and_then shenanigans.
impl From<GetBucketEncryptionOutput> for BucketEncryption {
fn from(output: GetBucketEncryptionOutput) -> Self {
let sse_algorithm = output.server_side_encryption_configuration
.and_then(|config| config.rules)
let sse_algorithm = output
.server_side_encryption_configuration
.map(|config| config.rules)
.and_then(|rules| {
if rules.is_empty() {
None
Expand All @@ -37,7 +38,7 @@ impl From<GetBucketEncryptionOutput> for BucketEncryption {
}
})
.and_then(|rule| rule.apply_server_side_encryption_by_default)
.and_then(|rule| rule.sse_algorithm);
.map(|rule| rule.sse_algorithm);

match sse_algorithm {
None => Self::None,
Expand Down Expand Up @@ -106,15 +107,17 @@ mod tests {
fn test_from_default_encryption() {
let default = ServerSideEncryptionByDefault::builder()
.sse_algorithm(ServerSideEncryption::Aes256)
.build();
.build()
.unwrap();

let rule = ServerSideEncryptionRule::builder()
.apply_server_side_encryption_by_default(default)
.build();

let configuration = ServerSideEncryptionConfiguration::builder()
.rules(rule)
.build();
.build()
.unwrap();

let output = GetBucketEncryptionOutput::builder()
.server_side_encryption_configuration(configuration)
Expand All @@ -132,15 +135,17 @@ mod tests {
let default = ServerSideEncryptionByDefault::builder()
.kms_master_key_id("arn:aws:foo:bar:test")
.sse_algorithm(ServerSideEncryption::AwsKms)
.build();
.build()
.unwrap();

let rule = ServerSideEncryptionRule::builder()
.apply_server_side_encryption_by_default(default)
.build();

let configuration = ServerSideEncryptionConfiguration::builder()
.rules(rule)
.build();
.build()
.unwrap();

let output = GetBucketEncryptionOutput::builder()
.server_side_encryption_configuration(configuration)
Expand All @@ -157,15 +162,17 @@ mod tests {
fn test_from_unknown_encryption() {
let default = ServerSideEncryptionByDefault::builder()
.sse_algorithm(ServerSideEncryption::from("wat"))
.build();
.build()
.unwrap();

let rule = ServerSideEncryptionRule::builder()
.apply_server_side_encryption_by_default(default)
.build();

let configuration = ServerSideEncryptionConfiguration::builder()
.rules(rule)
.build();
.build()
.unwrap();

let output = GetBucketEncryptionOutput::builder()
.server_side_encryption_configuration(configuration)
Expand All @@ -182,7 +189,8 @@ mod tests {
fn test_from_no_rules() {
let configuration = ServerSideEncryptionConfiguration::builder()
.set_rules(Some(Vec::new()))
.build();
.build()
.unwrap();

let output = GetBucketEncryptionOutput::builder()
.server_side_encryption_configuration(configuration)
Expand Down
7 changes: 4 additions & 3 deletions src/s3/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ pub enum BucketLogging {
impl From<GetBucketLoggingOutput> for BucketLogging {
fn from(output: GetBucketLoggingOutput) -> Self {
output.logging_enabled.map_or(Self::Disabled, |logging| {
let target = logging.target_bucket.expect("target bucket");
Self::Enabled(target)
Self::Enabled(logging.target_bucket)
})
}
}
Expand Down Expand Up @@ -44,7 +43,9 @@ mod tests {
fn test_from_for_bucket_logging_enabled() {
let logging_enabled = LoggingEnabled::builder()
.target_bucket("foo")
.build();
.target_prefix("test")
.build()
.unwrap();

let output = GetBucketLoggingOutput::builder()
.set_logging_enabled(Some(logging_enabled))
Expand Down
9 changes: 5 additions & 4 deletions src/s3/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ impl TryFrom<GetBucketPolicyOutput> for BucketPolicy {
let statements_array = statements.as_array()
.expect("Bucket policy has no Statements element");

for statement in statements_array.iter() {
for statement in statements_array {
// Policies MUST have an effect. This should never fail.
let effect = statement["Effect"].as_str()
let effect = statement["Effect"]
.as_str()
.expect("Bucket policy statement does not have an explicit Effect");

// If we're denying stuff, wildcards are fine and we can proceed
Expand All @@ -162,8 +163,8 @@ impl TryFrom<GetBucketPolicyOutput> for BucketPolicy {
}

Ok(Self {
actions: actions,
principals: principals,
actions,
principals,
})
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/s3/public_access_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ impl From<GetPublicAccessBlockOutput> for PublicAccessBlock {
let config = output.public_access_block_configuration
.expect("public_access_block_configuration");

let block_public_acls = config.block_public_acls;
let block_public_policy = config.block_public_policy;
let ignore_public_acls = config.ignore_public_acls;
let restrict_public_buckets = config.restrict_public_buckets;
let block_public_acls = config.block_public_acls.unwrap_or(false);
let block_public_policy = config.block_public_policy.unwrap_or(false);
let ignore_public_acls = config.ignore_public_acls.unwrap_or(false);
let restrict_public_buckets = config.restrict_public_buckets.unwrap_or(false);

let blocks = vec![
PublicAccessBlockType::BlockPublicAcls(block_public_acls),
Expand Down

0 comments on commit 95a1540

Please sign in to comment.