mirror of
https://github.com/fleetdm/fleet
synced 2026-04-23 14:37:17 +00:00
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #38393 # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. See [Changes files](https://github.com/fleetdm/fleet/blob/main/docs/Contributing/guides/committing-changes.md#changes-files) for more information. ## Testing - [x] QA'd all new/changed functionality manually ## fleetd/orbit/Fleet Desktop - [x] If the change applies to only one platform, confirmed that `runtime.GOOS` is used as needed to isolate changes - [x] Verified that fleetd runs on macOS, Linux and Windows (Linux only) --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com>
109 lines
2.9 KiB
Go
109 lines
2.9 KiB
Go
//go:build linux
|
|
|
|
package containerd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/containerd/containerd"
|
|
"github.com/containerd/containerd/cio"
|
|
"github.com/containerd/containerd/namespaces"
|
|
"github.com/osquery/osquery-go/plugin/table"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
// ContainersColumns is the schema of the containerd_containers table.
|
|
func ContainersColumns() []table.ColumnDefinition {
|
|
return []table.ColumnDefinition{
|
|
table.TextColumn("namespace"),
|
|
table.TextColumn("id"),
|
|
table.TextColumn("image"),
|
|
table.TextColumn("image_digest"),
|
|
table.TextColumn("state"),
|
|
table.BigIntColumn("created"),
|
|
table.TextColumn("runtime"),
|
|
table.TextColumn("command"),
|
|
table.BigIntColumn("pid"),
|
|
}
|
|
}
|
|
|
|
// GenerateContainers is called to return the results for the containerd_containers table at query time.
|
|
// Constraints for generating can be retrieved from the queryContext.
|
|
func GenerateContainers(ctx context.Context, queryContext table.QueryContext) ([]map[string]string, error) {
|
|
client, err := containerd.New("/run/containerd/containerd.sock")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to connect to containerd: %v", err)
|
|
}
|
|
defer client.Close()
|
|
|
|
// Get all namespaces so we can iterate over them
|
|
namespacesList, err := client.NamespaceService().List(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to list namespaces: %v", err)
|
|
}
|
|
|
|
rows := []map[string]string{}
|
|
for _, namespace := range namespacesList {
|
|
nsCtx := namespaces.WithNamespace(ctx, namespace)
|
|
|
|
containers, err := client.Containers(nsCtx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to list containers: %v", err)
|
|
}
|
|
|
|
for _, container := range containers {
|
|
info, err := container.Info(nsCtx)
|
|
if err != nil {
|
|
log.Printf("Failed to get info for container %s: %v", container.ID(), err)
|
|
continue
|
|
}
|
|
|
|
// Get image digest if possible
|
|
imageDigest := ""
|
|
img, err := container.Image(nsCtx)
|
|
if err == nil {
|
|
imageDigest = img.Target().Digest.String()
|
|
}
|
|
|
|
// Get state and pid from task if possible
|
|
state := "unknown"
|
|
pid := ""
|
|
command := ""
|
|
task, err := container.Task(nsCtx, cio.Load)
|
|
if err == nil {
|
|
status, err := task.Status(nsCtx)
|
|
if err == nil {
|
|
state = string(status.Status)
|
|
}
|
|
taskPid := task.Pid()
|
|
if taskPid > 0 {
|
|
pid = fmt.Sprintf("%d", taskPid)
|
|
}
|
|
// Try to get the command from the process spec
|
|
spec, err := container.Spec(nsCtx)
|
|
if err == nil && spec.Process != nil && len(spec.Process.Args) > 0 {
|
|
command = strings.Join(spec.Process.Args, " ")
|
|
}
|
|
} else {
|
|
state = "stopped"
|
|
}
|
|
|
|
row := map[string]string{
|
|
"namespace": namespace,
|
|
"id": info.ID,
|
|
"image": info.Image,
|
|
"image_digest": imageDigest,
|
|
"state": state,
|
|
"created": fmt.Sprintf("%d", info.CreatedAt.Unix()),
|
|
"runtime": info.Runtime.Name,
|
|
"pid": pid,
|
|
"command": command,
|
|
}
|
|
rows = append(rows, row)
|
|
}
|
|
}
|
|
|
|
return rows, nil
|
|
}
|