Skip to content

Commit

Permalink
fix: improve target error-handling
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Kotzbauer <git@ckotzbauer.de>
  • Loading branch information
ckotzbauer committed Feb 1, 2022
1 parent 43c8401 commit 4002454
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 26 deletions.
19 changes: 13 additions & 6 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@ func (c *CronService) runBackgroundService() {

for _, image := range containerImages {
sbom, err := sy.ExecuteSyft(image)
// Error is already handled from syft module.
if err == nil {
for _, pod := range image.Pods {
k8s.UpdatePodAnnotation(pod)
}
if err != nil {
// Error is already handled from syft module.
continue
}

errOccurred := false

for _, t := range c.targets {
t.ProcessSbom(image, sbom)
err = t.ProcessSbom(image, sbom)
errOccurred = errOccurred || err != nil
}

if !errOccurred {
for _, pod := range image.Pods {
k8s.UpdatePodAnnotation(pod)
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions internal/target/dtrack_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ func (g *DependencyTrackTarget) ValidateConfig() error {
func (g *DependencyTrackTarget) Initialize() {
}

func (g *DependencyTrackTarget) ProcessSbom(image kubernetes.ContainerImage, sbom string) {
func (g *DependencyTrackTarget) ProcessSbom(image kubernetes.ContainerImage, sbom string) error {
fullRef, err := parser.Parse(image.Image)
if err != nil {
logrus.WithError(err).Errorf("Could not parse image %s", image.Image)
return
return nil
}

imageName := fullRef.Repository()
tagName := fullRef.Tag()

if sbom == "" {
logrus.Infof("Empty SBOM - skip image (image=%s)", image.ImageID)
return
return nil
}

client, _ := dtrack.NewClient(g.baseUrl, dtrack.WithAPIKey(g.apiKey))
Expand All @@ -67,8 +67,11 @@ func (g *DependencyTrackTarget) ProcessSbom(image kubernetes.ContainerImage, sbo
)
if err != nil {
logrus.Errorf("Could not upload BOM: %v", err)
return err
}

logrus.Infof("Uploaded SBOM (upload-token=%s)", uploadToken)
return nil
}

func (g *DependencyTrackTarget) Cleanup(allImages []string) {
Expand Down
28 changes: 15 additions & 13 deletions internal/target/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,33 +99,33 @@ func (g *GitAccount) openExistingRepo(path string) (*git.Repository, *git.Worktr
return r, w
}

func (g *GitAccount) CommitAll(path, message string) {
func (g *GitAccount) CommitAll(path, message string) error {
r, w := g.openExistingRepo(path)

if r == nil && w == nil {
return
return nil
}

status, err := w.Status()

if err != nil {
logrus.WithError(err).Error("Status failed")
return
return err
}

if status.IsClean() {
logrus.Debug("Git-Worktree is clean, skip commit")
return
return nil
}

_, err = w.Add(".")

if err != nil {
logrus.WithError(err).Error("Add failed")
return
return err
}

g.commitAndPush(w, r, message)
return g.commitAndPush(w, r, message)
}

func (g *GitAccount) Remove(workTree, path string) {
Expand All @@ -143,29 +143,29 @@ func (g *GitAccount) Remove(workTree, path string) {
}
}

func (g *GitAccount) CommitAndPush(path, message string) {
func (g *GitAccount) CommitAndPush(path, message string) error {
r, w := g.openExistingRepo(path)

if r == nil && w == nil {
return
return nil
}

status, err := w.Status()

if err != nil {
logrus.WithError(err).Error("Status failed")
return
return err
}

if status.IsClean() {
logrus.Debug("Git-Worktree is clean, skip commit")
return
return nil
}

g.commitAndPush(w, r, message)
return g.commitAndPush(w, r, message)
}

func (g *GitAccount) commitAndPush(w *git.Worktree, r *git.Repository, message string) {
func (g *GitAccount) commitAndPush(w *git.Worktree, r *git.Repository, message string) error {
commit, err := w.Commit(message, &git.CommitOptions{
Author: &object.Signature{
Name: g.Name,
Expand All @@ -178,7 +178,7 @@ func (g *GitAccount) commitAndPush(w *git.Worktree, r *git.Repository, message s

if err != nil {
logrus.WithError(err).Error("Commit failed")
return
return err
}

err = r.Push(&git.PushOptions{
Expand All @@ -187,9 +187,11 @@ func (g *GitAccount) commitAndPush(w *git.Worktree, r *git.Repository, message s

if err != nil {
logrus.WithError(err).Error("Push failed")
return err
}

logrus.Info("Push was successful")
return nil
}

func (g *GitAccount) tokenAuth() transport.AuthMethod {
Expand Down
6 changes: 3 additions & 3 deletions internal/target/git_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,23 @@ func (g *GitTarget) Initialize() {
viper.GetString(internal.ConfigKeyGitBranch))
}

func (g *GitTarget) ProcessSbom(image kubernetes.ContainerImage, sbom string) {
func (g *GitTarget) ProcessSbom(image kubernetes.ContainerImage, sbom string) error {
imageID := image.ImageID
filePath := g.imageIDToFilePath(imageID)

dir := filepath.Dir(filePath)
err := os.MkdirAll(dir, 0777)
if err != nil {
logrus.WithError(err).Error("Directory could not be created")
return
return err
}

err = os.WriteFile(filePath, []byte(sbom), 0640)
if err != nil {
logrus.WithError(err).Error("SBOM could not be saved")
}

g.gitAccount.CommitAll(g.workingTree, fmt.Sprintf("Created new SBOM for image %s", imageID))
return g.gitAccount.CommitAll(g.workingTree, fmt.Sprintf("Created new SBOM for image %s", imageID))
}

func (g *GitTarget) Cleanup(allImages []string) {
Expand Down
2 changes: 1 addition & 1 deletion internal/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
type Target interface {
Initialize()
ValidateConfig() error
ProcessSbom(imageID kubernetes.ContainerImage, sbom string)
ProcessSbom(image kubernetes.ContainerImage, sbom string) error
Cleanup(allImages []string)
}

0 comments on commit 4002454

Please sign in to comment.