Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image name in container filter #2394

Merged
merged 1 commit into from
Dec 13, 2024
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
28 changes: 23 additions & 5 deletions deepfence_agent/tools/apache/scope/probe/docker/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ func (r *Reporter) Report() (report.Report, error) {
return report.MakeReport(), nil
}

var imageIDTagMap map[string]basicImage
result := report.MakeReport()
result.Container = r.containerTopology(localAddrs)
result.ContainerImage = r.containerImageTopology()
result.ContainerImage, imageIDTagMap = r.containerImageTopology()
result.Container = r.containerTopology(localAddrs, imageIDTagMap)
result.Overlay = r.overlayTopology()
return result, nil
}
Expand All @@ -73,7 +74,7 @@ func getLocalIPs() ([]string, []net.IP, error) {
return ips, addrs, nil
}

func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology {
func (r *Reporter) containerTopology(localAddrs []net.IP, imageIDTagMap map[string]basicImage) report.Topology {
result := report.MakeTopology()
nodes := []report.TopologyNode{}
r.registry.WalkContainers(func(c Container) {
Expand Down Expand Up @@ -117,6 +118,11 @@ func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology {
if node.Metadata.NodeID == "" {
continue
}
if basicImageDetails, ok := imageIDTagMap[node.Metadata.DockerImageID]; ok {
node.Metadata.ImageNameWithTag = basicImageDetails.ImageNameWithTag
node.Metadata.ImageName = basicImageDetails.ImageName
node.Metadata.ImageTag = basicImageDetails.ImageTag
}
var isInHostNamespace bool
node.Sets, isInHostNamespace = networkInfo(node.Metadata.NodeID)
tags, ok := containerImageTags[node.Metadata.NodeID]
Expand All @@ -141,9 +147,16 @@ func (r *Reporter) containerTopology(localAddrs []net.IP) report.Topology {
return result
}

func (r *Reporter) containerImageTopology() report.Topology {
type basicImage struct {
ImageName string `json:"docker_image_name,omitempty"`
ImageNameWithTag string `json:"docker_image_name_with_tag,omitempty"`
ImageTag string `json:"docker_image_tag,omitempty"`
}

func (r *Reporter) containerImageTopology() (report.Topology, map[string]basicImage) {
result := report.MakeTopology()
imageTagsMap := r.registry.GetImageTags()
imageIDTagMap := make(map[string]basicImage)
r.registry.WalkImages(func(image docker_client.APIImages) {
imageID := trimImageID(image.ID)
shortImageID := getShortImageID(imageID)
Expand All @@ -165,6 +178,11 @@ func (r *Reporter) containerImageTopology() report.Topology {
metadata.ImageNameWithTag = imageFullName
metadata.ImageName = ImageNameWithoutTag(imageFullName)
metadata.ImageTag = ImageNameTag(imageFullName)
imageIDTagMap[imageID] = basicImage{
ImageName: metadata.ImageName,
ImageNameWithTag: metadata.ImageNameWithTag,
ImageTag: metadata.ImageTag,
}
}
var tags []string
var ok bool
Expand Down Expand Up @@ -194,7 +212,7 @@ func (r *Reporter) containerImageTopology() report.Topology {
})
})

return result
return result, imageIDTagMap
}

func (r *Reporter) overlayTopology() report.Topology {
Expand Down
4 changes: 4 additions & 0 deletions deepfence_server/handler/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func (h *Handler) CompleteContainerInfo(w http.ResponseWriter, r *http.Request)
genericCompleteInfoHandler[model.Container](w, r, h)
}

func (h *Handler) CompleteContainerImageInfo(w http.ResponseWriter, r *http.Request) {
genericCompleteInfoHandler[model.ContainerImage](w, r, h)
}

func genericCompleteInfoHandler[T reporters.Cypherable](w http.ResponseWriter, r *http.Request, h *Handler) {
defer r.Body.Close()
var req completion.CompletionNodeFieldReq
Expand Down
1 change: 1 addition & 0 deletions deepfence_server/model/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ type Container struct {
Processes []Process `json:"processes" required:"true"`
DockerLabels map[string]interface{} `json:"docker_labels" required:"true" nested_json:"true"`
HostName string `json:"host_name" required:"true"`
DockerImageNameWithTag string `json:"docker_image_name_with_tag" required:"true"`
DockerContainerCommand string `json:"docker_container_command" required:"true"`
DockerContainerState string `json:"docker_container_state" required:"true"`
DockerContainerStateHuman string `json:"docker_container_state_human" required:"true"`
Expand Down
1 change: 1 addition & 0 deletions deepfence_server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func SetupRoutes(r *chi.Mux, serverPort string, serveOpenapiDocs bool, ingestC c
r.Post("/compliance", dfHandler.CompleteComplianceInfo)
r.Post("/pod", dfHandler.CompletePodInfo)
r.Post("/container", dfHandler.CompleteContainerInfo)
r.Post("/containerimage", dfHandler.CompleteContainerImageInfo)
})

r.Route("/search", func(r chi.Router) {
Expand Down
Loading