using System.CommandLine; using System.CommandLine.Parsing; using ScadaLink.Commons.Messages.Management; namespace ScadaLink.CLI.Commands; public static class SecurityCommands { public static Command Build(Option contactPointsOption, Option formatOption) { var command = new Command("security") { Description = "Manage security settings" }; command.Add(BuildApiKey(contactPointsOption, formatOption)); command.Add(BuildRoleMapping(contactPointsOption, formatOption)); return command; } private static Command BuildApiKey(Option contactPointsOption, Option formatOption) { var group = new Command("api-key") { Description = "Manage API keys" }; var listCmd = new Command("list") { Description = "List all API keys" }; listCmd.SetAction(async (ParseResult result) => { return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new ListApiKeysCommand()); }); group.Add(listCmd); var nameOption = new Option("--name") { Description = "API key name", Required = true }; var createCmd = new Command("create") { Description = "Create an API key" }; createCmd.Add(nameOption); createCmd.SetAction(async (ParseResult result) => { var name = result.GetValue(nameOption)!; return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new CreateApiKeyCommand(name)); }); group.Add(createCmd); var idOption = new Option("--id") { Description = "API key ID", Required = true }; var deleteCmd = new Command("delete") { Description = "Delete an API key" }; deleteCmd.Add(idOption); deleteCmd.SetAction(async (ParseResult result) => { var id = result.GetValue(idOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new DeleteApiKeyCommand(id)); }); group.Add(deleteCmd); return group; } private static Command BuildRoleMapping(Option contactPointsOption, Option formatOption) { var group = new Command("role-mapping") { Description = "Manage LDAP role mappings" }; var listCmd = new Command("list") { Description = "List all role mappings" }; listCmd.SetAction(async (ParseResult result) => { return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new ListRoleMappingsCommand()); }); group.Add(listCmd); var ldapGroupOption = new Option("--ldap-group") { Description = "LDAP group name", Required = true }; var roleOption = new Option("--role") { Description = "Role name", Required = true }; var createCmd = new Command("create") { Description = "Create a role mapping" }; createCmd.Add(ldapGroupOption); createCmd.Add(roleOption); createCmd.SetAction(async (ParseResult result) => { var ldapGroup = result.GetValue(ldapGroupOption)!; var role = result.GetValue(roleOption)!; return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new CreateRoleMappingCommand(ldapGroup, role)); }); group.Add(createCmd); var idOption = new Option("--id") { Description = "Mapping ID", Required = true }; var deleteCmd = new Command("delete") { Description = "Delete a role mapping" }; deleteCmd.Add(idOption); deleteCmd.SetAction(async (ParseResult result) => { var id = result.GetValue(idOption); return await CommandHelpers.ExecuteCommandAsync( result, contactPointsOption, formatOption, new DeleteRoleMappingCommand(id)); }); group.Add(deleteCmd); return group; } }