diff --git a/cmd/root.go b/cmd/root.go index 4bb1631..040eb16 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -55,9 +55,12 @@ The full address is kept on $HOME/.mole/.`) // id is a hidden flag used to carry the unique identifier of the instance to // the child process when the `--detached` flag is used. cmd.Flags().StringVarP(&conf.Id, mole.IdFlagName, "", "", "") - cmd.Flags().MarkHidden(mole.IdFlagName) + err := cmd.Flags().MarkHidden(mole.IdFlagName) + if err != nil { + return err + } - err := cmd.MarkFlagRequired("server") + err = cmd.MarkFlagRequired("server") if err != nil { return err } diff --git a/cmd/start_local.go b/cmd/start_local.go index 51e2d71..d76e6dc 100644 --- a/cmd/start_local.go +++ b/cmd/start_local.go @@ -43,9 +43,7 @@ var startLocalCmd = &cobra.Command{ } func init() { - var err error - - err = bindFlags(conf, startLocalCmd) + err := bindFlags(conf, startLocalCmd) if err != nil { log.WithError(err).Error("error parsing command line arguments") os.Exit(1) diff --git a/fsutils/fsutils.go b/fsutils/fsutils.go index 0ce5718..aad38aa 100644 --- a/fsutils/fsutils.go +++ b/fsutils/fsutils.go @@ -141,7 +141,11 @@ func CreatePidFile(id string) (string, error) { return "", fmt.Errorf("could not create pid file for application instance %s: %v", id, err) } defer pf.Close() - pf.WriteString(strconv.Itoa(os.Getpid())) + + _, err = pf.WriteString(strconv.Itoa(os.Getpid())) + if err != nil { + return "", err + } return pfl, nil } diff --git a/mole/mole.go b/mole/mole.go index 0dfffd4..5a4d9c5 100644 --- a/mole/mole.go +++ b/mole/mole.go @@ -255,7 +255,10 @@ func (c *Client) handleSignals() { signal.Notify(c.sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt) sig := <-c.sigs log.Debugf("process signal %s received", sig) - c.Stop() + err := c.Stop() + if err != nil { + log.WithError(err).Error("instance not properly stopped") + } } // Merge overwrites Configuration from the given Alias. @@ -264,9 +267,7 @@ func (c *Client) handleSignals() { // only if they are found on the givenFlags which should contain the name of // all flags given by the user through UI (e.g. CLI). func (c *Configuration) Merge(al *alias.Alias, givenFlags []string) error { - var fl flags - - fl = givenFlags + var fl flags = givenFlags if !fl.lookup("verbose") { c.Verbose = al.Verbose @@ -417,7 +418,14 @@ func startDaemonProcess(instanceConf *DetachedInstance) error { os.Exit(0) } - defer cntxt.Release() + defer func() { + err := cntxt.Release() + if err != nil { + log.WithFields(log.Fields{ + "id": instanceConf.Id, + }).WithError(err).Error("error detaching the mole instance") + } + }() return nil } diff --git a/mole/rpc.go b/mole/rpc.go index 5e5b1c6..5de84ba 100644 --- a/mole/rpc.go +++ b/mole/rpc.go @@ -21,7 +21,10 @@ func ShowRpc(params interface{}) (json.RawMessage, error) { return nil, fmt.Errorf("client configuration could not be found.") } - runtime := cli.Runtime() + runtime, err := cli.Runtime() + if err != nil { + return nil, err + } cj, err := json.Marshal(runtime) if err != nil { diff --git a/mole/runtime.go b/mole/runtime.go index 308bc51..9ad5444 100644 --- a/mole/runtime.go +++ b/mole/runtime.go @@ -68,7 +68,7 @@ func (ir InstancesRuntime) ToToml() (string, error) { return buf.String(), nil } -func (c *Client) Runtime() *Runtime { +func (c *Client) Runtime() (*Runtime, error) { runtime := Runtime(*c.Conf) if c.Tunnel != nil { @@ -76,15 +76,25 @@ func (c *Client) Runtime() *Runtime { destination := &AddressInputList{} for _, channel := range c.Tunnel.Channels() { - source.Set(channel.Source) - destination.Set(channel.Destination) + var err error + + err = source.Set(channel.Source) + if err != nil { + return nil, err + } + + err = destination.Set(channel.Destination) + if err != nil { + return nil, err + } + } runtime.Source = *source runtime.Destination = *destination } - return &runtime + return &runtime, nil } // Running checks if an instance of mole is running on the system. diff --git a/rpc/rpc.go b/rpc/rpc.go index 43a0b1c..a17e15a 100644 --- a/rpc/rpc.go +++ b/rpc/rpc.go @@ -65,13 +65,33 @@ func (h *Handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2 log.Errorf("rpc request method %s not supported", req.Method) if !req.Notif { + var err error + resp := &jsonrpc2.Response{} - resp.SetResult(jsonrpc2.Error{ + err = resp.SetResult(jsonrpc2.Error{ Code: jsonrpc2.CodeMethodNotFound, Message: fmt.Sprintf("method %s not found", req.Method), }) + if err != nil { + log.WithFields(log.Fields{ + "notification": req.Notif, + "method": req.Method, + "id": req.ID, + }).WithError(err).Error("could not send rpc error response") + + return + } - sendResponse(ctx, conn, req, resp) + err = sendResponse(ctx, conn, req, resp) + if err != nil { + log.WithFields(log.Fields{ + "notification": req.Notif, + "method": req.Method, + "id": req.ID, + }).WithError(err).Error("could not send rpc response") + + return + } } return @@ -86,13 +106,33 @@ func (h *Handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2 }).WithError(err).Warn("error executing rpc method.") if !req.Notif { + var err error resp := &jsonrpc2.Response{} - resp.SetResult(jsonrpc2.Error{ + + err = resp.SetResult(jsonrpc2.Error{ Code: jsonrpc2.CodeInternalError, Message: fmt.Sprintf("error executing rpc method %s", req.Method), }) + if err != nil { + log.WithFields(log.Fields{ + "notification": req.Notif, + "method": req.Method, + "id": req.ID, + }).WithError(err).Error("could not send rpc error response") + + return + } - sendResponse(ctx, conn, req, resp) + err = sendResponse(ctx, conn, req, resp) + if err != nil { + log.WithFields(log.Fields{ + "notification": req.Notif, + "method": req.Method, + "id": req.ID, + }).WithError(err).Error("could not send rpc response") + + return + } } return @@ -108,13 +148,33 @@ func (h *Handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2 }).WithError(err).Warn("error executing rpc method.") if !req.Notif { + var err error + resp := &jsonrpc2.Response{} - resp.SetResult(jsonrpc2.Error{ + err = resp.SetResult(jsonrpc2.Error{ Code: jsonrpc2.CodeInternalError, Message: fmt.Sprintf("error executing rpc method %s", req.Method), }) + if err != nil { + log.WithFields(log.Fields{ + "notification": req.Notif, + "method": req.Method, + "id": req.ID, + }).WithError(err).Error("could not send rpc error response") + + return + } - sendResponse(ctx, conn, req, resp) + err = sendResponse(ctx, conn, req, resp) + if err != nil { + log.WithFields(log.Fields{ + "notification": req.Notif, + "method": req.Method, + "id": req.ID, + }).WithError(err).Error("could not send rpc response") + + return + } } return @@ -123,7 +183,16 @@ func (h *Handler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2 if !req.Notif { resp := &jsonrpc2.Response{ID: req.ID, Result: &rm} - sendResponse(ctx, conn, req, resp) + err = sendResponse(ctx, conn, req, resp) + if err != nil { + log.WithFields(log.Fields{ + "notification": req.Notif, + "method": req.Method, + "id": req.ID, + }).WithError(err).Error("could not send rpc response") + + return + } } } @@ -137,12 +206,6 @@ type Method func(params interface{}) (json.RawMessage, error) func sendResponse(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request, resp *jsonrpc2.Response) error { if err := conn.SendResponse(ctx, resp); err != nil { - log.WithFields(log.Fields{ - "notification": req.Notif, - "method": req.Method, - "id": req.ID, - }).WithError(err).Warn("error sending rpc response.") - return err } diff --git a/tunnel/key.go b/tunnel/key.go index a70a650..179deff 100644 --- a/tunnel/key.go +++ b/tunnel/key.go @@ -49,7 +49,7 @@ func (k PemKey) IsEncrypted() (bool, error) { return false, err } - return x509.IsEncryptedPEMBlock(p), nil + return x509.IsEncryptedPEMBlock(p), nil //nolint } // Parse translates a pem key to a signer to create signatures that verify