-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from bechampion/flightchecks
NOJ - Initial flight checks
- Loading branch information
Showing
10 changed files
with
198 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
systemd "github.com/bechampion/gohip/systemd" | ||
) | ||
|
||
func RunPreflightChecks() error { | ||
clamavError := systemd.DefaultDbAgeCheck() | ||
|
||
return errors.Join(clamavError) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package systemd | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"strings" | ||
"time" | ||
) | ||
|
||
var clamavDbFile ClamavDbFile | ||
|
||
func init() { | ||
clamavDbFile = ClamavDbFile{path: "/var/lib/clamav/daily.cld"} | ||
} | ||
|
||
type ClamavDbFile struct { | ||
path string | ||
} | ||
|
||
func DefaultDbAgeCheck() error { | ||
details, clamavError := GetClamConfDetails() | ||
|
||
if clamavError != nil { | ||
return clamavError | ||
} | ||
|
||
return DbConfigAgeCheck(details) | ||
} | ||
|
||
func DbFileAgeCheck(clamavDbFile ClamavDbFile) error { | ||
hoursInWeek := 24 * 7 | ||
|
||
fi, err := os.Stat(clamavDbFile.path) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
mtime := fi.ModTime() | ||
|
||
hoursSince := int(time.Since(mtime).Hours()) | ||
|
||
if hoursSince > hoursInWeek { | ||
return errors.New(fmt.Sprintf("virus definition is too old: %s is more than %d hours old (> 7 days)", clamavDbFile.path, hoursSince)) | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
func DbConfigAgeCheck(details ClamConfDetails) error { | ||
weekAgo := time.Now().Add(-time.Hour * 24 * 7) | ||
|
||
tooOld := details.DailyCld.Before(weekAgo) | ||
|
||
if tooOld { | ||
return errors.New(fmt.Sprintf("virus definition is more than 7 days old: %s", details.DailyCld.String())) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type ClamConfDetails struct { | ||
version string | ||
sigs string | ||
DailyCld time.Time | ||
} | ||
|
||
func GetClamConfDetails() (ClamConfDetails, error) { | ||
cmd := exec.Command("clamconf") | ||
var out bytes.Buffer | ||
cmd.Stdout = &out | ||
err := cmd.Run() | ||
if err != nil { | ||
return ClamConfDetails{}, errors.New(fmt.Sprintf("%v", err)) | ||
} | ||
|
||
const layout = "Mon Jan 02 15:04:05 2006" | ||
lines := strings.Split(out.String(), "\n") | ||
re := regexp.MustCompile(`^daily.c[l|v]d: version (.*), sigs: (.*), built on (.*)`) | ||
|
||
for i := range lines { | ||
line := lines[i] | ||
finds := re.FindStringSubmatch(line) | ||
|
||
if len(finds) > 0 { | ||
cd := ClamConfDetails{} | ||
cd.DailyCld, _ = time.Parse(layout, finds[3]) | ||
cd.version = finds[1] | ||
cd.sigs = finds[2] | ||
return cd, nil | ||
} | ||
} | ||
|
||
return ClamConfDetails{}, errors.New(fmt.Sprintf("Could not determine timestamp for daily.cld in clamconf output")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package systemd | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestFileIs7DaysOld(t *testing.T) { | ||
clamavError := fileErrorCheck(t, time.Now().Add(-time.Hour*24*7)) | ||
|
||
if clamavError != nil { | ||
t.Errorf("should not be old enough: \n\t%v", clamavError) | ||
} | ||
} | ||
|
||
func TestDbConfigIsAlmost7DaysOld(t *testing.T) { | ||
weekAgo := time.Now().Add(-time.Hour*24*7 + time.Hour) | ||
details := ClamConfDetails{version: "1.0", sigs: "2.0", DailyCld: weekAgo} | ||
|
||
err := DbConfigAgeCheck(details) | ||
|
||
if err != nil { | ||
t.Errorf("DailyCld should have been recent enough: \n%v", err) | ||
} | ||
} | ||
|
||
func TestDbConfigIsOver7DaysOld(t *testing.T) { | ||
weekAgo := time.Now().Add(-time.Hour*24*7 - time.Hour) | ||
details := ClamConfDetails{version: "1.0", sigs: "2.0", DailyCld: weekAgo} | ||
|
||
err := DbConfigAgeCheck(details) | ||
|
||
if err == nil { | ||
t.Errorf("DailyCld should have been too old") | ||
} | ||
} | ||
|
||
func TestFileIsAlmost7DaysOld(t *testing.T) { | ||
clamavError := fileErrorCheck(t, time.Now().Add(-time.Hour*24*7+time.Hour)) | ||
|
||
if clamavError != nil { | ||
t.Errorf("should not be old enough: \n\t%v", clamavError) | ||
} | ||
} | ||
|
||
func TestFileIsMoreThan7DaysOld(t *testing.T) { | ||
clamavError := fileErrorCheck(t, time.Now().Add(-time.Hour*24*7-time.Hour)) | ||
|
||
if clamavError == nil { | ||
t.Errorf("should not be old enough: \n\t%v", clamavError) | ||
} | ||
} | ||
|
||
func fileErrorCheck(t *testing.T, when time.Time) error { | ||
nowFilePath := "/tmp/now" | ||
|
||
t.Cleanup(func() { | ||
os.Remove(nowFilePath) | ||
}) | ||
|
||
_, err := os.Create(nowFilePath) | ||
if err != nil { | ||
t.Errorf("%v", err) | ||
} | ||
|
||
os.Chtimes(nowFilePath, when, when) | ||
|
||
clamavDbFile := ClamavDbFile{path: nowFilePath} | ||
|
||
return DbFileAgeCheck(clamavDbFile) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters