|
| 1 | +package rhcc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/google/go-cmp/cmp" |
| 11 | + "github.com/quay/zlog" |
| 12 | + |
| 13 | + "github.com/quay/claircore" |
| 14 | + "github.com/quay/claircore/libvuln/driver" |
| 15 | +) |
| 16 | + |
| 17 | +func Digest(name string) claircore.Digest { |
| 18 | + h := sha256.New() |
| 19 | + io.WriteString(h, name) |
| 20 | + d, err := claircore.NewDigest("sha256", h.Sum(nil)) |
| 21 | + if err != nil { |
| 22 | + panic(err) |
| 23 | + } |
| 24 | + return d |
| 25 | +} |
| 26 | + |
| 27 | +func TestEnrich(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + ctx := zlog.Test(context.Background(), t) |
| 30 | + firstLayerHash := Digest("first layer") |
| 31 | + secondLayerHash := Digest("second layer") |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + vr *claircore.VulnerabilityReport |
| 35 | + layers []*claircore.Layer |
| 36 | + want map[string]string |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "vuln in package in different layer from rhcc package", |
| 40 | + vr: &claircore.VulnerabilityReport{ |
| 41 | + Packages: map[string]*claircore.Package{ |
| 42 | + "1": { |
| 43 | + Name: "some-rh-package-slash-image", |
| 44 | + RepositoryHint: "rhcc", |
| 45 | + Version: "v1.0.0", |
| 46 | + Kind: claircore.BINARY, |
| 47 | + }, |
| 48 | + "2": { |
| 49 | + Name: "grafana", |
| 50 | + Version: "v4.7.0", |
| 51 | + Kind: claircore.BINARY, |
| 52 | + }, |
| 53 | + }, |
| 54 | + Environments: map[string][]*claircore.Environment{ |
| 55 | + "1": {{IntroducedIn: firstLayerHash}}, |
| 56 | + "2": {{IntroducedIn: secondLayerHash}}, |
| 57 | + }, |
| 58 | + Vulnerabilities: map[string]*claircore.Vulnerability{ |
| 59 | + "4": { |
| 60 | + Name: "something bad with grafana", |
| 61 | + FixedInVersion: "v100.0.0", |
| 62 | + }, |
| 63 | + }, |
| 64 | + PackageVulnerabilities: map[string][]string{ |
| 65 | + "2": {"4"}, |
| 66 | + }, |
| 67 | + }, |
| 68 | + layers: []*claircore.Layer{ |
| 69 | + {Hash: firstLayerHash}, |
| 70 | + {Hash: secondLayerHash}, |
| 71 | + }, |
| 72 | + want: map[string]string{firstLayerHash.String(): "1"}, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "vuln in package in same layer as rhcc package", |
| 76 | + vr: &claircore.VulnerabilityReport{ |
| 77 | + Packages: map[string]*claircore.Package{ |
| 78 | + "1": { |
| 79 | + Name: "some-rh-package-slash-image", |
| 80 | + RepositoryHint: "rhcc", |
| 81 | + Version: "v1.0.0", |
| 82 | + Kind: claircore.BINARY, |
| 83 | + }, |
| 84 | + "2": { |
| 85 | + Name: "grafana", |
| 86 | + Version: "v4.7.0", |
| 87 | + Kind: claircore.BINARY, |
| 88 | + }, |
| 89 | + }, |
| 90 | + Environments: map[string][]*claircore.Environment{ |
| 91 | + "1": {{IntroducedIn: firstLayerHash}}, |
| 92 | + "2": {{IntroducedIn: firstLayerHash}}, |
| 93 | + }, |
| 94 | + Vulnerabilities: map[string]*claircore.Vulnerability{ |
| 95 | + "4": { |
| 96 | + Name: "something bad with grafana", |
| 97 | + FixedInVersion: "v100.0.0", |
| 98 | + }, |
| 99 | + }, |
| 100 | + PackageVulnerabilities: map[string][]string{ |
| 101 | + "2": {"4"}, |
| 102 | + }, |
| 103 | + }, |
| 104 | + layers: []*claircore.Layer{ |
| 105 | + {Hash: firstLayerHash}, |
| 106 | + {Hash: secondLayerHash}, |
| 107 | + }, |
| 108 | + want: map[string]string{firstLayerHash.String(): "1"}, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "vuln in package in same layer as rhcc package and rhcc vuln in same layer", |
| 112 | + vr: &claircore.VulnerabilityReport{ |
| 113 | + Packages: map[string]*claircore.Package{ |
| 114 | + "1": { |
| 115 | + Name: "some-rh-package-slash-image", |
| 116 | + RepositoryHint: "rhcc", |
| 117 | + Version: "v1.0.0", |
| 118 | + Kind: claircore.BINARY, |
| 119 | + }, |
| 120 | + "2": { |
| 121 | + Name: "grafana", |
| 122 | + Version: "v4.7.0", |
| 123 | + Kind: claircore.BINARY, |
| 124 | + }, |
| 125 | + }, |
| 126 | + Environments: map[string][]*claircore.Environment{ |
| 127 | + "1": {{IntroducedIn: firstLayerHash}}, |
| 128 | + "2": {{IntroducedIn: firstLayerHash}}, |
| 129 | + }, |
| 130 | + Vulnerabilities: map[string]*claircore.Vulnerability{ |
| 131 | + "4": { |
| 132 | + Name: "something bad with grafana", |
| 133 | + FixedInVersion: "v100.0.0", |
| 134 | + }, |
| 135 | + "5": { |
| 136 | + Name: "something bad ubi", |
| 137 | + FixedInVersion: "v100.0.0", |
| 138 | + }, |
| 139 | + }, |
| 140 | + PackageVulnerabilities: map[string][]string{ |
| 141 | + "2": {"4"}, |
| 142 | + "1": {"5"}, |
| 143 | + }, |
| 144 | + }, |
| 145 | + layers: []*claircore.Layer{ |
| 146 | + {Hash: firstLayerHash}, |
| 147 | + {Hash: secondLayerHash}, |
| 148 | + }, |
| 149 | + want: map[string]string{firstLayerHash.String(): "1"}, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "multiple rhcc packages in different layers", |
| 153 | + vr: &claircore.VulnerabilityReport{ |
| 154 | + Packages: map[string]*claircore.Package{ |
| 155 | + "1": { |
| 156 | + Name: "some-rh-package-slash-image", |
| 157 | + RepositoryHint: "rhcc", |
| 158 | + Version: "v1.0.0", |
| 159 | + Kind: claircore.BINARY, |
| 160 | + }, |
| 161 | + "2": { |
| 162 | + Name: "some-other-rh-package-slash-image", |
| 163 | + RepositoryHint: "rhcc", |
| 164 | + Version: "v1.0.0", |
| 165 | + Kind: claircore.BINARY, |
| 166 | + }, |
| 167 | + "3": { |
| 168 | + Name: "grafana", |
| 169 | + Version: "v4.7.0", |
| 170 | + Kind: claircore.BINARY, |
| 171 | + }, |
| 172 | + }, |
| 173 | + Environments: map[string][]*claircore.Environment{ |
| 174 | + "1": {{IntroducedIn: firstLayerHash}}, |
| 175 | + "2": {{IntroducedIn: secondLayerHash}}, |
| 176 | + "3": {{IntroducedIn: firstLayerHash}}, |
| 177 | + }, |
| 178 | + Vulnerabilities: map[string]*claircore.Vulnerability{ |
| 179 | + "4": { |
| 180 | + Name: "something bad with grafana", |
| 181 | + FixedInVersion: "v100.0.0", |
| 182 | + }, |
| 183 | + "5": { |
| 184 | + Name: "something bad ubi", |
| 185 | + FixedInVersion: "v100.0.0", |
| 186 | + }, |
| 187 | + "6": { |
| 188 | + Name: "something bad s2i", |
| 189 | + FixedInVersion: "v100.0.0", |
| 190 | + }, |
| 191 | + }, |
| 192 | + PackageVulnerabilities: map[string][]string{ |
| 193 | + "3": {"4"}, |
| 194 | + "1": {"5"}, |
| 195 | + "2": {"6"}, |
| 196 | + }, |
| 197 | + }, |
| 198 | + layers: []*claircore.Layer{ |
| 199 | + {Hash: firstLayerHash}, |
| 200 | + {Hash: secondLayerHash}, |
| 201 | + }, |
| 202 | + want: map[string]string{firstLayerHash.String(): "1", secondLayerHash.String(): "2"}, |
| 203 | + }, |
| 204 | + } |
| 205 | + |
| 206 | + e := &Enricher{} |
| 207 | + nog := &noopGetter{} |
| 208 | + for _, tc := range tests { |
| 209 | + t.Run(tc.name, func(t *testing.T) { |
| 210 | + tp, data, err := e.Enrich(ctx, nog, tc.vr) |
| 211 | + if err != nil { |
| 212 | + t.Fatal(err) |
| 213 | + } |
| 214 | + if tp != "message/vnd.clair.map.layer; enricher=clair.rhcc schema=??" { |
| 215 | + t.Fatal("wrong type") |
| 216 | + } |
| 217 | + got := make(map[string]string) |
| 218 | + if err := json.Unmarshal(data[0], &got); err != nil { |
| 219 | + t.Error(err) |
| 220 | + } |
| 221 | + if !cmp.Equal(got, tc.want) { |
| 222 | + t.Error(cmp.Diff(got, tc.want)) |
| 223 | + } |
| 224 | + }) |
| 225 | + |
| 226 | + } |
| 227 | +} |
| 228 | + |
| 229 | +func TestName(t *testing.T) { |
| 230 | + e := &Enricher{} |
| 231 | + if e.Name() != "rhcc" { |
| 232 | + t.Fatal("name should be rhcc") |
| 233 | + } |
| 234 | +} |
| 235 | + |
| 236 | +type noopGetter struct{} |
| 237 | + |
| 238 | +func (f *noopGetter) GetEnrichment(ctx context.Context, tags []string) ([]driver.EnrichmentRecord, error) { |
| 239 | + return nil, nil |
| 240 | +} |
0 commit comments