Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions src/Bower.Management.Api/SourceSetupJournald.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
using System.Globalization;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using static Bower.Management.Api.SourceSetupRegistry;

namespace Bower.Management.Api;

/// <summary>
/// Bounded, allow-listed systemd journald source (src/Bower.Source.Journald,
/// docs/deployment/journald-source.md). Enforces explicit systemd unit or
/// SYSLOG_IDENTIFIER allow-lists (no empty list, no wildcards) and durable cursor tracking.
/// </summary>
internal sealed partial class JournaldSourceSetup : SourceSetupType
{
public override string Id => "journald";

public override string Title => "Linux systemd journald";

public override string Summary =>
"Reads allow-listed systemd units or syslog identifiers directly from the local journal with durable cursors.";

public override string Why =>
"Modern Linux hosts record SSH, sudo and authentication telemetry in systemd-journald. Naming explicit units or identifiers keeps general host noise out while preserving durable resume across restarts.";

public override IReadOnlyList<string> Needs =>
[
"A Linux host running systemd-journald with the collector user in the systemd-journal group.",
"Explicit systemd unit names (for example ssh.service) or SYSLOG_IDENTIFIER values (for example sshd, sudo).",
"A signed journald parser pack such as linux-journald-ssh loaded on the collector."
];

public override IReadOnlyList<SourceFieldRule> Fields { get; } =
[
new(
new SourceFieldDefinition(
"subscriptionName", "Subscription name", SourceFieldKinds.Text, true,
"Stable cursor name for this journald subscription. Keep it unchanged across restarts.",
"linux-journald-ssh", MaximumLength: 64),
IdentifierPattern(),
"Use letters, digits, dots, hyphens and underscores only."),
new(
new SourceFieldDefinition(
"units", "Allowed systemd units", SourceFieldKinds.NameList, false,
"Explicit systemd unit names, comma- or space-separated (for example ssh.service, sshd.service). Wildcards are refused.",
"ssh.service,sshd.service", Placeholder: "ssh.service, sshd.service", MaximumLength: 512),
JournaldEntryPattern(),
"Use explicit unit names (letters, digits, '.', '_', '-', '@', ':') with no wildcards."),
new(
new SourceFieldDefinition(
"identifiers", "Allowed SYSLOG_IDENTIFIER values", SourceFieldKinds.NameList, false,
"Optional explicit SYSLOG_IDENTIFIER values (for example sshd, sudo). Wildcards are refused.",
Placeholder: "sshd, sudo", MaximumLength: 512),
JournaldEntryPattern(),
"Use explicit identifier names (letters, digits, '.', '_', '-', '@', ':') with no wildcards."),
new(new SourceFieldDefinition(
"maxBatchSize", "Maximum batch size", SourceFieldKinds.Number, true,
"Maximum journal entries read per poll cycle (1 to 500).",
"128", Minimum: 1, Maximum: 500, MaximumLength: 4)),
Application("linux-journald-ssh", "The application name events carry in Sentinel."),
SourceSetupRegistry.Environment(),
Silence()
];

public override string CheckDescription =>
"Bower watches the collector's heartbeat for the application and environment above. It turns green when the collector reports journald events.";

public override string Guide => "docs/deployment/journald-source.md";

public override void Validate(IReadOnlyDictionary<string, string> values, List<SetupCheck> checks)
{
IReadOnlyList<string> units = List(values, "units");
IReadOnlyList<string> identifiers = List(values, "identifiers");
if (units.Count == 0 && identifiers.Count == 0)
{
checks.Add(Fail(
"units",
"Journald requires at least one explicit systemd unit or SYSLOG_IDENTIFIER.",
"Enter at least one unit such as ssh.service or identifier such as sshd."));
return;
}

if (units.Count + identifiers.Count > 64)
{
checks.Add(Fail(
"units",
"Combined units and SYSLOG_IDENTIFIER entries cannot exceed 64.",
"List at most 64 explicit units and identifiers in total."));
}
}

public override SourceSetupBuild Build(IReadOnlyDictionary<string, string> values, string collectorId)
{
IReadOnlyList<string> units = List(values, "units");
IReadOnlyList<string> identifiers = List(values, "identifiers");
int maxBatchSize = int.Parse(values["maxBatchSize"], CultureInfo.InvariantCulture);

JsonObject config = new()
{
["apiVersion"] = "bower.security/v1",
["kind"] = "JournaldSource",
["subscriptionName"] = values["subscriptionName"],
["units"] = new JsonArray([.. units.Select(u => JsonValue.Create(u))]),
["identifiers"] = new JsonArray([.. identifiers.Select(i => JsonValue.Create(i))]),
["maxRecordBytes"] = 32_768,
["maxBatchSize"] = maxBatchSize,
["application"] = values["application"],
["environment"] = values["environment"]
};

SourceSetupFile journaldFile = new(
"journald.json",
"/etc/bower/journald.json",
"Allow-listed systemd journald source settings. Bower reads only the listed units and identifiers.",
Json(config));

string systemdDropIn =
"[Service]\n" +
"SupplementaryGroups=systemd-journal\n" +
"Environment=BOWER_JOURNALD_CONFIG=/etc/bower/journald.json\n";

return new SourceSetupBuild(
[journaldFile, Expectations(values["application"], values["environment"], values["maximumSilenceMinutes"])],
[
new("collector", "BOWER_JOURNALD_CONFIG", "/etc/bower/journald.json"),
ExpectationsVariable()
],
[new SourceSetupSnippet(
"systemd: read-only journal access drop-in",
"Place in /etc/systemd/system/bower-collector.service.d/journald.conf so the collector reads the journal without root privileges.",
"ini",
systemdDropIn)],
ApplicationVerification(values["application"], values["environment"]));
}

public override JsonObject Sample(IReadOnlyDictionary<string, string> values)
{
IReadOnlyList<string> units = List(values, "units");
IReadOnlyList<string> identifiers = List(values, "identifiers");
string unit = units.Count > 0
? units[0]
: identifiers.Count > 0
? identifiers[0]
: "sshd.service";
return new JsonObject
{
["eventType"] = "ssh_login_failed",
["eventAction"] = "auth.ssh.failed",
["eventResult"] = "failure",
["application"] = new JsonObject { ["name"] = values["application"], ["environment"] = values["environment"] },
["actor"] = new JsonObject { ["username"] = "ops.user@example.org" },
["source"] = new JsonObject { ["ipAddress"] = "198.51.100.42" },
["attributes"] = new JsonObject
{
["journald.subscription"] = values["subscriptionName"],
["journald.unit"] = unit,
["auth.note"] = "Failed password for ops.user@example.org (TFN 123 456 782)"
}
};
}

[GeneratedRegex("^[A-Za-z0-9._@:-]{1,128}$")]
private static partial Regex JournaldEntryPattern();
}
212 changes: 212 additions & 0 deletions src/Bower.Management.Api/SourceSetupKubernetes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
using System.Globalization;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using static Bower.Management.Api.SourceSetupRegistry;

namespace Bower.Management.Api;

/// <summary>
/// Bounded, allow-listed Kubernetes container log source (src/Bower.Source.Kubernetes,
/// docs/deployment/kubernetes-source.md, deploy/kubernetes/daemonset.yaml). Enforces
/// explicit namespaces and label selectors (no cluster-wide collection, no pod annotation discovery).
/// </summary>
internal sealed partial class KubernetesSourceSetup : SourceSetupType
{
public override string Id => "kubernetes";

public override string Title => "Kubernetes container logs";

public override string Summary =>
"Tails CRI container logs for explicitly allow-listed namespaces and pod label selectors using a namespaced DaemonSet.";

public override string Why =>
"Container clusters mix noisy application stdout with security events. Requiring explicit namespaces and label selectors avoids cluster-wide collection and uses namespaced read-only pod RBAC instead of ClusterRole.";

public override IReadOnlyList<string> Needs =>
[
"A Kubernetes cluster using CRI-format container logs under /var/log/pods.",
"Explicit allow-listed namespace names and exact pod label selectors (key=value).",
"A namespaced Role and RoleBinding granting get/list on pods only (never ClusterRole)."
];

public override IReadOnlyList<SourceFieldRule> Fields { get; } =
[
new(
new SourceFieldDefinition(
"namespaces", "Allowed namespaces", SourceFieldKinds.NameList, true,
"Explicit Kubernetes namespaces (1 to 32, lowercase DNS-1123 labels). Wildcards and cluster-wide collection are refused.",
"security-apps", Placeholder: "security-apps, payments", MaximumLength: 512),
DnsLabelPattern(),
"Each namespace must be lowercase letters, digits or hyphens (1 to 63 characters, starting and ending with a letter or digit)."),
new(
new SourceFieldDefinition(
"labelSelectors", "Pod label selectors (key=value)", SourceFieldKinds.NameList, true,
"Explicit key=value selectors (1 to 16). Pods must match at least one selector to be collected.",
"bower.security/collect=true", Placeholder: "bower.security/collect=true", MaximumLength: 512),
LabelSelectorPattern(),
"Use exact key=value pairs (for example bower.security/collect=true) with no wildcards."),
new(
new SourceFieldDefinition(
"allowedMetadataLabels", "Allowed pod metadata label keys", SourceFieldKinds.NameList, false,
"Optional allow-list of pod label keys (up to 16) to attach as k8s.label.* attributes.",
"app.kubernetes.io/name", Placeholder: "app.kubernetes.io/name, tier", MaximumLength: 512),
LabelKeyPattern(),
"Label keys may use letters, digits, '.', '_', '-' and '/' only."),
new(new SourceFieldDefinition(
"podLogRoot", "Node pod log directory", SourceFieldKinds.FilePath, true,
"Read-only hostPath mounted from the node containing CRI container logs.",
"/var/log/pods", Placeholder: "/var/log/pods", MaximumLength: 260)),
new(new SourceFieldDefinition(
"maxPods", "Maximum pods per node", SourceFieldKinds.Number, true,
"Upper bound on pods watched per node (1 to 500).",
"200", Minimum: 1, Maximum: 500, MaximumLength: 4)),
new(new SourceFieldDefinition(
"maxBatchLines", "Maximum lines per poll", SourceFieldKinds.Number, true,
"Upper bound on CRI log lines read per poll cycle (1 to 500).",
"256", Minimum: 1, Maximum: 500, MaximumLength: 4)),
new(new SourceFieldDefinition(
"startAtEnd", "Start new container logs at end", SourceFieldKinds.Boolean, true,
"When true, newly discovered container logs start at the current end instead of replaying historical lines.",
"true", MaximumLength: 5)),
Application("k8s-security", "The application name events carry in Sentinel."),
SourceSetupRegistry.Environment(),
Silence()
];

public override string CheckDescription =>
"Bower watches the collector's heartbeat for the application and environment above. It turns green when the collector reports Kubernetes security events.";

public override string Guide => "docs/deployment/kubernetes-source.md";

public override void Validate(IReadOnlyDictionary<string, string> values, List<SetupCheck> checks)
{
IReadOnlyList<string> namespaces = List(values, "namespaces");
IReadOnlyList<string> selectors = List(values, "labelSelectors");
IReadOnlyList<string> allowedLabels = List(values, "allowedMetadataLabels");

if (namespaces.Count is 0 or > 32)
{
checks.Add(Fail(
"namespaces",
"Kubernetes requires between 1 and 32 explicitly allow-listed namespaces.",
"List 1 to 32 lowercase namespace names."));
}

if (selectors.Count is 0 or > 16)
{
checks.Add(Fail(
"labelSelectors",
"Kubernetes requires between 1 and 16 explicit key=value pod label selectors.",
"List 1 to 16 selectors such as bower.security/collect=true."));
}

if (allowedLabels.Count > 16)
{
checks.Add(Fail(
"allowedMetadataLabels",
"Allowed pod metadata label keys cannot exceed 16.",
"List at most 16 pod label keys."));
}
}

public override SourceSetupBuild Build(IReadOnlyDictionary<string, string> values, string collectorId)
{
IReadOnlyList<string> namespaces = List(values, "namespaces");
IReadOnlyList<string> selectors = List(values, "labelSelectors");
IReadOnlyList<string> allowedLabels = List(values, "allowedMetadataLabels");

JsonArray selectorArray = new();
foreach (string entry in selectors)
{
int split = entry.IndexOf('=', StringComparison.Ordinal);
selectorArray.Add((JsonNode)new JsonObject
{
["key"] = entry[..split],
["value"] = entry[(split + 1)..]
});
}

JsonObject config = new()
{
["apiVersion"] = "bower.security/v1",
["kind"] = "KubernetesLogSource",
["podLogRoot"] = values["podLogRoot"],
["namespaces"] = new JsonArray([.. namespaces.Select(ns => JsonValue.Create(ns))]),
["labelSelectors"] = selectorArray,
["allowedMetadataLabels"] = new JsonArray([.. allowedLabels.Select(key => JsonValue.Create(key))]),
["maxLineBytes"] = 65_536,
["maxPartialBytes"] = 65_536,
["maxBatchLines"] = int.Parse(values["maxBatchLines"], CultureInfo.InvariantCulture),
["maxPods"] = int.Parse(values["maxPods"], CultureInfo.InvariantCulture),
["startAtEnd"] = string.Equals(values["startAtEnd"], "true", StringComparison.Ordinal),
["application"] = values["application"],
["environment"] = values["environment"]
};

SourceSetupFile kubernetesFile = new(
"kubernetes.json",
"/etc/bower/kubernetes.json",
"Allow-listed Kubernetes container log source settings. Mounted read-only into the Bower DaemonSet.",
Json(config));

string primaryNs = namespaces[0];
string rbacSnippet =
$"# Namespaced read-only pod discovery (repeat per allowed namespace; never use ClusterRole)\n" +
$"apiVersion: rbac.authorization.k8s.io/v1\n" +
$"kind: Role\n" +
$"metadata:\n" +
$" name: bower-pod-reader\n" +
$" namespace: {primaryNs}\n" +
$"rules:\n" +
$" - apiGroups: [\"\"]\n" +
$" resources: [\"pods\"]\n" +
$" verbs: [\"get\", \"list\"]\n" +
$"---\n" +
$"# Deploy hardened DaemonSet from deploy/kubernetes/daemonset.yaml\n";

return new SourceSetupBuild(
[kubernetesFile, Expectations(values["application"], values["environment"], values["maximumSilenceMinutes"])],
[
new("kubernetes-daemonset", "BOWER_KUBERNETES_CONFIG", "/etc/bower/kubernetes.json"),
ExpectationsVariable()
],
[new SourceSetupSnippet(
"Kubernetes: namespaced Role (pods get/list only)",
"Apply in each allow-listed namespace alongside deploy/kubernetes/daemonset.yaml.",
"yaml",
rbacSnippet)],
ApplicationVerification(values["application"], values["environment"]));
}

public override JsonObject Sample(IReadOnlyDictionary<string, string> values)
{
IReadOnlyList<string> namespaces = List(values, "namespaces");
string primaryNs = namespaces.Count > 0 ? namespaces[0] : "security-apps";
return new JsonObject
{
["eventType"] = "auth_failure",
["eventAction"] = "container.auth.denied",
["eventResult"] = "failure",
["application"] = new JsonObject { ["name"] = values["application"], ["environment"] = values["environment"] },
["actor"] = new JsonObject { ["username"] = "service.user@example.org" },
["source"] = new JsonObject { ["ipAddress"] = "198.51.100.77" },
["attributes"] = new JsonObject
{
["k8s.namespace"] = primaryNs,
["k8s.pod"] = "auth-api-7d9f8c6b4-x2k9p",
["k8s.container"] = "auth-api",
["k8s.stream"] = "stdout",
["auth.detail"] = "Rejected bearer token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0ZXN0In0.dummysignature123456"
}
};
}

[GeneratedRegex("^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$")]
private static partial Regex DnsLabelPattern();

[GeneratedRegex("^[A-Za-z0-9._/-]{1,128}=[A-Za-z0-9._-]{1,63}$")]
private static partial Regex LabelSelectorPattern();

[GeneratedRegex("^[A-Za-z0-9._/-]{1,128}$")]
private static partial Regex LabelKeyPattern();
}
2 changes: 2 additions & 0 deletions src/Bower.Management.Api/SourceSetupRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ internal static partial class SourceSetupRegistry
new WindowsEventLogSourceSetup(),
new FileTailSourceSetup(),
new SqlServerSourceSetup(),
new JournaldSourceSetup(),
new KubernetesSourceSetup(),
new DockerSourceSetup(),
new AwsSourceSetup(),
new GcpSourceSetup(),
Expand Down
Loading