// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! What a droplet's name and tags say about who made it and for what. //! //! A droplet's name and tags say which source and tier it counts against and //! which row of the droplet database it is. Droplets made before that //! database existed carried the runner or run they were for instead, and are //! still recognised, so that one running across an upgrade is reaped rather //! than stranded. //! //! `owner` is **the only gate on destruction.** The listing is filtered by tag //! on the server, but a filter that silently stopped working -- a renamed //! query parameter, an API change -- would hand back every droplet in the //! team, and a long-lived host is far older than any lifetime cap. So //! ownership is re-established from scratch for every droplet, and every //! condition must hold before it is anything but "not ours". const std = @import("std"); /// Carried by every droplet this daemon creates, and what the listing asks for. pub const marker = "ci-ephemeral"; /// The marker the per-forge Python provisioners used, as /// `forgejo-ephemeral--`. Accepted while `legacy` is on, so that /// droplets those services left running when this one replaced them are /// counted and reaped rather than stranded until someone notices. pub const legacy_prefix = "forgejo-ephemeral-"; const forgejo_runner_prefix = "fjrunner-"; const forgejo_job_prefix = "fjjob-"; const forgejo_name_prefix = "fj-runner-"; const radicle_run_prefix = "radrun-"; const radicle_name_prefix = "rad-run-"; const droplet_prefix = "cidroplet-"; const droplet_name_prefix = "ci-droplet-"; /// What a droplet is for. pub const Work = union(enum) { /// A row of the droplet database, which says what it is for. droplet: u64, /// From before the droplet database: a Forgejo ephemeral runner, by the /// id the forge gave it. forgejo_runner: u64, /// From before the droplet database: a Radicle CI run, by the id this /// daemon gave it. radicle_run: u64, }; pub const Owner = struct { /// The configured name of the source: a forge's, or `radicle`. source: []const u8, tier: []const u8, work: Work, }; /// Whether `name` can be a source or tier name: lowercase letters and digits /// only. DigitalOcean allows more in a tag, but a hyphen here would make /// `ci--` ambiguous to read back. pub fn validName(name: []const u8) bool { if (name.len == 0 or name.len > 32) return false; for (name) |c| switch (c) { 'a'...'z', '0'...'9' => {}, else => return false, }; return true; } /// `ci--`: which budget a droplet counts against. pub fn tierTag(buf: []u8, source: []const u8, tier: []const u8) ![]const u8 { return std.fmt.bufPrint(buf, "ci-{s}-{s}", .{ source, tier }); } /// A DigitalOcean-safe, stable tag for a Forgejo job handle. /// /// Handles are opaque and may contain characters tags do not allow, so they /// are hashed rather than sanitised. Sixteen hex digits of SHA-256, which is /// what the Python provisioners wrote, so a droplet either one made is /// recognised by both. pub fn jobTag(handle: []const u8) [forgejo_job_prefix.len + 16]u8 { var digest: [32]u8 = undefined; std.crypto.hash.sha2.Sha256.hash(handle, &digest, .{}); var out: [forgejo_job_prefix.len + 16]u8 = undefined; @memcpy(out[0..forgejo_job_prefix.len], forgejo_job_prefix); const hex = std.fmt.bytesToHex(digest[0..8], .lower); @memcpy(out[forgejo_job_prefix.len..], &hex); return out; } pub fn isJobTag(tag: []const u8) bool { return std.mem.startsWith(u8, tag, forgejo_job_prefix); } pub fn forgejoName(buf: []u8, runner_id: u64) ![]const u8 { return std.fmt.bufPrint(buf, forgejo_name_prefix ++ "{d}", .{runner_id}); } pub fn forgejoRunnerTag(buf: []u8, runner_id: u64) ![]const u8 { return std.fmt.bufPrint(buf, forgejo_runner_prefix ++ "{d}", .{runner_id}); } pub fn dropletName(buf: []u8, id: u64) ![]const u8 { return std.fmt.bufPrint(buf, droplet_name_prefix ++ "{d}", .{id}); } pub fn dropletTag(buf: []u8, id: u64) ![]const u8 { return std.fmt.bufPrint(buf, droplet_prefix ++ "{d}", .{id}); } pub fn radicleName(buf: []u8, run_id: u64) ![]const u8 { return std.fmt.bufPrint(buf, radicle_name_prefix ++ "{d}", .{run_id}); } pub fn radicleRunTag(buf: []u8, run_id: u64) ![]const u8 { return std.fmt.bufPrint(buf, radicle_run_prefix ++ "{d}", .{run_id}); } /// The sources and tiers a droplet may name, which is what makes a tier tag /// ours rather than something that merely looks like one. pub const Known = struct { sources: []const []const u8, tiers: []const []const u8, /// Whether the Python provisioners' tags are recognised as well. legacy: bool = false, fn has(list: []const []const u8, name: []const u8) bool { for (list) |item| if (std.mem.eql(u8, item, name)) return true; return false; } }; /// Who made `name` and `tags`, or null if it was not this daemon. /// /// Every condition must hold: /// /// * it carries the marker (or, with `legacy`, the old per-forge one); /// * exactly one tag names a known source and tier; /// * exactly one tag names the droplet's row, or (from before the droplet /// database) the runner or run it is for, and only one kind; /// * its name is the one assigned at creation, and agrees with that tag. /// /// A droplet created by hand, by terraform, or by tack fails all of them. pub fn owner(known: Known, name: []const u8, tags: []const []const u8) ?Owner { var marked = false; var budget: ?struct { source: []const u8, tier: []const u8 } = null; var budgets: usize = 0; var work: ?Work = null; var works: usize = 0; for (tags) |tag| { if (std.mem.eql(u8, tag, marker)) { marked = true; } else if (std.mem.startsWith(u8, tag, "ci-")) { if (splitBudget(known, tag["ci-".len..])) |b| { budget = .{ .source = b[0], .tier = b[1] }; budgets += 1; } } else if (known.legacy and std.mem.startsWith(u8, tag, legacy_prefix)) { if (splitBudget(known, tag[legacy_prefix.len..])) |b| { // A legacy tag is its own marker: the old services had no // other one. marked = true; budget = .{ .source = b[0], .tier = b[1] }; budgets += 1; } } else if (std.mem.startsWith(u8, tag, forgejo_runner_prefix)) { if (parseId(tag[forgejo_runner_prefix.len..])) |id| { work = .{ .forgejo_runner = id }; works += 1; } } else if (std.mem.startsWith(u8, tag, droplet_prefix)) { if (parseId(tag[droplet_prefix.len..])) |id| { work = .{ .droplet = id }; works += 1; } } else if (std.mem.startsWith(u8, tag, radicle_run_prefix)) { if (parseId(tag[radicle_run_prefix.len..])) |id| { work = .{ .radicle_run = id }; works += 1; } } } if (!marked or budgets != 1 or works != 1) return null; // Compared as text, not as a parsed number: the name has to be exactly // the one written at creation, and `fj-runner-01` is not. var buf: [32]u8 = undefined; const expected = switch (work.?) { .droplet => |id| dropletName(&buf, id), .forgejo_runner => |id| forgejoName(&buf, id), .radicle_run => |id| radicleName(&buf, id), } catch return null; if (!std.mem.eql(u8, name, expected)) return null; return .{ .source = budget.?.source, .tier = budget.?.tier, .work = work.? }; } /// `-`, both known, or null. fn splitBudget(known: Known, rest: []const u8) ?[2][]const u8 { const dash = std.mem.findScalar(u8, rest, '-') orelse return null; const source = rest[0..dash]; const tier = rest[dash + 1 ..]; if (!Known.has(known.sources, source) or !Known.has(known.tiers, tier)) return null; return .{ source, tier }; } /// Decimal digits only, as written at creation: no sign, no leading `+`, no /// whitespace, and nothing `parseInt` would forgive. fn parseId(text: []const u8) ?u64 { if (text.len == 0 or text.len > 20) return null; for (text) |c| if (c < '0' or c > '9') return null; return std.fmt.parseInt(u64, text, 10) catch null; } const testing = std.testing; const test_known: Known = .{ .sources = &.{ "ocjtech", "codeberg", "radicle" }, .tiers = &.{ "s", "m", "l" }, }; test "a Forgejo droplet is ours" { const o = owner(test_known, "fj-runner-2848", &.{ "ci-ephemeral", "ci-ocjtech-s", "fjjob-0123456789abcdef", "fjrunner-2848" }).?; try testing.expectEqualStrings("ocjtech", o.source); try testing.expectEqualStrings("s", o.tier); try testing.expectEqual(Work{ .forgejo_runner = 2848 }, o.work); } test "a Radicle droplet is ours" { const o = owner(test_known, "rad-run-7", &.{ "ci-ephemeral", "ci-radicle-l", "radrun-7" }).?; try testing.expectEqualStrings("radicle", o.source); try testing.expectEqual(Work{ .radicle_run = 7 }, o.work); } test "a droplet from the database is ours" { const o = owner(test_known, "ci-droplet-42", &.{ "ci-ephemeral", "ci-codeberg-m", "cidroplet-42" }).?; try testing.expectEqualStrings("codeberg", o.source); try testing.expectEqualStrings("m", o.tier); try testing.expectEqual(Work{ .droplet = 42 }, o.work); // the name must agree, and one kind only try testing.expectEqual(null, owner(test_known, "ci-droplet-43", &.{ "ci-ephemeral", "ci-codeberg-m", "cidroplet-42" })); try testing.expectEqual(null, owner(test_known, "ci-droplet-42", &.{ "ci-ephemeral", "ci-codeberg-m", "cidroplet-42", "radrun-42" })); try testing.expectEqual(null, owner(test_known, "fj-runner-42", &.{ "ci-ephemeral", "ci-codeberg-m", "cidroplet-42" })); } test "a long-lived host is never ours" { try testing.expectEqual(null, owner(test_known, "cloud04", &.{})); try testing.expectEqual(null, owner(test_known, "cloud04", &.{"ci-ephemeral"})); // tack's droplets share the team and are nobody's business here try testing.expectEqual(null, owner(test_known, "tack-run-12", &.{ "tack", "ci-ephemeral" })); } test "every condition must hold" { // no marker try testing.expectEqual(null, owner(test_known, "fj-runner-1", &.{ "ci-ocjtech-s", "fjrunner-1" })); // no budget, or an unknown one try testing.expectEqual(null, owner(test_known, "fj-runner-1", &.{ "ci-ephemeral", "fjrunner-1" })); try testing.expectEqual(null, owner(test_known, "fj-runner-1", &.{ "ci-ephemeral", "ci-gitlab-s", "fjrunner-1" })); try testing.expectEqual(null, owner(test_known, "fj-runner-1", &.{ "ci-ephemeral", "ci-ocjtech-xl", "fjrunner-1" })); // two budgets try testing.expectEqual(null, owner(test_known, "fj-runner-1", &.{ "ci-ephemeral", "ci-ocjtech-s", "ci-ocjtech-m", "fjrunner-1" })); // two runner tags, or one of each kind try testing.expectEqual(null, owner(test_known, "fj-runner-1", &.{ "ci-ephemeral", "ci-ocjtech-s", "fjrunner-1", "fjrunner-2" })); try testing.expectEqual(null, owner(test_known, "fj-runner-1", &.{ "ci-ephemeral", "ci-ocjtech-s", "fjrunner-1", "radrun-1" })); // a name that disagrees with the tag, or is the other kind's try testing.expectEqual(null, owner(test_known, "fj-runner-2", &.{ "ci-ephemeral", "ci-ocjtech-s", "fjrunner-1" })); try testing.expectEqual(null, owner(test_known, "rad-run-1", &.{ "ci-ephemeral", "ci-ocjtech-s", "fjrunner-1" })); try testing.expectEqual(null, owner(test_known, "fj-runner-01", &.{ "ci-ephemeral", "ci-ocjtech-s", "fjrunner-1" })); // ids that parseInt would forgive try testing.expectEqual(null, owner(test_known, "fj-runner-+1", &.{ "ci-ephemeral", "ci-ocjtech-s", "fjrunner-+1" })); try testing.expectEqual(null, owner(test_known, "fj-runner-", &.{ "ci-ephemeral", "ci-ocjtech-s", "fjrunner-" })); } test "legacy tags only when asked" { const tags: []const []const u8 = &.{ "forgejo-ephemeral-codeberg-m", "fjjob-0123456789abcdef", "fjrunner-5" }; try testing.expectEqual(null, owner(test_known, "fj-runner-5", tags)); var with_legacy = test_known; with_legacy.legacy = true; const o = owner(with_legacy, "fj-runner-5", tags).?; try testing.expectEqualStrings("codeberg", o.source); try testing.expectEqualStrings("m", o.tier); } test "job tags match the Python provisioners'" { // hashlib.sha256(b"abc").hexdigest()[:16] try testing.expectEqualStrings("fjjob-ba7816bf8f01cfea", &jobTag("abc")); } test "names are validated" { try testing.expect(validName("ocjtech")); try testing.expect(validName("s")); try testing.expect(!validName("")); try testing.expect(!validName("oc-jtech")); try testing.expect(!validName("Ocjtech")); } test { std.testing.refAllDecls(@This()); }