Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/shell-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ jobs:
lib/tool-manifest.sh \
scripts/health-check.sh \
scripts/tool-health-check.sh \
scripts/import-ssh-keys.sh \
tests/test-installer-functions.sh \
tests/test-ssh-key-import.sh \
tests/test-shell-functions.sh \
tests/docker-ubuntu-smoke.sh \
macos/defaults.sh \
Expand All @@ -70,7 +72,9 @@ jobs:
lib/tool-manifest.sh \
scripts/health-check.sh \
scripts/tool-health-check.sh \
scripts/import-ssh-keys.sh \
tests/test-installer-functions.sh \
tests/test-ssh-key-import.sh \
tests/test-shell-functions.sh \
tests/docker-ubuntu-smoke.sh \
macos/defaults.sh \
Expand Down Expand Up @@ -120,6 +124,9 @@ jobs:
- name: Test interactive installer functions with stubs
run: ./tests/test-installer-functions.sh

- name: Test SSH key import in a temporary home
run: ./tests/test-ssh-key-import.sh

ubuntu-container-smoke:
runs-on: ubuntu-latest
timeout-minutes: 10
Expand Down Expand Up @@ -239,6 +246,8 @@ jobs:

windows-smoke:
runs-on: windows-latest
env:
DOTFILES_DISPOSABLE_WINDOWS: '1'
steps:
- name: Checkout
uses: actions/checkout@v7
Expand Down
1 change: 1 addition & 0 deletions FUTURE_IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A parking lot for possible enhancements. Active items are prioritized; finished
- Delta configuration is now an explicit opt-in with conservative Git state restoration.
- Legacy PATH de-duplication lines are migrated automatically during shell startup updates.
- The installer now offers interactive `gh auth login` only when explicitly approved, with SSH/GPG setup hints.
- Optional incoming SSH key import is separate from client authentication and requires explicit confirmation.
- `scripts/tool-health-check.sh` validates the curated tool manifest with `jq` and reports versions.
- nvm upgrades now offer an explicit migration of global npm packages from the previous Node version.
- Remote bootstrap helpers now support optional SHA-256 verification, with dynamic upstream channels documented as explicit exceptions.
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,24 @@ For local-only package entries, edit the scaffold created at:

The public template is [`windows/packages.private.example.psd1`](windows/packages.private.example.psd1).

### Incoming SSH access

Interactive installers can optionally import public keys from a GitHub or GitLab.com username. The helper shows the destination and fingerprints and asks again before writing. It does not install or start an SSH server. `--all --yes`, `-Force`, minimal, sync, and dry-run modes do not import keys automatically.

To preview or run the helper independently:

```bash
bash scripts/import-ssh-keys.sh --provider github --username USERNAME --dry-run
bash scripts/import-ssh-keys.sh --provider github --username USERNAME
```

```powershell
.\scripts\import-ssh-keys.ps1 -Provider github -Username USERNAME -DryRun
.\scripts\import-ssh-keys.ps1 -Provider github -Username USERNAME
```

These are keys authorized for incoming connections, not keys used by `gh auth login`. Removing a key from the provider does not revoke it here; remove its line from the selected `authorized_keys` file manually. See the [Windows guide](docs/windows.md#incoming-ssh-keys) for the shared administrator file and ACL requirements.

## Safety and recovery

- Run the appropriate dry-run before the first installation and after material configuration changes. Review package manifests separately because the Unix dry-run skips the package/tool phase.
Expand Down
4 changes: 4 additions & 0 deletions docs/linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ After optional tools are approved, an interactive run can offer `gh auth login`

Authentication is not started in automatic, minimal, sync, dry-run, or non-interactive flows.

## Incoming SSH keys

Run `bash scripts/import-ssh-keys.sh --provider github --username USERNAME --dry-run` to preview, then omit `--dry-run` to confirm an import. `gitlab` is also supported. The helper uses `~/.ssh/authorized_keys`, preserves existing lines, and makes a backup before replacement. It does not configure or start sshd. Removing a key from the provider does not revoke it here; remove its line manually.

## Local configuration

### Private aliases
Expand Down
4 changes: 4 additions & 0 deletions docs/macos.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ Apply it only after reviewing the tracked app list:

This operation is intentionally separate and destructive with respect to the existing Dock arrangement.

## Incoming SSH keys

Run `bash scripts/import-ssh-keys.sh --provider github --username USERNAME --dry-run` to preview, then omit `--dry-run` to confirm an import. `gitlab` is also supported. The helper uses `~/.ssh/authorized_keys`, preserves existing lines, and makes a backup before replacement. It does not configure or start sshd. Removing a key from the provider does not revoke it here; remove its line manually.

## Local configuration

### Private aliases
Expand Down
6 changes: 6 additions & 0 deletions docs/windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ There should not be a standalone `Program Files\nodejs\node.exe` shadowing NVM.

This repo does not require a root `.nvmrc`; individual Node projects can maintain their own. The shared `npmupg` function continues to operate on whichever Node version is active.

## Incoming SSH keys

Run `.\scripts\import-ssh-keys.ps1 -Provider github -Username USERNAME -DryRun` to preview; omit `-DryRun` to confirm an import. `gitlab` is also supported. The helper requires `ssh-keygen`, checks fingerprints and ACLs, and refuses reparse points. It does not modify sshd configuration, start the service, or open the firewall.

When the effective `AuthorizedKeysFile` cannot be determined, inspect `sshd_config` and explicitly select `-Destination User` or `-Destination Administrators`. The latter is a shared file for administrator accounts, requires an elevated shell for writes, and only permits SYSTEM and Administrators ACL entries. An unusual custom path is not written automatically. Existing keys are preserved and a backup is made on replacement. Removing a key from the provider does not revoke access here; remove its line manually.

## Local configuration

### PowerShell overlays
Expand Down
7 changes: 7 additions & 0 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,13 @@ if (-not $Minimal -and -not $Sync) {
Write-Info "Minimal or sync mode: skipping package manager bootstrap."
}

if (-not $Minimal -and -not $Sync -and -not $Force -and -not $DryRun -and -not $CleanBackups -and -not $RestoreGitDefaults -and -not [Console]::IsInputRedirected) {
$importSshKeys = Read-Host 'Import public SSH keys to allow incoming connections to this machine? [y/N]'
if ($importSshKeys -match '^[Yy]$') {
& (Join-Path $PSScriptRoot 'scripts\import-ssh-keys.ps1')
}
}

Write-WindowsAliasSummary

Write-Section "Complete"
Expand Down
9 changes: 9 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -842,4 +842,13 @@ fi
report_git_identity_hint
print_install_summary

if ! $MINIMAL_MODE && ! $SYNC_MODE && ! $UNINSTALL_MODE && ! $CLEAN_BACKUPS && ! $DRY_RUN && ! $YES_MODE && ! $INSTALL_ALL && [ -t 0 ]; then
read -r -p 'Import public SSH keys to allow incoming connections to this machine? [y/N] ' import_ssh_keys
if [[ "$import_ssh_keys" =~ ^[Yy]$ ]]; then
if ! bash "$DOTFILES_HOME/scripts/import-ssh-keys.sh"; then
echo 'SSH key import failed; review the message above.' >&2
fi
fi
fi

echo -e "\n🚀 Setup complete. You may need to restart your shell or source the new config."
214 changes: 214 additions & 0 deletions scripts/import-ssh-keys.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
[CmdletBinding()]
param(
[ValidateSet('github', 'gitlab')][string]$Provider,
[string]$Username,
[ValidateSet('User', 'Administrators')][string]$Destination,
[switch]$DryRun
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

function Read-PublicKeys {
param([string]$Url)
$request = [Net.HttpWebRequest]::Create($Url)
$request.AllowAutoRedirect = $false
$request.Timeout = 30000
$request.ReadWriteTimeout = 30000
$response = $request.GetResponse()
try {
if ([int]$response.StatusCode -ne 200 -or $response.ContentLength -gt 1048576) { throw 'Unexpected or oversized response.' }
$stream = $response.GetResponseStream()
$buffer = New-Object byte[] 8192
$memory = New-Object IO.MemoryStream
try {
while (($count = $stream.Read($buffer, 0, $buffer.Length)) -gt 0) {
if ($memory.Length + $count -gt 1048576) { throw 'Public-key response exceeds 1 MiB.' }
$memory.Write($buffer, 0, $count)
}
return [Text.Encoding]::UTF8.GetString($memory.ToArray())
} finally { $memory.Dispose(); $stream.Dispose() }
} finally { $response.Dispose() }
}

function Test-SafeAcl {
param([string]$Path, [string[]]$AllowedSids)
$acl = Get-Acl -LiteralPath $Path
Assert-SafeAcl -Acl $acl -Path $Path -AllowedSids $AllowedSids
}

function Assert-SafeAcl {
param($Acl, [string]$Path, [string[]]$AllowedSids)
$owner = $acl.GetOwner([Security.Principal.SecurityIdentifier]).Value
if ($owner -notin $AllowedSids) { throw "Unexpected owner on $Path; inspect its ACL before importing." }
foreach ($rule in $acl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier])) {
if ($rule.AccessControlType -eq 'Allow' -and $rule.IdentityReference.Value -notin $AllowedSids) {
throw "Unexpected allowed identity on $Path; inspect its ACL before importing."
}
}
}

function Get-UserSshTarget {
Join-Path $HOME '.ssh\authorized_keys'
}

function Resolve-AuthorizedKeysDestination {
param([string]$Line)
if ($Line -notmatch '^authorizedkeysfile\s+') { return $null }
$value = ($Line -replace '^authorizedkeysfile\s+', '').Trim().Replace('\', '/')
if ($value -match '^\.ssh/authorized_keys(?:\s|$)') { return 'User' }
if ($value -match '^(?:__PROGRAMDATA__|%programdata%)/ssh/administrators_authorized_keys(?:\s|$)') { return 'Administrators' }
return $null
}

function Invoke-DotfilesSshKeyImport {
param(
[ValidateSet('github', 'gitlab')][string]$Provider,
[string]$Username,
[ValidateSet('User', 'Administrators')][string]$Destination,
[switch]$DryRun
)

if (-not $Provider) { $Provider = Read-Host 'Provider (github/gitlab)' }
if ($Provider -notin @('github', 'gitlab')) { throw 'Provider must be github or gitlab.' }
if (-not $Username) { $Username = Read-Host 'Username' }
if ($Username -notmatch '^[A-Za-z0-9_][A-Za-z0-9_.-]*$' -or $Username.Contains('..')) { throw 'Invalid username.' }
if (-not (Get-Command ssh-keygen.exe -ErrorAction SilentlyContinue)) { throw 'ssh-keygen is required.' }

$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$userSid = $identity.User.Value
$adminSid = 'S-1-5-32-544'
$systemSid = 'S-1-5-18'
$groups = @(whoami.exe /groups /fo csv /nh | ConvertFrom-Csv -Header Name, Type, Sid, Attributes)
if ($LASTEXITCODE -ne 0) { throw 'Cannot determine Windows group membership.' }
$isAdminMember = @($groups | Where-Object Sid -eq $adminSid).Count -gt 0
$elevated = (New-Object Security.Principal.WindowsPrincipal($identity)).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
$userTarget = Get-UserSshTarget
$adminTarget = Join-Path $env:ProgramData 'ssh\administrators_authorized_keys'

# Ask when the effective sshd configuration cannot be established safely.
$resolved = $null
$sshd = Get-Command sshd.exe -ErrorAction SilentlyContinue
if (-not (Get-Service -Name sshd -ErrorAction SilentlyContinue)) { Write-Warning 'OpenSSH Server service was not found; it will not be installed or started.' }
if ($sshd) {
$config = @(& $sshd.Source -T -C "user=$env:USERNAME,host=localhost,addr=127.0.0.1" 2>$null)
if ($LASTEXITCODE -eq 0) {
$line = @($config | Where-Object { $_ -match '^authorizedkeysfile\s+' } | Select-Object -First 1)
if ($line.Count -eq 1) {
$resolved = Resolve-AuthorizedKeysDestination -Line $line[0]
}
}
} else { Write-Warning 'OpenSSH Server was not found; it will not be installed or started.' }
if ($Destination -and $resolved -and $Destination -ne $resolved) { throw 'Selected destination conflicts with the effective sshd configuration.' }
if ($resolved) { Write-Warning 'The sshd configuration was checked for localhost; Match rules can select a different file for remote clients.' }
if ($Destination -and -not $resolved) { Write-Warning 'The effective AuthorizedKeysFile could not be verified; confirm that this destination matches sshd_config.' }
if (-not $Destination) { $Destination = $resolved }
if (-not $Destination) {
Write-Warning 'Could not determine the effective AuthorizedKeysFile. Check sshd_config before selecting a destination.'
$Destination = Read-Host 'Destination (User/Administrators; anything else cancels)'
if ($Destination -notin @('User', 'Administrators')) { throw 'No destination selected.' }
}
if ($Destination -eq 'Administrators') {
if (-not $isAdminMember) { throw 'The shared administrators file is only intended for administrator accounts.' }
Write-Warning 'This file grants access to administrator accounts and is shared between administrators.'
if (-not $DryRun -and -not $elevated) { throw 'Run this helper from an elevated PowerShell session to modify the shared file.' }
$target = $adminTarget
$allowedSids = @($adminSid, $systemSid)
} else {
$target = $userTarget
$allowedSids = @($userSid, $adminSid, $systemSid)
}
$folder = Split-Path -Parent $target
foreach ($path in @($folder, $target)) {
if (Test-Path -LiteralPath $path) {
if ((Get-Item -LiteralPath $path -Force).Attributes -band [IO.FileAttributes]::ReparsePoint) { throw "Refusing a reparse point: $path" }
Test-SafeAcl -Path $path -AllowedSids $allowedSids
}
}

$hostName = if ($Provider -eq 'github') { 'github.com' } else { 'gitlab.com' }
$payload = Read-PublicKeys -Url "https://$hostName/$Username.keys"
if ([string]::IsNullOrWhiteSpace($payload)) { throw 'No public keys returned.' }
$temp = Join-Path ([IO.Path]::GetTempPath()) ('dotfiles-ssh-key-' + [guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $temp | Out-Null
try {
$keys = @()
foreach ($line in ($payload -split "`r?`n")) {
if ([string]::IsNullOrWhiteSpace($line)) { continue }
if ($line -match '\p{Cc}') { throw 'Public key contains control characters.' }
if ($line -notmatch '^(ssh-ed25519|ssh-rsa|ecdsa-sha2-nistp(?:256|384|521)|sk-ssh-ed25519@openssh\.com|sk-ecdsa-sha2-nistp256@openssh\.com)\s+[A-Za-z0-9+/]+={0,3}(?:\s+[^\r\n]*)?$') { throw 'Invalid or unsupported public key.' }
$keyFile = Join-Path $temp 'key.pub'
[IO.File]::WriteAllText($keyFile, $line + "`n", (New-Object Text.UTF8Encoding($false)))
$fingerprint = @(& ssh-keygen.exe -lf $keyFile 2>&1)
if ($LASTEXITCODE -ne 0) { throw "Public-key validation failed: $($fingerprint -join ' ')" }
Write-Host ($fingerprint -join ' ')
$keys += $line
}
if ($keys.Count -eq 0) { throw 'No valid public keys returned.' }
$original = if (Test-Path -LiteralPath $target) { [IO.File]::ReadAllBytes($target) } else { $null }
$existingText = if ($null -ne $original) { [Text.Encoding]::UTF8.GetString($original) } else { '' }
# SSH key base64 is case-sensitive; PowerShell hashtables are not.
$seen = [Collections.Generic.HashSet[string]]::new([StringComparer]::Ordinal)
foreach ($line in ($existingText -split "`r?`n")) {
$fields = $line.TrimStart([char]0xFEFF) -split '\s+'
for ($i = 0; $i -lt $fields.Count - 1; $i++) {
if ($fields[$i] -match '^(ssh-(rsa|ed25519)|ecdsa-sha2-nistp(256|384|521)|sk-(ssh-ed25519|ecdsa-sha2-nistp256)@openssh\.com)$') { [void]$seen.Add("$($fields[$i]) $($fields[$i+1])"); break }
}
}
$newKeys = @()
foreach ($line in $keys) {
$parts = $line -split '\s+', 3
$id = "$($parts[0]) $($parts[1])"
if ($seen.Add($id)) { $newKeys += $line }
}
Write-Host "Destination: $target"
Write-Host "New keys: $($newKeys.Count); already present or duplicate: $($keys.Count - $newKeys.Count)"
Write-Warning 'Removing a key from the provider does not revoke it here. Remove it from authorized_keys manually.'
if ($DryRun -or $newKeys.Count -eq 0) { return }
if ((Read-Host 'Add these keys to allow incoming SSH access? [y/N]') -notmatch '^[Yy]$') { Write-Host 'No changes made.'; return }

if (-not (Test-Path -LiteralPath $folder)) {
if ($Destination -ne 'User') { throw 'Shared SSH folder does not exist.' }
New-Item -ItemType Directory -Path $folder | Out-Null
$folderAcl = New-Object Security.AccessControl.DirectorySecurity
$folderAcl.SetAccessRuleProtection($true, $false)
foreach ($sid in $allowedSids) {
$rule = New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($sid)), 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
$folderAcl.AddAccessRule($rule)
}
Set-Acl -LiteralPath $folder -AclObject $folderAcl
}
foreach ($path in @($folder, $target)) {
if ((Test-Path -LiteralPath $path) -and ((Get-Item -LiteralPath $path -Force).Attributes -band [IO.FileAttributes]::ReparsePoint)) { throw "SSH path changed to a reparse point: $path" }
}
Test-SafeAcl -Path $folder -AllowedSids $allowedSids
if (Test-Path -LiteralPath $target) {
if ($null -eq $original -or (Get-FileHash -LiteralPath $target -Algorithm SHA256).Hash -ne ([BitConverter]::ToString([Security.Cryptography.SHA256]::Create().ComputeHash($original)).Replace('-', ''))) { throw 'authorized_keys changed; retry.' }
Test-SafeAcl -Path $target -AllowedSids $allowedSids
} elseif ($null -ne $original) { throw 'authorized_keys was removed; retry.' }
$staged = Join-Path $folder ('.authorized_keys.dotfiles.' + [guid]::NewGuid().ToString('N'))
try {
$suffix = if ($existingText.Length -gt 0 -and -not $existingText.EndsWith("`n")) { "`r`n" } else { '' }
$bytes = [Text.Encoding]::UTF8.GetBytes($suffix + (($newKeys -join "`r`n") + "`r`n"))
$file = [IO.File]::Open($staged, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::None)
try { if ($null -ne $original) { $file.Write($original, 0, $original.Length) }; $file.Write($bytes, 0, $bytes.Length) } finally { $file.Dispose() }
$fileAcl = New-Object Security.AccessControl.FileSecurity
$fileAcl.SetAccessRuleProtection($true, $false)
foreach ($sid in $allowedSids) { $fileAcl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($sid)), 'FullControl', 'Allow'))) }
Set-Acl -LiteralPath $staged -AclObject $fileAcl
if ($null -ne $original) {
$backup = Join-Path $folder ('authorized_keys.bak.' + [guid]::NewGuid().ToString('N'))
[IO.File]::Replace($staged, $target, $backup)
Write-Host "Previous authorized_keys saved to $backup"
} else { [IO.File]::Move($staged, $target) }
Write-Host 'Keys imported.'
} finally { if (Test-Path -LiteralPath $staged) { Remove-Item -LiteralPath $staged -Force } }
} finally {
Remove-Item -LiteralPath (Join-Path $temp 'key.pub') -Force -ErrorAction SilentlyContinue
Remove-Item -LiteralPath $temp -Force -ErrorAction SilentlyContinue
}
}

if ($MyInvocation.InvocationName -ne '.') {
Invoke-DotfilesSshKeyImport @PSBoundParameters
}
Loading
Loading