Skip to content

Commit

Permalink
Allow user to configure default ssh port and override ssh port
Browse files Browse the repository at this point in the history
  • Loading branch information
robgonnella committed Sep 10, 2023
1 parent 28b5d4e commit 7565be3
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 222 deletions.
2 changes: 2 additions & 0 deletions internal/config/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ type SSHOverride struct {
Target string `json:"target"`
User string `json:"user"`
Identity string `json:"identity"`
Port string `json:"port"`
}

// SSHConfig represents the config needed to ssh to servers
type SSHConfig struct {
User string `json:"user"`
Identity string `json:"identity"`
Port string `json:"port"`
Overrides []SSHOverride `json:"overrides"`
}

Expand Down
175 changes: 0 additions & 175 deletions internal/config/repo-sqlite.go

This file was deleted.

6 changes: 2 additions & 4 deletions internal/config/repo-yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/google/uuid"
"github.com/robgonnella/ops/internal/exception"
"github.com/spf13/viper"
)

// JSONRepo is our repo implementation for json
Expand All @@ -22,9 +21,7 @@ type JSONRepo struct {
}

// NewJSONRepo returns a new ops repo for flat yaml file
func NewJSONRepo() *JSONRepo {
configPath := viper.Get("config-path").(string)

func NewJSONRepo(configPath string) *JSONRepo {
repo := &JSONRepo{
configPath: configPath,
configs: []*Config{},
Expand Down Expand Up @@ -226,6 +223,7 @@ func copyConfig(c *Config) *Config {
SSH: SSHConfig{
User: c.SSH.User,
Identity: c.SSH.Identity,
Port: c.SSH.Port,
Overrides: c.SSH.Overrides,
},
CIDR: c.CIDR,
Expand Down
21 changes: 4 additions & 17 deletions internal/config/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/robgonnella/ops/internal/config"
"github.com/robgonnella/ops/internal/exception"
"github.com/robgonnella/ops/internal/test_util"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,26 +21,14 @@ func assertEqualConf(t *testing.T, expected, actual *config.Config) {
}
}

func TestConfigSqliteRepo(t *testing.T) {
testDBFile := "config.db"
func TestConfigYamlRepo(t *testing.T) {
testConfigFile := "config.json"

defer func() {
os.RemoveAll(testDBFile)
os.RemoveAll(testConfigFile)
}()

db, err := test_util.GetDBConnection(testDBFile)

if err != nil {
t.Logf("failed to create test db: %s", err.Error())
t.FailNow()
}

if err := test_util.Migrate(db, config.ConfigModel{}); err != nil {
t.Logf("failed to migrate test db: %s", err.Error())
t.FailNow()
}

repo := config.NewSqliteRepo(db)
repo := config.NewJSONRepo(testConfigFile)

t.Run("returns record not found error", func(st *testing.T) {
_, err := repo.Get("10")
Expand Down
4 changes: 3 additions & 1 deletion internal/core/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func getDefaultConfig(networkInfo *network.NetworkInfo) *config.Config {
SSH: config.SSHConfig{
User: user,
Identity: identity,
Port: "22",
Overrides: []config.SSHOverride{},
},
CIDR: networkInfo.Cidr,
Expand All @@ -35,7 +36,8 @@ func getDefaultConfig(networkInfo *network.NetworkInfo) *config.Config {

// CreateNewAppCore creates and returns a new instance of *core.Core
func CreateNewAppCore(networkInfo *network.NetworkInfo) (*Core, error) {
configRepo := config.NewJSONRepo()
configPath := viper.Get("config-path").(string)
configRepo := config.NewJSONRepo(configPath)
configService := config.NewConfigService(configRepo)

conf, err := configService.GetByCIDR(networkInfo.Cidr)
Expand Down
4 changes: 2 additions & 2 deletions internal/discovery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (s *ScannerService) pollNetwork() {
Status: PortClosed,
},
}
s.handleDiscoveryResult(dr)
go s.handleDiscoveryResult(dr)
case scanner.SYNResult:
res := r.Payload.(*scanner.SynScanResult)
dr := &DiscoveryResult{
Expand All @@ -115,7 +115,7 @@ func (s *ScannerService) pollNetwork() {
Status: PortStatus(res.Port.Status),
},
}
s.handleDiscoveryResult(dr)
go s.handleDiscoveryResult(dr)
}
case err := <-s.errorChan:
s.log.Error().Err(err).Msg("discovery service encountered an error")
Expand Down
55 changes: 44 additions & 11 deletions internal/discovery/uname.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NewUnameScanner(conf config.Config) *UnameScanner {
func (s UnameScanner) GetServerDetails(ctx context.Context, ip string) (*Details, error) {
user := s.conf.SSH.User
identity := s.conf.SSH.Identity
port := s.conf.SSH.Port

for _, o := range s.conf.SSH.Overrides {
if o.Target == ip {
Expand All @@ -35,29 +36,61 @@ func (s UnameScanner) GetServerDetails(ctx context.Context, ip string) (*Details
if o.Identity != "" {
identity = o.Identity
}

if o.Port != "" {
port = o.Port
}
}
}

cmd := exec.Command("ssh", "-i", identity, user+"@"+ip, "uname -a")

unameOutput, err := cmd.Output()
unameCmd := exec.Command(
"ssh",
"-i",
identity,
"-p",
port,
"-o",
"BatchMode=yes",
"-o",
"StrictHostKeyChecking=no",
"-l",
user,
ip,
"uname -a",
)

unameOutput, err := unameCmd.Output()

if err != nil {
return nil, err
}

info := strings.Split(string(unameOutput), " ")

os := info[0]
operatingSystem := info[0]
hostname := info[1]

switch os {
switch operatingSystem {
case "Darwin":
os = "MacOS"
operatingSystem = "MacOS"
case "Linux":
cmd = exec.Command("ssh", "-i", identity, user+"@"+ip, "cat /etc/os-release")

osReleaseOutput, err := cmd.Output()
osReleaseCmd := exec.Command(
"ssh",
"-i",
identity,
"-p",
port,
"-o",
"BatchMode=yes",
"-o",
"StrictHostKeyChecking=no",
"-l",
user,
ip,
"cat /etc/os-release",
)

osReleaseOutput, err := osReleaseCmd.Output()

if err != nil {
return nil, err
Expand All @@ -67,13 +100,13 @@ func (s UnameScanner) GetServerDetails(ctx context.Context, ip string) (*Details

for i, name := range osReleaseRegexp.SubexpNames() {
if name == "os" {
os = match[i]
operatingSystem = match[i]
}
}
}

return &Details{
Hostname: hostname,
OS: os,
OS: operatingSystem,
}, nil
}
Loading

0 comments on commit 7565be3

Please sign in to comment.