fix(agent): load enrollment secrets as systemd credentials
This commit is contained in:
1 parent
75a09aaf9a
commit
e82dbf9a0a
3 files changed
+55
-7
No files matched your search
@@ -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 <<EOF | sudo tee /etc/systemd/system/$SERVICE_NAME.service >/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
|
||||
|
||||
@@ -16,14 +16,29 @@ public sealed class AgentOptions
|
||||
[Required, Url]
|
||||
public string CentralUrl { get; init; } = string.Empty;
|
||||
|
||||
/// <summary>Site token for agent registration.</summary>
|
||||
[Required, MinLength(1)]
|
||||
public string SiteToken { get; init; } = string.Empty;
|
||||
/// <summary>Site token for initial agent registration.</summary>
|
||||
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)]
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user