Skip to content

Commit 8ee6c6b

Browse files
committed
allow authenticating to AWS with the EC2 instance role
This changes the credentials provider used to fetch the AWS credentials from EnvironmentProvider (which just looked at environment variables) to DefaultCredentialsProvider, which looks at: 1. Environment variables 2. ~/.aws/credentials 3. EC2 instance roles The old behavior is preserved when the environment variable is present, but this will also allow using EC2 instance roles which are going to be implemented on the production server. A new FORCE_S3 environment variable was also added.
1 parent f01dedd commit 8ee6c6b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/db/file.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use error::Result;
1414
use failure::err_msg;
1515
use rusoto_s3::{S3, PutObjectRequest, GetObjectRequest, S3Client};
1616
use rusoto_core::region::Region;
17-
use rusoto_credential::EnvironmentProvider;
17+
use rusoto_credential::DefaultCredentialsProvider;
1818

1919

2020
fn get_file_list_from_dir<P: AsRef<Path>>(path: P,
@@ -115,12 +115,19 @@ pub fn get_path(conn: &Connection, path: &str) -> Option<Blob> {
115115
fn s3_client() -> Option<S3Client> {
116116
// If AWS keys aren't configured, then presume we should use the DB exclusively
117117
// for file storage.
118-
if std::env::var_os("AWS_ACCESS_KEY_ID").is_none() {
118+
if std::env::var_os("AWS_ACCESS_KEY_ID").is_none() && std::env::var_os("FORCE_S3").is_none() {
119119
return None;
120120
}
121+
let creds = match DefaultCredentialsProvider::new() {
122+
Ok(creds) => creds,
123+
Err(err) => {
124+
warn!("failed to retrieve AWS credentials: {}", err);
125+
return None;
126+
}
127+
};
121128
Some(S3Client::new_with(
122129
rusoto_core::request::HttpClient::new().unwrap(),
123-
EnvironmentProvider::default(),
130+
creds,
124131
std::env::var("S3_ENDPOINT").ok().map(|e| Region::Custom {
125132
name: "us-west-1".to_owned(),
126133
endpoint: e,

0 commit comments

Comments
 (0)