Skip to content

Commit

Permalink
main: Exit if input files are not named pipes.
Browse files Browse the repository at this point in the history
In discussions with the team, we decided that the input files will
always be named pipes. As a result, we felt it was necessary to
enforce a file-type check that produces a helpful error message.
  • Loading branch information
sfox-equinix committed Aug 17, 2023
1 parent 4686f8d commit 1607c77
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cmd/namedpipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ func RunNamedPipe(ctx context.Context, osArgs []string, h *health.Health, optLog

h.AddReadiness(namedpipe.NamedPipeProcessorComponentName)
eg.Go(func() error {
err := common.IsNamedPipe(sshdLogFilePath)
if err != nil {
return fmt.Errorf("failed to check if sshd log path is a named pipe: %q - %w",
sshdLogFilePath, err)
}

sshdProcessor := sshd.NewSshdProcessor(groupCtx, logins, nodeName, mid, eventWriter, pprov)
npi := namedpipe.NewNamedPipeIngester(logger, h)
if distro == util.DistroRocky {
Expand All @@ -158,10 +164,16 @@ func RunNamedPipe(ctx context.Context, osArgs []string, h *health.Health, optLog

h.AddReadiness(namedpipe.NamedPipeProcessorComponentName)
eg.Go(func() error {
err := common.IsNamedPipe(auditdLogFilePath)
if err != nil {
return fmt.Errorf("failed to check if auditd log path is a named pipe: %q - %w",
auditdLogFilePath, err)
}

np := namedpipe.NewNamedPipeIngester(logger, h)
alp := auditlog.NewAuditLogIngester(auditdLogFilePath, auditLogChan, np)

err := alp.Ingest(groupCtx)
err = alp.Ingest(groupCtx)
if logger.Level().Enabled(zap.DebugLevel) {
logger.Debugf("audit log ingester exited (%v)", err)
}
Expand Down
23 changes: 23 additions & 0 deletions internal/common/namedpipe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package common

Check warning

Code scanning / Revive (reported by Codacy)

should have a package comment Warning

should have a package comment

import (
"errors"
"os"
)

var errNotNamedPipe = errors.New("not a named pipe")

// IsNamedPipe returns a non-nil error if filePath cannot be stat'ed
// or if it is not a named pipe.
func IsNamedPipe(filePath string) error {
sshdLogFileInfo, err := os.Stat(filePath)
if err != nil {
return err
}

if (sshdLogFileInfo.Mode() & os.ModeNamedPipe) == os.ModeNamedPipe {
return nil
}

return errNotNamedPipe
}
42 changes: 42 additions & 0 deletions internal/common/namedpipe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package common

import (
"os"
"path/filepath"
"syscall"
"testing"

"github.com/stretchr/testify/require"
)

func TestIsNamedPipe(t *testing.T) {
t.Parallel()

namedPipePath := filepath.Join(t.TempDir(), "foo.pipe")

require.NoError(t, syscall.Mkfifo(namedPipePath, 0o600))

require.NoError(t, IsNamedPipe(namedPipePath))
}

func TestIsNamedPipe_RegularFile(t *testing.T) {
t.Parallel()

regularFilePath := filepath.Join(t.TempDir(), "foo.txt")

regularFile, err := os.Create(regularFilePath)
require.NoError(t, err)
_ = regularFile.Close()

require.ErrorIs(t, IsNamedPipe(regularFilePath), errNotNamedPipe)
}

func TestIsNamedPipe_StatFailure(t *testing.T) {
t.Parallel()

regularFilePath := filepath.Join(t.TempDir(), string([]byte{0x90, 0x90, 0x90, 0x90}))

var expErr *os.PathError

require.ErrorAs(t, IsNamedPipe(regularFilePath), &expErr)
}

0 comments on commit 1607c77

Please sign in to comment.