示例#1
0
        public void Add(long projectId, string name, string query)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (string.IsNullOrEmpty(query))
            {
                throw new ArgumentNullException(nameof(query));
            }

            if (_policyRuleRepository.Get(projectId, name).Any())
            {
                return;
            }

            var policy = new PolicyRules
            {
                Added     = _timeService.GetUtc(),
                Name      = name,
                ProjectId = projectId,
                Query     = query
            };

            _policyRuleRepository.Insert(policy);

            _policyRuleRepository.Save();
        }
        public SdlRuleDto[] GetProjectSdlRules(long projectId)
        {
            if (!_userAuthorityValidator.HasUserAuthorities(
                    _userPrincipal.Info.Id,
                    new[]
            {
                Authorities.UI.Project.Settings.ViewSdlPolicy
            },
                    projectId))
            {
                throw new UnauthorizedAccessException();
            }

            return(_policyRuleRepository.Get(projectId)
                   .Select(new SdlRuleRenderer().GetSpec())
                   .ToArray());
        }
        /// <summary>
        ///   Handles the specified event to handle.
        /// </summary>
        /// <param name="eventToHandle">The event to handle.</param>
        public void Handle(Event eventToHandle)
        {
            PublishPolicyEvent(eventToHandle, EventKeys.Policy.CheckStarted);

            Tasks task = null;

            using (var telemetryScope = _telemetryScopeProvider.Create <Tasks>(TelemetryOperationNames.Task.Update))
            {
                try
                {
                    var data = eventToHandle.Data;

                    if ((data == null) || !data.ContainsKey(Variables.TaskId))
                    {
                        PublishPolicyEvent(eventToHandle, EventKeys.Policy.CheckFinished);

                        return;
                    }

                    var taskId = long.Parse(data[Variables.TaskId]);

                    task = _taskRepository.GetById(taskId);

                    telemetryScope.SetEntity(task);

                    var projectId = long.Parse(data[Variables.ProjectId]);
                    var rules     = _policyRuleRepository.Get(projectId).ToArray();

                    var policyRules = rules.Select(_ => _ruleParser.ParsePolicyRule(_)).ToArray();

                    var success = true;

                    var violatedRules = new List <IPolicyRule>();

                    // ReSharper disable once LoopCanBePartlyConvertedToQuery
                    foreach (var policyRule in policyRules)
                    {
                        var ruleResult = _ruleExecutorDirector.Execute <IPolicyRule, PolicyRuleResult>(policyRule, data);

                        if (ruleResult.Success)
                        {
                            continue;
                        }

                        success = false;

                        violatedRules.Add(policyRule);
                    }

                    if (!success)
                    {
                        task.ViolatePolicy(
                            Resources.PolicyViolated
                            .FormatWith(violatedRules.Select(_ => _.Rule.Name).ToCommaSeparatedString()));
                    }
                    else
                    {
                        task.SuccessPolicy();
                    }

                    _taskRepository.Save();

                    PublishPolicyEvent(eventToHandle, EventKeys.Policy.CheckFinished);
                    PublishPolicyEvent(eventToHandle, success ? EventKeys.Policy.Successful : EventKeys.Policy.Violation);

                    telemetryScope.WriteSuccess();
                }
                catch (Exception exception)
                {
                    _logger.Error(Resources.PolicyCheckingError, exception);

                    try
                    {
                        task?.ErrorPolicy(Resources.PolicyError.FormatWith(exception.Message, exception.Format()));
                        _taskRepository.Save();
                    }
                    catch (Exception innerException)
                    {
                        _logger.Error(Resources.PolicyCheckingError, innerException);
                    }

                    PublishPolicyEvent(eventToHandle, EventKeys.Policy.CheckFinished);
                    PublishPolicyEvent(eventToHandle, EventKeys.Policy.Error);

                    telemetryScope.WriteException(exception);
                }
            }
        }