mirror of
https://github.com/wickedyoda/public-setupfiles.git
synced 2026-11-16 13:31:56 -06:00
Update ssh on windows pc.yml
This commit is contained in:
1 parent
20dddf4feb
commit
69802266ae
1 file changed
+126
-40
@@ -48,40 +48,106 @@
|
|||||||
}
|
}
|
||||||
notify: Restart OpenSSH server
|
notify: Restart OpenSSH server
|
||||||
|
|
||||||
- name: Ensure SSH public-key authentication is enabled
|
- name: Configure SSH public-key authentication and SFTP
|
||||||
ansible.windows.win_lineinfile:
|
ansible.windows.win_powershell:
|
||||||
path: C:\ProgramData\ssh\sshd_config
|
script: |
|
||||||
regexp: '^\s*#?\s*PubkeyAuthentication\s+'
|
$ErrorActionPreference = 'Stop'
|
||||||
line: PubkeyAuthentication yes
|
$configPath = Join-Path $env:ProgramData 'ssh\sshd_config'
|
||||||
notify: Restart OpenSSH server
|
$original = [System.IO.File]::ReadAllText($configPath)
|
||||||
|
$lines = [System.Collections.Generic.List[string]]::new()
|
||||||
|
Get-Content -LiteralPath $configPath | ForEach-Object { $lines.Add([string]$_) }
|
||||||
|
|
||||||
- name: Enable the administrators authorized-keys configuration
|
function Set-GlobalDirective {
|
||||||
ansible.windows.win_lineinfile:
|
param([string]$Pattern, [string]$Value)
|
||||||
path: C:\ProgramData\ssh\sshd_config
|
$matchIndex = -1
|
||||||
regexp: '^\s*#?\s*Match\s+Group\s+administrators\s*$'
|
for ($index = 0; $index -lt $lines.Count; $index++) {
|
||||||
line: Match Group administrators
|
if ($lines[$index] -match '^\s*Match\s+') {
|
||||||
notify: Restart OpenSSH server
|
$matchIndex = $index
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- name: Configure the authorized key file for administrator accounts
|
$limit = if ($matchIndex -ge 0) { $matchIndex } else { $lines.Count }
|
||||||
ansible.windows.win_lineinfile:
|
for ($index = 0; $index -lt $limit; $index++) {
|
||||||
path: C:\ProgramData\ssh\sshd_config
|
if ($lines[$index] -match $Pattern) {
|
||||||
regexp: '^\s*#?\s*AuthorizedKeysFile\s+__PROGRAMDATA__/ssh/administrators_authorized_keys\s*$'
|
$lines[$index] = $Value
|
||||||
line: ' AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys'
|
return
|
||||||
insertafter: '^Match Group administrators$'
|
}
|
||||||
notify: Restart OpenSSH server
|
}
|
||||||
|
|
||||||
- name: Configure the SFTP subsystem
|
if ($matchIndex -ge 0) {
|
||||||
ansible.windows.win_lineinfile:
|
$lines.Insert($matchIndex, $Value)
|
||||||
path: C:\ProgramData\ssh\sshd_config
|
}
|
||||||
regexp: '^\s*#?\s*Subsystem\s+sftp\s+.*$'
|
else {
|
||||||
line: Subsystem sftp sftp-server.exe
|
$lines.Add($Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Set-GlobalDirective '^\s*#?\s*PubkeyAuthentication\s+\S+' 'PubkeyAuthentication yes'
|
||||||
|
Set-GlobalDirective '^\s*#?\s*Subsystem\s+sftp\s+.*$' 'Subsystem sftp sftp-server.exe'
|
||||||
|
|
||||||
|
$administratorMatch = -1
|
||||||
|
for ($index = 0; $index -lt $lines.Count; $index++) {
|
||||||
|
if ($lines[$index] -match '^\s*#?\s*Match\s+Group\s+administrators\s*$') {
|
||||||
|
$administratorMatch = $index
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($administratorMatch -lt 0) {
|
||||||
|
$lines.Add('')
|
||||||
|
$lines.Add('Match Group administrators')
|
||||||
|
$lines.Add(' AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$lines[$administratorMatch] = 'Match Group administrators'
|
||||||
|
$authorizedKeysIndex = -1
|
||||||
|
for ($index = $administratorMatch + 1; $index -lt $lines.Count; $index++) {
|
||||||
|
if ($lines[$index] -match '^\s*Match\s+') { break }
|
||||||
|
if ($lines[$index] -match '^\s*#?\s*AuthorizedKeysFile\s+') {
|
||||||
|
$authorizedKeysIndex = $index
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($authorizedKeysIndex -ge 0) {
|
||||||
|
$lines[$authorizedKeysIndex] = ' AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys'
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$lines.Insert($administratorMatch + 1, ' AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$updated = [string]::Join("`r`n", $lines) + "`r`n"
|
||||||
|
if ($updated -ne $original) {
|
||||||
|
[System.IO.File]::WriteAllText($configPath, $updated, [System.Text.UTF8Encoding]::new($false))
|
||||||
|
$Ansible.Changed = $true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$Ansible.Changed = $false
|
||||||
|
}
|
||||||
notify: Restart OpenSSH server
|
notify: Restart OpenSSH server
|
||||||
|
|
||||||
- name: Add the Flint4 public key for Windows administrators
|
- name: Add the Flint4 public key for Windows administrators
|
||||||
ansible.windows.win_lineinfile:
|
ansible.windows.win_powershell:
|
||||||
path: C:\ProgramData\ssh\administrators_authorized_keys
|
parameters:
|
||||||
line: "{{ flint4_ssh_public_key }}"
|
Flint4PublicKey: "{{ flint4_ssh_public_key }}"
|
||||||
create: true
|
script: |
|
||||||
|
param([string]$Flint4PublicKey)
|
||||||
|
$ErrorActionPreference = 'Stop'
|
||||||
|
$authorizedKeys = Join-Path $env:ProgramData 'ssh\administrators_authorized_keys'
|
||||||
|
if (-not (Test-Path -LiteralPath $authorizedKeys)) {
|
||||||
|
New-Item -Path $authorizedKeys -ItemType File -Force | Out-Null
|
||||||
|
}
|
||||||
|
|
||||||
|
$existingKeys = @(Get-Content -LiteralPath $authorizedKeys)
|
||||||
|
if ($existingKeys -notcontains $Flint4PublicKey) {
|
||||||
|
Add-Content -LiteralPath $authorizedKeys -Value $Flint4PublicKey -Encoding ascii
|
||||||
|
$Ansible.Changed = $true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$Ansible.Changed = $false
|
||||||
|
}
|
||||||
|
|
||||||
- name: Restrict administrator authorized-key file permissions
|
- name: Restrict administrator authorized-key file permissions
|
||||||
ansible.windows.win_powershell:
|
ansible.windows.win_powershell:
|
||||||
@@ -99,18 +165,38 @@
|
|||||||
changed_when: false
|
changed_when: false
|
||||||
|
|
||||||
- name: Allow SSH from domain and private networks
|
- name: Allow SSH from domain and private networks
|
||||||
ansible.windows.win_firewall_rule:
|
ansible.windows.win_powershell:
|
||||||
name: Ansible OpenSSH Server TCP 22
|
script: |
|
||||||
description: Allow SSH and SFTP management from domain and private networks.
|
$ErrorActionPreference = 'Stop'
|
||||||
localport: 22
|
$displayName = 'Ansible OpenSSH Server TCP 22'
|
||||||
protocol: tcp
|
$rule = Get-NetFirewallRule -DisplayName $displayName -ErrorAction SilentlyContinue |
|
||||||
direction: in
|
Select-Object -First 1
|
||||||
action: allow
|
|
||||||
profiles:
|
if (-not $rule) {
|
||||||
- Domain
|
New-NetFirewallRule -DisplayName $displayName `
|
||||||
- Private
|
-Description 'Allow SSH and SFTP management from domain and private networks.' `
|
||||||
enabled: true
|
-Direction Inbound -Action Allow -Protocol TCP -LocalPort 22 `
|
||||||
state: present
|
-Profile Domain,Private | Out-Null
|
||||||
|
$Ansible.Changed = $true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$currentProfiles = @(([string]$rule.Profile -split ',\s*') | Sort-Object)
|
||||||
|
$expectedProfiles = @('Domain', 'Private')
|
||||||
|
$profilesDiffer = (@(Compare-Object $currentProfiles $expectedProfiles).Count -gt 0)
|
||||||
|
$needsUpdate = (-not $rule.Enabled) -or
|
||||||
|
($rule.Action.ToString() -ne 'Allow') -or
|
||||||
|
($rule.Direction.ToString() -ne 'Inbound') -or
|
||||||
|
$profilesDiffer
|
||||||
|
|
||||||
|
if ($needsUpdate) {
|
||||||
|
Set-NetFirewallRule -Name $rule.Name -Enabled True -Action Allow `
|
||||||
|
-Direction Inbound -Profile Domain,Private
|
||||||
|
$Ansible.Changed = $true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$Ansible.Changed = $false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
- name: Ensure the OpenSSH server service is enabled and running
|
- name: Ensure the OpenSSH server service is enabled and running
|
||||||
ansible.windows.win_service:
|
ansible.windows.win_service:
|
||||||
|
|||||||
Reference in new issue
Block a user