Skip to content

Commit

Permalink
Merge pull request #6889 from OSGeo/backport-6879-to-release/3.6
Browse files Browse the repository at this point in the history
[Backport release/3.6] /vsiaz/: accept Azure connection string with only BlobEndpoint and SharedAccessSignature (fixes #6870)
  • Loading branch information
rouault committed Dec 10, 2022
2 parents b977d55 + 77b4397 commit f2105d6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
51 changes: 51 additions & 0 deletions autotest/gcore/vsiaz.py
Expand Up @@ -1127,6 +1127,57 @@ def test_vsiaz_fake_test_BlobEndpointInConnectionString():
)


###############################################################################


def test_vsiaz_fake_test_SharedAccessSignatureInConnectionString():

if gdaltest.webserver_port == 0:
pytest.skip()

with gdaltest.config_option(
"AZURE_STORAGE_CONNECTION_STRING",
"BlobEndpoint=http://127.0.0.1:%d/myaccount;SharedAccessSignature=sp=rl&st=2022-12-06T20:41:17Z&se=2022-12-07T04:41:17Z&spr=https&sv=2021-06-08&sr=c&sig=xxxxxxxx"
% gdaltest.webserver_port,
):

signed_url = gdal.GetSignedURL("/vsiaz/az_fake_bucket/resource")
assert (
signed_url
== "http://127.0.0.1:%d/myaccount/az_fake_bucket/resource?sp=rl&st=2022-12-06T20:41:17Z&se=2022-12-07T04:41:17Z&spr=https&sv=2021-06-08&sr=c&sig=xxxxxxxx"
% gdaltest.webserver_port
)

def method(request):

request.protocol_version = "HTTP/1.1"
h = request.headers
if "Authorization" in h:
sys.stderr.write("Bad headers: %s\n" % str(h))
request.send_response(403)
return
request.send_response(200)
request.send_header("Content-type", "text/plain")
request.send_header("Content-Length", 3)
request.send_header("Connection", "close")
request.end_headers()
request.wfile.write("""foo""".encode("ascii"))

handler = webserver.SequentialHandler()
handler.add(
"GET",
"/myaccount/az_fake_bucket/resource?sp=rl&st=2022-12-06T20:41:17Z&se=2022-12-07T04:41:17Z&spr=https&sv=2021-06-08&sr=c&sig=xxxxxxxx",
custom_method=method,
)
with webserver.install_http_handler(handler):
f = open_for_read("/vsiaz_streaming/az_fake_bucket/resource")
assert f is not None
data = gdal.VSIFReadL(1, 4, f).decode("ascii")
gdal.VSIFCloseL(f)

assert data == "foo"


###############################################################################
# Test rename

Expand Down
39 changes: 29 additions & 10 deletions port/cpl_azure.cpp
Expand Up @@ -346,28 +346,45 @@ static bool ParseStorageConnectionString(const std::string& osStorageConnectionS
bool& bUseHTTPS,
CPLString& osEndpoint,
CPLString& osStorageAccount,
CPLString& osStorageKey)
CPLString& osStorageKey,
CPLString& osSAS)
{
osStorageAccount = AzureCSGetParameter(osStorageConnectionString,
"AccountName", true);
"AccountName", false);
osStorageKey = AzureCSGetParameter(osStorageConnectionString,
"AccountKey", true);
if( osStorageAccount.empty() || osStorageKey.empty() )
return false;
"AccountKey", false);

CPLString osProtocol(AzureCSGetParameter(
const CPLString osProtocol(AzureCSGetParameter(
osStorageConnectionString, "DefaultEndpointsProtocol", false));
bUseHTTPS = (osProtocol != "http");

CPLString osBlobEndpoint = AzureCSGetParameter(
if( osStorageAccount.empty() || osStorageKey.empty() )
{
osStorageAccount.clear();
osStorageKey.clear();

const CPLString osBlobEndpoint = AzureCSGetParameter(
osStorageConnectionString, "BlobEndpoint", false);
osSAS = AzureCSGetParameter(
osStorageConnectionString, "SharedAccessSignature", false);
if( !osBlobEndpoint.empty() && !osSAS.empty() )
{
osEndpoint = osBlobEndpoint;
return true;
}

return false;
}

const CPLString osBlobEndpoint = AzureCSGetParameter(
osStorageConnectionString, "BlobEndpoint", false);
if( !osBlobEndpoint.empty() )
{
osEndpoint = osBlobEndpoint;
}
else
{
CPLString osEndpointSuffix(AzureCSGetParameter(
const CPLString osEndpointSuffix(AzureCSGetParameter(
osStorageConnectionString, "EndpointSuffix", false));
if( !osEndpointSuffix.empty() )
osEndpoint = (bUseHTTPS ? "https://" : "http://") + osStorageAccount + "." + osServicePrefix + "." + osEndpointSuffix;
Expand Down Expand Up @@ -470,7 +487,8 @@ static bool GetConfigurationFromCLIConfigFile(const std::string& osServicePrefix
bUseHTTPS,
osEndpoint,
osStorageAccount,
osStorageKey);
osStorageKey,
osSAS);
}

if( osStorageAccount.empty() )
Expand Down Expand Up @@ -542,7 +560,8 @@ bool VSIAzureBlobHandleHelper::GetConfiguration(const std::string& osPathForOpti
bUseHTTPS,
osEndpoint,
osStorageAccount,
osStorageKey);
osStorageKey,
osSAS);
}
else
{
Expand Down

0 comments on commit f2105d6

Please sign in to comment.