forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding SAS Credential Support to the Event Grid SDK (Azure#13277)
* Added SAS credential support and SAS token generation method in client * Modified test sanitizer * Added additional documentation
- Loading branch information
Showing
17 changed files
with
425 additions
and
168 deletions.
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
28 changes: 28 additions & 0 deletions
28
sdk/eventgrid/Azure.Messaging.EventGrid/src/Customization/SharedAccessSignatureCredential.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,28 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Azure.Messaging.EventGrid | ||
{ | ||
/// <summary> | ||
/// SAS token used to authenticate to the Event Grid service. | ||
/// </summary> | ||
public class SharedAccessSignatureCredential | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SharedAccessSignatureCredential"/> class. | ||
/// </summary> | ||
/// <param name="signature">SAS token used for authentication</param> | ||
public SharedAccessSignatureCredential(string signature) | ||
{ | ||
Signature = signature; | ||
} | ||
/// <summary> | ||
/// SAS token used to authenticate to the Event Grid service. | ||
/// </summary> | ||
public string Signature { get; } | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...grid/Azure.Messaging.EventGrid/src/Customization/SharedAccessSignatureCredentialPolicy.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,33 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Azure.Core; | ||
using Azure.Core.Pipeline; | ||
|
||
namespace Azure.Messaging.EventGrid | ||
{ | ||
internal class SharedAccessSignatureCredentialPolicy : HttpPipelineSynchronousPolicy | ||
{ | ||
private readonly SharedAccessSignatureCredential _credential; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="SharedAccessSignatureCredentialPolicy"/> class. | ||
/// </summary> | ||
/// <param name="credential">The <see cref="SharedAccessSignatureCredential"/> used to authenticate requests.</param> | ||
public SharedAccessSignatureCredentialPolicy(SharedAccessSignatureCredential credential) | ||
{ | ||
Argument.AssertNotNull(credential, nameof(credential)); | ||
_credential = credential; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void OnSendingRequest(HttpMessage message) | ||
{ | ||
base.OnSendingRequest(message); | ||
message.Request.Headers.SetValue(Constants.SasTokenName, _credential.Signature); | ||
} | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
sdk/eventgrid/Azure.Messaging.EventGrid/tests/EventGridClientLiveTests.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,155 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Azure.Core.TestFramework; | ||
using Azure.Messaging.EventGrid; | ||
using Azure.Messaging.EventGrid.Models; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Messaging.EventGrid.Tests | ||
{ | ||
public class EventGridClientLiveTests : EventGridLiveTestBase | ||
{ | ||
public EventGridClientLiveTests(bool async) | ||
: base(async) | ||
{ | ||
} | ||
|
||
[Test] | ||
public async Task CanPublishEvent() | ||
{ | ||
EventGridClientOptions options = Recording.InstrumentClientOptions(new EventGridClientOptions()); | ||
EventGridClient client = InstrumentClient( | ||
new EventGridClient( | ||
new Uri(TestEnvironment.TopicHost), | ||
new AzureKeyCredential(TestEnvironment.TopicKey), | ||
options)); | ||
await client.PublishEventsAsync(GetEventsList()); | ||
} | ||
|
||
[Test] | ||
public async Task CanPublishEventToDomain() | ||
{ | ||
EventGridClientOptions options = Recording.InstrumentClientOptions(new EventGridClientOptions()); | ||
EventGridClient client = InstrumentClient( | ||
new EventGridClient( | ||
new Uri(TestEnvironment.DomainHost), | ||
new AzureKeyCredential(TestEnvironment.DomainKey), | ||
options)); | ||
await client.PublishEventsAsync(GetEventsWithTopicsList()); | ||
|
||
} | ||
|
||
[Test] | ||
public async Task CanPublishCloudEvent() | ||
{ | ||
EventGridClientOptions options = Recording.InstrumentClientOptions(new EventGridClientOptions()); | ||
EventGridClient client = InstrumentClient( | ||
new EventGridClient( | ||
new Uri(TestEnvironment.CloudEventTopicHost), | ||
new AzureKeyCredential(TestEnvironment.CloudEventTopicKey), | ||
options)); | ||
await client.PublishCloudEventsAsync(GetCloudEventsList()); | ||
} | ||
|
||
[Test] | ||
public async Task CanPublishEventUsingSAS() | ||
{ | ||
EventGridClient client = new EventGridClient( | ||
new Uri(TestEnvironment.TopicHost), | ||
new AzureKeyCredential(TestEnvironment.TopicKey)); | ||
|
||
string sasToken = client.BuildSharedAccessSignature(DateTimeOffset.UtcNow.AddMinutes(60)); | ||
|
||
EventGridClient sasTokenClient = InstrumentClient( | ||
new EventGridClient( | ||
new Uri(TestEnvironment.TopicHost), | ||
new SharedAccessSignatureCredential(sasToken), | ||
Recording.InstrumentClientOptions(new EventGridClientOptions()))); | ||
await sasTokenClient.PublishEventsAsync(GetEventsList()); | ||
} | ||
|
||
private IList<EventGridEvent> GetEventsList() | ||
{ | ||
List<EventGridEvent> eventsList = new List<EventGridEvent>(); | ||
|
||
for (int i = 0; i < 10; i++) | ||
{ | ||
eventsList.Add( | ||
new EventGridEvent( | ||
Recording.Random.NewGuid().ToString(), | ||
$"Subject-{i}", | ||
"hello", | ||
"Microsoft.MockPublisher.TestEvent", | ||
Recording.Now, | ||
"1.0")); | ||
} | ||
|
||
return eventsList; | ||
} | ||
|
||
private IList<EventGridEvent> GetEventsWithTopicsList() | ||
{ | ||
List<EventGridEvent> eventsList = new List<EventGridEvent>(); | ||
|
||
for (int i = 0; i < 10; i++) | ||
{ | ||
eventsList.Add( | ||
new EventGridEvent( | ||
Recording.Random.NewGuid().ToString(), | ||
$"Topic-{i}", | ||
$"Subject-{i}", | ||
"hello", | ||
"Microsoft.MockPublisher.TestEvent", | ||
Recording.Now, | ||
"1", | ||
"1.0")); | ||
} | ||
|
||
return eventsList; | ||
} | ||
|
||
private IList<CloudEvent> GetCloudEventsList() | ||
{ | ||
List<CloudEvent> eventsList = new List<CloudEvent>(); | ||
|
||
for (int i = 0; i < 10; i++) | ||
{ | ||
eventsList.Add( | ||
new CloudEvent( | ||
Recording.Random.NewGuid().ToString(), | ||
$"Subject-{i}", | ||
"record", | ||
"1.0")); | ||
} | ||
|
||
return eventsList; | ||
} | ||
|
||
private IList<object> GetCustomEventsList() | ||
{ | ||
List<object> eventsList = new List<object>(); | ||
|
||
for (int i = 0; i < 10; i++) | ||
{ | ||
eventsList.Add(new TestEvent() | ||
{ | ||
dataVersion = "1.0", | ||
eventTime = Recording.Now, | ||
eventType = "Microsoft.MockPublisher.TestEvent", | ||
id = Recording.Random.NewGuid().ToString(), | ||
subject = $"Subject-{i}", | ||
topic = $"Topic-{i}" | ||
}); | ||
} | ||
|
||
return eventsList; | ||
} | ||
} | ||
} |
Oops, something went wrong.