Skip to content

Commit

Permalink
chore(ssh): improve logs for SSH web connection
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybarreto authored and gustavosbarreto committed Dec 2, 2024
1 parent 7857c3b commit f7667b5
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions ssh/web/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,31 @@ func getAuth(creds *Credentials, magicKey *rsa.PrivateKey) ([]ssh.AuthMethod, er
}

func newSession(ctx context.Context, cache cache.Cache, conn *Conn, creds *Credentials, dim Dimensions, info Info) error {
log.WithFields(log.Fields{
logger := log.WithFields(log.Fields{
"user": creds.Username,
"device": creds.Device,
"cols": dim.Cols,
"rows": dim.Rows,
}).Info("handling web client request started")
"ip": info.IP,
})

defer log.WithFields(log.Fields{
"user": creds.Username,
"device": creds.Device,
"cols": dim.Cols,
"rows": dim.Rows,
}).Info("handling web client request end")
logger.Info("handling web client request started")

defer logger.Info("handling web client request end")

uuid := uuid.Generate()

user := fmt.Sprintf("%s@%s", creds.Username, uuid)
auth, err := getAuth(creds, magickey.GetRerefence())
if err != nil {
logger.WithError(err).Debug("failed to get the credentials")

return ErrGetAuth
}

if err := cache.Set(ctx, "web-ip/"+user, fmt.Sprintf("%s:%s", creds.Device, info.IP), 1*time.Minute); err != nil {
logger.WithError(err).Debug("failed to set the session IP on the cache")

return err
}

Expand All @@ -140,30 +142,40 @@ func newSession(ctx context.Context, cache cache.Cache, conn *Conn, creds *Crede
return e
}

logger.WithError(err).Debug("failed to receive the connection banner")

return ErrAuthentication
}

defer connection.Close()

agent, err := connection.NewSession()
if err != nil {
logger.WithError(err).Debug("failed to create a new session")

return ErrSession
}

defer agent.Close()

stdin, err := agent.StdinPipe()
if err != nil {
logger.WithError(err).Debug("failed to create the stdin pipe")

return err
}

stdout, err := agent.StdoutPipe()
if err != nil {
logger.WithError(err).Debug("failed to create the stdout pipe")

return err
}

stderr, err := agent.StderrPipe()
if err != nil {
logger.WithError(err).Debug("failed to create the stderr pipe")

return err
}

Expand All @@ -172,10 +184,14 @@ func newSession(ctx context.Context, cache cache.Cache, conn *Conn, creds *Crede
ssh.TTY_OP_ISPEED: 14400,
ssh.TTY_OP_OSPEED: 14400,
}); err != nil {
logger.WithError(err).Debug("failed to request the pty on session")

return ErrPty
}

if err := agent.Shell(); err != nil {
logger.WithError(err).Debug("failed to request the shell on session")

return ErrShell
}

Expand All @@ -190,12 +206,7 @@ func newSession(ctx context.Context, cache cache.Cache, conn *Conn, creds *Crede
return
}

log.WithFields(
log.Fields{
"user": creds.Username,
"device": creds.Device,
"ip": info.IP,
}).WithError(err).Error("failed to read the message from the client")
logger.WithError(err).Error("failed to read the message from the client")

return
}
Expand All @@ -205,23 +216,15 @@ func newSession(ctx context.Context, cache cache.Cache, conn *Conn, creds *Crede
buffer := message.Data.([]byte)

if _, err := stdin.Write(buffer); err != nil {
log.WithError(err).Error("failed to write the message data on the SSH session")
logger.WithError(err).Error("failed to write the message data on the SSH session")

return
}
case messageKindResize:
dim := message.Data.(Dimensions)

if err := agent.WindowChange(dim.Rows, dim.Cols); err != nil {
log.WithFields(
log.Fields{
"user": creds.Username,
"device": creds.Device,
"ip": info.IP,
"cols": dim.Cols,
"rows": dim.Rows,
},
).WithError(err).Error("failed to change the size of window for terminal session")
logger.WithError(err).Error("failed to change the size of window for terminal session")

return
}
Expand All @@ -233,7 +236,7 @@ func newSession(ctx context.Context, cache cache.Cache, conn *Conn, creds *Crede
go io.Copy(conn, stderr) //nolint:errcheck

if err := agent.Wait(); err != nil {
log.WithError(err).Warning("client remote command returned a error")
logger.WithError(err).Warning("client remote command returned a error")
}

return nil
Expand Down

0 comments on commit f7667b5

Please sign in to comment.