private static void startup(Utility.ProgramArgs programArgs, string args) { // Initialize diagnostic logging. if (programArgs.Valid) { LogX.Initialize(programArgs.TargetInstance.Replace(@"\", "$")); } else { LogX.Initialize(); } logX = new LogX("Idera.SQLsecure.Collector.Program"); // Log to event log that data loader is starting. AppLog.WriteAppEventInfo(SQLsecureEvent.DlInfoStartMsg, SQLsecureCat.DlStartCat, DateTime.Now.ToString(), args); logX.loggerX.Info("Collector started with parameters: ", args); }
static void Main(string[] args) { //for manual run (for developers) /*args = new string[4]; * args[0] = "-TargetInstance"; * args[1] = "SS_SQL_SECURE\\MSSQLSERVER2012"; * args[2] = "-Repository"; * args[3] = "SS_SQL_SECURE\\MSSQLSERVER2014";*/ string targetName = string.Empty; bool isOK = true; bool needToWriteFailedSnapshot = false; System.Diagnostics.Stopwatch swTotal = new System.Diagnostics.Stopwatch(); swTotal.Start(); try { // Initialize the data loader utility. string argsString = Utility.ProgramArgs.ArgsToString(args); // Parse the command line arguments. Utility.ProgramArgs programArgs = new Utility.ProgramArgs(args); if (string.IsNullOrEmpty(programArgs.EncryptedPassword)) { startup(programArgs, argsString); using (logX.m_logX.DebugCall()) { if (programArgs.Valid) { targetName = programArgs.TargetInstance; // Initialize and validate the repository. m_Repository = new Repository(programArgs.Repository, programArgs.RepositoryUser, programArgs.RepositoryPassword); if (m_Repository.IsValid) { // Check license. if (m_Repository.IsLicenseOk()) { Sql.Database.CreateApplicationActivityEventInRepository( m_Repository.ConnectionString, programArgs.TargetInstance, 0, Collector.Constants. ActivityType_Info, Collector.Constants. ActivityEvent_Start, "Starting snapshot collection for " + programArgs.TargetInstance); // Check if the target instance is registered in the Repository. if (m_Repository.IsTargetRegistered(programArgs.TargetInstance)) { // Retrieve target instance credentials from the repository. string server, sqlLogin, sqlPassword, sqlAuthTypeString, serverLogin, serverPassword, serverTypeString; int? port; ServerType serverType = ServerType.OnPremise; AuthType authType = AuthType.Null; if (m_Repository.GetTargetCredentials(programArgs.TargetInstance, out server, out port, out sqlLogin, out sqlPassword, out sqlAuthTypeString, out serverLogin, out serverPassword, out serverTypeString)) { serverType = Helper.ConvertSQLTypeStringToEnum(serverTypeString); authType = (AuthType)Enum.Parse(typeof(AuthType), sqlAuthTypeString); m_targetUserName = serverLogin; m_targetUserPassword = serverPassword; // SQLSecure 3.1 (Biresh Kumar Mishra) - Add Support for Azure VM if (serverType == ServerType.SQLServerOnAzureVM) { m_SQLServerOnAzureVM_FullName = server; if (server.IndexOf(".") != -1) { m_SQLServerOnAzureVM_DomainName = server.Substring(server.IndexOf(".") + 1); server = server.Substring(0, server.IndexOf(".")); } } if (string.IsNullOrEmpty(serverLogin)) { // Only issue warning for this case Sql.Database.CreateApplicationActivityEventInRepository(m_Repository.ConnectionString, targetName, 0, Collector.Constants.ActivityType_Warning, Collector.Constants.ActivityEvent_Start, string.Format("No credentials specified for Operating System and Active Directory, using SQLsecure Collector user {0}", WindowsIdentity.GetCurrent().Name)); } if (string.IsNullOrEmpty(sqlLogin)) { throw new Exception("No credentials specified for collecting SQL Server security."); } if (serverType == ServerType.OnPremise) { GetIdentitiesForImpersonation(sqlLogin, sqlPassword, authType, serverLogin, serverPassword); } else if (serverType == ServerType.AzureSQLDatabase && authType == AuthType.W) { //AuthenticationResult authenticationResult= AzureDatabase.GetConnectionToken(serverLogin, serverPassword); } //SQLsecure 3.1 (Tushar)--Support for Azure VM. else if (serverType == ServerType.SQLServerOnAzureVM) { GetIdentitiesForImpersonation(sqlLogin, sqlPassword, authType, serverLogin, serverPassword); } } Program.ImpersonationContext wi; // Initialize and validate the target. System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); if (serverType == ServerType.OnPremise) { wi = SetTargetSQLServerImpersonationContext(); m_Target = new Target(programArgs.TargetInstance, m_Repository); RestoreImpersonationContext(wi); } else if (serverType == ServerType.AzureSQLDatabase) { m_Target = new Target(programArgs.TargetInstance, m_Repository); } //SQLsecure 3.1 (Tushar)--Support for Azure VM. else if (serverType == ServerType.SQLServerOnAzureVM) { wi = SetTargetSQLServerImpersonationContext(); m_Target = new Target(programArgs.TargetInstance, m_Repository); RestoreImpersonationContext(wi); } sw.Stop(); logX.loggerX.Verbose("TIMING - Time to initialize and validate target = " + sw.ElapsedMilliseconds.ToString() + " msec"); if (m_Target.IsValid) { if (serverType == ServerType.OnPremise) { wi = SetTargetImpersonationContext(); // Load the permissions data. m_Target.LoadData(programArgs.AutomatedRun); RestoreImpersonationContext(wi); } else if (serverType == ServerType.AzureSQLDatabase) { //SQLsecure 3.1 (Tushar)--Passing the server name becasue we are not creating server object for azure DB. m_Target.LoadDataAzureDB(programArgs.AutomatedRun, server); } //SQLsecure 3.1 (Tushar)--Support for Azure VM. else if (serverType == ServerType.SQLServerOnAzureVM) { wi = SetTargetImpersonationContext(); m_Target.LoadDataForAzureVM(programArgs.AutomatedRun); RestoreImpersonationContext(wi); } } else { needToWriteFailedSnapshot = true; logX.loggerX.Error("ERROR - target instance is not valid"); Sql.Database.CreateApplicationActivityEventInRepository( m_Repository.ConnectionString, programArgs.TargetInstance, 0, Constants.ActivityType_Error, Constants.ActivityEvent_Error, "Target " + programArgs.TargetInstance + " could not be found"); AppLog.WriteAppEventError(SQLsecureEvent.DlErrOpenTargetConnectionFailed, SQLsecureCat.DlValidationCat, "Target " + programArgs.TargetInstance + " could not be found"); isOK = false; } } else { needToWriteFailedSnapshot = true; logX.loggerX.Error("ERROR - target instance is not registered"); Sql.Database.CreateApplicationActivityEventInRepository( m_Repository.ConnectionString, programArgs.TargetInstance, 0, Collector.Constants.ActivityType_Error, Collector.Constants.ActivityEvent_Error, "Target " + programArgs.TargetInstance + " is not registered"); AppLog.WriteAppEventError(SQLsecureEvent.DlErrTargetNotRegistered, SQLsecureCat.DlValidationCat, "Target " + programArgs.TargetInstance + " is not registered"); isOK = false; } if (needToWriteFailedSnapshot) { int snapshotID = m_Repository.CreateErrorSnapshot(programArgs.TargetInstance); if (snapshotID != 0) { Sql.Database.UpdateRepositoryRegisteredServerTable( m_Repository.ConnectionString, snapshotID, Constants.StatusError); } } } else { logX.loggerX.Error("ERROR - license check failed."); Sql.Database.CreateApplicationActivityEventInRepository( m_Repository.ConnectionString, programArgs.TargetInstance, 0, Collector.Constants. ActivityType_Error, Collector.Constants. ActivityEvent_Error, "The SQLsecure Collector was unable to aquire a valid license"); AppLog.WriteAppEventInfo(SQLsecureEvent.DlErrNoLicense, SQLsecureCat.DlValidationCat); isOK = false; } } else { logX.loggerX.Error("ERROR - SQLsecure Repository is invalid."); isOK = false; } } else // Invalid args display usage { if (logX == null) { logX = new LogX("Idera.SQLsecure.Collector.Program"); } logX.loggerX.Error("ERROR: Failed to parse the arguments"); Console.WriteLine(Constants.CopyrightMsg); Console.WriteLine(Constants.UsageMsg); isOK = false; } } } else //handle output of the encrypted password { Console.WriteLine(string.Format("Encrypted Password: {0}", programArgs.EncryptedPassword)); } } catch (Exception ex) { string msg = "Collection Error: " + ex.Message; if (m_Repository != null && m_Repository.IsValid) { Sql.Database.CreateApplicationActivityEventInRepository(m_Repository.ConnectionString, targetName, 0, Collector.Constants.ActivityType_Error, Collector.Constants.ActivityEvent_Error, msg); } AppLog.WriteAppEventError(SQLsecureEvent.DlErrOpenTargetConnectionFailed, SQLsecureCat.DlValidationCat, "Target " + targetName + " " + ex.Message); if (logX == null) { logX = new LogX("Idera.SQLsecure.Collector.Program"); } logX.loggerX.Error(msg); } finally { // Undo the impersonation if (m_targetImpersionationContext != null) { m_targetImpersionationContext.Undo(); m_targetImpersionationContext.Dispose(); } if (m_targetSQLServerImpersionationContext != null) { m_targetSQLServerImpersionationContext.Undo(); m_targetSQLServerImpersionationContext.Dispose(); } if (TargetServer != null) { TargetServer.RealUnbind(); } } swTotal.Stop(); logX.loggerX.Verbose("TIMING - Total Time for Collector = " + swTotal.ElapsedMilliseconds.ToString() + " msec"); // Exiting utility, do shutdown processing. shutdown(); Environment.ExitCode = isOK ? 0 : 1; }