From 87d8388810176bfa932664513bc709dc2f078897 Mon Sep 17 00:00:00 2001 From: rony5394 <143897221+rony5394@users.noreply.github.com> Date: Sat, 18 Apr 2026 16:35:45 +0200 Subject: [PATCH] feat: some safety checks --- docker/docker.go | 24 ++++++++++++++++++++++-- host/exchangeKeys.go | 19 ++++++++++++++----- host/host.go | 2 ++ main.go | 12 ++++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/docker/docker.go b/docker/docker.go index ce66aee..452b166 100644 --- a/docker/docker.go +++ b/docker/docker.go @@ -14,6 +14,7 @@ import ( "net/http" + "github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/network" "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" @@ -149,6 +150,7 @@ func listServices(w http.ResponseWriter, r *http.Request){ } + SERVICES: for _, service := range list{ var settings map[string]string = service.Spec.Labels; @@ -159,10 +161,28 @@ func listServices(w http.ResponseWriter, r *http.Request){ targetVolumes := strings.Split(settings["blazena.volumes"], ","); + var validVolumeNames []string; + for _, mnt := range service.Spec.TaskTemplate.ContainerSpec.Mounts{ + if mnt.Type != mount.TypeVolume { + continue + } + validVolumeNames = append(validVolumeNames, mnt.Source); + } + if !contains(validNodeHostnames, settings["blazena.node"]) { - errMsg := "node with hostname:'"+ settings["blazena.node"] +"' does not exist."; + errMsg := "Node with hostname:'"+ settings["blazena.node"] +"' does not exist."; slog.Warn("Invalid Service Config!", slog.String("serviceId", service.ID), slog.String("errMessage", errMsg)); - continue; + continue SERVICES; + } + + for _, volume := range targetVolumes{ + if contains(validVolumeNames, volume){ + continue; + } + + errMsg := "Volume name '"+ volume + "' is not in the service spec!"; + slog.Warn("Invalid Service Config!", slog.String("serviceId", service.ID), slog.String("errMessage", errMsg)); + continue SERVICES; } services = append(services, aService{ diff --git a/host/exchangeKeys.go b/host/exchangeKeys.go index bc3c5da..12eb1a8 100644 --- a/host/exchangeKeys.go +++ b/host/exchangeKeys.go @@ -1,10 +1,13 @@ package host import ( + "bytes" "encoding/json" "io" + "log/slog" "net/http" - "bytes" + "os" + cfg "github.com/rony5394/blazena/config" ); @@ -19,12 +22,15 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{ if err != nil { panic("Failed to marshal body."+ err.Error()); + slog.Error("Failed to marshal body.", slog.Any("propagatedError", err), slog.String("note", "Input for this marshal operation is that ssh pk. So the kebab is going on!")) + os.Exit(42); } rq, err := http.NewRequest("POST", Config.DockerManagerBaseUrl + "/keys", bytes.NewBuffer(bodyEncoded)); if err != nil{ - panic("Failed to create http request"+ err.Error()); + slog.Error("Failed to create request", slog.Any("propagatedError", err), slog.String("note", "not send just create the object")); + os.Exit(1); } rq.Header.Set("Authorization", "Bearer "+ token); @@ -32,7 +38,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{ rs, err := http.DefaultClient.Do(rq); if err != nil{ - panic("Failed to send http request"+ err.Error()); + slog.Error("Failed to send http request", slog.Any("propagatedError", err)); + os.Exit(1); } defer rs.Body.Close(); @@ -40,7 +47,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{ rsBodyRaw, err := io.ReadAll(rs.Body); if err != nil{ - panic("Failed to read response's body!"+err.Error()); + slog.Error("Failed to read response body!" , slog.Any("propagatedError", err)); + os.Exit(1); } var rsBody struct{ @@ -49,7 +57,8 @@ func exchangeKeys(Config cfg.Config, sshKeyPem string)string{ err = json.Unmarshal(rsBodyRaw, &rsBody); if err != nil{ - panic("Failed to unmarshal rsBodyRaw!"+ err.Error()); + slog.Error("Failed to unmarshal rsBodyRaw!", slog.Any("propagatedError", err)); + os.Exit(1); } diff --git a/host/host.go b/host/host.go index 71f2a15..35a71e9 100644 --- a/host/host.go +++ b/host/host.go @@ -102,6 +102,8 @@ func Run(Config cfg.Config) { slog.Error("Failed to shutdown docker api!"); os.Exit(1); } + + slog.Info("Finished whole backup run."); } func getServices(Config cfg.Config)[]aService{ diff --git a/main.go b/main.go index 11d510e..effb001 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,18 @@ import ( "log/slog" ); +/* + If the exit code is X then it means Y: + + | X | Y | + |----|------------------------------------------------------------------------------------------| + | 0 | Everything should be good, normal exit. | + | 1 | Some common error, but that still mean it is going to crash. | + | 3 | Ask yourself if you are not using dev version in prod. If not then spam the developer. | + | 42 | WHAT THE ACTUAL ***** IS HAPPENING. or assume something is very wrong in the app itself. | + | 69 | [INSERT HERE] | +*/ + func main() { if(len(os.Args) < 2){ panic("Usage: blazena ");