Skip to content

sshconfig

import "github.com/linuskendall/cosmonaut/internal/sshconfig"

Package sshconfig manages the local SSH configuration for codespace connections. It writes per-codespace config files into \~/.ssh/cosmonaut/ and ensures the main \~/.ssh/config includes them.

Index

Constants

HostStarScopedLine is the form a bare `Host *` is rewritten to when the user accepts the scoping fix. The negation patterns prevent the catch-all block from contributing IdentityFile / IdentityAgent / etc. to codespace hosts (gh emits both `cs-*` and `cs.*` aliases).

const HostStarScopedLine = "Host * !cs-* !cs.*"

MainConfigBackupSuffix is appended to the main ssh config path for the one-shot backup written before ScopeHostStarBlocks first modifies it.

const MainConfigBackupSuffix = ".cosmonaut.bak"

const SSHIncludeLine = "Include ~/.ssh/cosmonaut/*.conf"

func EnsureConfigIncludesGenerated

func EnsureConfigIncludesGenerated(mainConfigPath string) error

EnsureConfigIncludesGenerated ensures the main SSH config includes the generated configs.

func EnsureIncludeLine

func EnsureIncludeLine(configText string) string

EnsureIncludeLine ensures the SSH include line is at the top of the config. It removes any existing copy and prepends it.

func NeedsHostStarScoping

func NeedsHostStarScoping(mainConfigPath string) bool

NeedsHostStarScoping reports whether mainConfigPath contains any bare `Host *` lines that should be narrowed so the catch-all block doesn't apply to codespace hosts. Returns false on read errors or missing file (the GUI banner shouldn't pester users without an actionable fix).

func ParsePrimaryHostAlias

func ParsePrimaryHostAlias(sshConfig string) (string, error)

ParsePrimaryHostAlias extracts the first concrete Host entry from SSH config text.

func ReadExistingAlias

func ReadExistingAlias(includeDir, codespaceName string) (string, bool)

ReadExistingAlias reads the SSH alias from an existing codespace config file. Returns the alias and true if the file exists and contains a valid Host entry, or empty string and false otherwise.

func RefreshAllManagedExtras

func RefreshAllManagedExtras(includeDir string) (int, error)

RefreshAllManagedExtras walks includeDir and refreshes the managed block in every *.conf file. Returns the number of files updated. Safe to call on every applet startup: idempotent and cheap.

func RefreshManagedExtras

func RefreshManagedExtras(path string) (bool, error)

RefreshManagedExtras rewrites the managed block in path to the current version. Returns true if the file was changed. No-op if already current or if the file doesn't exist.

func ScopeHostStarBlocks

func ScopeHostStarBlocks(mainConfigPath string) (bool, error)

ScopeHostStarBlocks rewrites bare `Host *` lines in mainConfigPath to `Host * !cs-* !cs.*` so codespace hosts skip catch-all auth rules (e.g. an IdentityFile pointing at a YubiKey-resident SK key that blocks ssh when the device isn't plugged in). Idempotent.

Writes a one-shot backup to mainConfigPath+MainConfigBackupSuffix before the first modification, so the user can recover the original if the rewrite breaks something else for them. Subsequent runs leave the existing backup untouched.

func WriteCodespaceConfig

func WriteCodespaceConfig(includeDir, codespaceName, content string) error

WriteCodespaceConfig writes the SSH config for a codespace, replacing any prior cosmonaut-managed tail with the current one.

type SSHPaths

SSHPaths holds the resolved SSH directory paths.

type SSHPaths struct {
    MainConfigPath string
    IncludeDir     string
}

func ResolvePaths

func ResolvePaths() SSHPaths

ResolvePaths returns the SSH paths for the current platform.

func (SSHPaths) CodespaceConfigPath

func (p SSHPaths) CodespaceConfigPath(codespaceName string) string

CodespaceConfigPath returns the path for a codespace-specific SSH config.

Generated by gomarkdoc