-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[Storage] SAS Credential in Storage. #17646
Merged
kasobol-msft
merged 32 commits into
Azure:master
from
kasobol-msft:sas-credential-in-storage
Jan 11, 2021
Merged
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
a4c5ebc
Add AzureSasCredential
kasobol-msft 5fdb953
corner case.
kasobol-msft bb719f2
Merge remote-tracking branch 'upstream/master' into sas-credential
kasobol-msft 61c3839
api.
kasobol-msft 2504f78
core as project ref (todo undo this)
kasobol-msft fa6536d
first client.
kasobol-msft 2bccb52
hack azure core in webjobs for now.
kasobol-msft bd20b54
api.
kasobol-msft fd86385
constructors.
kasobol-msft 4b38c1e
blob tests.
kasobol-msft 8becf87
datalake tests.
kasobol-msft e117feb
share tests + take out sas on share and service clients as there isn'…
kasobol-msft f35672e
queues tests.
kasobol-msft 454586c
well that works. nvm.
kasobol-msft 1740da6
remarks.
kasobol-msft d68dd4e
error message.
kasobol-msft 0f83c9b
predicate shouldn't be optional.
kasobol-msft 0aa98f0
merge master
kasobol-msft 01c538b
post-merge tweaks.
kasobol-msft 730a3b5
message about right UriBuilder.
kasobol-msft 31d9c96
added uri validation.
kasobol-msft 3389e39
changelog.
kasobol-msft 39d059b
user delegation sas change.
kasobol-msft 43dcaf6
batch.
kasobol-msft f8ab972
merge master.
kasobol-msft e33ca4c
this test doesn't work well in playback mode.
kasobol-msft f108a76
pr feedback.
kasobol-msft c35d5e2
comments.
kasobol-msft 5975ee9
validation.
kasobol-msft f68c029
Merge remote-tracking branch 'upstream/master' into sas-credential-in…
kasobol-msft 4deba35
undo project references workaround.
kasobol-msft 4b59f79
undo webjobs project ref workaround.
kasobol-msft 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.ComponentModel; | ||
using System.Threading; | ||
using Azure.Core; | ||
|
||
namespace Azure | ||
{ | ||
/// <summary> | ||
/// Shared access signature credential used to authenticate to an Azure Service. | ||
/// It provides the ability to update the signature without creating a new client. | ||
/// </summary> | ||
public class AzureSasCredential | ||
{ | ||
private string _signature; | ||
|
||
/// <summary> | ||
/// Signature used to authenticate to an Azure service. | ||
/// </summary> | ||
[EditorBrowsable(EditorBrowsableState.Never)] | ||
public string Signature | ||
{ | ||
get => Volatile.Read(ref _signature); | ||
private set => Volatile.Write(ref _signature, value); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AzureSasCredential"/> class. | ||
/// </summary> | ||
/// <param name="signature">Signature to use to authenticate with the Azure service.</param> | ||
/// <exception cref="System.ArgumentNullException"> | ||
/// Thrown when the <paramref name="signature"/> is null. | ||
/// </exception> | ||
/// <exception cref="System.ArgumentException"> | ||
/// Thrown when the <paramref name="signature"/> is empty. | ||
/// </exception> | ||
#pragma warning disable CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable. | ||
public AzureSasCredential(string signature) => Update(signature); | ||
#pragma warning restore CS8618 // Non-nullable field is uninitialized. Consider declaring as nullable. | ||
|
||
/// <summary> | ||
/// Updates the signature. | ||
/// This is intended to be used when you've regenerated your signature | ||
/// and want to update long lived clients. | ||
/// </summary> | ||
/// <param name="signature">Signature to authenticate the service against.</param> | ||
/// <exception cref="System.ArgumentNullException"> | ||
/// Thrown when the <paramref name="signature"/> is null. | ||
/// </exception> | ||
/// <exception cref="System.ArgumentException"> | ||
/// Thrown when the <paramref name="signature"/> is empty. | ||
/// </exception> | ||
public void Update(string signature) | ||
{ | ||
Argument.AssertNotNullOrEmpty(signature, nameof(signature)); | ||
Signature = signature; | ||
} | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
sdk/core/Azure.Core/src/Shared/AzureSasCredentialPolicy.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Azure.Core.Pipeline; | ||
|
||
namespace Azure.Core | ||
{ | ||
internal class AzureSasCredentialPolicy : HttpPipelineSynchronousPolicy | ||
{ | ||
private readonly AzureSasCredential _credential; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="AzureSasCredentialPolicy"/> class. | ||
/// </summary> | ||
/// <param name="credential">The <see cref="AzureSasCredentialPolicy"/> used to authenticate requests.</param> | ||
public AzureSasCredentialPolicy(AzureSasCredential credential) | ||
{ | ||
Argument.AssertNotNull(credential, nameof(credential)); | ||
_credential = credential; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void OnSendingRequest(HttpMessage message) | ||
{ | ||
base.OnSendingRequest(message); | ||
string query = message.Request.Uri.Query; | ||
string signature = _credential.Signature; | ||
if (signature.StartsWith("?", StringComparison.InvariantCulture)) | ||
{ | ||
signature = signature.Substring(1); | ||
} | ||
if (!query.Contains(signature)) | ||
{ | ||
query = string.IsNullOrEmpty(query) ? '?' + signature : query + '&' + signature; | ||
message.Request.Uri.Query = query; | ||
} | ||
} | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
sdk/core/Azure.Core/tests/AzureSasCredentialPolicyTests.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core.Pipeline; | ||
using Azure.Core.TestFramework; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Core.Tests | ||
{ | ||
public class AzureSasCredentialPolicyTests : PolicyTestBase | ||
{ | ||
[TestCase("sig=test_signature_value")] | ||
[TestCase("?sig=test_signature_value")] | ||
public async Task SetsSignatureEmptyQuery(string signatureValue) | ||
{ | ||
var transport = new MockTransport(new MockResponse(200)); | ||
var sasPolicy = new AzureSasCredentialPolicy(new AzureSasCredential(signatureValue)); | ||
|
||
await SendGetRequest(transport, sasPolicy); | ||
|
||
Assert.AreEqual("?sig=test_signature_value", transport.SingleRequest.Uri.Query); | ||
} | ||
|
||
[TestCase("sig=test_signature_value")] | ||
[TestCase("?sig=test_signature_value")] | ||
public async Task SetsSignatureNonEmptyQuery(string signatureValue) | ||
{ | ||
var transport = new MockTransport(new MockResponse(200)); | ||
var sasPolicy = new AzureSasCredentialPolicy(new AzureSasCredential(signatureValue)); | ||
string query = "?foo=bar"; | ||
|
||
await SendGetRequest(transport, sasPolicy, query: query); | ||
|
||
Assert.AreEqual($"?foo=bar&sig=test_signature_value", transport.SingleRequest.Uri.Query); | ||
} | ||
|
||
[TestCase("sig=test_signature_value")] | ||
[TestCase("?sig=test_signature_value")] | ||
public async Task VerifyRetryEmptyQuery(string signatureValue) | ||
{ | ||
var transport = new MockTransport(new MockResponse(200), new MockResponse(200)); | ||
var sasPolicy = new AzureSasCredentialPolicy(new AzureSasCredential(signatureValue)); | ||
|
||
using (Request request = transport.CreateRequest()) | ||
{ | ||
request.Method = RequestMethod.Get; | ||
var pipeline = new HttpPipeline(transport, new[] { sasPolicy }); | ||
await pipeline.SendRequestAsync(request, CancellationToken.None); | ||
await pipeline.SendRequestAsync(request, CancellationToken.None); | ||
} | ||
|
||
Assert.AreEqual("?sig=test_signature_value", transport.Requests[0].Uri.Query); | ||
} | ||
|
||
[TestCase("sig=test_signature_value")] | ||
[TestCase("?sig=test_signature_value")] | ||
public async Task VerifyRetryNonEmptyQuery(string signatureValue) | ||
{ | ||
var transport = new MockTransport(new MockResponse(200), new MockResponse(200)); | ||
var sasPolicy = new AzureSasCredentialPolicy(new AzureSasCredential(signatureValue)); | ||
string query = "?foo=bar"; | ||
|
||
using (Request request = transport.CreateRequest()) | ||
{ | ||
request.Method = RequestMethod.Get; | ||
request.Uri.Query = query; | ||
var pipeline = new HttpPipeline(transport, new[] { sasPolicy }); | ||
await pipeline.SendRequestAsync(request, CancellationToken.None); | ||
await pipeline.SendRequestAsync(request, CancellationToken.None); | ||
} | ||
|
||
Assert.AreEqual("?foo=bar&sig=test_signature_value", transport.Requests[0].Uri.Query); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a blurb to all these doc comments telling people to only use this overload if they need to roll SAS signatures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added remarks.