feat(yay): Add support for PACMAN_AUTH (#1706)

This commit is contained in:
J Guerreiro 2022-02-08 08:39:41 +00:00 committed by GitHub
parent 7f7b69447d
commit 7f5a060324
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 0 deletions

View file

@ -143,6 +143,14 @@ func (c *Configuration) String() string {
// check privilege elevator exists otherwise try to find another one.
func (c *Configuration) setPrivilegeElevator() error {
if auth := os.Getenv("PACMAN_AUTH"); auth != "" {
c.SudoBin = auth
if auth != "sudo" {
c.SudoFlags = ""
c.SudoLoop = false
}
}
for _, bin := range [...]string{c.SudoBin, "sudo"} {
if _, err := exec.LookPath(bin); err == nil {
c.SudoBin = bin

View file

@ -7,6 +7,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// GIVEN a non existing build dir in the config
@ -221,3 +222,77 @@ func TestConfiguration_setPrivilegeElevator_custom_script(t *testing.T) {
assert.Equal(t, "-v", config.SudoFlags)
assert.True(t, config.SudoLoop)
}
// GIVEN default config and sudo loop enabled
// GIVEN doas as PACMAN_AUTH env variable
// WHEN setPrivilegeElevator gets called
// THEN sudobin should be changed to "doas"
func TestConfiguration_setPrivilegeElevator_pacman_auth_doas(t *testing.T) {
oldPath := os.Getenv("PATH")
path, err := os.MkdirTemp("", "yay-test")
require.NoError(t, err)
doas := filepath.Join(path, "doas")
_, err = os.Create(doas)
os.Chmod(doas, 0o755)
require.NoError(t, err)
sudo := filepath.Join(path, "sudo")
_, err = os.Create(sudo)
os.Chmod(sudo, 0o755)
require.NoError(t, err)
defer os.RemoveAll(path)
config := DefaultConfig()
config.SudoBin = "sudo"
config.SudoLoop = true
config.SudoFlags = "-v"
os.Setenv("PACMAN_AUTH", "doas")
os.Setenv("PATH", path)
err = config.setPrivilegeElevator()
os.Setenv("PATH", oldPath)
assert.NoError(t, err)
assert.Equal(t, "doas", config.SudoBin)
assert.Equal(t, "", config.SudoFlags)
assert.False(t, config.SudoLoop)
}
// GIVEN config with doas configed and sudo loop enabled
// GIVEN sudo as PACMAN_AUTH env variable
// WHEN setPrivilegeElevator gets called
// THEN sudobin should be changed to "sudo"
func TestConfiguration_setPrivilegeElevator_pacman_auth_sudo(t *testing.T) {
oldPath := os.Getenv("PATH")
path, err := os.MkdirTemp("", "yay-test")
require.NoError(t, err)
doas := filepath.Join(path, "doas")
_, err = os.Create(doas)
os.Chmod(doas, 0o755)
require.NoError(t, err)
sudo := filepath.Join(path, "sudo")
_, err = os.Create(sudo)
os.Chmod(sudo, 0o755)
require.NoError(t, err)
defer os.RemoveAll(path)
config := DefaultConfig()
config.SudoBin = "doas"
config.SudoLoop = true
config.SudoFlags = "-v"
os.Setenv("PACMAN_AUTH", "sudo")
os.Setenv("PATH", path)
err = config.setPrivilegeElevator()
os.Setenv("PATH", oldPath)
assert.NoError(t, err)
assert.Equal(t, "sudo", config.SudoBin)
assert.Equal(t, "-v", config.SudoFlags)
assert.True(t, config.SudoLoop)
}