Skip to content

Commit 582b1d2

Browse files
refactor: remove code duplication
1 parent 891a240 commit 582b1d2

File tree

3 files changed

+73
-128
lines changed

3 files changed

+73
-128
lines changed

cli/cli.go

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import (
66
"log/slog"
77
"os"
88
"os/signal"
9-
"os/user"
10-
"path/filepath"
11-
"strconv"
129
"strings"
1310
"syscall"
1411

@@ -17,6 +14,7 @@ import (
1714
"github.com/coder/boundary/jail"
1815
"github.com/coder/boundary/rules"
1916
"github.com/coder/boundary/tls"
17+
"github.com/coder/boundary/util"
2018
"github.com/coder/serpent"
2119
)
2220

@@ -90,7 +88,7 @@ func Run(ctx context.Context, config Config, args []string) error {
9088
ctx, cancel := context.WithCancel(ctx)
9189
defer cancel()
9290
logger := setupLogging(config.LogLevel)
93-
username, uid, gid, homeDir, configDir := getUserInfo()
91+
username, uid, gid, homeDir, configDir := util.GetUserInfo()
9492

9593
// Get command arguments
9694
if len(args) == 0 {
@@ -205,42 +203,6 @@ func Run(ctx context.Context, config Config, args []string) error {
205203
return nil
206204
}
207205

208-
// getUserInfo returns information about the current user, handling sudo scenarios
209-
func getUserInfo() (string, int, int, string, string) {
210-
// Only consider SUDO_USER if we're actually running with elevated privileges
211-
// In environments like Coder workspaces, SUDO_USER may be set to 'root'
212-
// but we're not actually running under sudo
213-
if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" && os.Geteuid() == 0 && sudoUser != "root" {
214-
// We're actually running under sudo with a non-root original user
215-
user, err := user.Lookup(sudoUser)
216-
if err != nil {
217-
return getCurrentUserInfo() // Fallback to current user
218-
}
219-
220-
uid, _ := strconv.Atoi(os.Getenv("SUDO_UID"))
221-
gid, _ := strconv.Atoi(os.Getenv("SUDO_GID"))
222-
223-
// If we couldn't get UID/GID from env, parse from user info
224-
if uid == 0 {
225-
if parsedUID, err := strconv.Atoi(user.Uid); err == nil {
226-
uid = parsedUID
227-
}
228-
}
229-
if gid == 0 {
230-
if parsedGID, err := strconv.Atoi(user.Gid); err == nil {
231-
gid = parsedGID
232-
}
233-
}
234-
235-
configDir := getConfigDir(user.HomeDir)
236-
237-
return sudoUser, uid, gid, user.HomeDir, configDir
238-
}
239-
240-
// Not actually running under sudo, use current user
241-
return getCurrentUserInfo()
242-
}
243-
244206
// setupLogging creates a slog logger with the specified level
245207
func setupLogging(logLevel string) *slog.Logger {
246208
var level slog.Level
@@ -265,31 +227,6 @@ func setupLogging(logLevel string) *slog.Logger {
265227
return slog.New(handler)
266228
}
267229

268-
// getCurrentUserInfo gets information for the current user
269-
func getCurrentUserInfo() (string, int, int, string, string) {
270-
currentUser, err := user.Current()
271-
if err != nil {
272-
// Fallback with empty values if we can't get user info
273-
return "", 0, 0, "", ""
274-
}
275-
276-
uid, _ := strconv.Atoi(currentUser.Uid)
277-
gid, _ := strconv.Atoi(currentUser.Gid)
278-
279-
configDir := getConfigDir(currentUser.HomeDir)
280-
281-
return currentUser.Username, uid, gid, currentUser.HomeDir, configDir
282-
}
283-
284-
// getConfigDir determines the config directory based on XDG_CONFIG_HOME or fallback
285-
func getConfigDir(homeDir string) string {
286-
// Use XDG_CONFIG_HOME if set, otherwise fallback to ~/.config/coder_boundary
287-
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
288-
return filepath.Join(xdgConfigHome, "coder_boundary")
289-
}
290-
return filepath.Join(homeDir, ".config", "coder_boundary")
291-
}
292-
293230
// createJailer creates a new jail instance for the current platform
294231
func createJailer(config jail.Config, unprivileged bool) (jail.Jailer, error) {
295232
if unprivileged {

e2e_tests/boundary_integration_test.go

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,14 @@ import (
88
"os/exec"
99
"os/user"
1010
"path/filepath"
11-
"strconv"
1211
"strings"
1312
"testing"
1413
"time"
1514

15+
"github.com/coder/boundary/util"
1616
"github.com/stretchr/testify/require"
1717
)
1818

19-
// getUserInfo returns information about the current user, handling sudo scenarios
20-
func getUserInfo() (string, int, int, string, string) {
21-
// Only consider SUDO_USER if we're actually running with elevated privileges
22-
// In environments like Coder workspaces, SUDO_USER may be set to 'root'
23-
// but we're not actually running under sudo
24-
if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" && os.Geteuid() == 0 && sudoUser != "root" {
25-
// We're actually running under sudo with a non-root original user
26-
user, err := user.Lookup(sudoUser)
27-
if err != nil {
28-
return getCurrentUserInfo() // Fallback to current user
29-
}
30-
31-
uid, _ := strconv.Atoi(os.Getenv("SUDO_UID"))
32-
gid, _ := strconv.Atoi(os.Getenv("SUDO_GID"))
33-
34-
// If we couldn't get UID/GID from env, parse from user info
35-
if uid == 0 {
36-
if parsedUID, err := strconv.Atoi(user.Uid); err == nil {
37-
uid = parsedUID
38-
}
39-
}
40-
if gid == 0 {
41-
if parsedGID, err := strconv.Atoi(user.Gid); err == nil {
42-
gid = parsedGID
43-
}
44-
}
45-
46-
configDir := getConfigDir(user.HomeDir)
47-
48-
return sudoUser, uid, gid, user.HomeDir, configDir
49-
}
50-
51-
// Not actually running under sudo, use current user
52-
return getCurrentUserInfo()
53-
}
54-
55-
// getCurrentUserInfo gets information for the current user
56-
func getCurrentUserInfo() (string, int, int, string, string) {
57-
currentUser, err := user.Current()
58-
if err != nil {
59-
// Fallback with empty values if we can't get user info
60-
return "", 0, 0, "", ""
61-
}
62-
63-
uid, _ := strconv.Atoi(currentUser.Uid)
64-
gid, _ := strconv.Atoi(currentUser.Gid)
65-
66-
configDir := getConfigDir(currentUser.HomeDir)
67-
68-
return currentUser.Username, uid, gid, currentUser.HomeDir, configDir
69-
}
70-
71-
// getConfigDir determines the config directory based on XDG_CONFIG_HOME or fallback
72-
func getConfigDir(homeDir string) string {
73-
// Use XDG_CONFIG_HOME if set, otherwise fallback to ~/.config/coder_boundary
74-
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
75-
return filepath.Join(xdgConfigHome, "coder_boundary")
76-
}
77-
return filepath.Join(homeDir, ".config", "coder_boundary")
78-
}
79-
8019
// findProjectRoot finds the project root by looking for go.mod file
8120
func findProjectRoot(t *testing.T) string {
8221
cwd, err := os.Getwd()
@@ -193,7 +132,7 @@ func TestBoundaryIntegration(t *testing.T) {
193132
fmt.Printf("u.Username: %v\n", u.Username)
194133
fmt.Printf("u.HomeDir: %v\n", u.HomeDir)
195134

196-
sudoUser, uid, gid, homeDir, configDir := getUserInfo()
135+
sudoUser, uid, gid, homeDir, configDir := util.GetUserInfo()
197136
fmt.Printf("sudoUser: %v\n", sudoUser)
198137
fmt.Printf("uid: %v\n", uid)
199138
fmt.Printf("gid: %v\n", gid)

util/user.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package util
2+
3+
import (
4+
"os"
5+
"os/user"
6+
"path/filepath"
7+
"strconv"
8+
)
9+
10+
// GetUserInfo returns information about the current user, handling sudo scenarios
11+
func GetUserInfo() (string, int, int, string, string) {
12+
// Only consider SUDO_USER if we're actually running with elevated privileges
13+
// In environments like Coder workspaces, SUDO_USER may be set to 'root'
14+
// but we're not actually running under sudo
15+
if sudoUser := os.Getenv("SUDO_USER"); sudoUser != "" && os.Geteuid() == 0 && sudoUser != "root" {
16+
// We're actually running under sudo with a non-root original user
17+
user, err := user.Lookup(sudoUser)
18+
if err != nil {
19+
return getCurrentUserInfo() // Fallback to current user
20+
}
21+
22+
uid, _ := strconv.Atoi(os.Getenv("SUDO_UID"))
23+
gid, _ := strconv.Atoi(os.Getenv("SUDO_GID"))
24+
25+
// If we couldn't get UID/GID from env, parse from user info
26+
if uid == 0 {
27+
if parsedUID, err := strconv.Atoi(user.Uid); err == nil {
28+
uid = parsedUID
29+
}
30+
}
31+
if gid == 0 {
32+
if parsedGID, err := strconv.Atoi(user.Gid); err == nil {
33+
gid = parsedGID
34+
}
35+
}
36+
37+
configDir := getConfigDir(user.HomeDir)
38+
39+
return sudoUser, uid, gid, user.HomeDir, configDir
40+
}
41+
42+
// Not actually running under sudo, use current user
43+
return getCurrentUserInfo()
44+
}
45+
46+
// getCurrentUserInfo gets information for the current user
47+
func getCurrentUserInfo() (string, int, int, string, string) {
48+
currentUser, err := user.Current()
49+
if err != nil {
50+
// Fallback with empty values if we can't get user info
51+
return "", 0, 0, "", ""
52+
}
53+
54+
uid, _ := strconv.Atoi(currentUser.Uid)
55+
gid, _ := strconv.Atoi(currentUser.Gid)
56+
57+
configDir := getConfigDir(currentUser.HomeDir)
58+
59+
return currentUser.Username, uid, gid, currentUser.HomeDir, configDir
60+
}
61+
62+
// getConfigDir determines the config directory based on XDG_CONFIG_HOME or fallback
63+
func getConfigDir(homeDir string) string {
64+
// Use XDG_CONFIG_HOME if set, otherwise fallback to ~/.config/coder_boundary
65+
if xdgConfigHome := os.Getenv("XDG_CONFIG_HOME"); xdgConfigHome != "" {
66+
return filepath.Join(xdgConfigHome, "coder_boundary")
67+
}
68+
return filepath.Join(homeDir, ".config", "coder_boundary")
69+
}

0 commit comments

Comments
 (0)