Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3005cb4

Browse files
authored
feat(agent): set additional login vars, LOGNAME and SHELL (#16874)
This change stes additional env vars. This is useful for programs that assume their presence (for instance, Zed remote relies on SHELL). See `man login`.
1 parent 86b61ef commit 3005cb4

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

agent/agent_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import (
5151
"github.com/coder/coder/v2/agent/agentssh"
5252
"github.com/coder/coder/v2/agent/agenttest"
5353
"github.com/coder/coder/v2/agent/proto"
54+
"github.com/coder/coder/v2/agent/usershell"
5455
"github.com/coder/coder/v2/codersdk"
5556
"github.com/coder/coder/v2/codersdk/agentsdk"
5657
"github.com/coder/coder/v2/codersdk/workspacesdk"
@@ -1193,6 +1194,53 @@ func TestAgent_SSHConnectionEnvVars(t *testing.T) {
11931194
}
11941195
}
11951196

1197+
func TestAgent_SSHConnectionLoginVars(t *testing.T) {
1198+
t.Parallel()
1199+
1200+
envInfo := usershell.SystemEnvInfo{}
1201+
u, err := envInfo.User()
1202+
require.NoError(t, err, "get current user")
1203+
shell, err := envInfo.Shell(u.Username)
1204+
require.NoError(t, err, "get current shell")
1205+
1206+
tests := []struct {
1207+
key string
1208+
want string
1209+
}{
1210+
{
1211+
key: "USER",
1212+
want: u.Username,
1213+
},
1214+
{
1215+
key: "LOGNAME",
1216+
want: u.Username,
1217+
},
1218+
{
1219+
key: "HOME",
1220+
want: u.HomeDir,
1221+
},
1222+
{
1223+
key: "SHELL",
1224+
want: shell,
1225+
},
1226+
}
1227+
for _, tt := range tests {
1228+
tt := tt
1229+
t.Run(tt.key, func(t *testing.T) {
1230+
t.Parallel()
1231+
1232+
session := setupSSHSession(t, agentsdk.Manifest{}, codersdk.ServiceBannerConfig{}, nil)
1233+
command := "sh -c 'echo $" + tt.key + "'"
1234+
if runtime.GOOS == "windows" {
1235+
command = "cmd.exe /c echo %" + tt.key + "%"
1236+
}
1237+
output, err := session.Output(command)
1238+
require.NoError(t, err)
1239+
require.Equal(t, tt.want, strings.TrimSpace(string(output)))
1240+
})
1241+
}
1242+
}
1243+
11961244
func TestAgent_Metadata(t *testing.T) {
11971245
t.Parallel()
11981246

agent/agentssh/agentssh.go

+3
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,10 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string,
900900
cmd.Dir = homedir
901901
}
902902
cmd.Env = append(ei.Environ(), env...)
903+
// Set login variables (see `man login`).
903904
cmd.Env = append(cmd.Env, fmt.Sprintf("USER=%s", username))
905+
cmd.Env = append(cmd.Env, fmt.Sprintf("LOGNAME=%s", username))
906+
cmd.Env = append(cmd.Env, fmt.Sprintf("SHELL=%s", shell))
904907

905908
// Set SSH connection environment variables (these are also set by OpenSSH
906909
// and thus expected to be present by SSH clients). Since the agent does

0 commit comments

Comments
 (0)