Skip to content

Commit

Permalink
Added an helper function to create .ssh directory inside home
Browse files Browse the repository at this point in the history
Signed-off-by: GLVS Kiriti <[email protected]>
  • Loading branch information
GLVSKiriti authored and poiana committed Apr 2, 2024
1 parent 9f9bb47 commit fc57631
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
21 changes: 5 additions & 16 deletions events/syscall/read_ssh_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,13 @@ var _ = events.Register(
)

func ReadSshInformation(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, "known_hosts")
Expand Down
25 changes: 25 additions & 0 deletions events/syscall/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ limitations under the License.
package syscall

import (
"os"
"os/exec"
"os/user"
"path/filepath"
"strconv"
sys "syscall"

Expand Down Expand Up @@ -91,3 +93,26 @@ func runAsUser(h events.Helper, username string, cmdName string, cmdArgs ...stri
}
return cmd.Run()
}

// Creates a temp directory and .ssh directory inside it.
func CreateSshDirectoryUnderHome() (string, error) {
// Creates temporary data for testing.
var (
tempDirectoryName string
err error
)
// Loop until a unique temporary directory is successfully created
for {
if tempDirectoryName, err = os.MkdirTemp("/home", "falco-event-generator-"); err == nil {
break
}
}

// Create the SSH directory
sshDir := filepath.Join(tempDirectoryName, ".ssh")
if err := os.Mkdir(sshDir, 0755); err != nil {
return "", err
}

return tempDirectoryName, nil
}

0 comments on commit fc57631

Please sign in to comment.