Skip to content

Commit d606d7c

Browse files
committed
further restructuring of Connect-SGWServer Cmdlet
1 parent bb02d72 commit d606d7c

1 file changed

Lines changed: 35 additions & 49 deletions

File tree

src/StorageGRID-Webscale.psm1

Lines changed: 35 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,29 @@ function global:Connect-SGWServer {
119119

120120
# check if untrusted SSL certificates should be ignored
121121
if ($Insecure) {
122-
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
122+
if ($PSVersionTable.PSVersion.Major -lt 6) {
123+
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
124+
}
125+
else {
126+
$PSDefaultParameterValues.Add("Invoke-RestMethod:SkipCertificateCheck",$true)
127+
}
123128
}
124129

125-
# check if proxy is used
126-
$ProxyRegistry = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
127-
$ProxySettings = Get-ItemProperty -Path $ProxyRegistry
128-
if ($ProxySettings.ProxyEnable) {
129-
Write-Warning "Proxy Server $($ProxySettings.ProxyServer) configured in Internet Explorer may be used to connect to the OCI server!"
130-
}
131-
if ($ProxySettings.AutoConfigURL) {
132-
Write-Warning "Proxy Server defined in automatic proxy configuration script $($ProxySettings.AutoConfigURL) configured in Internet Explorer may be used to connect to the OCI server!"
130+
# TODO: Remove try/catch as soon as PowerShell 6 fixes OSVersion implementation
131+
try {
132+
if ([environment]::OSVersion.Platform -match "Win") {
133+
# check if proxy is used
134+
$ProxyRegistry = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
135+
$ProxySettings = Get-ItemProperty -Path $ProxyRegistry
136+
if ($ProxySettings.ProxyEnable) {
137+
Write-Warning "Proxy Server $($ProxySettings.ProxyServer) configured in Internet Explorer may be used to connect to the OCI server!"
138+
}
139+
if ($ProxySettings.AutoConfigURL) {
140+
Write-Warning "Proxy Server defined in automatic proxy configuration script $($ProxySettings.AutoConfigURL) configured in Internet Explorer may be used to connect to the OCI server!"
141+
}
142+
}
133143
}
144+
catch {}
134145

135146
$Server = New-Object -TypeName PSCustomObject
136147
$Server | Add-Member -MemberType NoteProperty -Name Name -Value $Name
@@ -151,34 +162,16 @@ function global:Connect-SGWServer {
151162
$Server | Add-Member -MemberType NoteProperty -Name APIVersion -Value $Response.apiVersion
152163
$Server | Add-Member -MemberType NoteProperty -Name Headers -Value @{"Authorization"="Bearer $($Response.data)"}
153164
}
154-
if (!$Transient) {
155-
Set-Variable -Name CurrentSGWServer -Value $Server -Scope Global
156-
}
157-
158-
return $Server
159165
}
160166
Catch {
161-
if ($_.Exception.Message -match "Unauthorized") {
162-
Write-Error "Authorization for $($Server.BaseURI)/api/v1/authorize with user $($Credential.UserName) failed"
167+
$ResponseBody = ParseExceptionBody $_.Exception.Response
168+
if ($_.Exception.Message -match "Unauthorized") {
169+
Write-Error "Authorization for $BaseURI/rest/v1/login with user $($Credential.UserName) failed"
163170
return
164171
}
165172
else {
166-
if ($HTTPS) {
167-
$result = $_.Exception.Response.GetResponseStream()
168-
$reader = New-Object System.IO.StreamReader($result)
169-
$reader.BaseStream.Position = 0
170-
$reader.DiscardBufferedData()
171-
$responseBody = $reader.ReadToEnd()
172-
if ($responseBody.StartsWith('{')) {
173-
$responseBody = $responseBody | ConvertFrom-Json | ConvertTo-Json
174-
}
175-
Write-Error "GET to $Uri failed with status code $($_.Exception.Response.StatusCode) and response body:`n$responseBody"
176-
return
177-
}
178-
else {
179-
Write-Warning "Login to $BaseURI/api/v1/authorize failed via HTTPS protocol"
180-
$HTTP = $True
181-
}
173+
Write-Error "Login to $BaseURI/rest/v1/login failed via HTTP protocol. Exception message: $($_.Exception.Message)`n $ResponseBody"
174+
return
182175
}
183176
}
184177
}
@@ -191,32 +184,25 @@ function global:Connect-SGWServer {
191184
$Server | Add-Member -MemberType NoteProperty -Name APIVersion -Value $Response.apiVersion
192185
$Server | Add-Member -MemberType NoteProperty -Name Headers -Value @{"Authorization"="Bearer $($Response.data)"}
193186
}
194-
195-
if (!$Transient) {
196-
Set-Variable -Name CurrentSGWServer -Value $Server -Scope Global
197-
}
198-
199-
return $Server
200187
}
201188
Catch {
202-
if ($_.Exception.Message -match "Unauthorized") {
203-
Write-Error "Authorization for $($Server.BaseURI)/api/v1/authorize with user $($Credential.UserName) failed"
189+
$ResponseBody = ParseExceptionBody $_.Exception.Response
190+
if ($_.Exception.Message -match "Unauthorized") {
191+
Write-Error "Authorization for $BaseURI/rest/v1/login with user $($Credential.UserName) failed"
204192
return
205193
}
206194
else {
207-
$result = $_.Exception.Response.GetResponseStream()
208-
$reader = New-Object System.IO.StreamReader($result)
209-
$reader.BaseStream.Position = 0
210-
$reader.DiscardBufferedData()
211-
$responseBody = $reader.ReadToEnd()
212-
if ($responseBody.StartsWith('{')) {
213-
$responseBody = $responseBody | ConvertFrom-Json | ConvertTo-Json
214-
}
215-
Write-Error "GET to $Uri failed with status code $($_.Exception.Response.StatusCode) and response body:`n$responseBody"
195+
Write-Error "Login to $BaseURI/rest/v1/login failed via HTTP protocol. Exception message: $($_.Exception.Message)`n $ResponseBody"
216196
return
217197
}
218198
}
219199
}
200+
201+
if (!$Transient) {
202+
Set-Variable -Name CurrentSGWServer -Value $Server -Scope Global
203+
}
204+
205+
return $Server
220206
}
221207

222208
<#

0 commit comments

Comments
 (0)