-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add SBOM in sync-request metadata (#43)
Signed-off-by: felipecruz91 <[email protected]>
- Loading branch information
1 parent
c03370f
commit faa37b7
Showing
9 changed files
with
430 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package legacy | ||
|
||
import ( | ||
"github.com/atomist-skills/go-skill/policy/types" | ||
"olympos.io/encoding/edn" | ||
) | ||
|
||
func BuildLocalEvalMocks(sb *types.SBOM) map[edn.Keyword]edn.RawMessage { | ||
m := map[edn.Keyword]edn.RawMessage{} | ||
if sb == nil { | ||
return m | ||
} | ||
|
||
m[ImagePackagesByDigestQueryName], _ = edn.Marshal(MockImagePackagesByDigestForLocalEval(sb)) | ||
|
||
if sb.Source.Image != nil && sb.Source.Image.Config != nil { | ||
m[GetUserQueryName], _ = edn.Marshal(MockGetUserForLocalEval(sb.Source.Image.Config.Config.User)) | ||
} | ||
|
||
return m | ||
} | ||
|
||
func MockImagePackagesByDigestForLocalEval(sb *types.SBOM) ImagePackagesByDigestResponse { | ||
vulns := map[string][]Vulnerability{} | ||
for _, tuple := range sb.Vulnerabilities { | ||
vulnsForPurl := []Vulnerability{} | ||
for _, v := range tuple.Vulnerabilities { | ||
vulnsForPurl = append(vulnsForPurl, Vulnerability{ | ||
Cvss: Cvss{ | ||
Severity: &v.Cvss.Severity, | ||
Score: &v.Cvss.Score, | ||
}, | ||
FixedBy: &v.FixedBy, | ||
Source: v.Source, | ||
SourceID: v.SourceId, | ||
URL: &v.Url, | ||
VulnerableRange: v.VulnerableRange, | ||
}) | ||
} | ||
vulns[tuple.Purl] = vulnsForPurl | ||
} | ||
|
||
pkgs := []Packages{} | ||
for _, a := range sb.Artifacts { | ||
pkgs = append(pkgs, Packages{ | ||
Package: PackageWithLicenses{ | ||
Licenses: a.Licenses, | ||
Name: a.Name, | ||
Namespace: &a.Namespace, | ||
Version: a.Version, | ||
Purl: a.Purl, | ||
Type: a.Type, | ||
Vulnerabilities: vulns[a.Purl], | ||
}, | ||
}) | ||
} | ||
|
||
return ImagePackagesByDigestResponse{ | ||
ImagePackagesByDigest: &ImagePackagesByDigest{ | ||
ImagePackages: ImagePackages{ | ||
Packages: pkgs, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package legacy | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/atomist-skills/go-skill/policy/types" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"olympos.io/encoding/edn" | ||
) | ||
|
||
func Test_BuildLocalEvalMocks(t *testing.T) { | ||
type args struct { | ||
sb *types.SBOM | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want map[edn.Keyword]edn.RawMessage | ||
}{ | ||
{ | ||
name: "Without SBOM", | ||
args: args{ | ||
sb: nil, | ||
}, | ||
want: map[edn.Keyword]edn.RawMessage{}, | ||
}, | ||
{ | ||
name: "With SBOM", | ||
args: args{ | ||
sb: &types.SBOM{ | ||
Source: types.Source{ | ||
Image: &types.ImageSource{ | ||
Config: &v1.ConfigFile{ | ||
Config: v1.Config{ | ||
User: "root", | ||
}, | ||
}, | ||
}, | ||
}, | ||
Artifacts: []types.Package{ | ||
{ | ||
Name: "pkg1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[edn.Keyword]edn.RawMessage{ | ||
"image-packages-by-digest": []byte(`{:imagePackagesByDigest{:imagePackages{:packages[{:package{:licenses nil :name"pkg1":namespace"":version"":purl"":type"":vulnerabilities nil}}]}}}`), | ||
"get-user": []byte(`{:docker.image/user"root"}`), | ||
}, | ||
}, | ||
{ | ||
name: "SBOM without image source", | ||
args: args{ | ||
sb: &types.SBOM{ | ||
Source: types.Source{ | ||
Image: nil, | ||
}, | ||
Artifacts: []types.Package{ | ||
{ | ||
Name: "pkg1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[edn.Keyword]edn.RawMessage{ | ||
"image-packages-by-digest": []byte(`{:imagePackagesByDigest{:imagePackages{:packages[{:package{:licenses nil :name"pkg1":namespace"":version"":purl"":type"":vulnerabilities nil}}]}}}`), | ||
}, | ||
}, | ||
{ | ||
name: "SBOM without image config file", | ||
args: args{ | ||
sb: &types.SBOM{ | ||
Source: types.Source{ | ||
Image: &types.ImageSource{ | ||
Config: nil, | ||
}, | ||
}, | ||
Artifacts: []types.Package{ | ||
{ | ||
Name: "pkg1", | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: map[edn.Keyword]edn.RawMessage{ | ||
"image-packages-by-digest": []byte(`{:imagePackagesByDigest{:imagePackages{:packages[{:package{:licenses nil :name"pkg1":namespace"":version"":purl"":type"":vulnerabilities nil}}]}}}`), | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := BuildLocalEvalMocks(tt.args.sb); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("BuildLocalEvalMocks() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package legacy | ||
|
||
const ( | ||
GetUserQueryName = "get-user" | ||
) | ||
|
||
type DockerImageUser struct { | ||
ImageUser string `edn:"docker.image/user,omitempty"` | ||
} | ||
|
||
func MockGetUserForLocalEval(user string) DockerImageUser { | ||
return DockerImageUser{ | ||
ImageUser: user, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.