Example #1
0
        private static async Task <User> CreateIamUserAsync(AWSCredentials credentials, string userName)
        {
            using (var client = new Amazon.IdentityManagement.AmazonIdentityManagementServiceClient(credentials,
                                                                                                    RegionEndpoint.EUCentral1))
            {
                var request  = new CreateUserRequest(userName);
                var response = await client.CreateUserAsync(request);

                Console.WriteLine($"User {userName} was created.");


                //create creds for user
                var createKeyRequest = new CreateAccessKeyRequest {
                    UserName = response.User.UserName
                };
                var accessKeyResponse = await client.CreateAccessKeyAsync(createKeyRequest);

                //add policy to user (demo)
                var attachUserPolicyRequest = new AttachUserPolicyRequest();
                attachUserPolicyRequest.UserName  = userName;
                attachUserPolicyRequest.PolicyArn = "arn:aws:iam::aws:policy/AWSElementalMediaStoreFullAccess";
                await client.AttachUserPolicyAsync(attachUserPolicyRequest);


                return(response.User);
            }
        }
Example #2
0
        private static void CreateAccessKey(String User)
        {
            if (String.IsNullOrEmpty(Token))
            {
                stsClient = new AmazonIdentityManagementServiceClient(AccessKeyId, SecretKey, iamconfig);
            }
            else
            {
                stsClient = new AmazonIdentityManagementServiceClient(AccessKeyId, SecretKey, Token, iamconfig);
            }

            try
            {
                CreateAccessKeyRequest accesskeyReq = new CreateAccessKeyRequest();
                if (!String.IsNullOrEmpty(User))
                {
                    accesskeyReq.UserName = User;
                }
                CreateAccessKeyResponse response = stsClient.CreateAccessKey(accesskeyReq);
                Console.WriteLine("Access keys :{0}, Secret Key: {1}", response.AccessKey.AccessKeyId,
                                  response.AccessKey.SecretAccessKey);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error occured while creating user. " + ex.ToString());
            }
        }
Example #3
0
        // snippet-end:[IAM.dotnetv3.CreateUserAsync]

        // snippet-start:[IAM.dotnetv3.CreateAccessKey]

        /// <summary>
        /// Create a new AccessKey for the user.
        /// </summary>
        /// <param name="client">The initialized IAM client object.</param>
        /// <param name="userName">The name of the user for whom to create the key.</param>
        /// <returns>A new IAM access key for the user.</returns>
        public static async Task <AccessKey> CreateAccessKeyAsync(
            AmazonIdentityManagementServiceClient client,
            string userName)
        {
            var request = new CreateAccessKeyRequest
            {
                UserName = userName,
            };

            var response = await client.CreateAccessKeyAsync(request);

            if (response.AccessKey is not null)
            {
                Console.WriteLine($"Successfully created Access Key for {userName}.");
            }

            return(response.AccessKey);
        }
Example #4
0
        public async Task CreateAccessKeyAsyncTest()
        {
            var mockIAMClient = new Mock <AmazonIdentityManagementServiceClient>();

            mockIAMClient.Setup(client => client.CreateAccessKeyAsync(
                                    It.IsAny <CreateAccessKeyRequest>(),
                                    It.IsAny <CancellationToken>()
                                    )).Callback <CreateAccessKeyRequest, CancellationToken>((request, token) =>
            {
                if (!string.IsNullOrEmpty(request.UserName))
                {
                    Assert.Equal(request.UserName, UserName);
                }
            }).Returns((CreateAccessKeyRequest r, CancellationToken token) =>
            {
                return(Task.FromResult(new CreateAccessKeyResponse()
                {
                    AccessKey = new AccessKey
                    {
                        UserName = UserName,
                    },
                    HttpStatusCode = System.Net.HttpStatusCode.OK,
                }));
            });

            var client  = mockIAMClient.Object;
            var request = new CreateAccessKeyRequest
            {
                UserName = UserName,
            };

            var response = await client.CreateAccessKeyAsync(request);

            var ok = (response.AccessKey.UserName == UserName) && (response.HttpStatusCode == System.Net.HttpStatusCode.OK);

            Assert.True(ok, "Could not create AccessKey.");
        }
 public Task <CreateAccessKeyResponse> CreateAccessKeyAsync(CreateAccessKeyRequest request,
                                                            CancellationToken cancellationToken = new CancellationToken())
 {
     throw new System.NotImplementedException();
 }