|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- using Confluent.Kafka.Admin;
- using Microsoft.AspNetCore.Mvc;
- using TelpoKafkaConsole.Model;
- using TelpoKafkaConsole.Service;
- using TelpoKafkaConsole.WebApi.Controllers.Api;
- using TelpoKafkaConsole.WebApi.Model.Request;
- using static Confluent.Kafka.ConfigPropertyNames;
-
- namespace TelpoKafkaConsole.WebApi.Controllers
- {
- [Route("api/[controller]")]
- [ApiController]
- public class ScramAclsController : ControllerBase
- {
- private readonly KafkaAdminService _servicekafkaAdmin;
- public ScramAclsController(KafkaAdminService kafkaAdminService) { _servicekafkaAdmin = kafkaAdminService; }
-
- // POST api/<ScramAclsController>/Consumer
- [HttpPost("Consumer")] // 添加了路由
- public async Task<ApiResponse<string>> Consumer([FromBody] ScramAclsConsumerReq consumer)
- {
-
- // 创建 SASL 用户
- ScramCredentialsUser scramUser = new ScramCredentialsUser
- {
- Name = consumer.Name,
- Password = consumer.Password
- };
- await _servicekafkaAdmin.AlterUserScramCredentialsAsync(scramUser);
-
- // 检查并创建主题(如果不存在)
- var topics = await _servicekafkaAdmin.DescribeTopicsAsync(new List<string> { consumer.Topic });
- if (topics.Count == 0)
- {
- await _servicekafkaAdmin.CreateTopic(consumer.Topic, TimeSpan.FromDays(3), Math.Max(3, consumer.NumPartitions));
- }
-
- // 创建 ACLs
- List<AclBinding> aclBindings = new List<AclBinding>
- {
- // 允许用户读取特定主题
- new AclBinding
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Topic,
- Name = consumer.Topic,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Read,
- PermissionType = AclPermissionType.Allow
- }
- },
- // 允许用户读取特定消费组
- new AclBinding
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Group,
- Name = consumer.Group,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Read,
- PermissionType = AclPermissionType.Allow
- }
- },
- // 禁止用户执行任何其他操作(写入、修改、删除、描述等)
- new AclBinding
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Group,
- Name = consumer.Group,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.All,
- PermissionType = AclPermissionType.Deny
- }
- }
- };
-
- // 创建 ACLs
- await _servicekafkaAdmin.CreateAclsAsync(aclBindings);
-
- return ApiResponse<string>.Success($"创建消费者用户 {consumer.Name} 和 ACLs 规则成功");
-
-
- }
-
- /**public async Task<ApiResponse<string>> Consumer([FromBody] ScramAclsConsumerReq consumer)
- {
- // 创建用户
- ScramCredentialsUser scramUser = new()
- {
- Name = consumer.Name,
- Password = consumer.Password,
- };
- await _servicekafkaAdmin.AlterUserScramCredentialsAsync(scramUser);
- // 创建 topic
- var topics = await _servicekafkaAdmin.DescribeTopicsAsync(new List<string> { consumer.Topic });
- if (topics.Count.Equals(0))
- {
- await _servicekafkaAdmin.CreateTopic(consumer.Topic, TimeSpan.FromDays(3), consumer.NumPartitions>3 ? 3: consumer.NumPartitions);
- }
-
- // 创建 alcs
- List<AclBinding> aclBindings = new()
- {
- //new AclBinding()
- //{
- // Pattern = new ResourcePattern
- // {
- // Type = ResourceType.Broker,
- // Name = "kafka-cluster",
- // ResourcePatternType = ResourcePatternType.Literal
- // },
- // Entry = new AccessControlEntry
- // {
- // Principal = $"User:{consumer.Name}",
- // Host = "*",
- // Operation = AclOperation.All,
- // PermissionType = AclPermissionType.Deny
- // }
- //},
- //new AclBinding()
- // {
- // Pattern = new ResourcePattern
- // {
- // Type = ResourceType.Broker,
- // Name = "kafka-cluster",
- // ResourcePatternType = ResourcePatternType.Literal
- // },
- // Entry = new AccessControlEntry
- // {
- // Principal = $"User:superuser",
- // Host = "*",
- // Operation = AclOperation.All,
- // PermissionType = AclPermissionType.Allow
- // }
- //},
- //// 禁止查看
- // new AclBinding()
- // {
- // Pattern = new ResourcePattern
- // {
- // Type = ResourceType.Broker,
- // Name = "kafka-cluster",
- // ResourcePatternType = ResourcePatternType.Literal
- // },
- // Entry = new AccessControlEntry
- // {
- // Principal = $"User:{consumer.Name}",
- // Host = "*",
- // Operation = AclOperation.Describe,
- // PermissionType = AclPermissionType.Deny
- // }
- // },
- // // 禁止修改
- // new AclBinding()
- // {
- // Pattern = new ResourcePattern
- // {
- // Type = ResourceType.Broker,
- // Name = "kafka-cluster",
- // ResourcePatternType = ResourcePatternType.Literal
- // },
- // Entry = new AccessControlEntry
- // {
- // Principal = $"User:{consumer.Name}",
- // Host = "*",
- // Operation = AclOperation.Alter,
- // PermissionType = AclPermissionType.Deny
- // }
- // },
- //// 禁止写入
- //new AclBinding()
- // {
- // Pattern = new ResourcePattern
- // {
- // Type = ResourceType.Broker,
- // Name = "kafka-cluster",
- // ResourcePatternType = ResourcePatternType.Literal
- // },
- // Entry = new AccessControlEntry
- // {
- // Principal = $"User:{consumer.Name}",
- // Host = "*",
- // Operation = AclOperation.Write,
- // PermissionType = AclPermissionType.Deny
- // }
- // },
- //// 禁止创建
- //new AclBinding()
- // {
- // Pattern = new ResourcePattern
- // {
- // Type = ResourceType.Broker,
- // Name = "kafka-cluster",
- // ResourcePatternType = ResourcePatternType.Literal
- // },
- // Entry = new AccessControlEntry
- // {
- // Principal = $"User:{consumer.Name}",
- // Host = "*",
- // Operation = AclOperation.Create,
- // PermissionType = AclPermissionType.Deny
- // }
- // },
- //new AclBinding()
- // {
- // Pattern = new ResourcePattern
- // {
- // Type = ResourceType.Broker,
- // Name = "kafka-cluster",
- // ResourcePatternType = ResourcePatternType.Literal
- // },
- // Entry = new AccessControlEntry
- // {
- // Principal = $"User:{consumer.Name}",
- // Host = "*",
- // Operation = AclOperation.Unknown,
- // PermissionType = AclPermissionType.Deny
- // }
- // },
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Group,
- Name = consumer.Group,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Read,
- PermissionType = AclPermissionType.Allow
- }
- },
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Topic,
- Name = consumer.Topic,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Read,
- PermissionType = AclPermissionType.Allow
- }
- },
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Any,
- Name = consumer.Group,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Write,
- PermissionType = AclPermissionType.Deny
- }
- },
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Topic,
- Name = consumer.Group,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Write,
- PermissionType = AclPermissionType.Deny
- }
- },
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Topic,
- Name = consumer.Group,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Alter,
- PermissionType = AclPermissionType.Deny
- }
- },
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Topic,
- Name = consumer.Group,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Describe,
- PermissionType = AclPermissionType.Deny
- }
- },
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Topic,
- Name = consumer.Group,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{consumer.Name}",
- Host = "*",
- Operation = AclOperation.Delete,
- PermissionType = AclPermissionType.Deny
- }
- }
- };
- await _servicekafkaAdmin.CreateAclsAsync(aclBindings);
-
-
- return ApiResponse<string>.Success($"创建 消费者用户 {consumer.Name} Acls 规则成功");
- }
- */
-
- // POST api/<ScramAclsController>/Producer
- [HttpPost("Producer")] // 添加了路由
- public async Task<ApiResponse<string>> Producer([FromBody] ScramAclsProducerReq producer)
- {
- // 创建用户
- ScramCredentialsUser scramUser = new()
- {
- Name = producer.Name,
- Password = producer.Password,
- };
- await _servicekafkaAdmin.AlterUserScramCredentialsAsync(scramUser);
- // 创建 topic
- var topics = await _servicekafkaAdmin.DescribeTopicsAsync(new List<string> { producer.Topic });
- if (topics.Count.Equals(0))
- {
- await _servicekafkaAdmin.CreateTopic(producer.Topic, TimeSpan.FromDays(3), producer.NumPartitions);
- }
- // 创建 alcs
- List<AclBinding> aclBindings = new()
- {
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Broker,
- Name = "kafka-cluster",
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{producer.Name}",
- Host = "*",
- Operation = AclOperation.All,
- PermissionType = AclPermissionType.Deny
- }
- },
- new AclBinding()
- {
- Pattern = new ResourcePattern
- {
- Type = ResourceType.Topic,
- Name = producer.Topic,
- ResourcePatternType = ResourcePatternType.Literal
- },
- Entry = new AccessControlEntry
- {
- Principal = $"User:{producer.Name}",
- Host = "*",
- Operation = AclOperation.Write,
- PermissionType = AclPermissionType.Allow
- }
- }
- };
- await _servicekafkaAdmin.CreateAclsAsync(aclBindings);
- return ApiResponse<string>.Success($"创建 生产者用户 {producer.Name} Acls 规则成功");
- }
-
- // DELETE api/<ScramAclsController>/{username}
- [HttpDelete("{username}")]
- public async Task<ApiResponse<string>> Delete(string username)
- {
- // 删除用户
- var scramUsers = await _servicekafkaAdmin.DescribeUserScramCredentialsAsync(new List<string>
- {
- username
- });
- if (scramUsers.Count==1)
- {
- ScramCredentialsUser scramUser = new()
- {
- Name = username
- };
- await _servicekafkaAdmin.AlterUserScramCredentialsAsync(scramUser, "DELETE");
- }
-
- // 删除alcs
- var acls = await _servicekafkaAdmin.DescribeAclsAsync();
- var userAclsBinding = acls.Where(i => i.Entry.Principal.EndsWith(username)).ToList();
- if (userAclsBinding.Count>0)
- {
- await _servicekafkaAdmin.DeleteAclsAsync(userAclsBinding);
- }
-
- return ApiResponse<string>.Success($"删除用户 {username} 和 Acls 规则成功");
-
- }
- }
- }
|