-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAWS.SAML.psm1
164 lines (133 loc) · 6.02 KB
/
AWS.SAML.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
using module .\AWS.SAML.Browser.psm1
using module .\AWS.SAML.Settings.psm1
using module .\AWS.SAML.Utils.psm1
using module .\AWS.SAML.Profile.psm1
<#
.SYNOPSIS
Get AWS STS credentials for using in the CLI from a SAML based login.
.DESCRIPTION
Get AWS STS credentials for using in the CLI from a SAML based login.
.EXAMPLE
C:\PS> Login-AWSSAML
.PARAMETER InitURL
The SAML Login Initiation URL. If not passed you will be prompted and it will be saved for future use.
.PARAMETER Browser
Choose the browser to handle the login process. Options: Chrome, Firefox, Edge, IE Default: Chrome
.PARAMETER ProfileName
When specified the credentials are saved as an AWS profile under the specified name for use in the CLI
.PARAMETER SessionDuration
When specified your role session lasts for the specified duration in seconds. By default, the value is set to 3600 seconds.
#>
function New-AWSSAMLLogin {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Low')]
[Alias('Login-AWSSAML','las')]
param(
[String]$InitURL,
[ValidateSet('Chrome', 'Firefox', 'Edge', 'IE')]
[String]$Browser = 'Chrome',
[Switch]$NoBrowserProfile,
[Alias('Profile')]
[String]$ProfileName,
[Int]$SessionDuration = 3600
)
if ($pscmdlet.ShouldProcess('AWS SAML', 'login'))
{
if([String]::IsNullOrWhiteSpace($InitURL)){
$InitURL = Get-AWSSAMLURL
}
# Start Browser for Login
$driver = Start-Browser -InitURL $InitURL -Browser $Browser -NoProfile:$NoBrowserProfile
# Get SAML Assertion
$samlAssertion = Get-SAMLAssertion -Driver $driver
# Get Selected Role
$consoleData = Get-ConsoleData -Driver $driver
# Close Browser
$Driver.quit()
# Clear Screen
Clear-Host
# Get Role Details from SAML
$arns = Get-SAMLRole -Assertion $samlAssertion -AccountID $consoleData.AccountID -Role $consoleData.Role
# Get STS Credentials with SAML
$sts = Use-STSRoleWithSAML -PrincipalArn $arns.PrincipalArn -RoleArn $arns.RoleArn -SAMLAssertion $samlAssertion -DurationInSeconds $SessionDuration
# Store Credentials for use
if($ProfileName){
# Store in Profile
Set-AWSProfile -ProfileName $ProfileName -AccessKeyId $sts.Credentials.AccessKeyId -SecretAccessKey $sts.Credentials.SecretAccessKey -SessionToken $sts.Credentials.SessionToken -AccountID $consoleData.AccountID -Role $consoleData.Role -SessionDuration $SessionDuration
}else{
# Store in Environment Variable
Add-AWSSTSCred -STS $sts
}
# Output Console Data
return [PSCustomObject]@{
Account = $consoleData.Alias
AccountID = $consoleData.AccountID
User = $consoleData.Name
Role = $consoleData.Role
Expires = $sts.Credentials.Expiration
}
}
}
<#
.SYNOPSIS
Update saved profiles with new STS Credentials from SAML based login.
.DESCRIPTION
Update saved profiles with new STS Credentials from SAML based login. Must have previously saved profiles using `New-AWSSAMLLogin -Profile <name>`
.EXAMPLE
C:\PS> Update-AWSSAMLLogin
.PARAMETER InitURL
The SAML Login Initiation URL. If not passed you will be prompted if an existing Initiation URL is not found.
.PARAMETER Browser
Choose the browser to handle the login process. Options: Chrome, Firefox, Edge, IE Default: Chrome
.PARAMETER ProfileName
When specified the credentials are saved as an AWS profile under the specified name for use in the CLI
.PARAMETER SessionDuration
When specified your role session lasts for the specified duration in seconds. By default, the value is set to 3600 seconds.
#>
function Update-AWSSAMLLogin {
[CmdletBinding(SupportsShouldProcess=$true, ConfirmImpact='Low')]
[Alias('Update-AWSSAML','uas')]
param(
[String]$InitURL,
[ValidateSet('Chrome', 'Firefox', 'Edge', 'IE')]
[String]$Browser = 'Chrome',
[Alias('Profile')]
[String]$ProfileName,
[Int]$SessionDuration
)
if ($pscmdlet.ShouldProcess('AWS SAML', 'update'))
{
if([String]::IsNullOrWhiteSpace($InitURL)){
$InitURL = Get-AWSSAMLURL
}
# Start Browser for Login
$driver = Start-Browser -InitURL $InitURL -Browser $Browser -Headless
# Get SAML Assertion
# TODO: Implement timeout and relaunch of non headless browser for authentication issue detection
$samlAssertion = Get-SAMLAssertion -Driver $driver
# Close Browser
$Driver.quit()
# Renew each valid profile
if($ProfileName){
$profiles = Get-AWSProfile -ProfileName $ProfileName
}else{
$profiles = Get-AWSProfile
}
foreach ($profile in $profiles) {
if($profile.AccountID -ne '' -and $profile.Role -ne ''){
Write-Output "Updating Profile $($profile.Name)"
# Get Role Details from SAML
$arns = Get-SAMLRole -Assertion $samlAssertion -AccountID $profile.AccountID -Role $profile.Role
if($arns){
# Allow passed in duration to override profile settings
if(!($SessionDuration)){
$SessionDuration = $profile.Duration
}
# Get STS Credentials with SAML
$sts = Use-STSRoleWithSAML -PrincipalArn $arns.PrincipalArn -RoleArn $arns.RoleArn -SAMLAssertion $samlAssertion -DurationInSeconds $SessionDuration
# Update Profile
Set-AWSProfile -ProfileName $profile.Name -AccessKeyId $sts.Credentials.AccessKeyId -SecretAccessKey $sts.Credentials.SecretAccessKey -SessionToken $sts.Credentials.SessionToken -AccountID $profile.AccountID -Role $profile.Role -SessionDuration $profile.Duration
}
}
}
}
}