-
Notifications
You must be signed in to change notification settings - Fork 1
/
host.go
110 lines (94 loc) · 3.52 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package host
import (
"errors"
"fmt"
"net"
"strings"
"github.com/ScoreTrak/ScoreTrak/pkg/service"
"github.com/asaskevich/govalidator"
"github.com/gofrs/uuid"
"gorm.io/gorm"
)
// Host model represents a single machine. This could be an IP address or a resolvable hostname
type Host struct {
ID uuid.UUID `json:"id,omitempty" gorm:"type:uuid;primary_key;"`
// Address represents the hostname or an IP address of the remote computer
Address string `json:"address" gorm:"not null;default:null"`
// Comma Separated List of allowed CIDRs, and hostnames
AddressListRange *string `json:"address_list_range" gorm:"not null;default:''"`
// The ID of a host group that the host belongs to.
HostGroupID *uuid.UUID `json:"host_group_id,omitempty" gorm:"type:uuid"`
// The ID of a team that this host belongs too.
TeamID uuid.UUID `json:"team_id,omitempty" gorm:"type:uuid;not null"`
// Hide is responsible for hiding the host on the scoring table
Hide *bool `json:"pause,omitempty" gorm:"not null;default:false"`
// Pause is responsible for pausing the host on scoring table
Pause *bool `json:"hide,omitempty" gorm:"not null;default:false"`
// Enables to Edit the hostname. If a single host needs to be eddited for one check_service, and kept only visible for other check_service, you can make 2 services that point to same address, and have different edit_host properties.
EditHost *bool `json:"edit_host,omitempty" gorm:"not null;default:false"`
// Services is an aggregate of all child Services that belong to a host
Services []*service.Service `json:"services,omitempty" gorm:"foreignkey:HostID;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT"`
}
var ErrInvalidHostNameOrIPAddress = errors.New("invalid Hostname, or IP address")
func (p *Host) BeforeSave(tx *gorm.DB) (err error) {
if p.AddressListRange != nil || p.Address != "" {
p.Address = strings.ReplaceAll(p.Address, " ", "")
if (p.AddressListRange == nil || p.Address == "") && p.ID != uuid.Nil {
hst := &Host{}
err := tx.Where("id = ?", p.ID).First(hst).Error
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return fmt.Errorf("unable to retrieve the requested entry, in order to validate address: %w", err)
}
if p.AddressListRange == nil {
p.AddressListRange = hst.AddressListRange
}
if p.Address == "" {
p.Address = hst.Address
}
}
if !govalidator.IsHost(p.Address) {
return fmt.Errorf("%w: %s", ErrInvalidHostNameOrIPAddress, p.Address)
}
if p.AddressListRange != nil {
*p.AddressListRange = strings.ReplaceAll(*p.AddressListRange, " ", "")
err = validateIfAddressInRange(p.Address, *p.AddressListRange)
if err != nil {
return err
}
}
}
return nil
}
func (p *Host) BeforeCreate(tx *gorm.DB) (err error) {
if p.ID == uuid.Nil {
u, err := uuid.NewV4()
if err != nil {
return err
}
p.ID = u
}
return nil
}
var ErrAddressNotInRange = errors.New("the provided ip address was not in allowed range")
var ErrNotAValidHostname = errors.New("not a valid hostname")
func validateIfAddressInRange(addr string, addresses string) (err error) {
if addresses == "" {
return nil
}
addressList := strings.Split(addresses, ",")
for i := range addressList {
switch _, network, err := net.ParseCIDR(addressList[i]); {
case err == nil:
if network.Contains(net.ParseIP(addr)) {
return nil
}
case govalidator.IsHost(addressList[i]):
if strings.EqualFold(addressList[i], addr) {
return nil
}
default:
return fmt.Errorf("%w: %s", ErrNotAValidHostname, addressList[i])
}
}
return ErrAddressNotInRange
}