Skip to content

Commit

Permalink
Bugfix/276 segmentation fault for ssh playbook (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartendeKruijf authored Dec 5, 2024
1 parent 52b9c1f commit 4554e74
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pkg/core/capability/ssh/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ func execute(command cacao.Command,
return cacao.NewVariables(), err
}
session, client, err := getSession(config, target)
defer client.Close()
if err != nil {
return cacao.NewVariables(), err
}
defer client.Close()

return executeCommand(session, command)
}
Expand All @@ -67,12 +67,12 @@ func executeCommand(session *ssh.Session,
command cacao.Command) (cacao.Variables, error) {

response, err := session.Output(StripSshPrepend(command.Command))
defer session.Close()

if err != nil {
log.Error(err)
return cacao.NewVariables(), err
}
defer session.Close()
results := cacao.NewVariables(cacao.Variable{Type: cacao.VariableTypeString,
Name: sshResultVariableName,
Value: string(response)})
Expand Down
46 changes: 46 additions & 0 deletions test/integration/capability/ssh/ssh_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"soarca/pkg/models/execution"
"testing"

"github.com/go-playground/assert/v2"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -58,3 +59,48 @@ func TestSshConnection(t *testing.T) {
fmt.Println(results)

}

func TestSshConnectionToNonExistingServer(t *testing.T) {
sshCapability := new(ssh.SshCapability)

expectedCommand := cacao.Command{
Type: "ssh",
Command: "ls -la",
}

expectedAuthenticationInformation := cacao.AuthenticationInformation{
ID: "some-authid-1",
Type: "user-auth",
Username: "sshtest",
Password: "pdKY77qNxpI5MAizirtjCVOcm0KFKIs"}

expectedTarget := cacao.AgentTarget{
Type: "ssh",
Address: map[cacao.NetAddressType][]string{"ipv4": {"10.10.10.10"}},
// Port: "22",
AuthInfoIdentifier: "some-authid-1",
}

expectedVariables := cacao.Variable{
Type: "string",
Name: "var1",
Value: "testing",
}

var executionId, _ = uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
var playbookId = "playbook--d09351a2-a075-40c8-8054-0b7c423db83f"
var stepId = "step--81eff59f-d084-4324-9e0a-59e353dbd28f"
var metadata = execution.Metadata{ExecutionId: executionId, PlaybookId: playbookId, StepId: stepId}
data := capability.Context{
Command: expectedCommand,
Target: expectedTarget,
Authentication: expectedAuthenticationInformation,
Variables: cacao.NewVariables(expectedVariables),
}
results, err := sshCapability.Execute(metadata,
data)
assert.NotEqual(t, err, nil)

fmt.Println(results)

}

0 comments on commit 4554e74

Please sign in to comment.