-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ssh client and device inspection
- Loading branch information
Showing
10 changed files
with
277 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package inspect | ||
|
||
import ( | ||
"bwizard/internal/pkg/ssh" | ||
ssh2 "golang.org/x/crypto/ssh" | ||
) | ||
|
||
type DeviceInspection struct { | ||
Hostname string | ||
CPU string | ||
Ram string | ||
} | ||
|
||
// Device inspects a device regarding hostname, cpu, memory and disk size | ||
func Device(client *ssh.Client) (*DeviceInspection, error) { | ||
session, err := client.NewSession() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer func(session *ssh2.Session) { | ||
err := session.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
}(session) | ||
|
||
hostnameCmd := "hostname" | ||
hostnameOutput, err := session.CombinedOutput(hostnameCmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
cpuCmd := "lscpu | grep 'Model name' | cut -f 2 -d \":\" | awk '{$1=$1}1'" | ||
cpuOutput, err := session.CombinedOutput(cpuCmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ramCmd := "cat /proc/meminfo | grep 'MemTotal' | cut -f 2 -d \":\" | awk '{$1=$1}1'" | ||
ramOutput, err := session.CombinedOutput(ramCmd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &DeviceInspection{ | ||
Hostname: string(hostnameOutput), | ||
CPU: string(cpuOutput), | ||
Ram: string(ramOutput), | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ssh | ||
|
||
import ( | ||
"golang.org/x/crypto/ssh" | ||
"net" | ||
"os" | ||
"strconv" | ||
) | ||
|
||
type Credentials struct { | ||
Username string | ||
PrivateKeyPath string | ||
} | ||
|
||
type Client struct { | ||
host string | ||
port uint16 | ||
config *ssh.ClientConfig | ||
client *ssh.Client | ||
} | ||
|
||
// NewClient returns a new SSH client | ||
func NewClient(host string, port uint16, credentials *Credentials) (*Client, error) { | ||
privateKey, err := os.ReadFile(credentials.PrivateKeyPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
signer, err := ssh.ParsePrivateKey(privateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config := &ssh.ClientConfig{ | ||
User: credentials.Username, | ||
Auth: []ssh.AuthMethod{ | ||
ssh.PublicKeys(signer), | ||
}, | ||
// TODO: Only use that for development. Implement a proper host key check | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), | ||
} | ||
|
||
return &Client{ | ||
host: host, | ||
port: port, | ||
config: config, | ||
}, nil | ||
} | ||
|
||
// Connect starts a SSH session to the server | ||
func (c *Client) Connect() error { | ||
client, err := ssh.Dial("tcp", net.JoinHostPort(c.host, strconv.Itoa(int(c.port))), c.config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.client = client | ||
|
||
return nil | ||
} | ||
|
||
// Close closes the connection to the server | ||
func (c *Client) Close() error { | ||
if c.client == nil { | ||
return nil | ||
} | ||
|
||
return c.client.Close() | ||
} | ||
|
||
// NewSession returns a new session to run commands in | ||
func (c *Client) NewSession() (*ssh.Session, error) { | ||
return c.client.NewSession() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package device | ||
|
||
import ( | ||
"bwizard/internal/pkg/inspect" | ||
"bwizard/internal/pkg/ssh" | ||
deviceSvc "bwizard/internal/pkg/wizard/domain/svc/device" | ||
deviceValue "bwizard/internal/pkg/wizard/domain/valueobject/device" | ||
) | ||
|
||
type InspectionSvcImpl struct { | ||
} | ||
|
||
var _ deviceSvc.InspectionService = &InspectionSvcImpl{} | ||
|
||
// Inspect gains information from a backup device such like the operating system or the agent | ||
func (i *InspectionSvcImpl) Inspect(ips []deviceValue.IPAddress) (*deviceValue.Inspection, error) { | ||
credentials := ssh.Credentials{ | ||
Username: "root", | ||
} | ||
|
||
var clients []*ssh.Client | ||
for _, ip := range ips { | ||
|
||
client, err := ssh.NewClient(string(ip), 22, &credentials) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := client.Connect(); err != nil { | ||
continue | ||
} | ||
|
||
clients = append(clients, client) | ||
|
||
inspection, err := inspect.Device(client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return deviceValue.NewInspectionFromDeviceInspection(inspection), nil | ||
} | ||
|
||
defer func(clients []*ssh.Client) { | ||
for _, client := range clients { | ||
err := client.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
}(clients) | ||
|
||
return nil, nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package device | ||
|
||
import ( | ||
"bwizard/internal/pkg/wizard/domain/valueobject/device" | ||
"github.com/google/uuid" | ||
"time" | ||
) | ||
|
||
type Device struct { | ||
ID uuid.UUID `db:"id"` | ||
Name string `db:"name"` | ||
Kind device.Kind `db:"kind"` | ||
Protection device.ProtectionStatus `db:"protection"` | ||
LastBackup *time.Time `db:"last_backup" goqu:"omitempty"` | ||
IPs []device.IPAddress `db:"ips"` | ||
Agent string `db:"agent"` | ||
CreatedAt time.Time `db:"created_at"` | ||
UpdatedAt time.Time `db:"updated_at"` | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package device | ||
|
||
import ( | ||
"bwizard/internal/pkg/wizard/domain/entity/device" | ||
"context" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type Repository interface { | ||
GetDeviceByID(ctx context.Context, id uuid.UUID) (*device.Device, error) | ||
SaveDevice(ctx context.Context, device *device.Device) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package device | ||
|
||
import ( | ||
"bwizard/internal/pkg/wizard/domain/valueobject/device" | ||
) | ||
|
||
type InspectionService interface { | ||
Inspect(ips []device.IPAddress) (*device.Inspection, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package device | ||
|
||
import "bwizard/internal/pkg/inspect" | ||
|
||
type ProtectionStatus string | ||
type Kind string | ||
type IPAddress string | ||
|
||
const ( | ||
ProtectionStatusOk ProtectionStatus = "ok" | ||
ProtectionStatusWarning ProtectionStatus = "warning" | ||
ProtectionStatusError ProtectionStatus = "error" | ||
|
||
KindServer Kind = "server" | ||
KindComputer Kind = "computer" | ||
KindMobile Kind = "mobile" | ||
) | ||
|
||
type OperatingSystem struct { | ||
Name string `db:"name"` | ||
CPU string `db:"cpu"` | ||
Ram string `db:"ram"` | ||
TotalHardDrive string `db:"ram"` | ||
} | ||
|
||
type Inspection struct { | ||
Hostname string `db:"hostname"` | ||
CPU string `db:"cpu"` | ||
Ram string `db:"ram"` | ||
} | ||
|
||
// NewInspectionFromDeviceInspection returns a new domain inspection from inspect.DeviceInspection | ||
func NewInspectionFromDeviceInspection(inspection *inspect.DeviceInspection) *Inspection { | ||
return &Inspection{ | ||
Hostname: inspection.Hostname, | ||
CPU: inspection.CPU, | ||
Ram: inspection.Ram, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters