|
23 | 23 | using System.Threading.Tasks; |
24 | 24 | using System.Linq; |
25 | 25 | using System; |
26 | | -using System.Net.Http; |
27 | 26 | using System.Collections.Concurrent; |
28 | 27 | using System.Net; |
29 | 28 | using System.Security.Cryptography.X509Certificates; |
@@ -308,165 +307,8 @@ public CachedSchemaRegistryClient(IEnumerable<KeyValuePair<string, string>> conf |
308 | 307 | $"Configured value for {SchemaRegistryConfig.PropertyNames.SchemaRegistryLatestCacheTtlSecs} must be an integer."); |
309 | 308 | } |
310 | 309 |
|
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); |
470 | 312 |
|
471 | 313 | foreach (var property in config) |
472 | 314 | { |
|
0 commit comments