-
Notifications
You must be signed in to change notification settings - Fork 20
/
filesystem.go
265 lines (234 loc) · 6.98 KB
/
filesystem.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
263
264
265
package checks
import (
"errors"
"os"
"regexp"
"strings"
"github.com/CiscoCloud/distributive/chkutil"
"github.com/CiscoCloud/distributive/errutil"
"github.com/CiscoCloud/distributive/fsstatus"
"github.com/CiscoCloud/distributive/tabular"
log "github.com/Sirupsen/logrus"
)
type fileCondition func(path string) (bool, error)
// isType checks if the resource at path is of the type specified by name by
// passing path to checker. Mostly used to abstract Directory, File, Symlink.
func isType(name string, checker fileCondition, path string) (int, string, error) {
boo, err := checker(path)
if os.IsNotExist(err) {
return 1, "No such file or directory: " + path, nil
} else if os.IsPermission(err) {
return 1, "", errors.New("Insufficient Permissions to read: " + path)
} else if boo {
return errutil.Success()
}
return 1, "Is not a " + name + ": " + path, nil
}
/*
#### file
Description: Does this regular file exist?
Parameters:
- Path (filepath): Path to file
Example parameters:
- "/var/mysoftware/config.file", "/foo/bar/baz"
*/
type File struct{ path string }
func init() {
chkutil.Register("File", func() chkutil.Check {
return &File{}
})
chkutil.Register("Directory", func() chkutil.Check {
return &Directory{}
})
chkutil.Register("Symlink", func() chkutil.Check {
return &Symlink{}
})
chkutil.Register("Permissions", func() chkutil.Check {
return &Permissions{}
})
chkutil.Register("Checksum", func() chkutil.Check {
return &Checksum{}
})
chkutil.Register("FileMatches", func() chkutil.Check {
return &FileMatches{}
})
}
func (chk File) New(params []string) (chkutil.Check, error) {
if len(params) != 1 {
return chk, errutil.ParameterLengthError{1, params}
}
chk.path = params[0]
return chk, nil
}
func (chk File) Status() (int, string, error) {
return isType("file", fsstatus.IsFile, chk.path)
}
/*
#### directory
Description: Does this regular directory exist?
Parameters:
- Path (filepath): Path to directory
Example parameters:
- "/var/run/mysoftware.d/", "/foo/bar/baz/"
*/
type Directory struct{ path string }
func (chk Directory) New(params []string) (chkutil.Check, error) {
if len(params) != 1 {
return chk, errutil.ParameterLengthError{1, params}
}
chk.path = params[0]
return chk, nil
}
func (chk Directory) Status() (int, string, error) {
return isType("directory", fsstatus.IsDirectory, chk.path)
}
/*
#### symlink
Description: Does this symlink exist?
Parameters:
- Path (filepath): Path to symlink
Example parameters:
- "/var/run/mysoftware.d/", "/foo/bar/baz", "/bin/sh"
*/
type Symlink struct{ path string }
func (chk Symlink) New(params []string) (chkutil.Check, error) {
if len(params) != 1 {
return chk, errutil.ParameterLengthError{1, params}
}
chk.path = params[0]
return chk, nil
}
func (chk Symlink) Status() (int, string, error) {
return isType("symlink", fsstatus.IsSymlink, chk.path)
}
/*
#### checksum
Description: Does this file match the expected checksum when using the specified
algorithm?
Parameters:
- Algorithm (string): MD5 | SHA1 | SHA224 | SHA256 | SHA384 | SHA512 |
SHA3224 | SHA3256 | SHA3384 | SHA3512
- Expected checksum (checksum/string)
- Path (filepath): Path to file to check the checksum of
Example parameters:
- MD5, SHA1, SHA224, SHA256, SHA384, SHA512, SHA3224, SHA3256, SHA3384,
- d41d8cd98f00b204e9800998ecf8427e, c6cf669dbd4cf2fbd59d03cc8039420a48a037fe
- /dev/null, /etc/config/important-file.conf
*/
type Checksum struct{ algorithm, expectedChksum, path string }
func (chk Checksum) ID() string { return "checksum" }
func (chk Checksum) New(params []string) (chkutil.Check, error) {
if len(params) != 3 {
return chk, errutil.ParameterLengthError{3, params}
}
valid := []string{"MD5", "SHA1", "SHA224", "SHA256", "SHA384", "SHA512"}
if !tabular.StrIn(strings.ToUpper(params[0]), valid) {
return chk, errutil.ParameterTypeError{params[0], "algorithm"}
}
chk.algorithm = params[0]
path := params[2]
chk.path = path
// TODO validate length of checksum string
chk.expectedChksum = params[1]
return chk, nil
}
func (chk Checksum) Status() (int, string, error) {
if _, err := os.Stat(chk.path); err != nil {
return 2, "", err
}
// getFileChecksum is self-explanatory
fileChecksum := func(algorithm string, path string) string {
if path == "" {
log.Fatal("getFileChecksum got a blank path")
} else if _, err := os.Stat(chk.path); err != nil {
log.WithFields(log.Fields{
"path": chk.path,
}).Fatal("fileChecksum got an invalid path")
}
// we already validated the aglorithm
chksum, _ := fsstatus.Checksum(algorithm, chkutil.FileToBytes(path))
return chksum
}
actualChksum := fileChecksum(chk.algorithm, chk.path)
if actualChksum == chk.expectedChksum {
return errutil.Success()
}
msg := "Checksums do not match for file: " + chk.path
return errutil.GenericError(msg, chk.expectedChksum, []string{actualChksum})
}
/*
#### FileMatches
Description: Does this file match this regexp?
Parameters:
- Path (filepath): Path to file to check the contents of
- Regexp (regexp): Regexp to query file with
Example parameters:
- /dev/null, /etc/config/important-file.conf
- "str", "myvalue=expected", "IP=\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}"
*/
type FileMatches struct {
path string
re *regexp.Regexp
}
func (chk FileMatches) New(params []string) (chkutil.Check, error) {
if len(params) != 2 {
return chk, errutil.ParameterLengthError{2, params}
}
re, err := regexp.Compile(params[1])
if err != nil {
return chk, errutil.ParameterTypeError{params[1], "regexp"}
}
chk.re = re
path := params[0]
chk.path = path
return chk, nil
}
func (chk FileMatches) Status() (int, string, error) {
if _, err := os.Stat(chk.path); err != nil {
return 2, "", err
}
if chk.re.Match(chkutil.FileToBytes(chk.path)) {
return errutil.Success()
}
msg := "File does not match regexp:"
msg += "\n\tFile: " + chk.path
msg += "\n\tRegexp: " + chk.re.String()
return 1, msg, nil
}
/*
#### Permissions
Description: Does this file have the given Permissions?
Parameters:
- Path (filepath): Path to file to check the Permissions of
- Mode (filemode): Filemode to expect
Example parameters:
- /dev/null, /etc/config/important-file.conf
- -rwxrwxrwx, -rw-rw---- -rw-------, -rwx-r-x-r-x
*/
type Permissions struct{ path, expectedPerms string }
func (chk Permissions) New(params []string) (chkutil.Check, error) {
if len(params) != 2 {
return chk, errutil.ParameterLengthError{2, params}
}
mode := params[1]
modeRe := `-([r-][w-][x-]){3}`
if len(mode) != 10 || !regexp.MustCompile(modeRe).MatchString(mode) {
return chk, errutil.ParameterTypeError{mode, "filemode"}
}
chk.path = params[0]
chk.expectedPerms = mode
return chk, nil
}
func (chk Permissions) Status() (int, string, error) {
if _, err := os.Stat(chk.path); err != nil {
return 1, "", err
}
passed, err := fsstatus.FileHasPermissions(chk.expectedPerms, chk.path)
if err != nil {
return 1, "", err
}
if passed {
return errutil.Success()
}
return 1, "File did not have permissions: " + chk.expectedPerms, nil
}