Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
serving scan reports and adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Erez Freiberger committed Apr 6, 2017
1 parent ab0ac54 commit aad87b9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
21 changes: 4 additions & 17 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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.
Expand Down
4 changes: 1 addition & 3 deletions pkg/imageserver/types.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/imageserver/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"golang.org/x/net/webdav"

docker "github.com/fsouza/go-dockerclient"
iiapi "github.com/openshift/image-inspector/pkg/api"
)

Expand Down Expand Up @@ -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 {

Expand Down
19 changes: 17 additions & 2 deletions pkg/inspector/image-inspector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package inspector

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
Expand All @@ -10,6 +11,7 @@ import (
"os"
"path"
"strings"
"time"

"archive/tar"
"crypto/rand"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 1 addition & 14 deletions pkg/inspector/image-inspector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit aad87b9

Please sign in to comment.