Skip to content

Commit 61dd33c

Browse files
committed
CPL AWS: Fix aws sso cache file location and region parameter (Fixes OSGeo#12064)
Needed to wire through the profile name from the config, then use that for the file name hash rather than the start url if it exists. This makes it match the way boto3 (the aws python library) does it. Also pulled the region out of the cache file and used that when building the token url as the old way only worked if your AWS account was based in US-east-1
1 parent 926d626 commit 61dd33c

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

autotest/gcore/vsis3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6238,7 +6238,7 @@ def test_vsis3_read_credentials_sso(tmp_vsimem, aws_test_config, webserver_port)
62386238
)
62396239

62406240
gdal.FileFromMemBuffer(
6241-
tmp_vsimem / "sso" / "cache" / "327c3fda87ce286848a574982ddd0b7c7487f816.json",
6241+
tmp_vsimem / "sso" / "cache" / "0ad374308c5a4e22f723adf10145eafad7c4031c.json",
62426242
'{"startUrl": "https://example.com", "region": "us-east-1", "accessToken": "sso-accessToken", "expiresAt": "9999-01-01T00:00:00Z"}',
62436243
)
62446244

port/cpl_aws.cpp

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,8 @@ bool VSIS3HandleHelper::GetConfigurationFromAWSConfigFiles(
11871187
std::string &osSourceProfile, std::string &osExternalId,
11881188
std::string &osMFASerial, std::string &osRoleSessionName,
11891189
std::string &osWebIdentityTokenFile, std::string &osSSOStartURL,
1190-
std::string &osSSOAccountID, std::string &osSSORoleName)
1190+
std::string &osSSOAccountID, std::string &osSSORoleName,
1191+
std::string &osSSOSession)
11911192
{
11921193
// See http://docs.aws.amazon.com/cli/latest/userguide/cli-config-files.html
11931194
// If AWS_DEFAULT_PROFILE is set (obsolete, no longer documented), use it in
@@ -1247,7 +1248,6 @@ bool VSIS3HandleHelper::GetConfigurationFromAWSConfigFiles(
12471248
const char *pszLine;
12481249
std::map<std::string, std::map<std::string, std::string>>
12491250
oMapSSOSessions;
1250-
std::string osSSOSession;
12511251
while ((pszLine = CPLReadLineL(fp)) != nullptr)
12521252
{
12531253
if (STARTS_WITH(pszLine, "[sso-session ") &&
@@ -1516,13 +1516,11 @@ static bool GetTemporaryCredentialsForRole(
15161516
/************************************************************************/
15171517

15181518
// Issue a GetRoleCredentials request
1519-
static bool GetTemporaryCredentialsForSSO(const std::string &osSSOStartURL,
1520-
const std::string &osSSOAccountID,
1521-
const std::string &osSSORoleName,
1522-
std::string &osTempSecretAccessKey,
1523-
std::string &osTempAccessKeyId,
1524-
std::string &osTempSessionToken,
1525-
std::string &osExpirationEpochInMS)
1519+
static bool GetTemporaryCredentialsForSSO(
1520+
const std::string &osSSOStartURL, const std::string &osSSOSession,
1521+
const std::string &osSSOAccountID, const std::string &osSSORoleName,
1522+
std::string &osTempSecretAccessKey, std::string &osTempAccessKeyId,
1523+
std::string &osTempSessionToken, std::string &osExpirationEpochInMS)
15261524
{
15271525
std::string osSSOFilename = GetAWSRootDirectory();
15281526
osSSOFilename += GetDirSeparator();
@@ -1531,8 +1529,14 @@ static bool GetTemporaryCredentialsForSSO(const std::string &osSSOStartURL,
15311529
osSSOFilename += "cache";
15321530
osSSOFilename += GetDirSeparator();
15331531

1532+
std::string hashValue = osSSOStartURL;
1533+
if (!osSSOSession.empty())
1534+
{
1535+
hashValue = osSSOSession;
1536+
}
1537+
15341538
GByte hash[CPL_SHA1_HASH_SIZE];
1535-
CPL_SHA1(osSSOStartURL.data(), osSSOStartURL.size(), hash);
1539+
CPL_SHA1(hashValue.data(), hashValue.size(), hash);
15361540
osSSOFilename += CPLGetLowerCaseHex(hash, sizeof(hash));
15371541
osSSOFilename += ".json";
15381542

@@ -1587,9 +1591,13 @@ static bool GetTemporaryCredentialsForSSO(const std::string &osSSOStartURL,
15871591
headers += "x-amz-sso_bearer_token: " + osAccessToken;
15881592
aosOptions.AddNameValue("HEADERS", headers.c_str());
15891593

1594+
const std::string osRegion = oRoot.GetString("region", "us-east-1");
1595+
const std::string osDefaultHost("portal.sso." + osRegion +
1596+
".amazonaws.com");
1597+
15901598
const bool bUseHTTPS = CPLTestBool(CPLGetConfigOption("AWS_HTTPS", "YES"));
1591-
const std::string osHost(CPLGetConfigOption(
1592-
"CPL_AWS_SSO_ENDPOINT", "portal.sso.us-east-1.amazonaws.com"));
1599+
const std::string osHost(
1600+
CPLGetConfigOption("CPL_AWS_SSO_ENDPOINT", osDefaultHost.c_str()));
15931601

15941602
const std::string osURL = (bUseHTTPS ? "https://" : "http://") + osHost +
15951603
osResourceAndQueryString;
@@ -1721,7 +1729,7 @@ bool VSIS3HandleHelper::GetOrRefreshTemporaryCredentialsForSSO(
17211729
gosGlobalAccessKeyId.clear();
17221730
gosGlobalSessionToken.clear();
17231731
if (GetTemporaryCredentialsForSSO(
1724-
gosSSOStartURL, gosSSOAccountID, gosSSORoleName,
1732+
gosSSOStartURL, "", gosSSOAccountID, gosSSORoleName,
17251733
gosGlobalSecretAccessKey, gosGlobalAccessKeyId,
17261734
gosGlobalSessionToken, osExpirationEpochInMS))
17271735
{
@@ -1824,14 +1832,15 @@ bool VSIS3HandleHelper::GetConfiguration(
18241832
std::string osSSOStartURL;
18251833
std::string osSSOAccountID;
18261834
std::string osSSORoleName;
1835+
std::string osSSOSession;
18271836
// coverity[tainted_data]
18281837
if (GetConfigurationFromAWSConfigFiles(
18291838
osPathForOption,
18301839
/* pszProfile = */ nullptr, osSecretAccessKey, osAccessKeyId,
18311840
osSessionToken, osRegion, osCredentials, osRoleArn, osSourceProfile,
18321841
osExternalId, osMFASerial, osRoleSessionName,
18331842
osWebIdentityTokenFile, osSSOStartURL, osSSOAccountID,
1834-
osSSORoleName))
1843+
osSSORoleName, osSSOSession))
18351844
{
18361845
if (osSecretAccessKey.empty() && !osRoleArn.empty())
18371846
{
@@ -1858,7 +1867,8 @@ bool VSIS3HandleHelper::GetConfiguration(
18581867
osRegionSP, osCredentialsSP, osRoleArnSP,
18591868
osSourceProfileSP, osExternalIdSP, osMFASerialSP,
18601869
osRoleSessionNameSP, osWebIdentityTokenFile,
1861-
osSSOStartURLSP, osSSOAccountIDSP, osSSORoleNameSP))
1870+
osSSOStartURLSP, osSSOAccountIDSP, osSSORoleNameSP,
1871+
osSSOSession))
18621872
{
18631873
if (GetConfigurationFromAssumeRoleWithWebIdentity(
18641874
/* bForceRefresh = */ false, osPathForOption,
@@ -1929,14 +1939,14 @@ bool VSIS3HandleHelper::GetConfiguration(
19291939
return false;
19301940
}
19311941

1932-
if (!osSSOStartURL.empty())
1942+
if (!osSSOStartURL.empty() || !osSSOSession.empty())
19331943
{
19341944
std::string osTempSecretAccessKey;
19351945
std::string osTempAccessKeyId;
19361946
std::string osTempSessionToken;
19371947
std::string osExpirationEpochInMS;
19381948
if (GetTemporaryCredentialsForSSO(
1939-
osSSOStartURL, osSSOAccountID, osSSORoleName,
1949+
osSSOStartURL, osSSOSession, osSSOAccountID, osSSORoleName,
19401950
osTempSecretAccessKey, osTempAccessKeyId,
19411951
osTempSessionToken, osExpirationEpochInMS))
19421952
{

port/cpl_aws.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ class VSIS3HandleHelper final : public IVSIS3LikeHandleHelper
165165
std::string &osSourceProfile, std::string &osExternalId,
166166
std::string &osMFASerial, std::string &osRoleSessionName,
167167
std::string &osWebIdentityTokenFile, std::string &osSSOStartURL,
168-
std::string &osSSOAccountID, std::string &osSSORoleName);
168+
std::string &osSSOAccountID, std::string &osSSORoleName,
169+
std::string &osSSOSession);
169170

170171
static bool GetConfiguration(const std::string &osPathForOption,
171172
CSLConstList papszOptions,

0 commit comments

Comments
 (0)