 8d9b62daf3
			
		
	
	8d9b62daf3
	
	
	
		
			
			This commit implements Phase 2 of the CHORUS Task Execution Engine development plan, providing a comprehensive execution environment abstraction layer with Docker container sandboxing support. ## New Features ### Core Sandbox Interface - Comprehensive ExecutionSandbox interface with isolated task execution - Support for command execution, file I/O, environment management - Resource usage monitoring and sandbox lifecycle management - Standardized error handling with SandboxError types and categories ### Docker Container Sandbox Implementation - Full Docker API integration with secure container creation - Transparent repository mounting with configurable read/write access - Advanced security policies with capability dropping and privilege controls - Comprehensive resource limits (CPU, memory, disk, processes, file handles) - Support for tmpfs mounts, masked paths, and read-only bind mounts - Container lifecycle management with proper cleanup and health monitoring ### Security & Resource Management - Configurable security policies with SELinux, AppArmor, and Seccomp support - Fine-grained capability management with secure defaults - Network isolation options with configurable DNS and proxy settings - Resource monitoring with real-time CPU, memory, and network usage tracking - Comprehensive ulimits configuration for process and file handle limits ### Repository Integration - Seamless repository mounting from local paths to container workspaces - Git configuration support with user credentials and global settings - File inclusion/exclusion patterns for selective repository access - Configurable permissions and ownership for mounted repositories ### Testing Infrastructure - Comprehensive test suite with 60+ test cases covering all functionality - Docker integration tests with Alpine Linux containers (skipped in short mode) - Mock sandbox implementation for unit testing without Docker dependencies - Security policy validation tests with read-only filesystem enforcement - Resource usage monitoring and cleanup verification tests ## Technical Details ### Dependencies Added - github.com/docker/docker v28.4.0+incompatible - Docker API client - github.com/docker/go-connections v0.6.0 - Docker connection utilities - github.com/docker/go-units v0.5.0 - Docker units and formatting - Associated Docker API dependencies for complete container management ### Architecture - Interface-driven design enabling multiple sandbox implementations - Comprehensive configuration structures for all sandbox aspects - Resource usage tracking with detailed metrics collection - Error handling with retryable error classification - Proper cleanup and resource management throughout sandbox lifecycle ### Compatibility - Maintains backward compatibility with existing CHORUS architecture - Designed for future integration with Phase 3 Core Task Execution Engine - Extensible design supporting additional sandbox implementations (VM, process) This Phase 2 implementation provides the foundation for secure, isolated task execution that will be integrated with the AI model providers from Phase 1 in the upcoming Phase 3 development. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
		
			
				
	
	
		
			138 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			138 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package winio
 | |
| 
 | |
| import (
 | |
| 	"bytes"
 | |
| 	"encoding/binary"
 | |
| 	"errors"
 | |
| )
 | |
| 
 | |
| type fileFullEaInformation struct {
 | |
| 	NextEntryOffset uint32
 | |
| 	Flags           uint8
 | |
| 	NameLength      uint8
 | |
| 	ValueLength     uint16
 | |
| }
 | |
| 
 | |
| var (
 | |
| 	fileFullEaInformationSize = binary.Size(&fileFullEaInformation{})
 | |
| 
 | |
| 	errInvalidEaBuffer = errors.New("invalid extended attribute buffer")
 | |
| 	errEaNameTooLarge  = errors.New("extended attribute name too large")
 | |
| 	errEaValueTooLarge = errors.New("extended attribute value too large")
 | |
| )
 | |
| 
 | |
| // ExtendedAttribute represents a single Windows EA.
 | |
| type ExtendedAttribute struct {
 | |
| 	Name  string
 | |
| 	Value []byte
 | |
| 	Flags uint8
 | |
| }
 | |
| 
 | |
| func parseEa(b []byte) (ea ExtendedAttribute, nb []byte, err error) {
 | |
| 	var info fileFullEaInformation
 | |
| 	err = binary.Read(bytes.NewReader(b), binary.LittleEndian, &info)
 | |
| 	if err != nil {
 | |
| 		err = errInvalidEaBuffer
 | |
| 		return ea, nb, err
 | |
| 	}
 | |
| 
 | |
| 	nameOffset := fileFullEaInformationSize
 | |
| 	nameLen := int(info.NameLength)
 | |
| 	valueOffset := nameOffset + int(info.NameLength) + 1
 | |
| 	valueLen := int(info.ValueLength)
 | |
| 	nextOffset := int(info.NextEntryOffset)
 | |
| 	if valueLen+valueOffset > len(b) || nextOffset < 0 || nextOffset > len(b) {
 | |
| 		err = errInvalidEaBuffer
 | |
| 		return ea, nb, err
 | |
| 	}
 | |
| 
 | |
| 	ea.Name = string(b[nameOffset : nameOffset+nameLen])
 | |
| 	ea.Value = b[valueOffset : valueOffset+valueLen]
 | |
| 	ea.Flags = info.Flags
 | |
| 	if info.NextEntryOffset != 0 {
 | |
| 		nb = b[info.NextEntryOffset:]
 | |
| 	}
 | |
| 	return ea, nb, err
 | |
| }
 | |
| 
 | |
| // DecodeExtendedAttributes decodes a list of EAs from a FILE_FULL_EA_INFORMATION
 | |
| // buffer retrieved from BackupRead, ZwQueryEaFile, etc.
 | |
| func DecodeExtendedAttributes(b []byte) (eas []ExtendedAttribute, err error) {
 | |
| 	for len(b) != 0 {
 | |
| 		ea, nb, err := parseEa(b)
 | |
| 		if err != nil {
 | |
| 			return nil, err
 | |
| 		}
 | |
| 
 | |
| 		eas = append(eas, ea)
 | |
| 		b = nb
 | |
| 	}
 | |
| 	return eas, err
 | |
| }
 | |
| 
 | |
| func writeEa(buf *bytes.Buffer, ea *ExtendedAttribute, last bool) error {
 | |
| 	if int(uint8(len(ea.Name))) != len(ea.Name) {
 | |
| 		return errEaNameTooLarge
 | |
| 	}
 | |
| 	if int(uint16(len(ea.Value))) != len(ea.Value) {
 | |
| 		return errEaValueTooLarge
 | |
| 	}
 | |
| 	entrySize := uint32(fileFullEaInformationSize + len(ea.Name) + 1 + len(ea.Value))
 | |
| 	withPadding := (entrySize + 3) &^ 3
 | |
| 	nextOffset := uint32(0)
 | |
| 	if !last {
 | |
| 		nextOffset = withPadding
 | |
| 	}
 | |
| 	info := fileFullEaInformation{
 | |
| 		NextEntryOffset: nextOffset,
 | |
| 		Flags:           ea.Flags,
 | |
| 		NameLength:      uint8(len(ea.Name)),
 | |
| 		ValueLength:     uint16(len(ea.Value)),
 | |
| 	}
 | |
| 
 | |
| 	err := binary.Write(buf, binary.LittleEndian, &info)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	_, err = buf.Write([]byte(ea.Name))
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	err = buf.WriteByte(0)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	_, err = buf.Write(ea.Value)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	_, err = buf.Write([]byte{0, 0, 0}[0 : withPadding-entrySize])
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| // EncodeExtendedAttributes encodes a list of EAs into a FILE_FULL_EA_INFORMATION
 | |
| // buffer for use with BackupWrite, ZwSetEaFile, etc.
 | |
| func EncodeExtendedAttributes(eas []ExtendedAttribute) ([]byte, error) {
 | |
| 	var buf bytes.Buffer
 | |
| 	for i := range eas {
 | |
| 		last := false
 | |
| 		if i == len(eas)-1 {
 | |
| 			last = true
 | |
| 		}
 | |
| 
 | |
| 		err := writeEa(&buf, &eas[i], last)
 | |
| 		if err != nil {
 | |
| 			return nil, err
 | |
| 		}
 | |
| 	}
 | |
| 	return buf.Bytes(), nil
 | |
| }
 |