Skip to content

Commit

Permalink
feat: add pod namespace as tag in dtrack project (#157)
Browse files Browse the repository at this point in the history
* feat: add pod namespace as tag in dtrack project

This PR is to add the namespace name as tag in project of dtrack

Fixes: #156

* Updated processbom interface with required args
  • Loading branch information
rvgud authored Aug 29, 2022
1 parent 56e8ab4 commit 11ba433
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion internal/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (p *Processor) scanPod(pod libk8s.PodInfo) {
}

for _, t := range p.Targets {
err = t.ProcessSbom(container.Image, sbom)
err = t.ProcessSbom(container.Image, sbom, pod.PodNamespace)
errOccurred = errOccurred || err != nil
}
}
Expand Down
14 changes: 9 additions & 5 deletions internal/target/dtrack/dtrack_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ type DependencyTrackTarget struct {
}

const (
kubernetesCluster = "kubernetes-cluster"
sbomOperator = "sbom-operator"
rawImageId = "raw-image-id"
kubernetesCluster = "kubernetes-cluster"
sbomOperator = "sbom-operator"
rawImageId = "raw-image-id"
podNamespaceTagKey = "namespace"
)

func NewDependencyTrackTarget(baseUrl, apiKey, k8sClusterId string) *DependencyTrackTarget {
Expand All @@ -49,7 +50,7 @@ func (g *DependencyTrackTarget) ValidateConfig() error {
func (g *DependencyTrackTarget) Initialize() {
}

func (g *DependencyTrackTarget) ProcessSbom(image *libk8s.RegistryImage, sbom string) error {
func (g *DependencyTrackTarget) ProcessSbom(image *libk8s.RegistryImage, sbom string, podNamespace string) error {
projectName, version := getRepoWithVersion(image)

if sbom == "" {
Expand Down Expand Up @@ -93,7 +94,10 @@ func (g *DependencyTrackTarget) ProcessSbom(image *libk8s.RegistryImage, sbom st
if !containsTag(project.Tags, rawImageId) {
project.Tags = append(project.Tags, dtrack.Tag{Name: fmt.Sprintf("%s=%s", rawImageId, image.ImageID)})
}

podNamespaceTag := podNamespaceTagKey + "=" + podNamespace
if !containsTag(project.Tags, podNamespaceTag) {
project.Tags = append(project.Tags, dtrack.Tag{Name: podNamespaceTag})
}
_, err = client.Project.Update(context.Background(), project)
if err != nil {
logrus.WithError(err).Errorf("Could not update project tags")
Expand Down
2 changes: 1 addition & 1 deletion internal/target/git/git_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (g *GitTarget) Initialize() {
g.gitAccount.PrepareRepository(g.repository, g.workingTree, g.branch)
}

func (g *GitTarget) ProcessSbom(image *libk8s.RegistryImage, sbom string) error {
func (g *GitTarget) ProcessSbom(image *libk8s.RegistryImage, sbom string, podNamespace string) error {
imageID := image.ImageID
filePath := g.ImageIDToFilePath(imageID)

Expand Down
2 changes: 1 addition & 1 deletion internal/target/oci/oci_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (g *OciTarget) ValidateConfig() error {
func (g *OciTarget) Initialize() {
}

func (g *OciTarget) ProcessSbom(image *libk8s.RegistryImage, sbom string) error {
func (g *OciTarget) ProcessSbom(image *libk8s.RegistryImage, sbom string, podNamespace string) error {
ref, err := name.ParseReference(image.ImageID)
if err != nil {
logrus.WithError(err).Errorf("failed to parse reference %s", image.ImageID)
Expand Down
2 changes: 1 addition & 1 deletion internal/target/oci/oci_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func TestOci(t *testing.T) {
assert.NoError(t, err)

img := liboci.RegistryImage{ImageID: os.Getenv("TEST_DIGEST")}
err = oci.ProcessSbom(&img, string(sbom))
err = oci.ProcessSbom(&img, string(sbom), "TEST_NAMESPACE")
assert.NoError(t, err)
}
2 changes: 1 addition & 1 deletion internal/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
type Target interface {
Initialize()
ValidateConfig() error
ProcessSbom(image *libk8s.RegistryImage, sbom string) error
ProcessSbom(image *libk8s.RegistryImage, sbom string, podNamespace string) error
LoadImages() []*libk8s.RegistryImage
Remove(images []*libk8s.RegistryImage)
}

0 comments on commit 11ba433

Please sign in to comment.