Skip to content

Instantly share code, notes, and snippets.

@pascal-hofmann
Created June 17, 2020 07:09
Show Gist options
  • Save pascal-hofmann/53e9f417e843384563f428dadcb3fd27 to your computer and use it in GitHub Desktop.
Save pascal-hofmann/53e9f417e843384563f428dadcb3fd27 to your computer and use it in GitHub Desktop.
Simplified Packer Buildfile: Using windows-restart with session-manager-plugin leads to timeout and 100% cpu load
<powershell>
# Version and download URL
$openSSHVersion = "7.6.1.0p1-Beta"
$openSSHURL = "https://github.com/PowerShell/Win32-OpenSSH/releases/download/v$openSSHVersion/OpenSSH-Win64.zip"
Set-ExecutionPolicy Unrestricted
# GitHub became TLS 1.2 only on Feb 22, 2018
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
# Function to unzip an archive to a given destination
Add-Type -AssemblyName System.IO.Compression.FileSystem
Function Unzip
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0)]
[string] $ZipFile,
[Parameter(Mandatory=$true, Position=1)]
[string] $OutPath
)
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipFile, $outPath)
}
# Set various known paths
$openSSHZip = Join-Path $env:TEMP 'OpenSSH.zip'
$openSSHInstallDir = Join-Path $env:ProgramFiles 'OpenSSH'
$openSSHInstallScript = Join-Path $openSSHInstallDir 'install-sshd.ps1'
$openSSHDownloadKeyScript = Join-Path $openSSHInstallDir 'download-key-pair.ps1'
$openSSHDaemon = Join-Path $openSSHInstallDir 'sshd.exe'
$openSSHDaemonConfig = [io.path]::combine($env:ProgramData, 'ssh', 'sshd_config')
# Download and unpack the binary distribution of OpenSSH
Invoke-WebRequest -Uri $openSSHURL `
-OutFile $openSSHZip `
-ErrorAction Stop
Unzip -ZipFile $openSSHZip `
-OutPath "$env:TEMP" `
-ErrorAction Stop
Remove-Item $openSSHZip `
-ErrorAction SilentlyContinue
# Move into Program Files
Move-Item -Path (Join-Path $env:TEMP 'OpenSSH-Win64') `
-Destination $openSSHInstallDir `
-ErrorAction Stop
# Run the install script, terminate if it fails
& Powershell.exe -ExecutionPolicy Bypass -File $openSSHInstallScript
if ($LASTEXITCODE -ne 0) {
throw("Failed to install OpenSSH Server")
}
# Add a firewall rule to allow inbound SSH connections to sshd.exe
New-NetFirewallRule -Name sshd `
-DisplayName "OpenSSH Server (sshd)" `
-Group "Remote Access" `
-Description "Allow access via TCP port 22 to the OpenSSH Daemon" `
-Enabled True `
-Direction Inbound `
-Protocol TCP `
-LocalPort 22 `
-Program "$openSSHDaemon" `
-Action Allow `
-ErrorAction Stop
# Ensure sshd automatically starts on boot
Set-Service sshd -StartupType Automatic `
-ErrorAction Stop
# Set the default login shell for SSH connections to Powershell
New-Item -Path HKLM:\SOFTWARE\OpenSSH -Force
New-ItemProperty -Path HKLM:\SOFTWARE\OpenSSH `
-Name DefaultShell `
-Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" `
-ErrorAction Stop
$keyDownloadScript = @'
# Download the instance key pair and authorize Administrator logins using it
$openSSHAdminUser = 'c:\ProgramData\ssh'
$openSSHAuthorizedKeys = Join-Path $openSSHAdminUser 'authorized_keys'
If (-Not (Test-Path $openSSHAdminUser)) {
New-Item -Path $openSSHAdminUser -Type Directory
}
$keyUrl = "http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key"
$keyReq = [System.Net.WebRequest]::Create($keyUrl)
$keyResp = $keyReq.GetResponse()
$keyRespStream = $keyResp.GetResponseStream()
$streamReader = New-Object System.IO.StreamReader $keyRespStream
$keyMaterial = $streamReader.ReadToEnd()
$keyMaterial | Out-File -Append -FilePath $openSSHAuthorizedKeys -Encoding ASCII
# Ensure access control on authorized_keys meets the requirements
$acl = Get-ACL -Path $openSSHAuthorizedKeys
$acl.SetAccessRuleProtection($True, $True)
Set-Acl -Path $openSSHAuthorizedKeys -AclObject $acl
$acl = Get-ACL -Path $openSSHAuthorizedKeys
$ar = New-Object System.Security.AccessControl.FileSystemAccessRule( `
"NT Authority\Authenticated Users", "ReadAndExecute", "Allow")
$acl.RemoveAccessRule($ar)
$ar = New-Object System.Security.AccessControl.FileSystemAccessRule( `
"BUILTIN\Administrators", "FullControl", "Allow")
$acl.RemoveAccessRule($ar)
$ar = New-Object System.Security.AccessControl.FileSystemAccessRule( `
"BUILTIN\Users", "FullControl", "Allow")
$acl.RemoveAccessRule($ar)
Set-Acl -Path $openSSHAuthorizedKeys -AclObject $acl
Disable-ScheduledTask -TaskName "Download Key Pair"
$sshdConfigContent = @"
# Modified sshd_config, created by Packer provisioner
PasswordAuthentication yes
PubKeyAuthentication yes
PidFile __PROGRAMDATA__/ssh/logs/sshd.pid
AuthorizedKeysFile __PROGRAMDATA__/ssh/authorized_keys
AllowUsers Administrator
Subsystem sftp sftp-server.exe
"@
Set-Content -Path C:\ProgramData\ssh\sshd_config `
-Value $sshdConfigContent
'@
$keyDownloadScript | Out-File $openSSHDownloadKeyScript
# Create Task - Ensure the name matches the verbatim version above
$taskName = "Download Key Pair"
$principal = New-ScheduledTaskPrincipal `
-UserID "NT AUTHORITY\SYSTEM" `
-LogonType ServiceAccount `
-RunLevel Highest
$action = New-ScheduledTaskAction -Execute 'Powershell.exe' `
-Argument "-NoProfile -File ""$openSSHDownloadKeyScript"""
$trigger = New-ScheduledTaskTrigger -AtStartup
Register-ScheduledTask -Action $action `
-Trigger $trigger `
-Principal $principal `
-TaskName $taskName `
-Description $taskName
Disable-ScheduledTask -TaskName $taskName
# Run the install script, terminate if it fails
& Powershell.exe -ExecutionPolicy Bypass -File $openSSHDownloadKeyScript
if ($LASTEXITCODE -ne 0) {
throw("Failed to download key pair")
}
# Restart to ensure public key authentication works and SSH comes up
Restart-Computer
</powershell>
<runAsLocalSystem>true</runAsLocalSystem>
{
"variables": {
"aws_access_key_id": "{{env `AWS_ACCESS_KEY_ID`}}",
"aws_secret_access_key": "{{env `AWS_SECRET_ACCESS_KEY`}}",
"aws_account_id": "{{env `AWS_ACCOUNT_ID`}}",
"region": "{{env `AWS_REGION`}}",
"ssh_username": "Administrator",
"image_name": "windows-server-2016-base",
"subnet_id": "{{env `SUBNET_ID`}}",
"security_group_id": "{{env `SECURITY_GROUP_ID`}}"
},
"builders": [{
"type": "amazon-ebs",
"launch_block_device_mappings":[{
"device_name":"/dev/sda1",
"volume_size":100,
"volume_type":"gp2",
"encrypted":false,
"delete_on_termination":true
}],
"subnet_id": "{{user `subnet_id` }}",
"security_group_id": "{{user `security_group_id`}}",
"access_key": "{{user `aws_access_key_id` }}",
"secret_key": "{{user `aws_secret_access_key` }}",
"region": "{{user `region` }}",
"instance_type": "t2.xlarge",
"associate_public_ip_address": false,
"user_data_file": "00-configure-source-ssh.ps1",
"source_ami_filter": {
"filters": {
"name": "Windows_Server-2016-English-Full-Base-*",
"root-device-type": "ebs",
"virtualization-type": "hvm"
},
"most_recent": true,
"owners": [
"amazon"
]
},
"communicator": "ssh",
"ssh_timeout": "5m",
"ssh_username": "{{user `ssh_username`}}",
"ssh_interface": "session_manager",
"ami_description": "{{user `image_name` }}",
"ami_name": "win-server-2016-base-agent-{{isotime \"20060102150405\" | clean_resource_name}}",
"ami_virtualization_type": "hvm",
"tags": {
"ManagedBy": "packer",
"Name": "win-server-2016-base-agent"
},
"temporary_iam_instance_profile_policy_document": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssmmessages:OpenDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:CreateControlChannel",
"ssm:UpdateInstanceInformation"
],
"Resource": "*"
}
]
}
}],
"provisioners": [
{
"type": "windows-restart",
"restart_command": "shutdown /r /t 60 /f /d p:4:1 /c \"packer restart\""
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment