Skip to content
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

🐛 support sudo over ssh on alpine #1869

Merged
merged 1 commit into from
Sep 24, 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
18 changes: 6 additions & 12 deletions providers/os/connection/local/statutil/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,10 @@ func (s *statHelper) linux(name string) (os.FileInfo, error) {
return nil, os.ErrNotExist
}

// run stat
lstat := "-L"
format := "--printf"

var sb strings.Builder

sb.WriteString("stat ")
sb.WriteString(lstat)
sb.WriteString(" ")
sb.WriteString("stat -L ")
sb.WriteString(path)
sb.WriteString(" ")
sb.WriteString(format)
sb.WriteString(" '%s\n%f\n%u\n%g\n%X\n%Y\n%C'")
sb.WriteString(" -c '%s.%f.%u.%g.%X.%Y.%C'")

// NOTE: handling the exit code here does not work for all cases
// sometimes stat returns something like: failed to get security context of '/etc/ssh/sshd_config': No data available
Expand All @@ -134,7 +125,7 @@ func (s *statHelper) linux(name string) (os.FileInfo, error) {
return nil, err
}

statsData := strings.Split(string(data), "\n")
statsData := strings.Split(strings.TrimSpace(string(data)), ".")
if len(statsData) != 7 {
log.Debug().Str("path", path).Msg("could not parse file stat information")
// TODO: we may need to parse the returning error to better distinguish between a real error and file not found
Expand All @@ -143,6 +134,9 @@ func (s *statHelper) linux(name string) (os.FileInfo, error) {
return nil, errors.New("could not parse file stat: " + path)
}

// Note: The SElinux context may not be supported by stats on all OSs.
// For example: Alpine does not support it, resulting in statsData[6] == "C"

size, err := strconv.Atoi(statsData[0])
if err != nil {
return nil, errors.Wrap(err, "could not stat "+name)
Expand Down
18 changes: 4 additions & 14 deletions providers/os/connection/local/statutil/testdata/linux.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@ stdout = "x86_64"
[commands."uname -s"]
stdout = "Linux"

[commands."stat -L /etc/ssh/sshd_config --printf '%s\n%f\n%u\n%g\n%X\n%Y\n%C'"]
stdout = """4317
8180
0
0
1590420240
1590418792
[commands."stat -L /etc/ssh/sshd_config -c '%s.%f.%u.%g.%X.%Y.%C'"]
stdout = """4317.8180.0.0.1590420240.1590418792.?
"""

[commands."stat -L /usr/bin/su --printf '%s\n%f\n%u\n%g\n%X\n%Y\n%C'"]
stdout = """71728
89ed
0
0
1634057181
1629123001
[commands."stat -L /usr/bin/su -c '%s.%f.%u.%g.%X.%Y.%C'"]
stdout = """71728.89ed.0.0.1634057181.1629123001.?
"""


Expand Down
18 changes: 4 additions & 14 deletions providers/os/connection/ssh/cat/testdata/cat.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,15 @@ UsePAM yes
"""


[commands."sudo stat -L /etc/ssh/sshd_config --printf '%s\n%f\n%u\n%g\n%X\n%Y\n%C'"]
stdout = """4317
8180
0
0
1590420240
1590418792
[commands."sudo stat -L /etc/ssh/sshd_config -c '%s.%f.%u.%g.%X.%Y.%C'"]
stdout = """4317.8180.0.0.1590420240.1590418792.?
"""

[commands."sudo test -e /etc/ssh"]
stdout = ""

[commands."sudo stat -L /etc/ssh --printf '%s\n%f\n%u\n%g\n%X\n%Y\n%C'"]
stdout = """271
41ed
0
0
1635245760
1635147499
[commands."sudo stat -L /etc/ssh -c '%s.%f.%u.%g.%X.%Y.%C'"]
stdout = """271.41ed.0.0.1635245760.1635147499.?
"""

[commands."sudo ls -1 '/etc/ssh'"]
Expand Down
Loading