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

main: Exit if input files are not named pipes. #114

Merged
merged 1 commit into from
Aug 17, 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
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)
}
Loading