-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetectors_modern.go
More file actions
554 lines (465 loc) · 13.1 KB
/
Copy pathdetectors_modern.go
File metadata and controls
554 lines (465 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// Copyright © 2022-2023 Christian R. Vozar
// Licensed under the MIT License. All rights reserved.
package criprof
import (
"context"
"strings"
)
// Modern container runtimes and environments
const (
runtimePodman = "podman" // Podman (daemonless container engine)
runtimeCRIO = "cri-o" // CRI-O (Kubernetes CRI implementation)
runtimeFirecracker = "firecracker" // Firecracker microVM
runtimeKata = "kata" // Kata Containers (secure runtime)
runtimeGVisor = "gvisor" // gVisor (application kernel)
runtimeSysbox = "sysbox" // Sysbox (system containers)
// Cloud-specific schedulers
schedulerECS = "ecs" // AWS ECS
schedulerFargate = "fargate" // AWS Fargate
schedulerGKE = "gke" // Google Kubernetes Engine
schedulerAKS = "aks" // Azure Kubernetes Service
schedulerEKS = "eks" // Amazon EKS
schedulerCloudRun = "cloud-run" // Google Cloud Run
schedulerLambda = "lambda" // AWS Lambda (container image support)
schedulerACI = "aci" // Azure Container Instances
// Modern image formats
formatOCI = "oci" // OCI (Open Container Initiative)
formatSingularity = "singularity" // Singularity/Apptainer (HPC)
)
// PodmanDetector detects Podman container runtime
type PodmanDetector struct {
fs FileSystem
}
func (d *PodmanDetector) Name() string {
return "podman-marker"
}
func (d *PodmanDetector) Priority() int {
return 95
}
func (d *PodmanDetector) Detect(ctx context.Context) (*Detection, error) {
// Check for Podman-specific marker
if _, err := d.fs.Stat("/run/.containerenv"); err == nil {
// Read the file to check for Podman-specific content
data, err := d.fs.ReadFile("/run/.containerenv")
if err == nil && strings.Contains(string(data), "podman") {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimePodman,
Confidence: 0.95,
Source: d.Name(),
}, nil
}
// Generic containerenv could be Podman or CRI-O
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimePodman,
Confidence: 0.70,
Source: d.Name(),
}, nil
}
// Check for Podman environment variable
if _, ok := EnvironmentVariables["PODMAN_SYSTEMD_UNIT"]; ok {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimePodman,
Confidence: 0.90,
Source: d.Name(),
}, nil
}
return nil, nil
}
// CRIODetector detects CRI-O container runtime
type CRIODetector struct {
fs FileSystem
}
func (d *CRIODetector) Name() string {
return "cri-o-marker"
}
func (d *CRIODetector) Priority() int {
return 95
}
func (d *CRIODetector) Detect(ctx context.Context) (*Detection, error) {
// Check for CRI-O specific files
if _, err := d.fs.Stat("/var/run/crio/crio.sock"); err == nil {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeCRIO,
Confidence: 0.95,
Source: d.Name(),
}, nil
}
// Check cgroup for crio
data, err := d.fs.ReadFile("/proc/self/cgroup")
if err == nil && strings.Contains(string(data), "crio") {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeCRIO,
Confidence: 0.90,
Source: d.Name(),
}, nil
}
return nil, nil
}
// FirecrackerDetector detects Firecracker microVM
type FirecrackerDetector struct {
fs FileSystem
}
func (d *FirecrackerDetector) Name() string {
return "firecracker-dmi"
}
func (d *FirecrackerDetector) Priority() int {
return 90
}
func (d *FirecrackerDetector) Detect(ctx context.Context) (*Detection, error) {
// Firecracker sets specific DMI product name
data, err := d.fs.ReadFile("/sys/class/dmi/id/product_name")
if err == nil && strings.TrimSpace(string(data)) == "Firecracker" {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeFirecracker,
Confidence: 0.98,
Source: d.Name(),
}, nil
}
return nil, nil
}
// KataContainersDetector detects Kata Containers
type KataContainersDetector struct {
fs FileSystem
}
func (d *KataContainersDetector) Name() string {
return "kata-containers"
}
func (d *KataContainersDetector) Priority() int {
return 90
}
func (d *KataContainersDetector) Detect(ctx context.Context) (*Detection, error) {
// Check for Kata-specific markers
if _, err := d.fs.Stat("/run/kata-containers"); err == nil {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeKata,
Confidence: 0.95,
Source: d.Name(),
}, nil
}
// Check DMI for QEMU (Kata uses QEMU)
data, err := d.fs.ReadFile("/sys/class/dmi/id/product_name")
if err == nil && strings.Contains(string(data), "QEMU") {
// Lower confidence - could be any QEMU VM
data2, err2 := d.fs.ReadFile("/proc/cpuinfo")
if err2 == nil && strings.Contains(string(data2), "QEMU") {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeKata,
Confidence: 0.60,
Source: d.Name(),
}, nil
}
}
return nil, nil
}
// GVisorDetector detects gVisor (runsc)
type GVisorDetector struct {
fs FileSystem
}
func (d *GVisorDetector) Name() string {
return "gvisor-marker"
}
func (d *GVisorDetector) Priority() int {
return 90
}
func (d *GVisorDetector) Detect(ctx context.Context) (*Detection, error) {
// gVisor shows up in cgroups
data, err := d.fs.ReadFile("/proc/self/cgroup")
if err == nil && (strings.Contains(string(data), "runsc") || strings.Contains(string(data), "gvisor")) {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeGVisor,
Confidence: 0.95,
Source: d.Name(),
}, nil
}
// Check for gVisor-specific /proc entries
if _, err := d.fs.Stat("/proc/self/root/dev/gvisor"); err == nil {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeGVisor,
Confidence: 0.98,
Source: d.Name(),
}, nil
}
return nil, nil
}
// SysboxDetector detects Sysbox system containers
type SysboxDetector struct {
fs FileSystem
}
func (d *SysboxDetector) Name() string {
return "sysbox-marker"
}
func (d *SysboxDetector) Priority() int {
return 90
}
func (d *SysboxDetector) Detect(ctx context.Context) (*Detection, error) {
// Sysbox sets specific environment variable
if _, ok := EnvironmentVariables["SYSBOX_CONTAINER"]; ok {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeSysbox,
Confidence: 0.98,
Source: d.Name(),
}, nil
}
// Check for Sysbox markers in cgroup
data, err := d.fs.ReadFile("/proc/self/cgroup")
if err == nil && strings.Contains(string(data), "sysbox") {
return &Detection{
Type: DetectionTypeRuntime,
Value: runtimeSysbox,
Confidence: 0.90,
Source: d.Name(),
}, nil
}
return nil, nil
}
// AWS ECS Detector
type ECSDetector struct{}
func (d *ECSDetector) Name() string {
return "aws-ecs"
}
func (d *ECSDetector) Priority() int {
return 85
}
func (d *ECSDetector) Detect(ctx context.Context) (*Detection, error) {
// ECS sets specific environment variables
if _, ok := EnvironmentVariables["ECS_CONTAINER_METADATA_URI"]; ok {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerECS,
Confidence: 0.98,
Source: d.Name(),
}, nil
}
if _, ok := EnvironmentVariables["ECS_CONTAINER_METADATA_URI_V4"]; ok {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerECS,
Confidence: 0.98,
Source: d.Name(),
}, nil
}
return nil, nil
}
// AWS Fargate Detector
type FargateDetector struct{}
func (d *FargateDetector) Name() string {
return "aws-fargate"
}
func (d *FargateDetector) Priority() int {
return 85
}
func (d *FargateDetector) Detect(ctx context.Context) (*Detection, error) {
// Fargate is ECS + specific launch type
if launchType, ok := EnvironmentVariables["AWS_EXECUTION_ENV"]; ok {
if strings.Contains(strings.ToLower(launchType), "fargate") {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerFargate,
Confidence: 0.99,
Source: d.Name(),
}, nil
}
}
// Check for Fargate-specific metadata
if _, ok := EnvironmentVariables["ECS_CONTAINER_METADATA_URI_V4"]; ok {
if _, ok2 := EnvironmentVariables["AWS_EXECUTION_ENV"]; ok2 {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerFargate,
Confidence: 0.85,
Source: d.Name(),
}, nil
}
}
return nil, nil
}
// Google Cloud Run Detector
type CloudRunDetector struct{}
func (d *CloudRunDetector) Name() string {
return "google-cloud-run"
}
func (d *CloudRunDetector) Priority() int {
return 85
}
func (d *CloudRunDetector) Detect(ctx context.Context) (*Detection, error) {
// Cloud Run sets K_SERVICE environment variable
if _, ok := EnvironmentVariables["K_SERVICE"]; ok {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerCloudRun,
Confidence: 0.98,
Source: d.Name(),
}, nil
}
// Also check for K_REVISION and K_CONFIGURATION
if _, ok := EnvironmentVariables["K_REVISION"]; ok {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerCloudRun,
Confidence: 0.95,
Source: d.Name(),
}, nil
}
return nil, nil
}
// AWS Lambda Container Detector
type LambdaContainerDetector struct{}
func (d *LambdaContainerDetector) Name() string {
return "aws-lambda-container"
}
func (d *LambdaContainerDetector) Priority() int {
return 85
}
func (d *LambdaContainerDetector) Detect(ctx context.Context) (*Detection, error) {
// Lambda sets specific environment variables
if _, ok := EnvironmentVariables["AWS_LAMBDA_FUNCTION_NAME"]; ok {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerLambda,
Confidence: 0.99,
Source: d.Name(),
}, nil
}
if _, ok := EnvironmentVariables["LAMBDA_TASK_ROOT"]; ok {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerLambda,
Confidence: 0.98,
Source: d.Name(),
}, nil
}
return nil, nil
}
// Azure Container Instances Detector
type ACIDetector struct{}
func (d *ACIDetector) Name() string {
return "azure-container-instances"
}
func (d *ACIDetector) Priority() int {
return 85
}
func (d *ACIDetector) Detect(ctx context.Context) (*Detection, error) {
// ACI sets specific environment variables
if _, ok := EnvironmentVariables["ACI_RESOURCE_GROUP"]; ok {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerACI,
Confidence: 0.98,
Source: d.Name(),
}, nil
}
if _, ok := EnvironmentVariables["CONTAINER_GROUP_NAME"]; ok {
return &Detection{
Type: DetectionTypeScheduler,
Value: schedulerACI,
Confidence: 0.90,
Source: d.Name(),
}, nil
}
return nil, nil
}
// Singularity/Apptainer Detector (HPC environments)
type SingularityDetector struct{}
func (d *SingularityDetector) Name() string {
return "singularity-apptainer"
}
func (d *SingularityDetector) Priority() int {
return 90
}
func (d *SingularityDetector) Detect(ctx context.Context) (*Detection, error) {
// Singularity/Apptainer set specific environment variables
if _, ok := EnvironmentVariables["SINGULARITY_CONTAINER"]; ok {
return &Detection{
Type: DetectionTypeRuntime,
Value: "singularity",
Confidence: 0.99,
Source: d.Name(),
}, nil
}
if _, ok := EnvironmentVariables["APPTAINER_CONTAINER"]; ok {
return &Detection{
Type: DetectionTypeRuntime,
Value: "apptainer",
Confidence: 0.99,
Source: d.Name(),
}, nil
}
if _, ok := EnvironmentVariables["SINGULARITY_NAME"]; ok {
return &Detection{
Type: DetectionTypeRuntime,
Value: "singularity",
Confidence: 0.95,
Source: d.Name(),
}, nil
}
return nil, nil
}
// OCI Image Format Detector
type OCIImageDetector struct {
fs FileSystem
}
func (d *OCIImageDetector) Name() string {
return "oci-image-format"
}
func (d *OCIImageDetector) Priority() int {
return 85
}
func (d *OCIImageDetector) Detect(ctx context.Context) (*Detection, error) {
// Check for OCI-specific markers
if _, err := d.fs.Stat("/var/lib/containers"); err == nil {
return &Detection{
Type: DetectionTypeImageFormat,
Value: formatOCI,
Confidence: 0.80,
Source: d.Name(),
}, nil
}
// Podman typically uses OCI format
if _, ok := EnvironmentVariables["PODMAN_SYSTEMD_UNIT"]; ok {
return &Detection{
Type: DetectionTypeImageFormat,
Value: formatOCI,
Confidence: 0.85,
Source: d.Name(),
}, nil
}
return nil, nil
}
// Singularity Image Format Detector
type SingularityImageDetector struct{}
func (d *SingularityImageDetector) Name() string {
return "singularity-image-format"
}
func (d *SingularityImageDetector) Priority() int {
return 85
}
func (d *SingularityImageDetector) Detect(ctx context.Context) (*Detection, error) {
if _, ok := EnvironmentVariables["SINGULARITY_CONTAINER"]; ok {
return &Detection{
Type: DetectionTypeImageFormat,
Value: formatSingularity,
Confidence: 0.95,
Source: d.Name(),
}, nil
}
if _, ok := EnvironmentVariables["APPTAINER_CONTAINER"]; ok {
return &Detection{
Type: DetectionTypeImageFormat,
Value: formatSingularity,
Confidence: 0.95,
Source: d.Name(),
}, nil
}
return nil, nil
}