-
Notifications
You must be signed in to change notification settings - Fork 28
Add support for Listing and Removing API Keys #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| 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", "List of API Keys", Example = | ||
| "seqcli apikey list")] | ||
| class ListCommand : Command | ||
| { | ||
| private readonly SeqConnectionFactory _connectionFactory; | ||
| private readonly ConnectionFeature _connection; | ||
|
|
||
| public ListCommand(SeqConnectionFactory connectionFactory) | ||
| { | ||
| _connectionFactory = connectionFactory; | ||
| _connection = Enable<ConnectionFeature>(); | ||
| } | ||
|
|
||
| protected override async Task<int> 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.Title, | ||
| a.Id, | ||
| a.Token, | ||
| a.MinimumLevel, | ||
| a.AppliedProperties, | ||
| a.CanActAsPrincipal, | ||
| a.InputFilter, | ||
| a.UseServerTimestamps, | ||
| a.IsDefault | ||
| }); | ||
|
|
||
| foreach (var apiKey in data) | ||
| { | ||
| var apiKeyString = JsonConvert.SerializeObject(apiKey); | ||
|
|
||
| Console.WriteLine(apiKeyString); | ||
| } | ||
| return 0; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| 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", "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) | ||
| { | ||
| _connectionFactory = connectionFactory; | ||
| _connection = Enable<ConnectionFeature>(); | ||
| 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<int> 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.Where(ak => ak.Title == _title || ak.Id == _id).ToList(); | ||
| if (!apiKeyToRemove.Any()) | ||
| { | ||
| Console.WriteLine($"\"{_title}\" API Key doesn't exist"); | ||
| return -1; | ||
| } | ||
|
|
||
| foreach (var apiKeyEntity in apiKeyToRemove) | ||
| { | ||
| await connection.ApiKeys.RemoveAsync(apiKeyEntity); | ||
| } | ||
| return 0; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
ida worthwhile option/alternative to title? Would help with the ambiguous ones where names overlap.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have added
Idand put a check in to ensure that they only specify a single value since both could be confusing as to if its removing only when both match or just one.