Skip to content

Commit

Permalink
updater: add a clean function to fetchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin-M authored and jzelinskie committed Feb 24, 2016
1 parent 6b3f95d commit 431c0cc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
5 changes: 5 additions & 0 deletions updater/fetchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ var fetchers = make(map[string]Fetcher)

// Fetcher represents anything that can fetch vulnerabilities.
type Fetcher interface {
// FetchUpdate gets vulnerability updates.
FetchUpdate(database.Datastore) (FetcherResponse, error)

// Clean deletes any allocated resources.
// It is invoked when Clair stops.
Clean()
}

// FetcherResponse represents the sum of results of an update.
Expand Down
13 changes: 8 additions & 5 deletions updater/fetchers/debian/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ func parseDebianJSON(data *jsonData) (vulnerabilities []database.Vulnerability,
// Create and add the feature version.
pkg := database.FeatureVersion{
Feature: database.Feature{
Name: pkgName,
Namespace: database.Namespace{
Name: "debian:" + database.DebianReleasesMapping[releaseName],
},
},
Name: pkgName,
Namespace: database.Namespace{
Name: "debian:" + database.DebianReleasesMapping[releaseName],
},
},
Version: version,
}
vulnerability.FixedIn = append(vulnerability.FixedIn, pkg)
Expand Down Expand Up @@ -249,3 +249,6 @@ func urgencyToSeverity(urgency string) types.Priority {
return types.Unknown
}
}

// Clean deletes any allocated resources.
func (fetcher *DebianFetcher) Clean() {}
5 changes: 4 additions & 1 deletion updater/fetchers/rhel/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func parseRHSA(ovalReader io.Reader) (vulnerabilities []database.Vulnerability,
var ov oval
err = xml.NewDecoder(ovalReader).Decode(&ov)
if err != nil {
log.Errorf("could not decode RHEL's XML: %s.", err)
log.Errorf("could not decode RHEL's XML: %s", err)
err = cerrors.ErrCouldNotParse
return
}
Expand Down Expand Up @@ -358,3 +358,6 @@ func priority(def definition) types.Priority {
return types.Unknown
}
}

// Clean deletes any allocated resources.
func (f *RHELFetcher) Clean() {}
27 changes: 16 additions & 11 deletions updater/fetchers/ubuntu/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ const (
)

var (
repositoryLocalPath string

ubuntuIgnoredReleases = map[string]struct{}{
"upstream": struct{}{},
"devel": struct{}{},
Expand Down Expand Up @@ -79,9 +77,11 @@ var (
ErrFilesystem = errors.New("updater/fetchers: something went wrong when interacting with the fs")
)

// UbuntuFetcher implements updater.Fetcher and get vulnerability updates from
// UbuntuFetcher implements updater.Fetcher and gets vulnerability updates from
// the Ubuntu CVE Tracker.
type UbuntuFetcher struct{}
type UbuntuFetcher struct {
repositoryLocalPath string
}

func init() {
updater.RegisterFetcher("Ubuntu", &UbuntuFetcher{})
Expand All @@ -92,31 +92,31 @@ func (fetcher *UbuntuFetcher) FetchUpdate(datastore database.Datastore) (resp up
log.Info("fetching Ubuntu vulnerabilities")

// Check to see if the repository does not already exist.
if _, pathExists := os.Stat(repositoryLocalPath); repositoryLocalPath == "" || os.IsNotExist(pathExists) {
if _, pathExists := os.Stat(fetcher.repositoryLocalPath); fetcher.repositoryLocalPath == "" || os.IsNotExist(pathExists) {
// Create a temporary folder and download the repository.
p, err := ioutil.TempDir(os.TempDir(), "ubuntu-cve-tracker")
if err != nil {
return resp, ErrFilesystem
}

// bzr wants an empty target directory.
repositoryLocalPath = p + "/repository"
fetcher.repositoryLocalPath = p + "/repository"

// Create the new repository.
err = createRepository(repositoryLocalPath)
err = createRepository(fetcher.repositoryLocalPath)
if err != nil {
return resp, err
}
} else {
// Update the repository that's already on disk.
err = updateRepository(repositoryLocalPath)
err = updateRepository(fetcher.repositoryLocalPath)
if err != nil {
return resp, err
}
}

// Get revision number.
revisionNumber, err := getRevisionNumber(repositoryLocalPath)
revisionNumber, err := getRevisionNumber(fetcher.repositoryLocalPath)
if err != nil {
return resp, err
}
Expand All @@ -128,15 +128,15 @@ func (fetcher *UbuntuFetcher) FetchUpdate(datastore database.Datastore) (resp up
}

// Get the list of vulnerabilities that we have to update.
modifiedCVE, err := collectModifiedVulnerabilities(revisionNumber, dbRevisionNumber, repositoryLocalPath)
modifiedCVE, err := collectModifiedVulnerabilities(revisionNumber, dbRevisionNumber, fetcher.repositoryLocalPath)
if err != nil {
return resp, err
}

notes := make(map[string]struct{})
for cvePath := range modifiedCVE {
// Open the CVE file.
file, err := os.Open(repositoryLocalPath + "/" + cvePath)
file, err := os.Open(fetcher.repositoryLocalPath + "/" + cvePath)
if err != nil {
// This can happen when a file is modified and then moved in another
// commit.
Expand Down Expand Up @@ -425,3 +425,8 @@ func ubuntuPriorityToSeverity(priority string) types.Priority {
log.Warning("Could not determine a vulnerability priority from: %s", priority)
return types.Unknown
}

// Clean deletes any allocated resources.
func (fetcher *UbuntuFetcher) Clean() {
os.RemoveAll(fetcher.repositoryLocalPath)
}
5 changes: 5 additions & 0 deletions updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ func Run(config *config.UpdaterConfig, datastore database.Datastore, st *utils.S
}
}

// Clean resources.
for _, fetcher := range fetchers {
fetcher.Clean()
}

log.Info("updater service stopped")
}

Expand Down

0 comments on commit 431c0cc

Please sign in to comment.