Add TestFingerprintCommand

This commit is contained in:
2021-06-07 18:37:45 +02:00
parent 35c1665950
commit 1ec090eca4

View File

@@ -1,4 +1,5 @@
using Akari.Prototype.Server.Services;
using Akari.Prototype.Server.Options;
using Akari.Prototype.Server.Services;
using Akari.Prototype.Server.Utils;
using Akari.Prototype.Server.Utils.Extensions;
using CliFx;
@@ -6,12 +7,17 @@ using CliFx.Attributes;
using CliFx.Exceptions;
using CliFx.Extensibility;
using CliFx.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Spectre.Console;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Akari.Prototype.Server.Cli.Commands
@@ -46,6 +52,11 @@ namespace Akari.Prototype.Server.Cli.Commands
throw new CommandException("Name can't be more than 200 bytes (UTF-8)");
}
if (Name.Contains('$'))
{
throw new CommandException("Name can't contain a '$'");
}
if (_fingerprintManager.Contains(Name))
{
throw new CommandException("A fingerprint with this name is already registered.");
@@ -88,12 +99,66 @@ namespace Akari.Prototype.Server.Cli.Commands
throw new CommandException($"The fingerprint {Name} doesn't exist");
}
console.AsAnsiConsole().Markup($"[green]Successfully deleted {Name}[/]");
console.AsAnsiConsole().MarkupLine($"[green]Successfully deleted {Name}[/]");
return default;
}
}
[Command("fp t", Description = "Test a fingerprint")]
public class TestFingerprintCommand : ICommand
{
public const int AuthTimeout = 5_000;
[CommandParameter(0, Description = "The fingerprint name")]
public string Name { get; init; }
private readonly IServiceProvider _serviceProvider;
private readonly IAuthManager _authManager;
private readonly IFingerprintManager _fingerprintManager;
public TestFingerprintCommand(IServiceProvider serviceProvider, IAuthManager authManager, IFingerprintManager fingerprintManager)
{
_serviceProvider = serviceProvider;
_authManager = authManager;
_fingerprintManager = fingerprintManager;
}
public async ValueTask ExecuteAsync(IConsole console)
{
var ansiConsole = console.AsAnsiConsole();
if (!_fingerprintManager.Contains(Name))
{
throw new CommandException("This fingerprint does not exist");
}
using var scope = _serviceProvider.CreateScope();
var services = scope.ServiceProvider;
using var provider = new TcpProviderService(services.GetRequiredService<ILogger<TcpProviderService>>(),
services.GetRequiredService<IOptions<TcpProviderOptions>>(),
services.GetRequiredService<IHostApplicationLifetime>(),
services.GetRequiredService<IFingerprintManager>());
var source = new CancellationTokenSource(AuthTimeout);
await provider.StartAsync(source.Token);
ansiConsole.MarkupLine($"Waiting for [yellow]{Name}[/] auth");
await Task.Delay(AuthTimeout);
if (!_authManager.TryGetKey(Name, out _))
{
throw new CommandException("Timed out while waiting for fingerprint auth");
}
ansiConsole.MarkupLine($"[green]Successfully retrieved '{Name}' key[/]");
}
}
public FingerprintCommands(IFingerprintManager fingerprintManager)
{
_fingerprintManager = fingerprintManager;