| | | 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 System.ServiceModel.Channels; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.ServiceModel.Channels; |
| | | 9 | | |
| | | 10 | | public sealed class KafkaSecurity |
| | | 11 | | { |
| | | 12 | | private KafkaTransportSecurity _transportSecurity; |
| | | 13 | | private KafkaMessageSecurity _messageSecurity; |
| | | 14 | | private KafkaSecurityMode _mode; |
| | | 15 | | internal const KafkaSecurityMode DefaultMode = KafkaSecurityMode.None; |
| | | 16 | | |
| | | 17 | | public KafkaSecurity() |
| | 44 | 18 | | : this(DefaultMode, new KafkaTransportSecurity(), new KafkaMessageSecurity()) |
| | | 19 | | { |
| | | 20 | | |
| | 44 | 21 | | } |
| | | 22 | | |
| | 44 | 23 | | private KafkaSecurity(KafkaSecurityMode mode, KafkaTransportSecurity transportSecurity, |
| | 44 | 24 | | KafkaMessageSecurity messageSecurity) |
| | | 25 | | { |
| | 44 | 26 | | _mode = mode; |
| | 44 | 27 | | _transportSecurity = transportSecurity; |
| | 44 | 28 | | _messageSecurity = messageSecurity; |
| | 44 | 29 | | } |
| | | 30 | | |
| | | 31 | | [DefaultValue(DefaultMode)] |
| | | 32 | | public KafkaSecurityMode Mode |
| | | 33 | | { |
| | 4 | 34 | | get { return _mode; } |
| | | 35 | | set |
| | | 36 | | { |
| | 5 | 37 | | if (!KafkaSecurityModeHelper.IsDefined(value)) |
| | | 38 | | { |
| | 0 | 39 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 40 | | } |
| | | 41 | | |
| | 5 | 42 | | if (!KafkaSecurityModeHelper.IsSupported(value)) |
| | | 43 | | { |
| | 0 | 44 | | throw new NotSupportedException(SR.SecurityModeNotSupportedYet); |
| | | 45 | | } |
| | | 46 | | |
| | 5 | 47 | | _mode = value; |
| | 5 | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public KafkaTransportSecurity Transport |
| | | 52 | | { |
| | 4 | 53 | | get => _transportSecurity; |
| | 4 | 54 | | set => _transportSecurity = value ?? throw new ArgumentNullException(nameof(value)); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public KafkaMessageSecurity Message |
| | | 58 | | { |
| | 4 | 59 | | get => _messageSecurity; |
| | 0 | 60 | | set => _messageSecurity = value ?? throw new ArgumentNullException(nameof(value)); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | internal SecurityBindingElement CreateMessageSecurity() |
| | | 64 | | { |
| | 331 | 65 | | return null; |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | internal void ApplySecurity(KafkaTransportBindingElement bindingElement) |
| | | 69 | | { |
| | 331 | 70 | | if (_mode == KafkaSecurityMode.Transport || _mode == KafkaSecurityMode.TransportWithMessageCredential) |
| | | 71 | | { |
| | 30 | 72 | | _transportSecurity.ConfigureTransportSecurityWithClientAuthentication(bindingElement); |
| | | 73 | | } |
| | 301 | 74 | | else if (_mode == KafkaSecurityMode.TransportCredentialOnly) |
| | | 75 | | { |
| | 10 | 76 | | _transportSecurity.ConfigureClientAuthenticationOnly(bindingElement); |
| | | 77 | | } |
| | 291 | 78 | | else if (_mode == KafkaSecurityMode.None || _mode == KafkaSecurityMode.Message) |
| | | 79 | | { |
| | 291 | 80 | | _transportSecurity.ConfigureNoTransportSecurity(bindingElement); |
| | | 81 | | } |
| | 291 | 82 | | } |
| | | 83 | | } |