AWS STS: Download an S3 Object with STS Credentials and Chilkat.Http
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.
Reads private STS XML as in the normalization example, plus qa_data/aws/sts_download.json with bucket and object_key strings. Uses all three temporary credential components with Chilkat.Http.S3_DownloadBd. Supply the raw object key, not a pre-encoded URL. Choose a small object that fits in memory, a general purpose bucket in us-west-2, and a session authorized for s3:GetObject on that object; the earlier bucket-list-only session policy is insufficient. KMS-encrypted objects may also need kms:Decrypt. The request is read-only and writes no file. Redirects are disabled so a region mismatch fails explicitly.
AWS references: AssumeRole
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set stringCompare [new_CkStringBuilder]
# Load a privately stored STS XML response; never print its contents.
set tokenXml [new_CkXml]
CkXml_put_Strict $tokenXml 1
set success [CkXml_LoadXmlFile $tokenXml "qa_data/tokens/aws_session_token.xml"]
if {$success != 1} then {
puts "Could not load private STS XML."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
exit
}
set credentialPath ""
CkStringBuilder_SetString $stringCompare [CkXml_tag $tokenXml]
set stringMatch1 [CkStringBuilder_ContentsEqual $stringCompare "GetSessionTokenResponse" 1]
if {$stringMatch1 == 1} then {
set credentialPath "GetSessionTokenResult|Credentials"
}
CkStringBuilder_SetString $stringCompare [CkXml_tag $tokenXml]
set stringMatch2 [CkStringBuilder_ContentsEqual $stringCompare "AssumeRoleResponse" 1]
if {$stringMatch2 == 1} then {
set credentialPath "AssumeRoleResult|Credentials"
}
CkStringBuilder_SetString $stringCompare [CkXml_tag $tokenXml]
set stringMatch3 [CkStringBuilder_ContentsEqual $stringCompare "AssumeRoleWithWebIdentityResponse" 1]
if {$stringMatch3 == 1} then {
set credentialPath "AssumeRoleWithWebIdentityResult|Credentials"
}
CkStringBuilder_SetString $stringCompare [CkXml_tag $tokenXml]
set stringMatch4 [CkStringBuilder_ContentsEqual $stringCompare "AssumeRoleWithSAMLResponse" 1]
if {$stringMatch4 == 1} then {
set credentialPath "AssumeRoleWithSAMLResult|Credentials"
}
CkStringBuilder_SetString $stringCompare [CkXml_tag $tokenXml]
set stringMatch5 [CkStringBuilder_ContentsEqual $stringCompare "GetFederationTokenResponse" 1]
if {$stringMatch5 == 1} then {
set credentialPath "GetFederationTokenResult|Credentials"
}
CkStringBuilder_SetString $stringCompare [CkXml_tag $tokenXml]
set stringMatch6 [CkStringBuilder_ContentsEqual $stringCompare "GetDelegatedAccessTokenResponse" 1]
if {$stringMatch6 == 1} then {
set credentialPath "GetDelegatedAccessTokenResult|Credentials"
}
CkStringBuilder_SetString $stringCompare [CkXml_tag $tokenXml]
set stringMatch7 [CkStringBuilder_ContentsEqual $stringCompare "AssumeRootResponse" 1]
if {$stringMatch7 == 1} then {
set credentialPath "AssumeRootResult|Credentials"
}
CkStringBuilder_SetString $stringCompare $credentialPath
set stringMatch8 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {$stringMatch8 == 1} then {
puts "Unsupported STS credential response."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
exit
}
set path [new_CkStringBuilder]
set success [CkStringBuilder_SetString $path $credentialPath]
if {$success != 1} then {
puts "Could not construct credential path."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set success [CkStringBuilder_Append $path "|AccessKeyId"]
if {$success != 1} then {
puts "Could not construct credential path."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set accessKey [CkXml_getChildContent $tokenXml [CkStringBuilder_getAsString $path]]
CkStringBuilder_SetString $stringCompare $accessKey
set stringMatch9 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkXml_get_LastMethodSuccess $tokenXml] != 1] || [$stringMatch9 == 1]} then {
puts "Missing credential field AccessKeyId."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set success [CkStringBuilder_SetString $path $credentialPath]
if {$success != 1} then {
puts "Could not construct credential path."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set success [CkStringBuilder_Append $path "|SecretAccessKey"]
if {$success != 1} then {
puts "Could not construct credential path."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set secretKey [CkXml_getChildContent $tokenXml [CkStringBuilder_getAsString $path]]
CkStringBuilder_SetString $stringCompare $secretKey
set stringMatch10 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkXml_get_LastMethodSuccess $tokenXml] != 1] || [$stringMatch10 == 1]} then {
puts "Missing credential field SecretAccessKey."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set success [CkStringBuilder_SetString $path $credentialPath]
if {$success != 1} then {
puts "Could not construct credential path."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set success [CkStringBuilder_Append $path "|SessionToken"]
if {$success != 1} then {
puts "Could not construct credential path."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set sessionToken [CkXml_getChildContent $tokenXml [CkStringBuilder_getAsString $path]]
CkStringBuilder_SetString $stringCompare $sessionToken
set stringMatch11 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkXml_get_LastMethodSuccess $tokenXml] != 1] || [$stringMatch11 == 1]} then {
puts "Missing credential field SessionToken."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set success [CkStringBuilder_SetString $path $credentialPath]
if {$success != 1} then {
puts "Could not construct credential path."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set success [CkStringBuilder_Append $path "|Expiration"]
if {$success != 1} then {
puts "Could not construct credential path."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set expiration [CkXml_getChildContent $tokenXml [CkStringBuilder_getAsString $path]]
CkStringBuilder_SetString $stringCompare $expiration
set stringMatch12 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkXml_get_LastMethodSuccess $tokenXml] != 1] || [$stringMatch12 == 1]} then {
puts "Missing credential field Expiration."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
exit
}
set expires [new_CkDateTime]
set now [new_CkDateTime]
set success [CkDateTime_SetFromTimestamp $expires $expiration]
if {$success != 1} then {
puts "Stored expiration is invalid."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
exit
}
set success [CkDateTime_SetFromCurrentSystemTime $now]
if {$success != 1} then {
puts "Could not read current time."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
exit
}
if {[CkDateTime_DiffSeconds $expires $now] <= 60} then {
puts "Stored credentials are expired or expire within 60 seconds; obtain fresh credentials."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
exit
}
set session [new_CkJsonObject]
set success [CkJsonObject_UpdateString $session "access_key" $accessKey]
if {$success != 1} then {
puts "Could not build normalized session."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
exit
}
set success [CkJsonObject_UpdateString $session "secret_key" $secretKey]
if {$success != 1} then {
puts "Could not build normalized session."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
exit
}
set success [CkJsonObject_UpdateString $session "session_token" $sessionToken]
if {$success != 1} then {
puts "Could not build normalized session."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
exit
}
set success [CkJsonObject_UpdateString $session "expiration" $expiration]
if {$success != 1} then {
puts "Could not build normalized session."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
exit
}
set download [new_CkJsonObject]
CkJsonObject_put_Strict $download 1
set success [CkJsonObject_LoadFile $download "qa_data/aws/sts_download.json"]
if {$success != 1} then {
puts "Could not load download configuration."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
delete_CkJsonObject $download
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $download "bucket"]
set stringMatch13 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $download "bucket"] != 1] || [$stringMatch13 == 1]} then {
puts "Missing bucket."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
delete_CkJsonObject $download
exit
}
CkStringBuilder_SetString $stringCompare [CkJsonObject_stringOf $download "object_key"]
set stringMatch14 [CkStringBuilder_ContentsEqual $stringCompare "" 1]
if {expr [[CkJsonObject_JsonTypeOf $download "object_key"] != 1] || [$stringMatch14 == 1]} then {
puts "Missing object_key."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
delete_CkJsonObject $download
exit
}
set http [new_CkHttp]
CkHttp_put_RequireSslCertVerify $http 1
CkHttp_put_FollowRedirects $http 0
CkHttp_put_ConnectTimeout $http 20
CkHttp_put_ReadTimeout $http 60
CkHttp_put_VerboseLogging $http 0
CkHttp_put_AwsAccessKey $http [CkJsonObject_stringOf $session "access_key"]
CkHttp_put_AwsSecretKey $http [CkJsonObject_stringOf $session "secret_key"]
CkHttp_put_AwsSessionToken $http [CkJsonObject_stringOf $session "session_token"]
CkHttp_put_AwsRegion $http "us-west-2"
CkHttp_put_AwsEndpoint $http "s3.us-west-2.amazonaws.com"
# Current Chilkat S3 methods use Signature Version 4.
set objectData [new_CkBinData]
set success [CkHttp_S3_DownloadBd $http [CkJsonObject_stringOf $download "bucket"] [CkJsonObject_stringOf $download "object_key"] $objectData]
if {$success != 1} then {
puts "S3 download failed. HTTP status: [CkHttp_get_LastStatus $http]"
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
delete_CkJsonObject $download
delete_CkHttp $http
delete_CkBinData $objectData
exit
}
if {[CkHttp_get_LastStatus $http] != 200} then {
puts "Unexpected S3 status; do not use the data."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
delete_CkJsonObject $download
delete_CkHttp $http
delete_CkBinData $objectData
exit
}
puts "Downloaded bytes: [CkBinData_get_NumBytes $objectData]"
# Consume objectData in memory; no object contents or credentials are logged.
puts "Example completed."
delete_CkStringBuilder $stringCompare
delete_CkXml $tokenXml
delete_CkStringBuilder $path
delete_CkDateTime $expires
delete_CkDateTime $now
delete_CkJsonObject $session
delete_CkJsonObject $download
delete_CkHttp $http
delete_CkBinData $objectData