-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathpaths.go
65 lines (58 loc) · 2.05 KB
/
paths.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
package filesystem
import (
"fmt"
"io/fs"
"kitops/pkg/lib/constants"
"os"
"path/filepath"
"strings"
)
// VerifySubpath checks that path.Join(context, subDir) is a subdirectory of context, following
// symlinks if present.
func VerifySubpath(context, subDir string) (absPath string, err error) {
// Get absolute path for context and context + subDir
absContext, err := filepath.Abs(context)
if err != nil {
return "", fmt.Errorf("failed to resolve absolute path for %s: %w", context, err)
}
fullPath := filepath.Clean(filepath.Join(absContext, subDir))
// Get actual paths, ignoring symlinks along the way
resolvedContext, err := filepath.EvalSymlinks(absContext)
if err != nil {
return "", fmt.Errorf("error resolving %s: %w", absContext, err)
}
resolvedFullPath, err := filepath.EvalSymlinks(fullPath)
if err != nil {
return "", fmt.Errorf("error resolving %s: %w", absContext, err)
}
// Get relative path between context and the full path to check if the
// actual full, absolute path is a subdirectory of context
relPath, err := filepath.Rel(resolvedContext, resolvedFullPath)
if err != nil {
return "", fmt.Errorf("failed to get relative path: %w", err)
}
if strings.Contains(relPath, "..") {
return "", fmt.Errorf("paths must be within context directory")
}
return resolvedFullPath, nil
}
func PathExists(path string) (fs.FileInfo, bool) {
fi, err := os.Stat(path)
if err != nil && os.IsNotExist(err) {
return nil, false
}
return fi, true
}
// Searches for a kit file in the given context directory.
// It checks for accepted kitfile names and returns the path of the first found kitfile.
// If no kitfile is found, it returns the path (contextDir + kitfile)
// of the default kitfile.
func FindKitfileInPath(contextDir string) string {
var defaultKitFileNames = []string{"Kitfile", "kitfile", ".kitfile"}
for _, fileName := range defaultKitFileNames {
if _, exists := PathExists(filepath.Join(contextDir, fileName)); exists {
return filepath.Join(contextDir, fileName)
}
}
return filepath.Join(contextDir, constants.DefaultKitFileName)
}