You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-11-06 09:09:19 +02:00
feat(cnbBuild): support builders with different CNB user ids (#4625)
Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>
This commit is contained in:
32
pkg/cnbutils/user.go
Normal file
32
pkg/cnbutils/user.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package cnbutils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func CnbUserInfo() (int, int, error) {
|
||||
uidStr, ok := os.LookupEnv("CNB_USER_ID")
|
||||
if !ok {
|
||||
return 0, 0, errors.New("environment variable CNB_USER_ID not found")
|
||||
}
|
||||
|
||||
gidStr, ok := os.LookupEnv("CNB_GROUP_ID")
|
||||
if !ok {
|
||||
return 0, 0, errors.New("environment variable CNB_GROUP_ID not found")
|
||||
}
|
||||
|
||||
uid, err := strconv.Atoi(uidStr)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
gid, err := strconv.Atoi(gidStr)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
return uid, gid, nil
|
||||
}
|
||||
@@ -42,6 +42,7 @@ type runner interface {
|
||||
type ExecRunner interface {
|
||||
runner
|
||||
RunExecutable(executable string, params ...string) error
|
||||
RunExecutableWithAttrs(executable string, sysProcAttr *syscall.SysProcAttr, params ...string) error
|
||||
RunExecutableInBackground(executable string, params ...string) (Execution, error)
|
||||
}
|
||||
|
||||
@@ -127,9 +128,18 @@ func (c *Command) RunShell(shell, script string) error {
|
||||
//
|
||||
// Thus the executable needs to be on the PATH of the current process and it is not sufficient to alter the PATH on cmd.Env.
|
||||
func (c *Command) RunExecutable(executable string, params ...string) error {
|
||||
return c.RunExecutableWithAttrs(executable, nil, params...)
|
||||
}
|
||||
|
||||
// RunExecutableWithAttrs runs the specified executable with parameters and as a specified UID and GID
|
||||
// !! While the cmd.Env is applied during command execution, it is NOT involved when the actual executable is resolved.
|
||||
//
|
||||
// Thus the executable needs to be on the PATH of the current process and it is not sufficient to alter the PATH on cmd.Env.
|
||||
func (c *Command) RunExecutableWithAttrs(executable string, sysProcAttr *syscall.SysProcAttr, params ...string) error {
|
||||
c.prepareOut()
|
||||
|
||||
cmd := ExecCommand(executable, params...)
|
||||
cmd.SysProcAttr = sysProcAttr
|
||||
|
||||
if len(c.dir) > 0 {
|
||||
cmd.Dir = c.dir
|
||||
|
||||
@@ -511,6 +511,10 @@ func (f *FilesMock) Chmod(path string, mode os.FileMode) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FilesMock) Chown(path string, uid, gid int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *FilesMock) Abs(path string) (string, error) {
|
||||
f.init()
|
||||
return f.toAbsPath(path), nil
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"regexp"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/SAP/jenkins-library/pkg/command"
|
||||
)
|
||||
@@ -25,10 +26,11 @@ type ExecMockRunner struct {
|
||||
}
|
||||
|
||||
type ExecCall struct {
|
||||
Execution *Execution
|
||||
Async bool
|
||||
Exec string
|
||||
Params []string
|
||||
Execution *Execution
|
||||
SysProcAttrs *syscall.SysProcAttr
|
||||
Async bool
|
||||
Exec string
|
||||
Params []string
|
||||
}
|
||||
|
||||
type Execution struct {
|
||||
@@ -61,8 +63,11 @@ func (m *ExecMockRunner) AppendEnv(e []string) {
|
||||
}
|
||||
|
||||
func (m *ExecMockRunner) RunExecutable(e string, p ...string) error {
|
||||
return m.RunExecutableWithAttrs(e, nil, p...)
|
||||
}
|
||||
|
||||
exec := ExecCall{Exec: e, Params: p}
|
||||
func (m *ExecMockRunner) RunExecutableWithAttrs(e string, attrs *syscall.SysProcAttr, p ...string) error {
|
||||
exec := ExecCall{Exec: e, SysProcAttrs: attrs, Params: p}
|
||||
m.Calls = append(m.Calls, exec)
|
||||
|
||||
c := strings.Join(append([]string{e}, p...), " ")
|
||||
|
||||
@@ -31,6 +31,7 @@ type FileUtils interface {
|
||||
FileRemove(path string) error
|
||||
MkdirAll(path string, perm os.FileMode) error
|
||||
Chmod(path string, mode os.FileMode) error
|
||||
Chown(path string, uid, gid int) error
|
||||
Glob(pattern string) (matches []string, err error)
|
||||
Chdir(path string) error
|
||||
TempDir(string, string) (string, error)
|
||||
@@ -144,6 +145,17 @@ func (f Files) Chmod(path string, mode os.FileMode) error {
|
||||
return os.Chmod(path, mode)
|
||||
}
|
||||
|
||||
// Chown is a recursive wrapper for os.Chown().
|
||||
func (f Files) Chown(path string, uid, gid int) error {
|
||||
return filepath.WalkDir(path, func(name string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.Chown(name, uid, gid)
|
||||
})
|
||||
}
|
||||
|
||||
// Unzip will decompress a zip archive, moving all files and folders
|
||||
// within the zip file (parameter 1) to an output directory (parameter 2).
|
||||
// from https://golangcode.com/unzip-files-in-go/ with the following license:
|
||||
|
||||
Reference in New Issue
Block a user