How to remove unreadable characters from aws policy document?

35 views Asked by At

When I get the GetRolePolicyResponse.PolicyDocument. Its unreadable character. how to convert this to readable characters.

var inlinePolicyDetails = iamClient.GetRolePolicyAsync(new GetRolePolicyRequest
                {
                    RoleName = "xyzRole",
                    PolicyName = "xyzPolicy"
                }).GetAwaiter().GetResult();
                Console.WriteLine(inlinePolicyDetails.PolicyDocument);

The output looks like this:

%7B%0A%20%20%20%20%22Version%22%3A%20%222012-10-17%22%2C%0A%20%20%20%20%22Statement%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Action%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22XYZ%3AABCD%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Effect%22%3A%20%22Allow%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Resource%22%3A%20%22%2A%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%0A%7D
           

It suppose to look like this:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "XYZ:ABCD" ], "Resource": "*" } ] }

1

There are 1 answers

0
Shankar S On

The policy document is basically UrlEncoded and hence it has all special characters are url encoded and hence you have to decode to make it readable text

 var inlinePolicyDetails = iamClient.GetRolePolicyAsync(new GetRolePolicyRequest
                    {
                        RoleName = "xyzRole",
                        PolicyName = "xyzPolicy"
                    }).GetAwaiter().GetResult();
                    Console.WriteLine(inlinePolicyDetails.PolicyDocument);
     
Console.WriteLine(HttpUtility.UrlDecode(inlinePolicyDetails.PolicyDocument));

more details: https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/iam-policies-display.html