Skip to content

Commit

Permalink
Merge pull request #115 from steffenfritz/113-change-create-a-verbose…
Browse files Browse the repository at this point in the history
…-debug-mode

Added debug functions with flag -D
  • Loading branch information
steffenfritz authored Nov 5, 2024
2 parents 6922bb4 + 7ec1e0c commit 229bd1d
Show file tree
Hide file tree
Showing 211 changed files with 24,711 additions and 4 deletions.
31 changes: 31 additions & 0 deletions cmd/ftrove/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
projectname := flag.StringP("project", "p", "", "A name for the project or scan session.")
resumeuuid := flag.StringP("resume", "r", "", "Resume an aborted session. Provide the session uuid.")
timezone := flag.StringP("timezone", "z", "", "Set the time zone to a region in which the timestamps of files are to be translated. If this flag is not set, the local time zone is used. Example: Europe/Berlin")
debug := flag.BoolP("debug", "D", false, "Enable debug mode. This creates the diagnostic file debug_ftrove.")

// updateFT := flag.BoolP("update-all", "u", false, "Update FileTrove, siegfried and NSRL.")
printversion := flag.BoolP("version", "v", false, "Show version and build.")
Expand All @@ -63,6 +64,32 @@ func main() {

starttime := time.Now()

var fddebug os.File
var err error

if *debug {
fddebug, err = ft.DebugCreateDebugPackage()
if err != nil {
logger.Error("Could not create debug package:", slog.String("error", err.Error()))
os.Exit(1)
}
defer fddebug.Close()

err = ft.DebugHostinformation(fddebug)
if err != nil {
logger.Error("Could not create debug hostinformation:", slog.String("error", err.Error()))
}
err = ft.DebugCheckInstalled(fddebug)
if err != nil {
logger.Error("Could not check installed version:", slog.String("error", err.Error()))
}
err = ft.DebugWriteFlags(fddebug, flag.Args())
if err != nil {
logger.Error("Could not write flags:", slog.String("error", err.Error()))
}

}

// Init new session with flags
var sessionmd ft.SessionMD
sessionmd.Starttime = starttime.Format(time.RFC3339)
Expand Down Expand Up @@ -356,6 +383,9 @@ func main() {

// Create file list
filelist, dirlist, err := ft.CreateFileList(*inDir)
if *debug {
ft.DebugWriteFileList(fddebug, filelist, dirlist)
}
if err != nil {
logger.Error("An error occurred during the creation of the file list.", slog.String("error", err.Error()))
err = ftdb.Close()
Expand Down Expand Up @@ -676,4 +706,5 @@ func main() {
if err != nil {
_ = fmt.Errorf("ERROR: Could not close error log file: " + err.Error())
}

}
105 changes: 105 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package filetrove

import (
info "github.com/elastic/go-sysinfo"
"os"
"runtime"
"strconv"
)

// DebugCreateDebugPackage creates the file for compiling information into a debug package
func DebugCreateDebugPackage() (os.File, error) {
file, err := os.OpenFile("debug_ftrove", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return os.File{}, err
}
// We don't close the file handle here, this must be done after all information are gathered
file.WriteString("BEGIN FILETROVE DEBUG\n")

return *file, err
}

// DebugCheckInstalled checks if FileTrove is installed by checking if the database exists
func DebugCheckInstalled(fd os.File) error {
filePath := "db/filetrove.db"
_, err := fd.WriteString("\nBEGIN INSTALLED\n")

_, err = os.Stat(filePath)
if err == nil {
fd.WriteString("FileTrove installed: True")
fd.WriteString("\nEND INSTALLED\n")
return nil
}
if os.IsNotExist(err) {
fd.WriteString("FileTrove installed: False")
fd.WriteString("\nEND INSTALLED\n")
return nil
}

return err
}

// DebugHostinformation writes host stats and returns on error
func DebugHostinformation(fd os.File) error {
_, err := fd.WriteString("\nBEGIN HOST INFORMATION\n")
infoOnHost, err := info.Host()

if err != nil {
return err
}

fd.WriteString("OSName: " + infoOnHost.Info().OS.Name + "\n")
fd.WriteString("OSVersion: " + infoOnHost.Info().OS.Version + "\n")
fd.WriteString("Platform: " + infoOnHost.Info().OS.Platform + "\n")
mem, err := infoOnHost.Memory()
if err != nil {
fd.WriteString("Mem: " + err.Error())
return err
}
fd.WriteString("Memory Total: " + strconv.Itoa(int(mem.Total)) + "\n")
fd.WriteString("Memory Used: " + strconv.Itoa(int(mem.Used)) + "\n")
fd.WriteString("Go Runtime: " + runtime.Version() + "\n")

fd.WriteString("END HOST INFORMATION\n")

return nil
}

// DebugWriteFlags takes parsed flags from main and writes them to the diag file
func DebugWriteFlags(fd os.File, args []string) error {
_, err := fd.WriteString("\nBEGIN FLAGS\n")
if err != nil {
return err
}
for k, v := range args {
_, err = fd.WriteString(strconv.Itoa(k) + ": " + v + "\n")
}

fd.WriteString("END FLAGS\n")

return err
}

func DebugWriteFileList(fd os.File, filelist []string, dirlist []string) error {
fd.WriteString("\nBEGIN FILELIST\n")
for _, file := range filelist {
_, err := fd.WriteString(file + "\n")
if err != nil {
return err
}
}
fd.WriteString("END FILELIST\n")

fd.WriteString("\nBEGIN DIRLIST\n")
for _, dir := range dirlist {
_, err := fd.WriteString(dir + "\n")
if err != nil {
return err
}
}
fd.WriteString("END DIRLIST\n")

return nil
}

// DebugCollectLogs collects log files
49 changes: 49 additions & 0 deletions debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package filetrove

import (
"os"
"reflect"
"testing"
)

func TestCreateDebugPackage(t *testing.T) {
tests := []struct {
name string
want os.File
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := DebugCreateDebugPackage()
if (err != nil) != tt.wantErr {
t.Errorf("DebugCreateDebugPackage() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DebugCreateDebugPackage() got = %v, want %v", got, tt.want)
}
})
}
}

func TestHostinformation(t *testing.T) {
type args struct {
fd os.File
}
tests := []struct {
name string
args args
wantErr bool
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DebugHostinformation(tt.args.fd); (err != nil) != tt.wantErr {
t.Errorf("DebugHostinformation() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ toolchain go1.23.1
require (
github.com/VirusTotal/yara-x/go v0.9.0
github.com/djherbis/times v1.6.0
github.com/elastic/go-sysinfo v1.15.0
github.com/google/uuid v1.6.0
github.com/mattn/go-sqlite3 v1.14.23
github.com/richardlehane/siegfried v1.11.1
Expand All @@ -18,7 +19,10 @@ require (
)

require (
github.com/elastic/go-windows v1.0.0 // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/richardlehane/characterize v1.0.0 // indirect
github.com/richardlehane/match v1.0.5 // indirect
github.com/richardlehane/mscfb v1.0.4 // indirect
Expand All @@ -32,4 +36,5 @@ require (
golang.org/x/term v0.24.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
howett.net/plist v0.0.0-20181124034731-591f970eefbb // indirect
)
24 changes: 20 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,31 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/djherbis/times v1.6.0 h1:w2ctJ92J8fBvWPxugmXIv7Nz7Q3iDMKNx9v5ocVH20c=
github.com/djherbis/times v1.6.0/go.mod h1:gOHeRAz2h+VJNZ5Gmc/o7iD9k4wW7NMVqieYCY99oc0=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/elastic/go-sysinfo v1.15.0 h1:54pRFlAYUlVNQ2HbXzLVZlV+fxS7Eax49stzg95M4Xw=
github.com/elastic/go-sysinfo v1.15.0/go.mod h1:jPSuTgXG+dhhh0GKIyI2Cso+w5lPJ5PvVqKlL8LV/Hk=
github.com/elastic/go-windows v1.0.0 h1:qLURgZFkkrYyTTkvYpsZIgf83AUsdIHfvlJaqaZ7aSY=
github.com/elastic/go-windows v1.0.0/go.mod h1:TsU0Nrp7/y3+VwE82FoZF8gC/XFg/Elz6CcloAxnPgU=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/k0kubun/go-ansi v0.0.0-20180517002512-3bf9e2903213/go.mod h1:vNUNkEQ1e29fT/6vq2aBdFsgNPmy8qMdSay1npru+Sw=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.23 h1:gbShiuAP1W5j9UOksQ06aiiqPMxYecovVGwmTxWtuw0=
github.com/mattn/go-sqlite3 v1.14.23/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/richardlehane/characterize v1.0.0 h1:2MMnKFqYd+hsKpQrPkc5JjbcIzVBIfvSoaMd563GOj0=
github.com/richardlehane/characterize v1.0.0/go.mod h1:9mhxzxtWkXoLQpkg+gt7ioK6//+3hrsv3VHkbj8kbuQ=
github.com/richardlehane/match v1.0.5 h1:+tuXp28xaIPsvKbhHyuivce9qMEfE8nP9d0wSxJef9o=
Expand Down Expand Up @@ -65,9 +78,12 @@ golang.org/x/term v0.24.0/go.mod h1:lOBK/LVxemqiMij05LGJ0tzNr8xlmwBRJ81PX6wVLH8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
howett.net/plist v0.0.0-20181124034731-591f970eefbb h1:jhnBjNi9UFpfpl8YZhA9CrOqpnJdvzuiHsl/dnxl11M=
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
27 changes: 27 additions & 0 deletions vendor/github.com/elastic/go-sysinfo/.editorconfig

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/elastic/go-sysinfo/.gitattributes

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/elastic/go-sysinfo/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/elastic/go-sysinfo/.golangci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions vendor/github.com/elastic/go-sysinfo/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 229bd1d

Please sign in to comment.