Skip to content

Commit 8fe0961

Browse files
committed
Add support for defining a proxy to use for Event Hub communications routing.
1 parent 52d35e8 commit 8fe0961

4 files changed

Lines changed: 67 additions & 13 deletions

File tree

src/VirtualClient/VirtualClient.Contracts/Constants.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,19 @@ public static class EnvironmentVariable
171171
public const string USERPROFILE = nameof(USERPROFILE);
172172

173173
/// <summary>
174-
/// Name = VC_PASSWORD
174+
/// Name = VC_EVENT_HUB_PROXY
175175
/// </summary>
176-
public const string VC_PASSWORD = nameof(VC_PASSWORD);
176+
public const string VC_EVENT_HUB_PROXY = nameof(VC_EVENT_HUB_PROXY);
177+
178+
/// <summary>
179+
/// Name = VC_EVENT_HUB_PROXY_USERNAME
180+
/// </summary>
181+
public const string VC_EVENT_HUB_PROXY_USERNAME = nameof(VC_EVENT_HUB_PROXY_USERNAME);
182+
183+
/// <summary>
184+
/// Name = VC_EVENT_HUB_PROXY_PASSWORD
185+
/// </summary>
186+
public const string VC_EVENT_HUB_PROXY_PASSWORD = nameof(VC_EVENT_HUB_PROXY_PASSWORD);
177187

178188
/// <summary>
179189
/// Name = VC_LOGS_DIR
@@ -190,6 +200,11 @@ public static class EnvironmentVariable
190200
/// </summary>
191201
public const string VC_PACKAGES_DIR = nameof(VC_PACKAGES_DIR);
192202

203+
/// <summary>
204+
/// Name = VC_PASSWORD
205+
/// </summary>
206+
public const string VC_PASSWORD = nameof(VC_PASSWORD);
207+
193208
/// <summary>
194209
/// Name = VC_STATE_DIR
195210
/// </summary>

src/VirtualClient/VirtualClient.Core/DependencyFactory.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace VirtualClient
1010
using System.Linq;
1111
using System.Net;
1212
using System.Security.Cryptography.X509Certificates;
13-
using System.Threading.Tasks;
1413
using Azure.Core;
1514
using Azure.Messaging.EventHubs.Producer;
1615
using Microsoft.Extensions.Logging;
@@ -136,16 +135,18 @@ public static DiskManager CreateDiskManager(PlatformID platform)
136135
/// <param name="eventHubName">The name of the Event Hub within the namespace (e.g. telemetry-logs, telemetry-metrics).</param>
137136
/// <param name="eventHubNameSpace">Event hub namespace</param>
138137
/// <param name="tokenCredential">Azure Token credential to authenticate with </param>
139-
public static EventHubTelemetryChannel CreateEventHubTelemetryChannel(string eventHubName, string eventHubNameSpace = null, TokenCredential tokenCredential = null, string eventHubConnectionString = null)
138+
/// <param name="clientOptions">Options for configuring the Event Hub producer client. For example, these can be used to support the use of a proxy.</param>
139+
public static EventHubTelemetryChannel CreateEventHubTelemetryChannel(string eventHubName, string eventHubNameSpace = null, TokenCredential tokenCredential = null, string eventHubConnectionString = null, EventHubProducerClientOptions clientOptions = null)
140140
{
141141
EventHubProducerClient client;
142+
142143
if (!string.IsNullOrEmpty(eventHubConnectionString))
143144
{
144-
client = new EventHubProducerClient(eventHubConnectionString, eventHubName);
145+
client = new EventHubProducerClient(eventHubConnectionString, eventHubName, clientOptions);
145146
}
146147
else
147148
{
148-
client = new EventHubProducerClient(eventHubNameSpace, eventHubName, tokenCredential);
149+
client = new EventHubProducerClient(eventHubNameSpace, eventHubName, tokenCredential, clientOptions);
149150
}
150151

151152
EventHubTelemetryChannel channel = new EventHubTelemetryChannel(client, enableDiagnostics: true);
@@ -160,7 +161,8 @@ public static EventHubTelemetryChannel CreateEventHubTelemetryChannel(string eve
160161
/// <param name="settings">Defines the settings for each individual Event Hub targeted.</param>
161162
/// <param name="level">The logging severity level.</param>
162163
/// <param name="flushTimeout">A timeout to apply to flush operations.</param>
163-
public static IEnumerable<ILoggerProvider> CreateEventHubLoggerProviders(DependencyEventHubStore eventHubStore, EventHubLogSettings settings, LogLevel level, TimeSpan? flushTimeout = null)
164+
/// <param name="clientOptions">Options for configuring the Event Hub producer client. For example, these can be used to support the use of a proxy.</param>
165+
public static IEnumerable<ILoggerProvider> CreateEventHubLoggerProviders(DependencyEventHubStore eventHubStore, EventHubLogSettings settings, LogLevel level, TimeSpan? flushTimeout = null, EventHubProducerClientOptions clientOptions = null)
164166
{
165167
List<ILoggerProvider> loggerProviders = new List<ILoggerProvider>();
166168

@@ -189,7 +191,8 @@ public static IEnumerable<ILoggerProvider> CreateEventHubLoggerProviders(Depende
189191
eventHubName: settings.TracesHubName,
190192
eventHubNameSpace: authenticationContext.EventHubNamespace,
191193
authenticationContext.TokenCredential,
192-
authenticationContext.ConnectionString);
194+
authenticationContext.ConnectionString,
195+
clientOptions);
193196

194197
tracesChannel.EventTransmissionError += (sender, args) =>
195198
{
@@ -207,7 +210,8 @@ public static IEnumerable<ILoggerProvider> CreateEventHubLoggerProviders(Depende
207210
eventHubName: settings.MetricsHubName,
208211
eventHubNameSpace: authenticationContext.EventHubNamespace,
209212
authenticationContext.TokenCredential,
210-
authenticationContext.ConnectionString);
213+
authenticationContext.ConnectionString,
214+
clientOptions);
211215

212216
metricsChannel.EventTransmissionError += (sender, args) =>
213217
{
@@ -226,7 +230,8 @@ public static IEnumerable<ILoggerProvider> CreateEventHubLoggerProviders(Depende
226230
eventHubName: settings.EventsHubName,
227231
eventHubNameSpace: authenticationContext.EventHubNamespace,
228232
authenticationContext.TokenCredential,
229-
authenticationContext.ConnectionString);
233+
authenticationContext.ConnectionString,
234+
clientOptions);
230235

231236
systemEventsChannel.EventTransmissionError += (sender, args) =>
232237
{

src/VirtualClient/VirtualClient.Main/CommandBase.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ namespace VirtualClient
1111
using System.IO;
1212
using System.IO.Abstractions;
1313
using System.Linq;
14+
using System.Net;
1415
using System.Reflection;
1516
using System.Runtime.InteropServices;
1617
using System.Security.Cryptography.X509Certificates;
1718
using System.Text.RegularExpressions;
1819
using System.Threading;
1920
using System.Threading.Tasks;
21+
using Azure.Messaging.EventHubs.Producer;
2022
using Microsoft.Extensions.Configuration;
2123
using Microsoft.Extensions.DependencyInjection;
2224
using Microsoft.Extensions.Logging;
@@ -945,7 +947,28 @@ protected virtual IList<ILoggerProvider> InitializeLoggerProviders(IServiceColle
945947

946948
case "eventhub":
947949
DependencyEventHubStore store = EndpointUtility.CreateEventHubStoreReference(DependencyStore.Telemetry, endpoint: loggerParameters, this.CertificateManager ?? new CertificateManager());
948-
CommandBase.AddEventHubLogging(loggingProviders, configuration, store, loggingLevel);
950+
951+
// Supports the use of a Proxy for routing Event Hub traffic. This allows clients to use secure endpoints inside of their network
952+
// to route traffic to the Event Hub namespace (vs. a direct connection).
953+
// (e.g. http://proxy-dmz.contoso.com:135).
954+
EventHubProducerClientOptions clientOptions = new EventHubProducerClientOptions();
955+
Uri proxy = null;
956+
string proxyUri = platformSpecifics.GetEnvironmentVariable(EnvironmentVariable.VC_EVENT_HUB_PROXY);
957+
if (!string.IsNullOrWhiteSpace(proxyUri))
958+
{
959+
clientOptions.ConnectionOptions.TransportType = Azure.Messaging.EventHubs.EventHubsTransportType.AmqpWebSockets;
960+
clientOptions.ConnectionOptions.Proxy = new System.Net.WebProxy(new Uri(proxyUri), true);
961+
962+
string proxyUsername = platformSpecifics.GetEnvironmentVariable(EnvironmentVariable.VC_EVENT_HUB_PROXY_USERNAME);
963+
string proxyPassword = platformSpecifics.GetEnvironmentVariable(EnvironmentVariable.VC_EVENT_HUB_PROXY_PASSWORD);
964+
965+
if (!string.IsNullOrWhiteSpace(proxyUsername) && !string.IsNullOrWhiteSpace(proxyPassword))
966+
{
967+
clientOptions.ConnectionOptions.Proxy.Credentials = new NetworkCredential(proxyUsername, proxyPassword);
968+
}
969+
}
970+
971+
CommandBase.AddEventHubLogging(loggingProviders, configuration, store, loggingLevel, proxy);
949972
break;
950973

951974
case "proxy":
@@ -1090,15 +1113,15 @@ private static void AddCustomLogging(List<ILoggerProvider> loggingProviders, ISe
10901113
}
10911114
}
10921115

1093-
private static void AddEventHubLogging(List<ILoggerProvider> loggingProviders, IConfiguration configuration, DependencyEventHubStore eventHubStore, LogLevel level)
1116+
private static void AddEventHubLogging(List<ILoggerProvider> loggingProviders, IConfiguration configuration, DependencyEventHubStore eventHubStore, LogLevel level, Uri proxyUri = null)
10941117
{
10951118
if (eventHubStore != null)
10961119
{
10971120
EventHubLogSettings settings = configuration.GetSection(nameof(EventHubLogSettings)).Get<EventHubLogSettings>();
10981121

10991122
if (settings.IsEnabled)
11001123
{
1101-
IEnumerable<ILoggerProvider> eventHubProviders = DependencyFactory.CreateEventHubLoggerProviders(eventHubStore, settings, level);
1124+
IEnumerable<ILoggerProvider> eventHubProviders = DependencyFactory.CreateEventHubLoggerProviders(eventHubStore, settings, level, proxyUri: proxyUri);
11021125
if (eventHubProviders?.Any() == true)
11031126
{
11041127
loggingProviders.AddRange(eventHubProviders);

website/docs/guides/0610-integration-event-hub.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,17 @@ The following documentation illustrates how to use connection string-style refer
239239
--event-hub="Endpoint=sb://aaa.servicebus.windows.net/;SharedAccessKeyName=TelemetrySharedAccessKey;SharedAccessKey=bbbbbbbbbb..."
240240
```
241241

242+
### Proxy Support
243+
Virtual Client supports using a proxy to relay Event Hub communications. The application will use a simple web proxy model passing the AMQP protocol traffic to the proxy
244+
using simple HTTP/web sockets communications. The proxy supports anonymous authentication as well as basic authentication (i.e. username and password). Set the following
245+
environment variables to enable support for proxy communications:
246+
247+
| Environment Variable | Description |
248+
|-----------------------------|-------------|
249+
| VC_EVENT_HUB_PROXY | The host name or IP address of the proxy server to use for Event Hub communications routing. |
250+
| VC_EVENT_HUB_PROXY_USERNAME | The username to use for basic authentication with the proxy endpoint. If not defined, anonymous authentication will be used. |
251+
| VC_EVENT_HUB_PROXY_PASSWORD | The password to use for basic authentication with the proxy endpoint. If not defined, anonymous authentication will be used. |
252+
242253
### Create Event Hub Namespace
243254
The Virtual Client emits data for each one of these categories into a distinct/singular target Event Hub within an Event Hub namespace (a 1-to-1 mapping).
244255
In order to use Event Hub with the Virtual Client, an Event Hub namespace must be setup. The following recommendations relate to the Event Hub namespace itself.

0 commit comments

Comments
 (0)