I want to get the tags information of AWS IAM users using rest service or AWS Java SDK. I am not able to find any AWS SDK example or class to get the IAM user tag information. Below is the example how I am getting list of users but I want to pull their tags detail also
AmazonIdentityManagement iam= AmazonIdentityManagementClientBuilder.standard().withCredentials(awCreds).withRegion(Regions.US_EAST_1).build(); ListiamUsers=iam.listUsers().getUsers();
Can someone tell me how to do that?
I tried below program also to list the tags of a user but I am getting below error: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
public class ListUserTags {
public static void main(String args[]) {
String kSecret = "adfdskjfdjssfjdjfdsdumykeysecret3eiiid";
String dateStamp = getConvertedDateString("yyyyMMdd", new Date());
System.out.println("dateStamp:" dateStamp);
String regionName = "us-east-1";
String serviceName = "iam";
String xmzDate = getConvertedDateString("yyyyMMdd'T'HHmmss'Z'", new Date());;
System.out.println("date:" xmzDate);
String signature = getSignatureKey(kSecret, dateStamp, regionName, serviceName); //signature
System.out.println("signature:" signature);
OkHttpClient client = new OkHttpClient().clone();
//MediaType mediaType = MediaType.parse("text/plain");
Request request = new Request.Builder()
.url("https://iam.amazonaws.com/?Action=ListUserTags&Version=2010-05-08&UserName=SVC_ServiceAccount04")
.method("GET", null)
.addHeader("host", "iam.amazonaws.com")
.addHeader("X-Amz-Date", xmzDate)
.addHeader("Authorization", "AWS4-HMAC-SHA256 Credential=ABC787dsfsdDummyAccessKey/" dateStamp "/us-east-1/iam/aws4_request, SignedHeaders=host;x-amz-date, Signature=" signature)
System.out.println("req:" request.headers().toString());
Response response = client.newCall(request).execute();
}
static byte[] HmacSHA256(String data, byte[] key) throws Exception {
String algorithm = "HmacSHA256";
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(data.getBytes("UTF-8"));
}
static String getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
byte[] kSecret = ("AWS4" key).getBytes("UTF-8");
byte[] kDate = HmacSHA256(dateStamp, kSecret);
byte[] kRegion = HmacSHA256(regionName, kDate);
byte[] kService = HmacSHA256(serviceName, kRegion);
byte[] kSigning = HmacSHA256("aws4_request", kService);
return Hex.encodeHexString(kSigning);
}
public static String getConvertedDateString(String format, Date date) {
return new SimpleDateFormat(format).format(date);
}
}
CodePudding user response:
Have you tried the ListUserTags. Seems to be what you're looking for
CodePudding user response:
Fetching the tags for a user can be done as follows:
public static void main(String[] args) {
// Please consider using aws configure and avoid hard-coding credentials
BasicAWSCredentials awsCredentials = new BasicAWSCredentials("access_key_id", "secret_key_id");
AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
.build();
// Create a request with the username for which we want to query the tags
ListUserTagsRequest request = new ListUserTagsRequest().withUserName("username");
ListUserTagsResult result;
List<Tag> tags = new ArrayList<>();
do {
result = iam.listUserTags(request);
tags.addAll(result.getTags());
request.setMarker(result.getMarker());
} while (result.isTruncated());
// Do something with the tags
System.out.println(tags);
}
I think the code this self-explanatory. More in-depth documentation for AWS IAM Java sdk can be found here.