• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

essentialkaos / ek / 24389768914

14 Apr 2026 08:46AM UTC coverage: 99.307% (-0.07%) from 99.372%
24389768914

push

github

web-flow
Merge pull request #645 from essentialkaos/develop

Version 14.0.0

2148 of 2175 new or added lines in 105 files covered. (98.76%)

11 existing lines in 5 files now uncovered.

15196 of 15302 relevant lines covered (99.31%)

98.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.14
/knf/validators/network/validators.go
1
// Package network provides KNF validators for checking items related to network
2
package network
3

4
// ////////////////////////////////////////////////////////////////////////////////// //
5
//                                                                                    //
6
//                         Copyright (c) 2026 ESSENTIAL KAOS                          //
7
//      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
8
//                                                                                    //
9
// ////////////////////////////////////////////////////////////////////////////////// //
10

11
import (
12
        "fmt"
13
        "net"
14
        "net/mail"
15
        "net/url"
16
        "strconv"
17

18
        "github.com/essentialkaos/ek/v14/knf"
19
)
20

21
// ////////////////////////////////////////////////////////////////////////////////// //
22

23
var (
24
        // IP returns error if configuration property isn't a valid IP address
25
        IP = validateIP
26

27
        // Port returns error if configuration property isn't a valid port number
28
        Port = validatePort
29

30
        // MAC returns error if configuration property isn't a valid MAC address
31
        MAC = validateMAC
32

33
        // CIDR returns error if configuration property isn't a valid CIDR address
34
        CIDR = validateCIDR
35

36
        // URL returns error if configuration property isn't a valid URL
37
        URL = validateURL
38

39
        // Mail returns error if configuration property isn't a valid email address
40
        Mail = validateMail
41

42
        // HasIP returns error if system doesn't have interface with IP from configuration
43
        // property
44
        HasIP = validateHasIP
45
)
46

47
// ////////////////////////////////////////////////////////////////////////////////// //
48

49
// validateIP returns error if configuration property isn't a valid IP address
50
func validateIP(config knf.IConfig, prop string, value any) error {
12✔
51
        v := config.GetS(prop)
12✔
52

12✔
53
        if v == "" {
16✔
54
                return nil
4✔
55
        }
4✔
56

57
        ip := net.ParseIP(v)
8✔
58

8✔
59
        if ip == nil {
12✔
60
                return fmt.Errorf("%q is not a valid IP address", v)
4✔
61
        }
4✔
62

63
        return nil
4✔
64
}
65

66
// validatePort returns error if configuration property isn't a valid port number
67
func validatePort(config knf.IConfig, prop string, value any) error {
16✔
68
        v := config.GetS(prop)
16✔
69

16✔
70
        if v == "" {
20✔
71
                return nil
4✔
72
        }
4✔
73

74
        portInt, err := strconv.Atoi(config.GetS(prop))
12✔
75

12✔
76
        if err != nil || portInt < 0 || portInt > 65535 {
20✔
77
                return fmt.Errorf("%q is not a valid port number", v)
8✔
78
        }
8✔
79

80
        return nil
4✔
81
}
82

83
// validateMAC returns error if configuration property isn't a valid MAC address
84
func validateMAC(config knf.IConfig, prop string, value any) error {
12✔
85
        v := config.GetS(prop)
12✔
86

12✔
87
        if v == "" {
16✔
88
                return nil
4✔
89
        }
4✔
90

91
        _, err := net.ParseMAC(v)
8✔
92

8✔
93
        if err != nil {
12✔
94
                return fmt.Errorf("%q is not a valid MAC address: %v", v, err)
4✔
95
        }
4✔
96

97
        return nil
4✔
98
}
99

100
// validateCIDR returns error if configuration property isn't a valid CIDR address
101
func validateCIDR(config knf.IConfig, prop string, value any) error {
12✔
102
        v := config.GetS(prop)
12✔
103

12✔
104
        if v == "" {
16✔
105
                return nil
4✔
106
        }
4✔
107

108
        _, _, err := net.ParseCIDR(v)
8✔
109

8✔
110
        if err != nil {
12✔
111
                return fmt.Errorf("%q is not a valid CIDR address: %v", v, err)
4✔
112
        }
4✔
113

114
        return nil
4✔
115
}
116

117
// validateURL returns error if configuration property isn't a valid URL
118
func validateURL(config knf.IConfig, prop string, value any) error {
12✔
119
        v := config.GetS(prop)
12✔
120

12✔
121
        if v == "" {
16✔
122
                return nil
4✔
123
        }
4✔
124

125
        _, err := url.ParseRequestURI(v)
8✔
126

8✔
127
        if err != nil {
12✔
128
                return fmt.Errorf("%q is not a valid URL address: %v", v, err)
4✔
129
        }
4✔
130

131
        return nil
4✔
132
}
133

134
// validateMail returns error if configuration property isn't a valid email address
135
func validateMail(config knf.IConfig, prop string, value any) error {
16✔
136
        v := config.GetS(prop)
16✔
137

16✔
138
        if v == "" {
20✔
139
                return nil
4✔
140
        }
4✔
141

142
        _, err := mail.ParseAddress(v)
12✔
143

12✔
144
        if err != nil {
16✔
145
                return fmt.Errorf("%q is not a valid email address", v)
4✔
146
        }
4✔
147

148
        return nil
8✔
149
}
150

151
// validateHasIP returns error if system doesn't have interface with IP from configuration
152
// property
153
func validateHasIP(config knf.IConfig, prop string, value any) error {
12✔
154
        v := config.GetS(prop)
12✔
155

12✔
156
        if v == "" {
16✔
157
                return nil
4✔
158
        }
4✔
159

160
        interfaces, err := net.Interfaces()
8✔
161

8✔
162
        if err != nil {
8✔
NEW
163
                return fmt.Errorf("can't get interfaces info for check: %v", err)
×
164
        }
×
165

166
        for _, i := range interfaces {
42✔
167
                addr, err := i.Addrs()
34✔
168

34✔
169
                if err != nil {
34✔
170
                        continue
×
171
                }
172

173
                for _, a := range addr {
66✔
174
                        var ip net.IP
32✔
175

32✔
176
                        switch v := a.(type) {
32✔
177
                        case *net.IPNet:
32✔
178
                                ip = v.IP
32✔
NEW
179
                        case *net.IPAddr:
×
NEW
180
                                ip = v.IP
×
NEW
181
                        default:
×
NEW
182
                                continue
×
183
                        }
184

185
                        if v == ip.String() {
36✔
186
                                return nil
4✔
187
                        }
4✔
188
                }
189
        }
190

191
        return fmt.Errorf("the system does not have an interface with the address %q", v)
4✔
192
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc