feat(agent): wire up agentssh server to allow exec into container#16638
Conversation
d096ac4 to
56cb9e9
Compare
56cb9e9 to
db9df4b
Compare
db9df4b to
36707f9
Compare
| // EnvInfoer encapsulates external information required by CreateCommand. | ||
| type EnvInfoer interface { | ||
| // CurrentUser returns the current user. | ||
| CurrentUser() (*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) | ||
| } | ||
|
|
||
| type systemEnvInfoer struct{} | ||
|
|
||
| var defaultEnvInfoer EnvInfoer = &systemEnvInfoer{} | ||
|
|
||
| // DefaultEnvInfoer returns a default implementation of | ||
| // EnvInfoer. This reads information using the default Go | ||
| // implementations. | ||
| func DefaultEnvInfoer() EnvInfoer { | ||
| return defaultEnvInfoer | ||
| } | ||
|
|
||
| func (systemEnvInfoer) CurrentUser() (*user.User, error) { | ||
| return user.Current() | ||
| } | ||
|
|
||
| func (systemEnvInfoer) Environ() []string { | ||
| return os.Environ() | ||
| } | ||
|
|
||
| func (systemEnvInfoer) UserHomeDir() (string, error) { | ||
| return userHomeDir() | ||
| } | ||
|
|
||
| func (systemEnvInfoer) UserShell(username string) (string, error) { | ||
| return usershell.Get(username) | ||
| } | ||
|
|
There was a problem hiding this comment.
review: moved to usershell package
| func (*DockerEnvInfoer) Environ() []string { | ||
| // Return a clone of the environment so that the caller can't modify it | ||
| return os.Environ() | ||
| } | ||
|
|
||
| func (*DockerEnvInfoer) UserHomeDir() (string, error) { | ||
| // We default the working directory of the command to the user's home | ||
| // directory. Since this came from inside the container, we cannot guarantee | ||
| // that this exists on the host. Return the "real" home directory of the user | ||
| // instead. | ||
| return os.UserHomeDir() | ||
| } | ||
|
|
There was a problem hiding this comment.
review: we now use the embedded usershell.SystemEnvInfo instead
mafredri
left a comment
There was a problem hiding this comment.
Looks great 👍🏻, minor nits/suggestions inline.
| slog.F("before", append([]string{name}, args...)), | ||
| slog.F("after", append([]string{modifiedName}, modifiedArgs...)), | ||
| ) | ||
| cmd := s.Execer.PTYCommandContext(ctx, modifiedName, modifiedArgs...) |
There was a problem hiding this comment.
Didn't we also have a docker execer? Did we end up not implementing that change, and are only relying on env infoer now? Or are we modifying commands from multiple angles? If former, all good, if latter, I'm wondering if we could unify the concepts (perhaps infoer can be applied on the execer level or vice-versa).
There was a problem hiding this comment.
That's all in the envinfoer now.
|
|
||
| // HomeDir returns the home directory of the current user, giving | ||
| // priority to the $HOME environment variable. | ||
| func HomeDir() (string, error) { |
There was a problem hiding this comment.
Is this still useful as a separate function or should we just require usage of envinfoer?
There was a problem hiding this comment.
I'd like to unexport it and require going through envinfoer, but trying to keep the scope of refactorings small here.
There was a problem hiding this comment.
I'm going to mark it as deprecated for now, we can remove it later.
Co-authored-by: Mathias Fredriksson <mafredri@gmail.com> Co-authored-by: Danny Kopping <danny@coder.com>
mafredri
left a comment
There was a problem hiding this comment.
Just one minor suggestion, but otherwise this is great 👍🏻
Relates to #16419
Builds on top of #16623 and wires up the ReconnectingPTY server. This does nothing to wire up the web terminal yet but the added test demonstrates the functionality working.
Other changes:
SystemEnvInfointerface to theagent/usershellpackage to address follow-up from feat(agent/agentcontainers): add ContainerEnvInfoer #16623 (comment)usershellinfo.Getas deprecated. Consumers should use theEnvInfoerinterface instead.