1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! Probe an S3-compatible endpoint: create the bucket if needed, then
//! put/get/delete a small object. Example:
//!
//! ```text
//! cargo run --example s3_probe -- \
//! --endpoint http://127.0.0.1:9000 --bucket vuln-notif \
//! --access-key rustfs --secret-key rustfs-secret --region us-east-1
//! ```
use anyhow::{Context, Result, anyhow, bail};
use aws_sdk_s3::error::SdkError;
use aws_sdk_s3::primitives::ByteStream;
use vuln_notif::config::S3Config;
use vuln_notif::s3;
const PROBE_KEY: &str = "vuln-notif-probe.txt";
const PROBE_BODY: &[u8] = b"vuln-notif s3 probe\n";
const USAGE: &str = "usage: s3_probe --bucket BUCKET [--endpoint URL] [--access-key KEY] \
[--secret-key SECRET] [--region REGION]";
#[tokio::main]
async fn main() -> Result<()> {
let mut endpoint = None;
let mut bucket = None;
let mut access_key = None;
let mut secret_key = None;
let mut region = "us-east-1".to_owned();
let mut args = std::env::args().skip(1);
while let Some(arg) = args.next() {
match arg.as_str() {
"--endpoint" => endpoint = Some(next_value(&mut args, "--endpoint")?),
"--bucket" => bucket = Some(next_value(&mut args, "--bucket")?),
"--access-key" => access_key = Some(next_value(&mut args, "--access-key")?),
"--secret-key" => secret_key = Some(next_value(&mut args, "--secret-key")?),
"--region" => region = next_value(&mut args, "--region")?,
"--help" | "-h" => {
println!("{USAGE}");
return Ok(());
}
other => bail!("unknown argument {other}; {USAGE}"),
}
}
let cfg = S3Config {
bucket: bucket.ok_or_else(|| anyhow!("--bucket is required; {USAGE}"))?,
path: PROBE_KEY.to_owned(),
endpoint_url: endpoint,
access_key_id: access_key,
secret_access_key: secret_key,
region,
};
let client = s3::client(&cfg).await?;
match client.create_bucket().bucket(&cfg.bucket).send().await {
Ok(_) => println!("created bucket {}", cfg.bucket),
Err(err) => {
let exists = match &err {
SdkError::ServiceError(ctx) => {
ctx.err().is_bucket_already_owned_by_you()
|| ctx.err().is_bucket_already_exists()
}
_ => false,
};
if !exists {
return Err(
anyhow::Error::new(err).context(format!("create bucket {}", cfg.bucket))
);
}
println!("bucket {} already exists", cfg.bucket);
}
}
client
.put_object()
.bucket(&cfg.bucket)
.key(&cfg.path)
.content_type("text/plain")
.body(ByteStream::from_static(PROBE_BODY))
.send()
.await
.with_context(|| format!("put s3://{}/{}", cfg.bucket, cfg.path))?;
println!("put {} bytes", PROBE_BODY.len());
let body = client
.get_object()
.bucket(&cfg.bucket)
.key(&cfg.path)
.send()
.await
.with_context(|| format!("get s3://{}/{}", cfg.bucket, cfg.path))?
.body
.collect()
.await
.with_context(|| format!("read s3://{}/{} body", cfg.bucket, cfg.path))?
.into_bytes();
if body != PROBE_BODY {
bail!("probe object content mismatch ({} bytes)", body.len());
}
println!("got {} bytes", body.len());
client
.delete_object()
.bucket(&cfg.bucket)
.key(&cfg.path)
.send()
.await
.with_context(|| format!("delete s3://{}/{}", cfg.bucket, cfg.path))?;
println!("deleted s3://{}/{}", cfg.bucket, cfg.path);
Ok(())
}
fn next_value(args: &mut impl Iterator<Item = String>, flag: &str) -> Result<String> {
args.next()
.ok_or_else(|| anyhow!("{flag} requires a value; {USAGE}"))
}