Skip to content

Commit

Permalink
add db curator
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Goodman <[email protected]>
  • Loading branch information
wagoodman committed Oct 4, 2024
1 parent 8b398b3 commit e89455d
Show file tree
Hide file tree
Showing 9 changed files with 1,074 additions and 10 deletions.
8 changes: 8 additions & 0 deletions grype/db/v6/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ type Writer interface {
io.Closer
}

type Curator interface {
Reader() (Reader, error)
Status() Status
Delete() error
Update() (bool, error)
Import(dbArchivePath string) error
}

type Config struct {
DBDirPath string
}
Expand Down
16 changes: 16 additions & 0 deletions grype/db/v6/description.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package v6

import (
"encoding/json"
"fmt"
"io"
"path"
"time"

Expand Down Expand Up @@ -85,3 +87,17 @@ func NewDescriptionFromDir(fs afero.Fs, dir string) (*Description, error) {
func (m Description) String() string {
return fmt.Sprintf("DB(version=%s built=%s checksum=%s)", m.SchemaVersion, m.Built, m.Checksum)
}

func writeDescription(writer io.Writer, m Description) error {
if m.SchemaVersion == "" {
return fmt.Errorf("missing schema version")
}

contents, err := json.MarshalIndent(m, "", " ")
if err != nil {
return fmt.Errorf("failed to encode metadata file: %w", err)
}

_, err = writer.Write(contents)
return err
}
22 changes: 22 additions & 0 deletions grype/db/v6/description_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -125,3 +126,24 @@ func TestTime_JSONUnmarshalling(t *testing.T) {
})
}
}

func TestDatabaseDescription_Write(t *testing.T) {

validDescription := Description{
SchemaVersion: "1.0.0",
Built: Time{Time: time.Date(2023, 9, 26, 12, 2, 3, 0, time.UTC)},
Checksum: "xxh64:dummychecksum",
}

sb := strings.Builder{}

err := writeDescription(&sb, validDescription)
require.NoError(t, err)

expected := fmt.Sprintf(`{
"schemaVersion": "1.0.0",
"built": "2023-09-26T12:02:03Z",
"checksum": "xxh64:dummychecksum"
}`)
assert.Equal(t, expected, sb.String())
}
12 changes: 5 additions & 7 deletions grype/db/v6/distribution/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ type Config struct {
CACert string

// validations
ValidateByHashOnGet bool
RequireUpdateCheck bool
RequireUpdateCheck bool

// timeouts
CheckTimeout time.Duration
Expand All @@ -50,11 +49,10 @@ type client struct {

func DefaultConfig() Config {
return Config{
LatestURL: "https://grype.anchore.io/databases/latest.json",
ValidateByHashOnGet: true,
RequireUpdateCheck: false,
CheckTimeout: 30 * time.Second,
UpdateTimeout: 300 * time.Second,
LatestURL: "https://grype.anchore.io/databases/latest.json",
RequireUpdateCheck: false,
CheckTimeout: 30 * time.Second,
UpdateTimeout: 300 * time.Second,
}
}

Expand Down
Loading

0 comments on commit e89455d

Please sign in to comment.