Beispiel #1
0
        public async Task <AttachedPolicyType[]> ListAttachedRolePoliciesAsync(string roleName, string patchPrefix = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            ListAttachedRolePoliciesResponse response = null;
            var results = new List <AttachedPolicyType>();

            while ((response = await _IAMClient.ListAttachedRolePoliciesAsync(new ListAttachedRolePoliciesRequest()
            {
                MaxItems = 1000,
                Marker = response?.Marker,
                RoleName = roleName,
                PathPrefix = patchPrefix
            }, cancellationToken).EnsureSuccessAsync()) != null)
            {
                if (!response.AttachedPolicies.IsNullOrEmpty())
                {
                    results.AddRange(response.AttachedPolicies);
                }

                if (!response.IsTruncated)
                {
                    break;
                }

                await Task.Delay(100);
            }

            return(results.ToArray());
        }
        /// <summary>
        /// Unmarshaller the response from the service to the response class.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override AmazonWebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            ListAttachedRolePoliciesResponse response = new ListAttachedRolePoliciesResponse();

            context.Read();
            int targetDepth = context.CurrentDepth;

            while (context.ReadAtDepth(targetDepth))
            {
                if (context.IsStartElement)
                {
                    if (context.TestExpression("ListAttachedRolePoliciesResult", 2))
                    {
                        UnmarshallResult(context, response);
                        continue;
                    }

                    if (context.TestExpression("ResponseMetadata", 2))
                    {
                        response.ResponseMetadata = ResponseMetadataUnmarshaller.Instance.Unmarshall(context);
                    }
                }
            }

            return(response);
        }
        private static void UnmarshallResult(XmlUnmarshallerContext context, ListAttachedRolePoliciesResponse response)
        {
            int originalDepth = context.CurrentDepth;
            int targetDepth   = originalDepth + 1;

            if (context.IsStartOfDocument)
            {
                targetDepth += 2;
            }

            while (context.ReadAtDepth(originalDepth))
            {
                if (context.IsStartElement || context.IsAttribute)
                {
                    if (context.TestExpression("AttachedPolicies/member", targetDepth))
                    {
                        var unmarshaller = AttachedPolicyTypeUnmarshaller.Instance;
                        var item         = unmarshaller.Unmarshall(context);
                        response.AttachedPolicies.Add(item);
                        continue;
                    }
                    if (context.TestExpression("IsTruncated", targetDepth))
                    {
                        var unmarshaller = BoolUnmarshaller.Instance;
                        response.IsTruncated = unmarshaller.Unmarshall(context);
                        continue;
                    }
                    if (context.TestExpression("Marker", targetDepth))
                    {
                        var unmarshaller = StringUnmarshaller.Instance;
                        response.Marker = unmarshaller.Unmarshall(context);
                        continue;
                    }
                }
            }

            return;
        }
Beispiel #4
0
        public async Task AttachIamRolePolicy(IAmazonIdentityManagementService iamClient, string roleName,
                                              string policyArn)
        {
            try
            {
                Console.WriteLine("Attaching IAM Role Policy");
                ListAttachedRolePoliciesResponse response = await iamClient.ListAttachedRolePoliciesAsync(
                    new ListAttachedRolePoliciesRequest()
                {
                    RoleName = roleName
                });

                List <AttachedPolicyType> attachedPolicies = response.AttachedPolicies;

                foreach (var attachedPolicyType in attachedPolicies)
                {
                    if (policyArn.Equals(attachedPolicyType.PolicyArn))
                    {
                        Console.WriteLine($"Policy : {policyArn} is already attached to role {roleName}");
                        return;
                    }
                }

                await iamClient.AttachRolePolicyAsync(new AttachRolePolicyRequest()
                {
                    RoleName  = roleName,
                    PolicyArn = policyArn
                });

                Console.WriteLine($"Successfully attached policy : {policyArn} to role {roleName}");
            }
            catch (Exception e)
            {
                Console.WriteLine($"IAM policy attach failed: {e}");
                throw;
            }
        }