Skip to content

Commit

Permalink
feat: enhance SSH configuration checks and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dz0ny committed Jan 3, 2025
1 parent 9eb2fe5 commit 3c071d8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
29 changes: 24 additions & 5 deletions checks/ssh_config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package checks

import (
"os"
"strings"

"github.com/caarlos0/log"
Expand Down Expand Up @@ -72,11 +71,31 @@ func (s *SSHConfigCheck) Passed() bool {
}

func (s *SSHConfigCheck) IsRunnable() bool {
if _, err := os.Stat("/etc/ssh/sshd_config"); os.IsNotExist(err) {
s.status = "/etc/ssh/sshd_config not found"
return false

// Check if sshd service is running via systemd
sshdStatus, err := shared.RunCommand("systemctl", "is-active", "sshd")
if err != nil || strings.TrimSpace(string(sshdStatus)) == "active" {
return true
}
return true

// Check if ssh service is running via systemd
sshStatus, err := shared.RunCommand("systemctl", "is-active", "ssh")
if err != nil || strings.TrimSpace(string(sshStatus)) == "active" {
return true
}
// Check if ssh socket service is enabled via systemd
sshSocketStatus, err := shared.RunCommand("systemctl", "is-enabled", "sshd.socket")
if err != nil || strings.TrimSpace(string(sshSocketStatus)) == "enabled" {
return true
}

// Check if ssh socket service is enabled via systemd
sshSocketStatus, err = shared.RunCommand("systemctl", "is-enabled", "ssh.socket")
if err != nil || strings.TrimSpace(string(sshSocketStatus)) == "enabled" {
return true
}

return false
}

func (s *SSHConfigCheck) ReportIfDisabled() bool {
Expand Down
67 changes: 67 additions & 0 deletions checks/ssh_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package checks

import (
"testing"

"github.com/ParetoSecurity/pareto-linux/shared"
"github.com/stretchr/testify/assert"
)

func TestCheckSSHConfig(t *testing.T) {

tests := []struct {
name string
setupMocks map[string]string
expectedPassed bool
expectedDetail string
}{
{
name: "All ok",
setupMocks: map[string]string{
"sshd -T": "PasswordAuthentication no\nPermitRootLogin no",
},
expectedPassed: true,
expectedDetail: "",
},
{
name: "PasswordAuthentication is enabled",
setupMocks: map[string]string{
"sshd -T": "PasswordAuthentication yes\nPermitRootLogin no",
},
expectedPassed: false,
expectedDetail: "PasswordAuthentication is enabled",
},
{
name: "PermitRootLogin is enabled",
setupMocks: map[string]string{
"sshd -T": "PasswordAuthentication no\nPermitRootLogin yes",
},
expectedPassed: false,
expectedDetail: "Root login is enabled",
},

{
name: "PermitEmptyPasswords is enabled",
setupMocks: map[string]string{
"sshd -T": "PasswordAuthentication no\nPermitRootLogin no\nPermitEmptyPasswords yes",
},
expectedPassed: false,
expectedDetail: "Empty passwords are allowed",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
shared.RunCommandMocks = tt.setupMocks
lookPathMock = func(file string) (string, error) {
return file, nil
}
su := &SSHConfigCheck{}

err := su.Run()
assert.Nil(t, err)
assert.Equal(t, tt.expectedPassed, su.passed)
assert.Equal(t, tt.expectedDetail, su.status)
})
}
}
4 changes: 4 additions & 0 deletions shared/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"os"
"testing"

"strings"

Expand Down Expand Up @@ -62,6 +63,9 @@ func SystemSerial() (string, error) {
}

func IsRoot() bool {
if testing.Testing() {
return true
}
return os.Geteuid() == 0
}

Expand Down

0 comments on commit 3c071d8

Please sign in to comment.