Skip to content

Commit

Permalink
fetchers/alpine: auto detect namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Dec 19, 2016
1 parent 59e6c62 commit 3be8dfc
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions updater/fetchers/alpine/alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ func (f *fetcher) FetchUpdate(db database.Datastore) (resp updater.FetcherRespon
return
}

var namespaces []string
namespaces, err = detectNamespaces(f.repositoryLocalPath)
// Append any changed vulnerabilities to the response.
for _, namespace := range []string{"v3.3", "v3.4"} {
for _, namespace := range namespaces {
var vulns []database.Vulnerability
vulns, err = parseVulnsFromNamespace(f.repositoryLocalPath, namespace)
if err != nil {
Expand All @@ -99,6 +101,40 @@ func (f *fetcher) FetchUpdate(db database.Datastore) (resp updater.FetcherRespon
return
}

func detectNamespaces(path string) ([]string, error) {
// Open the root directory.
dir, err := os.Open(path)
if err != nil {
return nil, err
}
defer dir.Close()

// Get a list of the namspaces from the directory names.
names, err := dir.Readdirnames(0)
if err != nil {
return nil, err
}

var namespaces []string
for _, name := range names {
// Filter out hidden directories like `.git`.
if strings.HasPrefix(name, ".") {
continue
}

namespaces = append(namespaces, name)
}

return namespaces, nil
}

type parserFunc func(io.Reader) ([]database.Vulnerability, error)

var parsers = map[string]parserFunc{
"v3.3": parse33YAML,
"v3.4": parse34YAML,
}

func parseVulnsFromNamespace(repositoryPath, namespace string) (vulns []database.Vulnerability, err error) {
var file io.ReadCloser
file, err = os.Open(repositoryPath + "/" + namespace + "/main.yaml")
Expand All @@ -107,13 +143,12 @@ func parseVulnsFromNamespace(repositoryPath, namespace string) (vulns []database
}
defer file.Close()

switch namespace {
case "v3.3":
vulns, err = parse33YAML(file)
case "v3.4":
vulns, err = parse34YAML(file)
parseFunc, exists := parsers[namespace]
if !exists {
return
}

vulns, err = parseFunc(file)
return
}

Expand Down

0 comments on commit 3be8dfc

Please sign in to comment.