Skip to content

Commit

Permalink
Merge pull request #18 from jhiemstrawisc/pr-16
Browse files Browse the repository at this point in the history
Add path-style hosting to plugin
  • Loading branch information
jhiemstrawisc authored Feb 29, 2024
2 parents 6c240c8 + af9b857 commit 91c03b4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 15 deletions.
7 changes: 6 additions & 1 deletion src/S3Commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ bool AmazonRequest::parseURL( const std::string & url,

auto j = url.find( "/", i + 3 );
if( j == std::string::npos ) {
host = bucket + "." + substring( url, i + 3 );
if (style == "path") {
host = substring( url, i + 3 );
} else {
host = bucket + "." + substring( url, i + 3 );
}

path = "/" + object;
return true;
}
Expand Down
27 changes: 18 additions & 9 deletions src/S3Commands.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ public:
const std::string & skf,
const std::string & b,
const std::string & o,
const std::string & style,
int sv = 4
) :
HTTPRequest( s ),
accessKeyFile(akf),
secretKeyFile(skf),
signatureVersion(sv),
bucket(b),
object(o)
object(o),
style(style)
{
requiresSignature = true;
// Start off by parsing the hostUrl, which we use in conjunction with the bucket to fill in the host (for setting host header).
Expand All @@ -33,8 +35,11 @@ public:

// Now that we have the host and canonicalURI, we can build the actual url we perform the curl against.
// Using the previous example, we'd get a new hostUrl of "https://my-bucket.my-url.com:443/my-object".
hostUrl = protocol + "://" +
host + canonicalURI;
if (style == "path") {
hostUrl = protocol + "://" + host + "/" + b + canonicalURI;
} else {
hostUrl = protocol + "://" + host + canonicalURI;
}

// If we can, set the region based on the host.
size_t secondDot = host.find( ".", 2 + 1 );
Expand Down Expand Up @@ -77,6 +82,7 @@ protected:
std::string region;
std::string service;

std::string style;
private:
bool createV4Signature( const std::string & payload, std::string & authorizationHeader, bool sendContentSHA = false );

Expand All @@ -91,9 +97,10 @@ public:
const std::string & akf,
const std::string & skf,
const std::string & b,
const std::string & o
const std::string & o,
const std::string & style
) :
AmazonRequest(s, akf, skf, b, o){}
AmazonRequest(s, akf, skf, b, o, style){}

virtual ~AmazonS3Upload();

Expand All @@ -111,9 +118,10 @@ public:
const std::string & akf,
const std::string & skf,
const std::string & b,
const std::string & o
const std::string & o,
const std::string & style
) :
AmazonRequest(s, akf, skf, b, o){}
AmazonRequest(s, akf, skf, b, o, style){}

virtual ~AmazonS3Download();

Expand All @@ -128,9 +136,10 @@ public:
const std::string & akf,
const std::string & skf,
const std::string & b,
const std::string & o
const std::string & o,
const std::string & style
) :
AmazonRequest(s, akf, skf, b, o){}
AmazonRequest(s, akf, skf, b, o, style){}

virtual ~AmazonS3Head();

Expand Down
17 changes: 12 additions & 5 deletions src/S3File.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "XrdVersion.hh"
#include "S3FileSystem.hh"
#include "S3File.hh"
#include "stl_string_utils.hh"

#include <curl/curl.h>

Expand Down Expand Up @@ -95,6 +96,7 @@ S3File::Open(const char *path, int Oflag, mode_t Mode, XrdOucEnv &env)
std::string configured_s3_service_url = m_oss->getS3ServiceURL();
std::string configured_s3_access_key = m_oss->getS3AccessKeyFile();
std::string configured_s3_secret_key = m_oss->getS3SecretKeyFile();
std::string configured_s3_url_style = m_oss->getS3URLStyle();


// We used to query S3 here to see if the object existed, but of course
Expand All @@ -105,6 +107,7 @@ S3File::Open(const char *path, int Oflag, mode_t Mode, XrdOucEnv &env)
this->s3_service_url = configured_s3_service_url;
this->s3_access_key = configured_s3_access_key;
this->s3_secret_key = configured_s3_secret_key;
this->s3_url_style = configured_s3_url_style;
return 0;
}

Expand All @@ -117,7 +120,8 @@ S3File::Read(void *buffer, off_t offset, size_t size)
this->s3_access_key,
this->s3_secret_key,
this->s3_bucket_name,
this->s3_object_name
this->s3_object_name,
this->s3_url_style
);


Expand All @@ -140,7 +144,8 @@ S3File::Fstat(struct stat *buff)
this->s3_access_key,
this->s3_secret_key,
this->s3_bucket_name,
this->s3_object_name
this->s3_object_name,
this->s3_url_style
);

if(! head.SendRequest()) {
Expand Down Expand Up @@ -169,10 +174,11 @@ S3File::Fstat(struct stat *buff)
std::string attr = substring( line, 0, colon );
std::string value = substring( line, colon + 1 );
trim(value);
toLower(attr);

if( attr == "Content-Length" ) {
if( attr == "content-length" ) {
this->content_length = std::stol(value);
} else if( attr == "Last-Modified" ) {
} else if( attr == "last-modified" ) {
struct tm t;
char * eos = strptime( value.c_str(),
"%a, %d %b %Y %T %Z",
Expand Down Expand Up @@ -213,7 +219,8 @@ S3File::Write(const void *buffer, off_t offset, size_t size)
this->s3_access_key,
this->s3_secret_key,
this->s3_bucket_name,
this->s3_object_name
this->s3_object_name,
this->s3_url_style
);

std::string payload( (char *)buffer, size );
Expand Down
1 change: 1 addition & 0 deletions src/S3File.hh
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private:
std::string s3_object_name;
std::string s3_access_key;
std::string s3_secret_key;
std::string s3_url_style;

size_t content_length;
time_t last_modified;
Expand Down
14 changes: 14 additions & 0 deletions src/S3FileSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "S3FileSystem.hh"
#include "S3Directory.hh"
#include "S3File.hh"
#include "stl_string_utils.hh"

#include <memory>
#include <vector>
Expand Down Expand Up @@ -85,6 +86,8 @@ S3FileSystem::Config(XrdSysLogger *lp, const char *configfn)
value, this->s3_access_key_file ) ) { Config.Close(); return false; }
if(! handle_required_config( attribute, "s3.secret_key_file",
value, this->s3_secret_key_file ) ) { Config.Close(); return false; }
if(! handle_required_config( attribute, "s3.url_style",
value, this->s3_url_style ) ) { Config.Close(); return false; }
}

if( this->s3_service_name.empty() ) {
Expand All @@ -95,6 +98,17 @@ S3FileSystem::Config(XrdSysLogger *lp, const char *configfn)
m_log.Emsg("Config", "s3.region not specified");
return false;
}
if( this->s3_url_style.empty() ) {
m_log.Emsg("Config", "s3.url_style not specified");
return false;
} else {
// We want this to be case-insensitive.
toLower( this->s3_url_style );
}
if( this->s3_url_style != "virtual" && this->s3_url_style != "path" ) {
m_log.Emsg("Config", "invalid s3.url_style specified. Must be 'virtual' or 'path'");
return false;
}

int retc = Config.LastError();
if( retc ) {
Expand Down
3 changes: 3 additions & 0 deletions src/S3FileSystem.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public:
const std::string & getS3ServiceName() const { return s3_service_name; }
const std::string & getS3Region() const { return s3_region; }
const std::string & getS3ServiceURL() const { return s3_service_url; }
const std::string & getS3URLStyle() const { return s3_url_style; }

const std::string & getS3AccessKeyFile() const { return s3_access_key_file; }
const std::string & getS3SecretKeyFile() const { return s3_secret_key_file; }
Expand All @@ -81,4 +82,6 @@ private:

std::string s3_access_key_file;
std::string s3_secret_key_file;

std::string s3_url_style;
};

0 comments on commit 91c03b4

Please sign in to comment.