From 78cef02fdad4afd12a2a00df51d457c796278bfa Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Fri, 13 Jan 2017 02:08:52 -0500 Subject: [PATCH] pkg: cerrors -> commonerr --- api/v1/routes.go | 30 +++++++++++------------ database/pgsql/feature.go | 8 +++--- database/pgsql/keyvalue.go | 6 ++--- database/pgsql/layer.go | 12 ++++----- database/pgsql/layer_test.go | 12 ++++----- database/pgsql/lock.go | 6 ++--- database/pgsql/namespace.go | 6 ++--- database/pgsql/notification.go | 6 ++--- database/pgsql/notification_test.go | 10 ++++---- database/pgsql/pgsql.go | 10 ++++---- database/pgsql/vulnerability.go | 18 +++++++------- database/pgsql/vulnerability_test.go | 12 ++++----- ext/vulnmdsrc/nvd/nvd.go | 10 ++++---- ext/vulnsrc/alpine/alpine.go | 4 +-- ext/vulnsrc/debian/debian.go | 6 ++--- ext/vulnsrc/oracle/oracle.go | 8 +++--- ext/vulnsrc/rhel/rhel.go | 8 +++--- ext/vulnsrc/ubuntu/ubuntu.go | 12 ++++----- notifier/notifier.go | 4 +-- {utils/errors => pkg/commonerr}/errors.go | 7 +++--- utils/http/http.go | 8 +++--- worker/detectors/feature/rpm/rpm.go | 8 +++--- worker/worker.go | 22 ++++++++--------- worker/worker_test.go | 6 ++--- 24 files changed, 120 insertions(+), 119 deletions(-) rename {utils/errors => pkg/commonerr}/errors.go (91%) diff --git a/api/v1/routes.go b/api/v1/routes.go index 344e2e28..bde7560d 100644 --- a/api/v1/routes.go +++ b/api/v1/routes.go @@ -27,8 +27,8 @@ import ( "github.com/coreos/clair/api/context" "github.com/coreos/clair/database" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/pkg/tarutil" - cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/worker" ) @@ -118,7 +118,7 @@ func postLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx return postLayerRoute, statusUnprocessableEntity } - if _, badreq := err.(*cerrors.ErrBadRequest); badreq { + if _, badreq := err.(*commonerr.ErrBadRequest); badreq { writeResponse(w, r, http.StatusBadRequest, LayerEnvelope{Error: &Error{err.Error()}}) return postLayerRoute, http.StatusBadRequest } @@ -143,7 +143,7 @@ func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx * _, withVulnerabilities := r.URL.Query()["vulnerabilities"] dbLayer, err := ctx.Store.FindLayer(p.ByName("layerName"), withFeatures, withVulnerabilities) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, LayerEnvelope{Error: &Error{err.Error()}}) return getLayerRoute, http.StatusNotFound } else if err != nil { @@ -159,7 +159,7 @@ func getLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx * func deleteLayer(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) { err := ctx.Store.DeleteLayer(p.ByName("layerName")) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, LayerEnvelope{Error: &Error{err.Error()}}) return deleteLayerRoute, http.StatusNotFound } else if err != nil { @@ -223,7 +223,7 @@ func getVulnerabilities(w http.ResponseWriter, r *http.Request, p httprouter.Par } dbVulns, nextPage, err := ctx.Store.ListVulnerabilities(namespace, limit, page) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}}) return getVulnerabilityRoute, http.StatusNotFound } else if err != nil { @@ -273,7 +273,7 @@ func postVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Para err = ctx.Store.InsertVulnerabilities([]database.Vulnerability{vuln}, true) if err != nil { switch err.(type) { - case *cerrors.ErrBadRequest: + case *commonerr.ErrBadRequest: writeResponse(w, r, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{err.Error()}}) return postVulnerabilityRoute, http.StatusBadRequest default: @@ -290,7 +290,7 @@ func getVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Param _, withFixedIn := r.URL.Query()["fixedIn"] dbVuln, err := ctx.Store.FindVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName")) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}}) return getVulnerabilityRoute, http.StatusNotFound } else if err != nil { @@ -334,7 +334,7 @@ func putVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Param err = ctx.Store.InsertVulnerabilities([]database.Vulnerability{vuln}, true) if err != nil { switch err.(type) { - case *cerrors.ErrBadRequest: + case *commonerr.ErrBadRequest: writeResponse(w, r, http.StatusBadRequest, VulnerabilityEnvelope{Error: &Error{err.Error()}}) return putVulnerabilityRoute, http.StatusBadRequest default: @@ -349,7 +349,7 @@ func putVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Param func deleteVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) { err := ctx.Store.DeleteVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName")) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, VulnerabilityEnvelope{Error: &Error{err.Error()}}) return deleteVulnerabilityRoute, http.StatusNotFound } else if err != nil { @@ -363,7 +363,7 @@ func deleteVulnerability(w http.ResponseWriter, r *http.Request, p httprouter.Pa func getFixes(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) { dbVuln, err := ctx.Store.FindVulnerability(p.ByName("namespaceName"), p.ByName("vulnerabilityName")) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}}) return getFixesRoute, http.StatusNotFound } else if err != nil { @@ -403,11 +403,11 @@ func putFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *co err = ctx.Store.InsertVulnerabilityFixes(p.ByName("vulnerabilityNamespace"), p.ByName("vulnerabilityName"), []database.FeatureVersion{dbFix}) if err != nil { switch err.(type) { - case *cerrors.ErrBadRequest: + case *commonerr.ErrBadRequest: writeResponse(w, r, http.StatusBadRequest, FeatureEnvelope{Error: &Error{err.Error()}}) return putFixRoute, http.StatusBadRequest default: - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}}) return putFixRoute, http.StatusNotFound } @@ -422,7 +422,7 @@ func putFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *co func deleteFix(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) { err := ctx.Store.DeleteVulnerabilityFix(p.ByName("vulnerabilityNamespace"), p.ByName("vulnerabilityName"), p.ByName("fixName")) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, FeatureEnvelope{Error: &Error{err.Error()}}) return deleteFixRoute, http.StatusNotFound } else if err != nil { @@ -468,7 +468,7 @@ func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params } dbNotification, nextPage, err := ctx.Store.GetNotification(p.ByName("notificationName"), limit, page) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, NotificationEnvelope{Error: &Error{err.Error()}}) return deleteNotificationRoute, http.StatusNotFound } else if err != nil { @@ -484,7 +484,7 @@ func getNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params func deleteNotification(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *context.RouteContext) (string, int) { err := ctx.Store.DeleteNotification(p.ByName("notificationName")) - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { writeResponse(w, r, http.StatusNotFound, NotificationEnvelope{Error: &Error{err.Error()}}) return deleteNotificationRoute, http.StatusNotFound } else if err != nil { diff --git a/database/pgsql/feature.go b/database/pgsql/feature.go index a0be7612..c39bd5b7 100644 --- a/database/pgsql/feature.go +++ b/database/pgsql/feature.go @@ -1,4 +1,4 @@ -// Copyright 2016 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -21,12 +21,12 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" ) func (pgSQL *pgSQL) insertFeature(feature database.Feature) (int, error) { if feature.Name == "" { - return 0, cerrors.NewBadRequestError("could not find/insert invalid Feature") + return 0, commonerr.NewBadRequestError("could not find/insert invalid Feature") } // Do cache lookup. @@ -65,7 +65,7 @@ func (pgSQL *pgSQL) insertFeature(feature database.Feature) (int, error) { func (pgSQL *pgSQL) insertFeatureVersion(fv database.FeatureVersion) (id int, err error) { err = versionfmt.Valid(fv.Feature.Namespace.VersionFormat, fv.Version) if err != nil { - return 0, cerrors.NewBadRequestError("could not find/insert invalid FeatureVersion") + return 0, commonerr.NewBadRequestError("could not find/insert invalid FeatureVersion") } // Do cache lookup. diff --git a/database/pgsql/keyvalue.go b/database/pgsql/keyvalue.go index 264774c7..14730d51 100644 --- a/database/pgsql/keyvalue.go +++ b/database/pgsql/keyvalue.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,14 +18,14 @@ import ( "database/sql" "time" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" ) // InsertKeyValue stores (or updates) a single key / value tuple. func (pgSQL *pgSQL) InsertKeyValue(key, value string) (err error) { if key == "" || value == "" { log.Warning("could not insert a flag which has an empty name or value") - return cerrors.NewBadRequestError("could not insert a flag which has an empty name or value") + return commonerr.NewBadRequestError("could not insert a flag which has an empty name or value") } defer observeQueryTime("InsertKeyValue", "all", time.Now()) diff --git a/database/pgsql/layer.go b/database/pgsql/layer.go index aab7cfe0..5b50efd3 100644 --- a/database/pgsql/layer.go +++ b/database/pgsql/layer.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -22,8 +22,8 @@ import ( "github.com/guregu/null/zero" "github.com/coreos/clair/database" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils" - cerrors "github.com/coreos/clair/utils/errors" ) func (pgSQL *pgSQL) FindLayer(name string, withFeatures, withVulnerabilities bool) (database.Layer, error) { @@ -247,12 +247,12 @@ func (pgSQL *pgSQL) InsertLayer(layer database.Layer) error { // Verify parameters if layer.Name == "" { log.Warning("could not insert a layer which has an empty Name") - return cerrors.NewBadRequestError("could not insert a layer which has an empty Name") + return commonerr.NewBadRequestError("could not insert a layer which has an empty Name") } // Get a potentially existing layer. existingLayer, err := pgSQL.FindLayer(layer.Name, true, false) - if err != nil && err != cerrors.ErrNotFound { + if err != nil && err != commonerr.ErrNotFound { return err } else if err == nil { if existingLayer.EngineVersion >= layer.EngineVersion { @@ -271,7 +271,7 @@ func (pgSQL *pgSQL) InsertLayer(layer database.Layer) error { if layer.Parent != nil { if layer.Parent.ID == 0 { log.Warning("Parent is expected to be retrieved from database when inserting a layer.") - return cerrors.NewBadRequestError("Parent is expected to be retrieved from database when inserting a layer.") + return commonerr.NewBadRequestError("Parent is expected to be retrieved from database when inserting a layer.") } parentID = zero.IntFrom(int64(layer.Parent.ID)) @@ -429,7 +429,7 @@ func (pgSQL *pgSQL) DeleteLayer(name string) error { } if affected <= 0 { - return cerrors.ErrNotFound + return commonerr.ErrNotFound } return nil diff --git a/database/pgsql/layer_test.go b/database/pgsql/layer_test.go index dcd420b7..3aa6e622 100644 --- a/database/pgsql/layer_test.go +++ b/database/pgsql/layer_test.go @@ -1,4 +1,4 @@ -// Copyright 2016 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt/dpkg" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils/types" ) @@ -363,19 +363,19 @@ func testInsertLayerUpdate(t *testing.T, datastore database.Datastore) { func testInsertLayerDelete(t *testing.T, datastore database.Datastore) { err := datastore.DeleteLayer("TestInsertLayerX") - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) err = datastore.DeleteLayer("TestInsertLayer3") assert.Nil(t, err) _, err = datastore.FindLayer("TestInsertLayer3", false, false) - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) _, err = datastore.FindLayer("TestInsertLayer4a", false, false) - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) _, err = datastore.FindLayer("TestInsertLayer4b", true, false) - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) } func cmpFV(a, b database.FeatureVersion) bool { diff --git a/database/pgsql/lock.go b/database/pgsql/lock.go index 2f491caa..f677232e 100644 --- a/database/pgsql/lock.go +++ b/database/pgsql/lock.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,7 +17,7 @@ package pgsql import ( "time" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" ) // Lock tries to set a temporary lock in the database. @@ -80,7 +80,7 @@ func (pgSQL *pgSQL) Unlock(name, owner string) { func (pgSQL *pgSQL) FindLock(name string) (string, time.Time, error) { if name == "" { log.Warning("could not find an invalid lock") - return "", time.Time{}, cerrors.NewBadRequestError("could not find an invalid lock") + return "", time.Time{}, commonerr.NewBadRequestError("could not find an invalid lock") } defer observeQueryTime("FindLock", "all", time.Now()) diff --git a/database/pgsql/namespace.go b/database/pgsql/namespace.go index 941db0d1..8d4b304b 100644 --- a/database/pgsql/namespace.go +++ b/database/pgsql/namespace.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -18,12 +18,12 @@ import ( "time" "github.com/coreos/clair/database" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" ) func (pgSQL *pgSQL) insertNamespace(namespace database.Namespace) (int, error) { if namespace.Name == "" { - return 0, cerrors.NewBadRequestError("could not find/insert invalid Namespace") + return 0, commonerr.NewBadRequestError("could not find/insert invalid Namespace") } if pgSQL.cache != nil { diff --git a/database/pgsql/notification.go b/database/pgsql/notification.go index eec59c21..6d079753 100644 --- a/database/pgsql/notification.go +++ b/database/pgsql/notification.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,7 +19,7 @@ import ( "time" "github.com/coreos/clair/database" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" "github.com/guregu/null/zero" "github.com/pborman/uuid" ) @@ -242,7 +242,7 @@ func (pgSQL *pgSQL) DeleteNotification(name string) error { } if affected <= 0 { - return cerrors.ErrNotFound + return commonerr.ErrNotFound } return nil diff --git a/database/pgsql/notification_test.go b/database/pgsql/notification_test.go index 476b5452..5dab2c22 100644 --- a/database/pgsql/notification_test.go +++ b/database/pgsql/notification_test.go @@ -1,4 +1,4 @@ -// Copyright 2016 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt/dpkg" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils/types" ) @@ -37,7 +37,7 @@ func TestNotification(t *testing.T) { // Try to get a notification when there is none. _, err = datastore.GetAvailableNotification(time.Second) - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) // Create some data. f1 := database.Feature{ @@ -126,7 +126,7 @@ func TestNotification(t *testing.T) { // Verify the renotify behaviour. if assert.Nil(t, datastore.SetNotificationNotified(notification.Name)) { _, err := datastore.GetAvailableNotification(time.Second) - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) time.Sleep(50 * time.Millisecond) notificationB, err := datastore.GetAvailableNotification(20 * time.Millisecond) @@ -164,7 +164,7 @@ func TestNotification(t *testing.T) { assert.Nil(t, datastore.DeleteNotification(notification.Name)) _, err = datastore.GetAvailableNotification(time.Millisecond) - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) } // Update a vulnerability and ensure that the old/new vulnerabilities are correct. diff --git a/database/pgsql/pgsql.go b/database/pgsql/pgsql.go index aec49b7e..f1d8d7d6 100644 --- a/database/pgsql/pgsql.go +++ b/database/pgsql/pgsql.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -34,8 +34,8 @@ import ( "github.com/coreos/clair/config" "github.com/coreos/clair/database" "github.com/coreos/clair/database/pgsql/migrations" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils" - cerrors "github.com/coreos/clair/utils/errors" ) var ( @@ -196,12 +196,12 @@ func openDatabase(registrableComponentConfig config.RegistrableComponentConfig) func parseConnectionString(source string) (dbName string, pgSourceURL string, err error) { if source == "" { - return "", "", cerrors.NewBadRequestError("pgsql: no database connection string specified") + return "", "", commonerr.NewBadRequestError("pgsql: no database connection string specified") } sourceURL, err := url.Parse(source) if err != nil { - return "", "", cerrors.NewBadRequestError("pgsql: database connection string is not a valid URL") + return "", "", commonerr.NewBadRequestError("pgsql: database connection string is not a valid URL") } dbName = strings.TrimPrefix(sourceURL.Path, "/") @@ -280,7 +280,7 @@ func handleError(desc string, err error) error { } if err == sql.ErrNoRows { - return cerrors.ErrNotFound + return commonerr.ErrNotFound } log.Errorf("%s: %v", desc, err) diff --git a/database/pgsql/vulnerability.go b/database/pgsql/vulnerability.go index 201fa549..17461374 100644 --- a/database/pgsql/vulnerability.go +++ b/database/pgsql/vulnerability.go @@ -1,4 +1,4 @@ -// Copyright 2016 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,8 +23,8 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils" - cerrors "github.com/coreos/clair/utils/errors" "github.com/guregu/null/zero" ) @@ -37,7 +37,7 @@ func (pgSQL *pgSQL) ListVulnerabilities(namespaceName string, limit int, startID if err != nil { return nil, -1, handleError("searchNamespace", err) } else if id == 0 { - return nil, -1, cerrors.ErrNotFound + return nil, -1, commonerr.ErrNotFound } // Query. @@ -130,7 +130,7 @@ func scanVulnerability(queryer Queryer, queryName string, vulnerabilityRow *sql. } if vulnerability.ID == 0 { - return vulnerability, cerrors.ErrNotFound + return vulnerability, commonerr.ErrNotFound } // Query the FixedIn FeatureVersion now. @@ -195,12 +195,12 @@ func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, on // Verify parameters if vulnerability.Name == "" || vulnerability.Namespace.Name == "" { - return cerrors.NewBadRequestError("insertVulnerability needs at least the Name and the Namespace") + return commonerr.NewBadRequestError("insertVulnerability needs at least the Name and the Namespace") } if !onlyFixedIn && !vulnerability.Severity.IsValid() { msg := fmt.Sprintf("could not insert a vulnerability that has an invalid Severity: %s", vulnerability.Severity) log.Warning(msg) - return cerrors.NewBadRequestError(msg) + return commonerr.NewBadRequestError(msg) } for i := 0; i < len(vulnerability.FixedIn); i++ { fifv := &vulnerability.FixedIn[i] @@ -212,7 +212,7 @@ func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, on } else if fifv.Feature.Namespace.Name != vulnerability.Namespace.Name { msg := "could not insert an invalid vulnerability that contains FixedIn FeatureVersion that are not in the same namespace as the Vulnerability" log.Warning(msg) - return cerrors.NewBadRequestError(msg) + return commonerr.NewBadRequestError(msg) } } @@ -228,7 +228,7 @@ func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, on // Find existing vulnerability and its Vulnerability_FixedIn_Features (for update). existingVulnerability, err := findVulnerability(tx, vulnerability.Namespace.Name, vulnerability.Name, true) - if err != nil && err != cerrors.ErrNotFound { + if err != nil && err != commonerr.ErrNotFound { tx.Rollback() return err } @@ -237,7 +237,7 @@ func (pgSQL *pgSQL) insertVulnerability(vulnerability database.Vulnerability, on // Because this call tries to update FixedIn FeatureVersion, import all other data from the // existing one. if existingVulnerability.ID == 0 { - return cerrors.ErrNotFound + return commonerr.ErrNotFound } fixedIn := vulnerability.FixedIn diff --git a/database/pgsql/vulnerability_test.go b/database/pgsql/vulnerability_test.go index e1d8d4c4..788856ba 100644 --- a/database/pgsql/vulnerability_test.go +++ b/database/pgsql/vulnerability_test.go @@ -1,4 +1,4 @@ -// Copyright 2016 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt/dpkg" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils/types" ) @@ -37,7 +37,7 @@ func TestFindVulnerability(t *testing.T) { // Find a vulnerability that does not exist. _, err = datastore.FindVulnerability("", "") - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) // Find a normal vulnerability. v1 := database.Vulnerability{ @@ -93,15 +93,15 @@ func TestDeleteVulnerability(t *testing.T) { // Delete non-existing Vulnerability. err = datastore.DeleteVulnerability("TestDeleteVulnerabilityNamespace1", "CVE-OPENSSL-1-DEB7") - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) err = datastore.DeleteVulnerability("debian:7", "TestDeleteVulnerabilityVulnerability1") - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) // Delete Vulnerability. err = datastore.DeleteVulnerability("debian:7", "CVE-OPENSSL-1-DEB7") if assert.Nil(t, err) { _, err := datastore.FindVulnerability("debian:7", "CVE-OPENSSL-1-DEB7") - assert.Equal(t, cerrors.ErrNotFound, err) + assert.Equal(t, commonerr.ErrNotFound, err) } } diff --git a/ext/vulnmdsrc/nvd/nvd.go b/ext/vulnmdsrc/nvd/nvd.go index 31636a77..1b98f609 100644 --- a/ext/vulnmdsrc/nvd/nvd.go +++ b/ext/vulnmdsrc/nvd/nvd.go @@ -35,7 +35,7 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/vulnmdsrc" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils/types" ) @@ -79,7 +79,7 @@ func (a *appender) BuildCache(datastore database.Datastore) error { if a.localPath == "" { // Create a temporary folder to store the NVD data and create hashes struct. if a.localPath, err = ioutil.TempDir(os.TempDir(), "nvd-data"); err != nil { - return cerrors.ErrFilesystem + return commonerr.ErrFilesystem } a.dataFeedHashes = make(map[string]string) @@ -97,7 +97,7 @@ func (a *appender) BuildCache(datastore database.Datastore) error { var nvd nvd if err = xml.NewDecoder(dataFeedReader).Decode(&nvd); err != nil { log.Errorf("could not decode NVD data feed '%s': %s", dataFeedName, err) - return cerrors.ErrCouldNotParse + return commonerr.ErrCouldNotParse } // For each entry of this data feed: @@ -179,14 +179,14 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin r, err := http.Get(fmt.Sprintf(dataFeedURL, dataFeedName)) if err != nil { log.Errorf("could not download NVD data feed file '%s': %s", dataFeedName, err) - return dataFeedReaders, dataFeedHashes, cerrors.ErrCouldNotDownload + return dataFeedReaders, dataFeedHashes, commonerr.ErrCouldNotDownload } // Un-gzip it. gr, err := gzip.NewReader(r.Body) if err != nil { log.Errorf("could not read NVD data feed file '%s': %s", dataFeedName, err) - return dataFeedReaders, dataFeedHashes, cerrors.ErrCouldNotDownload + return dataFeedReaders, dataFeedHashes, commonerr.ErrCouldNotDownload } // Store it to a file at the same time if possible. diff --git a/ext/vulnsrc/alpine/alpine.go b/ext/vulnsrc/alpine/alpine.go index a2bbf7ec..20a59033 100644 --- a/ext/vulnsrc/alpine/alpine.go +++ b/ext/vulnsrc/alpine/alpine.go @@ -31,8 +31,8 @@ import ( "github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/ext/vulnsrc" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils" - cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" ) @@ -172,7 +172,7 @@ func (u *updater) pullRepository() (commit string, err error) { if out, err := utils.Exec(u.repositoryLocalPath, "git", "clone", secdbGitURL, "."); err != nil { u.Clean() log.Errorf("could not pull alpine-secdb repository: %s. output: %s", err, out) - return "", cerrors.ErrCouldNotDownload + return "", commonerr.ErrCouldNotDownload } } else { // The repository exists and it needs to be refreshed via a pull. diff --git a/ext/vulnsrc/debian/debian.go b/ext/vulnsrc/debian/debian.go index 84d4398f..a0388e72 100644 --- a/ext/vulnsrc/debian/debian.go +++ b/ext/vulnsrc/debian/debian.go @@ -31,7 +31,7 @@ import ( "github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/ext/vulnsrc" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils/types" ) @@ -69,7 +69,7 @@ func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateRespo r, err := http.Get(url) if err != nil { log.Errorf("could not download Debian's update: %s", err) - return resp, cerrors.ErrCouldNotDownload + return resp, commonerr.ErrCouldNotDownload } // Get the SHA-1 of the latest update's JSON data @@ -110,7 +110,7 @@ func buildResponse(jsonReader io.Reader, latestKnownHash string) (resp vulnsrc.U err = json.NewDecoder(teedJSONReader).Decode(&data) if err != nil { log.Errorf("could not unmarshal Debian's JSON: %s", err) - return resp, cerrors.ErrCouldNotParse + return resp, commonerr.ErrCouldNotParse } // Calculate the hash and skip updating if the hash has been seen before. diff --git a/ext/vulnsrc/oracle/oracle.go b/ext/vulnsrc/oracle/oracle.go index 32afeaa0..569752ad 100644 --- a/ext/vulnsrc/oracle/oracle.go +++ b/ext/vulnsrc/oracle/oracle.go @@ -29,7 +29,7 @@ import ( "github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/ext/vulnsrc" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils/types" "github.com/coreos/pkg/capnslog" ) @@ -103,7 +103,7 @@ func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateRespo r, err := http.Get(ovalURI) if err != nil { log.Errorf("could not download Oracle's update list: %s", err) - return resp, cerrors.ErrCouldNotDownload + return resp, commonerr.ErrCouldNotDownload } defer r.Body.Close() @@ -126,7 +126,7 @@ func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateRespo r, err := http.Get(ovalURI + elsaFilePrefix + strconv.Itoa(elsa) + ".xml") if err != nil { log.Errorf("could not download Oracle's update file: %s", err) - return resp, cerrors.ErrCouldNotDownload + return resp, commonerr.ErrCouldNotDownload } // Parse the XML. @@ -160,7 +160,7 @@ func parseELSA(ovalReader io.Reader) (vulnerabilities []database.Vulnerability, err = xml.NewDecoder(ovalReader).Decode(&ov) if err != nil { log.Errorf("could not decode Oracle's XML: %s", err) - err = cerrors.ErrCouldNotParse + err = commonerr.ErrCouldNotParse return } diff --git a/ext/vulnsrc/rhel/rhel.go b/ext/vulnsrc/rhel/rhel.go index 6714dcd8..f1d88986 100644 --- a/ext/vulnsrc/rhel/rhel.go +++ b/ext/vulnsrc/rhel/rhel.go @@ -31,7 +31,7 @@ import ( "github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt/rpm" "github.com/coreos/clair/ext/vulnsrc" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils/types" ) @@ -107,7 +107,7 @@ func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateRespo r, err := http.Get(ovalURI) if err != nil { log.Errorf("could not download RHEL's update list: %s", err) - return resp, cerrors.ErrCouldNotDownload + return resp, commonerr.ErrCouldNotDownload } // Get the list of RHSAs that we have to process. @@ -129,7 +129,7 @@ func (u *updater) Update(datastore database.Datastore) (resp vulnsrc.UpdateRespo r, err := http.Get(ovalURI + rhsaFilePrefix + strconv.Itoa(rhsa) + ".xml") if err != nil { log.Errorf("could not download RHEL's update file: %s", err) - return resp, cerrors.ErrCouldNotDownload + return resp, commonerr.ErrCouldNotDownload } // Parse the XML. @@ -163,7 +163,7 @@ func parseRHSA(ovalReader io.Reader) (vulnerabilities []database.Vulnerability, err = xml.NewDecoder(ovalReader).Decode(&ov) if err != nil { log.Errorf("could not decode RHEL's XML: %s", err) - err = cerrors.ErrCouldNotParse + err = commonerr.ErrCouldNotParse return } diff --git a/ext/vulnsrc/ubuntu/ubuntu.go b/ext/vulnsrc/ubuntu/ubuntu.go index ea0035da..0a74db4a 100644 --- a/ext/vulnsrc/ubuntu/ubuntu.go +++ b/ext/vulnsrc/ubuntu/ubuntu.go @@ -33,8 +33,8 @@ import ( "github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt/dpkg" "github.com/coreos/clair/ext/vulnsrc" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils" - cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/utils/types" ) @@ -176,7 +176,7 @@ func (u *updater) pullRepository() (err error) { // Branch repository. if out, err := utils.Exec(u.repositoryLocalPath, "bzr", "branch", "--use-existing-dir", trackerRepository, "."); err != nil { log.Errorf("could not branch Ubuntu repository: %s. output: %s", err, out) - return cerrors.ErrCouldNotDownload + return commonerr.ErrCouldNotDownload } return nil @@ -187,7 +187,7 @@ func (u *updater) pullRepository() (err error) { os.RemoveAll(u.repositoryLocalPath) log.Errorf("could not pull Ubuntu repository: %s. output: %s", err, out) - return cerrors.ErrCouldNotDownload + return commonerr.ErrCouldNotDownload } return nil @@ -197,12 +197,12 @@ func getRevisionNumber(pathToRepo string) (int, error) { out, err := utils.Exec(pathToRepo, "bzr", "revno") if err != nil { log.Errorf("could not get Ubuntu repository's revision number: %s. output: %s", err, out) - return 0, cerrors.ErrCouldNotDownload + return 0, commonerr.ErrCouldNotDownload } revno, err := strconv.Atoi(strings.TrimSpace(string(out))) if err != nil { log.Errorf("could not parse Ubuntu repository's revision number: %s. output: %s", err, out) - return 0, cerrors.ErrCouldNotDownload + return 0, commonerr.ErrCouldNotDownload } return revno, nil } @@ -255,7 +255,7 @@ func collectModifiedVulnerabilities(revision int, dbRevision, repositoryLocalPat out, err := utils.Exec(repositoryLocalPath, "bzr", "log", "--verbose", "-r"+strconv.Itoa(dbRevisionInt+1)+"..", "-n0") if err != nil { log.Errorf("could not get Ubuntu vulnerabilities repository logs: %s. output: %s", err, out) - return nil, cerrors.ErrCouldNotDownload + return nil, commonerr.ErrCouldNotDownload } scanner := bufio.NewScanner(bytes.NewReader(out)) diff --git a/notifier/notifier.go b/notifier/notifier.go index d1fa6b2f..09f8b368 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -28,8 +28,8 @@ import ( "github.com/coreos/clair/config" "github.com/coreos/clair/database" "github.com/coreos/clair/ext/notification" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils" - cerrors "github.com/coreos/clair/utils/errors" ) const ( @@ -127,7 +127,7 @@ func findTask(datastore database.Datastore, renotifyInterval time.Duration, whoA notification, err := datastore.GetAvailableNotification(renotifyInterval) if err != nil { // There is no notification or an error occurred. - if err != cerrors.ErrNotFound { + if err != commonerr.ErrNotFound { log.Warningf("could not get notification to send: %s", err) } diff --git a/utils/errors/errors.go b/pkg/commonerr/errors.go similarity index 91% rename from utils/errors/errors.go rename to pkg/commonerr/errors.go index 65508f2b..1e690eea 100644 --- a/utils/errors/errors.go +++ b/pkg/commonerr/errors.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,8 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package errors defines error types that are used in several modules -package errors +// Package commonerr defines reusable error types common throughout the Clair +// codebase. +package commonerr import "errors" diff --git a/utils/http/http.go b/utils/http/http.go index b02430a4..46ac5a0e 100644 --- a/utils/http/http.go +++ b/utils/http/http.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -21,8 +21,8 @@ import ( "net/http" "github.com/coreos/clair/database" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/pkg/tarutil" - cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/worker" ) @@ -48,11 +48,11 @@ func WriteHTTPError(w http.ResponseWriter, httpStatus int, err error) { if httpStatus == 0 { httpStatus = http.StatusInternalServerError // Try to guess the http status code from the error type - if _, isBadRequestError := err.(*cerrors.ErrBadRequest); isBadRequestError { + if _, isBadRequestError := err.(*commonerr.ErrBadRequest); isBadRequestError { httpStatus = http.StatusBadRequest } else { switch err { - case cerrors.ErrNotFound: + case commonerr.ErrNotFound: httpStatus = http.StatusNotFound case database.ErrBackendException: httpStatus = http.StatusServiceUnavailable diff --git a/worker/detectors/feature/rpm/rpm.go b/worker/detectors/feature/rpm/rpm.go index ba45f610..3a158d83 100644 --- a/worker/detectors/feature/rpm/rpm.go +++ b/worker/detectors/feature/rpm/rpm.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -25,8 +25,8 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt" "github.com/coreos/clair/ext/versionfmt/rpm" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils" - cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/worker/detectors" ) @@ -55,13 +55,13 @@ func (detector *RpmFeaturesDetector) Detect(data map[string][]byte) ([]database. defer os.RemoveAll(tmpDir) if err != nil { log.Errorf("could not create temporary folder for RPM detection: %s", err) - return []database.FeatureVersion{}, cerrors.ErrFilesystem + return []database.FeatureVersion{}, commonerr.ErrFilesystem } err = ioutil.WriteFile(tmpDir+"/Packages", f, 0700) if err != nil { log.Errorf("could not create temporary file for RPM detection: %s", err) - return []database.FeatureVersion{}, cerrors.ErrFilesystem + return []database.FeatureVersion{}, commonerr.ErrFilesystem } // Query RPM diff --git a/worker/worker.go b/worker/worker.go index c3d2de3d..984def88 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,8 +20,8 @@ import ( "github.com/coreos/pkg/capnslog" "github.com/coreos/clair/database" + "github.com/coreos/clair/pkg/commonerr" "github.com/coreos/clair/utils" - cerrors "github.com/coreos/clair/utils/errors" "github.com/coreos/clair/worker/detectors" ) @@ -41,11 +41,11 @@ var ( // ErrUnsupported is the error that should be raised when an OS or package // manager is not supported. - ErrUnsupported = cerrors.NewBadRequestError("worker: OS and/or package manager are not supported") + ErrUnsupported = commonerr.NewBadRequestError("worker: OS and/or package manager are not supported") // ErrParentUnknown is the error that should be raised when a parent layer // has yet to be processed for the current layer. - ErrParentUnknown = cerrors.NewBadRequestError("worker: parent layer is unknown, it must be processed first") + ErrParentUnknown = commonerr.NewBadRequestError("worker: parent layer is unknown, it must be processed first") ) // Process detects the Namespace of a layer, the features it adds/removes, and @@ -55,15 +55,15 @@ var ( func Process(datastore database.Datastore, imageFormat, name, parentName, path string, headers map[string]string) error { // Verify parameters. if name == "" { - return cerrors.NewBadRequestError("could not process a layer which does not have a name") + return commonerr.NewBadRequestError("could not process a layer which does not have a name") } if path == "" { - return cerrors.NewBadRequestError("could not process a layer which does not have a path") + return commonerr.NewBadRequestError("could not process a layer which does not have a path") } if imageFormat == "" { - return cerrors.NewBadRequestError("could not process a layer which does not have a format") + return commonerr.NewBadRequestError("could not process a layer which does not have a format") } log.Debugf("layer %s: processing (Location: %s, Engine version: %d, Parent: %s, Format: %s)", @@ -71,11 +71,11 @@ func Process(datastore database.Datastore, imageFormat, name, parentName, path s // Check to see if the layer is already in the database. layer, err := datastore.FindLayer(name, false, false) - if err != nil && err != cerrors.ErrNotFound { + if err != nil && err != commonerr.ErrNotFound { return err } - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { // New layer case. layer = database.Layer{Name: name, EngineVersion: Version} @@ -83,10 +83,10 @@ func Process(datastore database.Datastore, imageFormat, name, parentName, path s // We need to get it with its Features in order to diff them. if parentName != "" { parent, err := datastore.FindLayer(parentName, true, false) - if err != nil && err != cerrors.ErrNotFound { + if err != nil && err != commonerr.ErrNotFound { return err } - if err == cerrors.ErrNotFound { + if err == commonerr.ErrNotFound { log.Warningf("layer %s: the parent layer (%s) is unknown. it must be processed first", name, parentName) return ErrParentUnknown diff --git a/worker/worker_test.go b/worker/worker_test.go index fb8c270f..cc69ff11 100644 --- a/worker/worker_test.go +++ b/worker/worker_test.go @@ -1,4 +1,4 @@ -// Copyright 2015 clair authors +// Copyright 2017 clair authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -23,7 +23,7 @@ import ( "github.com/coreos/clair/database" "github.com/coreos/clair/ext/versionfmt/dpkg" - cerrors "github.com/coreos/clair/utils/errors" + "github.com/coreos/clair/pkg/commonerr" // Register the required detectors. _ "github.com/coreos/clair/worker/detectors/data/docker" @@ -57,7 +57,7 @@ func TestProcessWithDistUpgrade(t *testing.T) { if layer, exists := datastore.layers[name]; exists { return layer, nil } - return database.Layer{}, cerrors.ErrNotFound + return database.Layer{}, commonerr.ErrNotFound } // Create the list of FeatureVersions that should not been upgraded from one layer to another.