Skip to content

Commit

Permalink
Refactored code by using a helper function CreateSshDirectoryUnderHom…
Browse files Browse the repository at this point in the history
…e to remove code redundancy

Signed-off-by: GLVS Kiriti <glvskiriti2003369@gmail.com>
  • Loading branch information
GLVSKiriti authored and poiana committed Apr 2, 2024
1 parent adab5f3 commit 0833a43
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions events/syscall/adding_ssh_keys_to_authorized_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,21 @@ import (

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.
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 {
// Also creates .ssh directory inside tempDirectory
tempDirectoryName, err := CreateSshDirectoryUnderHome()
if err != nil {
return err
}
sshDir := filepath.Join(tempDirectoryName, ".ssh")
defer os.RemoveAll(tempDirectoryName)

// 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)

// Create authorized_keys file, and write into it
return os.WriteFile(filename, []byte("ssh-rsa <ssh_public_key>\n"), os.FileMode(0755))
}

0 comments on commit 0833a43

Please sign in to comment.