Skip to content

Commit 6b9c72f

Browse files
committed
arch op: turn into string, implement pattern match
Signed-off-by: Hank Donnay <[email protected]>
1 parent 85d6ae4 commit 6b9c72f

File tree

6 files changed

+96
-35
lines changed

6 files changed

+96
-35
lines changed

archop.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package claircore
2+
3+
import (
4+
"bytes"
5+
"database/sql/driver"
6+
"fmt"
7+
"regexp"
8+
)
9+
10+
type ArchOp uint
11+
12+
const (
13+
opInvalid ArchOp = iota // invalid
14+
15+
OpEquals // equals
16+
OpNotEquals // not equals
17+
OpPatternMatch // pattern match
18+
)
19+
20+
func (o ArchOp) Cmp(a, b string) bool {
21+
switch {
22+
case b == "":
23+
return true
24+
case a == "":
25+
return false
26+
default:
27+
}
28+
switch o {
29+
case OpEquals:
30+
return a == b
31+
case OpNotEquals:
32+
return a != b
33+
case OpPatternMatch:
34+
re, err := regexp.Compile(b)
35+
if err != nil {
36+
return false
37+
}
38+
return re.MatchString(a)
39+
default:
40+
}
41+
return false
42+
}
43+
44+
//go:generate stringer -type=ArchOp -linecomment
45+
46+
func (o ArchOp) MarshalText() (text []byte, err error) {
47+
return []byte(o.String()), nil
48+
}
49+
50+
func (o *ArchOp) UnmarshalText(text []byte) error {
51+
i := bytes.Index([]byte(_ArchOp_name), text)
52+
if i == -1 {
53+
*o = ArchOp(0)
54+
return nil
55+
}
56+
idx := uint8(i)
57+
for i, off := range _ArchOp_index {
58+
if off == idx {
59+
*o = ArchOp(i)
60+
return nil
61+
}
62+
}
63+
panic("unreachable")
64+
}
65+
66+
func (o ArchOp) Value() (driver.Value, error) {
67+
return o.String(), nil
68+
}
69+
70+
func (o *ArchOp) Scan(i interface{}) error {
71+
switch v := i.(type) {
72+
case []byte:
73+
return o.UnmarshalText(v)
74+
case string:
75+
return o.UnmarshalText([]byte(v))
76+
case int64:
77+
if v >= int64(len(_ArchOp_index)-1) {
78+
return fmt.Errorf("unable to scan ArchOp from enum %d", v)
79+
}
80+
*o = ArchOp(v)
81+
default:
82+
return fmt.Errorf("unable to scan ArchOp from type %T", i)
83+
}
84+
return nil
85+
}

archop_string.go

+5-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/vulnstore/postgres/update_e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (e *e2e) DeleteUpdateOperations(ctx context.Context) func(*testing.T) {
292292
}
293293

294294
// checkInsertedVulns confirms vulnerabilitiles are inserted into the database correctly when
295-
// store.UpdateVulnerabilities is calld.
295+
// store.UpdateVulnerabilities is called.
296296
func checkInsertedVulns(ctx context.Context, t *testing.T, db *sqlx.DB, id uuid.UUID, vulns []*claircore.Vulnerability) {
297297
const query = `SELECT
298298
vuln.hash_kind,

libvuln/migrations/migration1.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const (
7070
repo_key TEXT,
7171
repo_uri TEXT,
7272
fixed_in_version TEXT,
73-
arch_operation bigint,
73+
arch_operation TEXT,
7474
vulnerable_range VersionRange NOT NULL DEFAULT VersionRange('{}', '{}', '()'),
7575
version_kind TEXT,
7676
UNIQUE (hash_kind, hash)

pkg/ovalutil/rpm.go

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ func mapArchOp(op oval.Operation) claircore.ArchOp {
119119
return claircore.OpEquals
120120
case oval.OpNotEquals:
121121
return claircore.OpNotEquals
122+
case oval.OpPatternMatch:
123+
return claircore.OpPatternMatch
122124
default:
123125
}
124126
return claircore.ArchOp(0)

vulnerability.go

+2-29
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
11
package claircore
22

3-
import "time"
4-
5-
type ArchOp uint
6-
7-
const (
8-
_ ArchOp = iota // invalid
9-
10-
OpEquals // equals
11-
OpNotEquals // not equals
3+
import (
4+
"time"
125
)
136

14-
func (o ArchOp) Cmp(a, b string) bool {
15-
switch {
16-
case b == "":
17-
return true
18-
case a == "":
19-
return false
20-
default:
21-
}
22-
switch o {
23-
case OpEquals:
24-
return a == b
25-
case OpNotEquals:
26-
return a != b
27-
default:
28-
}
29-
return false
30-
}
31-
32-
//go:generate stringer -type=ArchOp -linecomment
33-
347
type Vulnerability struct {
358
// unique ID of this vulnerability. this will be created as discovered by the library
369
// and used for persistence and hash map indexes

0 commit comments

Comments
 (0)