Skip to content

Commit

Permalink
improve remote access logging
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenmiller committed May 17, 2024
1 parent 062fd2b commit 2805655
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
10 changes: 5 additions & 5 deletions pkg/c8y/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ func (c *Client) SendRequest(ctx context.Context, options RequestOptions) (*Resp
return nil, dryRunErr
}

localLogger.Info(c.hideSensitiveInformationIfActive(fmt.Sprintf("Headers: %v", req.Header)))
localLogger.Info(c.HideSensitiveInformationIfActive(fmt.Sprintf("Headers: %v", req.Header)))

if options.PrepareRequest != nil {
req, err = options.PrepareRequest(req)
Expand Down Expand Up @@ -936,7 +936,7 @@ func (c *Client) SetBasicAuthorization(req *http.Request) {
} else {
headerUsername = c.Username
}
Logger.Infof("Current username: %s", c.hideSensitiveInformationIfActive(headerUsername))
Logger.Infof("Current username: %s", c.HideSensitiveInformationIfActive(headerUsername))
req.SetBasicAuth(headerUsername, c.Password)
}

Expand Down Expand Up @@ -1102,7 +1102,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}, middl
}

if req != nil {
Logger.Infof("Sending request: %s %s", req.Method, c.hideSensitiveInformationIfActive(req.URL.String()))
Logger.Infof("Sending request: %s %s", req.Method, c.HideSensitiveInformationIfActive(req.URL.String()))
}

// Log the body (if applicable)
Expand Down Expand Up @@ -1289,7 +1289,7 @@ func CheckResponse(r *http.Response) error {
return errorResponse
}

func (c *Client) hideSensitiveInformationIfActive(message string) string {
func (c *Client) HideSensitiveInformationIfActive(message string) string {

if strings.ToLower(os.Getenv(EnvVarLoggerHideSensitive)) != "true" {
return message
Expand Down Expand Up @@ -1388,7 +1388,7 @@ func (c *Client) DefaultDryRunHandler(options *RequestOptions, req *http.Request
}
}

Logger.Info(c.hideSensitiveInformationIfActive(message))
Logger.Info(c.HideSensitiveInformationIfActive(message))
}

type CumulocityTokenClaim struct {
Expand Down
25 changes: 11 additions & 14 deletions pkg/remoteaccess/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/gorilla/websocket"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/reubenmiller/go-c8y/pkg/logger"
"github.com/reubenmiller/go-c8y/pkg/proxy"
)

Expand Down Expand Up @@ -40,17 +39,15 @@ type RemoteAccessClient struct {
client *c8y.Client
ctx RemoteAccessOptions
listener net.Listener
log logger.Logger
}

// Create new Remote Access client to allow local clients
// to connect to a device via the Cloud Remote Access feature
func NewRemoteAccessClient(client *c8y.Client, opt RemoteAccessOptions, log logger.Logger) *RemoteAccessClient {
func NewRemoteAccessClient(client *c8y.Client, opt RemoteAccessOptions) *RemoteAccessClient {
return &RemoteAccessClient{
client: client,
ctx: opt,
listener: nil,
log: log,
}
}

Expand All @@ -68,13 +65,13 @@ func (c *RemoteAccessClient) createRemoteAccessConnection() (*websocket.Conn, st
requestHeader.Add("Content-Type", "application/json")

if c.client.Token != "" {
c.log.Debug("Using bearer token")
c8y.Logger.Debug("Using bearer token")
requestHeader.Add("Authorization", "Bearer "+c.client.Token)
} else {
c.log.Debug("Using basic auth")
c8y.Logger.Debug("Using basic auth")
requestHeader.Add("Authorization", c8y.NewBasicAuthString(c.client.GetTenantName(context.Background()), c.client.Username, c.client.Password))
}
c.log.Infof("Connection to Cumulocity IoT CRA: remote=%s, headers=%v", remoteURL, requestHeader)
c8y.Logger.Infof("Connecting to Cumulocity IoT: url=%s, headers=%v", remoteURL, c.client.HideSensitiveInformationIfActive(fmt.Sprintf("%v", requestHeader)))

wsConn, _, err := websocket.DefaultDialer.Dial(remoteURL, requestHeader)
return wsConn, remoteURL, err
Expand All @@ -94,10 +91,10 @@ func (c *RemoteAccessClient) GetListenerAddress() string {
func (c *RemoteAccessClient) ListenServe(r io.ReadCloser, w io.Writer) error {
clientWsConn, remoteURL, err := c.createRemoteAccessConnection()
if err != nil {
c.log.Errorf("DIALER: %v", err.Error())
c8y.Logger.Errorf("DIALER: %v", err.Error())
return err
}
c.log.Infof("Proxying traffic to %v via %v for %v", remoteURL, clientWsConn.RemoteAddr(), "stdio")
c8y.Logger.Infof("Proxying traffic to %v via %v for %v", remoteURL, clientWsConn.RemoteAddr(), "stdio")

// block until finished as stdio mode can not launch multiple instances
proxy.CopyReadWriter(clientWsConn, r, w)
Expand All @@ -113,11 +110,11 @@ func (c *RemoteAccessClient) Listen(addr string) error {
return err
}

c.log.Infof("Creating listener. network=%s, address=%s", network, localAddress)
c8y.Logger.Infof("Creating listener. network=%s, address=%s", network, localAddress)

l, err := net.Listen(network, localAddress)
if err != nil {
c.log.Errorf("%s LISTENER: %v", strings.ToUpper(network), err.Error())
c8y.Logger.Errorf("%s LISTENER: %v", strings.ToUpper(network), err.Error())
return err
}

Expand All @@ -138,16 +135,16 @@ func (c *RemoteAccessClient) Serve() error {
// Listen for an incoming connection.
tcpConn, err := c.listener.Accept()
if err != nil {
c.log.Errorf("ACCEPT: %v", err.Error())
c8y.Logger.Errorf("ACCEPT: %v", err.Error())
}

clientWsConn, remoteURL, err := c.createRemoteAccessConnection()
if err != nil {
c.log.Errorf("DIALER: %v", err.Error())
c8y.Logger.Errorf("DIALER: %v", err.Error())
return err
}
// Handle connections in a new goroutine.
c.log.Infof("Proxying traffic to %v via %v for %v", remoteURL, clientWsConn.RemoteAddr(), tcpConn.RemoteAddr())
c8y.Logger.Infof("Proxying traffic to %v via %v for %v", remoteURL, clientWsConn.RemoteAddr(), tcpConn.RemoteAddr())
go proxy.Copy(clientWsConn, tcpConn)
}
}

0 comments on commit 2805655

Please sign in to comment.