-
Notifications
You must be signed in to change notification settings - Fork 0
/
GcveAuthentication.psm1
218 lines (181 loc) · 6.15 KB
/
GcveAuthentication.psm1
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
<#
.SYNOPSIS
Automate authentication to VMware vCenter and VMware NSX in a GCVE private cloud
.DESCRIPTION
Requires properly configured gcloud CLI and adequate Google Cloud IAM permissions
Set the project and zone with:
gcloud config set core/project <project>
gcloud config set compute/zone <zone>
#>
#Requires -Modules VMware.VimAutomation.Core
#Requires -Modules VMware.Sdk.Nsx.Policy
function Test-GcpZoneProject {
<#
.SYNOPSIS
Verify that gcloud configuration is set for subsequent commands to succeed
#>
$ZoneSet = $(gcloud config get compute/zone)
$ProjectSet = $(gcloud config get core/project)
if (-not ($ZoneSet -and $ProjectSet)) {
throw "Location not set. Run 'gcloud config set compute/zone <zone>' to configure."
}
}
function Connect-NsxServerGcve {
<#
.SYNOPSIS
Run Connect-NsxServer with credentials pulled from GCVE automatically, if possible
#>
param (
[Parameter(Mandatory)][string]
# Name of private cloud
$PrivateCloud
)
while ($null -eq $Global:defaultNsxConnections) {
try {
$cred = Get-GcveNsxCredentials -PrivateCloud $PrivateCloud
$nsxmanager = (Get-GcveFqdn -PrivateCloud $PrivateCloud).nsx
}
catch {
$cred = Get-Credential
$nsxmanager = Read-Host -Prompt "NSX Manager hostname or IP"
}
Connect-NsxServer -Server $nsxmanager -Credential $cred | Out-Null
}
Write-Host "Connected to $($global:defaultNsxConnections.Name)" -ForegroundColor Green
}
function Connect-VIServerGcve {
<#
.SYNOPSIS
Run Connect-VIServer with credentials pulled from GCVE automatically, if possible
#>
param (
[Parameter(Mandatory)][string]
# Name of private cloud
$PrivateCloud
)
while ($null -eq $global:DefaultVIServers) {
try {
$cred = Get-GcveVcenterCredentials -PrivateCloud $PrivateCloud
$vcenter = (Get-GcveFqdn -PrivateCloud $PrivateCloud).vcenter
}
catch {
$cred = Get-Credential
$vcenter = Read-Host -Prompt "VMware vCenter hostname or IP"
}
Connect-VIServer -Server $vcenter -Credential $cred | Out-Null
}
Write-Host "Connected to $($global:DefaultVIServers.Name)" -ForegroundColor Green
}
function Get-GcveNsxCredentials {
<#
.SYNOPSIS
Use gcloud CLI to fetch NSX credentials for a private cloud
.OUTPUTS
PSCredential object
.EXAMPLE
(Get-GcveNsxCredentials -PrivateCloud orange).Password |
ConvertFrom-SecureString -AsPlainText
#>
param (
[Parameter(Mandatory)][string]
# Name of private cloud
$PrivateCloud
)
Test-GcpZoneProject
$nsxCredentials = (gcloud vmware private-clouds nsx credentials describe --private-cloud=$PrivateCloud --format=json) |
ConvertFrom-Json
$password = ConvertTo-SecureString $nsxCredentials.password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($nsxCredentials.username, $password)
return $credential
}
function Get-GcveVcenterCredentials {
<#
.SYNOPSIS
Use gcloud CLI to fetch VMware vCenter credentials for a private cloud
.OUTPUTS
PSCredential object
.EXAMPLE
(Get-GcveVcenterCredentials -PrivateCloud orange).Password |
ConvertFrom-SecureString -AsPlainText
#>
param (
[Parameter(Mandatory)][string]
# Name of private cloud
$PrivateCloud
)
Test-GcpZoneProject
$vcCredentials = (gcloud vmware private-clouds vcenter credentials describe --private-cloud=$PrivateCloud --format=json) |
ConvertFrom-Json
$password = ConvertTo-SecureString $vcCredentials.password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($vcCredentials.username, $password)
return $credential
}
function Get-GcSecret {
<#
.SYNOPSIS
Use gcloud CLI to fetch a secret stored in Google Cloud Secret Manager
.OUTPUTS
PSCredential object
.EXAMPLE
$cred = Get-GcSecret -Name 'ContentLibrarySync' -Domain '@gve.local'
$url = Get-GcveFqdn -PrivateCloud 'orange'
Connect-VIServer -Server $url.vcenter -Credential $cred
.LINK
https://console.cloud.google.com/security/secret-manager
#>
param (
[Parameter(Mandatory)][string]
# Name of secret
$Name,
[string]
# optional domain or string to append to secret when authenticating to target
$Domain
)
Test-GcpZoneProject
$secretValue = (gcloud secrets versions access latest --secret=$Name)
if (-not $secretValue) { throw "Error accessing secret: $($Name)" }
$userName = $Name + $Domain
$password = ConvertTo-SecureString $secretValue -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ($userName, $password)
return $credential
}
function Get-GcveFqdn {
<#
.SYNOPSIS
Use gcloud CLI to fetch NSX and vCenter hostnames for a private cloud
.DESCRIPTION
Get the FQDN of vCenter and NSX management endpoints (in .gve.goog domain).
Optionally launch local browser to open URLs as a convenience.
.OUTPUTS
Hashtable
.EXAMPLE
Get-GcveFqdn -PrivateCloud orange -LaunchVcenter
Get-GcveFqdn -PrivateCloud orange -LaunchNsx
.LINK
Get-GcveNsxCredentials
Get-GcveVcenterCredentials
#>
param (
[Parameter(Mandatory)][string]
# Name of Private Cloud
$PrivateCloud,
[switch]
# Open default web browser to vCenter URL
$LaunchVcenter,
[switch]
# Open default web browser to NSX URL
$LaunchNsx
)
Test-GcpZoneProject
$pc = (gcloud vmware private-clouds describe $PrivateCloud --format=json) | ConvertFrom-Json
$fqdn = @{"nsx" = $pc.nsx.fqdn; "vcenter" = $pc.vcenter.fqdn }
if ($LaunchVcenter) { Start-Process "https://$($fqdn.vcenter)/ui/" }
if ($LaunchNsx) { Start-Process "https://$($fqdn.nsx)" }
return $fqdn
}
Export-ModuleMember -Function Connect-VIServerGcve,
Connect-NsxServerGcve,
Get-GcveNsxCredentials,
Get-GcveVcenterCredentials,
Get-GcveFqdn,
Get-GcSecret