|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- using Confluent.Kafka;
- using Confluent.Kafka.Admin;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TelpoKafkaConsole.Model;
- using static System.Net.Mime.MediaTypeNames;
-
- namespace TelpoKafkaConsole.Service
- {
- public class KafkaAdminService
- {
- private readonly ILogger<KafkaAdminService> _logger;
- private readonly ServiceConfig _configService;
- public IAdminClient _adminClient;
- public KafkaAdminService(ILogger<KafkaAdminService> logger, IOptions<ServiceConfig> _optConfigService)
- {
- _logger = logger;
- _configService = _optConfigService.Value;
- _adminClient = new AdminClientBuilder(new AdminClientConfig
- {
- BootstrapServers = _configService.KafkaServerAddress,
- SecurityProtocol = SecurityProtocol.SaslSsl,
- SaslMechanism = SaslMechanism.ScramSha256,
- SaslUsername = "superuser",
- SaslPassword = "password",
- SslCaLocation = _configService.KafkaServerPEMLocation
- // Add any other configuration options as needed
- }).Build();
- }
-
- public List<GroupInfo> ListGroups()
- {
-
- try
- {
- var groups = _adminClient.ListGroups(TimeSpan.FromSeconds(10));
- return groups;
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
-
- #region UserScramCredentials
-
- public async Task<List<UserScramCredentialsDescription>> DescribeUserScramCredentialsAsync(IEnumerable<string> users)
- {
- try
- {
-
- var timeout = TimeSpan.FromSeconds(10);
- var descResult = await _adminClient.DescribeUserScramCredentialsAsync(users, new DescribeUserScramCredentialsOptions() { RequestTimeout = timeout });
- return descResult.UserScramCredentialsDescriptions;
- //foreach (var description in descResult.UserScramCredentialsDescriptions)
- //{
- // Console.WriteLine($" User: {description.User}");
- // foreach (var scramCredentialInfo in description.ScramCredentialInfos)
- // {
- // Console.WriteLine($" Mechanism: {scramCredentialInfo.Mechanism}");
- // Console.WriteLine($" Iterations: {scramCredentialInfo.Iterations}");
- // }
- //}
- }
- catch (DescribeUserScramCredentialsException e)
- {
- if (e.Error.Code.ToString().Equals("Local_Partial")
- && e.Results.UserScramCredentialsDescriptions.Count == 1
- && e.Results.UserScramCredentialsDescriptions.First().ScramCredentialInfos.Count == 0
- )
- {
- return new List<UserScramCredentialsDescription>();
- }
- else
- {
- var errMsg = $"An error occurred describing user SCRAM credentials for some users:\n";
-
- foreach (var description in e.Results.UserScramCredentialsDescriptions)
- {
- errMsg += ($"User: {description.User} -- Error: {description.Error}\n");
- if (!description.Error.IsError)
- {
- foreach (var scramCredentialInfo in description.ScramCredentialInfos)
- {
- errMsg += ($"Mechanism: {scramCredentialInfo.Mechanism} -- Iterations: {scramCredentialInfo.Iterations}\n");
- }
- }
- }
- throw new Exception(errMsg);
- }
-
-
- }
- catch (KafkaException e)
- {
- // _logger.LogError($"An error occurred describing user SCRAM credentials: {e}");
- throw new KafkaException(e.Error);
-
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- }
- }
-
-
- public async Task AlterUserScramCredentialsAsync(ScramCredentialsUser scramUser, string Action = "UPSERT")
- {
- var alterations = new List<UserScramCredentialAlteration>();
- string user = scramUser.Name;
- var mechanism = scramUser.Mechanism;
- var iterations = scramUser.Iterations;
- var password = Encoding.UTF8.GetBytes(scramUser.Password);
- if (Action.Equals("DELETE"))
- {
- alterations.Add(new UserScramCredentialDeletion
- {
- User = user,
- Mechanism = mechanism,
- }
- );
- }
- else
- {
- alterations.Add(new UserScramCredentialUpsertion
- {
- User = user,
- ScramCredentialInfo = new ScramCredentialInfo
- {
- Mechanism = mechanism,
- Iterations = iterations,
- },
- Password = password,
- // Salt = salt,
- }
- );
- }
- var timeout = TimeSpan.FromSeconds(30);
- try
- {
- await _adminClient.AlterUserScramCredentialsAsync(alterations,new AlterUserScramCredentialsOptions() { RequestTimeout = timeout });
- //_logger.LogError("All AlterUserScramCredentials operations completed successfully");
- }
- catch (AlterUserScramCredentialsException e)
- {
- var errMsg = ($"An error occurred altering user SCRAM credentials for some users:");
- foreach (var result in e.Results)
- {
- errMsg += ($"User: {result.User} -- Error: {result.Error}\n");
- }
- throw new Exception( errMsg );
- }
- catch (KafkaException e)
- {
- //_logger.LogError($"An error occurred altering user SCRAM credentials: {e}");
- throw new KafkaException( e.Error );
- }
- catch (Exception ex)
- {
-
- //_logger.LogError(ex.Message);
- throw new Exception(ex.Message);
- }
- }
-
- #endregion
-
- #region ACLs
- public async Task<List<AclBinding>> DescribeAclsAsync()
- {
- //var name = "testtopic";//"";
- //var principal = "demo-consumer";
- var host = "*";
- List<AclBinding> ParseAclBindings = new()
- {
- new() {
- Pattern = new ResourcePattern
- {
- Type = Confluent.Kafka.Admin.ResourceType.Any,//resourceType,
- //Name = "demo-orders",
- ResourcePatternType = ResourcePatternType.Any//resourcePatternType
- },
- Entry = new AccessControlEntry
- {
- //Principal ="User:demo-consumer",
- Host = host,
- Operation = AclOperation.Any,//operation,
- PermissionType = AclPermissionType.Any//permissionType
- }
- }
- };
- List<AclBindingFilter> aclBindingFilters = ParseAclBindings.Select(aclBinding => aclBinding.ToFilter()).ToList();
- try
- {
- var result = await _adminClient.DescribeAclsAsync(aclBindingFilters[0]);
- return result.AclBindings;
- }
- catch (DescribeAclsException e)
- {
- //_logger.LogError($"An error occurred in describe ACLs operation: Code: {e.Result.Error.Code}" +
- // $", Reason: {e.Result.Error.Reason}");
- throw new Exception($"An error occurred in describe ACLs operation: Code: {e.Result.Error.Code}, Reason: {e.Result.Error.Reason}");
- }
- catch (KafkaException e)
- {
- throw new KafkaException(e.Error);
- }
- catch (Exception ex)
- {
-
- //_logger.LogError(ex.Message);
- throw new Exception(ex.Message);
- }
-
- }
-
- public async Task CreateAclsAsync(List<AclBinding> aclBindings)
- {
- try
- {
- await _adminClient.CreateAclsAsync(aclBindings);
- _logger.LogInformation("All create ACL operations completed successfully");
- }
- catch (CreateAclsException e)
- {
- var errMsg = ("One or more create ACL operations failed.\n");
- for (int i = 0; i < e.Results.Count; ++i)
- {
- var result = e.Results[i];
- if (!result.Error.IsError)
- {
- errMsg += ($"Create ACLs operation {i} completed successfully\n");
- }
- else
- {
- errMsg += ($"An error occurred in create ACL operation {i}: Code: {result.Error.Code}" +
- $", Reason: {result.Error.Reason}\n");
- }
- }
-
- throw new Exception(errMsg);
- }
- catch (KafkaException e)
- {
- //_logger.LogError($"An error occurred calling the CreateAcls operation: {e.Message}");
- throw new Exception($"An error occurred calling the CreateAcls operation: {e.Message}");
- }
- catch (Exception ex)
- {
-
- //_logger.LogError(ex.Message);
- throw new Exception(ex.Message);
- }
-
- }
-
- public async Task DeleteAclsAsync(List<AclBinding> aclBindings)
- {
-
- List<AclBindingFilter> aclBindingFilters = aclBindings.Select(aclBinding => aclBinding.ToFilter()).ToList();
- try
- {
- var results = await _adminClient.DeleteAclsAsync(aclBindingFilters);
- int i = 0;
- foreach (var result in results)
- {
- _logger.LogInformation($"Deleted ACLs in operation {i}");
- // PrintAclBindings(result.AclBindings);
- ++i;
- }
- }
- catch (DeleteAclsException e)
- {
- var errMsg = ("One or more create ACL operations failed.\n");
- for (int i = 0; i < e.Results.Count; ++i)
- {
- var result = e.Results[i];
- if (!result.Error.IsError)
- {
- errMsg += ($"Deleted ACLs in operation {i}\n");
- // PrintAclBindings(result.AclBindings);
- }
- else
- {
- errMsg += ($"An error occurred in delete ACL operation {i}: Code: {result.Error.Code}" +
- $", Reason: {result.Error.Reason}\n");
- }
- }
-
- throw new Exception (errMsg);
- }
- catch (KafkaException e)
- {
- throw new KafkaException(e.Error);
- }
- catch (Exception ex)
- {
-
- //_logger.LogError(ex.Message);
- throw new Exception(ex.Message);
- }
-
-
- }
- #endregion
-
- #region Topics
- public async Task CreateTopic(string topicName, TimeSpan retentionTime,int numPartitions=1)
- {
- try
- {
- var configEntries = new Dictionary<string, string>
- {
- { "retention.ms", ((int)retentionTime.TotalMilliseconds).ToString() }
- };
-
- await _adminClient.CreateTopicsAsync(
- new TopicSpecification[] {
- new() {
- Name = topicName,
- ReplicationFactor = 1,
- NumPartitions = numPartitions>3 ? 3 : numPartitions,
- Configs = configEntries
- }
- });
- }
- catch (CreateTopicsException e)
- {
- throw new Exception($"An error occurred creating topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
- }
- }
- public async Task DeleteTopics(IEnumerable<string> topicNames)
- {
- try
- {
- await _adminClient.DeleteTopicsAsync(topicNames);
- }
- catch (DeleteTopicsException e)
- {
- throw new Exception($"An error occurred deleting topic {e.Results[0].Topic}: {e.Results[0].Error.Reason}");
- }
- }
- public async Task<List<TopicDescription>> DescribeTopicsAsync(IEnumerable<string> topicNames)
- {
- try
- {
- // var topicCollection = TopicCollection.OfTopicNames(new[] { topicName });
- var topicCollection = TopicCollection.OfTopicNames(topicNames);
- var topicDescriptions = await _adminClient.DescribeTopicsAsync(topicCollection);
- return topicDescriptions.TopicDescriptions;
- //foreach (var topicDescription in topicDescriptions.TopicDescriptions)
- //{
- // Console.WriteLine($"Topic: {topicDescription.Name}");
- // foreach (var partition in topicDescription.Partitions)
- // {
- // Console.WriteLine($"Partition: {partition.Partition}, Leader: {partition.Leader}, Replicas: {string.Join(",", partition.Replicas)}, Isr: {string.Join(",", partition.ISR)}");
- // }
- //}
- }
- catch (DescribeTopicsException e)
- {
- if (e.Error.Code.ToString().Equals("Local_Partial")
- && e.Results.TopicDescriptions.First().Error.Code.ToString().Equals("UnknownTopicOrPart")
- && e.Results.TopicDescriptions.First().Name.Equals(topicNames.First())
- )
- {
- return new List<TopicDescription>();
- }
- else
- {
- throw new Exception($"An error occurred describing topic {e.Message}");
-
- }
- // throw new Exception($"An error occurred describing topic {e.Message}");
- }
- }
-
- #endregion
-
-
- }
- }
|