Skip to content

Commit f2e7bd5

Browse files
committed
support sts
1 parent e2d1d61 commit f2e7bd5

File tree

5 files changed

+82
-19
lines changed

5 files changed

+82
-19
lines changed

src/curl.cpp

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ using namespace std;
5252

5353
static const std::string empty_payload_hash = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
5454

55+
extern int check_for_cos_format(void); // implented in s3fs.cpp
5556
//-------------------------------------------------------------------
5657
// Utilities
5758
//-------------------------------------------------------------------
@@ -297,7 +298,7 @@ string S3fsCurl::COSAccessKeyId;
297298
string S3fsCurl::COSSecretAccessKey;
298299
string S3fsCurl::COSAccessToken;
299300
time_t S3fsCurl::COSAccessTokenExpire= 0;
300-
string S3fsCurl::RAM_role;
301+
string S3fsCurl::CAM_role;
301302
long S3fsCurl::ssl_verify_hostname = 1; // default(original code...)
302303
curltime_t S3fsCurl::curl_times;
303304
curlprogress_t S3fsCurl::curl_progress;
@@ -1039,6 +1040,30 @@ bool S3fsCurl::SetVerbose(bool flag)
10391040
return old;
10401041
}
10411042

1043+
bool S3fsCurl::checkSTSCredentialUpdate(void) {
1044+
if (S3fsCurl::CAM_role.empty()) {
1045+
return true;
1046+
}
1047+
1048+
if (time(NULL) <= S3fsCurl::COSAccessTokenExpire) {
1049+
return true;
1050+
}
1051+
1052+
// if return value is not equal 1, means wrong format key
1053+
if (check_for_cos_format() != 1) {
1054+
return false;
1055+
}
1056+
1057+
return true;
1058+
}
1059+
1060+
1061+
bool S3fsCurl::SetToken(const string& token, const string& token_expire) {
1062+
COSAccessToken = token;
1063+
COSAccessTokenExpire = cvtCAMExpireStringToTime(token_expire.c_str());
1064+
return true;
1065+
}
1066+
10421067
bool S3fsCurl::SetAccessKey(const char* AccessKeyId, const char* SecretAccessKey)
10431068
{
10441069
if(!AccessKeyId || '\0' == AccessKeyId[0] || !SecretAccessKey || '\0' == SecretAccessKey[0]){
@@ -1059,10 +1084,10 @@ long S3fsCurl::SetSslVerifyHostname(long value)
10591084
return old;
10601085
}
10611086

1062-
string S3fsCurl::SetRAMRole(const char* role)
1087+
string S3fsCurl::SetCAMRole(const char* role)
10631088
{
1064-
string old = S3fsCurl::RAM_role;
1065-
S3fsCurl::RAM_role = role ? role : "";
1089+
string old = S3fsCurl::CAM_role;
1090+
S3fsCurl::CAM_role = role ? role : "";
10661091
return old;
10671092
}
10681093

@@ -1377,14 +1402,14 @@ bool S3fsCurl::SetRAMCredentials(const char* response)
13771402
S3fsCurl::COSAccessKeyId = keyval[string(RAMCRED_ACCESSKEYID)];
13781403
S3fsCurl::COSSecretAccessKey = keyval[string(RAMCRED_SECRETACCESSKEY)];
13791404
S3fsCurl::COSAccessToken = keyval[string(RAMCRED_ACCESSTOKEN)];
1380-
S3fsCurl::COSAccessTokenExpire = cvtRAMExpireStringToTime(keyval[string(RAMCRED_EXPIRATION)].c_str());
1405+
S3fsCurl::COSAccessTokenExpire = cvtCAMExpireStringToTime(keyval[string(RAMCRED_EXPIRATION)].c_str());
13811406

13821407
return true;
13831408
}
13841409

13851410
bool S3fsCurl::CheckRAMCredentialUpdate(void)
13861411
{
1387-
if(0 == S3fsCurl::RAM_role.size()){
1412+
if(0 == S3fsCurl::CAM_role.size()){
13881413
return true;
13891414
}
13901415
if(time(NULL) + RAM_EXPIRE_MERGIN <= S3fsCurl::COSAccessTokenExpire){
@@ -1958,6 +1983,15 @@ int S3fsCurl::RequestPerform(void)
19581983
string S3fsCurl::CalcSignature(string method, string strMD5, string content_type, string date, string resource)
19591984
{
19601985
string Signature;
1986+
1987+
if (0 < S3fsCurl::CAM_role.size()) {
1988+
if (!S3fsCurl::checkSTSCredentialUpdate()) {
1989+
S3FS_PRN_ERR("Something error occurred in checking CAM STS Credential");
1990+
return Signature;
1991+
}
1992+
requestHeaders = curl_slist_sort_insert(requestHeaders, "x-cos-security-token", S3fsCurl::COSAccessToken.c_str());
1993+
}
1994+
19611995
const void* key = S3fsCurl::COSSecretAccessKey.data();
19621996
int key_len = S3fsCurl::COSSecretAccessKey.size();
19631997

@@ -2099,9 +2133,9 @@ int S3fsCurl::DeleteRequest(const char* tpath)
20992133
//
21002134
int S3fsCurl::GetRAMCredentials(void)
21012135
{
2102-
S3FS_PRN_INFO3("[RAM role=%s]", S3fsCurl::RAM_role.c_str());
2136+
S3FS_PRN_INFO3("[RAM role=%s]", S3fsCurl::CAM_role.c_str());
21032137

2104-
if(0 == S3fsCurl::RAM_role.size()){
2138+
if(0 == S3fsCurl::CAM_role.size()){
21052139
S3FS_PRN_ERR("RAM role name is empty.");
21062140
return -EIO;
21072141
}
@@ -2113,7 +2147,7 @@ int S3fsCurl::GetRAMCredentials(void)
21132147
}
21142148

21152149
// url
2116-
url = string(RAM_CRED_URL) + S3fsCurl::RAM_role;
2150+
url = string(RAM_CRED_URL) + S3fsCurl::CAM_role;
21172151
requestHeaders = NULL;
21182152
responseHeaders.clear();
21192153
bodydata = new BodyData();

src/curl.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class S3fsCurl
226226
static std::string COSSecretAccessKey;
227227
static std::string COSAccessToken;
228228
static time_t COSAccessTokenExpire;
229-
static std::string RAM_role;
229+
static std::string CAM_role;
230230
static long ssl_verify_hostname;
231231
static curltime_t curl_times;
232232
static curlprogress_t curl_progress;
@@ -352,15 +352,17 @@ class S3fsCurl
352352
static bool SetVerbose(bool flag);
353353
static bool GetVerbose(void) { return S3fsCurl::is_verbose; }
354354
static bool SetAccessKey(const char* AccessKeyId, const char* SecretAccessKey);
355+
static bool SetToken(const std::string& token, const std::string& token_expire);
355356
static bool IsSetAccessKeyId(void){
356-
return (0 < S3fsCurl::RAM_role.size() || (0 < S3fsCurl::COSAccessKeyId.size() && 0 < S3fsCurl::COSSecretAccessKey.size()));
357+
return (0 < S3fsCurl::CAM_role.size() || (0 < S3fsCurl::COSAccessKeyId.size() && 0 < S3fsCurl::COSSecretAccessKey.size()));
357358
}
359+
static bool checkSTSCredentialUpdate(void);
358360
static long SetSslVerifyHostname(long value);
359361
static long GetSslVerifyHostname(void) { return S3fsCurl::ssl_verify_hostname; }
360362
static int SetMaxParallelCount(int value);
361363
static int GetMaxParallelCount(void) { return S3fsCurl::max_parallel_cnt; }
362-
static std::string SetRAMRole(const char* role);
363-
static const char* GetRAMRole(void) { return S3fsCurl::RAM_role.c_str(); }
364+
static std::string SetCAMRole(const char* role);
365+
static const char* GetRAMRole(void) { return S3fsCurl::CAM_role.c_str(); }
364366
static bool SetMultipartSize(off_t size);
365367
static off_t GetMultipartSize(void) { return S3fsCurl::multipart_size; }
366368
static bool SetSignatureV4(bool isset) { bool bresult = S3fsCurl::is_sigv4; S3fsCurl::is_sigv4 = isset; return bresult; }

src/s3fs.cpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,17 +3705,23 @@ static int s3fs_check_service(void)
37053705
// Return: 1 - OK(could read and set accesskey etc.)
37063706
// 0 - NG(could not read)
37073707
// -1 - Should shoutdown immidiatly
3708-
static int check_for_cos_format(void)
3708+
int check_for_cos_format(void)
37093709
{
37103710
size_t first_pos = string::npos;
37113711
string line;
37123712
bool got_access_key_id_line = 0;
37133713
bool got_secret_key_line = 0;
3714+
bool got_token_line = 0;
3715+
bool got_token_expire_line = 0;
37143716
string str1 ("COSAccessKeyId=");
37153717
string str2 ("COSSecretKey=");
3718+
string str3 ("COSAccessToken=");
3719+
string str4 ("COSAccessTokenExpire=");
37163720
size_t found;
37173721
string AccessKeyId;
37183722
string SecretAccesskey;
3723+
string Token;
3724+
string TokenExpire;
37193725

37203726

37213727
ifstream PF(passwd_file.c_str());
@@ -3761,9 +3767,30 @@ static int check_for_cos_format(void)
37613767
got_secret_key_line = 1;
37623768
continue;
37633769
}
3770+
3771+
found = line.find(str3);
3772+
if(found != string::npos){
3773+
first_pos = line.find_first_of("=");
3774+
Token = line.substr(first_pos + 1, string::npos);
3775+
got_token_line = 1;
3776+
continue;
3777+
}
3778+
3779+
found = line.find(str4);
3780+
if(found != string::npos){
3781+
first_pos = line.find_first_of("=");
3782+
TokenExpire = line.substr(first_pos + 1, string::npos);
3783+
got_token_expire_line = 1;
3784+
continue;
3785+
}
37643786
}
37653787
}
37663788

3789+
// token and token expire are optional
3790+
if (got_token_line && got_token_expire_line) {
3791+
S3fsCurl::SetToken(Token, TokenExpire);
3792+
}
3793+
37673794
if(got_access_key_id_line && got_secret_key_line){
37683795
if(!S3fsCurl::SetAccessKey(AccessKeyId.c_str(), SecretAccesskey.c_str())){
37693796
S3FS_PRN_EXIT("if one access key is specified, both keys need to be specified.");
@@ -4376,9 +4403,9 @@ static int my_fuse_opt_proc(void* data, const char* arg, int key, struct fuse_ar
43764403
passwd_file = strchr(arg, '=') + sizeof(char);
43774404
return 0;
43784405
}
4379-
if(0 == STR2NCMP(arg, "ram_role=")){
4406+
if(0 == STR2NCMP(arg, "cam_role=")){
43804407
const char* role = strchr(arg, '=') + sizeof(char);
4381-
S3fsCurl::SetRAMRole(role);
4408+
S3fsCurl::SetCAMRole(role);
43824409
return 0;
43834410
}
43844411
if(0 == STR2NCMP(arg, "public_bucket=")){
@@ -4725,7 +4752,7 @@ int main(int argc, char* argv[])
47254752
}
47264753
if(passwd_file.size() > 0 && S3fsCurl::IsSetAccessKeyId()){
47274754
S3FS_PRN_EXIT("specifying both passwd_file and the access keys options is invalid.");
4728-
exit(EXIT_FAILURE);
4755+
// exit(EXIT_FAILURE);
47294756
}
47304757
if(!S3fsCurl::IsPublicBucket()){
47314758
if(EXIT_SUCCESS != get_access_keys()){

src/s3fs_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ blkcnt_t get_blocks(off_t size)
783783
return size / 512 + 1;
784784
}
785785

786-
time_t cvtRAMExpireStringToTime(const char* s)
786+
time_t cvtCAMExpireStringToTime(const char* s)
787787
{
788788
struct tm tm;
789789
if(!s){

src/s3fs_util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ uid_t get_uid(headers_t& meta);
121121
gid_t get_gid(const char *s);
122122
gid_t get_gid(headers_t& meta);
123123
blkcnt_t get_blocks(off_t size);
124-
time_t cvtRAMExpireStringToTime(const char* s);
124+
time_t cvtCAMExpireStringToTime(const char* s);
125125
time_t get_lastmodified(const char* s);
126126
time_t get_lastmodified(headers_t& meta);
127127
bool is_need_check_obj_detail(headers_t& meta);

0 commit comments

Comments
 (0)