-
Notifications
You must be signed in to change notification settings - Fork 3.7k
GH-44308: [C++][FS][Azure] Implement SAS token authentication #45021
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
Merged
kou
merged 22 commits into
apache:main
from
Tom-Newton:tomnewton/implement_sas_auth_on_azure/GH-44308
Dec 17, 2024
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1abba8e
Working with ConfigureSasCredential
Tom-Newton ea091b4
Real access test working against azurite
Tom-Newton 6df79f7
Fix creating getting Azure env in test
Tom-Newton 567488b
Use credential_kind_ to detect credential type inside GenerateSASToke…
Tom-Newton 6869067
Use original sas token inside `CopyFile` instead of generating a new …
Tom-Newton 0ffd832
Fix appending SAS token twice - all tests pass
Tom-Newton a2b826c
Support SAS token in AzureOptions::FromUri
Tom-Newton 9c6ec0e
Update SasCredential test to cover extra query parmaters
Tom-Newton f4177be
Adjust comment and increase SAS expiry time
Tom-Newton d19e87f
Update comments
Tom-Newton 15fe2c6
Autoformat
Tom-Newton b57ebea
PR comments
Tom-Newton e200b8e
Avoid generating SAS tokens on the fly.
Tom-Newton b0fd1d3
Add an extra test copying to a different container
Tom-Newton 744e111
Update comment
Tom-Newton c8fd94d
Remove AzureOptions::GenerateSASToken
Tom-Newton 83a0b40
Remove debug change
Tom-Newton d60742b
Rename Sas -> SAS
Tom-Newton 63f9773
Add a comment about the polling interval
Tom-Newton d55510a
Only allow query parameters we know can be part of a sas token
Tom-Newton 7576c4c
More renaming Sas -> SAS
Tom-Newton eece05e
Give up on `contexpr`. Make it a `static const std::set<std::string>`
Tom-Newton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -690,6 +690,36 @@ class TestAzureOptions : public ::testing::Test { | |
ASSERT_EQ(options.credential_kind_, AzureOptions::CredentialKind::kEnvironment); | ||
} | ||
|
||
void TestFromUriCredentialSASToken() { | ||
const std::string sas_token = | ||
"?se=2024-12-12T18:57:47Z&sig=pAs7qEBdI6sjUhqX1nrhNAKsTY%2B1SqLxPK%" | ||
"2BbAxLiopw%3D&sp=racwdxylti&spr=https,http&sr=c&sv=2024-08-04"; | ||
ASSERT_OK_AND_ASSIGN( | ||
auto options, | ||
AzureOptions::FromUri( | ||
"abfs://[email protected]/" + sas_token, nullptr)); | ||
ASSERT_EQ(options.credential_kind_, AzureOptions::CredentialKind::kSASToken); | ||
ASSERT_EQ(options.sas_token_, sas_token); | ||
} | ||
|
||
void TestFromUriCredentialSASTokenWithOtherParameters() { | ||
const std::string uri_query_string = | ||
"?enable_tls=false&se=2024-12-12T18:57:47Z&sig=pAs7qEBdI6sjUhqX1nrhNAKsTY%" | ||
"2B1SqLxPK%" | ||
"2BbAxLiopw%3D&sp=racwdxylti&spr=https,http&sr=c&sv=2024-08-04"; | ||
ASSERT_OK_AND_ASSIGN( | ||
auto options, | ||
AzureOptions::FromUri( | ||
"abfs://[email protected]:10000/container/dir/blob" + uri_query_string, | ||
nullptr)); | ||
ASSERT_EQ(options.credential_kind_, AzureOptions::CredentialKind::kSASToken); | ||
ASSERT_EQ(options.sas_token_, uri_query_string); | ||
ASSERT_EQ(options.blob_storage_authority, "127.0.0.1:10000"); | ||
ASSERT_EQ(options.dfs_storage_authority, "127.0.0.1:10000"); | ||
ASSERT_EQ(options.blob_storage_scheme, "http"); | ||
ASSERT_EQ(options.dfs_storage_scheme, "http"); | ||
} | ||
|
||
void TestFromUriCredentialInvalid() { | ||
ASSERT_RAISES(Invalid, AzureOptions::FromUri( | ||
"abfs://[email protected]/dir/file?" | ||
|
@@ -777,6 +807,10 @@ TEST_F(TestAzureOptions, FromUriCredentialWorkloadIdentity) { | |
TEST_F(TestAzureOptions, FromUriCredentialEnvironment) { | ||
TestFromUriCredentialEnvironment(); | ||
} | ||
TEST_F(TestAzureOptions, FromUriCredentialSASToken) { TestFromUriCredentialSASToken(); } | ||
TEST_F(TestAzureOptions, FromUriCredentialSASTokenWithOtherParameters) { | ||
TestFromUriCredentialSASTokenWithOtherParameters(); | ||
} | ||
TEST_F(TestAzureOptions, FromUriCredentialInvalid) { TestFromUriCredentialInvalid(); } | ||
TEST_F(TestAzureOptions, FromUriBlobStorageAuthority) { | ||
TestFromUriBlobStorageAuthority(); | ||
|
@@ -912,6 +946,20 @@ class TestAzureFileSystem : public ::testing::Test { | |
.Value; | ||
} | ||
|
||
Result<std::string> GetContainerSASToken( | ||
const std::string& container_name, | ||
Azure::Storage::StorageSharedKeyCredential storage_shared_key_credential) { | ||
std::string sas_token; | ||
Azure::Storage::Sas::BlobSasBuilder builder; | ||
std::chrono::seconds available_period(60); | ||
builder.ExpiresOn = std::chrono::system_clock::now() + available_period; | ||
builder.BlobContainerName = container_name; | ||
builder.Resource = Azure::Storage::Sas::BlobSasResource::BlobContainer; | ||
builder.SetPermissions(Azure::Storage::Sas::BlobContainerSasPermissions::All); | ||
builder.Protocol = Azure::Storage::Sas::SasProtocol::HttpsAndHttp; | ||
return builder.GenerateSasToken(storage_shared_key_credential); | ||
} | ||
|
||
void UploadLines(const std::vector<std::string>& lines, const std::string& path, | ||
int total_size) { | ||
ASSERT_OK_AND_ASSIGN(auto output, fs()->OpenOutputStream(path, {})); | ||
|
@@ -1617,6 +1665,31 @@ class TestAzureFileSystem : public ::testing::Test { | |
AssertObjectContents(fs.get(), path, payload); | ||
} | ||
|
||
void TestSASCredential() { | ||
auto data = SetUpPreexistingData(); | ||
|
||
ASSERT_OK_AND_ASSIGN(auto env, GetAzureEnv()); | ||
ASSERT_OK_AND_ASSIGN(auto options, MakeOptions(env)); | ||
ASSERT_OK_AND_ASSIGN( | ||
auto sas_token, | ||
GetContainerSASToken(data.container_name, | ||
Azure::Storage::StorageSharedKeyCredential( | ||
env->account_name(), env->account_key()))); | ||
// AzureOptions::FromUri will not cut off extra query parameters that it consumes, so | ||
// make sure these don't cause problems. | ||
ARROW_EXPECT_OK(options.ConfigureSASCredential( | ||
"?blob_storage_authority=dummy_value0&" + sas_token.substr(1) + | ||
"&credential_kind=dummy-value1")); | ||
EXPECT_OK_AND_ASSIGN(auto fs, AzureFileSystem::Make(options)); | ||
|
||
AssertFileInfo(fs.get(), data.ObjectPath(), FileType::File); | ||
|
||
// Test CopyFile because the most obvious implementation requires generating a SAS | ||
// token at runtime which doesn't work when the original auth is SAS token. | ||
ASSERT_OK(fs->CopyFile(data.ObjectPath(), data.ObjectPath() + "_copy")); | ||
AssertFileInfo(fs.get(), data.ObjectPath() + "_copy", FileType::File); | ||
} | ||
|
||
private: | ||
using StringMatcher = | ||
::testing::PolymorphicMatcher<::testing::internal::HasSubstrMatcher<std::string>>; | ||
|
@@ -2328,6 +2401,10 @@ TYPED_TEST(TestAzureFileSystemOnAllScenarios, CreateContainerFromPath) { | |
|
||
TYPED_TEST(TestAzureFileSystemOnAllScenarios, MovePath) { this->TestMovePath(); } | ||
|
||
TYPED_TEST(TestAzureFileSystemOnAllScenarios, SASCredential) { | ||
this->TestSASCredential(); | ||
} | ||
|
||
// Tests using Azurite (the local Azure emulator) | ||
|
||
TEST_F(TestAzuriteFileSystem, CheckIfHierarchicalNamespaceIsEnabledRuntimeError) { | ||
|
@@ -2634,6 +2711,17 @@ TEST_F(TestAzuriteFileSystem, CopyFileSuccessDestinationNonexistent) { | |
EXPECT_EQ(PreexistingData::kLoremIpsum, buffer->ToString()); | ||
} | ||
|
||
TEST_F(TestAzuriteFileSystem, CopyFileSuccessDestinationDifferentContainer) { | ||
auto data = SetUpPreexistingData(); | ||
auto data2 = SetUpPreexistingData(); | ||
const auto destination_path = data2.ContainerPath("copy-destionation"); | ||
ASSERT_OK(fs()->CopyFile(data.ObjectPath(), destination_path)); | ||
ASSERT_OK_AND_ASSIGN(auto info, fs()->GetFileInfo(destination_path)); | ||
ASSERT_OK_AND_ASSIGN(auto stream, fs()->OpenInputStream(info)); | ||
ASSERT_OK_AND_ASSIGN(auto buffer, stream->Read(1024)); | ||
EXPECT_EQ(PreexistingData::kLoremIpsum, buffer->ToString()); | ||
} | ||
|
||
TEST_F(TestAzuriteFileSystem, CopyFileSuccessDestinationSame) { | ||
auto data = SetUpPreexistingData(); | ||
ASSERT_OK(fs()->CopyFile(data.ObjectPath(), data.ObjectPath())); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.