-
Notifications
You must be signed in to change notification settings - Fork 458
/
validate.go
116 lines (92 loc) · 2.76 KB
/
validate.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package extensions
import (
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/blang/semver/v4"
"github.com/siderolabs/talos/pkg/machinery/extensions"
"github.com/siderolabs/talos/pkg/machinery/version"
)
// Validate the extension: compatibility, contents, etc.
func (ext *Extension) Validate() error {
if err := ext.validateConstraints(); err != nil {
return err
}
return ext.validateContents()
}
func (ext *Extension) validateConstraints() error {
constraint := ext.Manifest.Metadata.Compatibility.Talos.Version
if constraint != "" {
talosVersion, err := semver.ParseTolerant(version.Tag)
if err != nil {
return err
}
versionConstraint, err := semver.ParseRange(trim(constraint))
if err != nil {
return fmt.Errorf("error parsing Talos version constraint: %w", err)
}
if !versionConstraint(coreVersion(talosVersion)) {
return fmt.Errorf("version constraint %s can't be satisfied with Talos version %s", constraint, talosVersion)
}
}
return nil
}
// trim removes 'v' symbol anywhere in string if it's located before the number.
func trim(constraint string) string {
for i := 0; i < len(constraint); i++ {
if constraint[i] == 'v' && i+1 < len(constraint) && constraint[i+1] >= '0' && constraint[i+1] <= '9' {
constraint = constraint[:i] + constraint[i+1:]
}
}
return constraint
}
func coreVersion(talosVersion semver.Version) semver.Version {
return semver.Version{
Major: talosVersion.Major,
Minor: talosVersion.Minor,
Patch: talosVersion.Patch,
}
}
//nolint:gocyclo
func (ext *Extension) validateContents() error {
return filepath.WalkDir(ext.rootfsPath, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
itemPath, err := filepath.Rel(ext.rootfsPath, path)
if err != nil {
return err
}
itemPath = filepath.Join("/", itemPath)
// check for -------w-
if d.Type().Perm()&0o002 > 0 {
return fmt.Errorf("world-writeable files are not allowed: %q", itemPath)
}
// no special files
if !d.IsDir() && !d.Type().IsRegular() && d.Type().Type() != os.ModeSymlink {
return fmt.Errorf("special files are not allowed: %q", itemPath)
}
// regular file: check for file path being whitelisted
if !d.IsDir() {
allowed := false
for _, allowedPath := range extensions.AllowedPaths {
if strings.HasPrefix(itemPath, allowedPath) {
_, err = filepath.Rel(allowedPath, itemPath)
if err == nil {
allowed = true
break
}
}
}
if !allowed {
return fmt.Errorf("path %q is not allowed in extensions", itemPath)
}
}
return nil
})
}