Zig
Zig
AWS Security Token Service (STS) AssumeRole
See more AWS Security Token Service Examples
Returns a set of temporary security credentials that you can use to access AWS resources. These temporary credentials consist of an access key ID, a secret access key, and a security token. Typically, you use AssumeRole within your account or for cross-account access.Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
const rest = try chilkat.Rest.init();
defer rest.deinit();
// Connect to the Amazon AWS REST server.
// such as https://sts.us-west-2.amazonaws.com/
const b_tls = true;
const port = 443;
const b_auto_reconnect = true;
try rest.connect("sts.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect);
// Provide AWS credentials for the REST call.
const auth_aws = try chilkat.AuthAws.init();
defer auth_aws.deinit();
auth_aws.setAccessKey("AWS_ACCESS_KEY");
auth_aws.setSecretKey("AWS_SECRET_KEY");
// the region should match our URL above..
// See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_enable-regions.html
auth_aws.setRegion("us-west-2");
auth_aws.setServiceName("sts");
rest.setAuthAws(auth_aws) catch {};
// Sample Request
// https://sts.amazonaws.com/
// ?Version=2011-06-15
// &Action=AssumeRole
// &RoleSessionName=testAR
// &RoleArn=arn:aws:iam::123456789012:role/demo
// &PolicyArns.member.1.arn=arn:aws:iam::123456789012:policy/demopolicy1
// &PolicyArns.member.2.arn=arn:aws:iam::123456789012:policy/demopolicy2
// &Policy={"Version":"2012-10-17","Statement":[{"Sid":"Stmt1",
// "Effect":"Allow","Action":"s3:*","Resource":"*"}]}
// &DurationSeconds=3600
// &Tags.member.1.Key=Project
// &Tags.member.1.Value=Pegasus
// &Tags.member.2.Key=Team
// &Tags.member.2.Value=Engineering
// &Tags.member.3.Key=Cost-Center
// &Tags.member.3.Value=12345
// &TransitiveTagKeys.member.1=Project
// &TransitiveTagKeys.member.2=Cost-Center
// &ExternalId=123ABC
// &SourceIdentity=Alice
// &AUTHPARAMS
rest.addQueryParam("Version", "2011-06-15") catch {};
rest.addQueryParam("Action", "AssumeRole") catch {};
rest.addQueryParam("DurationSeconds", "3600") catch {};
rest.addQueryParam("RoleSessionName", "testAR") catch {};
rest.addQueryParam("RoleArn", "arn:aws:iam::123456789012:role/demo") catch {};
rest.addQueryParam("PolicyArns.member.1.arn", "arn:aws:iam::123456789012:policy/demopolicy1") catch {};
rest.addQueryParam("PolicyArns.member.2.arn", "arn:aws:iam::123456789012:policy/demopolicy2") catch {};
rest.addQueryParam("Policy", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"Stmt1\",\"Effect\":\"Allow\",\"Action\":\"s3:*\",\"Resource\":\"*\"}]}") catch {};
rest.addQueryParam("Tags.member.1.Key", "Project") catch {};
rest.addQueryParam("Tags.member.1.Value", "Pegasus") catch {};
rest.addQueryParam("Tags.member.2.Key", "Team") catch {};
rest.addQueryParam("Tags.member.2.Value", "Engineering") catch {};
rest.addQueryParam("Tags.member.3.Key", "Cost-Center") catch {};
rest.addQueryParam("Tags.member.3.Value", "12345") catch {};
rest.addQueryParam("TransitiveTagKeys.member.1", "Project") catch {};
rest.addQueryParam("TransitiveTagKeys.member.2", "Cost-Center") catch {};
rest.addQueryParam("ExternalId", "123ABC") catch {};
rest.addQueryParam("SourceIdentity", "Alice") catch {};
const response_xml = rest.fullRequestNoBody(alloc, "GET", "/") catch {
std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
return;
};
// A successful response will have a status code equal to 200.
if (rest.getResponseStatusCode() != 200) {
std.debug.print("response status code = {d}\n", .{rest.getResponseStatusCode()});
std.debug.print("response status text = {s}\n", .{try rest.getResponseStatusText(alloc)});
std.debug.print("response header: {s}\n", .{try rest.getResponseHeader(alloc)});
std.debug.print("response body: {s}\n", .{response_xml});
return;
}
// Examine the successful XML response (shown below)
const xml = try chilkat.Xml.init();
defer xml.deinit();
xml.loadXml(response_xml) catch {};
std.debug.print("{s}\n", .{try xml.getXml(alloc)});
// Sample response:
// <AssumeRoleResponse xmlns="https://sts.amazonaws.com/doc/2011-06-15/">
// <AssumeRoleResult>
// <SourceIdentity>Alice</SourceIdentity>
// <AssumedRoleUser>
// <Arn>arn:aws:sts::123456789012:assumed-role/demo/TestAR</Arn>
// <AssumedRoleId>ARO123EXAMPLE123:TestAR</AssumedRoleId>
// </AssumedRoleUser>
// <Credentials>
// <AccessKeyId>ASIAIOSFODNN7EXAMPLE</AccessKeyId>
// <SecretAccessKey>wJalrXUtnFEMI/K7MDENG/bPxRfiCYzEXAMPLEKEY</SecretAccessKey>
// <SessionToken>
// AQoDYXdzEPT//////////wEXAMPLEtc764bNrC9SAPBSM22wDOk4x4HIZ8j4FZTwdQW
// LWsKWHGBuFqwAeMicRXmxfpSPfIeoIYRqTflfKD8YUuwthAx7mSEI/qkPpKPi/kMcGd
// QrmGdeehM4IC1NtBmUpp2wUE8phUZampKsburEDy0KPkyQDYwT7WZ0wq5VSXDvp75YU
// 9HFvlRd8Tx6q6fE8YQcHNVXAkiY9q6d+xo0rKwT38xVqr7ZD0u0iPPkUL64lIZbqBAz
// +scqKmlzm8FDrypNC9Yjc8fPOLn9FX9KSYvKTr4rvx3iSIlTJabIQwj2ICCR/oLxBA==
// </SessionToken>
// <Expiration>2019-11-09T13:34:41Z</Expiration>
// </Credentials>
// <PackedPolicySize>6</PackedPolicySize>
// </AssumeRoleResult>
// <ResponseMetadata>
// <RequestId>c6104cbe-af31-11e0-8154-cbc7ccf896c7</RequestId>
// </ResponseMetadata>
// </AssumeRoleResponse>
// Sample parse code:
_ = try xml.getAttrValue(alloc, "xmlns");
_ = try xml.getChildContent(alloc, "AssumeRoleResult|SourceIdentity");
_ = try xml.getChildContent(alloc, "AssumeRoleResult|AssumedRoleUser|Arn");
_ = try xml.getChildContent(alloc, "AssumeRoleResult|AssumedRoleUser|AssumedRoleId");
_ = try xml.getChildContent(alloc, "AssumeRoleResult|Credentials|AccessKeyId");
_ = try xml.getChildContent(alloc, "AssumeRoleResult|Credentials|SecretAccessKey");
_ = try xml.getChildContent(alloc, "AssumeRoleResult|Credentials|SessionToken");
_ = try xml.getChildContent(alloc, "AssumeRoleResult|Credentials|Expiration");
_ = xml.getChildIntValue("AssumeRoleResult|PackedPolicySize");
_ = try xml.getChildContent(alloc, "ResponseMetadata|RequestId");
// Save the session token XML to a file for use by another Chilkat example..
try xml.saveXml("qa_data/tokens/aws_session_token.xml");
}