sudo

package
v2.0.0-beta.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 9, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sudo provides support for various methods of running commands with elevated privileges.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoSudo is returned when no supported sudo method is found.
	ErrNoSudo = errors.New("no supported sudo method found")
	// DefaultRegistry is the default sudo repository.
	DefaultRegistry = sync.OnceValue(func() *Registry {
		provider := NewRegistry()
		RegisterWindowsNoop(provider)
		RegisterUID0Noop(provider)
		RegisterSudo(provider)
		RegisterDoas(provider)
		return provider
	})
)

Functions

func Doas

func Doas(cmd string) string

Doas is a DecorateFunc that will wrap the given command in a doas call.

func Noop

func Noop(cmd string) string

Noop is a DecorateFunc that will return the given command unmodified.

func RegisterDoas

func RegisterDoas(repository *Registry)

RegisterDoas registers a doas DecorateFunc with the given repository.

func RegisterSudo

func RegisterSudo(repository *Registry)

RegisterSudo registers a sudo DecorateFunc with the given repository.

func RegisterUID0Noop

func RegisterUID0Noop(repository *Registry)

RegisterUID0Noop registers a noop DecorateFunc with the given repository which can be used when the user is root.

func RegisterWindowsNoop

func RegisterWindowsNoop(repository *Registry)

RegisterWindowsNoop registers a noop DecorateFunc with the given repository if the current session has effective administrator privileges. IsInRole uses CheckTokenMembership, which returns true only when the Administrators SID is present and not marked deny-only — the correct signal for an effectively elevated token. SSH sessions on Windows always provide a full elevated token for Administrators group members regardless of UAC; WinRM does too for domain accounts or when LocalAccountTokenFilterPolicy=1 is set.

func Sudo

func Sudo(cmd string) string

Sudo is a DecorateFunc that will wrap the given command in a sudo call.

Types

type Factory

type Factory = plumbing.Factory[cmd.Runner, cmd.Runner]

Factory is a factory for sudo runners.

type Provider

type Provider struct {
	// contains filtered or unexported fields
}

Provider provides a unified interface to interact with different sudo methods. It ensures that a suitable sudo runner is lazily initialized and made available for privileged command execution.

func NewSudoProvider

func NewSudoProvider(get RunnerProvider, runner cmd.Runner) *Provider

NewSudoProvider creates a new instance of Provider with the provided RunnerProvider function and runner.

func (*Provider) SudoRunner

func (p *Provider) SudoRunner() (cmd.Runner, error)

SudoRunner returns a cmd.Runner with a sudo decorator or an error if the decorator could not be initialized.

type Registry

type Registry = plumbing.Provider[cmd.Runner, cmd.Runner]

Registry is a repository for sudo runner factories.

func NewRegistry

func NewRegistry() *Registry

NewRegistry returns a new sudo repository.

type RunnerProvider

type RunnerProvider func(cmd.Runner) (cmd.Runner, error)

RunnerProvider is a function that returns a cmd.Runner with elevated privileges given a runner.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL