Skip to content

Commit 044b04f

Browse files
authored
Refactor AuthenticationHeaderValueProvider construction (#2463)
1 parent 73e585c commit 044b04f

File tree

4 files changed

+190
-241
lines changed

4 files changed

+190
-241
lines changed

src/Confluent.SchemaRegistry.Encryption/CachedDekRegistryClient.cs

Lines changed: 10 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -173,87 +173,8 @@ public CachedDekRegistryClient(IEnumerable<KeyValuePair<string, string>> config,
173173
$"Configured value for {SchemaRegistryConfig.PropertyNames.SchemaRegistryMaxCachedSchemas} must be an integer.");
174174
}
175175

176-
var basicAuthSource = config.FirstOrDefault(prop =>
177-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource)
178-
.Value ?? "";
179-
var basicAuthInfo = config.FirstOrDefault(prop =>
180-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthUserInfo).Value ?? "";
181-
182-
string username = null;
183-
string password = null;
184-
185-
if (basicAuthSource == "USER_INFO" || basicAuthSource == "")
186-
{
187-
if (basicAuthInfo != "")
188-
{
189-
var userPass = basicAuthInfo.Split(new char[] { ':' }, 2);
190-
if (userPass.Length != 2)
191-
{
192-
throw new ArgumentException(
193-
$"Configuration property {SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthUserInfo} must be of the form 'username:password'.");
194-
}
195-
196-
username = userPass[0];
197-
password = userPass[1];
198-
}
199-
}
200-
else if (basicAuthSource == "SASL_INHERIT")
201-
{
202-
if (basicAuthInfo != "")
203-
{
204-
throw new ArgumentException(
205-
$"{SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource} set to 'SASL_INHERIT', but {SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthUserInfo} as also specified.");
206-
}
207-
208-
var saslUsername = config.FirstOrDefault(prop => prop.Key == "sasl.username");
209-
var saslPassword = config.FirstOrDefault(prop => prop.Key == "sasl.password");
210-
if (saslUsername.Value == null)
211-
{
212-
throw new ArgumentException(
213-
$"{SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource} set to 'SASL_INHERIT', but 'sasl.username' property not specified.");
214-
}
215-
216-
if (saslPassword.Value == null)
217-
{
218-
throw new ArgumentException(
219-
$"{SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource} set to 'SASL_INHERIT', but 'sasl.password' property not specified.");
220-
}
221-
222-
username = saslUsername.Value;
223-
password = saslPassword.Value;
224-
}
225-
else
226-
{
227-
throw new ArgumentException(
228-
$"Invalid value '{basicAuthSource}' specified for property '{SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource}'");
229-
}
230-
231-
if (authenticationHeaderValueProvider != null)
232-
{
233-
if (username != null || password != null)
234-
{
235-
throw new ArgumentException(
236-
$"Invalid authentication header value provider configuration: Cannot specify both custom provider and username/password");
237-
}
238-
}
239-
else
240-
{
241-
if (username != null && password == null)
242-
{
243-
throw new ArgumentException(
244-
$"Invalid authentication header value provider configuration: Basic authentication username specified, but password not specified");
245-
}
246-
247-
if (username == null && password != null)
248-
{
249-
throw new ArgumentException(
250-
$"Invalid authentication header value provider configuration: Basic authentication password specified, but username not specified");
251-
}
252-
else if (username != null && password != null)
253-
{
254-
authenticationHeaderValueProvider = new BasicAuthenticationHeaderValueProvider(username, password);
255-
}
256-
}
176+
authenticationHeaderValueProvider = DekRestService.AuthenticationHeaderValueProvider(
177+
config, authenticationHeaderValueProvider, maxRetries, retriesWaitMs, retriesMaxWaitMs);
257178

258179
foreach (var property in config)
259180
{
@@ -271,6 +192,14 @@ public CachedDekRegistryClient(IEnumerable<KeyValuePair<string, string>> config,
271192
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryLatestCacheTtlSecs &&
272193
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource &&
273194
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthUserInfo &&
195+
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthCredentialsSource &&
196+
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthToken &&
197+
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthClientId &&
198+
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthClientSecret &&
199+
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthScope &&
200+
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthTokenEndpointUrl &&
201+
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthLogicalCluster &&
202+
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthIdentityPoolId &&
274203
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryKeySubjectNameStrategy &&
275204
property.Key != SchemaRegistryConfig.PropertyNames.SchemaRegistryValueSubjectNameStrategy &&
276205
property.Key != SchemaRegistryConfig.PropertyNames.SslCaLocation &&

src/Confluent.SchemaRegistry.Encryption/Rest/DekRestService.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,15 @@ public async Task<RegisteredDek> CreateDekAsync(string kekName, Dek dek)
9999
.ConfigureAwait(continueOnCapturedContext: false);
100100

101101
#endregion Deks
102+
103+
protected internal static IAuthenticationHeaderValueProvider
104+
AuthenticationHeaderValueProvider(
105+
IEnumerable<KeyValuePair<string, string>> config,
106+
IAuthenticationHeaderValueProvider authenticationHeaderValueProvider,
107+
int maxRetries, int retriesWaitMs, int retriesMaxWaitMs)
108+
{
109+
return RestService.AuthenticationHeaderValueProvider(config,
110+
authenticationHeaderValueProvider, maxRetries, retriesWaitMs, retriesMaxWaitMs);
111+
}
102112
}
103113
}

src/Confluent.SchemaRegistry/CachedSchemaRegistryClient.cs

Lines changed: 2 additions & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
using System.Threading.Tasks;
2424
using System.Linq;
2525
using System;
26-
using System.Net.Http;
2726
using System.Collections.Concurrent;
2827
using System.Net;
2928
using System.Security.Cryptography.X509Certificates;
@@ -308,165 +307,8 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf
308307
$"Configured value for {SchemaRegistryConfig.PropertyNames.SchemaRegistryLatestCacheTtlSecs} must be an integer.");
309308
}
310309

311-
var basicAuthSource = config.FirstOrDefault(prop =>
312-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource)
313-
.Value ?? "";
314-
var basicAuthInfo = config.FirstOrDefault(prop =>
315-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthUserInfo).Value ?? "";
316-
317-
string username = null;
318-
string password = null;
319-
320-
if (basicAuthSource == "USER_INFO" || basicAuthSource == "")
321-
{
322-
if (basicAuthInfo != "")
323-
{
324-
var userPass = basicAuthInfo.Split(new char[] { ':' }, 2);
325-
if (userPass.Length != 2)
326-
{
327-
throw new ArgumentException(
328-
$"Configuration property {SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthUserInfo} must be of the form 'username:password'.");
329-
}
330-
331-
username = userPass[0];
332-
password = userPass[1];
333-
if (authenticationHeaderValueProvider != null)
334-
{
335-
throw new ArgumentException(
336-
$"Invalid authentication header value provider configuration: Cannot specify both custom provider and username/password");
337-
}
338-
authenticationHeaderValueProvider = new BasicAuthenticationHeaderValueProvider(username, password);
339-
}
340-
}
341-
else if (basicAuthSource == "SASL_INHERIT")
342-
{
343-
if (basicAuthInfo != "")
344-
{
345-
throw new ArgumentException(
346-
$"{SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource} set to 'SASL_INHERIT', but {SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthUserInfo} as also specified.");
347-
}
348-
349-
var saslUsername = config.FirstOrDefault(prop => prop.Key == "sasl.username");
350-
var saslPassword = config.FirstOrDefault(prop => prop.Key == "sasl.password");
351-
if (saslUsername.Value == null)
352-
{
353-
throw new ArgumentException(
354-
$"{SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource} set to 'SASL_INHERIT', but 'sasl.username' property not specified.");
355-
}
356-
357-
if (saslPassword.Value == null)
358-
{
359-
throw new ArgumentException(
360-
$"{SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource} set to 'SASL_INHERIT', but 'sasl.password' property not specified.");
361-
}
362-
363-
username = saslUsername.Value;
364-
password = saslPassword.Value;
365-
if (authenticationHeaderValueProvider != null)
366-
{
367-
throw new ArgumentException(
368-
$"Invalid authentication header value provider configuration: Cannot specify both custom provider and username/password");
369-
}
370-
authenticationHeaderValueProvider = new BasicAuthenticationHeaderValueProvider(username, password);
371-
}
372-
else
373-
{
374-
throw new ArgumentException(
375-
$"Invalid value '{basicAuthSource}' specified for property '{SchemaRegistryConfig.PropertyNames.SchemaRegistryBasicAuthCredentialsSource}'");
376-
}
377-
378-
var bearerAuthSource = config.FirstOrDefault(prop =>
379-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthCredentialsSource).Value ?? "";
380-
381-
if (bearerAuthSource != "" && basicAuthSource != "")
382-
{
383-
throw new ArgumentException(
384-
$"Invalid authentication header value provider configuration: Cannot specify both basic and bearer authentication");
385-
}
386-
387-
string logicalCluster = null;
388-
string identityPoolId = null;
389-
string bearerToken = null;
390-
string clientId = null;
391-
string clientSecret = null;
392-
string scope = null;
393-
string tokenEndpointUrl = null;
394-
395-
if (bearerAuthSource == "STATIC_TOKEN" || bearerAuthSource == "OAUTHBEARER")
396-
{
397-
if (authenticationHeaderValueProvider != null)
398-
{
399-
throw new ArgumentException(
400-
$"Invalid authentication header value provider configuration: Cannot specify both custom provider and bearer authentication");
401-
}
402-
logicalCluster = config.FirstOrDefault(prop =>
403-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthLogicalCluster).Value;
404-
405-
identityPoolId = config.FirstOrDefault(prop =>
406-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthIdentityPoolId).Value;
407-
if (logicalCluster == null || identityPoolId == null)
408-
{
409-
throw new ArgumentException(
410-
$"Invalid bearer authentication provider configuration: Logical cluster and identity pool ID must be specified");
411-
}
412-
}
413-
414-
switch (bearerAuthSource)
415-
{
416-
case "STATIC_TOKEN":
417-
bearerToken = config.FirstOrDefault(prop =>
418-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthToken).Value;
419-
420-
if (bearerToken == null)
421-
{
422-
throw new ArgumentException(
423-
$"Invalid authentication header value provider configuration: Bearer authentication token not specified");
424-
}
425-
authenticationHeaderValueProvider = new StaticBearerAuthenticationHeaderValueProvider(bearerToken, logicalCluster, identityPoolId);
426-
break;
427-
428-
case "OAUTHBEARER":
429-
clientId = config.FirstOrDefault(prop =>
430-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthClientId).Value;
431-
432-
clientSecret = config.FirstOrDefault(prop =>
433-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthClientSecret).Value;
434-
435-
scope = config.FirstOrDefault(prop =>
436-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthScope).Value;
437-
438-
tokenEndpointUrl = config.FirstOrDefault(prop =>
439-
prop.Key.ToLower() == SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthTokenEndpointUrl).Value;
440-
441-
if (tokenEndpointUrl == null || clientId == null || clientSecret == null || scope == null)
442-
{
443-
throw new ArgumentException(
444-
$"Invalid bearer authentication provider configuration: Token endpoint URL, client ID, client secret, and scope must be specified");
445-
}
446-
authenticationHeaderValueProvider = new BearerAuthenticationHeaderValueProvider(
447-
new HttpClient(), clientId, clientSecret, scope, tokenEndpointUrl, logicalCluster, identityPoolId, maxRetries, retriesWaitMs, retriesMaxWaitMs);
448-
break;
449-
450-
case "CUSTOM":
451-
if (authenticationHeaderValueProvider == null)
452-
{
453-
throw new ArgumentException(
454-
$"Invalid authentication header value provider configuration: Custom authentication provider must be specified");
455-
}
456-
if(!(authenticationHeaderValueProvider is IAuthenticationBearerHeaderValueProvider))
457-
{
458-
throw new ArgumentException(
459-
$"Invalid authentication header value provider configuration: Custom authentication provider must implement IAuthenticationBearerHeaderValueProvider");
460-
}
461-
break;
462-
463-
case "":
464-
break;
465-
466-
default:
467-
throw new ArgumentException(
468-
$"Invalid value '{bearerAuthSource}' specified for property '{SchemaRegistryConfig.PropertyNames.SchemaRegistryBearerAuthCredentialsSource}'");
469-
}
310+
authenticationHeaderValueProvider = RestService.AuthenticationHeaderValueProvider(
311+
config, authenticationHeaderValueProvider, maxRetries, retriesWaitMs, retriesMaxWaitMs);
470312

471313
foreach (var property in config)
472314
{

0 commit comments

Comments
 (0)