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
29 changes: 29 additions & 0 deletions Bower.sln
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bower.DevSinks", "tools\Bow
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bower.AppHost.Tests", "tests\Bower.AppHost.Tests\Bower.AppHost.Tests.csproj", "{827CAE03-02C1-4625-AB39-B32FAF688A1D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bower.Source.Journald", "src\Bower.Source.Journald\Bower.Source.Journald.csproj", "{D784F971-24BA-435F-831E-4442A33FBC02}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bower.Source.Kubernetes", "src\Bower.Source.Kubernetes\Bower.Source.Kubernetes.csproj", "{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -663,6 +667,30 @@ Global
{827CAE03-02C1-4625-AB39-B32FAF688A1D}.Release|x64.Build.0 = Release|Any CPU
{827CAE03-02C1-4625-AB39-B32FAF688A1D}.Release|x86.ActiveCfg = Release|Any CPU
{827CAE03-02C1-4625-AB39-B32FAF688A1D}.Release|x86.Build.0 = Release|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Debug|x64.ActiveCfg = Debug|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Debug|x64.Build.0 = Debug|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Debug|x86.ActiveCfg = Debug|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Debug|x86.Build.0 = Debug|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Release|Any CPU.Build.0 = Release|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Release|x64.ActiveCfg = Release|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Release|x64.Build.0 = Release|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Release|x86.ActiveCfg = Release|Any CPU
{D784F971-24BA-435F-831E-4442A33FBC02}.Release|x86.Build.0 = Release|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Debug|x64.ActiveCfg = Debug|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Debug|x64.Build.0 = Debug|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Debug|x86.ActiveCfg = Debug|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Debug|x86.Build.0 = Debug|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Release|Any CPU.Build.0 = Release|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Release|x64.ActiveCfg = Release|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Release|x64.Build.0 = Release|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Release|x86.ActiveCfg = Release|Any CPU
{DBB32FBD-107E-4DDC-A110-F00C1A3EC9D9}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -700,5 +728,6 @@ Global
{29ABB074-0C7B-4729-B57D-8856A47AA932} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{BBF9180B-5DEE-43BB-8D0B-B11F7D368A2E} = {07C2787E-EAC7-C090-1BA3-A61EC2A24D84}
{827CAE03-02C1-4625-AB39-B32FAF688A1D} = {0AB3BF05-4346-4AA6-1389-037BE0695223}
{D784F971-24BA-435F-831E-4442A33FBC02} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
EndGlobalSection
EndGlobal
111 changes: 111 additions & 0 deletions deploy/kubernetes/daemonset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Hardened read-only DaemonSet for allow-listed Kubernetes container security telemetry (#92).
# RBAC is strictly namespaced (Role, never ClusterRole) and limited to get/list pods in
# explicitly approved namespaces. Replace 'app-production' with each approved namespace.
apiVersion: v1
kind: ServiceAccount
metadata:
name: bower-node-reader
namespace: bower-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: bower-pod-metadata-reader
namespace: app-production
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: bower-pod-metadata-reader
namespace: app-production
subjects:
- kind: ServiceAccount
name: bower-node-reader
namespace: bower-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: bower-pod-metadata-reader
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: bower-node-collector
namespace: bower-system
labels:
app.kubernetes.io/name: bower-node-collector
spec:
selector:
matchLabels:
app.kubernetes.io/name: bower-node-collector
template:
metadata:
labels:
app.kubernetes.io/name: bower-node-collector
spec:
serviceAccountName: bower-node-reader
automountServiceAccountToken: true
securityContext:
runAsNonRoot: true
runAsUser: 1654
runAsGroup: 1654
fsGroup: 1654
seccompProfile:
type: RuntimeDefault
containers:
- name: collector
image: ghcr.io/replace-me/bower:1.0.1
imagePullPolicy: IfNotPresent
env:
- name: BOWER_KUBERNETES_NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
- name: BOWER_KUBERNETES_NAMESPACES
value: app-production
- name: BOWER_KUBERNETES_LABEL_SELECTORS
value: bower.io/collect=true
- name: BOWER_KUBERNETES_ALLOWED_LABELS
value: app.kubernetes.io/name,app.kubernetes.io/component
- name: BOWER_QUEUE_PATH
value: /var/lib/bower/queue.db
- name: BOWER_POLICY_DIRECTORY
value: /app/policies/default
volumeMounts:
- name: varlogpods
mountPath: /var/log/pods
readOnly: true
- name: state
mountPath: /var/lib/bower
- name: temporary
mountPath: /tmp
resources:
requests:
cpu: 50m
memory: 128Mi
ephemeral-storage: 64Mi
limits:
cpu: 500m
memory: 256Mi
ephemeral-storage: 256Mi
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
volumes:
- name: varlogpods
hostPath:
path: /var/log/pods
type: Directory
- name: state
hostPath:
path: /var/lib/bower
type: DirectoryOrCreate
- name: temporary
emptyDir:
sizeLimit: 64Mi
36 changes: 36 additions & 0 deletions docs/deployment/journald-source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# systemd journald source

`Bower.Source.Journald` reads security-relevant entries directly from the host's
systemd journal (`libsystemd.so.0`) using explicit unit and `SYSLOG_IDENTIFIER`
allow-lists and durable SQLite cursors.

## Selection and default deny

- Configuration (`JournaldSourceOptions`) **requires** at least one explicit systemd
unit (`Units`, e.g. `sshd.service`) or `SYSLOG_IDENTIFIER` (`Identifiers`, e.g. `sshd`).
- Wildcards (`*`, `?`) and collect-all subscriptions are rejected at validation time.
- Even when `LibSystemdJournalReader` pushes `_SYSTEMD_UNIT=` / `SYSLOG_IDENTIFIER=`
disjunctions into `libsystemd`, `JournaldSource` enforces the same allow-list in
managed code before serialising any entry.
- Reading an entry never means delivering it: every record still flows through a signed
pack parser (such as `packs/linux-journald-ssh`), the Privacy & Secret Protection
Engine, and default-deny telemetry policies.

## Durability and backpressure

- Cursors are stored in `ISourceCursorStore` under `journald:<subscriptionName>`.
- A cursor advances **only after** the downstream processor acknowledges durable enqueue
or a definitive drop (unselected unit, malformed or oversized entry).
- If the collector crashes or downstream signals backpressure, polling stops before
advancing past the unacknowledged entry so restart resumes with zero loss; deterministic
event IDs derived from `__CURSOR` deduplicate retries.
- Per-record size is capped at 32 KiB and batch sizes are capped at 500 entries.

## Linux prerequisites

- Requires Linux with `libsystemd.so.0` and read access to the system journal (typically
membership in the `systemd-journal` group for the `bower` service account — no root
privileges required).
- Keep the `IJournalReader` seam when writing tests on macOS or Windows so unit tests
exercise cursor recovery, allow-list enforcement, malformed inputs and backpressure
without a live systemd daemon.
34 changes: 34 additions & 0 deletions docs/deployment/kubernetes-source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Kubernetes container log source

`Bower.Source.Kubernetes` collects allow-listed security telemetry from container logs
under `/var/log/pods` on Linux nodes with minimal pod metadata enrichment.

## Selection and least privilege

- **No cluster-wide default collection:** `KubernetesLogSourceOptions` requires explicit
allow-listed `Namespaces` (DNS-1123 names, max 32) and explicit `LabelSelectors`
(`key=value`, max 16).
- **No pod annotation auto-discovery:** pods cannot opt themselves into collection via
annotations. Only operator-approved namespaces and label selectors are queried.
- **Namespaced RBAC only:** grant the DaemonSet `ServiceAccount` a namespaced `Role`
(`get`, `list` on `pods`) strictly in each approved namespace — see
[`deploy/kubernetes/daemonset.yaml`](../../deploy/kubernetes/daemonset.yaml). Never
use a `ClusterRole` or secret/exec permissions.
- **Minimal metadata:** attaches `k8s.namespace`, `k8s.pod`, `k8s.container`,
`k8s.stream`, and only pod label keys explicitly named in `AllowedMetadataLabels`. All
labels pass through Bower's Privacy & Secret Protection Engine before persistence.

## CRI parsing, rotation and checkpoints

- Reuses `Bower.Source.File.FileTailReader` for symlink-safe, bounded file reads with
rename and copytruncate rotation detection.
- Parses standard CRI container log lines (`<RFC3339 timestamp> <stdout|stderr> <P|F> <message>`),
joining partial (`P`) fragments up to `MaxPartialBytes` (default 64 KiB) until the final
(`F`) line arrives. Malformed and oversized lines are counted and skipped safely.
- Only validated Bower `SecurityEventEnvelope` JSON records are forwarded; unrecognised
unstructured stdout noise is dropped at the source.
- Checkpoints (`FileTailCursor`) are persisted in `ISourceCursorStore` under
`k8s:<namespace>/<podUid>/<container>` strictly **after** caller acknowledgement,
surviving restarts and log rotation without loss or duplicates. Deterministic `EventId`
values derived from `(podUid, container, fileFingerprint, startOffset)` deduplicate
replays.
16 changes: 16 additions & 0 deletions docs/security/threat-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ evidence.
(including not-selected, rejected, invalid and oversized records); event ids from
channel, computer, record id and creation time deduplicate re-reads. Counters and
failure codes only. See [Windows Event Log source](../deployment/windows-event-log-source.md).
- systemd journald source (`Bower.Source.Journald`, new trust boundary: host journal
read via `libsystemd.so.0`): requires explicit unit or `SYSLOG_IDENTIFIER`
allow-lists (wildcards and collect-all forms are rejected at load and enforced in
managed code). Records are bounded to 32 KiB, mapped through signed pack parsers,
redacted before persistence, and cursors advance in `ISourceCursorStore` only after
durable acknowledgement. The service account needs only read membership in
`systemd-journal`, never root. See [journald source](../deployment/journald-source.md).
- Kubernetes container log source (`Bower.Source.Kubernetes`, new trust boundary:
namespaced Kubernetes API pod metadata lookup and read-only `/var/log/pods` mount):
enforces explicit namespace and label-selector allow-lists with no cluster-wide
default and no pod annotation auto-discovery. RBAC is limited to namespaced `Role`
`get`/`list` on `pods` in approved namespaces; ServiceAccount tokens are read only
from files and never logged. File reads reuse `Bower.Source.File`'s symlink-safe,
bounded tailer; CRI partial joins are capped; only allow-listed metadata labels are
attached (and pass through the privacy engine); non-envelope stdout noise is dropped.
See [Kubernetes source](../deployment/kubernetes-source.md).
- Event injection: the collector requires a shared bearer token
(`BOWER_INGEST_TOKEN`, compared in constant time) whenever it listens beyond
loopback and refuses to start without one. Kubernetes manifests add a
Expand Down
16 changes: 16 additions & 0 deletions src/Bower.Source.Journald/AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Bower.Source.Journald instructions

Bounded, allow-listed systemd journald source.

- Default deny: configuration must name at least one explicit systemd unit or
`SYSLOG_IDENTIFIER`. Wildcards and collect-all subscriptions are refused.
- Enforce unit and identifier allow-lists in managed code even when the underlying
reader applies native filters.
- Never log raw `MESSAGE` text or raw journal entries. Diagnostics report counters
and safe metadata codes only.
- Persist the journald cursor in `ISourceCursorStore` strictly **after** the caller
acknowledges durable enqueue or definitive drop. Never advance past an
unacknowledged record.
- Bound per-record bytes (at most 32 KiB) and batch counts; honour cancellation.
- Keep `LibSystemdJournalReader` behind `IJournalReader` so cursor, filtering,
malformed-input and backpressure logic remain platform-neutral and testable.
13 changes: 13 additions & 0 deletions src/Bower.Source.Journald/Bower.Source.Journald.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!-- LibraryImport for Linux libsystemd.so.0 journald reading (LibSystemdJournalReader.cs). -->
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../Bower.Abstractions/Bower.Abstractions.csproj" />
<ProjectReference Include="../Bower.Contracts/Bower.Contracts.csproj" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Bower.UnitTests" />
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions src/Bower.Source.Journald/IJournalReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace Bower.Source.Journald;

/// <summary>One raw allow-listed systemd journal entry.</summary>
public sealed record JournaldEntry(
string Cursor,
long RealtimeMicroseconds,
string? Unit,
string? SyslogIdentifier,
string Message,
string? Priority = null,
string? Hostname = null);

/// <summary>
/// Platform seam for reading systemd journald entries sequentially after an opaque cursor.
/// </summary>
public interface IJournalReader : IDisposable
{
ValueTask<IReadOnlyList<JournaldEntry>> ReadBatchAsync(
string? afterCursor,
int maxRecords,
CancellationToken cancellationToken = default);
}
Loading
Loading