From 5f48d41c3816849f410959bd6ee6e480a5107185 Mon Sep 17 00:00:00 2001 From: Andrew McClenaghan Date: Sun, 25 Feb 2018 11:30:44 +1000 Subject: [PATCH 1/3] Initial work on API Key management --- src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs | 50 +++++++++++++++++++ .../Cli/Commands/ApiKey/RemoveCommand.cs | 47 +++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs create mode 100644 src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs diff --git a/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs b/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs new file mode 100644 index 00000000..19e33480 --- /dev/null +++ b/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs @@ -0,0 +1,50 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Newtonsoft.Json; +using SeqCli.Cli.Features; +using SeqCli.Connection; +using Serilog; + +namespace SeqCli.Cli.Commands.ApiKey +{ + [Command("apikey", "list", "Send a structured log event to the server", Example = + "seqcli log -m 'Hello, {Name}!' -p Name=World -p App=Test")] + class ListCommand : Command + { + private readonly SeqConnectionFactory _connectionFactory; + private readonly ConnectionFeature _connection; + + public ListCommand(SeqConnectionFactory connectionFactory) + { + _connectionFactory = connectionFactory; + _connection = Enable(); + } + + protected override async Task Run() + { + var connection = _connectionFactory.Connect(_connection); + + var apiKeys = await connection.ApiKeys.ListAsync(); + Log.Debug("Retrieved ApiKeys {@ApiKeys}", apiKeys); + var data = apiKeys.Select(a => new + { + a.AppliedProperties, + a.CanActAsPrincipal, + a.InputFilter, + a.Title, + a.Token, + a.UseServerTimestamps, + a.MinimumLevel, + a.IsDefault + }); + foreach (var apiKey in data) + { + var apiKeyString = JsonConvert.SerializeObject(apiKey); + + Console.WriteLine(apiKeyString); + } + return 0; + } + } +} diff --git a/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs b/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs new file mode 100644 index 00000000..f3254e97 --- /dev/null +++ b/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Seq.Api.Model.Inputs; +using SeqCli.Cli.Features; +using SeqCli.Connection; + +namespace SeqCli.Cli.Commands.ApiKey +{ + [Command("apikey", "remove", "Send a structured log event to the server", Example = + "seqcli log -m 'Hello, {Name}!' -p Name=World -p App=Test")] + class RemoveCommand : Command + { + private readonly SeqConnectionFactory _connectionFactory; + private readonly ConnectionFeature _connection; + private string _title; + + public RemoveCommand(SeqConnectionFactory connectionFactory) + { + _connectionFactory = connectionFactory; + _connection = Enable(); + Options.Add( + "t=|title=", + "", + (t) => _title = t); + } + + protected override async Task Run() + { + var connection = _connectionFactory.Connect(_connection); + + var apiKeys = await connection.ApiKeys.ListAsync(); + var apiKeyToRemove = apiKeys.FirstOrDefault(ak => ak.Title == _title); + if (apiKeyToRemove == null) + { + Console.WriteLine($"\"{_title}\" API Key doesn't exist"); + return -1; + } + + await connection.ApiKeys.RemoveAsync(apiKeyToRemove); + Console.WriteLine($"\"{_title}\" API Key removed"); + return 0; + } + } +} From 3e7951852e9a8a2b0c1d9c53fc5a9e5e72203a21 Mon Sep 17 00:00:00 2001 From: Andrew McClenaghan Date: Mon, 26 Feb 2018 22:58:35 +1000 Subject: [PATCH 2/3] Fixed up to be more in line with the rest of the project based on feedback --- src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs | 10 ++++--- .../Cli/Commands/ApiKey/RemoveCommand.cs | 28 ++++++++++++++----- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs b/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs index 19e33480..8c6a02b6 100644 --- a/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs +++ b/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs @@ -9,7 +9,7 @@ namespace SeqCli.Cli.Commands.ApiKey { [Command("apikey", "list", "Send a structured log event to the server", Example = - "seqcli log -m 'Hello, {Name}!' -p Name=World -p App=Test")] + "seqcli apikey list")] class ListCommand : Command { private readonly SeqConnectionFactory _connectionFactory; @@ -29,15 +29,17 @@ protected override async Task Run() Log.Debug("Retrieved ApiKeys {@ApiKeys}", apiKeys); var data = apiKeys.Select(a => new { + a.Title, + a.Id, + a.Token, + a.MinimumLevel, a.AppliedProperties, a.CanActAsPrincipal, a.InputFilter, - a.Title, - a.Token, a.UseServerTimestamps, - a.MinimumLevel, a.IsDefault }); + foreach (var apiKey in data) { var apiKeyString = JsonConvert.SerializeObject(apiKey); diff --git a/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs b/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs index f3254e97..84b43b2f 100644 --- a/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs +++ b/src/SeqCli/Cli/Commands/ApiKey/RemoveCommand.cs @@ -9,13 +9,14 @@ namespace SeqCli.Cli.Commands.ApiKey { - [Command("apikey", "remove", "Send a structured log event to the server", Example = - "seqcli log -m 'Hello, {Name}!' -p Name=World -p App=Test")] + [Command("apikey", "remove", "Remove API Key from the server", Example = + "seqcli apikey remove -t TestApiKey")] class RemoveCommand : Command { private readonly SeqConnectionFactory _connectionFactory; private readonly ConnectionFeature _connection; private string _title; + private string _id; public RemoveCommand(SeqConnectionFactory connectionFactory) { @@ -23,24 +24,37 @@ public RemoveCommand(SeqConnectionFactory connectionFactory) _connection = Enable(); Options.Add( "t=|title=", - "", + "Remove API Keys with the specified title", (t) => _title = t); + + Options.Add( + "i=|id=", + "Remove API Keys with the specified Id", + (t) => _id = t); } protected override async Task Run() { + if (_title != default && _id != default) + { + Console.WriteLine("You can only specify \"title\" or \"id\" not both"); + return -1; + } + var connection = _connectionFactory.Connect(_connection); var apiKeys = await connection.ApiKeys.ListAsync(); - var apiKeyToRemove = apiKeys.FirstOrDefault(ak => ak.Title == _title); - if (apiKeyToRemove == null) + var apiKeyToRemove = apiKeys.Where(ak => ak.Title == _title || ak.Id == _id).ToList(); + if (!apiKeyToRemove.Any()) { Console.WriteLine($"\"{_title}\" API Key doesn't exist"); return -1; } - await connection.ApiKeys.RemoveAsync(apiKeyToRemove); - Console.WriteLine($"\"{_title}\" API Key removed"); + foreach (var apiKeyEntity in apiKeyToRemove) + { + await connection.ApiKeys.RemoveAsync(apiKeyEntity); + } return 0; } } From 84012f49e07b791dd02cd19e783eb2bc87a93666 Mon Sep 17 00:00:00 2001 From: Andrew McClenaghan Date: Wed, 28 Feb 2018 06:30:43 +1000 Subject: [PATCH 3/3] Fix up help text for apikey list command --- src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs b/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs index 8c6a02b6..393c9bb7 100644 --- a/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs +++ b/src/SeqCli/Cli/Commands/ApiKey/ListCommand.cs @@ -8,7 +8,7 @@ namespace SeqCli.Cli.Commands.ApiKey { - [Command("apikey", "list", "Send a structured log event to the server", Example = + [Command("apikey", "list", "List of API Keys", Example = "seqcli apikey list")] class ListCommand : Command {