Skip to content

Conversation

f0ssel
Copy link
Collaborator

@f0ssel f0ssel commented Sep 11, 2025

Summary

Implemented the getUserInfo() function in the CLI package to read environment variables and populate the UserInfo struct with appropriate user information for both sudo and non-sudo scenarios. This centralizes environment variable access and provides the data needed for proper user context handling throughout the application.

Implementation Details

Core Functions Added

  • getUserInfo() - Main function that detects sudo usage and returns appropriate UserInfo
  • getCurrentUserInfo() - Helper function for non-sudo scenarios
  • getConfigDir(homeDir) - Helper function for config directory logic with XDG support

Environment Variables Handled

  • SUDO_USER - Detects if running under sudo and gets original username
  • SUDO_UID - Original user ID (parsed to integer)
  • SUDO_GID - Original group ID (parsed to integer)
  • XDG_CONFIG_HOME - XDG config directory path for proper config location

UserInfo Struct Population

The function properly populates all UserInfo fields:

type UserInfo struct {
    Username  string  // Original user or current user
    Uid       int     // Parsed integer UID  
    Gid       int     // Parsed integer GID
    HomeDir   string  // User's home directory
    ConfigDir string  // XDG_CONFIG_HOME/coder_jail or ~/.config/coder_jail
}

Key Features

Sudo Detection & Handling

  • Automatic sudo detection via SUDO_USER environment variable
  • Original user lookup when running under sudo
  • Proper credential parsing from SUDO_UID and SUDO_GID
  • Graceful fallbacks when user lookup fails

Configuration Directory Logic

  • XDG Base Directory support - Uses XDG_CONFIG_HOME when available
  • Standard fallback - Falls back to ~/.config/coder_jail
  • Cross-platform compatibility - Works on both Linux and macOS

Error Handling

  • Comprehensive error handling with graceful fallbacks
  • No panics - All errors handled gracefully
  • Fallback to current user when sudo user lookup fails

Code Cleanup

Removed all commented environment variable code that was no longer needed:

  • namespace/linux.go - Removed 36+ lines of commented sudo handling code
  • namespace/macos.go - Removed 26+ lines of commented sudo handling code
  • tls/tls.go - Removed entire commented getConfigDir() function (37+ lines)

Testing

  • All existing tests pass
  • Code compiles successfully on all platforms
  • No breaking changes to external interfaces
  • Environment variables centralized in CLI package only

Benefits

  • 🏗️ Better Architecture: Environment variable access centralized in CLI package
  • 🧪 Improved Testability: User information can be easily mocked for testing
  • 🔒 Security: Proper handling of sudo context and user credentials
  • ⚡ Performance: No redundant environment variable reads
  • 🛠️ Maintainability: Clean, well-documented code with proper error handling
  • 🌐 Cross-platform: Works consistently across Linux and macOS

Usage

The getUserInfo() function is called once during CLI initialization and provides the user context needed by:

  • TLS certificate management (for config directory)
  • Namespace setup (for user credentials)
  • Environment variable restoration (for original user context)

f0ssel and others added 3 commits September 11, 2025 15:48
Implemented the getUserInfo function in cli/cli.go to read environment
variables and populate the UserInfo struct with appropriate user
information for both sudo and non-sudo scenarios.

Features:
- Detects sudo usage via SUDO_USER environment variable
- Retrieves original user information when running under sudo
- Parses SUDO_UID and SUDO_GID for proper user credentials
- Handles XDG_CONFIG_HOME for config directory determination
- Fallback to current user when not running under sudo
- Comprehensive error handling with graceful fallbacks

Cleanup:
- Removed all commented environment variable code from:
  - namespace/linux.go (sudo user/uid/gid handling)
  - namespace/macos.go (sudo user/uid handling)
  - tls/tls.go (commented getConfigDir function)
- Centralized all environment variable reading in CLI package

All tests pass and code compiles successfully.

Co-authored-by: f0ssel <19379394+f0ssel@users.noreply.github.com>
@blink-so blink-so bot changed the title F0ssel/no envs Implement getUserInfo function to read environment variables Sep 11, 2025
@f0ssel f0ssel merged commit 84bf277 into main Sep 11, 2025
7 checks passed
@f0ssel f0ssel deleted the f0ssel/no-envs branch September 16, 2025 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant