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

Skip to content

fix(agent/usershell): check shell on darwin via dscl #8366

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 2 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 20 additions & 3 deletions agent/usershell/usershell_darwin.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
package usershell

import "os"
import (
"os"
"os/exec"
"path/filepath"
"strings"

"golang.org/x/xerrors"
)

// Get returns the $SHELL environment variable.
func Get(_ string) (string, error) {
return os.Getenv("SHELL"), nil
func Get(username string) (string, error) {
// This command will output "UserShell: /bin/zsh" if successful, we
// can ignore the error since we have fallback behavior.
out, _ := exec.Command("dscl", ".", "-read", filepath.Join("/Users", username), "UserShell").Output()
s, ok := strings.CutPrefix(string(out), "UserShell: ")
if ok {
return strings.TrimSpace(s), nil
}
if s = os.Getenv("SHELL"); s != "" {
return s, nil
}
return "", xerrors.Errorf("shell for user %q not found via dscl or in $SHELL", username)
}
5 changes: 4 additions & 1 deletion agent/usershell/usershell_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ func Get(username string) (string, error) {
}
return parts[6], nil
}
return "", xerrors.Errorf("user %q not found in /etc/passwd", username)
if s := os.Getenv("SHELL"); s != "" {
return s, nil
}
return "", xerrors.Errorf("shell for user %q not found in /etc/passwd or $SHELL", username)
}
27 changes: 0 additions & 27 deletions agent/usershell/usershell_other_test.go

This file was deleted.

46 changes: 46 additions & 0 deletions agent/usershell/usershell_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package usershell_test

import (
"os/user"
"runtime"
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/agent/usershell"
)

//nolint:paralleltest,tparallel // This test sets an environment variable.
func TestGet(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}

t.Run("Fallback", func(t *testing.T) {
t.Setenv("SHELL", "/bin/sh")

t.Run("NonExistentUser", func(t *testing.T) {
shell, err := usershell.Get("notauser")
require.NoError(t, err)
require.Equal(t, "/bin/sh", shell)
})
})

t.Run("NoFallback", func(t *testing.T) {
// Disable env fallback for these tests.
t.Setenv("SHELL", "")

t.Run("NotFound", func(t *testing.T) {
_, err := usershell.Get("notauser")
require.Error(t, err)
})

t.Run("User", func(t *testing.T) {
u, err := user.Current()
require.NoError(t, err)
shell, err := usershell.Get(u.Username)
require.NoError(t, err)
require.NotEmpty(t, shell)
})
})
}