nanovms / ops

ops - build and run nanos unikernels
https://ops.city
MIT License
1.27k stars 132 forks source link

Go App Served via SSH #1585

Closed bobhenkel closed 6 months ago

bobhenkel commented 6 months ago

I'm starting to experiment where the edges of an unikernel environment are. I didn't expect this to work and it doesn't. I wrote a simple ssh server in go that in theory serves out a simple app, it doesn't allow the user to execute any commands, only to hit the ssh server and be served content much like a http hello world REST API. The app listens for ssh connections on port 23234. When it's running you can connect to it with ssh localhost -p23234 and it will create a PTY and print some text back.

I'm curious if it's possible with a nanos instance? If not great no big deal, I'll move onto using nanos in a more typical manner. If it can work what's needed? Thanks!

When NOT ran in a nanos unikernel this is the output when connecting to the the ssh server:

Hello, world!
Term: xterm-256color
PTY: /dev/pts/5
Connection to localhost closed.

When ran in a nanos unikernel I get this error when I deploy it to a google cloud instance:

PTY allocation request failed on channel 0
Requires an active PTY
Connection to 104.999.999.63 closed.

Here's the code:

package main

import (
        "context"
        "errors"
        "net"
        "os"
        "os/signal"
        "syscall"
        "time"

        "github.com/charmbracelet/log"
        "github.com/charmbracelet/ssh"
        "github.com/charmbracelet/wish"
        "github.com/charmbracelet/wish/activeterm"
        "github.com/charmbracelet/wish/logging"
)

const (
        host = ""
        port = "23234"
)

func main() {
        srv, err := wish.NewServer(
                wish.WithAddress(net.JoinHostPort(host, port)),
        //      wish.WithHostKeyPath(".ssh/id_ed25519"),

                // Wish can allocate a PTY per user session.
                ssh.AllocatePty(),

                wish.WithMiddleware(
                        func(next ssh.Handler) ssh.Handler {
                                return func(sess ssh.Session) {
                                        pty, _, _ := sess.Pty()
                                        wish.Printf(sess, "Hello, world!\r\n")
                                        wish.Printf(sess, "Term: "+pty.Term+"\r\n")
                                        wish.Printf(sess, "PTY: "+pty.Slave.Name()+"\r\n")
                                        next(sess)
                                }
                        },

                        activeterm.Middleware(),
                        logging.Middleware(),
                ),
        )
        if err != nil {
                log.Error("Could not start server", "error", err)
        }

        done := make(chan os.Signal, 1)
        signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

        go func() {
                log.Info("Starting SSH server", "host", host, "port", port)
                if err = srv.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
                        log.Error("Could not start server", "error", err)
                        done <- nil
                }
        }()

        <-done

        ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
        defer func() { cancel() }()
        log.Info("Stopping SSH server")
        if err := srv.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
                log.Error("Could not stop server", "error", err)
        }
}
rinor commented 6 months ago

it doesn't allow the user to execute any commands, only to hit the ssh server and be served content much like a http hello world REST API

you don't need a "real" PTY then.

Can you try and check if the following changes work for you?

  1. comment/delete ssh.AllocatePty(), or change it to ssh.EmulatePty(),
  2. remove wish.Printf(sess, "PTY: "+pty.Slave.Name()+"\r\n") since there are no fd allocated or change it to smth like if !pty.IsZero() { wish.Printf(sess, "PTY: "+pty.Slave.Name()+"\r\n") }
bobhenkel commented 6 months ago

Thanks for your time, it's appreciated! Seems so obvious now that someone showed me the solution 😄. For anyone else that is trying to use the Charm modules to make ssh apps like the Wish module here's a more interesting app from the Wish module examples that just works out of the box. I see myself leaning into Nano unikernels more for some of my services and apps.

package main

// An example Bubble Tea server. This will put an ssh session into alt screen
// and continually print up to date terminal information.

import (
    "context"
    "errors"
    "fmt"
    "net"
    "os"
    "os/signal"
    "syscall"
    "time"

    tea "github.com/charmbracelet/bubbletea"
    "github.com/charmbracelet/lipgloss"
    "github.com/charmbracelet/log"
    "github.com/charmbracelet/ssh"
    "github.com/charmbracelet/wish"
    "github.com/charmbracelet/wish/activeterm"
    "github.com/charmbracelet/wish/bubbletea"
    "github.com/charmbracelet/wish/logging"
)

const (
    host = ""
    port = "23234"
)

func main() {
    s, err := wish.NewServer(
        wish.WithAddress(net.JoinHostPort(host, port)),
        wish.WithMiddleware(
            bubbletea.Middleware(teaHandler),
            activeterm.Middleware(), // Bubble Tea apps usually require a PTY.
            logging.Middleware(),
        ),
    )
    if err != nil {
        log.Error("Could not start server", "error", err)
    }

    done := make(chan os.Signal, 1)
    signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
    log.Info("Starting SSH server", "host", host, "port", port)
    go func() {
        if err = s.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
            log.Error("Could not start server", "error", err)
            done <- nil
        }
    }()

    <-done
    log.Info("Stopping SSH server")
    ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
    defer func() { cancel() }()
    if err := s.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) {
        log.Error("Could not stop server", "error", err)
    }
}

// You can wire any Bubble Tea model up to the middleware with a function that
// handles the incoming ssh.Session. Here we just grab the terminal info and
// pass it to the new model. You can also return tea.ProgramOptions (such as
// tea.WithAltScreen) on a session by session basis.
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) {
    // This should never fail, as we are using the activeterm middleware.
    pty, _, _ := s.Pty()

    // When running a Bubble Tea app over SSH, you shouldn't use the default
    // lipgloss.NewStyle function.
    // That function will use the color profile from the os.Stdin, which is the
    // server, not the client.
    // We provide a MakeRenderer function in the bubbletea middleware package,
    // so you can easily get the correct renderer for the current session, and
    // use it to create the styles.
    // The recommended way to use these styles is to then pass them down to
    // your Bubble Tea model.
    renderer := bubbletea.MakeRenderer(s)
    txtStyle := renderer.NewStyle().Foreground(lipgloss.Color("10"))
    quitStyle := renderer.NewStyle().Foreground(lipgloss.Color("8"))

    m := model{
        term:      pty.Term,
        width:     pty.Window.Width,
        height:    pty.Window.Height,
        txtStyle:  txtStyle,
        quitStyle: quitStyle,
    }
    return m, []tea.ProgramOption{tea.WithAltScreen()}
}

// Just a generic tea.Model to demo terminal information of ssh.
type model struct {
    term      string
    width     int
    height    int
    txtStyle  lipgloss.Style
    quitStyle lipgloss.Style
}

func (m model) Init() tea.Cmd {
    return nil
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
    switch msg := msg.(type) {
    case tea.WindowSizeMsg:
        m.height = msg.Height
        m.width = msg.Width
    case tea.KeyMsg:
        switch msg.String() {
        case "q", "ctrl+c":
            return m, tea.Quit
        }
    }
    return m, nil
}

func (m model) View() string {
    s := fmt.Sprintf("Your term is %s\nYour window size is %dx%d", m.term, m.width, m.height)
    return m.txtStyle.Render(s) + "\n\n" + m.quitStyle.Render("Press 'q' to quit\n")
}

And when you hit that port with ssh on an instance with a Nano unikernel you get this and pressing q works!

Your term is xterm-256color
Your window size is 180x51

Press 'q' to quit
bobhenkel commented 6 months ago

I'll close this as the reply from @rinor solved it.