public void Should_Authenticate_Test_Admin_Identity()
        {
            IIdentityProvider serviceProvider = new CloudIdentityProvider(_testAdminIdentity);
            var userAccess = serviceProvider.Authenticate();

            Assert.IsNotNull(userAccess);
        }
        public void TestValidateToken()
        {
            IIdentityProvider provider = new CloudIdentityProvider(Bootstrapper.Settings.TestIdentity);
            UserAccess userAccess = provider.Authenticate();

            Assert.IsNotNull(userAccess);
            Assert.IsNotNull(userAccess.Token);
            Assert.IsNotNull(userAccess.Token.Id);

            try
            {
                UserAccess validated = provider.ValidateToken(userAccess.Token.Id);
                Assert.IsNotNull(validated);
                Assert.IsNotNull(validated.Token);
                Assert.AreEqual(userAccess.Token.Id, validated.Token.Id);

                Assert.IsNotNull(validated.User);
                Assert.AreEqual(userAccess.User.Id, validated.User.Id);
                Assert.AreEqual(userAccess.User.Name, validated.User.Name);
                Assert.AreEqual(userAccess.User.DefaultRegion, validated.User.DefaultRegion);
            }
            catch (UserNotAuthorizedException ex)
            {
                if (ex.Response.StatusCode != HttpStatusCode.Forbidden)
                    throw;

                Assert.Inconclusive("The service does not allow this user to access the Validate Token API.");
            }
        }
Example #3
0
        public static void Main(string[] args)
        {
            if (args.Length < 2 || args.Length > 3)
            {
                Console.WriteLine("Usage: {0} username api_key [region (US|UK)]", Environment.CommandLine);
                Environment.Exit(1);
            }
            RackspaceImpersonationIdentity auth = new RackspaceImpersonationIdentity();
            auth.Username = args[0];
            auth.APIKey = args[1];

            if (args.Length == 3)
            {
                if (args[2] != "UK" && args[2] != "US")
                {
                    Console.WriteLine("region must be either US or UK", Environment.CommandLine);
                    Environment.Exit(1);
                }
                switch (args[2])
                {
                    case "UK": { auth.CloudInstance = CloudInstance.UK; }; break;
                    case "US": { auth.CloudInstance = CloudInstance.Default; }; break;
                }
            }
            else
            {
                auth.CloudInstance = CloudInstance.Default;
            }

            try
            {
                IIdentityProvider identityProvider = new CloudIdentityProvider();
                var userAccess = identityProvider.Authenticate(auth);
            }
            catch (ResponseException ex2)
            {
                Console.WriteLine("Authentication failed with the following message: {0}", ex2.Message);
                Environment.Exit(1);
            }

            var cloudServers = new CloudServersProvider(auth);
            var servers = cloudServers.ListServers();
            foreach (Server serv in servers)
            {
                var date = System.DateTime.Now;
                var success = serv.CreateSnapshot(serv.Name + "_" + date.Year + "-" + date.Month + "-" + date.Day);
                if (success)
                {
                    Console.WriteLine("Image for server {0} has been created successfully.", serv.Name);
                }
                else
                {
                    Console.WriteLine("Image for server {0} could not be created.", serv.Name);
                }
            }
        }
Example #4
0
        private CloudIdentity CreateIdentity(string username, string apikey) {

            var result = new CloudIdentity() {
                Username = username,
                APIKey = apikey
            };

            var provider = new CloudIdentityProvider();
            var ua = provider.Authenticate(result);
            return result;
        }
        public void Should_Not_Hit_Cache_When_Authenticating_The_First_Time()
        {
            var cacheMock = new Mock<ICache<UserAccess>>();
            var restServiceMock = new Mock<IRestService>();

            restServiceMock.Setup(m => m.Execute<AuthenticationResponse>(It.IsAny<string>(), It.IsAny<HttpMethod>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<RequestSettings>())).Returns(new Response<AuthenticationResponse>(200, "OK", new AuthenticationResponse(), new List<HttpHeader>(), null));
            restServiceMock.Setup(m => m.Execute<AuthenticationResponse>(It.IsAny<Uri>(), It.IsAny<HttpMethod>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<RequestSettings>())).Returns(new Response<AuthenticationResponse>(200, "OK", new AuthenticationResponse(), new List<HttpHeader>(), null));

            var identityProvider = new CloudIdentityProvider(restServiceMock.Object, cacheMock.Object);

            identityProvider.Authenticate(new RackspaceCloudIdentity());

            cacheMock.Verify(m => m.Get(It.IsAny<string>(), It.IsAny<Func<UserAccess>>(), true), Times.Once());
        }
        public void Should_Always_Request_Fresh_Data_From_Cache_When_Authenticating()
        {
            var cacheMock = new Mock<ICache<UserAccess>>();
            var restServiceMock = new Mock<IRestService>();

            restServiceMock.Setup(m => m.Execute<AuthenticationResponse>(It.IsAny<string>(), It.IsAny<HttpMethod>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<RequestSettings>())).Returns(new Response<AuthenticationResponse>(200, "OK", new AuthenticationResponse(), new List<HttpHeader>(), null));
            restServiceMock.Setup(m => m.Execute<AuthenticationResponse>(It.IsAny<Uri>(), It.IsAny<HttpMethod>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<Dictionary<string, string>>(), It.IsAny<RequestSettings>())).Returns(new Response<AuthenticationResponse>(200, "OK", new AuthenticationResponse(), new List<HttpHeader>(), null));
            
            var identityProvider = new CloudIdentityProvider(restServiceMock.Object, cacheMock.Object);

            for (int i = 0; i < 100; i++)
            {
                identityProvider.Authenticate(new RackspaceCloudIdentity());
            }

            cacheMock.Verify(m => m.Get(It.IsAny<string>(), It.IsAny<Func<UserAccess>>(), true), Times.Exactly(100));
        }
    public async Task Run(string username, string apiKey, string region)
    {
        // Configure authentication
        var identity = new CloudIdentity
        {
            Username = username,
            APIKey = apiKey
        };
        var identityService = new CloudIdentityProvider(identity);
        var result = identityService.Authenticate();

        var serverService = new CloudServersProvider(identity, region, null, null);
        var rackConnectService = new RackConnectService(identityService, region);

        // Create a cloud server on your hybrid network
        Console.WriteLine($"Looking up your RackConnect network in {region}...");
        var networks = await rackConnectService.ListNetworksAsync();
        var network = networks.FirstOrDefault();
        if (network == null)
            throw new Exception($"You do not have a Hybrid Cloud / RackConnect network configured in the {region} which is required to run this sample.");

        Console.WriteLine("Creating sample cloud server... ");
        // Ubuntu 14.04 LTS (Trusty Tahr) (PVHVM)
        const string ubuntuImageId = "09de0a66-3156-48b4-90a5-1cf25a905207";
        // 512MB Standard Instance
        const string standardFlavorId = "2";
        var requestedServer = serverService.CreateServer("sample", ubuntuImageId, standardFlavorId,
            networks: new string[] {network.Id});
        serverService.WaitForServerActive(requestedServer.Id);

        Console.WriteLine("Allocating a public IP address...");
        var ip = await rackConnectService.CreatePublicIPAsync(
            new PublicIPCreateDefinition {ShouldRetain = true});
        await ip.WaitUntilActiveAsync();
        Console.WriteLine($"Acquired {ip.PublicIPv4Address}!");

        Console.WriteLine("Assigning the public IP to the sample cloud server...");
        await ip.AssignAsync(requestedServer.Id);
        await ip.WaitUntilActiveAsync();

        Console.WriteLine("Deleting sample cloud server...");
        serverService.DeleteServer(requestedServer.Id);

        Console.WriteLine("Deallocating the public IP address...");
        await ip.DeleteAsync();
    }
        public void TestAuthenticate()
        {
            IIdentityProvider provider = new CloudIdentityProvider(Bootstrapper.Settings.TestIdentity);
            UserAccess userAccess = provider.Authenticate();

            Assert.IsNotNull(userAccess);
            Assert.IsNotNull(userAccess.Token);
            Assert.IsNotNull(userAccess.Token.Id);
            Assert.IsNotNull(userAccess.Token.Expires);
            Assert.IsFalse(userAccess.Token.IsExpired);

            Assert.IsNotNull(userAccess.User);
            Assert.IsNotNull(userAccess.User.Id);
            Assert.IsNotNull(userAccess.User.Name);

            Assert.IsNotNull(userAccess.ServiceCatalog);

            Console.WriteLine(JsonConvert.SerializeObject(userAccess, Formatting.Indented));
        }
        public void Should_Throw_Error_When_Authenticating_With_Invalid_Password()
        {
            var identity = new RackspaceCloudIdentity()
                               {
                                   Username = _testIdentity.Username,
                                   Password = "******"
                               };
            IIdentityProvider serviceProvider = new CloudIdentityProvider(identity);

            try
            {
                var userAccess = serviceProvider.Authenticate();

                throw new Exception("This code path is invalid, exception was expected.");
            }
            catch (net.openstack.Core.Exceptions.Response.ResponseException)
            {
                Assert.IsTrue(true);
            }
        }
Example #10
0
        static bool Login()
        {
            auth = new RackspaceCloudIdentity();
            auth.Username = Username;
            auth.Password = Password;
            auth.APIKey = APIKey;
            auth.CloudInstance = AccountRegion == "LON" ? CloudInstance.UK : CloudInstance.Default;

            try
            {
                CloudIdentityProvider identityProvider = new CloudIdentityProvider();
                var userAccess = identityProvider.Authenticate(auth);
            }
            catch (Exception ex)
            {
                PrintException(ex);
                return false;
            }

            return true;
        }
        //Method to Authenticate the credentials and obtain token
        public void Authenticate()
        {
            try
            {
                //Creating an instance of CloudIdentityProvider providing the CloudIdentity object and urls in the constructor
                provider = new CloudIdentityProvider(identity, "https://identity.api.rackspacecloud.com/v1.0", "https://lon.identity.api.rackspacecloud.com/v1.0");
                //Calling the authenticate method which returns an UserAccess object containing token and user details
                UserAccess access = provider.Authenticate(identity);

                token = access.Token;

                userdetails = access.User;
                //Set the TokenId property
                TokenId = token.Id;
                //Verify with other programmers to make this method void
                cloudFiles = new CloudFilesProvider(identity);
                //return token.Id;
            }

            catch (UserAuthenticationException ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            catch (UserAuthorizationException ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            catch (UserNotAuthorizedException ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw ex;
            }
        }
        public void Should_Authenticate_NewUser()
        {
            Assert.IsNotNull(_testUser);

            IIdentityProvider provider = new CloudIdentityProvider();

            var userAccess =
                provider.Authenticate(new RackspaceCloudIdentity
                                          {Username = _testUser.Username, Password = _newTestUserPassword});

            Assert.IsNotNull(userAccess);
        }
Example #13
0
        public static void Main(string[] args)
        {
            Boolean containerExists = false;
            if (args.Length < 4 || args.Length > 5)
            {
                Console.WriteLine("Usage: {0} username api_key target_container path_to_file [region (US|UK)]", Environment.CommandLine);
                Environment.Exit(1);
            }
            RackspaceCloudIdentity auth = new RackspaceCloudIdentity();
            IEnumerable<Container> containerList = null;
            auth.Username = args[0];
            auth.APIKey = args[1];
            targetContainer = args[2];
            filePath = args[3];
            if (args.Length == 5)
            {
                if (args[4] != "UK" && args[4] != "US")
                {
                    Console.WriteLine("region must be either US or UK", Environment.CommandLine);
                    Environment.Exit(1);
                }
                switch (args[4])
                {
                    case "UK": {auth.CloudInstance = CloudInstance.UK;};break;
                    case "US": { auth.CloudInstance = CloudInstance.Default;}; break;
                }
            }

            try
            {
                IIdentityProvider identityProvider = new CloudIdentityProvider();
                var userAccess = identityProvider.Authenticate(auth);
            }
            catch (ResponseException ex2)
            {
                Console.WriteLine("Authentication failed with the following message: {0}",ex2.Message);
                Environment.Exit(1);
            }

            try
            {
                var cloudFilesProvider = new CloudFilesProvider(auth);
                containerList = cloudFilesProvider.ListContainers();

                foreach (Container container in containerList)
                {
                    if (container.Name == targetContainer)
                    {
                        containerExists = true;
                        break;
                    }
                }

                if (!containerExists)
                {
                    Console.WriteLine("Container \"{0}\" does not exist on the provided CloudFiles account.", targetContainer);
                    Environment.Exit(1);
                }
                if (!File.Exists(filePath))
                {
                    Console.WriteLine("The file specified ({0}) does not exist", filePath);
                    Environment.Exit(1);
                }
                cloudFilesProvider.CreateObjectFromFile(targetContainer, @filePath, Path.GetFileName(filePath));
            }
            catch (Exception ex2)
            {
                Console.WriteLine(ex2.Message);
                Environment.Exit(1);
            }
            Console.WriteLine("*SUCCESS* File: \"{0}\" uploaded to \"{1}\"", filePath, targetContainer);
        }
Example #14
0
        public void TestListEndpoints()
        {
            IIdentityProvider provider = new CloudIdentityProvider(Bootstrapper.Settings.TestIdentity);
            UserAccess userAccess = provider.Authenticate();
            Assert.IsNotNull(userAccess);
            Assert.IsNotNull(userAccess.Token);
            Assert.IsNotNull(userAccess.Token.Id);

            try
            {
                IEnumerable<ExtendedEndpoint> endpoints = provider.ListEndpoints(userAccess.Token.Id);
                Console.WriteLine(JsonConvert.SerializeObject(userAccess, Formatting.Indented));
            }
            catch (UserNotAuthorizedException ex)
            {
                if (ex.Response.StatusCode != HttpStatusCode.Forbidden)
                    throw;

                Assert.Inconclusive("The service does not allow this user to access the List Endpoints API.");
            }
        }