Exemple #1
0
        private async Task<(bool, object, object, IScanModel)> GetScanIds(ScanInstanceModel model)
        {
            bool allowSave = true;
            object nodeId = await this.Db.Nodes.GetIdBy(model.NodeAddress);//NodeAddress is indexed and unique.
            if (nodeId == null)
                throw new InvalidOperationException($"There is no node item for '{model.NodeAddress}' in the database.");


            object scanResourceId = await this.Db.ScanResources.GetScanResourceIdBy(model.ResourceName);//ScanResource.Name is indexed and unique
            if (scanResourceId == null)
                throw new InvalidOperationException($"There is no ScanResource item which's name is '{model.ResourceName}' in the database.");


            IScanModel scanModel = await this.Db.Scans.GetBy(model.Asset, scanResourceId);//Scan.Asset and Scan.ScanResourceId are indexed and unique
            if (scanModel == null)
                throw new InvalidOperationException($"There is no Scan item which's name is '{model.ResourceName}' in the database.");

            if (String.IsNullOrEmpty(model.FailedReason) && String.IsNullOrEmpty(model.Result))//Hem hata olmamış hem de null değer gelmiş.
            {
                allowSave = scanModel.SaveNullResult;
            }

            if (allowSave)
            {
                allowSave = scanModel.LastAssignedNodeId.Equals(nodeId);//offline gibi durumlarda atanan node' den gelmediyse kayıt etme.
                if (!allowSave)
                {
                   _=Utility.CreateLogger(nameof(ScanWorks), nameof(GetScanIds)).Code(786).Warning($"Last-Scan-Ids are not match. Orginal is {scanModel.LastAssignedNodeId} but incoming is {nodeId}").SaveAsync();
                }
            }

            return (allowSave, nodeId, scanResourceId, scanModel);
        }
Exemple #2
0
 private async Task<int> CheckMaxInstance(IScanModel scan)
 {
     if (scan.MaxInstance > 0)
     {
         int affected = await this.Db.ScanInstances.TrimToSize(scan.ScanId, scan.MaxInstance);
         if (affected > 0)
             ConsoleObserver.Instance.Notify(nameof(ScanWorks) + "_" + nameof(CheckMaxInstance), $"Max Instance exceed, total {affected} unnecessary records has been removed.", scan);
     }
     return 0;
 }
Exemple #3
0
        public async Task <int> Edit(IScanModel model)
        {
            if (null == model)
            {
                return(0);
            }

            await this.Db.Scans.ReplaceOneAsync((Scan)model);

            return(1);
        }
Exemple #4
0
        public async Task <IScanModel> Save(IScanModel scan)
        {
            if (null == scan)
            {
                return(null);
            }

            await this.Db.Scans.ReplaceOrInsertOneAsync((Scan)scan);

            return(scan);
        }
Exemple #5
0
        internal async Task<IScanModel> GetScanBy(string resourceName, string asset)
        {
            object scanResourceId = await this.Db.ScanResources.GetScanResourceIdBy(resourceName);//ScanResource.Name is indexed and unique
            if (scanResourceId == null)
                throw new InvalidOperationException($"There is no ScanResource item which's name is '{resourceName}' in the database.");

            IScanModel scan = await this.Db.Scans.GetBy(asset, scanResourceId);//Scan.Asset and Scan.ScanResourceId are indexed and unique
            if (scan == null)
                throw new InvalidOperationException($"There is no ScanResource item which's name is '{resourceName}' in the database.");

            return scan;
        }
Exemple #6
0
        internal async Task<int> AddChildScans(AddChildScansModel model)
        {
            int ret = 0;
            if (model.IsModelValid())
            {
                IScanModel parent = await this.GetScanBy(model.Parent.ResourceName, model.Parent.Asset);

                List<IScanModel> childList = new List<IScanModel>();
                foreach (ScanModel child in model.Childs)
                {
                    if (model.IsModelValid())
                    {
                        IScanModel entity = this.Db.ModelFactory.CreateScanModel();
                        entity.Asset = child.Asset;
                        entity.CreatedDate = await this.Db.GetDbDateTime();
                        entity.Enabled = true;
                        entity.Period = parent.Period;
                        entity.State = ScanState.NotStarted;
                        entity.MaxErrorLimit = parent.MaxErrorLimit;
                        entity.MaxOperationLimit = parent.MaxOperationLimit;

                        entity.Type = parent.Type;
                        entity.ScanResourceId = parent.ScanResourceId;
                        entity.SelectedNodeId = parent.SelectedNodeId;
                        entity.SaveType = parent.SaveType;
                        entity.ParentId = parent.ScanId;

                        childList.Add(entity);
                    }
                }
                if (childList.Any())
                {

                    foreach (IScanModel child in childList)
                    {
                        try
                        {
                            await this.Db.Scans.Save(child);
                        }
                        catch (Exception ex)
                        {
                            await Utility.CreateLogger(nameof(ScanWorks), nameof(AddChildScans)).Code(14).Error(ex).SaveAsync();
                        }
                    }
                    // ret = await this.Db.Scans.BatchSave(childList);
                }
            }

            return ret;
        }
Exemple #7
0
        public async Task <IActionResult> GetResult(string resourceName, string asset)
        {
            object ret = null;

            if (!String.IsNullOrEmpty(resourceName) && !String.IsNullOrEmpty(asset))
            {
                List <object> scanIdList = new List <object>();
                IScanModel    scan       = await this.GetScanBy(resourceName, asset);

                scanIdList.Add(scan.ScanId);

                ret = await this.Db.ScanInstances.GetListBy(new[] { scan.ScanId });
            }
            return(Json(ret));
        }
Exemple #8
0
        public Task <IActionResult> GetScanInstanceListBy(string resourceName, string asset, bool alsoGetParent, bool alsoGetChilds)
        {
            return(this.ResultAsync(async() =>
            {
                IEnumerable <ScanInstanceModel> ret = null;
                if (!String.IsNullOrEmpty(resourceName) && !String.IsNullOrEmpty(asset))
                {
                    List <object> scanIdList = new List <object>();
                    IScanModel scan = await new ScanWorks(this.Db).GetScanBy(resourceName, asset);
                    scanIdList.Add(scan.ScanId);

                    if (scan.ParentId != null && alsoGetParent)
                    {
                        scanIdList.Add(scan.ParentId);
                        // IScanModel parent = await this.Db.Scans.GetBy(scan.ParentId);
                        //if (null != parent)
                        //  scanIdList.Add(parent.ParentId);
                    }

                    if (alsoGetChilds)
                    {
                        var childList = await this.Db.Scans.GetRecursivelyChildList(scan.ScanId);
                        if (!childList.IsEmptyList())
                        {
                            scanIdList.AddRange(childList.Select(p => p.ScanId));
                        }
                    }

                    var temp = await this.Db.ScanInstances.GetListBy(scanIdList);
                    if (null != temp)
                    {
                        ret = temp.Select(i =>
                        {
                            ScanInstanceModel item = new ScanInstanceModel();
                            item.CopyPropertiesFrom(i);
                            return item;
                        });
                    }
                }

                return ret;
            }));
        }
Exemple #9
0
        internal async Task<int> EditScan(ScanModel model)
        {
            int ret = 0;
            if (model.IsModelValid())
            {
                IScanModel scan = await this.GetScanBy(model.ResourceName, model.Asset);

                scan.Period = model.Period;
                scan.Type = model.Type.Cast<int>().Cast<ScanType>();
                if (model.State.HasValue)
                    scan.State = model.State.Value.Cast<int>().Cast<ScanState>();

                await this.Db.Scans.Edit(scan);

                ret = 1;

                ConsoleObserver.Instance.Notify(nameof(ScanWorks) + "_" + nameof(EditScan), "Scan has been edit.", model);
            }

            return ret;
        }
Exemple #10
0
        public Task <IActionResult> SaveScan(ScanModel model)
        {
            return(this.ResultAsync(async() =>
            {
                int ret = 0;
                if (model.IsModelValid())
                {
                    IScanModel entity = this.Db.ModelFactory.CreateScanModel();
                    entity.Asset = model.Asset;
                    entity.Type = model.Type.Cast <int>().Cast <ScanType>();
                    entity.CreatedDate = await this.Db.GetDbDateTime();
                    entity.LastModifiedDate = entity.CreatedDate;
                    entity.Enabled = true;
                    entity.Period = model.Period;
                    entity.State = ScanState.NotStarted;
                    entity.MaxErrorLimit = model.MaxErrorLimit;
                    entity.MaxOperationLimit = model.MaxOperationLimit;
                    entity.SaveType = model.SaveType.Cast <int>().Cast <ScanSaveType>();
                    entity.Args = model.Args;
                    entity.SaveNullResult = model.SaveNullResult;
                    entity.MaxInstance = model.MaxInstance;

                    if (!String.IsNullOrEmpty(model.ResourceName))
                    {
                        entity.ScanResourceId = await this.Db.ScanResources.GetScanResourceIdBy(model.ResourceName);
                    }

                    if (!String.IsNullOrEmpty(model.NodeAddress))
                    {
                        entity.SelectedNodeId = await this.Db.Nodes.GetIdBy(model.NodeAddress);
                    }

                    await this.Db.Scans.Save(entity);
                    ret = 1;
                }

                return ret;
            }));
        }
Exemple #11
0
        private static async Task <INodeModel> ChoseNode(IKamajiContext db, IScanModel scan)
        {
            await _semaphoreSlim.WaitAsync();

            try
            {
                INodeModel ret = null;
                if (scan.SelectedNodeId != null)
                {
                    ret = await db.Nodes.GetBy(scan.SelectedNodeId);
                }
                else
                {
                    ret = await GetOptimumNode(db);
                }

                return(ret);
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Exemple #12
0
 private static async Task OnAssingFailed(IKamajiContext db, IScanModel scan)
 {
     scan.State = ScanState.AssignFailed;
     await db.Scans.Edit(scan);
 }
Exemple #13
0
        // işte şimdi node' a scan leri göndermemiz için bu çift raraflı dublex' i kullanacağız.

        public Task <int> AssignScan(INodeModel node, string prerequisiteName, string scanResourceName, IScanModel scan)
        {
            RestClient client = new RestClient(node.Address);

            ScanModel model = new ScanModel();

            model.CopyPropertiesFrom(scan);

            model.Type             = scan.Type.Cast <int>().Cast <ScanModel.ScanType>();
            model.PrerequisiteName = prerequisiteName;
            model.ResourceName     = scanResourceName;
            //model.State = scan.State.Cast<int>().Cast<ScanModel.ScanState>();
            model.SaveType       = scan.SaveType.Cast <int>().Cast <ScanModel.ScanSaveType>();
            model.Args           = scan.Args;
            model.SaveNullResult = scan.SaveNullResult;

            return(client.PostAsync <int>(NodesActions.AssignScan, model));
        }
Exemple #14
0
        public async Task <IActionResult> Spider(string resourceName, string asset)
        {
            HashSet <string> ret = new HashSet <string>();

            if (!String.IsNullOrEmpty(resourceName) && !String.IsNullOrEmpty(asset))
            {
                List <object> scanIdList = new List <object>();
                IScanModel    scan       = await this.GetScanBy(resourceName, asset);

                scanIdList.Add(scan.ScanId);

                if (scan.ParentId != null)
                {
                    scanIdList.Add(scan.ParentId);
                    // IScanModel parent = await this.Db.Scans.GetBy(scan.ParentId);
                    //if (null != parent)
                    //  scanIdList.Add(parent.ParentId);
                }

                var childList = await this.Db.Scans.GetRecursivelyChildList(scan.ScanId);

                if (!childList.IsEmptyList())
                {
                    scanIdList.AddRange(childList.Select(p => p.ScanId));
                }


                var temp = await this.Db.ScanInstances.GetListBy(scanIdList);

                if (null != temp)
                {
                    foreach (IScanInstanceModel scanInstance in temp)
                    {
                        if (scanInstance.Result != null)
                        {
                            var links = JsonConvert.DeserializeObject <List <string> >(scanInstance.Result);
                            ret.AddRange(links);
                        }
                    }
                }
            }


            return(Json(new { count = ret.Count, result = ret }));

            //const string host = "http://toastytech.com";
            //HashSet<string> spiderLinks = new HashSet<string>(System.IO.File.ReadAllLines("g:\\LinksSpider.csv").Select(i => host + i));

            //List<string> kamaji = new List<string>();
            //foreach (string link in ret)
            //{
            //    if (!spiderLinks.Contains(link))
            //        kamaji.Add(link);
            //}

            //List<string> spiders = new List<string>();
            //foreach (string link in spiderLinks)
            //{
            //    if (!ret.Contains(link))
            //        spiders.Add(link);
            //}

            //System.IO.File.WriteAllText("g:\\LinksKamaji.json", JsonConvert.SerializeObject(ret));


            //var result = new { count = ret.Count, links = ret, kamaji, spiders };


            ////System.IO.File.WriteAllText("g:\\result.json", JsonConvert.SerializeObject(result));

            //return Json(result);
        }