Skip to content

Commit

Permalink
🐛 support sudo over ssh on alpine
Browse files Browse the repository at this point in the history
stat does not support all the flags, especially printf. We can skip newline escapes, since we only grab simple fields. The only limitation here is the SElinux context, which we do not return yet (so we can revisit that then). Even so, it is the last value returned, so we can stick with `-c`

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus committed Sep 24, 2023
1 parent 71b7530 commit 14aa767
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
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

0 comments on commit 14aa767

Please sign in to comment.