public async Task ShouldThrowWhenWindowsDocumentDoesNotContainFileSystem()
        {
            // in this test be careful if the specified credentials belong to admin user or not
            // to make this test pass you need to specify the credentials of a user who isn't an admin on this machine

            var client = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.User.UserName, FactIfWindowsAuthenticationIsAvailable.User.Password, FactIfWindowsAuthenticationIsAvailable.User.Domain));
            var server = GetServer();

            await client.UploadAsync("abc.bin", new MemoryStream(1));

            using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "ShouldThrow_WindowsDocumentDoesNotContainsThisFS",
                conventions: client.Conventions,
                credentials: new OperationCredentials(null, new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.User.UserName, FactIfWindowsAuthenticationIsAvailable.User.Password, FactIfWindowsAuthenticationIsAvailable.User.Domain))))
            {
                await anotherClient.EnsureFileSystemExistsAsync(); // will pass because by using this api key we have access to <system> database

                ErrorResponseException errorResponse = null;

                try
                {
                    await anotherClient.UploadAsync("def.bin", new MemoryStream(1)); // should throw because a file system ShouldThrow_WindowsDocumentDoesNotContainsThisFS isn't added to ApiKeyDefinition
                }
                catch (ErrorResponseException ex)
                {
                    errorResponse = ex;
                }

                Assert.NotNull(errorResponse);
                Assert.Equal(HttpStatusCode.Forbidden, errorResponse.StatusCode);
            }
        }
        public async Task WillUseDefaultNetworkCredentialsWhenServerRequiresAuthentication()
        {
            var server = CreateRavenDbServer(Ports[0], fileSystemName: "WillUseDefaultCredentials", enableAuthentication: true); // enable authentication

            using (var client = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "WillUseDefaultCredentials"))
            {
                await client.Admin.CreateFileSystemAsync(new FileSystemDocument()
                {
                    Id = "Raven/FileSystem/" + client.FileSystem,
                    Settings =
                    {
                        {"Raven/FileSystem/DataDir", Path.Combine("FileSystems", client.FileSystem)}
                    }
                });

                await client.UploadAsync("a", new MemoryStream(new byte[] { 1, 2 }));

                var ms = new MemoryStream();
                (await client.DownloadAsync("a")).CopyTo(ms);

                var array = ms.ToArray();
                Assert.Equal(1, array[0]);
                Assert.Equal(2, array[1]);
            }
        }
Exemple #3
0
        public async Task ShouldThrowWhenWindowsDocumentDoesNotContainFileSystem()
        {
            // in this test be careful if the specified credentials belong to admin user or not

            var client = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(username, password, domain));
            var server = GetServer();

            await client.UploadAsync("abc.bin", new RandomStream(3));

            using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "ShouldThrow_WindowsDocumentDoesnContainsThisFS",
                                                                  credentials: new NetworkCredential(username, password, domain)))
            {
                await anotherClient.EnsureFileSystemExistsAsync(); // will pass because by using this api key we have access to <system> database

                ErrorResponseException errorResponse = null;

                try
                {
                    await anotherClient.UploadAsync("def.bin", new RandomStream(1)); // should throw because a file system ShouldThrow_ApiKeyDoesnContainsThisFS isn't added to ApiKeyDefinition
                }
                catch (InvalidOperationException ex)
                {
                    errorResponse = ex.InnerException as ErrorResponseException;
                }

                Assert.NotNull(errorResponse);
                Assert.Equal(HttpStatusCode.Forbidden, errorResponse.StatusCode);
            }
        }
Exemple #4
0
        public IAsyncFilesSession OpenAsyncSession(OpenFilesSessionOptions sessionOptions)
        {
            AssertInitialized();
            EnsureNotClosed();

            if (string.IsNullOrWhiteSpace(sessionOptions.FileSystem))
            {
                throw new ArgumentException("Filesystem cannot be null, empty or whitespace.", "FileSystem");
            }


            var sessionId = Guid.NewGuid();

            currentSessionId = sessionId;
            try
            {
                var client = new AsyncFilesServerClient(this.Url, sessionOptions.FileSystem, sessionOptions.Credentials, sessionOptions.ApiKey);

                var session = new AsyncFilesSession(this, client, this.Listeners, sessionId);
                AfterSessionCreated(session);
                return(session);
            }
            finally
            {
                currentSessionId = null;
            }
        }
Exemple #5
0
        public async Task CanCreateAndDeleteFileSystem()
        {
            var client      = (IAsyncFilesCommandsImpl)NewAsyncClient();
            var adminClient = client.Admin;

            const string newFileSystemName = "testName_CanDeleteFileSystem";

            await adminClient.CreateOrUpdateFileSystemAsync(new FileSystemDocument
            {
                Id       = "Raven/FileSystem/" + newFileSystemName,
                Settings =
                {
                    { Constants.FileSystem.DataDirectory, Path.Combine("~", Path.Combine("FileSystems", newFileSystemName)) }
                }
            }, newFileSystemName);

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, newFileSystemName))
            {
                await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] { 1 }));
            }

            var names = await adminClient.GetNamesAsync();

            Assert.Contains(newFileSystemName, names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.NotNull(stats.FirstOrDefault(x => x.Name == newFileSystemName));

            await adminClient.DeleteFileSystemAsync(newFileSystemName);

            names = await adminClient.GetNamesAsync();

            Assert.DoesNotContain(newFileSystemName, names);
        }
        public async Task WillUseDefaultNetworkCredentialsWhenServerRequiresAuthentication_CommandsUsage()
        {
            var server = CreateServer(8079, fileSystemName: "WillUseDefaultCredentials", enableAuthentication: true); // enable authentication

            using (var client = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "WillUseDefaultCredentials"))
            {
                await client.Admin.CreateFileSystemAsync(new FileSystemDocument()
                {
                    Id       = "Raven/FileSystem/" + client.FileSystemName,
                    Settings =
                    {
                        { Constants.FileSystem.DataDirectory, Path.Combine("FileSystems", client.FileSystemName) }
                    }
                });

                await client.UploadAsync("a", new MemoryStream(new byte[] { 1, 2 }));

                await client.UploadAsync("b", new MemoryStream(new byte[] { 2, 1, 0 }));

                var ms = new MemoryStream();
                (await client.DownloadAsync("a")).CopyTo(ms);

                var array = ms.ToArray();
                Assert.Equal(1, array[0]);
                Assert.Equal(2, array[1]);

                ms = new MemoryStream();
                (await client.DownloadAsync("b")).CopyTo(ms);

                array = ms.ToArray();
                Assert.Equal(2, array[0]);
                Assert.Equal(1, array[1]);
                Assert.Equal(0, array[2]);
            }
        }
Exemple #7
0
        public async Task Can_get_stats_for_all_active_file_systems()
        {
            var client = NewAsyncClient();
            var server = GetServer();

            using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "test"))
            {
                await anotherClient.EnsureFileSystemExistsAsync();

                await client.UploadAsync("test1", new RandomStream(10));        // will make it active

                await anotherClient.UploadAsync("test1", new RandomStream(10)); // will make it active

                await client.UploadAsync("test2", new RandomStream(10));

                var stats = await anotherClient.Admin.GetStatisticsAsync();

                var stats1 = stats.FirstOrDefault(x => x.Name == client.FileSystemName);
                Assert.NotNull(stats1);
                var stats2 = stats.FirstOrDefault(x => x.Name == anotherClient.FileSystemName);
                Assert.NotNull(stats2);

                Assert.Equal(2, stats1.Metrics.Requests.Count);
                Assert.Equal(1, stats2.Metrics.Requests.Count);

                Assert.Equal(0, stats1.ActiveSyncs.Count);
                Assert.Equal(0, stats1.PendingSyncs.Count);

                Assert.Equal(0, stats2.ActiveSyncs.Count);
                Assert.Equal(0, stats2.PendingSyncs.Count);
            }
        }
Exemple #8
0
        public async Task ShouldThrowWhenUsedApiKeyDefinitionDoesNotContainFileSystem()
        {
            var client = NewAsyncClient(enableAuthentication: true, apiKey: apiKey);
            var server = GetServer();

            await client.UploadAsync("abc.bin", new RandomStream(3));

            using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "ShouldThrow_ApiKeyDoesnContainsThisFS", apiKey: apiKey))
            {
                await anotherClient.EnsureFileSystemExistsAsync(); // will pass because by using this api key we have access to <system> database

                ErrorResponseException errorResponse = null;

                try
                {
                    await anotherClient.UploadAsync("def.bin", new RandomStream(1)); // should throw because a file system ShouldThrow_ApiKeyDoesnContainsThisFS isn't added to ApiKeyDefinition
                }
                catch (ErrorResponseException ex)
                {
                    errorResponse = ex;
                }

                Assert.NotNull(errorResponse);
                Assert.Equal(HttpStatusCode.Forbidden, errorResponse.StatusCode);
            }
        }
Exemple #9
0
        public async Task AdminClientWorkWithOAuthEnabled()
        {
            var client      = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, apiKey: apiKey);
            var adminClient = client.Admin;

            await adminClient.CreateFileSystemAsync(new FileSystemDocument
            {
                Id       = "Raven/FileSystem/" + "testName",
                Settings =
                {
                    { Constants.FileSystem.DataDirectory, Path.Combine("~", Path.Combine("FileSystems", "testName")) }
                }
            }, "testName");

            var names = await adminClient.GetNamesAsync();

            Assert.Equal(2, names.Length);
            Assert.Contains("AdminClientWorkWithOAuthEnabled", names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.Equal(0, stats.Length);             // 0 because our fs aren't active

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName"))
            {
                await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] { 1 }));
            }

            await adminClient.DeleteFileSystemAsync("testName", true);
        }
Exemple #10
0
        public async Task AdminClientWorkWithWinAuthEnabled()
        {
            var client      = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));
            var adminClient = client.Admin;

            await adminClient.CreateFileSystemAsync(MultiDatabase.CreateFileSystemDocument("testName"), "testName");

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName", new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain)))
            {
                await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] { 1 }));
            }

            var names = await adminClient.GetNamesAsync();

            Assert.Contains("testName", names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.NotNull(stats.FirstOrDefault(x => x.Name == "testName"));

            await adminClient.DeleteFileSystemAsync("testName");

            names = await adminClient.GetNamesAsync();

            Assert.DoesNotContain("testName", names);
        }
Exemple #11
0
        public async Task ShouldThrowWhenWindowsDocumentDoesNotContainFileSystem()
        {
            // in this test be careful if the specified credentials belong to admin user or not
            // to make this test pass you need to specify the credentials of a user who isn't an admin on this machine

            var client = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.User.UserName, FactIfWindowsAuthenticationIsAvailable.User.Password, FactIfWindowsAuthenticationIsAvailable.User.Domain));
            var server = GetServer();

            await client.UploadAsync("abc.bin", new MemoryStream(1));

            using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "ShouldThrow_WindowsDocumentDoesNotContainsThisFS",
                                                                  conventions: client.Conventions,
                                                                  credentials: new OperationCredentials(null, new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.User.UserName, FactIfWindowsAuthenticationIsAvailable.User.Password, FactIfWindowsAuthenticationIsAvailable.User.Domain))))
            {
                await anotherClient.EnsureFileSystemExistsAsync(); // will pass because by using this api key we have access to <system> database

                ErrorResponseException errorResponse = null;

                try
                {
                    await anotherClient.UploadAsync("def.bin", new MemoryStream(1)); // should throw because a file system ShouldThrow_WindowsDocumentDoesNotContainsThisFS isn't added to ApiKeyDefinition
                }
                catch (ErrorResponseException ex)
                {
                    errorResponse = ex;
                }

                Assert.NotNull(errorResponse);
                Assert.Equal(HttpStatusCode.Forbidden, errorResponse.StatusCode);
            }
        }
Exemple #12
0
        public async Task AdminClientWorkWithWinAuthEnabled()
        {
            var client      = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(username, password, domain));
            var adminClient = client.Admin;

            await adminClient.CreateFileSystemAsync(new FileSystemDocument
            {
                Id       = "Raven/FileSystem/" + "testName",
                Settings =
                {
                    { "Raven/FileSystem/DataDir", Path.Combine("~", Path.Combine("FileSystems", "testName")) }
                }
            }, "testName");

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName", new NetworkCredential(username, password, domain)))
            {
                await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] { 1 }));
            }

            var names = await adminClient.GetNamesAsync();

            Assert.Contains("testName", names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.NotNull(stats.FirstOrDefault(x => x.Name == "testName"));

            await adminClient.DeleteFileSystemAsync("testName");

            names = await adminClient.GetNamesAsync();

            Assert.DoesNotContain("testName", names);
        }
Exemple #13
0
        protected AsyncFilesServerClient CreateFileSystemClient(RavenConnectionStringOptions options, string fileSystemName)
        {
            var fsClient = new AsyncFilesServerClient(options.Url, fileSystemName, apiKey: options.ApiKey, credentials: options.Credentials);

            ValidateThatServerIsUpAndFileSystemExists(fsClient);

            return fsClient;
        }
Exemple #14
0
        protected AsyncFilesServerClient CreateFileSystemClient(RavenConnectionStringOptions options, string fileSystemName)
        {
            var fsClient = new AsyncFilesServerClient(options.Url, fileSystemName, apiKey: options.ApiKey, credentials: options.Credentials);

            ValidateThatServerIsUpAndFileSystemExists(fsClient);

            return(fsClient);
        }
Exemple #15
0
        public async Task <SynchronizationReport> SynchronizeFileToAsync(string fileName, SynchronizationDestination destination)
        {
            ICredentials credentials = null;

            if (string.IsNullOrEmpty(destination.Username) == false)
            {
                credentials = string.IsNullOrEmpty(destination.Domain)
                                  ? new NetworkCredential(destination.Username, destination.Password)
                                  : new NetworkCredential(destination.Username, destination.Password, destination.Domain);
            }

            var destinationClient = new AsyncFilesServerClient(destination.ServerUrl, destination.FileSystem, apiKey: destination.ApiKey, credentials: credentials).Synchronization;

            RavenJObject destinationMetadata;

            try
            {
                destinationMetadata = await destinationClient.Commands.GetMetadataForAsync(fileName);
            }
            catch (Exception ex)
            {
                var exceptionMessage = "Could not get metadata details for " + fileName + " from " + destination.Url;
                Log.WarnException(exceptionMessage, ex);

                return(new SynchronizationReport(fileName, Guid.Empty, SynchronizationType.Unknown)
                {
                    Exception = new SynchronizationException(exceptionMessage, ex)
                });
            }

            RavenJObject localMetadata = GetLocalMetadata(fileName);

            NoSyncReason            reason;
            SynchronizationWorkItem work = synchronizationStrategy.DetermineWork(fileName, localMetadata, destinationMetadata, FileSystemUrl, out reason);

            if (work == null)
            {
                Log.Debug("File '{0}' was not synchronized to {1}. {2}", fileName, destination.Url, reason.GetDescription());

                return(new SynchronizationReport(fileName, Guid.Empty, SynchronizationType.Unknown)
                {
                    Exception = new SynchronizationException(reason.GetDescription())
                });
            }

            return(await PerformSynchronizationAsync(destinationClient, work));
        }
Exemple #16
0
        protected void ValidateThatServerIsUpAndFileSystemExists(AsyncFilesServerClient fsClient)
        {
            var shouldDispose = false;

            try
            {
                var fileSystemStats = fsClient.GetStatisticsAsync().Result;
            }
            catch (Exception e)
            {
                shouldDispose = true;

                var responseException = e as ErrorResponseException;
                if (responseException != null && responseException.StatusCode == HttpStatusCode.ServiceUnavailable && responseException.Message.StartsWith("Could not find a file system named:"))
                {
                    throw new InvalidOperationException(
                              string.Format(
                                  "Migration tool does not support file system creation (file system '{0}' on server '{1}' must exist before running this tool).",
                                  fsClient.FileSystem,
                                  fsClient.ServerUrl), e);
                }

                if (e.InnerException != null)
                {
                    var webException = e.InnerException as WebException;
                    if (webException != null)
                    {
                        throw new InvalidOperationException(string.Format("Migration tool encountered a connection problem: '{0}'.", webException.Message), webException);
                    }
                }

                throw new InvalidOperationException(string.Format("Migration tool encountered a connection problem: '{0}'.", e.Message), e);
            }
            finally
            {
                if (shouldDispose)
                {
                    fsClient.Dispose();
                }
            }
        }
Exemple #17
0
        public async Task CanCreateFileSystemWithDefaultValues()
        {
            var client      = (IAsyncFilesCommandsImpl)NewAsyncClient();
            var adminClient = client.Admin;

            const string newFileSystemName = "testName_CanCreateFileSystemWithDefaultValues";

            await adminClient.CreateOrUpdateFileSystemAsync(new FileSystemDocument(), newFileSystemName);

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, newFileSystemName))
            {
                await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] { 1 }));
            }

            var names = await adminClient.GetNamesAsync();

            Assert.Contains(newFileSystemName, names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.NotNull(stats.FirstOrDefault(x => x.Name == newFileSystemName));
        }
Exemple #18
0
        public async Task AdminClientWorkWithOAuthEnabled()
        {
            var client      = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, apiKey: apiKey);
            var adminClient = client.Admin;

            await adminClient.CreateFileSystemAsync(MultiDatabase.CreateFileSystemDocument("testName"));

            var names = await adminClient.GetNamesAsync();

            Assert.Equal(2, names.Length);
            Assert.Contains("AdminClientWorkWithOAuthEnabled", names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.Equal(0, stats.Length); // 0 because our fs aren't active

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName"))
            {
                var buffer = Enumerable.Range(1, 1).Select(x => (byte)(x % byte.MaxValue)).ToArray();
                await createdFsClient.UploadAsync("fooFoo", new MemoryStream(buffer));
            }

            await adminClient.DeleteFileSystemAsync("testName", true);
        }
Exemple #19
0
        protected void ValidateThatServerIsUpAndFileSystemExists(AsyncFilesServerClient fsClient)
        {
            var shouldDispose = false;

            try
            {
                var fileSystemStats = fsClient.GetStatisticsAsync().Result;
            }
            catch (Exception e)
            {
                shouldDispose = true;

                var responseException = e as ErrorResponseException;
                if (responseException != null && responseException.StatusCode == HttpStatusCode.ServiceUnavailable && responseException.Message.StartsWith("Could not find a file system named:"))
                    throw new InvalidOperationException(
                        string.Format(
                            "Migration tool does not support file system creation (file system '{0}' on server '{1}' must exist before running this tool).",
                            fsClient.FileSystemName,
                            fsClient.ServerUrl), e);

                if (e.InnerException != null)
                {
                    var webException = e.InnerException as WebException;
                    if (webException != null)
                    {
                        throw new InvalidOperationException(string.Format("Migration tool encountered a connection problem: '{0}'.", webException.Message), webException);
                    }
                }

                throw new InvalidOperationException(string.Format("Migration tool encountered a connection problem: '{0}'.", e.Message), e);
            }
            finally
            {
                if (shouldDispose)
                    fsClient.Dispose();
            }
        }
		public async Task<SynchronizationReport> SynchronizeFileToAsync(string fileName, SynchronizationDestination destination)
		{
            ICredentials credentials = null;
            if (string.IsNullOrEmpty(destination.Username) == false)
            {
                credentials = string.IsNullOrEmpty(destination.Domain)
                                  ? new NetworkCredential(destination.Username, destination.Password)
                                  : new NetworkCredential(destination.Username, destination.Password, destination.Domain);
            }

		    var destinationClient = new AsyncFilesServerClient(destination.ServerUrl, destination.FileSystem, apiKey: destination.ApiKey, credentials: credentials).Synchronization;

            RavenJObject destinationMetadata;

            try
            {
                destinationMetadata = await destinationClient.Commands.GetMetadataForAsync(fileName);
            }
            catch (Exception ex)
            {
                var exceptionMessage = "Could not get metadata details for " + fileName + " from " + destination.Url;
                Log.WarnException(exceptionMessage, ex);

                return new SynchronizationReport(fileName, Guid.Empty, SynchronizationType.Unknown)
                {
                    Exception = new SynchronizationException(exceptionMessage, ex)
                };
            }

            RavenJObject localMetadata = GetLocalMetadata(fileName);

			NoSyncReason reason;
			SynchronizationWorkItem work = synchronizationStrategy.DetermineWork(fileName, localMetadata, destinationMetadata, FileSystemUrl, out reason);

			if (work == null)
			{
				Log.Debug("File '{0}' was not synchronized to {1}. {2}", fileName, destination.Url, reason.GetDescription());

				return new SynchronizationReport(fileName, Guid.Empty, SynchronizationType.Unknown)
				{
					Exception = new SynchronizationException(reason.GetDescription())
				};
			}

            return await PerformSynchronizationAsync(destinationClient, work);
		}
Exemple #21
0
        public async Task ShouldThrowWhenWindowsDocumentDoesNotContainFileSystem()
        {
            // in this test be careful if the specified credentials belong to admin user or not

            var client = NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(username, password, domain));
            var server = GetServer();

            await client.UploadAsync("abc.bin", new RandomStream(3));

            using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "ShouldThrow_WindowsDocumentDoesnContainsThisFS", 
                credentials: new NetworkCredential(username, password, domain)))
            {
                await anotherClient.EnsureFileSystemExistsAsync(); // will pass because by using this api key we have access to <system> database

                ErrorResponseException errorResponse = null;

                try
                {
                    await anotherClient.UploadAsync("def.bin", new RandomStream(1)); // should throw because a file system ShouldThrow_ApiKeyDoesnContainsThisFS isn't added to ApiKeyDefinition
                }
                catch (InvalidOperationException ex)
                {
                    errorResponse = ex.InnerException as ErrorResponseException;
                }

                Assert.NotNull(errorResponse);
                Assert.Equal(HttpStatusCode.Forbidden, errorResponse.StatusCode);
            }
        }
Exemple #22
0
        public async Task AdminClientWorkWithWinAuthEnabled()
        {
            var client = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(username, password, domain));
	        var adminClient = client.Admin;

            await adminClient.CreateFileSystemAsync(new FileSystemDocument
            {
                Id = "Raven/FileSystem/" + "testName",
                Settings =
                 {
                     {"Raven/FileSystem/DataDir", Path.Combine("~", Path.Combine("FileSystems", "testName"))}
                 }
            }, "testName");

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName", new NetworkCredential(username, password, domain)))
	        {
		        await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] {1}));
	        }

            var names = await adminClient.GetNamesAsync();

            Assert.Contains("testName", names);

            var stats = await adminClient.GetStatisticsAsync();

			Assert.NotNull(stats.FirstOrDefault(x => x.Name == "testName"));

	        await adminClient.DeleteFileSystemAsync("testName");

			names = await adminClient.GetNamesAsync();

			Assert.DoesNotContain("testName", names);
        }
Exemple #23
0
        public async Task CanCreateAndDeleteFileSystem()
        {
            var client = (IAsyncFilesCommandsImpl)NewAsyncClient();
            var adminClient = client.Admin;

            const string newFileSystemName = "testName_CanDeleteFileSystem";

            await adminClient.CreateOrUpdateFileSystemAsync(new FileSystemDocument
            {
                Id = "Raven/FileSystem/" + newFileSystemName,
                Settings =
                 {
                     {Constants.FileSystem.DataDirectory, Path.Combine("~", Path.Combine("FileSystems", newFileSystemName))}
                 }
            }, newFileSystemName);

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, newFileSystemName))
            {
                await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] { 1 }));
            }

            var names = await adminClient.GetNamesAsync();

            Assert.Contains(newFileSystemName, names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.NotNull(stats.FirstOrDefault(x => x.Name == newFileSystemName));

            await adminClient.DeleteFileSystemAsync(newFileSystemName);

            names = await adminClient.GetNamesAsync();

            Assert.DoesNotContain(newFileSystemName, names);
        }
Exemple #24
0
        public async Task CanCreateFileSystemWithDefaultValues()
        {
            var client = (IAsyncFilesCommandsImpl)NewAsyncClient();
            var adminClient = client.Admin;

            const string newFileSystemName = "testName_CanCreateFileSystemWithDefaultValues";

            await adminClient.CreateOrUpdateFileSystemAsync(new FileSystemDocument(), newFileSystemName);

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, newFileSystemName))
            {
                await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] { 1 }));
            }

            var names = await adminClient.GetNamesAsync();

            Assert.Contains(newFileSystemName, names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.NotNull(stats.FirstOrDefault(x => x.Name == newFileSystemName));
        }
Exemple #25
0
	    public async Task Can_get_stats_for_all_active_file_systems()
	    {
	        var client = NewAsyncClient();
	        var server = GetServer();

	        using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "test"))
	        {
	            await anotherClient.EnsureFileSystemExistsAsync();

                await client.UploadAsync("test1", new RandomStream(10)); // will make it active
	            await anotherClient.UploadAsync("test1", new RandomStream(10)); // will make it active

                await client.UploadAsync("test2", new RandomStream(10));

	            var stats = await anotherClient.Admin.GetStatisticsAsync();

	            var stats1 = stats.FirstOrDefault(x => x.Name == client.FileSystemName);
                Assert.NotNull(stats1);
	            var stats2 = stats.FirstOrDefault(x => x.Name == anotherClient.FileSystemName);
	            Assert.NotNull(stats2);

                Assert.Equal(2, stats1.Metrics.Requests.Count);
                Assert.Equal(1, stats2.Metrics.Requests.Count);

                Assert.Equal(0, stats1.ActiveSyncs.Count);
                Assert.Equal(0, stats1.PendingSyncs.Count);

                Assert.Equal(0, stats2.ActiveSyncs.Count);
                Assert.Equal(0, stats2.PendingSyncs.Count);
	        }
	    }
Exemple #26
0
        private async Task <DestinationSyncResult> SynchronizeDestinationAsync(SynchronizationDestination destination,
                                                                               bool forceSyncingContinuation)
        {
            try
            {
                ICredentials credentials = null;
                if (string.IsNullOrEmpty(destination.Username) == false)
                {
                    credentials = string.IsNullOrEmpty(destination.Domain)
                                      ? new NetworkCredential(destination.Username, destination.Password)
                                      : new NetworkCredential(destination.Username, destination.Password, destination.Domain);
                }

                var destinationClient = new AsyncFilesServerClient(destination.ServerUrl, destination.FileSystem,
                                                                   apiKey: destination.ApiKey, credentials: credentials).Synchronization;

                var lastETag = await destinationClient.GetLastSynchronizationFromAsync(storage.Id);

                var activeTasks           = synchronizationQueue.Active;
                var filesNeedConfirmation = GetSyncingConfigurations(destination).Where(sync => activeTasks.All(x => x.FileName != sync.FileName)).ToList();

                var confirmations = await ConfirmPushedFiles(filesNeedConfirmation, destinationClient);

                var needSyncingAgain = new List <FileHeader>();

                foreach (var confirmation in confirmations)
                {
                    if (confirmation.Status == FileStatus.Safe)
                    {
                        Log.Debug("Destination server {0} said that file '{1}' is safe", destination, confirmation.FileName);
                        RemoveSyncingConfiguration(confirmation.FileName, destination.Url);
                    }
                    else
                    {
                        storage.Batch(accessor =>
                        {
                            var fileHeader = accessor.ReadFile(confirmation.FileName);

                            if (fileHeader != null)
                            {
                                needSyncingAgain.Add(fileHeader);

                                Log.Debug("Destination server {0} said that file '{1}' is {2}.", destination, confirmation.FileName, confirmation.Status);
                            }
                        });
                    }
                }

                await EnqueueMissingUpdatesAsync(destinationClient, lastETag, needSyncingAgain);

                var reports = await Task.WhenAll(SynchronizePendingFilesAsync(destinationClient, forceSyncingContinuation));

                var destinationSyncResult = new DestinationSyncResult
                {
                    DestinationServer     = destination.ServerUrl,
                    DestinationFileSystem = destination.FileSystem
                };

                if (reports.Length > 0)
                {
                    var successfulSynchronizationsCount = reports.Count(x => x.Exception == null);

                    var failedSynchronizationsCount = reports.Count(x => x.Exception != null);

                    if (successfulSynchronizationsCount > 0 || failedSynchronizationsCount > 0)
                    {
                        Log.Debug(
                            "Synchronization to a destination {0} has completed. {1} file(s) were synchronized successfully, {2} synchronization(s) were failed",
                            destination.Url, successfulSynchronizationsCount, failedSynchronizationsCount);
                    }

                    destinationSyncResult.Reports = reports;
                }

                return(destinationSyncResult);
            }
            catch (Exception ex)
            {
                Log.WarnException(string.Format("Failed to perform a synchronization to a destination {0}", destination), ex);

                return(new DestinationSyncResult
                {
                    DestinationServer = destination.ServerUrl,
                    DestinationFileSystem = destination.FileSystem,
                    Exception = ex
                });
            }
        }
        private async Task<DestinationSyncResult> SynchronizeDestinationAsync(SynchronizationDestination destination,
																			  bool forceSyncingContinuation)
		{
			try
			{
                ICredentials credentials = null;
                if (string.IsNullOrEmpty(destination.Username) == false)
                {
                    credentials = string.IsNullOrEmpty(destination.Domain)
                                      ? new NetworkCredential(destination.Username, destination.Password)
                                      : new NetworkCredential(destination.Username, destination.Password, destination.Domain);
                }

                var destinationClient = new AsyncFilesServerClient(destination.ServerUrl, destination.FileSystem,
                                                                  apiKey: destination.ApiKey, credentials: credentials).Synchronization;

				var lastETag = await destinationClient.GetLastSynchronizationFromAsync(storage.Id);

				var activeTasks = synchronizationQueue.Active;
				var filesNeedConfirmation = GetSyncingConfigurations(destination).Where(sync => activeTasks.All(x => x.FileName != sync.FileName)).ToList();

				var confirmations = await ConfirmPushedFiles(filesNeedConfirmation, destinationClient);

				var needSyncingAgain = new List<FileHeader>();

				foreach (var confirmation in confirmations)
				{
					if (confirmation.Status == FileStatus.Safe)
					{
						Log.Debug("Destination server {0} said that file '{1}' is safe", destination, confirmation.FileName);
						RemoveSyncingConfiguration(confirmation.FileName, destination.Url);
					}
					else
					{
						storage.Batch(accessor =>
						{
							var fileHeader = accessor.ReadFile(confirmation.FileName);

							if (fileHeader != null)
							{
								needSyncingAgain.Add(fileHeader);

								Log.Debug("Destination server {0} said that file '{1}' is {2}.", destination, confirmation.FileName, confirmation.Status);
							}
						});
					}
				}

				await EnqueueMissingUpdatesAsync(destinationClient, lastETag, needSyncingAgain);

                var reports = await Task.WhenAll(SynchronizePendingFilesAsync(destination, destinationClient, forceSyncingContinuation));

				var destinationSyncResult = new DestinationSyncResult
				{
					DestinationServer = destination.ServerUrl,
                    DestinationFileSystem = destination.FileSystem
				};

				if (reports.Length > 0)
				{
					var successfulSynchronizationsCount = reports.Count(x => x.Exception == null);

					var failedSynchronizationsCount = reports.Count(x => x.Exception != null);

					if (successfulSynchronizationsCount > 0 || failedSynchronizationsCount > 0)
					{
						Log.Debug(
							"Synchronization to a destination {0} has completed. {1} file(s) were synchronized successfully, {2} synchronization(s) were failed",
							destination.Url, successfulSynchronizationsCount, failedSynchronizationsCount);
					}

					destinationSyncResult.Reports = reports;
				}

				return destinationSyncResult;
			}
			catch (Exception ex)
			{
				Log.WarnException(string.Format("Failed to perform a synchronization to a destination {0}", destination), ex);

				return new DestinationSyncResult
				{
					DestinationServer = destination.ServerUrl,
                    DestinationFileSystem = destination.FileSystem,
					Exception = ex
				};
			}
		}
        public async Task ShouldThrowWhenUsedApiKeyDefinitionDoesNotContainFileSystem()
        {
            var client = NewAsyncClient(enableAuthentication: true, apiKey: apiKey);
            var server = GetServer();

            await client.UploadAsync("abc.bin", new RandomStream(3));

            using (var anotherClient = new AsyncFilesServerClient(GetServerUrl(false, server.SystemDatabase.ServerUrl), "ShouldThrow_ApiKeyDoesnContainsThisFS", apiKey: apiKey))
            {
                await anotherClient.EnsureFileSystemExistsAsync(); // will pass because by using this api key we have access to <system> database

                ErrorResponseException errorResponse = null;

                try
                {
                    await anotherClient.UploadAsync("def.bin", new RandomStream(1)); // should throw because a file system ShouldThrow_ApiKeyDoesnContainsThisFS isn't added to ApiKeyDefinition
                }
                catch (InvalidOperationException ex)
                {
                    errorResponse = ex.InnerException as ErrorResponseException;
                }
                
                Assert.NotNull(errorResponse);
                Assert.Equal(HttpStatusCode.Forbidden, errorResponse.StatusCode);
            }
        }
        public async Task AdminClientWorkWithOAuthEnabled()
        {
            var client = (IAsyncFilesCommandsImpl) NewAsyncClient(enableAuthentication: true, apiKey: apiKey);
	        var adminClient = client.Admin;

            await adminClient.CreateFileSystemAsync(new FileSystemDocument
            {
                Id = "Raven/FileSystem/" + "testName",
                Settings =
                 {
                     { Constants.FileSystem.DataDirectory, Path.Combine("~", Path.Combine("FileSystems", "testName"))}
                 }
            }, "testName");

	        var names = await adminClient.GetNamesAsync();

            Assert.Equal(1, names.Length); // will not return 'testName' file system name because used apiKey doesn't have access to a such file system
            Assert.Equal("AdminClientWorkWithOAuthEnabled", names[0]);

			var stats = await adminClient.GetStatisticsAsync();            
			Assert.Equal(0, stats.Length); // 0 because our fs aren't active

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName"))
			{
				await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] { 1 }));
			}

			await adminClient.DeleteFileSystemAsync("testName", true);
        }
Exemple #30
0
        public async Task AdminClientWorkWithWinAuthEnabled()
        {
            var client = (IAsyncFilesCommandsImpl)NewAsyncClient(enableAuthentication: true, credentials: new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain));
            var adminClient = client.Admin;

            await adminClient.CreateFileSystemAsync(MultiDatabase.CreateFileSystemDocument("testName"), "testName");

            using (var createdFsClient = new AsyncFilesServerClient(client.ServerUrl, "testName", conventions: client.Conventions, credentials: new OperationCredentials(null, new NetworkCredential(FactIfWindowsAuthenticationIsAvailable.Admin.UserName, FactIfWindowsAuthenticationIsAvailable.Admin.Password, FactIfWindowsAuthenticationIsAvailable.Admin.Domain))))
            {
                await createdFsClient.UploadAsync("foo", new MemoryStream(new byte[] {1}));
            }

            var names = await adminClient.GetNamesAsync();

            Assert.Contains("testName", names);

            var stats = await adminClient.GetStatisticsAsync();

            Assert.NotNull(stats.FirstOrDefault(x => x.Name == "testName"));

            await adminClient.DeleteFileSystemAsync("testName");

            names = await adminClient.GetNamesAsync();

            Assert.DoesNotContain("testName", names);
        }
Exemple #31
0
        public IAsyncFilesSession OpenAsyncSession(OpenFilesSessionOptions sessionOptions)
        {
            AssertInitialized();
            EnsureNotClosed();

            if (string.IsNullOrWhiteSpace(sessionOptions.FileSystem))
                throw new ArgumentException("Filesystem cannot be null, empty or whitespace.", "FileSystem");


            var sessionId = Guid.NewGuid();
            currentSessionId = sessionId;
            try
            {
                var client = new AsyncFilesServerClient( this.Url, sessionOptions.FileSystem, sessionOptions.Credentials, sessionOptions.ApiKey );

                var session = new AsyncFilesSession(this, client, this.Listeners, sessionId);
                AfterSessionCreated(session);
                return session;
            }
            finally
            {
                currentSessionId = null;
            }
        }