public async Task AssignBuildSpecification(long nodeId, long?buildSpecId, string userName = null)
        {
            var it = await _ctx.Nodes.ById(nodeId);

            if (it == null)
            {
                throw new ArgumentException($"A Node with the InventoryItemId {nodeId} was not found.", nameof(nodeId));
            }

            var buildSpecExists = buildSpecId.HasValue && await _ctx.BuildSpecifications
                                  .OfBuildSpecType(BuildSpecificationTypeConstant.Application)
                                  .ExistsById(buildSpecId.Value);

            if (!buildSpecExists)
            {
                _logger.LogDebug($"Adding BuildSpec as null to {it.Fqdn}/{it.InventoryItemId} by {userName}");
                it.BuildSpecificationId = null;
                await _ctx.SaveChangesAsync();
            }
            else if (it.BuildSpecificationId != buildSpecId.Value)
            {
                it.BuildSpecificationId = buildSpecId.Value;
                await _ctx.SaveChangesAsync();
            }
        }
Exemple #2
0
        public async Task <long?> AddSoftwareComponent(JustificationTypeConstant type, long specId, string name,
                                                       string description, bool nonCore, PciScopeConstant[] pciScope, int[] environments)
        {
            if (!IsValidName(name))
            {
                throw new ArgumentException($@"The {type} name is not valid, it must not be empty whitespace.",
                                            nameof(name));
            }
            var parentId = await _ctx.BuildSpecifications.Where(p => p.Id == specId).Select(p => p.ParentId)
                           .FirstOrDefaultAsync();

            var ids = new List <long> {
                specId
            };

            if (parentId.HasValue)
            {
                ids.Add(parentId.Value);
            }

            if (_ctx.SoftwareComponents.Any(p =>
                                            ids.Contains(p.BuildSpecificationId) && p.Name == name && type == p.JustificationType))
            {
                return(null);
            }

            var @new = _ctx.SoftwareComponents.Add(new SoftwareComponent
            {
                JustificationType    = type,
                BuildSpecificationId = specId,
                Name        = name,
                Description = description,
                NonCore     = nonCore,
                PciScope    = pciScope == null || pciScope.Length == 0 || pciScope.Length == 3 ? null : pciScope.ConvertToFlag()
            }).Entity;

            await _ctx.SaveChangesAsync();

            var environmentCount = await _ctx.Environments.CountAsync();

            if (environments != null && environmentCount != environments.Length)
            {
                foreach (var environment in environments)
                {
                    _ctx.SoftwareComponentEnvironments.Add(new SoftwareComponentEnvironment
                    {
                        EnvironmentId       = environment,
                        SoftwareComponentId = @new.Id
                    });
                }
            }

            await _ctx.SaveChangesAsync();

            return(@new.Id);
        }
        public async Task SaveComplianceDataFromWebHook(AutomateWebHookMessage message)
        {
            message.end_time_utc = message.failed_critical_profiles?[0].controls?[0].results?[0].start_time;
            var node = await _ctx.Nodes.Active().ById(message.node_uuid);

            var text = node == null ? "did not find" : "found";

            _logger.LogInformation($"NotifyComplianceFailure WebHook got a compliance failure message for chef node id {message.node_uuid} and {text} a matching node {node?.Fqdn}.");
            if (node != null)
            {
                var run = await SaveComplianceData(node.InventoryItemId, message);

                if (run != null)
                {
                    if (run.EndDate != DateTime.MinValue)
                    {
                        node.LastComplianceResultDate = run.EndTime;
                        node.FailingSince ??= run.EndTime;
                    }
                    node.LastComplianceResultId = run.ResultId;

                    node.ComplianceStatus = run.Status;
                    await _ctx.SaveChangesAsync();
                }
            }
        }
        public async Task <long> AddJustification(JustificationTypeConstant type, long specId, string text)
        {
            if (!IsValidText(text))
            {
                throw new ArgumentException(@"The Text is not valid, it must not be empty whitespace.", nameof(text));
            }
            {
                var @new = _ctx.Justifications.Add(new data.Justification
                {
                    JustificationType    = type,
                    BuildSpecificationId = specId,
                    JustificationText    = text
                }).Entity;
                await _ctx.SaveChangesAsync();

                return(@new.Id);
            }
        }
Exemple #5
0
        public async Task AddOrUpdatePorts(long specId, long justificationId, bool external, bool outgoing,
                                           PortTypeConstant portType, string portString)
        {
            var valid = portString.ValidateDataPorts().Ports;
            var inDb  = _ctx.Ports.ForJustification(justificationId);

            _ctx.Ports.RemoveRange(inDb);

            foreach (var p in valid)
            {
                p.JustificationId      = justificationId;
                p.PortType             = portType;
                p.BuildSpecificationId = specId;
                p.IsExternal           = external || outgoing;
                p.IsOutgoing           = outgoing;
                _ctx.Ports.Add(p);
            }

            await _ctx.SaveChangesAsync();
        }
Exemple #6
0
        public async Task <string> Delete(long id, IUserSecurity user)
        {
            var type     = _buildSpecificationType == BuildSpecificationTypeConstant.OperatingSystem ? "OS" : "application";
            var toDelete = await Ctx.BuildSpecifications
                           .Include(p => p.Nodes)
                           .Include(p => p.Children)
                           .ById(_buildSpecificationType, id);

            if (toDelete == null)
            {
                return($"The {type} spec not found.");
            }

            if (toDelete.OwnerEmployeeId != user.EmployeeId)
            {
                return($"You are not authorized to delete this {type} spec.");
            }

            if (_buildSpecificationType == BuildSpecificationTypeConstant.Application && toDelete.Nodes.Any())
            {
                return
                    ("Some nodes have been assigned to this application spec and must be removed before allowing delete to occur.");
            }

            if (_buildSpecificationType == BuildSpecificationTypeConstant.OperatingSystem && toDelete.Children.Any())
            {
                return
                    ("Some application specs use this OS spec and must be changed before allowing the delete to occur.");
            }

            Ctx.SoftwareComponentEnvironments.RemoveRange(Ctx.SoftwareComponentEnvironments.Where(p => p.SoftwareComponent.BuildSpecificationId == toDelete.Id));
            Ctx.SoftwareComponents.RemoveRange(Ctx.SoftwareComponents.ForBuildSpec(toDelete.Id));
            Ctx.Justifications.RemoveRange(Ctx.Justifications.ForBuildSpec(toDelete.Id));
            Ctx.Ports.RemoveRange(Ctx.Ports.ForBuildSpec(toDelete.Id));
            Ctx.BuildSpecifications.Remove(toDelete);
            await Ctx.SaveChangesAsync();

            return(null);
        }
Exemple #7
0
        public async Task AssignBuildSpecification(long nodeId, long?buildSpecId)
        {
            var it = await _ctx.Nodes.ById(nodeId);

            if (it == null)
            {
                throw new ArgumentException(@"A Node with the InventoryItemId {id} was not found.", nameof(nodeId));
            }

            if (it.BuildSpecificationId != buildSpecId)
            {
                it.BuildSpecificationId = buildSpecId;
                await _ctx.SaveChangesAsync();
            }
        }
Exemple #8
0
        public async Task SaveComplianceDataFromWebHook(AutomateWebHookMessage message)
        {
            var node = await _ctx.Nodes.Active().ById(message.node_uuid);

            var text = node == null ? "did not find" : "found";

            _logger.LogTrace($"NotifyComplianceFailure WebHook got a compliance failure message for chef node id {message.node_uuid} and {text} a matching node {node?.Fqdn}.");
            if (node != null)
            {
                var run = await SaveComplianceData(node.InventoryItemId, message);

                if (run != null)
                {
                    node.LastComplianceResultDate = run.EndTime;
                    node.LastComplianceResultId   = run.ResultId;
                    if (!node.FailingSince.HasValue)
                    {
                        node.FailingSince = run.EndTime;
                    }
                    node.ComplianceStatus = run.Status;
                    await _ctx.SaveChangesAsync();
                }
            }
        }
        private async Task NotifyComplianceByChefId(AutomateWebHookMessage message)
        {
            var result = await _ctx.Nodes
                         .Include(p => p.Owner).ThenInclude(p => p.Supervisor).ThenInclude(p => p.Supervisor)
                         .Active()
                         .InPciScope()
                         .InChefScope()
                         .ProductIsNotExlcuded()
                         .ById(message.ChefNodeId);


            var now = DateTime.Now;

            if (result?.BuildSpecificationId != null && (result.LastEmailedOn == null || result.LastEmailedOn < now.AddDays(-1)))
            {
                var names  = new List <string>();
                var emails = new List <string>();

                AddToLists(result.Owner, LevelConstants.Self, ref emails, ref names);

                var bs = await _ctx.BuildSpecifications
                         .AsNoTracking()
                         .Include(p => p.Owner).ThenInclude(p => p.Supervisor).ThenInclude(p => p.Supervisor)
                         .Include(p => p.Parent).ThenInclude(p => p.Owner).ThenInclude(p => p.Supervisor).ThenInclude(p => p.Supervisor)
                         .ById(result.BuildSpecificationId.GetValueOrDefault());


                if (!string.IsNullOrWhiteSpace(bs.EmailAddress))
                {
                    emails.Add(bs.EmailAddress.ToLower()); //Add AppSpec Email
                }
                if (bs.IncludeRemedyEmailList && !string.IsNullOrEmpty(result.RemedyGroupEmailList))
                {
                    emails.Add(result.RemedyGroupEmailList.ToLower());
                }
                if (!string.IsNullOrWhiteSpace(bs.Parent?.EmailAddress))
                {
                    emails.Add(bs.Parent.EmailAddress.ToLower()); //Add OS Spec Email
                }
                AddToLists(bs.Owner, LevelConstants.Self, ref emails, ref names);
                AddToLists(bs.Parent?.Owner, LevelConstants.Self, ref emails, ref names);

                if (result.FailingSince < now.AddDays(-30))
                {
                    AddToLists(result.Owner, LevelConstants.Boss, ref emails, ref names);
                    AddToLists(bs.Owner, LevelConstants.Boss, ref emails, ref names);
                    AddToLists(bs.Parent?.Owner, LevelConstants.Boss, ref emails, ref names);
                }

                if (result.FailingSince < now.AddDays(-60))
                {
                    AddToLists(result.Owner, LevelConstants.BossesBoss, ref emails, ref names);
                    AddToLists(bs.Owner, LevelConstants.BossesBoss, ref emails, ref names);
                    AddToLists(bs.Parent?.Owner, LevelConstants.BossesBoss, ref emails, ref names);
                }

                var toSend  = emails.Distinct().ToArray();
                var toNames = names.Distinct().Reverse().ToArray();
                _logger.LogInformation(
                    $"Emailing Compliance Failure to {string.Join(",", toNames)} about {result.Fqdn} with pci-scope {result.PciScope}");

                await SendComplianceFailureMail(toSend, toNames, result.Fqdn, result.PciScope.ToString(),
                                                message.automate_failure_url);

                result.LastEmailedOn = now;
                await _ctx.SaveChangesAsync();
            }
        }