Line data Source code
1 : use anyhow::Result;
2 : use std::env;
3 :
4 : /// The R2 credentials of the release bucket, read from the environment
5 : pub struct RemoteAccess {
6 : /// The R2 S3 endpoint URL (`DASHQL_GET_R2_ENDPOINT`)
7 : pub r2_endpoint: String,
8 : /// The R2 access key id (`DASHQL_GET_R2_ACCESS_KEY_ID`)
9 : pub r2_access_key_id: String,
10 : /// The R2 secret access key (`DASHQL_GET_R2_SECRET_ACCESS_KEY`)
11 : pub r2_secret_access_key: String,
12 : }
13 :
14 : impl RemoteAccess {
15 8 : pub fn from_env() -> Result<RemoteAccess> {
16 8 : let r2_endpoint = env::var("DASHQL_GET_R2_ENDPOINT").map_err(|e| {
17 1 : anyhow::anyhow!(
18 : "failed to access environment variable DASHQL_GET_R2_ENDPOINT: {}",
19 : e
20 : )
21 1 : })?;
22 7 : let r2_access_key_id = env::var("DASHQL_GET_R2_ACCESS_KEY_ID").map_err(|e| {
23 0 : anyhow::anyhow!(
24 : "failed to access environment variable DASHQL_GET_R2_ACCESS_KEY_ID: {}",
25 : e
26 : )
27 0 : })?;
28 7 : let r2_secret_access_key = env::var("DASHQL_GET_R2_SECRET_ACCESS_KEY").map_err(|e| {
29 1 : anyhow::anyhow!(
30 : "failed to access environment variable DASHQL_GET_R2_SECRET_ACCESS_KEY: {}",
31 : e
32 : )
33 1 : })?;
34 : // Interestingly, doing the is_empty check results in unreachable warnings
35 6 : assert!(!r2_endpoint.is_empty());
36 6 : assert!(!r2_access_key_id.is_empty());
37 6 : assert!(!r2_secret_access_key.is_empty());
38 6 : Ok(RemoteAccess {
39 6 : r2_endpoint,
40 6 : r2_access_key_id,
41 6 : r2_secret_access_key,
42 6 : })
43 8 : }
44 :
45 : /// Build the client of the release bucket
46 23 : pub fn r2_client(&self) -> aws_sdk_s3::Client {
47 23 : let credentials = aws_credential_types::provider::SharedCredentialsProvider::new(self.get_credentials());
48 23 : let config = aws_sdk_s3::Config::builder()
49 23 : .behavior_version_latest()
50 23 : .endpoint_url(&self.r2_endpoint)
51 23 : .region(aws_config::Region::new("auto"))
52 23 : .credentials_provider(credentials)
53 23 : .build();
54 23 : aws_sdk_s3::Client::from_conf(config)
55 23 : }
56 :
57 23 : pub fn get_credentials(&self) -> aws_credential_types::Credentials {
58 23 : aws_credential_types::Credentials::new(
59 23 : self.r2_access_key_id.clone(),
60 23 : self.r2_secret_access_key.clone(),
61 23 : None,
62 23 : None,
63 : "r2",
64 : )
65 23 : }
66 : }
|