I want to issue temporary credentials to existing users, to allow them access to the AWS Management Console, by providing them a URL created with these temporary credentials.
I am following along with a written example given through AWS Documentation: Example Code Using IAM Query APIs
I have written the following code, which does NOT give any errors when executing, and DOES seem to return a session token, which should allow me to then properly form a URL to sign in.
Here is the code to return session token and subsequently URL:
$accessKeyId = 'accesskeyId'
$secretAccessKey = 'secretaccessKey'
$region = 'us-east-1'
Set-AWSCredentials -AccessKey $accessKeyId -SecretKey $secretAccessKey
$role = Use-STSRole -RoleSessionName "testSTS" -RoleArn "arn:aws:iam::1234567890:role/adminAccess" -DurationInSeconds 900
$jsonSession = @"
{
"sessionId": $([string]::Format("{0}", $role.Credentials.AccessKeyId)),
"sessionKey": $([string]::Format("{0}", $role.Credentials.SecretAccessKey)),
"sessionToken": $([string]::Format("{0}", $role.Credentials.SessionToken))
}
"@
Add-Type -AssemblyName System.Web
$Encode = [System.Web.HttpUtility]::UrlEncode($jsonSession)
$url = $([string]::Format("https://signin.aws.amazon.com/federation?Action=getSigninToken&Session={0}", $Encode))
$payload = Invoke-WebRequest -Uri $url | ConvertFrom-Json
$issuer = [System.Web.HttpUtility]::UrlEncode("https://1234567890.signin.aws.amazon.com")
$destination = [System.Web.HttpUtility]::UrlEncode("https://console.aws.amazon.com")
$signintoken = [System.Web.HttpUtility]::UrlEncode($payload.SigninToken)
$signInUrl = $([string]::Format("https://signin.aws.amazon.com/federation?Action=login&Issuer={0}&Destination={1}&SigninToken={2}", $issuer, $destination, $signintoken))
write-host $signInUrl
Unfortunately when I visit the url in web browser I get the following error "Amazon Web Services Sign In : The credentials in your login link were invalid. Please contact your administrator."
This is what the url returned to me looks like, obviously I have changed the accountid and real session token for security reasons:
Additionally The credentials and sessiontoken passed to me seem to work when using them to issue an API command like shown in the code below:
$accessKeyId = 'accesskeyId'
$secretAccessKey = 'secretAccessKey'
$region = 'us-east-1'
Set-AWSCredentials -AccessKey $accessKeyId -SecretKey $secretAccessKey
$role = Use-STSRole -RoleSessionName "testSTS" -RoleArn "arn:aws:iam::1234567890:role/adminAccess" -DurationInSeconds 900
$newAccessKeyId = $role.Credentials.AccessKeyId
$newSecretKey = $role.Credentials.SecretAccessKey
$newSessionToken = $role.Credentials.SessionToken
Set-AWSCredentials -AccessKey $newAccessKeyId -SecretKey $newSecretKey -SessionToken $newSessionToken
$secgroups = Get-EC2SecurityGroup
Updated: I tried removing the "issuer" parameter as article suggested below listed it as optional. I also tried adding "SessionType" to the original url for requesting sessiontoken, and the signin url still fails with same error.