Skip to content

Commit

Permalink
feat(command): a command that deletes old keys (#44)
Browse files Browse the repository at this point in the history
use --db archive_keys --key-ttl-hours <hours> to archive older keys
  • Loading branch information
cyberhck authored Aug 11, 2022
1 parent 37c612a commit d758806
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 4 deletions.
8 changes: 4 additions & 4 deletions API/API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HotChocolate.ApolloFederation" Version="12.12.1"/>
<PackageReference Include="HotChocolate.AspNetCore" Version="12.12.1"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0"/>
<PackageReference Include="HotChocolate.ApolloFederation" Version="12.12.1" />
<PackageReference Include="HotChocolate.AspNetCore" Version="12.12.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Business\Business.csproj"/>
<ProjectReference Include="..\Business\Business.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions Business.Tests/KeyServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,17 @@ public async Task FetchAllKeysTest()
Assert.AreEqual("2", enumerable.ElementAt(1).Body);
Assert.AreEqual("key_2", enumerable.ElementAt(1).Id);
}

[Test]
public async Task CleanupKeysTest()
{
var mockKeyRepo = new Mock<IKeyRepository>();
mockKeyRepo.Setup(x => x.CleanupKeys(It.IsAny<DateTime>())).Returns((DateTime x) =>
{
Assert.AreEqual(-1, x.Subtract(DateTime.Now).Hours);
return Task.CompletedTask;
});
var service = new KeyService(mockKeyRepo.Object);
await service.CleanupKeys(1);
}
}
6 changes: 6 additions & 0 deletions Business/KeyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IKeyService
{
Task<IEnumerable<Key>> FetchAllKeys();
Task<Key> Create(string publicKey);
Task CleanupKeys(int hoursBefore);
}

public class KeyService : IKeyService
Expand All @@ -31,4 +32,9 @@ public async Task<Key> Create(string publicKey)
});
return key.ToViewModel();
}

public Task CleanupKeys(int hoursBefore)
{
return _keyRepository.CleanupKeys(DateTime.Now.AddHours(-hoursBefore).ToUniversalTime());
}
}
9 changes: 9 additions & 0 deletions Startup/Cli.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using API;
using Business;
using Microsoft.EntityFrameworkCore;
using Startup.StartupExtensions;
using Storage;
Expand Down Expand Up @@ -33,6 +34,14 @@ public static void StartServer(string[] args)
app.Run();
}

public static void CleanupKeys(string[] args, int hoursBefore)
{
GetWebHost(args)
.Services
.GetRequiredService<IKeyService>()
.CleanupKeys(hoursBefore);
}

public static void Migrate(string[] args)
{
GetWebHost(args)
Expand Down
9 changes: 9 additions & 0 deletions Startup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{
Cli.StartServer(args);
}

var rootCommand = new RootCommand("key management service");

var serverSubcommand = new Command("server", "manage server");
Expand All @@ -23,6 +24,14 @@
var dbCommand = new Command("db", "manage database");
var dbMigrateCommand = new Command("migrate", "run migrations");
dbMigrateCommand.SetHandler(() => { Cli.Migrate(args); });
var dbArchiveCommand = new Command("archive_keys", "archive old keys");
var keyTtlHours = new Option<int>("--key-ttl-hours", () => 72);
dbArchiveCommand.AddOption(keyTtlHours);
dbArchiveCommand.SetHandler((hoursBefore) =>
{
Cli.CleanupKeys(args, hoursBefore);
}, keyTtlHours);
dbCommand.AddCommand(dbArchiveCommand);

dbCommand.AddCommand(dbMigrateCommand);
rootCommand.AddCommand(dbCommand);
Expand Down
11 changes: 11 additions & 0 deletions Startup/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
"ASPNETCORE_ENVIRONMENT": "Development"
},
"commandLineArgs": "server start"
},
"ArchiveKey": {
"commandName": "Project",
"dotnetRunMessages": false,
"launchBrowser": false,
"launchUrl": "/graphql",
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"commandLineArgs": "db archive_keys --key-ttl-hours 72"
}
}
}
7 changes: 7 additions & 0 deletions Storage/KeyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IKeyRepository
{
Task<IEnumerable<Key>> FindAllKeys();
Task<Key> Create(Key key);
Task CleanupKeys(DateTime createdBefore);
}

public class KeyRepository : IKeyRepository
Expand All @@ -28,4 +29,10 @@ public async Task<Key> Create(Key key)
await _db.SaveChangesAsync();
return entity.Entity;
}

public Task CleanupKeys(DateTime createdBefore)
{
_db.Keys.RemoveRange(_db.Keys.Where(x => x.CreatedAt < createdBefore));
return _db.SaveChangesAsync();
}
}

0 comments on commit d758806

Please sign in to comment.