Skip to content

Commit

Permalink
Refactored ReadSshInformation function to improve directory creation …
Browse files Browse the repository at this point in the history
…logic

Signed-off-by: GLVS Kiriti <glvskiriti2003369@gmail.com>
  • Loading branch information
GLVSKiriti authored and poiana committed Apr 2, 2024
1 parent 8599629 commit 9f9bb47
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions events/syscall/read_ssh_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ limitations under the License.
package syscall

import (
"path/filepath"

"github.com/falcosecurity/event-generator/events"

"os"
Expand All @@ -30,19 +32,31 @@ var _ = events.Register(

func ReadSshInformation(h events.Helper) error {
// Creates temporary data for testing.
directoryname := "/home/created-by-falco-event-generator/.ssh"
if err := os.MkdirAll(directoryname, 0755); err != nil {
var (
directoryname string
err error
)
// Loop until a unique temporary directory is successfully created
for {
if directoryname, err = os.MkdirTemp("/home", "falco-event-generator-"); err == nil {
break
}
}
defer os.RemoveAll(directoryname)

// Create the SSH directory
sshDir := filepath.Join(directoryname, ".ssh")
if err := os.Mkdir(sshDir, 0755); err != nil {
return err
}
defer os.RemoveAll("/home/created-by-falco-event-generator")

filename := directoryname + "/known_hosts"
if err := os.WriteFile(filename, nil, os.FileMode(0755)); err != nil {
// Create known_hosts file. os.Create is enough to trigger the rule
filename := filepath.Join(sshDir, "known_hosts")
if _, err := os.Create(filename); err != nil {
return err
}

h.Log().Info("attempting to simulate SSH information read")
file, err := os.Open(filename)
defer file.Close()
return err

return nil
}

0 comments on commit 9f9bb47

Please sign in to comment.