Skip to content

refactor(agent): Move SSH server into agentssh package #7004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address PR feedback
  • Loading branch information
mafredri committed Apr 6, 2023
commit 94d759396ad87cb386f54917ea51cc56e4a6fe0f
2 changes: 1 addition & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func (a *agent) init(ctx context.Context) {
}
sshSrv.Env = a.envVars
sshSrv.AgentToken = func() string { return *a.sessionToken.Load() }
sshSrv.Manifest = &a.manifest
a.sshServer = sshSrv

go a.runLoop(ctx)
Expand Down Expand Up @@ -478,7 +479,6 @@ func (a *agent) run(ctx context.Context) error {
}

oldManifest := a.manifest.Swap(&manifest)
a.sshServer.SetManifest(&manifest)

// The startup script should only execute on the first run!
if oldManifest == nil {
Expand Down
14 changes: 4 additions & 10 deletions agent/agentssh/agentssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"runtime"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/gliderlabs/ssh"
"github.com/pkg/sftp"
"go.uber.org/atomic"
gossh "golang.org/x/crypto/ssh"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -54,8 +54,7 @@ type Server struct {

Env map[string]string
AgentToken func() string

manifest atomic.Pointer[agentsdk.Manifest]
Manifest *atomic.Pointer[agentsdk.Manifest]

connCountVSCode atomic.Int64
connCountJetBrains atomic.Int64
Expand Down Expand Up @@ -130,11 +129,6 @@ func NewServer(ctx context.Context, logger slog.Logger, maxTimeout time.Duration
return s, nil
}

// SetManifest sets the manifest used for starting commands.
func (s *Server) SetManifest(m *agentsdk.Manifest) {
s.manifest.Store(m)
}

type ConnStats struct {
Sessions int64
VSCode int64
Expand Down Expand Up @@ -215,7 +209,7 @@ func (s *Server) sessionStart(session ssh.Session) (retErr error) {
session.DisablePTYEmulation()

if !isQuietLogin(session.RawCommand()) {
manifest := s.manifest.Load()
manifest := s.Manifest.Load()
if manifest != nil {
err = showMOTD(session, manifest.MOTDFile)
if err != nil {
Expand Down Expand Up @@ -388,7 +382,7 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string)
return nil, xerrors.Errorf("get user shell: %w", err)
}

manifest := s.manifest.Load()
manifest := s.Manifest.Load()
if manifest == nil {
return nil, xerrors.Errorf("no metadata was provided")
}
Expand Down
7 changes: 5 additions & 2 deletions agent/agentssh/agentssh_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Package agentssh_test provides tests for basic functinoality of the agentssh
// package, more test coverage can be found in the `agent` and `cli` package(s).
package agentssh_test

import (
Expand All @@ -10,6 +12,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/goleak"
"golang.org/x/crypto/ssh"

Expand All @@ -34,7 +37,7 @@ func TestNewServer_ServeClient(t *testing.T) {

// The assumption is that these are set before serving SSH connections.
s.AgentToken = func() string { return "" }
s.SetManifest(&agentsdk.Manifest{})
s.Manifest = atomic.NewPointer(&agentsdk.Manifest{})

ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
Expand Down Expand Up @@ -74,7 +77,7 @@ func TestNewServer_CloseActiveConnections(t *testing.T) {

// The assumption is that these are set before serving SSH connections.
s.AgentToken = func() string { return "" }
s.SetManifest(&agentsdk.Manifest{})
s.Manifest = atomic.NewPointer(&agentsdk.Manifest{})

ln, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
Expand Down