-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
47 lines (40 loc) · 1.01 KB
/
helpers.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
package main
import (
"fmt"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"syscall"
)
func checkFileExists(filePath string) bool {
_, err := os.Stat(filePath)
return !os.IsNotExist(err)
}
func chown(f *os.File, user *user.User) error {
uid, _ := strconv.Atoi(user.Uid)
gid, _ := strconv.Atoi(user.Gid)
return f.Chown(uid, gid)
}
func runAs(cmd *exec.Cmd, user *user.User) {
uid, _ := strconv.ParseInt(user.Uid, 10, 32)
gid, _ := strconv.ParseInt(user.Gid, 10, 32)
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}
cmd.Env = append(cmd.Env, fmt.Sprintf("HOME=%s", user.HomeDir))
}
var restrictedEnvVars = map[string]bool{
"AUDIT_UPLOAD_URL": true,
}
// Removes any restricted keys from the parent environment
func filterEnv(o []string) []string {
environ := []string{}
for _, e := range o {
key, _, _ := strings.Cut(e, "=")
if _, found := restrictedEnvVars[key]; !found {
environ = append(environ, e)
}
}
return environ
}