Files
Alphaeus Mote b4202e9794
CI / build-test (push) Failing after 11m51s
CI / images (push) Has been skipped
Release / release (push) Successful in 24m35s
Release / images (push) Successful in 5m13s
feat(enrollment): stable agent download links, optional public downloads, new Add Device dialog
- Stable links per package (/api/downloads/agent/Agent.msi, Agent.zip,
  Agent-<os>-<arch>.zip) always serve the newest build (GET and HEAD,
  X-Agent-Version); /api/downloads/agent lists version, SHA-256, size and
  link as JSON. With offload on they redirect to a fresh presigned link
  (S3 presigned links cannot be permanent).
- Public Downloads setting (off by default): on, anyone; off, install
  scripts (their package URLs carry the bootstrap token) and signed-in
  users with devices.view.
- No MSI/ZIP choice: the Windows script installs the MSI, or the ZIP in
  Windows PE. Add Device is redesigned: platform cards, token step with
  summary chips, install step, and per-platform downloads.
- Settings > Enrollment Tokens gains an Agent Downloads card.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 21:25:58 -05:00

123 lines
6.0 KiB
C#

using System.Net;
using System.Net.Http.Json;
using Backend.Data;
using Shared.Contracts.Api;
using Shared.Contracts.Models;
using Shared.Infrastructure.Security;
using Xunit;
using static Backend.Tests.AgentUpgradeTestKit;
namespace Backend.Tests;
/// <summary>Who may download agent packages, and the stable links that always serve the newest build.</summary>
public class AgentDownloadTests(BackendTestFixture fixture) : IClassFixture<BackendTestFixture>
{
private async Task<string> TokenAsync(bool revoked = false, bool expired = false)
{
var token = "dl-" + Guid.NewGuid().ToString("N");
await fixture.WithDbAsync(async db =>
{
db.BootstrapTokens.Add(new BootstrapTokenEntity
{
Id = Guid.NewGuid(),
TokenHash = TokenHashing.Sha256Hex(token),
CreatedUtc = DateTimeOffset.UtcNow,
ExpiresUtc = expired ? DateTimeOffset.UtcNow.AddMinutes(-1) : DateTimeOffset.UtcNow.AddHours(1),
MaxUses = 1,
Uses = 1, // spent: a download still goes through (the same install run again)
Revoked = revoked,
});
await db.SaveChangesAsync();
});
return token;
}
[Fact]
public async Task OffByDefaultAPackageNeedsALiveTokenOrASignedInUser()
{
await fixture.ResetAsync();
await fixture.SeedPackageAsync(V2, content: "zip v2");
using var anonymous = fixture.CreateClient();
var ct = TestContext.Current.CancellationToken;
Assert.Equal(HttpStatusCode.Unauthorized, (await anonymous.GetAsync($"{ApiRoutes.AgentPackage}?type=zip", ct)).StatusCode);
Assert.Equal(HttpStatusCode.Unauthorized, (await anonymous.GetAsync($"{ApiRoutes.AgentDownloads}/Agent.zip", ct)).StatusCode);
Assert.Equal(HttpStatusCode.Unauthorized, (await anonymous.GetAsync(ApiRoutes.AgentDownloads, ct)).StatusCode);
var live = await anonymous.GetAsync($"{ApiRoutes.AgentPackage}?type=zip&token={await TokenAsync()}", ct);
Assert.Equal(HttpStatusCode.OK, live.StatusCode);
Assert.Equal("zip v2", await live.Content.ReadAsStringAsync(ct));
Assert.Equal(HttpStatusCode.Unauthorized,
(await anonymous.GetAsync($"{ApiRoutes.AgentPackage}?type=zip&token={await TokenAsync(revoked: true)}", ct)).StatusCode);
Assert.Equal(HttpStatusCode.Unauthorized,
(await anonymous.GetAsync($"{ApiRoutes.AgentPackage}?type=zip&token={await TokenAsync(expired: true)}", ct)).StatusCode);
using var admin = await fixture.SignedInAsync("download-admin");
Assert.Equal(HttpStatusCode.OK, (await admin.GetAsync($"{ApiRoutes.AgentDownloads}/Agent.zip", ct)).StatusCode);
Assert.Equal(HttpStatusCode.OK, (await admin.GetAsync(ApiRoutes.AgentDownloads, ct)).StatusCode);
}
[Fact]
public async Task PublicStableLinksServeTheNewestBuild()
{
await fixture.ResetAsync();
await fixture.SetPublicDownloadsAsync(true);
await fixture.SeedPackageAsync(V1, content: "zip v1");
var newest = await fixture.SeedPackageAsync(V2, content: "zip v2");
await fixture.SeedPackageAsync(V2, AgentPackageTypes.Msi, content: "msi v2");
using var anonymous = fixture.CreateClient();
var ct = TestContext.Current.CancellationToken;
var zip = await anonymous.GetAsync($"{ApiRoutes.AgentDownloads}/agent.zip", ct);
Assert.Equal(HttpStatusCode.OK, zip.StatusCode);
Assert.Equal("zip v2", await zip.Content.ReadAsStringAsync(ct));
Assert.Equal(V2, zip.Headers.GetValues("X-Agent-Version").Single());
using var head = await anonymous.SendAsync(new HttpRequestMessage(HttpMethod.Head, $"{ApiRoutes.AgentDownloads}/Agent.msi"), ct);
Assert.Equal(HttpStatusCode.OK, head.StatusCode);
Assert.Equal(V2, head.Headers.GetValues("X-Agent-Version").Single());
Assert.Equal(HttpStatusCode.NotFound, (await anonymous.GetAsync($"{ApiRoutes.AgentDownloads}/Other.exe", ct)).StatusCode);
Assert.Equal(HttpStatusCode.NotFound, (await anonymous.GetAsync($"{ApiRoutes.AgentDownloads}/Agent-linux-x64.zip", ct)).StatusCode);
var listed = (await anonymous.GetFromJsonAsync<List<AgentDownloadDto>>(ApiRoutes.AgentDownloads, ct))!;
var listedZip = listed.Single(d => d.Type == AgentPackageTypes.WindowsZip);
Assert.True(listedZip.Available);
Assert.Equal(V2, listedZip.Version);
Assert.Equal(newest.Sha256, listedZip.Sha256);
Assert.EndsWith($"{ApiRoutes.AgentDownloads}/Agent.zip", listedZip.Url);
Assert.False(listed.Single(d => d.Type == AgentPackageTypes.LinuxX64).Available);
}
[Fact]
public async Task OnlyAnAdminTurnsThemOnAndOff()
{
await fixture.ResetAsync();
var ct = TestContext.Current.CancellationToken;
using var admin = await fixture.SignedInAsync("download-settings-admin");
Assert.False((await admin.GetFromJsonAsync<AgentDownloadSettingsDto>(ApiRoutes.AgentDownloadSettings, ct))!.Public);
var saved = await admin.PutAsJsonAsync(ApiRoutes.AgentDownloadSettings, new AgentDownloadSettingsDto(true), ct);
Assert.Equal(HttpStatusCode.OK, saved.StatusCode);
Assert.True((await admin.GetFromJsonAsync<AgentDownloadSettingsDto>(ApiRoutes.AgentDownloadSettings, ct))!.Public);
using var anonymous = fixture.CreateClient();
Assert.Equal(HttpStatusCode.Unauthorized,
(await anonymous.PutAsJsonAsync(ApiRoutes.AgentDownloadSettings, new AgentDownloadSettingsDto(false), ct)).StatusCode);
}
[Fact]
public async Task InstallScriptsPassTheirTokenToThePackageLinks()
{
using var client = fixture.CreateClient();
var ct = TestContext.Current.CancellationToken;
foreach (var type in new[] { "", "&type=zip", "&type=linux" })
{
var script = await client.GetStringAsync($"{ApiRoutes.InstallScript}?token=abc.123{type}", ct);
Assert.Contains($"{ApiRoutes.AgentPackage}?type=", script);
Assert.Contains("&token=abc.123", script);
}
}
}