| | | 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 Confluent.Kafka; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.ServiceModel.Channels; |
| | | 8 | | |
| | | 9 | | public sealed class KafkaTransportSecurity |
| | | 10 | | { |
| | | 11 | | private KafkaCredentialType _credentialType = KafkaCredentialType.None; |
| | | 12 | | |
| | | 13 | | public KafkaTransportSecurity() |
| | | 14 | | { |
| | | 15 | | |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public KafkaCredentialType CredentialType |
| | | 19 | | { |
| | | 20 | | get => _credentialType; |
| | | 21 | | set |
| | | 22 | | { |
| | | 23 | | if (!KafkaCredentialTypeHelper.IsDefined(value)) |
| | | 24 | | { |
| | | 25 | | throw new ArgumentOutOfRangeException(nameof(value)); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | if (!KafkaCredentialTypeHelper.IsSupported(value)) |
| | | 29 | | { |
| | | 30 | | throw new ArgumentException(SR.InvalidCredentialType); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | _credentialType = value; |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | internal void ConfigureTransportSecurityWithClientAuthentication(KafkaTransportBindingElement bindingElement) |
| | | 38 | | { |
| | | 39 | | |
| | | 40 | | if (_credentialType == KafkaCredentialType.None) |
| | | 41 | | { |
| | | 42 | | bindingElement.SecurityProtocol ??= SecurityProtocol.Ssl; |
| | | 43 | | bindingElement.SslCaPem ??= CaPem; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | if (_credentialType == KafkaCredentialType.SslKeyPairCertificate) |
| | | 47 | | { |
| | | 48 | | if (SslKeyPairCredential is null) |
| | | 49 | | { |
| | | 50 | | throw new NotSupportedException(SR.MissingSslKeyPairCredential); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | bindingElement.SecurityProtocol ??= SecurityProtocol.Ssl; |
| | | 54 | | bindingElement.SslCaPem ??= CaPem; |
| | | 55 | | bindingElement.SslKeyPem ??= SslKeyPairCredential.SslKeyPem; |
| | | 56 | | bindingElement.SslKeyPassword ??= SslKeyPairCredential.SslKeyPassword; |
| | | 57 | | bindingElement.SslCertificatePem ??= SslKeyPairCredential.SslCertificatePem; |
| | | 58 | | |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | if (_credentialType == KafkaCredentialType.SaslPlain) |
| | | 62 | | { |
| | | 63 | | if (SaslUsernamePasswordCredential is null) |
| | | 64 | | { |
| | | 65 | | throw new NotSupportedException(SR.MissingSaslUsernamePasswordCredential); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | bindingElement.SslCaPem ??= CaPem; |
| | | 69 | | bindingElement.SaslMechanism ??= SaslMechanism.Plain; |
| | | 70 | | bindingElement.SecurityProtocol ??= SecurityProtocol.SaslSsl; |
| | | 71 | | bindingElement.SaslUsername ??= SaslUsernamePasswordCredential.SaslUsername; |
| | | 72 | | bindingElement.SaslPassword ??= SaslUsernamePasswordCredential.SaslPassword; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // TODO maps other security mechanism once requested. (Gssapi, Scram, OAuth..) |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | internal void ConfigureClientAuthenticationOnly(KafkaTransportBindingElement bindingElement) |
| | | 79 | | { |
| | | 80 | | if (_credentialType == KafkaCredentialType.SaslPlain) |
| | | 81 | | { |
| | | 82 | | if (SaslUsernamePasswordCredential is null) |
| | | 83 | | { |
| | | 84 | | throw new NotSupportedException(SR.MissingSaslUsernamePasswordCredential); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | bindingElement.SecurityProtocol ??= SecurityProtocol.SaslPlaintext; |
| | | 88 | | bindingElement.SaslMechanism ??= SaslMechanism.Plain; |
| | | 89 | | bindingElement.SaslUsername ??= SaslUsernamePasswordCredential.SaslUsername; |
| | | 90 | | bindingElement.SaslPassword ??= SaslUsernamePasswordCredential.SaslPassword; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | // TODO maps other security mechanism once requested. (Gssapi, Scram, OAuth..) |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | internal void ConfigureNoTransportSecurity(KafkaTransportBindingElement bindingElement) |
| | | 97 | | { |
| | | 98 | | bindingElement.SecurityProtocol ??= SecurityProtocol.Plaintext; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public string CaPem { get; set; } |
| | | 102 | | |
| | | 103 | | public SaslUsernamePasswordCredential SaslUsernamePasswordCredential { get; set; } |
| | | 104 | | |
| | | 105 | | public SslKeyPairCredential SslKeyPairCredential { get; set; } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public class SaslUsernamePasswordCredential |
| | | 109 | | { |
| | | 110 | | public SaslUsernamePasswordCredential(string saslUsername, string saslPassword) |
| | | 111 | | { |
| | | 112 | | SaslUsername = saslUsername; |
| | | 113 | | SaslPassword = saslPassword; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | public string SaslUsername { get; } |
| | | 117 | | public string SaslPassword { get; } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | public class SslKeyPairCredential |
| | | 121 | | { |
| | 2 | 122 | | public string SslCertificatePem { get; set; } |
| | 2 | 123 | | public string SslKeyPem { get; set; } |
| | 2 | 124 | | public string SslKeyPassword { get; set; } |
| | | 125 | | } |