-
Notifications
You must be signed in to change notification settings - Fork 101
enricher: add RHCC enricher #1057
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
Draft
crozzy
wants to merge
1
commit into
quay:main
Choose a base branch
from
crozzy:rhcc-enricher
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,49 @@ | ||
| package rhcc | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/libvuln/driver" | ||
| "github.com/quay/claircore/rhel/rhcc" | ||
| ) | ||
|
|
||
| type Enricher struct{} | ||
|
|
||
| var ( | ||
| _ driver.Enricher = (*Enricher)(nil) | ||
| ) | ||
|
|
||
| const ( | ||
| // Type is the type of data returned from the Enricher's Enrich method. | ||
| Type = `message/vnd.clair.map.layer; enricher=clair.rhcc` | ||
|
crozzy marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| func (e *Enricher) Name() string { return "rhcc" } | ||
|
|
||
| func (e *Enricher) Enrich(ctx context.Context, g driver.EnrichmentGetter, r *claircore.VulnerabilityReport) (string, []json.RawMessage, error) { | ||
| rhccPkgs := make(map[string]string) | ||
| for id, p := range r.Packages { | ||
| if envs, ok := r.Environments[id]; ok && p.Kind == claircore.BINARY { | ||
| for _, e := range envs { | ||
| for _, repoID := range e.RepositoryIDs { | ||
| repo := r.Repositories[repoID] | ||
| if repo.Name == rhcc.GoldRepo.Name { | ||
| rhccPkgs[id] = e.IntroducedIn.String() | ||
| break | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(rhccPkgs) == 0 { | ||
| return Type, nil, nil | ||
| } | ||
| b, err := json.Marshal(rhccPkgs) | ||
| if err != nil { | ||
| return Type, nil, err | ||
| } | ||
| return Type, []json.RawMessage{b}, nil | ||
| } | ||
This file contains hidden or 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,226 @@ | ||
| package rhcc | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha256" | ||
| "encoding/json" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/quay/zlog" | ||
|
|
||
| "github.com/quay/claircore" | ||
| "github.com/quay/claircore/libvuln/driver" | ||
| ) | ||
|
|
||
| func Digest(name string) claircore.Digest { | ||
| h := sha256.New() | ||
| if _, err := io.WriteString(h, name); err != nil { | ||
| panic(err) | ||
| } | ||
| d, err := claircore.NewDigest("sha256", h.Sum(nil)) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return d | ||
| } | ||
|
|
||
| func TestEnrich(t *testing.T) { | ||
| t.Parallel() | ||
| ctx := zlog.Test(context.Background(), t) | ||
| firstLayerHash := Digest("first layer") | ||
| secondLayerHash := Digest("second layer") | ||
| tests := []struct { | ||
| name string | ||
| vr *claircore.VulnerabilityReport | ||
| layers []*claircore.Layer | ||
| want map[string]string | ||
| }{ | ||
| { | ||
| name: "one package that is a layer one that isn't", | ||
| vr: &claircore.VulnerabilityReport{ | ||
| Packages: map[string]*claircore.Package{ | ||
| "1": { | ||
| Name: "some-rh-package-slash-image", | ||
| Version: "v1.0.0", | ||
| Kind: claircore.BINARY, | ||
| }, | ||
| "2": { | ||
| Name: "grafana", | ||
| Version: "v4.7.0", | ||
| Kind: claircore.BINARY, | ||
| }, | ||
| }, | ||
| Environments: map[string][]*claircore.Environment{ | ||
| "1": {{IntroducedIn: firstLayerHash, RepositoryIDs: []string{"1"}}}, | ||
| "2": {{IntroducedIn: secondLayerHash}}, | ||
| }, | ||
| Repositories: map[string]*claircore.Repository{ | ||
| "1": { | ||
| ID: "1", | ||
| Name: "Red Hat Container Catalog", | ||
| URI: "https://catalog.redhat.com/software/containers/explore", | ||
| }, | ||
| }, | ||
| }, | ||
| layers: []*claircore.Layer{ | ||
| {Hash: firstLayerHash}, | ||
| {Hash: secondLayerHash}, | ||
| }, | ||
| want: map[string]string{"1": firstLayerHash.String()}, | ||
| }, | ||
| { | ||
| name: "two packages, neither are layers", | ||
| vr: &claircore.VulnerabilityReport{ | ||
| Packages: map[string]*claircore.Package{ | ||
| "1": { | ||
| Name: "cool app", | ||
| Version: "v1.0.0", | ||
| Kind: claircore.BINARY, | ||
| }, | ||
| "2": { | ||
| Name: "grafana", | ||
| Version: "v4.7.0", | ||
| Kind: claircore.BINARY, | ||
| }, | ||
| }, | ||
| Environments: map[string][]*claircore.Environment{ | ||
| "1": {{IntroducedIn: firstLayerHash}}, | ||
| "2": {{IntroducedIn: firstLayerHash}}, | ||
| }, | ||
| }, | ||
| layers: []*claircore.Layer{ | ||
| {Hash: firstLayerHash}, | ||
| {Hash: secondLayerHash}, | ||
| }, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "multiple rhcc packages in different layers", | ||
| vr: &claircore.VulnerabilityReport{ | ||
| Packages: map[string]*claircore.Package{ | ||
| "1": { | ||
| Name: "some-rh-package-slash-image", | ||
| RepositoryHint: "rhcc", | ||
| Version: "v1.0.0", | ||
| Kind: claircore.BINARY, | ||
| }, | ||
| "2": { | ||
| Name: "some-other-rh-package-slash-image", | ||
| RepositoryHint: "rhcc", | ||
| Version: "v1.0.0", | ||
| Kind: claircore.BINARY, | ||
| }, | ||
| "3": { | ||
| Name: "grafana", | ||
| Version: "v4.7.0", | ||
| Kind: claircore.BINARY, | ||
| }, | ||
| }, | ||
| Environments: map[string][]*claircore.Environment{ | ||
| "1": {{IntroducedIn: firstLayerHash, RepositoryIDs: []string{"1"}}}, | ||
| "2": {{IntroducedIn: secondLayerHash, RepositoryIDs: []string{"1"}}}, | ||
| "3": {{IntroducedIn: firstLayerHash}}, | ||
| }, | ||
| Repositories: map[string]*claircore.Repository{ | ||
| "1": { | ||
| ID: "1", | ||
| Name: "Red Hat Container Catalog", | ||
| URI: "https://catalog.redhat.com/software/containers/explore", | ||
| }, | ||
| }, | ||
| }, | ||
| layers: []*claircore.Layer{ | ||
| {Hash: firstLayerHash}, | ||
| {Hash: secondLayerHash}, | ||
| }, | ||
| want: map[string]string{"1": firstLayerHash.String(), "2": secondLayerHash.String()}, | ||
| }, | ||
| { | ||
| name: "multiple rhcc packages in same layers (source and binary)", | ||
| vr: &claircore.VulnerabilityReport{ | ||
| Packages: map[string]*claircore.Package{ | ||
| "1": { | ||
| Name: "some-rh-package-slash-image-binary", | ||
| Version: "v1.0.0", | ||
| Kind: claircore.BINARY, | ||
| Source: &claircore.Package{ | ||
| Name: "some-rh-package-slash-image-source", | ||
| Version: "v1.0.0", | ||
| Kind: claircore.SOURCE, | ||
| }, | ||
| }, | ||
| "2": { | ||
| Name: "some-rh-package-slash-image-source", | ||
| Version: "v1.0.0", | ||
| Kind: claircore.SOURCE, | ||
| }, | ||
| "3": { | ||
| Name: "grafana", | ||
| Version: "v4.7.0", | ||
| Kind: claircore.BINARY, | ||
| }, | ||
| }, | ||
| Environments: map[string][]*claircore.Environment{ | ||
| "1": {{IntroducedIn: firstLayerHash, RepositoryIDs: []string{"1"}}}, | ||
| "2": {{IntroducedIn: firstLayerHash, RepositoryIDs: []string{"1"}}}, | ||
| "3": {{IntroducedIn: secondLayerHash}}, | ||
| }, | ||
| Repositories: map[string]*claircore.Repository{ | ||
| "1": { | ||
| ID: "1", | ||
| Name: "Red Hat Container Catalog", | ||
| URI: "https://catalog.redhat.com/software/containers/explore", | ||
| }, | ||
| }, | ||
| }, | ||
| layers: []*claircore.Layer{ | ||
| {Hash: firstLayerHash}, | ||
| {Hash: secondLayerHash}, | ||
| }, | ||
| want: map[string]string{"1": firstLayerHash.String()}, | ||
| }, | ||
| } | ||
|
|
||
| e := &Enricher{} | ||
| nog := &noopGetter{} | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| tp, data, err := e.Enrich(ctx, nog, tc.vr) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if tc.want == nil { | ||
| if data != nil { | ||
| t.Fatal("unexpected data") | ||
| } | ||
| return | ||
| } | ||
| if tp != "message/vnd.clair.map.layer; enricher=clair.rhcc" { | ||
| t.Fatal("wrong type") | ||
| } | ||
| got := make(map[string]string) | ||
| if err := json.Unmarshal(data[0], &got); err != nil { | ||
| t.Error(err) | ||
| } | ||
| if !cmp.Equal(got, tc.want) { | ||
| t.Error(cmp.Diff(got, tc.want)) | ||
| } | ||
| }) | ||
|
|
||
| } | ||
| } | ||
|
|
||
| func TestName(t *testing.T) { | ||
| e := &Enricher{} | ||
| if e.Name() != "rhcc" { | ||
| t.Fatal("name should be rhcc") | ||
| } | ||
| } | ||
|
|
||
| type noopGetter struct{} | ||
|
|
||
| func (f *noopGetter) GetEnrichment(ctx context.Context, tags []string) ([]driver.EnrichmentRecord, error) { | ||
| return nil, nil | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.