-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghe_client.ps1
261 lines (227 loc) · 7.19 KB
/
ghe_client.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#
# ghe_client.ps1 : GheClient Implementation Classes
#
#####################################################
# SSH Command class helpers
#####################################################
class GheCommand
{
[String]$Query
[System.Object]$Response
GheCommand([String]$Query)
{ $this.Query = $Query }
# Virtual callback method used to parse the result
SetResponse([System.Object]$Response)
{ $this.Response = $Response }
}
class GheCommandCollection : System.Collections.ArrayList
{
}
#####################################################
# Mail class helpers
#####################################################
class GheMailMsg : System.Net.Mail.MailMessage
{
# Default constructor
GheMailMsg() : base() {}
# Copy constructors
GheMailMsg([System.Net.Mail.MailMessage] $Message) : base()
{
[GheMailMsg].GetProperties() | % {
$name = $_.Name
$value = $Message.psobject.properties[$name].Value
if($_.CanWrite)
{ $this.psobject.properties[$name].Value = $value }
elseif($value.GetType().GetInterface("IList"))
{ $value.ForEach({$this.psobject.properties[$name].Value.Add($_)}) }
}
}
}
class GheMailMsgCollection : System.Collections.Generic.List[System.Net.Mail.MailMessage]
{
}
#####################################################
# GheClient class
#####################################################
class GheClient
{
# Octokit Connection Support
[System.Uri]$ServerUri
[Octokit.ApiConnection]$ApiConnection
# Octokit overrides
Hidden [System.Reflection.MethodInfo]$_CreateImpersonationToken
# Ssh Connection Support
[String]$SshServerUri
[UInt16]$SshHostPort
[String]$SshKeyPath
[System.Management.Automation.PSCredential]$SshCredentials
# Extented Parameters
[Hashtable]$Params
GheClient(
[String]$ServerUri,
[String]$AdminToken,
[Int32]$SshHostPort,
[String]$SshKeyPath)
{
# Octokit Connection support
$this.ServerUri = [System.Uri]::new($ServerUri)
$this.ApiConnection = [GheClient]::GetApiConnection(
$this.ServerUri, $AdminToken)
# Overriding UserAdministrationClient.CreateImpersonationToken(...) which does not
# return the administration token :-(
$this._CreateImpersonationToken = $this.ApiConnection.GetType().
GetMethod("Post", [System.Type[]] @([System.Uri], [System.Object])).
MakeGenericMethod([System.Collections.Generic.Dictionary[String,Object]])
# Ssh Connection support
$this.SshServerUri = $this.ServerUri.Host
$this.SshHostPort = $SshHostPort
$this.SshKeyPath = $SshKeyPath
$this.SshCredentials = [System.Management.Automation.PSCredential]::new(
“admin”,
(ConvertTo-SecureString “admin” -AsPlainText -Force)
)
# Extented Parameters
$this.Params = [Hashtable]::new()
}
#####################################################
# Octokit Connection implementation
#####################################################
static [Octokit.ApiConnection] GetApiConnection(
[System.Uri] $ServerUri,
[String] $UserToken)
{
return [Octokit.ApiConnection]::new(
[Octokit.Connection]::new(
[Octokit.ProductHeaderValue]::new("GheClient"),
[System.Uri]::new($ServerUri, "/api/v3/"),
[Octokit.Internal.InMemoryCredentialStore]::new(
[Octokit.Credentials]::new($UserToken))
)
)
}
[String] CreateImpersonationToken([string]$Login)
{
[System.Object[]] $par_lst = @(
[Octokit.ApiUrls]::UserAdministrationAuthorization($Login),
[Octokit.NewImpersonationToken]::new([System.String[]] @("user"))
)
$res_val = $this._CreateImpersonationToken.Invoke($this.ApiConnection, $par_lst)
$res_obj = $res_val.Result
return $res_obj["token"]
}
[void] DeleteImpersonationToken([string]$Login)
{
$admin = [Octokit.UserAdministrationClient]::new($this.ApiConnection)
$res_val = $admin.DeleteImpersonationToken($Login)
$res_val.Result
}
#####################################################
# Ssh Connection implementation
#####################################################
[GheCommand] SendCommand([String]$CommandTxt)
{
$Command = [GheCommand]::new($CommandTxt)
$this.SendCommand($Command)
return $Command
}
Hidden [GheCommand] SendCommand([GheCommand]$CommandObj)
{
$CommandList = [GheCommandCollection]::new()
$CommandList.Add($CommandObj)
return $this.SendCommand($CommandList)[0]
}
Hidden [GheCommandCollection] SendCommand([GheCommandCollection]$CommandList)
{
# Open SSH session
$srv_ses = $null
$srv_wrn = $null
$srv_err = $null
try
{
$srv_ses = New-SSHSession `
-ComputerName $this.SshServerUri `
-Port $this.SshHostPort `
-KeyFile $this.SshKeyPath `
-Credential $this.SshCredentials `
-AcceptKey -Force `
-WarningVariable $srv_wrn `
-WarningAction SilentlyContinue `
-ErrorVariable $srv_err `
-ErrorAction Stop
}
catch
{
throw $srv_err
}
# Run all Commands in the list
ForEach($srv_cmd in $CommandList)
{
$srv_res = Invoke-SSHCommand -Index $srv_ses.SessionId -Command $srv_cmd.Query
$srv_cmd.SetResponse($srv_res)
}
# Close SSH session
Remove-SSHSession $srv_ses
return $CommandList
}
# Remove the Announce Message
[GheCommand] ClearAnnounce()
{ return $this.SendAnnounce($null) }
# Set or Remove the Announce Message
[GheCommand] SendAnnounce([String]$Message)
{
# Usage: ghe-announce [-s <message>|-u]
#
# Set or clear a global announcement banner, to be displayed to all users.
#
# OPTIONS:
# -h Show this message
# -s MESSAGE Set a global announcement banner
# -u Unset the global announcement banner
# Linux command to display announce
if($Message)
{ $CommandTxt = ('ghe-announce -s "{0}"' -f $Message) }
# Linux command to remove existing announce
else
{ $CommandTxt = 'ghe-announce -u' }
# Run ssh command to get the result
return $this.SendCommand($CommandTxt)
}
# Mail helpers
[GheCommand] SendMail(
[System.Net.Mail.MailMessage] $EmailObj)
{
$EmailList = [GheMailMsgCollection]::new()
$EmailList.Add($EmailObj)
return $this.SendMail($EmailList)[0]
}
[GheCommandCollection] SendMail(
[System.Collections.Generic.List[System.Net.Mail.MailMessage]] $EmailList
)
{
$CommandList = [GheCommandCollection]::new()
ForEach($Email in $EmailList)
{
if($Email.From) { $msg_txt = "From: $($Email.From)`n" }
else { $msg_txt = "From: $($Email.Sender)`n" }
$Email.psobject.Properties | Where { $_.Name -in @("To", "Cc", "Bcc") } |
ForEach-Object { $msg_txt += "{0}: {1}`n" -f $_.Name, ($_.Value -join ",") }
$msg_txt += "Subject : {0}`n{1}`n." -f
$Email.Subject.Replace("\","\\").Replace("`"","\`""),
$Email.Body.Replace("\","\\").Replace("`"","\`"")
$msg_cmd = "echo `"$msg_txt`" | sendmail -t /USER $($Email.Sender)"
$CommandList.Add([GheCommand]::new($msg_cmd))
}
if($CommandList.Count)
{ $this.SendCommand($CommandList) }
return $CommandList
}
# User Helpers
[GheCommand] UserSuspend([String] $login)
{ return $this.SendCommand("ghe-user-suspend {0}" -f $login) }
[GheCommand] UserUnsuspend([String] $login)
{ return $this.SendCommand("ghe-user-suspend {0}" -f $login) }
[GheCommand] UserPromote([String] $login)
{ return $this.SendCommand("ghe-user-promote {0}" -f $login) }
[GheCommand] UserDemote([String] $login)
{ return $this.SendCommand("ghe-user-demote {0}" -f $login) }
}