| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.ComponentModel; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels; |
| | | 9 | | |
| | | 10 | | public sealed class KafkaSecurity |
| | | 11 | | { |
| | | 12 | | internal const KafkaSecurityMode DefaultMode = KafkaSecurityMode.None; |
| | | 13 | | private KafkaSecurityMode _mode; |
| | | 14 | | private KafkaTransportSecurity _transportSecurity; |
| | | 15 | | private KafkaMessageSecurity _messageSecurity; |
| | | 16 | | |
| | | 17 | | public KafkaSecurity() |
| | 52 | 18 | | : this(DefaultMode, new KafkaTransportSecurity(), new KafkaMessageSecurity()) |
| | | 19 | | { |
| | 52 | 20 | | } |
| | | 21 | | |
| | 52 | 22 | | private KafkaSecurity(KafkaSecurityMode mode, KafkaTransportSecurity transportSecurity, KafkaMessageSecurity message |
| | | 23 | | { |
| | | 24 | | Fx.Assert(KafkaSecurityModeHelper.IsDefined(mode), $"Invalid SecurityMode value: {mode.ToString()}."); |
| | | 25 | | |
| | 52 | 26 | | if (!KafkaSecurityModeHelper.IsSupported(mode)) |
| | | 27 | | { |
| | 0 | 28 | | throw new NotSupportedException(SR.SecurityModeNotSupportedYet); |
| | | 29 | | } |
| | | 30 | | |
| | 52 | 31 | | _mode = mode; |
| | 52 | 32 | | _transportSecurity = transportSecurity ?? new KafkaTransportSecurity(); |
| | 52 | 33 | | _messageSecurity = messageSecurity ?? new KafkaMessageSecurity(); |
| | 52 | 34 | | } |
| | | 35 | | |
| | | 36 | | [DefaultValue(DefaultMode)] |
| | | 37 | | public KafkaSecurityMode Mode |
| | | 38 | | { |
| | 0 | 39 | | get { return _mode; } |
| | | 40 | | set |
| | | 41 | | { |
| | 8 | 42 | | if (!KafkaSecurityModeHelper.IsDefined(value)) |
| | | 43 | | { |
| | 0 | 44 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(value)) |
| | | 45 | | } |
| | | 46 | | |
| | 8 | 47 | | if (!KafkaSecurityModeHelper.IsSupported(value)) |
| | | 48 | | { |
| | 0 | 49 | | throw new NotSupportedException(SR.SecurityModeNotSupportedYet); |
| | | 50 | | } |
| | | 51 | | |
| | 8 | 52 | | _mode = value; |
| | 8 | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public KafkaTransportSecurity Transport |
| | | 57 | | { |
| | 0 | 58 | | get => _transportSecurity; |
| | 8 | 59 | | set => _transportSecurity = value ?? throw new ArgumentNullException(nameof(value)); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public KafkaMessageSecurity Message |
| | | 63 | | { |
| | 0 | 64 | | get => _messageSecurity; |
| | 0 | 65 | | set => _messageSecurity = value ?? throw new ArgumentNullException(nameof(value)); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | internal SecurityBindingElement CreateMessageSecurity() |
| | | 69 | | { |
| | 882 | 70 | | return null; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | internal void ApplySecurity(KafkaTransportBindingElement element) |
| | | 74 | | { |
| | 882 | 75 | | if (_mode == KafkaSecurityMode.Transport || _mode == KafkaSecurityMode.TransportWithMessageCredential) |
| | | 76 | | { |
| | 78 | 77 | | _transportSecurity.ConfigureTransportSecurityWithClientAuthentication(element); |
| | | 78 | | } |
| | 804 | 79 | | else if (_mode == KafkaSecurityMode.TransportCredentialOnly) |
| | | 80 | | { |
| | 26 | 81 | | _transportSecurity.ConfigureClientAuthenticationOnly(element); |
| | | 82 | | } |
| | 778 | 83 | | else if (_mode == KafkaSecurityMode.None || _mode == KafkaSecurityMode.Message) |
| | | 84 | | { |
| | 778 | 85 | | _transportSecurity.ConfigureNoTransportSecurity(element); |
| | | 86 | | } |
| | 778 | 87 | | } |
| | | 88 | | } |