From b93d36dacdec5846565f537df088348e8535c280 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sun, 28 Apr 2024 10:25:41 +0100 Subject: [PATCH] Tidying error handling and adding some fluff. --- Dockerfile | 1 + cif/check.go | 4 ++-- cif/update.go | 1 + helpers/config_loader.go | 2 +- main.go | 13 ++++++++++++- nrod/streams.go | 8 ++++++-- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index da62302..615835a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,4 +5,5 @@ RUN go build . FROM scratch COPY --from=builder /source/timetable-mgr /bin/timetable-mgr +USER 20400 CMD [ "/bin/timetable-mgr" ] \ No newline at end of file diff --git a/cif/check.go b/cif/check.go index 6c4123a..3179818 100644 --- a/cif/check.go +++ b/cif/check.go @@ -32,7 +32,7 @@ func CheckCif(cfg *helpers.Configuration) { log.Info("Full CIF download required") err := runCifFullDownload(cfg) if err != nil { - log.Error("Unable to run full CIF Update", zap.Error(err)) + log.Warn("Unable to run full CIF Update", zap.Error(err)) } return } @@ -61,6 +61,6 @@ func CheckCif(cfg *helpers.Configuration) { log.Info("CIF Update required", zap.Any("days to update", daysToUpdate)) err = runCifUpdateDownload(cfg, metadata, daysToUpdate) if err != nil { - log.Error("Daily CIF update failed", zap.Error(err)) + log.Warn("Daily CIF update failed", zap.Error(err)) } } diff --git a/cif/update.go b/cif/update.go index b306afd..a49af72 100644 --- a/cif/update.go +++ b/cif/update.go @@ -24,6 +24,7 @@ func runCifFullDownload(cfg *helpers.Configuration) error { dataStream, err := nrod.NrodStream(url, cfg) if err != nil { log.Error("Error downloading CIF data", zap.Error(err)) + return err } // Parse CIF file diff --git a/helpers/config_loader.go b/helpers/config_loader.go index 11ae40c..54d4f7a 100644 --- a/helpers/config_loader.go +++ b/helpers/config_loader.go @@ -119,7 +119,7 @@ func (c *Configuration) setConfigValue(key, value string) { // Provides a method to print the configuration struct. Only when the DEBUG env is set to true func (c *Configuration) PrintConfig() { - if os.Getenv("DEBUG") == "true" { + if os.Getenv("debug") == "true" { fmt.Println("Configuration:") fmt.Println("VstpOn: ", c.VstpOn) fmt.Println("NrodUser: ", c.NrodUser) diff --git a/main.go b/main.go index f2f09f7..e02c655 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/signal" + "os/user" "syscall" "time" _ "time/tzdata" @@ -31,7 +32,9 @@ func init() { printStartupBanner() fmt.Printf("%sVersion %s \n\n%s", bold+blue, helpers.Version, reset) - //checkRunAsRoot() + // Exits is being run as root + // not necessary and not secure + checkRunAsRoot() } func main() { @@ -126,6 +129,14 @@ func printStartupBanner() { func checkRunAsRoot() { uid := os.Getuid() if uid != 0 { + currUser, err := user.Current() + var msg string + if err != nil { + msg = "Unable to determine which user is running the application, but is not being run by root" + } else { + msg = fmt.Sprintf("Running as user: %s, %s", currUser.Uid, currUser.Username) + } + fmt.Println(blue + msg + reset) return } diff --git a/nrod/streams.go b/nrod/streams.go index abeecab..e93c709 100644 --- a/nrod/streams.go +++ b/nrod/streams.go @@ -2,6 +2,7 @@ package nrod import ( "compress/gzip" + "errors" "fmt" "io" "net/http" @@ -22,7 +23,6 @@ func NrodStream(url string, cfg *helpers.Configuration) (io.ReadCloser, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { - log.Error("Error creating HTTP Request", zap.Error(err)) return nil, err } @@ -34,9 +34,13 @@ func NrodStream(url string, cfg *helpers.Configuration) (io.ReadCloser, error) { return nil, err } + if resp == nil { + err = errors.New("http response error - response = nil") + return nil, err + } + if resp.StatusCode != http.StatusOK { err := fmt.Errorf("unexpected status code: %d", resp.StatusCode) - log.Error("Non-successful status code", zap.Error(err)) return nil, err }