Skip to content

feat(agent): wire up agentssh server to allow exec into container #16638

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 14 commits into from
Feb 26, 2025
Prev Previous commit
Next Next commit
simply naming of interface methods
  • Loading branch information
johnstcn committed Feb 25, 2025
commit 46a6b7d1c949f14e280094914a0de165bcb951dd
4 changes: 2 additions & 2 deletions agent/agentcontainers/containers_dockercli.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ func EnvInfo(ctx context.Context, execer agentexec.Execer, container, containerU
return &dei, nil
}

func (dei *DockerEnvInfoer) CurrentUser() (*user.User, error) {
func (dei *DockerEnvInfoer) User() (*user.User, error) {
// Clone the user so that the caller can't modify it
u := *dei.user
return &u, nil
}

func (dei *DockerEnvInfoer) UserShell(string) (string, error) {
func (dei *DockerEnvInfoer) Shell(string) (string, error) {
return dei.userShell, nil
}

Expand Down
6 changes: 3 additions & 3 deletions agent/agentcontainers/containers_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,15 +502,15 @@ func TestDockerEnvInfoer(t *testing.T) {
dei, err := EnvInfo(ctx, agentexec.DefaultExecer, ct.Container.ID, tt.containerUser)
require.NoError(t, err, "Expected no error from DockerEnvInfo()")

u, err := dei.CurrentUser()
u, err := dei.User()
require.NoError(t, err, "Expected no error from CurrentUser()")
require.Equal(t, tt.expectedUsername, u.Username, "Expected username to match")

hd, err := dei.UserHomeDir()
hd, err := dei.HomeDir()
require.NoError(t, err, "Expected no error from UserHomeDir()")
require.NotEmpty(t, hd, "Expected user homedir to be non-empty")

sh, err := dei.UserShell(tt.containerUser)
sh, err := dei.Shell(tt.containerUser)
require.NoError(t, err, "Expected no error from UserShell()")
require.Equal(t, tt.expectedUserShell, sh, "Expected user shell to match")

Expand Down
6 changes: 3 additions & 3 deletions agent/agentssh/agentssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,13 +709,13 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string,
if ei == nil {
ei = &usershell.SystemEnvInfo{}
}
currentUser, err := ei.CurrentUser()
currentUser, err := ei.User()
if err != nil {
return nil, xerrors.Errorf("get current user: %w", err)
}
username := currentUser.Username

shell, err := ei.UserShell(username)
shell, err := ei.Shell(username)
if err != nil {
return nil, xerrors.Errorf("get user shell: %w", err)
}
Expand Down Expand Up @@ -777,7 +777,7 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string,
_, err = os.Stat(cmd.Dir)
if cmd.Dir == "" || err != nil {
// Default to user home if a directory is not set.
homedir, err := ei.UserHomeDir()
homedir, err := ei.HomeDir()
if err != nil {
return nil, xerrors.Errorf("get home dir: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions agent/agentssh/agentssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ type fakeEnvInfoer struct {
UserShellFn func(string) (string, error)
}

func (f *fakeEnvInfoer) CurrentUser() (u *user.User, err error) {
func (f *fakeEnvInfoer) User() (u *user.User, err error) {
return f.CurrentUserFn()
}

func (f *fakeEnvInfoer) Environ() []string {
return f.EnvironFn()
}

func (f *fakeEnvInfoer) UserHomeDir() (string, error) {
func (f *fakeEnvInfoer) HomeDir() (string, error) {
return f.UserHomeDirFn()
}

func (f *fakeEnvInfoer) UserShell(u string) (string, error) {
func (f *fakeEnvInfoer) Shell(u string) (string, error) {
return f.UserShellFn(u)
}

Expand Down
18 changes: 9 additions & 9 deletions agent/usershell/usershell.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ func HomeDir() (string, error) {

// EnvInfoer encapsulates external information about the environment.
type EnvInfoer interface {
// CurrentUser returns the current user.
CurrentUser() (*user.User, error)
// User returns the current user.
User() (*user.User, error)
// Environ returns the environment variables of the current process.
Environ() []string
// UserHomeDir returns the home directory of the current user.
UserHomeDir() (string, error)
// UserShell returns the shell of the given user.
UserShell(username string) (string, error)
// HomeDir returns the home directory of the current user.
HomeDir() (string, error)
// Shell returns the shell of the given user.
Shell(username string) (string, error)
// ModifyCommand modifies the command and arguments before execution based on
// the environment. This is useful for executing a command inside a container.
// In the default case, the command and arguments are returned unchanged.
Expand All @@ -44,19 +44,19 @@ type EnvInfoer interface {
// just using the default Go implementations.
type SystemEnvInfo struct{}

func (SystemEnvInfo) CurrentUser() (*user.User, error) {
func (SystemEnvInfo) User() (*user.User, error) {
return user.Current()
}

func (SystemEnvInfo) Environ() []string {
return os.Environ()
}

func (SystemEnvInfo) UserHomeDir() (string, error) {
func (SystemEnvInfo) HomeDir() (string, error) {
return HomeDir()
}

func (SystemEnvInfo) UserShell(username string) (string, error) {
func (SystemEnvInfo) Shell(username string) (string, error) {
return Get(username)
}

Expand Down