-
Notifications
You must be signed in to change notification settings - Fork 0
/
checks.go
262 lines (229 loc) · 6.43 KB
/
checks.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package chkok
import (
"errors"
"fmt"
"net"
"os"
"syscall"
"time"
)
// Status is status of a given check (use Status* contants)
type Status uint8
const (
// StatusUnknown is when a check has not started
StatusUnknown Status = iota
// StatusRunning is when a check started to run
StatusRunning
// StatusStopped is when a check stopped before completion (timeout, etc.)
StatusStopped
// StatusDone is when a check successfully ran
StatusDone
)
// FileType is the type of a file resources, use Type* contants
type FileType uint8
const (
// TypeAny is when a file type is not important
TypeAny FileType = iota
// TypeFile is used when a path should be a regular file
TypeFile
// TypeDir is used when a path should be a directory
TypeDir
)
// Result is the results of a Check
type Result struct {
IsOK bool
Issues []error
}
// Check is the interface that all checks implement
type Check interface {
Suite() string
Name() string
Run() Result
Result() Result
Status() Status
}
// TimedCheck is the interface for checks that accept a timeout
type TimedCheck interface {
GetTimeout() time.Duration
SetTimeout(t time.Duration)
}
// CheckSuites is list of checks, grouped by suite name
type CheckSuites map[string][]Check
type baseCheck struct {
suite string
name string
status Status
result Result
}
func (bc *baseCheck) Suite() string {
return bc.suite
}
func (bc *baseCheck) Name() string {
return bc.name
}
func (bc *baseCheck) Result() Result {
return bc.result
}
func (bc *baseCheck) Status() Status {
return bc.status
}
// CheckFile checks for file/dir existence/type/uid/gid/size
type CheckFile struct {
baseCheck
path string
fileType FileType
uid int32 // -1 to skip
gid int32 // -1 to skip
absent bool
minSize int32 // -1 to sktip
maxSize int64 // 0 to skip
}
// NewCheckFile returns a new checkFile without a uid/gid/size checks
func NewCheckFile(path string) *CheckFile {
return &CheckFile{path: path, fileType: TypeAny, uid: -1, gid: -1, absent: false, minSize: -1, maxSize: -1}
}
func (chk *CheckFile) typeString() string {
switch chk.fileType {
case TypeFile:
return "file"
case TypeDir:
return "dir"
}
return "any"
}
// Name returns the unique name of the check
func (chk *CheckFile) Name() string {
return fmt.Sprintf("%v:%v", chk.typeString(), chk.path)
}
// Run runs the check
func (chk *CheckFile) Run() Result {
if chk.path == "" {
panic("check file path is empty")
}
chk.status = StatusRunning
chk.result = Result{IsOK: true, Issues: []error{}}
finfo, err := os.Lstat(chk.path)
if chk.absent { // file is not there
chk.status = StatusDone
return chk.result
}
if err != nil {
chk.result.IsOK = false
chk.result.Issues = append(chk.result.Issues, err)
chk.status = StatusDone
return chk.result
}
var fstat *syscall.Stat_t = finfo.Sys().(*syscall.Stat_t)
switch chk.fileType {
case TypeDir:
if !finfo.IsDir() {
chk.result.IsOK = false
chk.result.Issues = append(chk.result.Issues, errors.New("is not a directory"))
}
case TypeFile:
if !finfo.Mode().IsRegular() {
chk.result.IsOK = false
chk.result.Issues = append(chk.result.Issues, errors.New("is not a regular file"))
}
}
chk.checkUIDGID(fstat, &chk.result)
chk.checkSize(finfo.Size(), &chk.result)
chk.status = StatusDone
return chk.result
}
// CheckFile.checkUIDGID checks for file uid/gid attrs updates the provided result
func (chk *CheckFile) checkUIDGID(fstat *syscall.Stat_t, result *Result) {
if chk.uid > -1 {
if fstat == nil {
result.IsOK = false
result.Issues = append(result.Issues, fmt.Errorf("check for file owner is not supported on this system"))
} else if uint32(chk.uid) != fstat.Uid {
result.IsOK = false
result.Issues = append(result.Issues, fmt.Errorf("owner mismatch. want %v got %v", chk.uid, fstat.Uid))
}
}
if chk.gid > -1 {
if fstat == nil {
result.IsOK = false
result.Issues = append(result.Issues, fmt.Errorf("check for file group is not supported on this system"))
} else if uint32(chk.gid) != fstat.Gid {
result.IsOK = false
result.Issues = append(result.Issues, fmt.Errorf("group mismatch. want %v got %v", chk.gid, fstat.Gid))
}
}
}
// checkSize checks for file min/max size and updates the provided result
func (chk *CheckFile) checkSize(size int64, result *Result) {
if chk.minSize > -1 && size <= int64(chk.minSize) {
result.IsOK = false
result.Issues = append(result.Issues, fmt.Errorf(
"file too small, size %v is less than min size %v", size, chk.minSize))
}
if chk.maxSize > -1 && size >= chk.maxSize {
result.IsOK = false
result.Issues = append(result.Issues, fmt.Errorf(
"file too large, size %v is more than max size %v", size, chk.maxSize))
}
}
// CheckDial checks for a net resource by dialing
type CheckDial struct {
baseCheck
Network string
Address string
Absent bool
timeout time.Duration
}
// NewCheckDial returns a checkDial for local http availability by default
func NewCheckDial() *CheckDial {
timeout, err := time.ParseDuration("5s")
if err != nil {
panic("err creating check dial default timeout")
}
chk := CheckDial{Network: "tcp", Address: "127.0.0.1:80", Absent: false}
chk.SetTimeout(timeout)
return &chk
}
// Name returns the unique name of the check
func (chk *CheckDial) Name() string {
return fmt.Sprintf("%v:%v", chk.Network, chk.Address)
}
// GetTimeout gets the max duration for the check to timeout
func (chk *CheckDial) GetTimeout() time.Duration {
return chk.timeout
}
// SetTimeout sets the max duration for the check to timeout
func (chk *CheckDial) SetTimeout(timeout time.Duration) {
chk.timeout = timeout
}
// Run runs the check and returns the results
func (chk *CheckDial) Run() Result {
if chk.Network == "" {
panic("check dial network is empty")
}
if chk.Address == "" {
panic("check dial address is empty")
}
start := time.Now()
chk.result = Result{IsOK: true, Issues: []error{}}
conn, err := net.DialTimeout(chk.Network, chk.Address, chk.timeout)
if err != nil { // no connection
if chk.Absent {
chk.status = StatusDone
return chk.result
}
chk.result.IsOK = false
chk.result.Issues = append(chk.result.Issues, err)
chk.status = StatusDone
return chk.result
}
defer conn.Close()
elapsed := time.Since(start)
if elapsed > chk.timeout {
chk.status = StatusStopped
chk.result.IsOK = false
chk.result.Issues = append(chk.result.Issues, fmt.Errorf("check dial timed out after %v seconds", elapsed.Seconds()))
} else {
chk.status = StatusDone
}
return chk.result
}