fix(agent): protect systemd enrollment credentials #39

Merged
arnaud.dartois merged 2 commits from fix/agent-systemd-credentials into main 2026-10-22 19:15:56 +02:00
3 changed files with 55 additions and 6 deletions

No files matched your search

+11 -3
View File
@@ -22,7 +22,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 <<EOF | sudo tee /etc/systemd/system/$SERVICE_NAME.service >/dev/null
[Unit]
Description=Heimdall Agent
@@ -35,9 +43,9 @@ Restart=on-failure
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=Agent__CentralUrl=$CENTRAL_URL
Environment=Agent__AllowInsecureHttp=$ALLOW_INSECURE_HTTP
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
+40 -3
View File
@@ -19,14 +19,30 @@ public sealed class AgentOptions
/// <summary>Allow an unencrypted HTTP Central URL for isolated development networks only.</summary>
public bool AllowInsecureHttp { get; init; }
/// <summary>Site token for agent registration.</summary>
/// <summary>Site token for initial agent registration.</summary>
[Required, MinLength(1)]
public string SiteToken { get; init; } = string.Empty;
public string SiteToken
{
get => string.IsNullOrWhiteSpace(_siteToken)
? ReadSystemdCredential("site-token") ?? string.Empty
: _siteToken;
init => _siteToken = value ?? string.Empty;
}
private string _siteToken = string.Empty;
/// <summary>
/// Display name for this agent instance. Falls back to hostname if empty.
/// </summary>
public string? AgentName { get; init; }
public string? AgentName
{
get => string.IsNullOrWhiteSpace(_agentName)
? ReadSystemdCredential("agent-name")
: _agentName;
init => _agentName = value;
}
private string? _agentName;
/// <summary>Interval between heartbeats in seconds (default: 30).</summary>
[Range(5, 300)]
@@ -88,4 +104,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;
}
}
}
@@ -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;