Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect AWS_VIRTUAL_HOSTING env var #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions arbiter/drivers/s3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,14 +903,18 @@ S3::Resource::Resource(std::string base, std::string fullPath)
: m_baseUrl(base)
, m_bucket()
, m_object()
, m_virtualHosted(true)
, m_virtualHosted()
{
fullPath = sanitize(fullPath);
const std::size_t split(fullPath.find("/"));

m_bucket = fullPath.substr(0, split);
if (split != std::string::npos) m_object = fullPath.substr(split + 1);

// By default, we use virtual-hosted URLs if the bucket name contains no dots.
// This can be overridden with the AWS_VIRTUAL_HOSTING environment variable
// (set to "TRUE" or "FALSE")
//
// We would prefer to use virtual-hosted URLs all the time since path-style
// URLs are being deprecated in 2020. We also want to use HTTPS all the
// time, which is required for KMS-managed server-side encryption. However,
Expand All @@ -929,7 +933,10 @@ S3::Resource::Resource(std::string base, std::string fullPath)
// 2021 note: the deprecation date got delayed, and buckets containing
// dots still has no fix - see the note at the top of the first link above.
// So for the time being, we'll keep this forked logic below.
m_virtualHosted = m_bucket.find_first_of('.') == std::string::npos;
m_virtualHosted = parseBoolFromEnv(
"AWS_VIRTUAL_HOSTING",
m_bucket.find_first_of('.') == std::string::npos
);
}

std::string S3::Resource::canonicalUri() const
Expand Down
23 changes: 23 additions & 0 deletions arbiter/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,29 @@ std::unique_ptr<std::string> env(const std::string& var)
return result;
}

bool parseBoolFromEnv(const std::string& var, bool defaultValue)
{
auto value = env(var);
if (!value)
{
// env var is not set
return defaultValue;
}
if (value->empty())
{
// env var is set to the empty string; interpret as false
return false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this is backwards from what would be expected, e.g. if I run VERBOSE ./my-app I might expect that I've set VERBOSE to be enabled rather than disabled.

}

const char firstChar = std::tolower((*value)[0]);
if (firstChar == 't' || firstChar == 'T' || firstChar == '1')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can skip the lower/upper-case checks since we've already done tolower on our character.

return true;
else if (firstChar == 'f' || firstChar == 'F' || firstChar == '0')
return false;
else
return defaultValue;
}

std::vector<std::string> split(const std::string& in, const char delimiter)
{
std::size_t index(0);
Expand Down
4 changes: 4 additions & 0 deletions arbiter/util/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ inline std::string join(std::string path, Paths&&... paths)
*/
ARBITER_DLL std::unique_ptr<std::string> env(const std::string& var);

/** Parses a boolean value from an environment variable.
* Values are like "TRUE"/"FALSE"/"0"/"1" */
ARBITER_DLL bool parseBoolFromEnv(const std::string& var, bool defaultValue);

/** @brief Split a string on a token. */
ARBITER_DLL std::vector<std::string> split(
const std::string& s,
Expand Down