security

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2025 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Overview

Package security implements privilege management and execution of privileged actions in security contexts.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoSecurityCtx            = errors.New("security context not found")
	ErrSecurityCtxDataAssertion = errors.New("data type cannot be asserted")
)

Custom errors.

Functions

func DropCapabilities

func DropCapabilities() error

DropCapabilities drops any existing capabilities on the process.

func DropPrivileges

func DropPrivileges(config *Config) error

DropPrivileges will change `root` user to `nobody` and drop any unnecessary privileges only keeping the ones passed in `caps` argument. If current user is not root, this function is no-op and we expect either process or file to have necessary capabilities in the production environments.

func ExecAsUser

func ExecAsUser(data interface{}) error

ExecAsUser executes a subprocess as a given user inside a security context.

Types

type Config

type Config struct {
	RunAsUser      string      // Change to this user if app is started as root
	Caps           []cap.Value // Capabilities necessary for the app
	ReadPaths      []string    // Paths that "RunAsUser" user able to read
	ReadWritePaths []string    // Paths that "RunAsUser" user able to read/write
}

type ExecSecurityCtxData

type ExecSecurityCtxData struct {
	Context context.Context //nolint:containedctx
	Cmd     []string
	Environ []string
	UID     int
	GID     int
	StdOut  []byte
	Logger  *slog.Logger
}

ExecSecurityCtxData contains the input/output data for executing subprocess inside security context.

type SecurityContext

type SecurityContext struct {
	Logger   *slog.Logger
	Launcher *cap.Launcher
	Func     func(interface{}) error
	Caps     []cap.Value
	CapSet   *cap.Set
	Name     string
}

SecurityContext implements a security context where functions can be safely executed with required privileges on a thread locked to OS.

func NewSecurityContext

func NewSecurityContext(
	name string,
	caps []cap.Value,
	f func(interface{}) error,
	logger *slog.Logger,
) (*SecurityContext, error)

NewSecurityContext returns a new instance of SecurityContext.

func (*SecurityContext) Exec

func (s *SecurityContext) Exec(data interface{}) error

Exec executes the function inside the security context and returns error if any.

Jump to

Keyboard shortcuts

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