diff --git a/cmd/docker-slim-sensor/artifacts.go b/cmd/docker-slim-sensor/artifacts.go index e8d71da68f..7a88239df2 100755 --- a/cmd/docker-slim-sensor/artifacts.go +++ b/cmd/docker-slim-sensor/artifacts.go @@ -17,8 +17,9 @@ import ( "syscall" "github.com/docker-slim/docker-slim/messages" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" + "github.com/docker-slim/docker-slim/pkg/utils/fsutils" "github.com/docker-slim/docker-slim/report" - "github.com/docker-slim/docker-slim/utils" log "github.com/Sirupsen/logrus" ) @@ -239,7 +240,7 @@ func (p *artifactStore) saveArtifacts() { //TODO: use exludePaths to filter discovered links for linkName, linkProps := range p.linkMap { linkPath := fmt.Sprintf("%s/files%s", p.storeLocation, linkName) - linkDir := utils.FileDir(linkPath) + linkDir := fsutils.FileDir(linkPath) err := os.MkdirAll(linkDir, 0777) if err != nil { log.Warn("saveArtifacts - dir error => ", err) @@ -264,7 +265,7 @@ func (p *artifactStore) saveArtifacts() { for inPath, isDir := range includePaths { dstPath := fmt.Sprintf("%s/files%s", p.storeLocation, inPath) if isDir { - err, errs := utils.CopyDir(inPath, dstPath, true, true, nil, nil, nil) + err, errs := fsutils.CopyDir(inPath, dstPath, true, true, nil, nil, nil) if err != nil { log.Warnf("CopyDir(%v,%v) error: %v", inPath, dstPath, err) } @@ -273,7 +274,7 @@ func (p *artifactStore) saveArtifacts() { log.Warnf("CopyDir(%v,%v) copy errors: %+v", inPath, dstPath, errs) } } else { - if err := utils.CopyFile(inPath, dstPath, true); err != nil { + if err := fsutils.CopyFile(inPath, dstPath, true); err != nil { log.Warnf("CopyFile(%v,%v) error: %v", inPath, dstPath, err) } } @@ -301,17 +302,17 @@ func (p *artifactStore) saveReport() { if os.IsNotExist(err) { os.MkdirAll(artifactDirName, 0777) _, err = os.Stat(artifactDirName) - utils.FailOn(err) + errutils.FailOn(err) } reportFilePath := filepath.Join(artifactDirName, reportName) log.Debug("sensor: monitor - saving report to ", reportFilePath) reportData, err := json.MarshalIndent(creport, "", " ") - utils.FailOn(err) + errutils.FailOn(err) err = ioutil.WriteFile(reportFilePath, reportData, 0644) - utils.FailOn(err) + errutils.FailOn(err) } func getFileHash(artifactFileName string) (string, error) { @@ -357,7 +358,7 @@ func cpFile(src, dst string) error { } defer s.Close() - dstDir := utils.FileDir(dst) + dstDir := fsutils.FileDir(dst) err = os.MkdirAll(dstDir, 0777) if err != nil { log.Warnln("sensor: monitor - dir error =>", err) @@ -400,7 +401,7 @@ func cpFile(src, dst string) error { } //note: need to do the same for symlinks too - if err := utils.UpdateFileTimes(dst, sysStat.Atim, sysStat.Mtim); err != nil { + if err := fsutils.UpdateFileTimes(dst, sysStat.Atim, sysStat.Mtim); err != nil { log.Warnln("sensor: cpFile - UpdateFileTimes error =>", dst) return err } @@ -476,7 +477,7 @@ func createDummyFile(src, dst string) error { } //note: need to do the same for symlinks too - if err := utils.UpdateFileTimes(dst, sysStat.Mtim, sysStat.Atim); err != nil { + if err := fsutils.UpdateFileTimes(dst, sysStat.Mtim, sysStat.Atim); err != nil { log.Warnln("sensor: createDummyFile - UpdateFileTimes error =>", dst) return err } diff --git a/cmd/docker-slim-sensor/main.go b/cmd/docker-slim-sensor/main.go index b1e92545a2..4b2302eb2f 100755 --- a/cmd/docker-slim-sensor/main.go +++ b/cmd/docker-slim-sensor/main.go @@ -6,12 +6,12 @@ import ( "time" "github.com/docker-slim/docker-slim/messages" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" "github.com/docker-slim/docker-slim/report" "github.com/docker-slim/docker-slim/sensor/ipc" "github.com/docker-slim/docker-slim/sensor/monitors/fanotify" "github.com/docker-slim/docker-slim/sensor/monitors/pevent" "github.com/docker-slim/docker-slim/sensor/monitors/ptrace" - "github.com/docker-slim/docker-slim/utils" log "github.com/Sirupsen/logrus" "github.com/cloudimmunity/system" @@ -88,7 +88,7 @@ func main() { log.Infof("sensor: args => %#v", os.Args) dirName, err := os.Getwd() - utils.WarnOn(err) + errutils.WarnOn(err) log.Debugf("sensor: cwd => %#v", dirName) initSignalHandlers() @@ -101,10 +101,10 @@ func main() { doneChan = make(chan struct{}) err = ipc.InitChannels() - utils.FailOn(err) + errutils.FailOn(err) cmdChan, err := ipc.RunCmdServer(doneChan) - utils.FailOn(err) + errutils.FailOn(err) monDoneChan := make(chan bool, 1) monDoneAckChan := make(chan bool) diff --git a/cmd/docker-slim/cli.go b/cmd/docker-slim/cli.go index e58f8e1383..ef9b8972fe 100644 --- a/cmd/docker-slim/cli.go +++ b/cmd/docker-slim/cli.go @@ -8,7 +8,7 @@ import ( "github.com/docker-slim/docker-slim/master/commands" "github.com/docker-slim/docker-slim/master/config" - "github.com/docker-slim/docker-slim/utils" + "github.com/docker-slim/docker-slim/pkg/version" log "github.com/Sirupsen/logrus" "github.com/codegangsta/cli" @@ -24,7 +24,7 @@ var app *cli.App func init() { app = cli.NewApp() - app.Version = utils.CurrentVersion() + app.Version = version.Current() app.Name = AppName app.Usage = AppUsage app.CommandNotFound = func(ctx *cli.Context, command string) { diff --git a/master/builder/image_builder.go b/master/builder/image_builder.go index 2c6520f05b..d539a656de 100644 --- a/master/builder/image_builder.go +++ b/master/builder/image_builder.go @@ -6,7 +6,7 @@ import ( "github.com/docker-slim/docker-slim/master/config" "github.com/docker-slim/docker-slim/master/docker/dockerfile" - "github.com/docker-slim/docker-slim/utils" + "github.com/docker-slim/docker-slim/pkg/utils/fsutils" //log "github.com/Sirupsen/logrus" "github.com/cloudimmunity/go-dockerclientx" @@ -58,7 +58,7 @@ func NewImageBuilder(client *docker.Client, } dataDir := filepath.Join(artifactLocation, "files") - builder.HasData = utils.IsDir(dataDir) + builder.HasData = fsutils.IsDir(dataDir) return builder, nil } diff --git a/master/commands/build.go b/master/commands/build.go index f0d43e27fb..aad2dd3e5a 100644 --- a/master/commands/build.go +++ b/master/commands/build.go @@ -12,7 +12,9 @@ import ( "github.com/docker-slim/docker-slim/master/inspectors/container" "github.com/docker-slim/docker-slim/master/inspectors/container/probes/http" "github.com/docker-slim/docker-slim/master/inspectors/image" - "github.com/docker-slim/docker-slim/utils" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" + "github.com/docker-slim/docker-slim/pkg/utils/fsutils" + "github.com/docker-slim/docker-slim/pkg/version" log "github.com/Sirupsen/logrus" "github.com/dustin/go-humanize" @@ -44,7 +46,7 @@ func OnBuild(doDebug bool, client := dockerclient.New(clientConfig) imageInspector, err := image.NewInspector(client, imageRef) - utils.FailOn(err) + errutils.FailOn(err) if imageInspector.NoImage() { fmt.Println("docker-slim: [build] target image not found -", imageRef) @@ -53,9 +55,9 @@ func OnBuild(doDebug bool, log.Info("docker-slim: inspecting 'fat' image metadata...") err = imageInspector.Inspect() - utils.FailOn(err) + errutils.FailOn(err) - localVolumePath, artifactLocation := utils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID) + localVolumePath, artifactLocation := fsutils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID) imageInspector.ArtifactLocation = artifactLocation log.Infof("docker-slim: [%v] 'fat' image size => %v (%v)", @@ -65,7 +67,7 @@ func OnBuild(doDebug bool, log.Info("docker-slim: processing 'fat' image info...") err = imageInspector.ProcessCollectedData() - utils.FailOn(err) + errutils.FailOn(err) containerInspector, err := container.NewInspector(client, imageInspector, @@ -76,11 +78,11 @@ func OnBuild(doDebug bool, excludePaths, includePaths, doDebug) - utils.FailOn(err) + errutils.FailOn(err) log.Info("docker-slim: starting instrumented 'fat' container...") err = containerInspector.RunContainer() - utils.FailOn(err) + errutils.FailOn(err) log.Info("docker-slim: watching container monitor...") @@ -90,7 +92,7 @@ func OnBuild(doDebug bool, if doHTTPProbe { probe, err := http.NewCustomProbe(containerInspector, httpProbeCmds) - utils.FailOn(err) + errutils.FailOn(err) probe.Start() continueAfter.ContinueChan = probe.DoneChan() } @@ -113,24 +115,24 @@ func OnBuild(doDebug bool, <-continueAfter.ContinueChan fmt.Println("docker-slim: HTTP probe is done...") default: - utils.Fail("unknown continue-after mode") + errutils.Fail("unknown continue-after mode") } containerInspector.FinishMonitoring() log.Info("docker-slim: shutting down 'fat' container...") err = containerInspector.ShutdownContainer() - utils.WarnOn(err) + errutils.WarnOn(err) if !containerInspector.HasCollectedData() { imageInspector.ShowFatImageDockerInstructions() - fmt.Printf("docker-slim: [build] no data collected (no minified image generated) - done. (version: %v)\n", utils.CurrentVersion()) + fmt.Printf("docker-slim: [build] no data collected (no minified image generated) - done. (version: %v)\n", version.Current()) return } log.Info("docker-slim: processing instrumented 'fat' container info...") err = containerInspector.ProcessCollectedData() - utils.FailOn(err) + errutils.FailOn(err) if customImageTag == "" { customImageTag = imageInspector.SlimImageRepo @@ -143,21 +145,21 @@ func OnBuild(doDebug bool, artifactLocation, imageOverrides, overrides) - utils.FailOn(err) + errutils.FailOn(err) if !builder.HasData { log.Info("docker-slim: WARNING - no data artifacts") } err = builder.Build() - utils.FailOn(err) + errutils.FailOn(err) log.Infoln("docker-slim: created new image:", builder.RepoName, "( has data artifacts:", builder.HasData, ")") if doRmFileArtifacts { log.Info("docker-slim: removing temporary artifacts...") - err = utils.RemoveArtifacts(artifactLocation) //TODO: remove only the "files" subdirectory - utils.WarnOn(err) + err = fsutils.Remove(artifactLocation) //TODO: remove only the "files" subdirectory + errutils.WarnOn(err) } fmt.Println("docker-slim: [build] done.") diff --git a/master/commands/info.go b/master/commands/info.go index 98c1b41a0d..bb5eb93c39 100644 --- a/master/commands/info.go +++ b/master/commands/info.go @@ -6,7 +6,8 @@ import ( "github.com/docker-slim/docker-slim/master/config" "github.com/docker-slim/docker-slim/master/docker/dockerclient" "github.com/docker-slim/docker-slim/master/inspectors/image" - "github.com/docker-slim/docker-slim/utils" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" + "github.com/docker-slim/docker-slim/pkg/utils/fsutils" log "github.com/Sirupsen/logrus" "github.com/dustin/go-humanize" @@ -20,7 +21,7 @@ func OnInfo(statePath string, clientConfig *config.DockerClient, imageRef string client := dockerclient.New(clientConfig) imageInspector, err := image.NewInspector(client, imageRef) - utils.FailOn(err) + errutils.FailOn(err) if imageInspector.NoImage() { fmt.Println("docker-slim: [info] target image not found -", imageRef) @@ -29,9 +30,9 @@ func OnInfo(statePath string, clientConfig *config.DockerClient, imageRef string log.Info("docker-slim: inspecting 'fat' image metadata...") err = imageInspector.Inspect() - utils.FailOn(err) + errutils.FailOn(err) - _, artifactLocation := utils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID) + _, artifactLocation := fsutils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID) imageInspector.ArtifactLocation = artifactLocation log.Infof("docker-slim: [%v] 'fat' image size => %v (%v)", @@ -41,7 +42,7 @@ func OnInfo(statePath string, clientConfig *config.DockerClient, imageRef string log.Info("docker-slim: processing 'fat' image info...") err = imageInspector.ProcessCollectedData() - utils.FailOn(err) + errutils.FailOn(err) fmt.Println("docker-slim: [info] done.") } diff --git a/master/commands/profile.go b/master/commands/profile.go index 448b1002d8..7bb2f69328 100644 --- a/master/commands/profile.go +++ b/master/commands/profile.go @@ -11,7 +11,9 @@ import ( "github.com/docker-slim/docker-slim/master/inspectors/container" "github.com/docker-slim/docker-slim/master/inspectors/container/probes/http" "github.com/docker-slim/docker-slim/master/inspectors/image" - "github.com/docker-slim/docker-slim/utils" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" + "github.com/docker-slim/docker-slim/pkg/utils/fsutils" + "github.com/docker-slim/docker-slim/pkg/version" log "github.com/Sirupsen/logrus" "github.com/dustin/go-humanize" @@ -37,7 +39,7 @@ func OnProfile(doDebug bool, client := dockerclient.New(clientConfig) imageInspector, err := image.NewInspector(client, imageRef) - utils.FailOn(err) + errutils.FailOn(err) if imageInspector.NoImage() { fmt.Println("docker-slim: [profile] target image not found -", imageRef) @@ -46,9 +48,9 @@ func OnProfile(doDebug bool, log.Info("docker-slim: inspecting 'fat' image metadata...") err = imageInspector.Inspect() - utils.FailOn(err) + errutils.FailOn(err) - localVolumePath, artifactLocation := utils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID) + localVolumePath, artifactLocation := fsutils.PrepareSlimDirs(statePath, imageInspector.ImageInfo.ID) imageInspector.ArtifactLocation = artifactLocation log.Infof("docker-slim: [%v] 'fat' image size => %v (%v)", @@ -58,7 +60,7 @@ func OnProfile(doDebug bool, log.Info("docker-slim: processing 'fat' image info...") err = imageInspector.ProcessCollectedData() - utils.FailOn(err) + errutils.FailOn(err) containerInspector, err := container.NewInspector(client, imageInspector, @@ -69,11 +71,11 @@ func OnProfile(doDebug bool, excludePaths, includePaths, doDebug) - utils.FailOn(err) + errutils.FailOn(err) log.Info("docker-slim: starting instrumented 'fat' container...") err = containerInspector.RunContainer() - utils.FailOn(err) + errutils.FailOn(err) log.Info("docker-slim: watching container monitor...") @@ -83,7 +85,7 @@ func OnProfile(doDebug bool, if doHTTPProbe { probe, err := http.NewCustomProbe(containerInspector, httpProbeCmds) - utils.FailOn(err) + errutils.FailOn(err) probe.Start() continueAfter.ContinueChan = probe.DoneChan() } @@ -106,29 +108,29 @@ func OnProfile(doDebug bool, <-continueAfter.ContinueChan fmt.Println("docker-slim: HTTP probe is done...") default: - utils.Fail("unknown continue-after mode") + errutils.Fail("unknown continue-after mode") } containerInspector.FinishMonitoring() log.Info("docker-slim: shutting down 'fat' container...") err = containerInspector.ShutdownContainer() - utils.WarnOn(err) + errutils.WarnOn(err) if !containerInspector.HasCollectedData() { imageInspector.ShowFatImageDockerInstructions() - fmt.Printf("docker-slim: [profile] no data collected (no minified image generated) - done. (version: %v)\n", utils.CurrentVersion()) + fmt.Printf("docker-slim: [profile] no data collected (no minified image generated) - done. (version: %v)\n", version.Current()) return } log.Info("docker-slim: processing instrumented 'fat' container info...") err = containerInspector.ProcessCollectedData() - utils.FailOn(err) + errutils.FailOn(err) if doRmFileArtifacts { log.Info("docker-slim: removing temporary artifacts...") - err = utils.RemoveArtifacts(artifactLocation) //TODO: remove only the "files" subdirectory - utils.WarnOn(err) + err = fsutils.Remove(artifactLocation) //TODO: remove only the "files" subdirectory + errutils.WarnOn(err) } fmt.Println("docker-slim: [profile] done.") diff --git a/master/docker/dockerclient/client.go b/master/docker/dockerclient/client.go index f373f4cc83..a2fd9506d6 100644 --- a/master/docker/dockerclient/client.go +++ b/master/docker/dockerclient/client.go @@ -7,7 +7,7 @@ import ( "github.com/cloudimmunity/go-dockerclientx" "github.com/docker-slim/docker-slim/master/config" - "github.com/docker-slim/docker-slim/utils" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" log "github.com/Sirupsen/logrus" ) @@ -47,7 +47,7 @@ func New(config *config.DockerClient) *docker.Client { config.VerifyTLS && config.TLSCertPath != "": client, err = newTLSClient(config.Host, config.TLSCertPath, true) - utils.FailOn(err) + errutils.FailOn(err) log.Debug("docker-slim: new Docker client (TLS,verify) [1]") case config.Host != "" && @@ -55,13 +55,13 @@ func New(config *config.DockerClient) *docker.Client { !config.VerifyTLS && config.TLSCertPath != "": client, err = newTLSClient(config.Host, config.TLSCertPath, false) - utils.FailOn(err) + errutils.FailOn(err) log.Debug("docker-slim: new Docker client (TLS,no verify) [2]") case config.Host != "" && !config.UseTLS: client, err = docker.NewClient(config.Host) - utils.FailOn(err) + errutils.FailOn(err) log.Debug("docker-slim: new Docker client [3]") case config.Host == "" && @@ -70,27 +70,27 @@ func New(config *config.DockerClient) *docker.Client { config.Env["DOCKER_CERT_PATH"] != "" && config.Env["DOCKER_HOST"] != "": client, err = newTLSClient(config.Env["DOCKER_HOST"], config.Env["DOCKER_CERT_PATH"], false) - utils.FailOn(err) + errutils.FailOn(err) log.Debug("docker-slim: new Docker client (TLS,no verify) [4]") case config.Env["DOCKER_HOST"] != "": client, err = docker.NewClientFromEnv() - utils.FailOn(err) + errutils.FailOn(err) log.Debug("docker-slim: new Docker client (env) [5]") case config.Host == "" && config.Env["DOCKER_HOST"] == "": config.Host = "unix:///var/run/docker.sock" client, err = docker.NewClient(config.Host) - utils.FailOn(err) + errutils.FailOn(err) log.Debug("docker-slim: new Docker client (default) [6]") default: - utils.Fail("no config for Docker client") + errutils.Fail("no config for Docker client") } if config.Env["DOCKER_HOST"] == "" { if err := os.Setenv("DOCKER_HOST", config.Host); err != nil { - utils.WarnOn(err) + errutils.WarnOn(err) } log.Debug("docker-slim: configured DOCKER_HOST env var") diff --git a/master/inspectors/container/container_inspector.go b/master/inspectors/container/container_inspector.go index 9170aef873..66ee58654e 100644 --- a/master/inspectors/container/container_inspector.go +++ b/master/inspectors/container/container_inspector.go @@ -15,8 +15,9 @@ import ( "github.com/docker-slim/docker-slim/master/security/apparmor" "github.com/docker-slim/docker-slim/master/security/seccomp" "github.com/docker-slim/docker-slim/messages" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" + "github.com/docker-slim/docker-slim/pkg/utils/fsutils" "github.com/docker-slim/docker-slim/report" - "github.com/docker-slim/docker-slim/utils" log "github.com/Sirupsen/logrus" dockerapi "github.com/cloudimmunity/go-dockerclientx" @@ -111,7 +112,7 @@ func NewInspector(client *dockerapi.Client, // RunContainer starts the container inspector instance execution func (i *Inspector) RunContainer() error { artifactsPath := filepath.Join(i.LocalVolumePath, "artifacts") - sensorPath := filepath.Join(utils.ExeDir(), "docker-slim-sensor") + sensorPath := filepath.Join(fsutils.ExeDir(), "docker-slim-sensor") artifactsMountInfo := fmt.Sprintf("%s:/opt/dockerslim/artifacts", artifactsPath) sensorMountInfo := fmt.Sprintf("%s:/opt/dockerslim/bin/sensor:ro", sensorPath) @@ -169,7 +170,7 @@ func (i *Inspector) RunContainer() error { return err } - utils.FailWhen(i.ContainerInfo.NetworkSettings == nil, "docker-slim: error => no network info") + errutils.FailWhen(i.ContainerInfo.NetworkSettings == nil, "docker-slim: error => no network info") log.Debugf("container NetworkSettings.Ports => %#v", i.ContainerInfo.NetworkSettings.Ports) if err = i.initContainerChannels(); err != nil { @@ -244,7 +245,7 @@ func (i *Inspector) ShutdownContainer() error { } } else { - utils.WarnOn(err) + errutils.WarnOn(err) } removeOption := dockerapi.RemoveContainerOptions{ @@ -259,7 +260,7 @@ func (i *Inspector) ShutdownContainer() error { // FinishMonitoring ends the target container monitoring activities func (i *Inspector) FinishMonitoring() { cmdResponse, err := ipc.SendContainerCmd(&messages.StopMonitor{}) - utils.WarnOn(err) + errutils.WarnOn(err) _ = cmdResponse log.Debugf("'stop' response => '%v'", cmdResponse) @@ -275,7 +276,7 @@ func (i *Inspector) FinishMonitoring() { return } - utils.WarnOn(err) + errutils.WarnOn(err) _ = evt log.Debugf("docker-slim: sensor event => '%v'", evt) } @@ -288,7 +289,7 @@ func (i *Inspector) initContainerChannels() error { if os.IsNotExist(err) { os.MkdirAll(ipcLocation, 0777) _, err = os.Stat(ipcLocation) - utils.FailOn(err) + errutils.FailOn(err) } */ @@ -309,7 +310,7 @@ func (i *Inspector) shutdownContainerChannels() { // HasCollectedData returns true if any data was produced monitoring the target container func (i *Inspector) HasCollectedData() bool { - return utils.Exists(filepath.Join(i.ImageInspector.ArtifactLocation, report.DefaultContainerReportFileName)) + return fsutils.Exists(filepath.Join(i.ImageInspector.ArtifactLocation, report.DefaultContainerReportFileName)) } // ProcessCollectedData performs post-processing on the collected container data diff --git a/master/inspectors/image/image_inspector.go b/master/inspectors/image/image_inspector.go index 8fb1d076b2..d0ba0ec6c1 100644 --- a/master/inspectors/image/image_inspector.go +++ b/master/inspectors/image/image_inspector.go @@ -6,7 +6,7 @@ import ( "strings" "github.com/docker-slim/docker-slim/master/docker/dockerfile" - "github.com/docker-slim/docker-slim/utils" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" log "github.com/Sirupsen/logrus" "github.com/cloudimmunity/go-dockerclientx" @@ -110,7 +110,7 @@ func (i *Inspector) ProcessCollectedData() error { } fatImageDockerfileLocation := filepath.Join(i.ArtifactLocation, "Dockerfile.fat") err = dockerfile.SaveDockerfileData(fatImageDockerfileLocation, i.fatImageDockerInstructions) - utils.FailOn(err) + errutils.FailOn(err) return nil } diff --git a/utils/errors.go b/pkg/utils/errutils/errutils.go similarity index 69% rename from utils/errors.go rename to pkg/utils/errutils/errutils.go index a64295bf8c..3533f1dd65 100755 --- a/utils/errors.go +++ b/pkg/utils/errutils/errutils.go @@ -1,8 +1,10 @@ -package utils +package errutils import ( "runtime/debug" + "github.com/docker-slim/docker-slim/pkg/version" + log "github.com/Sirupsen/logrus" ) @@ -10,7 +12,7 @@ import ( func FailOn(err error) { if err != nil { stackData := debug.Stack() - log.WithError(err).WithField("version", CurrentVersion()).WithField("stack", string(stackData)).Fatal("docker-slim: failure") + log.WithError(err).WithField("version", version.Current()).WithField("stack", string(stackData)).Fatal("docker-slim: failure") } } @@ -18,7 +20,7 @@ func FailOn(err error) { func WarnOn(err error) { if err != nil { stackData := debug.Stack() - log.WithError(err).WithField("version", CurrentVersion()).WithField("stack", string(stackData)).Warn("docker-slim: warning") + log.WithError(err).WithField("version", version.Current()).WithField("stack", string(stackData)).Warn("docker-slim: warning") } } @@ -27,7 +29,7 @@ func FailWhen(cond bool, msg string) { if cond { stackData := debug.Stack() log.WithFields(log.Fields{ - "version": CurrentVersion(), + "version": version.Current(), "error": msg, "stack": string(stackData), }).Fatal("docker-slim: failure") @@ -38,7 +40,7 @@ func FailWhen(cond bool, msg string) { func Fail(msg string) { stackData := debug.Stack() log.WithFields(log.Fields{ - "version": CurrentVersion(), + "version": version.Current(), "error": msg, "stack": string(stackData), }).Fatal("docker-slim: failure") diff --git a/utils/dirs.go b/pkg/utils/fsutils/fsutils.go similarity index 95% rename from utils/dirs.go rename to pkg/utils/fsutils/fsutils.go index 6eb683b6f8..79b37e6528 100755 --- a/utils/dirs.go +++ b/pkg/utils/fsutils/fsutils.go @@ -1,4 +1,4 @@ -package utils +package fsutils import ( "errors" @@ -9,6 +9,8 @@ import ( "strings" "syscall" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" + log "github.com/Sirupsen/logrus" "github.com/cloudimmunity/pdiscover" ) @@ -24,6 +26,11 @@ var ( ErrUnsupportedFileObjectType = errors.New("unsupported file object type") ) +// Remove removes the artifacts generated during the current application execution +func Remove(artifactLocation string) error { + return os.RemoveAll(artifactLocation) +} + // Exists returns true if the target file system object exists func Exists(target string) bool { //if _, err := os.Stat(target); os.IsNotExist(err) { @@ -354,14 +361,14 @@ func CopyDir(src, dst string, // ExeDir returns the directory information for the application func ExeDir() string { exePath, err := pdiscover.GetOwnProcPath() - FailOn(err) + errutils.FailOn(err) return filepath.Dir(exePath) } // FileDir returns the directory information for the given file func FileDir(fileName string) string { dirName, err := filepath.Abs(filepath.Dir(fileName)) - FailOn(err) + errutils.FailOn(err) return dirName } @@ -382,12 +389,12 @@ func PrepareSlimDirs(statePath, imageID string) (string, string) { artifactDir, err := os.Stat(artifactLocation) if os.IsNotExist(err) { err = os.MkdirAll(artifactLocation, 0777) - FailOn(err) + errutils.FailOn(err) artifactDir, err = os.Stat(artifactLocation) - FailOn(err) + errutils.FailOn(err) log.Debug("created artifact directory: ", artifactLocation) } - FailWhen(!artifactDir.IsDir(), "artifact location is not a directory") + errutils.FailWhen(!artifactDir.IsDir(), "artifact location is not a directory") return localVolumePath, artifactLocation } diff --git a/utils/version.go b/pkg/version/version.go similarity index 78% rename from utils/version.go rename to pkg/version/version.go index 1aff60a996..e7aaf449d3 100755 --- a/utils/version.go +++ b/pkg/version/version.go @@ -1,4 +1,4 @@ -package utils +package version import ( "fmt" @@ -18,7 +18,7 @@ func init() { currentVersion = fmt.Sprintf("%v|%v|%v|%v|%v (%v)", runtime.GOOS, consts.AppVersionName, appVersionTag, appVersionRev, appVersionTime, runtime.Version()) } -// CurrentVersion returns the current version information -func CurrentVersion() string { +// Current returns the current version information +func Current() string { return currentVersion } diff --git a/scripts/src.fmt.sh b/scripts/src.fmt.sh index 756cb068cc..8297cbc58d 100755 --- a/scripts/src.fmt.sh +++ b/scripts/src.fmt.sh @@ -17,7 +17,7 @@ cd $BDIR/sensor gofmt -l -w -s . cd $BDIR/report gofmt -l -w -s . -cd $BDIR/utils +cd $BDIR/pkg gofmt -l -w -s . cd $BDIR/consts gofmt -l -w -s . diff --git a/scripts/src.inspect.sh b/scripts/src.inspect.sh index d4e71423a3..cd741a4e9e 100755 --- a/scripts/src.inspect.sh +++ b/scripts/src.inspect.sh @@ -25,7 +25,7 @@ golint ./... cd $BDIR/report go tool vet . golint ./... -cd $BDIR/utils +cd $BDIR/pkg go tool vet . golint ./... diff --git a/sensor/monitors/fanotify/monitor.go b/sensor/monitors/fanotify/monitor.go index 44a387f782..a96d3cb377 100644 --- a/sensor/monitors/fanotify/monitor.go +++ b/sensor/monitors/fanotify/monitor.go @@ -7,8 +7,8 @@ import ( "os" "strconv" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" "github.com/docker-slim/docker-slim/report" - "github.com/docker-slim/docker-slim/utils" fanapi "bitbucket.org/madmo/fanotify" log "github.com/Sirupsen/logrus" @@ -29,10 +29,10 @@ func Run(mountPoint string, stopChan chan struct{}) <-chan *report.FanMonitorRep nd, err := fanapi.Initialize(fanapi.FAN_CLASS_NOTIF, os.O_RDONLY) //TODO: need to propagate the FANOTIFY init failure back to the master instead of just crashing the sensor! - utils.FailOn(err) + errutils.FailOn(err) err = nd.Mark(fanapi.FAN_MARK_ADD|fanapi.FAN_MARK_MOUNT, fanapi.FAN_MODIFY|fanapi.FAN_ACCESS|fanapi.FAN_OPEN, -1, mountPoint) - utils.FailOn(err) + errutils.FailOn(err) eventsChan := make(chan *report.FanMonitorReport, 1) @@ -52,7 +52,7 @@ func Run(mountPoint string, stopChan chan struct{}) <-chan *report.FanMonitorRep for { data, err := nd.GetEvent() - utils.FailOn(err) + errutils.FailOn(err) log.Debugf("fanmon: data.Mask =>%x", data.Mask) if (data.Mask & fanapi.FAN_Q_OVERFLOW) == fanapi.FAN_Q_OVERFLOW { @@ -82,7 +82,7 @@ func Run(mountPoint string, stopChan chan struct{}) <-chan *report.FanMonitorRep } path, err := os.Readlink(fmt.Sprintf("/proc/self/fd/%d", data.File.Fd())) - utils.FailOn(err) + errutils.FailOn(err) log.Debug("fanmon: file path =>", path) data.File.Close() diff --git a/sensor/monitors/pevent/monitor.go b/sensor/monitors/pevent/monitor.go index 18952f1ffc..5017e9c649 100644 --- a/sensor/monitors/pevent/monitor.go +++ b/sensor/monitors/pevent/monitor.go @@ -1,8 +1,8 @@ package pevent import ( + "github.com/docker-slim/docker-slim/pkg/utils/errutils" "github.com/docker-slim/docker-slim/report" - "github.com/docker-slim/docker-slim/utils" log "github.com/Sirupsen/logrus" "github.com/cloudimmunity/pdiscover" @@ -17,7 +17,7 @@ func Run(stopChan chan struct{}) <-chan *report.PeMonitorReport { //"connection refused" with boot2docker... watcher, err := pdiscover.NewAllWatcher(pdiscover.PROC_EVENT_ALL) - utils.FailOn(err) + errutils.FailOn(err) reportChan := make(chan *report.PeMonitorReport, 1) @@ -39,7 +39,7 @@ func Run(stopChan chan struct{}) <-chan *report.PeMonitorReport { case <-watcher.Exec: case <-watcher.Exit: case err := <-watcher.Error: - utils.FailOn(err) + errutils.FailOn(err) } } diff --git a/sensor/monitors/ptrace/monitor.go b/sensor/monitors/ptrace/monitor.go index 05cee474f8..7a16786923 100644 --- a/sensor/monitors/ptrace/monitor.go +++ b/sensor/monitors/ptrace/monitor.go @@ -6,9 +6,9 @@ import ( "strconv" "syscall" + "github.com/docker-slim/docker-slim/pkg/utils/errutils" "github.com/docker-slim/docker-slim/report" "github.com/docker-slim/docker-slim/sensor/target" - "github.com/docker-slim/docker-slim/utils" log "github.com/Sirupsen/logrus" "github.com/cloudimmunity/system" @@ -51,7 +51,7 @@ func Run(startChan <-chan int, var err error app, err = target.Start(appName, appArgs, dirName, true) - utils.FailOn(err) + errutils.FailOn(err) targetPid := app.Process.Pid log.Debugf("ptmon: target PID ==> %d", targetPid) diff --git a/utils/artifacts.go b/utils/artifacts.go deleted file mode 100755 index c2cc9aa487..0000000000 --- a/utils/artifacts.go +++ /dev/null @@ -1,10 +0,0 @@ -package utils - -import ( - "os" -) - -// RemoveArtifacts removes the artifacts generated during the current application execution -func RemoveArtifacts(artifactLocation string) error { - return os.RemoveAll(artifactLocation) -}