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 f569b18 commit adab5f3
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions events/syscall/adding_ssh_keys_to_authorized_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,38 @@ package syscall

import (
"os"
"path/filepath"

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

var _ = events.Register(
AddingSshKeysToAuthorizedKeys,
events.WithDisabled(), // this rules is not included in falco_rules.yaml (stable rules), so disable the action
// events.WithDisabled(), // this rules is not included in falco_rules.yaml (stable rules), so disable the action
)

func AddingSshKeysToAuthorizedKeys(h events.Helper) error {
// Creates temporary data for testing.
directoryname := "/home/created-by-falco-event-generator/.ssh"
filename := directoryname + "/authorized_keys"
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)

if err := os.MkdirAll(directoryname, 0755); err != nil {
// 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")

// Create known_hosts file. os.Create is enough to trigger the rule
filename := filepath.Join(sshDir, "authorized_keys")

h.Log().Infof("writing to %s", filename)
return os.WriteFile(filename, []byte("ssh-rsa <ssh_public_key>\n"), os.FileMode(0755))
Expand Down

0 comments on commit adab5f3

Please sign in to comment.