loki-mode 5.42.2 → 5.46.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,275 @@
1
+ # Network Security
2
+
3
+ Network egress control and isolation for Loki Mode deployments.
4
+
5
+ ## Overview
6
+
7
+ This guide covers network-level security controls for restricting outbound network access from Loki Mode containers and pods to only the AI API endpoints required for operation.
8
+
9
+ ## Environment Variables
10
+
11
+ The following environment variables control network egress policy enforcement:
12
+
13
+ | Variable | Default | Description |
14
+ |----------|---------|-------------|
15
+ | `LOKI_NETWORK_EGRESS_POLICY` | `unrestricted` | `unrestricted` (default), `ai-only` (restrict to AI APIs), `none` (block all outbound) |
16
+ | `LOKI_ALLOWED_HOSTS` | (empty) | Comma-separated list of additional hostnames to allow when egress policy is `ai-only` |
17
+ | `LOKI_BLOCK_METADATA_ENDPOINT` | `false` | Block cloud metadata endpoint (169.254.169.254) from within the application |
18
+
19
+ Note: These variables are reserved for future application-level enforcement. Currently, network security is implemented at the infrastructure level using Docker networks or Kubernetes NetworkPolicy.
20
+
21
+ ## Docker Network Isolation
22
+
23
+ ### Custom Network with ICC Disabled
24
+
25
+ Create an isolated Docker network that prevents inter-container communication and restricts egress to known AI API endpoints:
26
+
27
+ ```bash
28
+ # Create an isolated bridge network with ICC disabled
29
+ docker network create \
30
+ --driver bridge \
31
+ --opt com.docker.network.bridge.enable_icc=false \
32
+ --subnet 172.28.0.0/16 \
33
+ loki-isolated
34
+ ```
35
+
36
+ ### Blocking the Cloud Metadata Endpoint
37
+
38
+ Cloud providers expose instance metadata at `169.254.169.254`. This endpoint can leak credentials (IAM roles, service account tokens). Block it from within the container host:
39
+
40
+ ```bash
41
+ # Block metadata endpoint for containers on the loki-isolated network
42
+ iptables -I DOCKER-USER -d 169.254.169.254 -j DROP
43
+ ```
44
+
45
+ ### Allowing Only AI API Endpoints
46
+
47
+ Restrict outbound traffic to only the AI provider API endpoints that Loki Mode requires:
48
+
49
+ ```bash
50
+ # Allow DNS resolution
51
+ iptables -A DOCKER-USER -p udp --dport 53 -j ACCEPT
52
+ iptables -A DOCKER-USER -p tcp --dport 53 -j ACCEPT
53
+
54
+ # Allow HTTPS to AI API endpoints only
55
+ # Anthropic (Claude)
56
+ iptables -A DOCKER-USER -d api.anthropic.com -p tcp --dport 443 -j ACCEPT
57
+ # OpenAI (Codex)
58
+ iptables -A DOCKER-USER -d api.openai.com -p tcp --dport 443 -j ACCEPT
59
+ # Google (Gemini)
60
+ iptables -A DOCKER-USER -d generativelanguage.googleapis.com -p tcp --dport 443 -j ACCEPT
61
+
62
+ # Drop all other outbound traffic from the isolated network
63
+ iptables -A DOCKER-USER -s 172.28.0.0/16 -j DROP
64
+ ```
65
+
66
+ ### Docker Compose Example
67
+
68
+ ```yaml
69
+ version: "3.8"
70
+
71
+ services:
72
+ loki:
73
+ image: asklokesh/loki-mode:latest
74
+ networks:
75
+ - loki-isolated
76
+ environment:
77
+ - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
78
+ security_opt:
79
+ - no-new-privileges:true
80
+ read_only: true
81
+ tmpfs:
82
+ - /tmp
83
+ volumes:
84
+ - ./workspace:/workspace
85
+
86
+ networks:
87
+ loki-isolated:
88
+ driver: bridge
89
+ driver_opts:
90
+ com.docker.network.bridge.enable_icc: "false"
91
+ ```
92
+
93
+ Note: Docker DNS-based iptables rules resolve at rule creation time. If provider IPs change, rules must be refreshed. For production use, consider a forward proxy (e.g., Squid, Envoy) with domain-based allowlisting instead of raw iptables.
94
+
95
+ ## Kubernetes NetworkPolicy
96
+
97
+ ### Egress-Restricted NetworkPolicy
98
+
99
+ The following `NetworkPolicy` restricts pod egress to only the AI API endpoints and DNS:
100
+
101
+ ```yaml
102
+ apiVersion: networking.k8s.io/v1
103
+ kind: NetworkPolicy
104
+ metadata:
105
+ name: loki-egress-policy
106
+ namespace: loki
107
+ spec:
108
+ podSelector:
109
+ matchLabels:
110
+ app: loki-mode
111
+ policyTypes:
112
+ - Egress
113
+ egress:
114
+ # Allow DNS resolution
115
+ - to: []
116
+ ports:
117
+ - protocol: UDP
118
+ port: 53
119
+ - protocol: TCP
120
+ port: 53
121
+ # Allow HTTPS to AI API endpoints
122
+ - to: []
123
+ ports:
124
+ - protocol: TCP
125
+ port: 443
126
+ ```
127
+
128
+ Important: Standard Kubernetes `NetworkPolicy` only supports IP-based rules, not domain names. To enforce domain-level egress control, use one of these approaches:
129
+
130
+ - **Cilium**: Supports `CiliumNetworkPolicy` with FQDN-based egress rules
131
+ - **Calico Enterprise**: Supports DNS-based network policies
132
+ - **Egress Gateway**: Route traffic through a proxy that enforces domain allowlists
133
+
134
+ ### Pod Security Context
135
+
136
+ Run Loki Mode pods with a restrictive security context:
137
+
138
+ ```yaml
139
+ apiVersion: v1
140
+ kind: Pod
141
+ metadata:
142
+ name: loki-worker
143
+ namespace: loki
144
+ labels:
145
+ app: loki-mode
146
+ spec:
147
+ securityContext:
148
+ runAsNonRoot: true
149
+ runAsUser: 1000
150
+ runAsGroup: 1000
151
+ fsGroup: 1000
152
+ seccompProfile:
153
+ type: RuntimeDefault
154
+ containers:
155
+ - name: loki
156
+ image: asklokesh/loki-mode:latest
157
+ securityContext:
158
+ allowPrivilegeEscalation: false
159
+ readOnlyRootFilesystem: true
160
+ capabilities:
161
+ drop:
162
+ - ALL
163
+ volumeMounts:
164
+ - name: workspace
165
+ mountPath: /workspace
166
+ - name: tmp
167
+ mountPath: /tmp
168
+ env:
169
+ - name: ANTHROPIC_API_KEY
170
+ valueFrom:
171
+ secretKeyRef:
172
+ name: loki-secrets
173
+ key: anthropic-api-key
174
+ volumes:
175
+ - name: workspace
176
+ emptyDir: {}
177
+ - name: tmp
178
+ emptyDir:
179
+ medium: Memory
180
+ sizeLimit: 256Mi
181
+ ```
182
+
183
+ ## TLS/HTTPS for Dashboard (v5.36.0)
184
+
185
+ Enable encrypted dashboard connections:
186
+
187
+ ```bash
188
+ # Using environment variables
189
+ export LOKI_TLS_ENABLED=true
190
+ export LOKI_TLS_CERT=/path/to/cert.pem
191
+ export LOKI_TLS_KEY=/path/to/key.pem
192
+
193
+ loki start ./prd.md
194
+ ```
195
+
196
+ Or via CLI flags:
197
+
198
+ ```bash
199
+ loki dashboard start --tls-cert /path/to/cert.pem --tls-key /path/to/key.pem
200
+ ```
201
+
202
+ ### Self-Signed Certificate (Development)
203
+
204
+ ```bash
205
+ openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes \
206
+ -subj "/CN=localhost"
207
+
208
+ export LOKI_TLS_CERT=cert.pem
209
+ export LOKI_TLS_KEY=key.pem
210
+ ```
211
+
212
+ ### Production TLS
213
+
214
+ For production deployments, use certificates from a trusted CA:
215
+
216
+ - Let's Encrypt (free, automated)
217
+ - AWS Certificate Manager
218
+ - Your organization's internal CA
219
+
220
+ ## Best Practices
221
+
222
+ ### Security Checklist
223
+
224
+ - Enable TLS/HTTPS for dashboard in production
225
+ - Use network policies to restrict egress to AI API endpoints only
226
+ - Block cloud metadata endpoint (169.254.169.254)
227
+ - Run containers with read-only root filesystem
228
+ - Use non-root user (UID 1000)
229
+ - Drop all capabilities
230
+ - Enable seccomp profile
231
+ - Use separate networks for different security zones
232
+ - Monitor network traffic for anomalies
233
+
234
+ ### Production Deployment
235
+
236
+ 1. Enable TLS with valid certificates
237
+ 2. Configure network policies or iptables rules
238
+ 3. Use a reverse proxy (nginx, Envoy) for additional security headers
239
+ 4. Enable audit logging to track network-related events
240
+ 5. Monitor `/metrics` endpoint for unexpected traffic patterns
241
+
242
+ ## Troubleshooting
243
+
244
+ ### Connection to AI API Fails
245
+
246
+ ```bash
247
+ # Check network policy
248
+ kubectl describe networkpolicy loki-egress-policy
249
+
250
+ # Test DNS resolution
251
+ kubectl exec -it loki-pod -- nslookup api.anthropic.com
252
+
253
+ # Check iptables rules
254
+ sudo iptables -L DOCKER-USER -n -v
255
+ ```
256
+
257
+ ### Dashboard HTTPS Not Working
258
+
259
+ ```bash
260
+ # Verify certificate files exist and are readable
261
+ ls -la /path/to/cert.pem /path/to/key.pem
262
+
263
+ # Check certificate validity
264
+ openssl x509 -in cert.pem -text -noout
265
+
266
+ # Verify dashboard is listening on HTTPS
267
+ curl -k https://localhost:57374/health
268
+ ```
269
+
270
+ ## See Also
271
+
272
+ - [Authentication Guide](authentication.md) - OIDC/SSO setup
273
+ - [Authorization Guide](authorization.md) - RBAC configuration
274
+ - [Audit Logging](audit-logging.md) - Security event tracking
275
+ - [Enterprise Features](../wiki/Enterprise-Features.md) - Complete enterprise guide