Skip to content

Commit bb02d72

Browse files
committed
Restructured workarounds and Connect-SGWServer Cmdlet
1 parent 5ab93c6 commit bb02d72

1 file changed

Lines changed: 114 additions & 56 deletions

File tree

src/StorageGRID-Webscale.psm1

Lines changed: 114 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,92 @@
1-
# Workaround to allow Powershell to accept untrusted certificates
2-
add-type @"
3-
using System.Net;
4-
using System.Security.Cryptography.X509Certificates;
5-
public class TrustAllCertsPolicy : ICertificatePolicy {
6-
public bool CheckValidationResult(
7-
ServicePoint srvPoint, X509Certificate certificate,
8-
WebRequest request, int certificateProblem) {
9-
return true;
1+
# workarounds for PowerShell issues
2+
if ($PSVersionTable.PSVersion.Major -lt 6) {
3+
Add-Type @"
4+
using System.Net;
5+
using System.Security.Cryptography.X509Certificates;
6+
public class TrustAllCertsPolicy : ICertificatePolicy {
7+
public bool CheckValidationResult(
8+
ServicePoint srvPoint, X509Certificate certificate,
9+
WebRequest request, int certificateProblem) {
10+
return true;
11+
}
1012
}
11-
}
1213
"@
1314

15+
# OCI 7.2 only supports TLS 1.2 and PowerShell does not auto negotiate it, thus enforcing TLS 1.2 which works for older OCI Versions as well
16+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
17+
18+
# Using .NET JSON Serializer as JSON serialization included in Invoke-RestMethod has a length restriction for JSON content
19+
Add-Type -AssemblyName System.Web.Extensions
20+
$global:javaScriptSerializer = New-Object System.Web.Script.Serialization.JavaScriptSerializer
21+
$global:javaScriptSerializer.MaxJsonLength = [System.Int32]::MaxValue
22+
$global:javaScriptSerializer.RecursionLimit = 99
23+
24+
# Functions necessary to parse JSON output from .NET serializer to PowerShell Objects
25+
function ParseItem($jsonItem) {
26+
if($jsonItem.PSObject.TypeNames -match "Array") {
27+
return ParseJsonArray($jsonItem)
28+
}
29+
elseif($jsonItem.PSObject.TypeNames -match "Dictionary") {
30+
return ParseJsonObject([HashTable]$jsonItem)
31+
}
32+
else {
33+
return $jsonItem
34+
}
35+
}
36+
37+
function ParseJsonObject($jsonObj) {
38+
$result = New-Object -TypeName PSCustomObject
39+
foreach ($key in $jsonObj.Keys) {
40+
$item = $jsonObj[$key]
41+
if ($item) {
42+
$parsedItem = ParseItem $item
43+
} else {
44+
$parsedItem = $null
45+
}
46+
$result | Add-Member -MemberType NoteProperty -Name $key -Value $parsedItem
47+
}
48+
return $result
49+
}
50+
51+
function ParseJsonArray($jsonArray) {
52+
$result = @()
53+
$jsonArray | ForEach-Object {
54+
$result += ,(ParseItem $_)
55+
}
56+
return $result
57+
}
58+
59+
function ParseJsonString($json) {
60+
$config = $javaScriptSerializer.DeserializeObject($json)
61+
if ($config -is [Array]) {
62+
return ParseJsonArray($config)
63+
}
64+
else {
65+
return ParseJsonObject($config)
66+
}
67+
}
68+
}
69+
70+
### Helper Functions ###
71+
72+
function ParseExceptionBody($Response) {
73+
if ($Response) {
74+
$Reader = New-Object System.IO.StreamReader($Response.GetResponseStream())
75+
$Reader.BaseStream.Position = 0
76+
$Reader.DiscardBufferedData()
77+
$ResponseBody = $reader.ReadToEnd()
78+
if ($ResponseBody.StartsWith('{')) {
79+
$ResponseBody = $ResponseBody | ConvertFrom-Json | ConvertTo-Json
80+
}
81+
return $ResponseBody
82+
}
83+
else {
84+
return $Response
85+
}
86+
}
87+
88+
### Cmdlets ###
89+
1490
<#
1591
.SYNOPSIS
1692
Connect to StorageGRID Webscale Management Server
@@ -27,19 +103,16 @@ function global:Connect-SGWServer {
27103
[parameter(Mandatory=$True,
28104
Position=1,
29105
HelpMessage="A System.Management.Automation.PSCredential object containing the credentials needed to log into the StorageGRID Webscale Management Server.")][System.Management.Automation.PSCredential]$Credential,
30-
[parameter(Mandatory=$True,
31-
Position=2,
32-
HelpMessage="Account ID for Tenenant Login")][System.Management.Automation.PSCredential]$AccountID,
33106
[parameter(Mandatory=$False,
34-
Position=3,
107+
Position=2,
35108
HelpMessage="This cmdlet always tries to establish a secure HTTPS connection to the StorageGRID Webscale Management Server, but it will fall back to HTTP if necessary. Specify -HTTP to skip the HTTPS connection attempt and only try HTTP.")][Switch]$HTTP,
36109
[parameter(Mandatory=$False,
37-
Position=4,
110+
Position=2,
38111
HelpMessage="This cmdlet always tries to establish a secure HTTPS connection to the StorageGRID Webscale Management Server, but it will fall back to HTTP if necessary. Specify -HTTPS to fail the connection attempt in that case rather than fall back to HTTP.")][Switch]$HTTPS,
39112
[parameter(Mandatory=$False,
40-
Position=5,
113+
Position=3,
41114
HelpMessage="If the StorageGRID Webscale Management Server certificate cannot be verified, the connection will fail. Specify -Insecure to ignore the validity of the StorageGRID Webscale Management Server certificate.")][Switch]$Insecure,
42-
[parameter(Position=6,
115+
[parameter(Position=4,
43116
Mandatory=$False,
44117
HelpMessage="Specify -Transient to not set the global variable `$CurrentOciServer.")][Switch]$Transient
45118
)
@@ -63,23 +136,12 @@ function global:Connect-SGWServer {
63136
$Server | Add-Member -MemberType NoteProperty -Name Name -Value $Name
64137
$Server | Add-Member -MemberType NoteProperty -Name Credential -Value $Credential
65138

66-
if ($AccountID) {
67-
$Body = @"
139+
$Body = @"
68140
{
69-
"accountId": "$AccountID",
70141
"username": "$($Credential.UserName)",
71142
"password": "$($Credential.GetNetworkCredential().Password)"
72143
}
73144
"@
74-
}
75-
else {
76-
$Body = @"
77-
{
78-
"username": "$($Credential.UserName)",
79-
"password": "$($Credential.GetNetworkCredential().Password)"
80-
}
81-
"@
82-
}
83145

84146
if ($HTTPS -or !$HTTP) {
85147
Try {
@@ -360,7 +422,6 @@ function Global:New-SGWAccount {
360422
"@
361423

362424
try {
363-
$Body
364425
$Result = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Server.Headers -Body $Body -ContentType "application/json"
365426
}
366427
catch {
@@ -924,39 +985,36 @@ function Global:New-SGWAccountS3AccessKey {
924985
}
925986

926987
Process {
927-
$Id = @($Id)
928-
foreach ($Id in $Id) {
929-
$Uri = $Server.BaseURI + "/api/v1/grid/accounts/$id/s3-access-keys"
988+
$Uri = $Server.BaseURI + "/api/v1/grid/accounts/$id/s3-access-keys"
930989

931-
if ($Expires) {
932-
$Body = @"
990+
if ($Expires) {
991+
$Body = @"
933992
{
934993
"expires": "$ExpirationDate"
935994
}
936995
"@
937-
}
938-
else {
939-
$Body = "{}"
940-
}
996+
}
997+
else {
998+
$Body = "{}"
999+
}
9411000

942-
try {
943-
$Body
944-
$Result = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Server.Headers -Body $Body -ContentType "application/json"
945-
}
946-
catch {
947-
$result = $_.Exception.Response.GetResponseStream()
948-
$reader = New-Object System.IO.StreamReader($result)
949-
$reader.BaseStream.Position = 0
950-
$reader.DiscardBufferedData()
951-
$responseBody = $reader.ReadToEnd()
952-
if ($responseBody.StartsWith('{')) {
953-
$responseBody = $responseBody | ConvertFrom-Json | ConvertTo-Json
954-
}
955-
Write-Error "GET to $Uri failed with status code $($_.Exception.Response.StatusCode) and response body:`n$responseBody"
1001+
try {
1002+
$Body
1003+
$Result = Invoke-RestMethod -Method POST -Uri $Uri -Headers $Server.Headers -Body $Body -ContentType "application/json"
1004+
}
1005+
catch {
1006+
$result = $_.Exception.Response.GetResponseStream()
1007+
$reader = New-Object System.IO.StreamReader($result)
1008+
$reader.BaseStream.Position = 0
1009+
$reader.DiscardBufferedData()
1010+
$responseBody = $reader.ReadToEnd()
1011+
if ($responseBody.StartsWith('{')) {
1012+
$responseBody = $responseBody | ConvertFrom-Json | ConvertTo-Json
9561013
}
957-
958-
Write-Output $Result.data
1014+
Write-Error "POST to $Uri failed with status code $($_.Exception.Response.StatusCode) and response body:`n$responseBody"
9591015
}
1016+
1017+
Write-Output $Result.data
9601018
}
9611019
}
9621020

@@ -1250,4 +1308,4 @@ function Global:Sync-SGWIdentitySources {
12501308

12511309
Write-Output $Result.data
12521310
}
1253-
}
1311+
}

0 commit comments

Comments
 (0)