Skip to content

Commit

Permalink
[Event Hubs Client] Hide Shared Key Credential (Azure#16429)
Browse files Browse the repository at this point in the history
The focus of these changes is to hide the shared key credential. The
credential types will be retired when the concept is added to Azure.Core,
and the client constructors are converted over to the shared types.
  • Loading branch information
jsquire authored and annelo-msft committed Feb 17, 2021
1 parent 040e792 commit 029ea8d
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public EventProcessorClient(Azure.Storage.Blobs.BlobContainerClient checkpointSt
public EventProcessorClient(Azure.Storage.Blobs.BlobContainerClient checkpointStore, string consumerGroup, string connectionString, Azure.Messaging.EventHubs.EventProcessorClientOptions clientOptions) { }
public EventProcessorClient(Azure.Storage.Blobs.BlobContainerClient checkpointStore, string consumerGroup, string connectionString, string eventHubName) { }
public EventProcessorClient(Azure.Storage.Blobs.BlobContainerClient checkpointStore, string consumerGroup, string fullyQualifiedNamespace, string eventHubName, Azure.Core.TokenCredential credential, Azure.Messaging.EventHubs.EventProcessorClientOptions clientOptions = null) { }
public EventProcessorClient(Azure.Storage.Blobs.BlobContainerClient checkpointStore, string consumerGroup, string fullyQualifiedNamespace, string eventHubName, Azure.Messaging.EventHubs.EventHubsSharedAccessKeyCredential credential, Azure.Messaging.EventHubs.EventProcessorClientOptions clientOptions = null) { }
public EventProcessorClient(Azure.Storage.Blobs.BlobContainerClient checkpointStore, string consumerGroup, string connectionString, string eventHubName, Azure.Messaging.EventHubs.EventProcessorClientOptions clientOptions) { }
public new string ConsumerGroup { get { throw null; } }
public new string EventHubName { get { throw null; } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<ItemGroup>
<Compile Include="$(AzureCoreSharedSources)Argument.cs" Link="SharedSource\Azure.Core\Argument.cs" />
<Compile Include="$(AzureCoreSharedSources)TaskExtensions.cs" Link="SharedSource\Azure.Core\TaskExtensions.cs" />
<Compile Include="..\..\Azure.Messaging.EventHubs\src\Authorization\EventHubsSharedAccessKeyCredential.cs" Link="EventHubsSharedAccessKeyCredential.cs" />
</ItemGroup>

<!--Embed the shared resources -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,13 +421,14 @@ public EventProcessorClient(BlobContainerClient checkpointStore,
/// does not assume the ability to manage the storage account and is safe to run with only read/write permission for blobs in the container.
/// </remarks>
///
public EventProcessorClient(BlobContainerClient checkpointStore,
string consumerGroup,
string fullyQualifiedNamespace,
string eventHubName,
EventHubsSharedAccessKeyCredential credential,
EventProcessorClientOptions clientOptions = default) : base((clientOptions ?? DefaultClientOptions).CacheEventCount, consumerGroup, fullyQualifiedNamespace, eventHubName, credential, CreateOptions(clientOptions))
internal EventProcessorClient(BlobContainerClient checkpointStore,
string consumerGroup,
string fullyQualifiedNamespace,
string eventHubName,
EventHubsSharedAccessKeyCredential credential,
EventProcessorClientOptions clientOptions = default) : base((clientOptions ?? DefaultClientOptions).CacheEventCount, consumerGroup, fullyQualifiedNamespace, eventHubName, (TokenCredential)(object)credential, CreateOptions(clientOptions))
{
// TODO: Update the credential type and base class constructor invocation.
Argument.AssertNotNull(checkpointStore, nameof(checkpointStore));
StorageManager = CreateStorageManager(checkpointStore);
}
Expand Down Expand Up @@ -481,8 +482,9 @@ internal EventProcessorClient(StorageManager storageManager,
string eventHubName,
int cacheEventCount,
EventHubsSharedAccessKeyCredential credential,
EventProcessorOptions clientOptions) : base(cacheEventCount, consumerGroup, fullyQualifiedNamespace, eventHubName, credential, clientOptions)
EventProcessorOptions clientOptions) : base(cacheEventCount, consumerGroup, fullyQualifiedNamespace, eventHubName, (TokenCredential)(object)credential, clientOptions)
{
// TODO: Update the credential type and base class constructor invocation.
Argument.AssertNotNull(storageManager, nameof(storageManager));

DefaultStartingPosition = (clientOptions?.DefaultStartingPosition ?? DefaultStartingPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public async Task EventsCanBeReadByOneProcessorClientUsingAnIdentityCredential()
/// </summary>
///
[Test]
[Ignore("Waiting on Azure.Core shared key credential")]
public async Task EventsCanBeReadByOneProcessorClientUsingTheSharedKeyCredential()
{
// Setup the environment.
Expand Down Expand Up @@ -543,8 +544,9 @@ private EventProcessorClient CreateProcessorWithSharedAccessKey(string consumerG
StorageManager storageManager = default,
EventProcessorOptions options = default)
{
// TODO: Update the credential type and connection construction.
var credential = new EventHubsSharedAccessKeyCredential(EventHubsTestEnvironment.Instance.SharedAccessKeyName, EventHubsTestEnvironment.Instance.SharedAccessKey);
EventHubConnection createConnection() => new EventHubConnection(EventHubsTestEnvironment.Instance.FullyQualifiedNamespace, eventHubName, credential);
EventHubConnection createConnection() => null; //new EventHubConnection(EventHubsTestEnvironment.Instance.FullyQualifiedNamespace, eventHubName, credential);

storageManager ??= new InMemoryStorageManager(_=> {});
return new TestEventProcessorClient(storageManager, consumerGroup, EventHubsTestEnvironment.Instance.FullyQualifiedNamespace, eventHubName, credential, createConnection, options);
Expand Down Expand Up @@ -654,8 +656,9 @@ internal TestEventProcessorClient(StorageManager storageManager,
string eventHubName,
EventHubsSharedAccessKeyCredential credential,
Func<EventHubConnection> connectionFactory,
EventProcessorOptions options) : base(storageManager, consumerGroup, fullyQualifiedNamespace, eventHubName, 100, credential, options)
EventProcessorOptions options) : base(storageManager, consumerGroup, fullyQualifiedNamespace, eventHubName, 100, (TokenCredential)(object)credential, options)
{
// TODO: Update the credential type and base class constructor invocation.
InjectedConnectionFactory = connectionFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public void ConstructorsValidateTheConsumerGroup(string consumerGroup)
{
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), consumerGroup, "dummyConnection", new EventProcessorClientOptions()), Throws.InstanceOf<ArgumentException>(), "The connection string constructor should validate the consumer group.");
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), consumerGroup, "dummyNamespace", "dummyEventHub", Mock.Of<TokenCredential>(), new EventProcessorClientOptions()), Throws.InstanceOf<ArgumentException>(), "The token credential constructor should validate the consumer group.");
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), consumerGroup, "dummyNamespace", "dummyEventHub", new EventHubsSharedAccessKeyCredential("key", "value"), new EventProcessorClientOptions()), Throws.InstanceOf<ArgumentException>(), "The shared key credential constructor should validate the consumer group.");

// TODO: Update the credential type and uncomment.
//Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), consumerGroup, "dummyNamespace", "dummyEventHub", new EventHubsSharedAccessKeyCredential("key", "value"), new EventProcessorClientOptions()), Throws.InstanceOf<ArgumentException>(), "The shared key credential constructor should validate the consumer group.");
}

/// <summary>
Expand All @@ -53,7 +55,9 @@ public void ConstructorsValidateTheBlobContainerClient()

Assert.That(() => new EventProcessorClient(null, "consumerGroup", fakeConnection, new EventProcessorClientOptions()), Throws.InstanceOf<ArgumentNullException>(), "The connection string constructor should validate the blob container client.");
Assert.That(() => new EventProcessorClient(null, "consumerGroup", "dummyNamespace", "dummyEventHub", Mock.Of<TokenCredential>(), new EventProcessorClientOptions()), Throws.InstanceOf<ArgumentNullException>(), "The token credential constructor should validate the blob container client.");
Assert.That(() => new EventProcessorClient(null, "consumerGroup", "dummyNamespace", "dummyEventHub", new EventHubsSharedAccessKeyCredential("key", "value"), new EventProcessorClientOptions()), Throws.InstanceOf<ArgumentNullException>(), "The shared key credential constructor should validate the blob container client.");

// TODO: Update the credential type and uncomment.
//Assert.That(() => new EventProcessorClient(null, "consumerGroup", "dummyNamespace", "dummyEventHub", new EventHubsSharedAccessKeyCredential("key", "value"), new EventProcessorClientOptions()), Throws.InstanceOf<ArgumentNullException>(), "The shared key credential constructor should validate the blob container client.");
}

/// <summary>
Expand All @@ -80,7 +84,9 @@ public void ConstructorsValidateTheConnectionString(string connectionString)
public void ConstructorValidatesTheNamespace(string constructorArgument)
{
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, constructorArgument, "dummy", Mock.Of<TokenCredential>()), Throws.InstanceOf<ArgumentException>(), "The token credential should validate.");
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, constructorArgument, "dummy", new EventHubsSharedAccessKeyCredential("key", "value")), Throws.InstanceOf<ArgumentException>(), "The shared key credential should validate.");

// TODO: Update the credential type and uncomment.
//Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, constructorArgument, "dummy", new EventHubsSharedAccessKeyCredential("key", "value")), Throws.InstanceOf<ArgumentException>(), "The shared key credential should validate.");
}

/// <summary>
Expand All @@ -93,7 +99,9 @@ public void ConstructorValidatesTheNamespace(string constructorArgument)
public void ConstructorValidatesTheEventHub(string constructorArgument)
{
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, "namespace", constructorArgument, Mock.Of<TokenCredential>()), Throws.InstanceOf<ArgumentException>(), "The token credential should validate.");
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, "namespace", constructorArgument, new EventHubsSharedAccessKeyCredential("key", "value")), Throws.InstanceOf<ArgumentException>(), "The shared key credential should validate.");

// TODO: Update the credential type and uncomment.
//Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, "namespace", constructorArgument, new EventHubsSharedAccessKeyCredential("key", "value")), Throws.InstanceOf<ArgumentException>(), "The shared key credential should validate.");
}

/// <summary>
Expand All @@ -104,7 +112,9 @@ public void ConstructorValidatesTheEventHub(string constructorArgument)
public void ConstructorValidatesTheCredential()
{
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, "namespace", "hubName", default(TokenCredential)), Throws.ArgumentNullException, "The token credential should validate.");
Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, "namespace", "hubName", default(EventHubsSharedAccessKeyCredential)), Throws.ArgumentNullException, "The shared key credential should validate.");

// TODO: Update the credential type and uncomment.
//Assert.That(() => new EventProcessorClient(Mock.Of<BlobContainerClient>(), EventHubConsumerClient.DefaultConsumerGroupName, "namespace", "hubName", default(EventHubsSharedAccessKeyCredential)), Throws.ArgumentNullException, "The shared key credential should validate.");
}

/// <summary>
Expand Down
Loading

0 comments on commit 029ea8d

Please sign in to comment.