From e82dbf9a0aed610827070d01c57dfbc6b2c61bad Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Thu, 22 Oct 2026 06:57:50 +0200 Subject: [PATCH] fix(agent): load enrollment secrets as systemd credentials --- install/install-agent.sh | 14 ++++-- src/Heimdall.Agent/AgentOptions.cs | 44 +++++++++++++++++-- .../Services/AgentRegistrationService.cs | 4 ++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/install/install-agent.sh b/install/install-agent.sh index c33894e..4f1af17 100755 --- a/install/install-agent.sh +++ b/install/install-agent.sh @@ -18,7 +18,15 @@ if [[ -n "$EXPECTED_SHA256" ]]; then ACTUAL=$(sha256sum "$TMP_DIR/$BINARY_NAME" chmod +x "$TMP_DIR/$BINARY_NAME"; [[ $EUID -eq 0 ]] && cp "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME" || sudo cp "$TMP_DIR/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME" id "$SERVICE_USER" &>/dev/null || sudo useradd --system --no-create-home --shell /usr/sbin/nologin "$SERVICE_USER" 2>/dev/null || true sudo mkdir -p "$STATE_DIR" && sudo chown "$SERVICE_USER:$SERVICE_USER" "$STATE_DIR" && sudo chmod 700 "$STATE_DIR" -AGENT_NAME_ENV=""; [[ -n "$AGENT_NAME" ]] && AGENT_NAME_ENV="Environment=Agent__AgentName=$AGENT_NAME" +sudo install -d -o root -g root -m 700 /etc/heimdall/credentials +printf '%s' "$SITE_TOKEN" | sudo tee /etc/heimdall/credentials/site-token >/dev/null +sudo chmod 600 /etc/heimdall/credentials/site-token +AGENT_NAME_CREDENTIAL="" +if [[ -n "$AGENT_NAME" ]]; then + printf '%s' "$AGENT_NAME" | sudo tee /etc/heimdall/credentials/agent-name >/dev/null + sudo chmod 600 /etc/heimdall/credentials/agent-name + AGENT_NAME_CREDENTIAL="LoadCredential=agent-name:/etc/heimdall/credentials/agent-name" +fi cat </dev/null [Unit] Description=Heimdall Agent @@ -30,9 +38,9 @@ ExecStart=$INSTALL_DIR/$BINARY_NAME Restart=on-failure Environment=ASPNETCORE_ENVIRONMENT=Production Environment=Agent__CentralUrl=$CENTRAL_URL -Environment=Agent__SiteToken=$SITE_TOKEN Environment=Agent__StateDirectory=$STATE_DIR -$AGENT_NAME_ENV +LoadCredential=site-token:/etc/heimdall/credentials/site-token +$AGENT_NAME_CREDENTIAL [Install] WantedBy=multi-user.target EOF diff --git a/src/Heimdall.Agent/AgentOptions.cs b/src/Heimdall.Agent/AgentOptions.cs index 89afb0f..5cf242f 100644 --- a/src/Heimdall.Agent/AgentOptions.cs +++ b/src/Heimdall.Agent/AgentOptions.cs @@ -16,14 +16,29 @@ public sealed class AgentOptions [Required, Url] public string CentralUrl { get; init; } = string.Empty; - /// Site token for agent registration. - [Required, MinLength(1)] - public string SiteToken { get; init; } = string.Empty; + /// Site token for initial agent registration. + public string SiteToken + { + get => string.IsNullOrWhiteSpace(_siteToken) + ? ReadSystemdCredential("site-token") ?? string.Empty + : _siteToken; + init => _siteToken = value ?? string.Empty; + } + + private string _siteToken = string.Empty; /// /// Display name for this agent instance. Falls back to hostname if empty. /// - public string? AgentName { get; init; } + public string? AgentName + { + get => string.IsNullOrWhiteSpace(_agentName) + ? ReadSystemdCredential("agent-name") + : _agentName; + init => _agentName = value; + } + + private string? _agentName; /// Interval between heartbeats in seconds (default: 30). [Range(5, 300)] @@ -85,4 +100,25 @@ public sealed class AgentOptions public string DockerSocketPath { get; init; } = "/var/run/docker.sock"; public string DockerImageName { get; init; } = "eltruncho/heimdall-agent"; public bool AutoUpdateEnabled { get; set; } + + private static string? ReadSystemdCredential(string name) + { + var directory = Environment.GetEnvironmentVariable("CREDENTIALS_DIRECTORY"); + if (string.IsNullOrWhiteSpace(directory)) + return null; + + try + { + var path = Path.Combine(directory, name); + return File.Exists(path) ? File.ReadAllText(path).TrimEnd('\r', '\n') : null; + } + catch (IOException) + { + return null; + } + catch (UnauthorizedAccessException) + { + return null; + } + } } diff --git a/src/Heimdall.Agent/Services/AgentRegistrationService.cs b/src/Heimdall.Agent/Services/AgentRegistrationService.cs index 2429ca1..63a69d9 100644 --- a/src/Heimdall.Agent/Services/AgentRegistrationService.cs +++ b/src/Heimdall.Agent/Services/AgentRegistrationService.cs @@ -149,6 +149,10 @@ public class AgentRegistrationService : BackgroundService private async Task RegisterAgentAsync(HttpClient client, string hostname, CancellationToken stoppingToken) { + if (string.IsNullOrWhiteSpace(_options.SiteToken)) + throw new InvalidOperationException( + "Agent registration requires Agent:SiteToken or a systemd site-token credential."); + _logger.LogInformation("Registering agent with Central at {CentralUrl}", _options.CentralUrl); var maxRetries = _options.RegistrationMaxRetries; -- 2.54.1