Sign agent releases and complete snapshot media lifecycle
This commit is contained in:
1 parent
07b29b1e8e
commit
d93eaca043
30 files changed
+954
-95
No files matched your search
@@ -41,7 +41,7 @@ public class UpdatesController : ControllerBase
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
[Authorize(AuthenticationSchemes = "JwtBearer,Cookies", Policy = AppPermissions.SiteRead)]
|
||||
[AllowAnonymous]
|
||||
[HttpGet("latest")]
|
||||
[ProducesResponseType(typeof(LatestReleaseResponse), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetLatestRelease(CancellationToken ct)
|
||||
@@ -123,6 +123,15 @@ public class UpdatesController : ControllerBase
|
||||
var image = await FetchAgentImageManifestAsync(release.Assets, client, ct);
|
||||
result.ContainerImage = image?.Image;
|
||||
result.ContainerImageDigest = image?.Digest;
|
||||
var manifestBytes = await FetchBoundedReleaseAssetAsync(release.Assets,
|
||||
"agent-update-manifest.json", client, 1_048_576, ct);
|
||||
var signatureBytes = await FetchBoundedReleaseAssetAsync(release.Assets,
|
||||
"agent-update-manifest.sig", client, 512, ct);
|
||||
if (manifestBytes is not null && signatureBytes is not null)
|
||||
{
|
||||
result.SignedManifest = Convert.ToBase64String(manifestBytes);
|
||||
result.ManifestSignature = Convert.ToBase64String(signatureBytes);
|
||||
}
|
||||
lock (_cacheLock) { _cache = new LatestReleaseCache(source!, result, DateTime.UtcNow); }
|
||||
return result;
|
||||
}
|
||||
@@ -163,6 +172,36 @@ public class UpdatesController : ControllerBase
|
||||
|
||||
private static bool IsSha256Digest(string? value) => value is { Length: 64 } && value.All(Uri.IsHexDigit);
|
||||
|
||||
private static async Task<byte[]?> FetchBoundedReleaseAssetAsync(List<ForgejoAsset>? assets,
|
||||
string name, HttpClient client, int maxBytes, CancellationToken ct)
|
||||
{
|
||||
var asset = assets?.FirstOrDefault(a => string.Equals(a.Name, name, StringComparison.Ordinal));
|
||||
var expected = asset is null ? null : ExtractSha256(asset.Digest);
|
||||
if (asset is null || !IsSha256Digest(expected)) return null;
|
||||
try
|
||||
{
|
||||
using var response = await client.GetAsync(asset.BrowserDownloadUrl,
|
||||
HttpCompletionOption.ResponseHeadersRead, ct);
|
||||
if (!response.IsSuccessStatusCode || response.Content.Headers.ContentLength is long length && length > maxBytes)
|
||||
return null;
|
||||
await using var stream = await response.Content.ReadAsStreamAsync(ct);
|
||||
using var buffer = new MemoryStream();
|
||||
var chunk = new byte[8192];
|
||||
while (true)
|
||||
{
|
||||
var read = await stream.ReadAsync(chunk, ct);
|
||||
if (read == 0) break;
|
||||
if (buffer.Length + read > maxBytes) return null;
|
||||
buffer.Write(chunk, 0, read);
|
||||
}
|
||||
var bytes = buffer.ToArray();
|
||||
return string.Equals(Convert.ToHexString(SHA256.HashData(bytes)), expected,
|
||||
StringComparison.OrdinalIgnoreCase) ? bytes : null;
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested) { throw; }
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
private static bool IsValidContainerImageRepository(string? image) => image is { Length: > 0 and <= 255 }
|
||||
&& char.IsAsciiLetterOrDigit(image[0])
|
||||
&& image.All(c => char.IsAsciiLetterOrDigit(c) || c is '.' or '_' or ':' or '/' or '-')
|
||||
@@ -197,6 +236,7 @@ public class UpdatesController : ControllerBase
|
||||
if (l.Contains("win-x64") || l.Contains("windows-x64")) return "win-x64";
|
||||
if (l.Contains("osx-x64") || l.Contains("macos-x64")) return "osx-x64";
|
||||
if (l.Contains("osx-arm64") || l.Contains("macos-arm64")) return "osx-arm64";
|
||||
if (l.Contains("agent-portable-")) return "portable";
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -215,6 +255,8 @@ public class UpdatesController : ControllerBase
|
||||
[JsonPropertyName("assets")] public Dictionary<string, PlatformAsset> Assets { get; set; } = [];
|
||||
[JsonPropertyName("containerImage")] public string? ContainerImage { get; set; }
|
||||
[JsonPropertyName("containerImageDigest")] public string? ContainerImageDigest { get; set; }
|
||||
[JsonPropertyName("signedManifest")] public string? SignedManifest { get; set; }
|
||||
[JsonPropertyName("manifestSignature")] public string? ManifestSignature { get; set; }
|
||||
}
|
||||
public class PlatformAsset
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user