|
| 1 | +// SPDX-FileCopyrightText: 2024-2025 OOMOL, Inc. <https://www.oomol.com> |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package shim |
| 5 | + |
| 6 | +import ( |
| 7 | + "bauklotze/pkg/machine/channel" |
| 8 | + "bauklotze/pkg/machine/define" |
| 9 | + "bauklotze/pkg/machine/vmconfig" |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "net" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + |
| 16 | + "golang.org/x/sync/errgroup" |
| 17 | + |
| 18 | + "github.com/oomol-lab/ovm-ssh-agent/pkg/sshagent" |
| 19 | + "github.com/oomol-lab/ovm-ssh-agent/pkg/system" |
| 20 | + |
| 21 | + "github.com/oomol-lab/ovm-ssh-agent/pkg/identity" |
| 22 | + forwarder "github.com/oomol-lab/ssh-forward" |
| 23 | + |
| 24 | + "github.com/sirupsen/logrus" |
| 25 | +) |
| 26 | + |
| 27 | +func TryStartSSHAuthService(ctx context.Context, mc *vmconfig.MachineConfig) error { |
| 28 | + select { |
| 29 | + case <-ctx.Done(): |
| 30 | + return fmt.Errorf("cancel TryStartSSHAuthService cause: %w", context.Cause(ctx)) |
| 31 | + case <-channel.WaitVMReady(): |
| 32 | + break |
| 33 | + } |
| 34 | + |
| 35 | + logrus.Infoln("Start SSH auth service") |
| 36 | + return startSSHAuthServiceAndForward(ctx, mc) |
| 37 | +} |
| 38 | + |
| 39 | +func startSSHAuthServiceAndForward(ctx context.Context, mc *vmconfig.MachineConfig) error { |
| 40 | + ctx, cancel := context.WithCancelCause(ctx) |
| 41 | + defer cancel(context.Canceled) |
| 42 | + |
| 43 | + g, ctx := errgroup.WithContext(ctx) |
| 44 | + |
| 45 | + localSocketFile := filepath.Join(mc.Dirs.SocksDir.GetPath(), "oo-ssh-agent-host.sock") |
| 46 | + if err := os.Remove(localSocketFile); err != nil && !os.IsNotExist(err) { |
| 47 | + return fmt.Errorf("failed to remove local ssh agent socket: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + listener, err := net.Listen("unix", localSocketFile) |
| 51 | + if err != nil { |
| 52 | + return fmt.Errorf("failed to listen unix socket: %w", err) |
| 53 | + } |
| 54 | + defer listener.Close() |
| 55 | + |
| 56 | + upstreamSocket := system.GetSSHAgent() |
| 57 | + if upstreamSocket == "" { |
| 58 | + return fmt.Errorf("upstream SSH agent socket empty") |
| 59 | + } |
| 60 | + logrus.Infof("upstream ssh agent listened in: %q", upstreamSocket) |
| 61 | + |
| 62 | + ooSSHAgent, err := sshagent.NewSSHAgent(ctx, upstreamSocket) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("failed to create oo ssh agent: %w", err) |
| 65 | + } |
| 66 | + defer ooSSHAgent.Close() |
| 67 | + |
| 68 | + // find local private keys ~/.ssh |
| 69 | + ooSSHAgent.LoadLocalKeys(identity.FindPrivateKeys()...) |
| 70 | + |
| 71 | + g.Go(func() error { |
| 72 | + return ooSSHAgent.Serve(listener) |
| 73 | + }) |
| 74 | + |
| 75 | + remoteSocketFile := "/opt/ssh_auth/oo-ssh-agent.sock" |
| 76 | + logrus.Infof("forward unix socket %q to %q", localSocketFile, |
| 77 | + fmt.Sprintf("%s@%s:%d:[%s]", mc.SSH.RemoteUsername, define.LocalHostURL, mc.SSH.Port, remoteSocketFile)) |
| 78 | + |
| 79 | + socketForwarder := forwarder.NewUnixRemote(localSocketFile, define.LocalHostURL, remoteSocketFile) |
| 80 | + socketForwarder.SetTunneledConnState(func(tun *forwarder.ForwardConfig, state *forwarder.TunneledConnState) { |
| 81 | + logrus.Infof("connect state: %v", state) |
| 82 | + }) |
| 83 | + |
| 84 | + socketForwarder.SetKeyFile(mc.SSH.IdentityPath) |
| 85 | + socketForwarder.SetUser(mc.SSH.RemoteUsername) |
| 86 | + socketForwarder.SetPort(mc.SSH.Port) |
| 87 | + |
| 88 | + // We set a callback to know when the tunnel is ready |
| 89 | + socketForwarder.SetConnState(func(tun *forwarder.ForwardConfig, state forwarder.ConnState) { |
| 90 | + switch state { |
| 91 | + case forwarder.StateStarting: |
| 92 | + logrus.Infof("socket forwarder state is staring") |
| 93 | + logrus.Infof("clean target socket file:%s", socketForwarder.Remote.String()) |
| 94 | + if err := socketForwarder.CleanTargetSocketFile(); err != nil { |
| 95 | + cancel(fmt.Errorf("failed to clean target socket file: %w", err)) |
| 96 | + } |
| 97 | + case forwarder.StateStarted: |
| 98 | + logrus.Infoln("socket forwarder state is: started") |
| 99 | + case forwarder.StateStopped: |
| 100 | + logrus.Infoln("socket forwarder state is: stopped") |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + g.Go(func() error { |
| 105 | + return socketForwarder.Start(ctx) |
| 106 | + }) |
| 107 | + |
| 108 | + return g.Wait() //nolint:wrapcheck |
| 109 | +} |
0 commit comments