Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add revision retention rule to keep last 10 revisions (#254)

Leendert de Borstb6cfc03b01d9
diff --git a/src/AliasVault.Admin/Main/Pages/Users/View.razor b/src/AliasVault.Admin/Main/Pages/Users/View.razor
index 753b09223..9890718bd 100644
--- a/src/AliasVault.Admin/Main/Pages/Users/View.razor
+++ b/src/AliasVault.Admin/Main/Pages/Users/View.razor
@@ -64,93 +64,93 @@ else
<div class="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 sm:p-6 dark:bg-gray-800">
<div class="items-center">
<div>
- <h3 class="mb-1 text-xl font-bold text-gray-900 dark:text-white">UserRefreshTokens (Logged in devices)</h3>
+ <h3 class="mb-1 text-xl font-bold text-gray-900 dark:text-white">Vaults</h3>
<table class="w-full text-sm text-left text-gray-500 shadow rounded border">
<thead class="text-xs text-gray-700 uppercase bg-gray-50">
<tr>
<th scope="col" class="px-4 py-3">ID</th>
- <th scope="col" class="px-4 py-3">Device</th>
- <th scope="col" class="px-4 py-3">Ip</th>
<th scope="col" class="px-4 py-3">Created</th>
- <th scope="col" class="px-4 py-3">Expires</th>
- <th scope="col" class="px-4 py-3"></th>
+ <th scope="col" class="px-4 py-3">Updated</th>
+ <th scope="col" class="px-4 py-3">Filesize</th>
+ <th scope="col" class="px-4 py-3">DB version</th>
+ <th scope="col" class="px-4 py-3">Revision</th>
+ <th scope="col" class="px-4 py-3">Credentials</th>
+ <th scope="col" class="px-4 py-3">Email Claims</th>
+ <th scope="col" class="px-4 py-3">Status</th>
+ <th scope="col" class="px-4 py-3">Actions</th>
</tr>
</thead>
<tbody>
- @foreach (var entry in RefreshTokenList)
+ @{
+ Vault? previousEntry = null;
+ }
+ @foreach (var entry in VaultList.OrderBy(e => e.UpdatedAt))
{
- <tr class="bg-white border-b hover:bg-gray-50">
- <td class="px-4 py-3 font-medium text-gray-900">@entry.Id</td>
- <td class="px-4 py-3">@entry.DeviceIdentifier</td>
- <td class="px-4 py-3">@entry.IpAddress</td>
- <td class="px-4 py-3">@entry.CreatedAt.ToString("yyyy-MM-dd HH:mm")</td>
- <td class="px-4 py-3">@entry.ExpireDate.ToString("yyyy-MM-dd HH:mm")</td>
- <td class="px-4 py-3">
- <button @onclick="() => RevokeRefreshToken(entry)" class="px-3 py-2 text-xs font-medium text-center text-white bg-red-700 rounded-lg hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800">Revoke</button>
- </td>
- </tr>
+ <tr class="bg-white border-b hover:bg-gray-50">
+ <td class="px-4 py-3 font-medium text-gray-900">@entry.Id</td>
+ <td class="px-4 py-3">@entry.CreatedAt.ToString("yyyy-MM-dd HH:mm")</td>
+ <td class="px-4 py-3">@entry.UpdatedAt.ToString("yyyy-MM-dd HH:mm")</td>
+ <td class="px-4 py-3">@Math.Round((double)entry.FileSize / 1024, 1) MB</td>
+ <td class="px-4 py-3">@entry.Version</td>
+ <td class="px-4 py-3">@entry.RevisionNumber</td>
+ <td class="px-4 py-3">@entry.CredentialsCount</td>
+ <td class="px-4 py-3">@entry.EmailClaimsCount</td>
+ <td class="px-4 py-3">
+ @if (entry == LatestVault)
+ {
+ <span class="bg-blue-100 text-blue-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded">Current</span>
+ }
+ @if (previousEntry != null && HasPasswordChanged(entry, previousEntry))
+ {
+ <span class="bg-green-100 text-green-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded">Password Changed</span>
+ }
+ </td>
+ <td class="px-4 py-3">
+ @if (entry != LatestVault)
+ {
+ <Button OnClick="() => MakeCurrentAsync(entry)">Make Current</Button>
+ }
+ </td>
+ </tr>
+
+ previousEntry = entry;
}
</tbody>
</table>
</div>
</div>
</div>
-
+
<div class="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm dark:border-gray-700 sm:p-6 dark:bg-gray-800">
<div class="items-center">
<div>
- <h3 class="mb-1 text-xl font-bold text-gray-900 dark:text-white">Vaults</h3>
+ <h3 class="mb-1 text-xl font-bold text-gray-900 dark:text-white">UserRefreshTokens (Logged in devices)</h3>
<table class="w-full text-sm text-left text-gray-500 shadow rounded border">
<thead class="text-xs text-gray-700 uppercase bg-gray-50">
<tr>
<th scope="col" class="px-4 py-3">ID</th>
+ <th scope="col" class="px-4 py-3">Device</th>
+ <th scope="col" class="px-4 py-3">Ip</th>
<th scope="col" class="px-4 py-3">Created</th>
- <th scope="col" class="px-4 py-3">Updated</th>
- <th scope="col" class="px-4 py-3">Filesize</th>
- <th scope="col" class="px-4 py-3">DB version</th>
- <th scope="col" class="px-4 py-3">Revision</th>
- <th scope="col" class="px-4 py-3">Credentials</th>
- <th scope="col" class="px-4 py-3">Email Claims</th>
- <th scope="col" class="px-4 py-3">Status</th>
- <th scope="col" class="px-4 py-3">Actions</th>
+ <th scope="col" class="px-4 py-3">Expires</th>
+ <th scope="col" class="px-4 py-3"></th>
</tr>
</thead>
<tbody>
- @{
- Vault? previousEntry = null;
- }
- @foreach (var entry in VaultList.OrderBy(e => e.UpdatedAt))
+ @foreach (var entry in RefreshTokenList)
{
<tr class="bg-white border-b hover:bg-gray-50">
<td class="px-4 py-3 font-medium text-gray-900">@entry.Id</td>
+ <td class="px-4 py-3">@entry.DeviceIdentifier</td>
+ <td class="px-4 py-3">@entry.IpAddress</td>
<td class="px-4 py-3">@entry.CreatedAt.ToString("yyyy-MM-dd HH:mm")</td>
- <td class="px-4 py-3">@entry.UpdatedAt.ToString("yyyy-MM-dd HH:mm")</td>
- <td class="px-4 py-3">@Math.Round((double)entry.FileSize / 1024, 1) MB</td>
- <td class="px-4 py-3">@entry.Version</td>
- <td class="px-4 py-3">@entry.RevisionNumber</td>
- <td class="px-4 py-3">@entry.CredentialsCount</td>
- <td class="px-4 py-3">@entry.EmailClaimsCount</td>
- <td class="px-4 py-3">
- @if (entry == LatestVault)
- {
- <span class="bg-blue-100 text-blue-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded">Current</span>
- }
- @if (previousEntry != null && HasPasswordChanged(entry, previousEntry))
- {
- <span class="bg-green-100 text-green-800 text-xs font-medium mr-2 px-2.5 py-0.5 rounded">Password Changed</span>
- }
- </td>
+ <td class="px-4 py-3">@entry.ExpireDate.ToString("yyyy-MM-dd HH:mm")</td>
<td class="px-4 py-3">
- @if (entry != LatestVault)
- {
- <Button OnClick="() => MakeCurrentAsync(entry)">Make Current</Button>
- }
+ <button @onclick="() => RevokeRefreshToken(entry)" class="px-3 py-2 text-xs font-medium text-center text-white bg-red-700 rounded-lg hover:bg-red-800 focus:ring-4 focus:outline-none focus:ring-red-300 dark:bg-red-600 dark:hover:bg-red-700 dark:focus:ring-red-800">Revoke</button>
</td>
</tr>
-
- previousEntry = entry;
}
</tbody>
</table>
diff --git a/src/AliasVault.Api/Controllers/VaultController.cs b/src/AliasVault.Api/Controllers/VaultController.cs
index 7f7a2d989..daa962248 100644
--- a/src/AliasVault.Api/Controllers/VaultController.cs
+++ b/src/AliasVault.Api/Controllers/VaultController.cs
@@ -50,10 +50,11 @@ public class VaultController(ILogger<VaultController> logger, IDbContextFactory<
{
Rules =
[
+ new RevisionRetentionRule { RevisionsToKeep = 10 },
new DailyRetentionRule { DaysToKeep = 3 },
new WeeklyRetentionRule { WeeksToKeep = 1 },
new MonthlyRetentionRule { MonthsToKeep = 1 },
- new VersionRetentionRule { VersionsToKeep = 3 },
+ new DbVersionRetentionRule { VersionsToKeep = 3 },
new LoginCredentialRetentionRule { CredentialsToKeep = 2 },
],
};
diff --git a/src/AliasVault.Api/Vault/RetentionRules/VersionRetentionRule.cs b/src/AliasVault.Api/Vault/RetentionRules/DbVersionRetentionRule.cs
similarity index 77%
rename from src/AliasVault.Api/Vault/RetentionRules/VersionRetentionRule.cs
rename to src/AliasVault.Api/Vault/RetentionRules/DbVersionRetentionRule.cs
index 776c4d89f..5c4390c8c 100644
--- a/src/AliasVault.Api/Vault/RetentionRules/VersionRetentionRule.cs
+++ b/src/AliasVault.Api/Vault/RetentionRules/DbVersionRetentionRule.cs
@@ -1,5 +1,5 @@
//-----------------------------------------------------------------------
-// <copyright file="VersionRetentionRule.cs" company="lanedirt">
+// <copyright file="DbVersionRetentionRule.cs" company="lanedirt">
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
@@ -10,12 +10,12 @@ namespace AliasVault.Api.Vault.RetentionRules;
using AliasServerDb;
/// <summary>
-/// Version retention rule that keeps the latest X unique versions of the vault.
+/// Version retention rule that keeps the latest X unique db versions of the vault.
/// </summary>
-public class VersionRetentionRule : IRetentionRule
+public class DbVersionRetentionRule : IRetentionRule
{
/// <summary>
- /// Gets or sets amount of versions to keep the vault.
+ /// Gets or sets amount of db versions to keep the vault.
/// </summary>
public int VersionsToKeep { get; set; }
diff --git a/src/AliasVault.Api/Vault/RetentionRules/RevisionRetentionRule.cs b/src/AliasVault.Api/Vault/RetentionRules/RevisionRetentionRule.cs
new file mode 100644
index 000000000..eef5081df
--- /dev/null
+++ b/src/AliasVault.Api/Vault/RetentionRules/RevisionRetentionRule.cs
@@ -0,0 +1,32 @@
+//-----------------------------------------------------------------------
+// <copyright file="RevisionRetentionRule.cs" company="lanedirt">
+// Copyright (c) lanedirt. All rights reserved.
+// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Api.Vault.RetentionRules;
+
+using AliasServerDb;
+
+/// <summary>
+/// Revision retention rule that keeps the latest X unique revisions of the vault.
+/// </summary>
+public class RevisionRetentionRule : IRetentionRule
+{
+ /// <summary>
+ /// Gets or sets amount of revisions to keep the vault.
+ /// </summary>
+ public int RevisionsToKeep { get; set; }
+
+ /// <inheritdoc cref="IRetentionRule.ApplyRule"/>
+ public IEnumerable<Vault> ApplyRule(List<Vault> vaults, DateTime now)
+ {
+ // For the specified amount of versions, take last vault per version.
+ return vaults
+ .GroupBy(x => x.RevisionNumber)
+ .Select(g => g.OrderByDescending(x => x.UpdatedAt).First())
+ .OrderByDescending(x => x.UpdatedAt)
+ .Take(RevisionsToKeep);
+ }
+}
diff --git a/src/Tests/AliasVault.UnitTests/Vault/RetentionManager/GeneralRetentionTests.cs b/src/Tests/AliasVault.UnitTests/Vault/RetentionManager/GeneralRetentionTests.cs
index f572e95fa..63bf88d4a 100644
--- a/src/Tests/AliasVault.UnitTests/Vault/RetentionManager/GeneralRetentionTests.cs
+++ b/src/Tests/AliasVault.UnitTests/Vault/RetentionManager/GeneralRetentionTests.cs
@@ -30,15 +30,15 @@ public class GeneralRetentionTests
now = new DateTime(2023, 6, 1, 12, 0, 0); // Set a fixed "now" date for testing: June 1, 2023, 12:00 PM
testVaults =
[
- new Vault { Version = "1.1.0", UpdatedAt = new DateTime(2023, 5, 31, 12, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 },
- new Vault { Version = "1.1.0", UpdatedAt = new DateTime(2023, 5, 31, 4, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 },
- new Vault { Version = "1.1.0", UpdatedAt = new DateTime(2023, 5, 30, 12, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 }, // 2 days ago
- new Vault { Version = "1.1.0", UpdatedAt = new DateTime(2023, 5, 29, 12, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 }, // 3 days ago
- new Vault { Version = "1.0.3", UpdatedAt = new DateTime(2023, 5, 28, 12, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 }, // 4 days ago
- new Vault { Version = "1.0.3", UpdatedAt = new DateTime(2023, 5, 18, 12, 0, 0), Salt = "def", Verifier = "def", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 }, // 2 weeks ago
- new Vault { Version = "1.0.3", UpdatedAt = new DateTime(2023, 5, 11, 12, 0, 0), Salt = "def", Verifier = "def", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 }, // 3 weeks ago
- new Vault { Version = "1.0.2", UpdatedAt = new DateTime(2023, 5, 1, 12, 0, 0), Salt = "def", Verifier = "def", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 }, // 1 month ago
- new Vault { Version = "1.0.1", UpdatedAt = new DateTime(2023, 4, 1, 12, 0, 0), Salt = "ghi", Verifier = "ghi", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 0 }, // 2 months ago
+ new Vault { Version = "1.1.0", UpdatedAt = new DateTime(2023, 5, 31, 12, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 9 },
+ new Vault { Version = "1.1.0", UpdatedAt = new DateTime(2023, 5, 31, 4, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 8 },
+ new Vault { Version = "1.1.0", UpdatedAt = new DateTime(2023, 5, 30, 12, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 7 }, // 2 days ago
+ new Vault { Version = "1.1.0", UpdatedAt = new DateTime(2023, 5, 29, 12, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 6 }, // 3 days ago
+ new Vault { Version = "1.0.3", UpdatedAt = new DateTime(2023, 5, 28, 12, 0, 0), Salt = "abc", Verifier = "abc", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 5 }, // 4 days ago
+ new Vault { Version = "1.0.3", UpdatedAt = new DateTime(2023, 5, 18, 12, 0, 0), Salt = "def", Verifier = "def", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 4 }, // 2 weeks ago
+ new Vault { Version = "1.0.3", UpdatedAt = new DateTime(2023, 5, 11, 12, 0, 0), Salt = "def", Verifier = "def", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 3 }, // 3 weeks ago
+ new Vault { Version = "1.0.2", UpdatedAt = new DateTime(2023, 5, 1, 12, 0, 0), Salt = "def", Verifier = "def", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 2 }, // 1 month ago
+ new Vault { Version = "1.0.1", UpdatedAt = new DateTime(2023, 4, 1, 12, 0, 0), Salt = "ghi", Verifier = "ghi", VaultBlob = string.Empty, EncryptionType = string.Empty, EncryptionSettings = string.Empty, RevisionNumber = 1 }, // 2 months ago
];
}
@@ -102,12 +102,12 @@ public class GeneralRetentionTests
}
/// <summary>
- /// Test the VersionRetentionRule.
+ /// Test the DbVersionRetentionRule.
/// </summary>
[Test]
public void VersionRetentionRuleTest()
{
- var rule = new VersionRetentionRule { VersionsToKeep = 2 };
+ var rule = new DbVersionRetentionRule { VersionsToKeep = 2 };
var result = rule.ApplyRule(testVaults, now).ToList();
Assert.Multiple(() =>
@@ -140,6 +140,25 @@ public class GeneralRetentionTests
});
}
+ /// <summary>
+ /// Test the RevisionRetentionRule.
+ /// </summary>
+ [Test]
+ public void RevisionRetentionRuleTest()
+ {
+ // Keep the last 5 unique revisions.
+ var rule = new RevisionRetentionRule { RevisionsToKeep = 5 };
+ var result = rule.ApplyRule(testVaults, now).ToList();
+
+ // Expecting five vaults to be kept: the latest 5 unique revisions (5-9).
+ Assert.Multiple(() =>
+ {
+ Assert.That(result, Has.Count.EqualTo(5));
+ Assert.That(result.Min(x => x.RevisionNumber), Is.EqualTo(5));
+ Assert.That(result.Max(x => x.RevisionNumber), Is.EqualTo(9));
+ });
+ }
+
/// <summary>
/// Test the RetentionPolicy object.
/// </summary>
@@ -150,10 +169,11 @@ public class GeneralRetentionTests
{
Rules = new List<IRetentionRule>
{
+ new RevisionRetentionRule { RevisionsToKeep = 1 },
new DailyRetentionRule { DaysToKeep = 2 },
new WeeklyRetentionRule { WeeksToKeep = 2 },
new MonthlyRetentionRule { MonthsToKeep = 1 },
- new VersionRetentionRule { VersionsToKeep = 3 },
+ new DbVersionRetentionRule { VersionsToKeep = 3 },
},
};