/// <summary> /// The base class for all probes. /// The Execute method describes the common collection logic. /// The specific probes should extend this class and override the abstracts methods for add specific details for the collection. /// The basic logic for all probes is: /// - Connect to host. /// - Verifies if the objectType has a setElement and then process the set. /// or /// - Get items to collect - This method is responsible for evaluates all the variables and operations from the object types. /// - Collect items through the SystemDataSouce implementation. A SystemDataSource is the object responsible by collect /// information in the specific technology. For instance, for the registry collect, use a RegistrySystemDataSource. /// - Build the Probe Result. /// </summary> protected ProbeBase() { if (this.ConnectionManager == null) this.ConnectionManager = new ConnectionManager(); this.ExecutionLogBuilder = new ExecutionLogBuilder(); }
public void Shoud_be_possible_to_build_an_execution_log_in_steps() { ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); executionLog.StartCollectOf("Registry"); executionLog.TryConnectToHost("176.16.3.166"); executionLog.CollectingInformationFrom("oval:id:7589"); executionLog.EndCollect(); IEnumerable<ProbeLogItem> executionsLog = executionLog.BuildExecutionLogs(); Assert.AreEqual(4, executionsLog.Count(), "the quantity of execution logs is not expected"); }
public void Shoud_be_possible_to_inform_the_systemInformation_collect() { ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); executionLog.StartCollectOf("Registry"); executionLog.TryConnectToHost("176.16.3.166"); executionLog.CollectingInformationFrom("oval:id:7589"); executionLog.CollectSystemInformation(); executionLog.EndCollectOf("Registry"); IEnumerable<ProbeLogItem> executionsLog = executionLog.BuildExecutionLogs(); Assert.AreEqual(5, executionsLog.Count()); }
public void Should_be_possible_to_build_an_execution_log_in_steps_with_custom_errors_logs() { ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); executionLog.StartCollectOf("Registry"); executionLog.TryConnectToHost("176.16.3.166"); executionLog.AnErrorOccurred("Erro trying connect to host 176.16.3.166"); executionLog.CollectingInformationFrom("oval:id:7589"); executionLog.EndCollect(); IEnumerable<ProbeLogItem> executionsLog = executionLog.BuildExecutionLogs(); Assert.AreEqual(5, executionsLog.Count()); Assert.AreEqual(TypeItemLog.Error, executionsLog.ElementAt(2).Type); }
private void UpdateSystemInformationOfTarget(IDocumentSession session, CollectRequest collectRequest, FamilyEnumeration plataform, ExecutionLogBuilder executionLog) { if (!collectRequest.Target.IsSystemInformationDefined()) { executionLog.CollectSystemInformation(); ISystemInformationService systemInformationService = ProbeManager.GetSystemInformationService(plataform); if (systemInformationService != null) { try { this.GetSystemInformationFromTarget(collectRequest, systemInformationService); session.SaveChanges(); } catch (RecoverableProbeException ex) { CollectExecution collectExecution = this.CreateCollectExecution(session, collectRequest); collectExecution.SetDateStartCollect(); this.ConfigureTheCollectRequestWithAnErrorProbeExecute(collectRequest, "SystemInformation", collectExecution, ex, executionLog); session.SaveChanges(); throw new SystemInformationException(ex.Message, ex); } catch (Exception ex) { CreateCollectionExcutionWithError(session, collectRequest, "SystemInformation", ex, executionLog); session.SaveChanges(); throw new SystemInformationException(ex.Message, ex); } } } }
private void StartCollect(IDocumentSession session, CollectRequest collectRequest, CollectExecution collectExecution, FamilyEnumeration plataform, ExecutionLogBuilder executionLog) { int attemptsOfEvaluateVariables = 1; bool anErrorOccured = false; bool allObjectWasCollect = false; IEnumerable<ObjectType> objectTypes = collectRequest.GetObjectTypesWasNotCollected(session); session.SaveChanges(); while (!collectTimeOut.IsExceededTheMaxAttemptsOfEvaluateVariables(attemptsOfEvaluateVariables) && !allObjectWasCollect && !anErrorOccured && !Interrupted) { IEnumerable<StateType> states = collectRequest.GetStates(session); IEnumerable<SelectedProbe> selectedProbes = ProbeManager.GetProbesFor(objectTypes, plataform); VariablesEvaluated variables = variableEvaluatorService.Evaluate(collectRequest, session); session.SaveChanges(); try { this.UpdateSystemInformationOfTarget(session, collectRequest, plataform, executionLog); this.ExecuteCollectWithProbes(session, selectedProbes, collectRequest, variables, states, collectExecution, executionLog); session.SaveChanges(); } catch (SystemInformationException ex) { string logMessage; if (ex.InnerException == null) { logMessage = String.Format(EXECUTION_ERROR_MESSAGE, ex.Message, ex.StackTrace); } else { logMessage = String.Format(EXECUTION_ERROR_MESSAGE_WITH_INNER_EXCEPTION, ex.Message, ex.InnerException.StackTrace, ex.StackTrace); } Logger.Error(logMessage); collectRequest.SetResultError(); collectRequest.Close(); anErrorOccured = true; session.SaveChanges(); break; } catch { // only ends the loop. The error already was treated anErrorOccured = true; } objectTypes = collectRequest.GetObjectTypesWasNotCollected(session); allObjectWasCollect = objectTypes.Count() == 0; attemptsOfEvaluateVariables++; } if (Interrupted) { collectRequest.Status = CollectRequestStatus.Canceled; } else { if ((!allObjectWasCollect) && (!anErrorOccured)) { CloseExecutionIfIncomplete(session, collectRequest, collectExecution, plataform, objectTypes); } } session.SaveChanges(); }
private void SetStartInstrumentationLog(ExecutionLogBuilder executionLog, DateTime startTime, string probeName) { String INSTRUMENTATION_LOG_MSG_FORMAT = "The collect of [{0}] probe was start at {1}"; var intrumentationLogMsg = string.Format(INSTRUMENTATION_LOG_MSG_FORMAT, probeName, startTime.ToLocalTime()); executionLog.AddInfo(intrumentationLogMsg); }
private void SetEndInstrumentationLog(ExecutionLogBuilder executionLog, DateTime initialTimeStamp, string probeName) { String INSTRUMENTATION_LOG_MSG_FORMAT = "The collect of [{0}] probe was finished at {1} (in {2} secs)"; var endTimeStamp = DateTime.Now; var totalTimeInSeconds = Convert.ToInt32(endTimeStamp.Subtract(initialTimeStamp).TotalSeconds); var executionLogMsg = string.Format(INSTRUMENTATION_LOG_MSG_FORMAT, probeName, endTimeStamp, totalTimeInSeconds); executionLog.AddInfo(executionLogMsg); }
public void Should_be_possible_to_clear_the_execution_log_after_of_the_build_executionLogs() { ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); executionLog.StartCollectOf("Registry"); executionLog.TryConnectToHost("176.16.3.166"); executionLog.CollectingInformationFrom("oval:id:7589"); executionLog.EndCollect(); IEnumerable<ProbeLogItem> executionsLog = executionLog.BuildExecutionLogs(); Assert.AreEqual(4, executionsLog.Count()); executionLog.StartCollectOf("Registry"); executionsLog = executionLog.BuildExecutionLogs(); Assert.AreEqual(1, executionsLog.Count()); }
/// <summary> /// Ends the request collect because this error is unrecoverable. /// This request was not execute because exists some error that is not possible perform the collect. /// Ex.: Invalid credentials, bad format of oval xmls, etc. /// </summary> /// <param name="collectRequest">The request collect.</param> /// <param name="probe">The probe.</param> /// <param name="collectExecution">the Collect execution object.</param> /// <param name="error">the exception that represents an error.</param> private void EndsACollectRequestBecauseThisErrorIsUnrecoverable(CollectRequest collectRequest, string probeCapability, CollectExecution collectExecution, Exception error, ExecutionLogBuilder executionLog) { ConfigureTheCollectRequestWithAnErrorProbeExecute(collectRequest, probeCapability, collectExecution, error, executionLog); collectRequest.Close(); }
private void CreateCollectionExcutionWithError(IDocumentSession session, CollectRequest collectRequest, string capability, Exception ex, ExecutionLogBuilder executionLog) { CollectExecution collectExecution = this.CreateCollectExecution(session, collectRequest); collectExecution.SetDateStartCollect(); EndsACollectRequestBecauseThisErrorIsUnrecoverable(collectRequest, capability, collectExecution, ex, executionLog); }
/// <summary> /// Configures the request collect with one probeExecute with the error status. /// </summary> /// <param name="collectRequest">The request collect.</param> /// <param name="probe">The probe.</param> /// <param name="collectExecution">The collect execution.</param> /// <param name="error">The error.</param> private void ConfigureTheCollectRequestWithAnErrorProbeExecute(CollectRequest collectRequest, string probeCapability, CollectExecution collectExecution, Exception error, ExecutionLogBuilder executionLog) { executionLog.AnErrorOccurred(error.Message); executionLog.EndCollect(); ProbeExecution executionWithError = collectFactory.CreateAProbeExecutionWithError(probeCapability, executionLog.BuildExecutionLogs()); collectExecution.ProbeExecutions.Add(executionWithError); }
/// <summary> /// This method is responsible to start of collect process. /// </summary> /// <param name="objectTypes">The object types.</param> public void ExecuteCollect(IDocumentSession session, CollectRequest collectRequest, FamilyEnumeration plataform) { ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); this.SetStatusToExecuting(collectRequest); CollectExecution collectExecution = this.CreateCollectExecution(session, collectRequest); try { collectExecution.SetDateStartCollect(); this.StartCollect(session, collectRequest, collectExecution, plataform, executionLog); this.EndCollect(session, collectRequest, collectExecution); } catch (Exception ex) { var logMessage = String.Format(EXECUTION_ERROR_MESSAGE, ex.Message, ex.StackTrace); Logger.Error(logMessage); this.EndsACollectRequestBecauseThisErrorIsUnrecoverable(collectRequest, "Collect Manager", collectExecution, ex, executionLog); } session.SaveChanges(); }
public void Should_be_possible_to_build_a_probeExecution_with_error_status_and_execution_logs_if_not_collect_was_executed() { CollectFactory collectFactory = new CollectFactory(provider.GetSession()); ProbeResult probeResult = probeResultFactory.CreateProbeResultForRegostryCollectWithError(); ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); executionLog.StartCollectOf("registry"); executionLog.TryConnectToHost("176.16.3.22"); executionLog.AnErrorOccurred("Error connecting to host"); executionLog.EndCollect(); ProbeExecution probeExecution = collectFactory.CreateAProbeExecutionWithError("registry", executionLog.BuildExecutionLogs()); Assert.IsNotNull(probeExecution); Assert.AreEqual("registry", probeExecution.Capability); Assert.IsTrue(probeExecution.ExecutionLogs.Count == 4, "the probe execution not have executionLogs expecteds"); Assert.IsTrue(probeExecution.HasErrors(), "the probe execution not have a status expected"); }
public void should_be_possible_to_buid_an_execution_log_in_steps_informing_what_element_is_collected() { string element = "Key: HKEY_LOCAL_MACHINE\\software\\microsoft\\windows\\currentVersion\\BuildType"; ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); executionLog.StartCollectOf("Registry"); executionLog.TryConnectToHost("176.16.3.166"); executionLog.ConnectedWithSuccess(); executionLog.CollectingInformationFrom("oval:id:7858"); executionLog.CollectingDataFrom(element); executionLog.EndCollect(); IEnumerable<ProbeLogItem> executionsLog = executionLog.BuildExecutionLogs(); Assert.AreEqual(6, executionsLog.Count()); }
public void Should_be_possible_to_build_an_execution_log_in_steps_with_custom_warning_logs() { ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); executionLog.StartCollectOf("Registry"); executionLog.TryConnectToHost("176.16.3.166"); executionLog.ConnectedWithSuccess(); executionLog.CollectingInformationFrom("oval:id:7589"); executionLog.CollectingDataFrom("Key: HKEY_LOCAL_MACHINE\\software\\microsoft\\windows\\currentVersion\\BuildType"); executionLog.Warning("The key of registry item is not exists"); executionLog.EndCollect(); IEnumerable<ProbeLogItem> executionsLog = executionLog.BuildExecutionLogs(); Assert.AreEqual(7, executionsLog.Count()); Assert.AreEqual(TypeItemLog.Warning, executionsLog.ElementAt(5).Type, "the type of log is not expected"); }
/// <summary> /// Executes the collect for the one probe. /// </summary> /// <param name="collectRequest">The request collect.</param> /// <param name="probe">The probe.</param> /// <param name="collectExecution">the object that represents the execution of a collect</param> /// <returns></returns> private ProbeExecution ExecuteCollect(IDocumentSession session, SelectedProbe probe, CollectRequest collectRequest, VariablesEvaluated variables, IEnumerable<StateType> states, ExecutionLogBuilder executionLog) { ProbeResult probeResult = null; executionLog.StartCollectOf(probe.Capability.OvalObject); var initialTimeStamp = DateTime.Now; this.SetStartInstrumentationLog(executionLog, initialTimeStamp, probe.Capability.OvalObject); probeResult = probe.Execute(connectionContext, Target, variables, collectRequest.GetSystemCharacteristics(session), states); this.SetEndInstrumentationLog(executionLog, initialTimeStamp, probe.Capability.OvalObject); this.MergeExecutionLogs(executionLog, probe, probeResult); return this.CreateTheProbeExecution(probeResult, probe); }
/// <summary> /// Execute the collect for the probes selected /// </summary> /// <param name="selectedProbes">The selected probes.</param> /// <param name="collectRequest">The request collect.</param> private void ExecuteCollectWithProbes(IDocumentSession session, IEnumerable<SelectedProbe> selectedProbes, CollectRequest collectRequest, VariablesEvaluated variables, IEnumerable<StateType> states, CollectExecution collectExecution, ExecutionLogBuilder executionLog) { foreach (SelectedProbe probe in selectedProbes) { if (Interrupted) { break; } try { ProbeExecution probeExecution = this.ExecuteCollect(session, probe, collectRequest, variables, states, executionLog); collectExecution.ProbeExecutions.Add(probeExecution); } catch (RecoverableProbeException ex) { this.ConfigureTheCollectRequestWithAnErrorProbeExecute(collectRequest, probe.Capability.OvalObject, collectExecution, ex, executionLog); session.SaveChanges(); throw ex; } catch (Exception ex) { EndsACollectRequestBecauseThisErrorIsUnrecoverable(collectRequest, probe.Capability.OvalObject, collectExecution, ex, executionLog); session.SaveChanges(); throw ex; } } session.SaveChanges(); }
/// <summary> /// this method makes the combination between probeLogs and contextLog, provided by ExecutionManager. /// This process normally occours in the end of collect of a probe. /// Because this, in this process is included the End entry in the log. /// </summary> /// <param name="probe">The probe.</param> /// <param name="probeResult">The probe result.</param> private void MergeExecutionLogs(ExecutionLogBuilder executionLog, SelectedProbe probe, ProbeResult probeResult) { executionLog.AddDetailInformation(probeResult.ExecutionLog); executionLog.EndCollectOf(probe.Capability.OvalObject); probeResult.ExecutionLog = executionLog.BuildExecutionLogs(); }
public void Should_be_possible_add_a_detail_information_in_the_log() { ExecutionLogBuilder executionLogDetailBuilder = new ExecutionLogBuilder(); executionLogDetailBuilder.CollectingDataFrom("Key: HKEY_LOCAL_MACHINE\\software\\microsoft\\windows\\currentVersion\\BuildType"); executionLogDetailBuilder.CollectingDataFrom("Key: HKEY_LOCAL_MACHINE\\software\\microsoft\\windows\\currentVersion\\PathName"); IEnumerable<ProbeLogItem> executionLogDetail = executionLogDetailBuilder.BuildExecutionLogs(); ExecutionLogBuilder executionLog = new ExecutionLogBuilder(); executionLog.StartCollectOf("Registry"); executionLog.TryConnectToHost("176.16.3.166"); executionLog.AddDetailInformation(executionLogDetail); executionLog.CollectingInformationFrom("oval:id:7589"); executionLog.EndCollect(); IEnumerable<ProbeLogItem> executionLogComplete = executionLog.BuildExecutionLogs(); Assert.AreEqual(6, executionLogComplete.Count(), "the quantity of logs entries is not expected"); Assert.AreEqual(executionLogComplete.ElementAt(2).Message, executionLogDetail.ElementAt(0).Message, "the detail log is no found in the correct position in the complete log"); Assert.AreEqual(executionLogComplete.ElementAt(3).Message, executionLogDetail.ElementAt(1).Message, "the detail log is no found in the correct position in the complete log"); }
public ProbeResult Execute( IList<IConnectionProvider> connectionContext, TargetInfo target, CollectInfo collectInfo) { this.ExecutionLogBuilder = new ExecutionLogBuilder(); this.ExecutionLogBuilder.TryConnectToHost(target.GetAddress()); this.OpenConnectionProvider(connectionContext, target); ExecuteAfterOpenConnectionProvider(); this.ExecutionLogBuilder.ConnectedToHostWithUserName(target.credentials.GetFullyQualifiedUsername()); this.ConfigureObjectCollector(); ProbeResultBuilder probeResultBuilder = this.CollectInformation(collectInfo); probeResultBuilder.AddExecutionLogs(this.ExecutionLogBuilder.BuildExecutionLogs()); return probeResultBuilder.ProbeResult; }