From aad87b99f51951faa7bbd1081e62c88c009beae9 Mon Sep 17 00:00:00 2001 From: Erez Freiberger Date: Tue, 27 Sep 2016 16:50:27 +0300 Subject: [PATCH] serving scan reports and adding tests --- pkg/api/types.go | 21 ++++----------------- pkg/imageserver/types.go | 4 +--- pkg/imageserver/webdav.go | 4 +--- pkg/inspector/image-inspector.go | 19 +++++++++++++++++-- pkg/inspector/image-inspector_test.go | 15 +-------------- 5 files changed, 24 insertions(+), 39 deletions(-) diff --git a/pkg/api/types.go b/pkg/api/types.go index 1684698..638b6c9 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -14,13 +14,13 @@ const ( StatusError OpenSCAPStatus = "Error" ) -type openSCAPMetadata struct { +type OpenSCAPMetadata struct { Status OpenSCAPStatus // Status of the OpenSCAP scan report ErrorMessage string // Error message from the openscap ContentTimeStamp string // Timestamp for this data } -func (osm *openSCAPMetadata) SetError(err error) { +func (osm *OpenSCAPMetadata) SetError(err error) { osm.Status = StatusError osm.ErrorMessage = err.Error() osm.ContentTimeStamp = string(time.Now().Format(time.RFC850)) @@ -33,21 +33,8 @@ var ( // InspectorMetadata is the metadata type with information about image-inspector's operation type InspectorMetadata struct { docker.Image // Metadata about the inspected image - - OpenSCAP *openSCAPMetadata -} - -// NewInspectorMetadata returns a new InspectorMetadata out of *docker.Image -// The OpenSCAP status will be NotRequested -func NewInspectorMetadata(imageMetadata *docker.Image) *InspectorMetadata { - return &InspectorMetadata{ - Image: *imageMetadata, - OpenSCAP: &openSCAPMetadata{ - Status: StatusNotRequested, - ErrorMessage: "", - ContentTimeStamp: string(time.Now().Format(time.RFC850)), - }, - } + // OpenSCAP describes the state of the OpenSCAP scan + OpenSCAP *OpenSCAPMetadata } // APIVersions holds a slice of supported API versions. diff --git a/pkg/imageserver/types.go b/pkg/imageserver/types.go index 093e388..6efd8c4 100644 --- a/pkg/imageserver/types.go +++ b/pkg/imageserver/types.go @@ -1,15 +1,13 @@ package imageserver import ( - docker "github.com/fsouza/go-dockerclient" iiapi "github.com/openshift/image-inspector/pkg/api" ) // ImageServer abstracts the serving of image information. type ImageServer interface { // ServeImage Serves the image - ServeImage(imageMetadata *docker.Image, - meta *iiapi.InspectorMetadata, + ServeImage(meta *iiapi.InspectorMetadata, scanReport []byte, htmlScanReport []byte) error } diff --git a/pkg/imageserver/webdav.go b/pkg/imageserver/webdav.go index 6f0b9fb..8b7ef1f 100644 --- a/pkg/imageserver/webdav.go +++ b/pkg/imageserver/webdav.go @@ -9,7 +9,6 @@ import ( "golang.org/x/net/webdav" - docker "github.com/fsouza/go-dockerclient" iiapi "github.com/openshift/image-inspector/pkg/api" ) @@ -37,8 +36,7 @@ func NewWebdavImageServer(opts ImageServerOptions, chroot bool) ImageServer { } // ServeImage Serves the image. -func (s *webdavImageServer) ServeImage(imageMetadata *docker.Image, - meta *iiapi.InspectorMetadata, +func (s *webdavImageServer) ServeImage(meta *iiapi.InspectorMetadata, scanReport []byte, htmlScanReport []byte) error { diff --git a/pkg/inspector/image-inspector.go b/pkg/inspector/image-inspector.go index 69c1ead..34ef3b4 100644 --- a/pkg/inspector/image-inspector.go +++ b/pkg/inspector/image-inspector.go @@ -1,6 +1,7 @@ package inspector import ( + "encoding/json" "fmt" "io" "io/ioutil" @@ -10,6 +11,7 @@ import ( "os" "path" "strings" + "time" "archive/tar" "crypto/rand" @@ -55,11 +57,24 @@ type defaultImageInspector struct { imageServer apiserver.ImageServer } +// NewInspectorMetadata returns a new InspectorMetadata out of *docker.Image +// The OpenSCAP status will be NotRequested +func NewInspectorMetadata(imageMetadata *docker.Image) iiapi.InspectorMetadata { + return iiapi.InspectorMetadata{ + Image: *imageMetadata, + OpenSCAP: &iiapi.OpenSCAPMetadata{ + Status: iiapi.StatusNotRequested, + ErrorMessage: "", + ContentTimeStamp: string(time.Now().Format(time.RFC850)), + }, + } +} + // NewDefaultImageInspector provides a new default inspector. func NewDefaultImageInspector(opts iicmd.ImageInspectorOptions) ImageInspector { inspector := &defaultImageInspector{ opts: opts, - meta: *iiapi.NewInspectorMetadata(&docker.Image{}), + meta: NewInspectorMetadata(&docker.Image{}), } // if serving then set up an image server @@ -121,7 +136,7 @@ func (i *defaultImageInspector) Inspect() error { } if i.imageServer != nil { - return i.imageServer.ServeImage(imageMetadata, &i.meta, + return i.imageServer.ServeImage(&i.meta, scanReport, htmlScanReport) } return nil diff --git a/pkg/inspector/image-inspector_test.go b/pkg/inspector/image-inspector_test.go index bae93b0..5d820f9 100644 --- a/pkg/inspector/image-inspector_test.go +++ b/pkg/inspector/image-inspector_test.go @@ -9,10 +9,6 @@ import ( "io/ioutil" "os" "testing" - - docker "github.com/fsouza/go-dockerclient" - iicmd "github.com/openshift/image-inspector/pkg/cmd" - "github.com/openshift/image-inspector/pkg/openscap" ) type FailMockScanner struct{} @@ -51,15 +47,6 @@ func (ms *NoResMockScanner) ResultsFileName() string { return "NoSuchFILE" } -type MockImageServer struct{} - -func (mis *MockImageServer) ServeImage(imageMetadata *docker.Image, - meta *iiapi.InspectorMetadata, - scanReport []byte, - htmlScanReport []byte) error { - return nil -} - func TestScanImage(t *testing.T) { iiWithHtml := defaultImageInspector{} iiWithHtml.opts.OpenScapHTML = true @@ -156,7 +143,7 @@ func TestGetAuthConfigs(t *testing.T) { } for k, v := range tests { - ii := &defaultImageInspector{*v.opts, iiapi.InspectorMetadata{}, &MockImageServer{}} + ii := &defaultImageInspector{*v.opts, iiapi.InspectorMetadata{}, nil} auths, err := ii.getAuthConfigs() if !v.shouldFail { if err != nil {