forked from jenkinsci/docker-ssh-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-sshd.ps1
105 lines (88 loc) · 3.65 KB
/
setup-sshd.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# The MIT License
#
# Copyright (c) 2019-2020, Alex Earl
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Usage:
# docker run jenkins/ssh-agent <public key>
# or
# docker run -e "JENKINS_AGENT_SSH_PUBKEY=<public key>" jenkins/ssh-agent
# or
# docker run -e "JENKINS_AGENT_SSH_PUBKEY=<public key>" -e "JENKINS_AGENT_SSH_KNOWNHOST_0=<known host entry>" -e "JENKINS_AGENT_SSH_KNOWNHOST_n=<known host entry>" jenkins/ssh-agent
[CmdletBinding()]
Param(
[Parameter(Position = 0, ValueFromRemainingArguments = $true)]
[string] $Cmd
)
function Get-SSHDir {
return Join-Path "C:/Users/$env:JENKINS_AGENT_USER" '.ssh'
}
function Check-SSHDir {
$sshDir = Get-SSHDir
if(-not (Test-Path $sshDir)) {
New-Item -Type Directory -Path $sshDir | Out-Null
icacls.exe $sshDir /setowner $env:JENKINS_AGENT_USER | Out-Null
icacls.exe $sshDir /grant $('{0}:(CI)(OI)(F)' -f $env:JENKINS_AGENT_USER) /grant "administrators:(CI)(OI)(F)" | Out-Null
icacls.exe $sshDir /inheritance:r | Out-Null
}
}
function Write-Key($Key) {
# this writes the key and sets the permissions correctly for pubkey auth
$authorizedKeys = Join-Path (Get-SSHDir) 'authorized_keys'
Set-Content -Path $authorizedKeys -Value "$Key" -Encoding UTF8
icacls.exe $authorizedKeys /setowner $env:JENKINS_AGENT_USER | Out-Null
}
function Write-HostKey($Key) {
# this writes the key and sets the permissions
$knownHosts = Join-Path (Get-SSHDir) 'known_hosts'
Set-Content -Path $knownHosts -Value "$Key" -Encoding UTF8
icacls.exe $knownHosts /setowner $env:JENKINS_AGENT_USER | Out-Null
}
# Give the user Full Access to the home directory
icacls.exe "C:/Users/$env:JENKINS_AGENT_USER" /grant "${env:JENKINS_AGENT_USER}:(CI)(OI)(F)" | Out-Null
# check the .ssh dir permissions
Check-SSHDir
if($env:JENKINS_AGENT_SSH_PUBKEY -match "^ssh-.*") {
Write-Key $env:JENKINS_AGENT_SSH_PUBKEY
}
$index = 0
$knownHostKeyVar = Get-ChildItem -Path "env:JENKINS_AGENT_SSH_KNOWNHOST_$index" -ErrorAction 'SilentlyContinue'
while($null -ne $knownHostKeyVar) {
Write-HostKey $knownHostKeyVar.Value
$index++
$knownHostKeyVar = Get-ChildItem env: -Name "JENKINS_AGENT_SSH_KNOWNHOST_$index"
}
if(![System.String]::IsNullOrWhiteSpace($Cmd)) {
if($Cmd -match "^ssh-.*") {
Write-Key $Cmd
} else {
& $Cmd
exit
}
}
# ensure variables passed to docker container are also exposed to ssh sessions
Get-ChildItem env: | ForEach-Object { setx /m $_.Name $_.Value | Out-Null }
Start-Service sshd
# dump network information
ipconfig
netstat -a
while($true) {
# if we don't do this endless loop, the container exits
Start-Sleep -Seconds 60
}