Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        protected override IEnumerable <CollectedItem> collectDataForSystemItem(ItemType systemItem)
        {
            this.CreateSMFCollectorInstance();

            var smfItem = (smf_item)systemItem;

            try
            {
                var collectedSmf = this.TryToCollectSMF(smfItem.fmri.Value);
                smfItem.service_name  = OvalHelper.CreateItemEntityWithStringValue(collectedSmf.ServiceName);
                smfItem.service_state = new EntityItemSmfServiceStateType()
                {
                    Value = collectedSmf.ServiceState
                };
                smfItem.protocol = new EntityItemSmfProtocolType()
                {
                    Value = collectedSmf.Protocol
                };
                smfItem.server_executable = OvalHelper.CreateItemEntityWithStringValue(collectedSmf.ServerExecutable);
                smfItem.server_arguements = OvalHelper.CreateItemEntityWithStringValue(collectedSmf.ServerArgs);
                smfItem.exec_as_user      = OvalHelper.CreateItemEntityWithStringValue(collectedSmf.ExecAsUser);
            }
            catch (NoSMFDataException)
            {
                ExecutionLogBuilder.AddInfo("An error occurred while trying to collect smf_object");
                smfItem.status      = StatusEnumeration.error;
                smfItem.message     = MessageType.FromErrorString("The fmri format is invalid.");
                smfItem.fmri.status = StatusEnumeration.error;
            }

            return(new ItemTypeHelper().CreateCollectedItemsWithOneItem(smfItem, BuildExecutionLog()));
        }
Exemplo n.º 3
0
        /// <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();
        }
Exemplo n.º 4
0
        public override void PrepareCollectionOfObjects(IEnumerable <ObjectType> allItemsToCollect, VariablesEvaluated variables)
        {
            base.PrepareCollectionOfObjects(allItemsToCollect, variables);
            if (allItemsToCollect.Count() > 0)
            {
                var variableEvaluator = new VariableEntityEvaluator(variables);

                var allSapObjects = allItemsToCollect.OfType <sapcode_object>().ToList();
                var issues        = allSapObjects.SelectMany(x => variableEvaluator.EvaluateVariableForEntity(((EntitySimpleBaseType)(x.Items[x.ItemsElementName.ToList().IndexOf(SapCodeObjectItemsChoices.issue)])))).Distinct();
                var systemNames   = allSapObjects.SelectMany(x => variableEvaluator.EvaluateVariableForEntity(((EntitySimpleBaseType)x.Items[x.ItemsElementName.ToList().IndexOf(SapCodeObjectItemsChoices.system_name)]))).Distinct();

                var systemIds = systemNames.Select(x => Convert.ToInt32(x));
                if (systemIds.Count() > 1)
                {
                    throw new NotSupportedException("Only concurrent collections of a single system is supported!");
                }

                ExecutionLogBuilder.AddInfo(string.Format("Authenticating at code control with user '{0}'.", AuthUser));
                var authResult = connectionProvider.authenticate(AuthUser, AuthPassword);
                if (authResult.error)
                {
                    ExecutionLogBuilder.AnErrorOccurred(string.Format("Error authenticating at code control : {0}.", authResult.errorMessage));
                }
                else
                {
                    ExecutionLogBuilder.AddInfo(string.Format("Successfully authenticated.", AuthUser));
                    int nSystem   = systemIds.Single();
                    var allIssues = issues.Select(x => (long)Convert.ToInt32(x)).ToArray();
                    ExecutionLogBuilder.AddInfo(
                        string.Format("Starting scan request for system {0} and issues '{1}'.", nSystem, string.Join(",", allIssues)));
                    issueResult = connectionProvider.scanIssueListBySystem(authResult.token, nSystem, allIssues);

                    var scanCriteria = new ScanCriteriaDTO()
                    {
                        scanIdList = new[] { issueResult.scanId ?? 0 }
                    };

                    var waitTime = 0L;
                    //const int timeOut = 3600000; // 1 hour
                    //const int timeOut = 10800000; // 3 hs
                    const int timeOut = 18000000;
                    while (((issueResult.status == "AWAITING") || (issueResult.status == "PROCESSING")) &&
                           (waitTime <= timeOut)
                           )
                    {
                        Thread.Sleep(40000);
                        issueResult = connectionProvider.findScan(authResult.token, scanCriteria).FirstOrDefault();
                        // Wait time is desconsidering remote call duration,
                        // should be done with a stop watch
                        waitTime += 40000;
                    }
                }
            }
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
        public void Should_be_possible_to_inform_the_finalization_of_an_especific_collect()
        {
            ExecutionLogBuilder executionLog = new ExecutionLogBuilder();

            executionLog.StartCollectOf("Registry");
            executionLog.TryConnectToHost("176.16.3.166");
            executionLog.CollectingInformationFrom("oval:id:7589");
            executionLog.EndCollectOf("Registry");
            IEnumerable <ProbeLogItem> executionsLog = executionLog.BuildExecutionLogs();

            Assert.AreEqual(4, executionsLog.Count());
        }
Exemplo n.º 7
0
        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");
        }
Exemplo n.º 8
0
        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());
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
0
        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());
        }
Exemplo n.º 11
0
        /// <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));
        }
Exemplo n.º 12
0
        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");
        }
Exemplo n.º 13
0
        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);
        }
Exemplo n.º 14
0
        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");
        }
Exemplo n.º 15
0
        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");
        }
Exemplo n.º 16
0
        /// <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();
        }
Exemplo n.º 17
0
 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);
             }
         }
     }
 }
Exemplo n.º 18
0
        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);
        }
Exemplo n.º 19
0
        /// <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();
        }
Exemplo n.º 20
0
 /// <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();
 }
Exemplo n.º 21
0
 /// <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();
 }
Exemplo n.º 22
0
        /// <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);
        }
Exemplo n.º 23
0
        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();
        }