exec

package
v0.0.0-...-bd1a880 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package exec provides functionality for executing commands in Docker containers.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrContainerNotFound indicates the container was not found
	ErrContainerNotFound = errors.New("container not found")

	// ErrContainerNotRunning indicates the container is not running
	ErrContainerNotRunning = errors.New("container not running")

	// ErrInvalidCommand indicates an invalid command
	ErrInvalidCommand = errors.New("invalid command")

	// ErrExecNotFound indicates the exec instance was not found
	ErrExecNotFound = errors.New("exec instance not found")

	// ErrExecCreateFailed indicates exec creation failed
	ErrExecCreateFailed = errors.New("exec creation failed")
)

Common errors

View Source
var DefaultSecurityValidator = func(config ExecConfig) error {

	dangerousCommands := []string{
		"rm -rf /", "mkfs", "dd", "reboot", "shutdown", "halt", "poweroff",
		"init", "chmod -R 777", "chown -R", "iptables", "route",
	}

	cmdStr := strings.Join(config.Cmd, " ")
	for _, dc := range dangerousCommands {
		if strings.Contains(cmdStr, dc) {
			return fmt.Errorf("potentially dangerous command detected: %s", dc)
		}
	}

	if config.Privileged {
		return fmt.Errorf("privileged mode not allowed")
	}

	return nil
}

DefaultSecurityValidator is the default security validator

Functions

func Create

func Create(ctx context.Context, client client.APIClient, containerID string, config ExecConfig, options CreateOptions) (string, error)

Create creates an exec instance in a container

func CreateAndWait

func CreateAndWait(ctx context.Context, client client.APIClient, containerID string, config ExecConfig, options CreateOptions) (int, []byte, []byte, error)

CreateAndWait creates an exec instance and waits for it to complete

func CreateMultiple

func CreateMultiple(ctx context.Context, client client.APIClient, containerID string, configs []ExecConfig, options CreateOptions) ([]string, error)

CreateMultiple creates multiple exec instances in a container

func GetRunningExecsCount

func GetRunningExecsCount(ctx context.Context, client client.APIClient, containerID string, options InspectOptions) (int, error)

GetRunningExecsCount gets the number of running execs for a container (UNRELIABLE)

func InspectMultiple

func InspectMultiple(ctx context.Context, client client.APIClient, execIDs []string, options InspectOptions) (map[string]*Info, error)

InspectMultiple inspects multiple exec instances

func SanitizeCommand

func SanitizeCommand(cmd []string) ([]string, error)

SanitizeCommand sanitizes a command for exec

func Start

func Start(ctx context.Context, client client.APIClient, execID string, options StartOptions) (io.ReadCloser, error)

Start starts an exec instance and returns an io.ReadCloser for the output stream

func StartAndWait

func StartAndWait(ctx context.Context, client client.APIClient, execID string, options StartOptions) (int, []byte, []byte, error)

StartAndWait starts an exec instance and waits for it to complete

func StartWithStdCopy

func StartWithStdCopy(ctx context.Context, client client.APIClient, execID string, options StartOptions, output OutputWriter) error

StartWithStdCopy starts an exec instance and copies its output to the provided writers

func ValidateUser

func ValidateUser(user string) bool

ValidateUser validates a user for exec

func WaitForExecToComplete

func WaitForExecToComplete(ctx context.Context, client client.APIClient, execID string, timeout time.Duration) (int, error)

WaitForExecToComplete waits for an exec instance to complete

Types

type CreateOptions

type CreateOptions struct {
	// Timeout is the timeout for the operation
	Timeout time.Duration

	// Logger for logging
	Logger *logrus.Logger

	// SecurityValidator validates security settings
	SecurityValidator func(config ExecConfig) error
}

CreateOptions defines options for creating an exec instance

type ExecConfig

type ExecConfig struct {
	// Command to execute in the container
	Cmd []string

	// User that will run the command
	User string

	// AttachStdin indicates whether to attach to stdin
	AttachStdin bool

	// AttachStdout indicates whether to attach to stdout
	AttachStdout bool

	// AttachStderr indicates whether to attach to stderr
	AttachStderr bool

	// Tty indicates whether to allocate a TTY
	Tty bool

	// DetachKeys is the key sequence used to detach from the container
	DetachKeys string

	// Env are additional environment variables
	Env []string

	// WorkingDir is the working directory
	WorkingDir string

	// Privileged indicates whether to run the command with extended privileges
	Privileged bool

	// SecurityOpts are security options
	SecurityOpts []string
}

ExecConfig defines configuration for creating an exec instance

type Info

type Info struct {
	ID          string    `json:"id"`
	ContainerID string    `json:"container_id"`
	Command     []string  `json:"command"`
	User        string    `json:"user"`
	Running     bool      `json:"running"`
	ExitCode    int       `json:"exit_code"`
	Pid         int       `json:"pid"`
	StartedAt   time.Time `json:"started_at,omitempty"`
	FinishedAt  time.Time `json:"finished_at,omitempty"`
	Privileged  bool      `json:"privileged"`
	OpenStdin   bool      `json:"open_stdin"`
	OpenStdout  bool      `json:"open_stdout"`
	OpenStderr  bool      `json:"open_stderr"`
}

Info represents information about an exec instance

func Inspect

func Inspect(ctx context.Context, client client.APIClient, execID string, options InspectOptions) (*Info, error)

Inspect inspects an exec instance

func ListExecs

func ListExecs(ctx context.Context, client client.APIClient, containerID string, options InspectOptions) ([]*Info, error)

ListExecs lists all exec instances for a container (UNRELIABLE)

func (*Info) JSON

func (e *Info) JSON() ([]byte, error)

JSON serializes an ExecInfo to JSON

func (*Info) String

func (e *Info) String() string

String returns a string representation of an ExecInfo

type InspectOptions

type InspectOptions struct {
	Timeout time.Duration
	Logger  *logrus.Logger
}

InspectOptions defines options for inspecting an exec instance

type OutputWriter

type OutputWriter interface {
	Write(p []byte) (n int, err error)
	WriteErr(p []byte) (n int, err error)
	Close() error
}

OutputWriter defines an interface for writing to stdout and stderr

type StartOptions

type StartOptions struct {
	Timeout    time.Duration
	Input      io.Reader
	RawOutput  bool
	DetachKeys string
	Logger     *logrus.Logger
	// Fields from ExecConfig needed for start/attach logic
	AttachStdin  bool
	AttachStdout bool
	AttachStderr bool
	Tty          bool
}

StartOptions defines options for starting an exec instance

type StdWriter

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

StdWriter is a basic implementation of OutputWriter that writes to io.Writers

func (*StdWriter) Close

func (w *StdWriter) Close() error

Close is a no-op for StdWriter

func (*StdWriter) Write

func (w *StdWriter) Write(p []byte) (int, error)

Write writes to stdout

func (*StdWriter) WriteErr

func (w *StdWriter) WriteErr(p []byte) (int, error)

WriteErr writes to stderr

Jump to

Keyboard shortcuts

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