AWS STS: Find the Account for an Access Key ID
See more AWS Security Token Service Examples
Uses the AWS STS Query API version 2011-06-15 and a verified TLS connection to the commercial-partition regional endpoint sts.us-west-2.amazonaws.com. Change the endpoint and signing region together for your deployment. Assumes Chilkat is unlocked.
Keep qa_data/aws/sts.json private and outside web roots/source control. Signed examples require nonempty access_key and secret_key strings; temporary source credentials also require session_token and ISO 8601 expiration. Federation exchanges load their specific fields without AWS keys. The examples do not print credentials, bearer tokens, full responses or diagnostic dumps. Credential-producing examples build a complete in-memory session object with access_key, secret_key, session_token and expiration; integrate that object into your application before it goes out of scope.
Set lookup_access_key_id to the access key ID being investigated. Uses the caller's credentials to sign the request. The returned account does not establish key activity, validity of a secret, or effective permissions. Do not supply a secret access key as the lookup value.
AWS references: GetAccessKeyInfo
Chilkat Visual Basic 6.0 Downloads
Dim success As Long
success = 0
Dim stringCompare As New ChilkatStringBuilder
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this file private and outside public web content/source control.
Dim credentials As New ChilkatJsonObject
credentials.Strict = 1
success = credentials.LoadFile("qa_data/aws/sts.json")
If (success <> 1) Then
Debug.Print "Could not load private STS configuration."
Exit Sub
End If
success = stringCompare.SetString(credentials.StringOf("access_key"))
Dim stringMatch1 As Long
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("access_key") <> 1) Or (stringMatch1 = 1)) Then
Debug.Print "Missing or invalid access_key."
Exit Sub
End If
success = stringCompare.SetString(credentials.StringOf("secret_key"))
Dim stringMatch2 As Long
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("secret_key") <> 1) Or (stringMatch2 = 1)) Then
Debug.Print "Missing or invalid secret_key."
Exit Sub
End If
Dim expires As New CkDateTime
Dim now As New CkDateTime
Dim region As String
region = "us-west-2"
If (credentials.HasMember("session_token") = 1) Then
success = stringCompare.SetString(credentials.StringOf("session_token"))
Dim stringMatch3 As Long
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("session_token") <> 1) Or (stringMatch3 = 1)) Then
Debug.Print "Missing or invalid session_token."
Exit Sub
End If
success = stringCompare.SetString(credentials.StringOf("expiration"))
Dim stringMatch4 As Long
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("expiration") <> 1) Or (stringMatch4 = 1)) Then
Debug.Print "Missing or invalid expiration."
Exit Sub
End If
success = expires.SetFromTimestamp(credentials.StringOf("expiration"))
If (success <> 1) Then
Debug.Print "Source expiration is invalid."
Exit Sub
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
Debug.Print "Could not read current time."
Exit Sub
End If
If (expires.DiffSeconds(now) <= 60) Then
Debug.Print "Source credentials are expired or expire within 60 seconds; obtain fresh credentials."
Exit Sub
End If
End If
success = stringCompare.SetString(credentials.StringOf("lookup_access_key_id"))
Dim stringMatch5 As Long
stringMatch5 = stringCompare.ContentsEqual("",1)
If ((credentials.JsonTypeOf("lookup_access_key_id") <> 1) Or (stringMatch5 = 1)) Then
Debug.Print "Missing or invalid lookup_access_key_id."
Exit Sub
End If
Dim endpoint As New ChilkatStringBuilder
success = endpoint.Append("sts.")
success = endpoint.Append(region)
success = endpoint.Append(".amazonaws.com")
Dim aws As New ChilkatAuthAws
aws.AccessKey = credentials.StringOf("access_key")
aws.SecretKey = credentials.StringOf("secret_key")
aws.Region = region
aws.ServiceName = "sts"
aws.SignatureVersion = 4
Dim rest As New ChilkatRest
rest.ConnectTimeoutMs = 20000
rest.IdleTimeoutMs = 60000
rest.VerboseLogging = 0
success = rest.SetAuthAws(aws)
If (success <> 1) Then
Debug.Print "Could not configure request signing."
Exit Sub
End If
' Keep this verified Socket alive while Rest uses its connection.
Dim connection As New ChilkatSocket
connection.RequireSslCertVerify = 1
connection.MaxReadIdleMs = 60000
connection.MaxSendIdleMs = 60000
success = connection.Connect(endpoint.GetAsString(),443,1,20000)
If (success <> 1) Then
Debug.Print "TLS connection failed; inspect diagnostics privately."
Exit Sub
End If
success = rest.UseConnection(connection,1)
If (success <> 1) Then
Debug.Print "Could not attach TLS connection."
Exit Sub
End If
Dim xml As New ChilkatXml
xml.Strict = 1
Dim value As String
Dim responseXml As String
responseXml = ""
success = rest.ClearAllQueryParams()
If (success <> 1) Then
Debug.Print "Could not reset request parameters."
Exit Sub
End If
success = rest.AddQueryParam("Action","GetAccessKeyInfo")
If (success <> 1) Then
Debug.Print "Could not set Action."
Exit Sub
End If
success = rest.AddQueryParam("Version","2011-06-15")
If (success <> 1) Then
Debug.Print "Could not set Version."
Exit Sub
End If
If (credentials.HasMember("session_token") = 1) Then
success = rest.AddQueryParam("X-Amz-Security-Token",credentials.StringOf("session_token"))
If (success <> 1) Then
Debug.Print "Could not set X-Amz-Security-Token."
Exit Sub
End If
End If
success = rest.AddQueryParam("AccessKeyId",credentials.StringOf("lookup_access_key_id"))
If (success <> 1) Then
Debug.Print "Could not set AccessKeyId."
Exit Sub
End If
responseXml = rest.FullRequestFormUrlEncoded("POST","/")
If (rest.LastMethodSuccess <> 1) Then
Debug.Print "STS transport failed; inspect diagnostics privately."
Exit Sub
End If
If (rest.ResponseStatusCode <> 200) Then
Debug.Print "STS HTTP status: " & rest.ResponseStatusCode
success = xml.LoadXml(responseXml)
If (success = 1) Then
value = xml.GetChildContent("Error|Code")
If (xml.LastMethodSuccess = 1) Then
Debug.Print "AWS error code: " & value
End If
value = xml.GetChildContent("RequestId")
If (xml.LastMethodSuccess = 1) Then
Debug.Print "AWS request ID: " & value
End If
End If
Exit Sub
End If
success = xml.LoadXml(responseXml)
If (success <> 1) Then
Debug.Print "Invalid STS XML response."
Exit Sub
End If
success = stringCompare.SetString(xml.Tag)
Dim stringMatch6 As Long
stringMatch6 = stringCompare.ContentsEqual("GetAccessKeyInfoResponse",1)
If (stringMatch6 <> 1) Then
Debug.Print "Unexpected STS response root."
Exit Sub
End If
value = xml.GetChildContent("ResponseMetadata|RequestId")
success = stringCompare.SetString(value)
Dim stringMatch7 As Long
stringMatch7 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch7 = 1)) Then
Debug.Print "Missing or empty ResponseMetadata|RequestId."
Exit Sub
End If
If (xml.HasChildWithTag("GetAccessKeyInfoResult") <> 1) Then
Debug.Print "Missing STS result."
Exit Sub
End If
value = xml.GetChildContent("GetAccessKeyInfoResult|Account")
success = stringCompare.SetString(value)
Dim stringMatch8 As Long
stringMatch8 = stringCompare.ContentsEqual("",1)
If ((xml.LastMethodSuccess <> 1) Or (stringMatch8 = 1)) Then
Debug.Print "Missing or empty GetAccessKeyInfoResult|Account."
Exit Sub
End If
Debug.Print "Account: " & value
Debug.Print "Example completed."