public void MockDeploy_UpdatesScriptsInstalledVersion() { //Arrange var creds = new GenericCredentialInfo(); var servers = new[] { new ResourceServer() { Name = "localhost" } }; var deploymentResults = new MigrationResultSet() { Success = true, Messages = new List <LogMessage>() { new LogMessage(LogSeverity.Info, "abc") } }; _serverRepositoryMock.Setup(x => x.ReadAllActive()) .Returns(servers.Select(s => new Server() { ServerName = s.Name, ServerType = ServerType.Database }).ToArray()); _databaseDeployer.Setup(ds => ds.DeployResource(It.IsAny <Server>(), creds)).Returns(deploymentResults); _refreshServerService.Setup(rss => rss.GetServerList()).Returns(servers); _refreshServerService.Setup(rss => rss.UpdateServerList(servers)); this.administrationInstallationRepositoryMock.Setup(x => x.UpdateAdminScriptsRun()); //Act var results = this.administrationInstallationService.InstallScripts(creds); //Assert Assert.IsTrue(results.Success); this.administrationInstallationRepositoryMock.Verify(x => x.UpdateAdminScriptsRun(), Times.Exactly(1)); }
public IDatabaseMigrator GetResourceMigrator(string serverName, GenericCredentialInfo credentialInfo) { var settings = GetDeploymentSettings(serverName, Names.Database.PdbResource); settings.MigrationResource = DatabaseDeploymentConfiguration.MigrateResource; settings.CreateScriptName = DeploymentDirectoryStructureConstants.CreateResouceCustomScript; settings.CredentialInfo = credentialInfo; return(new DatabaseMigrator(settings, connectionFactory)); }
public void InstallPrimaryServerAdminScripts(GenericCredentialInfo credentialInfo) { using (var conn = this.connectionFactory.GetEddsConnection(credentialInfo)) { //Deploy SQL Server Agent for collecting Relativity Agent history var statements = SplitScript(Resources.Create_QoS_CollectAgentUptime); foreach (var statement in statements) { conn.Execute(statement); } } }
public void AdminScriptInstall(string username, string password) { var creds = new GenericCredentialInfo { UserName = username, Password = password }; var results = this.administrationInstallationService.InstallScripts(creds); if (!results.Success) { throw new Exception($"Failed to install scripts, details: {string.Join("\r\n| ", results.Messages.Select(m => m.Text))}"); } }
/// <summary> /// Verifies that the credentials are valid /// </summary> /// <param name="credentialInfo">The credential information</param> /// <returns>results</returns> public bool CredentialsAreValid(GenericCredentialInfo credentialInfo) { //Get a list of active SQL servers and check each one var servers = this.serverRepository.ReadAllActive() .Where(s => s.ServerType == ServerType.Database); foreach (var server in servers) { this.administrationInstallationRepository.HasDbccPermissions(server.ServerName, credentialInfo); } //This will be true unless we failed the permissions check for a server above or a DB error occurred return(true); }
public ScriptInstallationResults InstallScripts(GenericCredentialInfo credentialInfo) { var results = new ScriptInstallationResults(); try { //Context setup with admin credentials results.AppendMessage("Setting database context..."); this.administrationInstallationRepository.InstallPrimaryServerAdminScripts(credentialInfo); //Do a server refresh prior to deployment var currentServers = refreshServerService.GetServerList(); if (currentServers != null && currentServers.Any()) { refreshServerService.UpdateServerList(currentServers); } //Get an array of all SQL servers registered in Relativity results.AppendMessage("Retrieving server list..."); var servers = this.serverRepository.ReadAllActive() .Where(s => s.ServerType == ServerType.Database); foreach (var server in servers) { //Install any per-server scripts that need admin privileges results.AppendMessage($"Installing scripts on {server.ServerName}..."); var deploymentResult = databaseDeployer.DeployResource(server, credentialInfo); HandleDeploymentResponse(deploymentResult, results); results.AppendMessage($"Scripts installed on {server.ServerName}."); } results.AppendMessage("Finished installing per-server scripts."); //Update the configuration to indicate that administrative scripts have been installed results.AppendMessage("Updating script installation history..."); this.administrationInstallationRepository.UpdateAdminScriptsRun(); results.AppendMessage("Installation complete."); results.Success = true; } catch (Exception e) { results.AppendMessage($"Installation failed. {e}"); results.Success = false; } return(results); }
public void CredentialsAreValid_RejectsInvalidCredentials() { //Arrange var creds = new GenericCredentialInfo(); var serverName = "localhost"; this.administrationInstallationRepositoryMock.Setup(x => x.HasDbccPermissions(serverName, creds)).Throws(new Exception("Bad Credentials")); _serverRepositoryMock.Setup(x => x.ReadAllActive()) .Returns(new[] { new Server { ServerName = serverName, ServerType = ServerType.Database } }); //Act + Assert Assert.Throws <Exception>(() => this.administrationInstallationService.CredentialsAreValid(creds)); }
public bool HasDbccPermissions(string targetServer, GenericCredentialInfo credentialInfo) { try { using (var conn = this.connectionFactory.GetTargetConnection(Names.Database.Msdb, targetServer, credentialInfo)) { conn.Execute("DBCC DBINFO"); } } catch (Exception ex) { throw new Exception($"Failed to get permissions for account [{credentialInfo.UserName ?? "Integrated"}] against server [{targetServer}]", ex); } return(true); }
public void CredentialsAreValid_AcceptsValidCredentials() { //Arrange var creds = new GenericCredentialInfo(); var serverName = "localhost"; this.administrationInstallationRepositoryMock.Setup(x => x.HasDbccPermissions(serverName, creds)).Returns(true); _serverRepositoryMock.Setup(x => x.ReadAllActive()) .Returns(new[] { new Server { ServerName = serverName, ServerType = ServerType.Database } }); //Act var results = this.administrationInstallationService.CredentialsAreValid(creds); //Assert Assert.IsTrue(results); }
public void DatabaseDeployer_DeployResource() { // Arrange var creds = new GenericCredentialInfo(); var server = new Server { ServerId = 123, ServerName = "abc" }; this.databaseMigratorFactory.Setup(f => f.GetResourceMigrator(server.ServerName, creds)) .Returns(this.databaseMigrator.Object); // Act var results = databaseDeployer.DeployResource(server, creds); // Assert Assert.That(results.Success, Is.True); }
public void ValidateCredentials_ChecksEachServer() { //Arrange var creds = new GenericCredentialInfo(); var serverNames = new[] { "localhost", "localhost2", "localhost3" }; var servers = serverNames.Select(sn => new Server { ServerName = sn, ServerType = ServerType.Database }).ToList(); this.administrationInstallationRepositoryMock.Setup(x => x.HasDbccPermissions(It.IsIn(serverNames), creds)).Returns(true); _serverRepositoryMock.Setup(x => x.ReadAllActive()) .Returns(servers); //Act this.administrationInstallationService.CredentialsAreValid(creds); //Assert this.administrationInstallationRepositoryMock.Verify(x => x.HasDbccPermissions(It.IsIn(serverNames), creds), Times.Exactly(3)); }
public void MockDeploy_PerServerScriptFailure_IndicatesDeploymentFailure() { //Arrange var creds = new GenericCredentialInfo(); _serverRepositoryMock.Setup(x => x.ReadAllActive()) .Returns(new[] { new Server { ServerName = "localhost", ServerType = ServerType.Database }, new Server { ServerName = "secret-base", ServerType = ServerType.Database } }); //Act var results = this.administrationInstallationService.InstallScripts(creds); //Assert Assert.IsFalse(results.Success); }
public virtual IDbConnection GetEddsConnection(GenericCredentialInfo credentialInfo = null) => this.GetDatabaseConnection(Names.Database.Edds, credentialInfo: credentialInfo);
protected override SqlConnectionStringBuilder GetConnectionString(string server = null, GenericCredentialInfo credentialInfo = null) { var builder = this.configService.GetConnectionStringBuilder("relativity"); if (builder == null) { throw new Exception("There is no connection string or connection information configured."); } if (string.IsNullOrEmpty(server) == false) { builder.DataSource = server; } builder.ApplicationName = $"{Names.Application.PerformanceDashboard} Configured"; return(builder .ModifyCreditentals(credentialInfo) .AddDefaultTimeout()); }
public virtual IDbConnection GetMasterConnection(string server = null, GenericCredentialInfo credentialInfo = null) => this.GetDatabaseConnection(Names.Database.Master, server, credentialInfo);
/// <summary> /// Use only when necessary. /// </summary> /// <param name="targetDatabase"></param> /// <param name="targetServer"></param> /// <param name="credentialInfo"></param> /// <returns></returns> public virtual IDbConnection GetTargetConnection(string targetDatabase, string targetServer, GenericCredentialInfo credentialInfo = null) => this.GetDatabaseConnection(targetDatabase, targetServer, credentialInfo).Open(targetDatabase);
public virtual string GetTargetConnectionString(string targetDatabase, string targetServer, GenericCredentialInfo credentialInfo = null) { using (var conn = this.GetDatabaseConnection(targetDatabase, targetServer, credentialInfo)) { return(conn.ConnectionString); } }
protected override IDbConnection GetServerConnection(string server = null, GenericCredentialInfo credentialInfo = null) => this.GetDatabaseConnection(null, server, credentialInfo);
protected override SqlConnectionStringBuilder GetConnectionString(string server = null, GenericCredentialInfo credentialInfo = null) { // Build up our string from the default context var builder = new SqlConnectionStringBuilder(GenericConnectionFactory.GetDefaultConnectionString()); if (string.IsNullOrEmpty(server) == false) { builder.DataSource = server; } else { var context = this.helper.GetDBContext(-1); builder.DataSource = context.ServerName; context.ReleaseConnection(); } builder.ApplicationName = Names.Application.PerformanceDashboard; return(builder .ModifyCreditentals(credentialInfo) .AddDefaultTimeout()); }
public IDbConnection GetTargetConnection(string targetDatabase, string targetServer, GenericCredentialInfo credentialInfo = null) => GetAttachDbFileConnectionStringBuilder(Names.Database.EddsPerformance) .ToString() .ToDbConnection();
public IDbConnection GetMasterConnection(string server = null, GenericCredentialInfo credentialInfo = null) => GetConnectionStringBuilder() .ToString() .ToDbConnection();
public IDbConnection GetEddsConnection(GenericCredentialInfo credentialInfo = null) { throw new NotImplementedException(); }
protected abstract IDbConnection GetDatabaseConnection(string initialCatalog, string server = null, GenericCredentialInfo credentialInfo = null);
private CachedConnectionString CreateCachedConnectionString(string database = null, string server = null, GenericCredentialInfo credentialInfo = null) { var connectionStringBuilder = this.GetConnectionString(server, credentialInfo); if (string.IsNullOrEmpty(database) && connectionStringBuilder.ContainsKey("Initial Catalog")) { connectionStringBuilder.Remove("Initial Catalog"); } if (!string.IsNullOrEmpty(database)) { connectionStringBuilder.InitialCatalog = database; } return(new CachedConnectionString { Database = database, Server = server, UserName = credentialInfo?.UserName, Password = credentialInfo?.Password, UseWindowsAuthentication = credentialInfo?.UseWindowsAuthentication, ConnectionString = connectionStringBuilder.ToString(), ExpiresOn = DateTime.UtcNow.AddSeconds(expiresOnSeconds) }); }
protected override IDbConnection GetDatabaseConnection(string initialCatalog, string server = null, GenericCredentialInfo credentialInfo = null) { lock (cachedConnectionStrings) { // Check the cache for a compatible connection var cached = cachedConnectionStrings.FirstOrDefault( c => c.Database == initialCatalog && c.Server == server && c.UserName == credentialInfo?.UserName && c.Password == credentialInfo?.Password && c.UseWindowsAuthentication == credentialInfo?.UseWindowsAuthentication); // If the result is expired then remove it if (cached != null && cached.ExpiresOn < DateTime.UtcNow) { cachedConnectionStrings.Remove(cached); } // if the result doesn't exist or is expired if (cached == null || cached.ExpiresOn < DateTime.UtcNow) { cached = CreateCachedConnectionString(initialCatalog, server, credentialInfo); cachedConnectionStrings.Add(cached); } // return the connection return(cached.ConnectionString.ToDbConnection()); } }
public string GetTargetConnectionString(string targetDatabase, string targetServer, GenericCredentialInfo credentialInfo = null) => GetConnectionStringBuilder().ToString();
internal static DeploymentSettings GetDeploymentSettings(string server, string databaseName, GenericCredentialInfo credentialInfo) { var settings = GetDeploymentSettings(server, databaseName); settings.CredentialInfo = credentialInfo; return(settings); }
protected abstract IDbConnection GetServerConnection(string server = null, GenericCredentialInfo credentialInfo = null);
public static SqlConnectionStringBuilder ModifyCreditentals(this SqlConnectionStringBuilder connBuilder, GenericCredentialInfo credentialInfo) { if (credentialInfo != null) { if (credentialInfo.UseWindowsAuthentication) { connBuilder.Remove("User ID"); connBuilder.Remove("Password"); connBuilder.IntegratedSecurity = true; } else { connBuilder.UserID = credentialInfo.UserName; connBuilder.Password = credentialInfo.Password; } } return(connBuilder); }
protected abstract SqlConnectionStringBuilder GetConnectionString(string server = null, GenericCredentialInfo credentialInfo = null);