public async Task <SpfPollResult> Process(string domain)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            DnsResult <List <List <string> > > spfDnsRecords = await _dnsClient.GetSpfRecords(domain);

            if (!_config.AllowNullResults && (spfDnsRecords.IsErrored ||
                                              spfDnsRecords.Value.Count == 0 ||
                                              spfDnsRecords.Value.TrueForAll(x =>
                                                                             x.TrueForAll(string.IsNullOrWhiteSpace))))
            {
                throw new SpfPollerException($"Unable to retrieve spf records for {domain}.");
            }

            if (spfDnsRecords.IsErrored)
            {
                string message  = string.Format(SpfProcessorResource.FailedSpfRecordQueryErrorMessage, domain, spfDnsRecords.Error);
                string markdown = string.Format(SpfProcessorMarkdownResource.FailedSpfRecordQueryErrorMessage, domain, spfDnsRecords.Error);
                Guid   id       = Guid.Parse("76390C8C-12D5-47FF-981E-D880D2B77216");

                return(new SpfPollResult(new Error(id, ErrorType.Error, message, markdown)));
            }

            SpfRecords root = await _spfRecordsParser.Parse(domain, spfDnsRecords.Value, spfDnsRecords.MessageSize);

            int lookupCount = await _spfRecordExpander.ExpandSpfRecords(domain, root);

            SpfPollResult pollResult = new SpfPollResult(root, lookupCount, stopwatch.Elapsed);

            EvaluationResult <SpfPollResult> errors = await _pollResultRulesEvaluator.Evaluate(pollResult);

            pollResult.Errors.AddRange(errors.Errors);

            return(pollResult);
        }
Exemplo n.º 2
0
 public static SpfRecordsPolled ToSpfRecordsPolled(this SpfPollResult spfPollResult, string domain)
 {
     return(new SpfRecordsPolled(domain,
                                 spfPollResult.Records.ToContract(true),
                                 spfPollResult.QueryCount,
                                 spfPollResult.Elapsed,
                                 spfPollResult.Errors.Select(_ => _.ToContract()).ToList()));
 }
        public async Task GreaterThanOrEqualToMaxDnsQueryCountShouldGiveErrorWithAtLeastMessage()
        {
            SpfPollResult spfPollResult = new SpfPollResult(new SpfRecords(new List <SpfRecord>(), 0), 21, TimeSpan.MaxValue);
            List <Error>  result        = await _rule.Evaluate(spfPollResult);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0].ErrorType, Is.EqualTo(ErrorType.Error));
            Assert.That(result[0].Message, Is.EqualTo("The SPF specification limits the amount of DNS lookups for a record to 10. This record currently has at least 20 which will likely cause SPF failures."));
        }
        public async Task MoreThanTenSpfRecordsShouldGiveError()
        {
            SpfPollResult spfPollResult = new SpfPollResult(new SpfRecords(new List <SpfRecord>(), 0), 11, TimeSpan.MaxValue);
            List <Error>  result        = await _rule.Evaluate(spfPollResult);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0].ErrorType, Is.EqualTo(ErrorType.Error));
            Assert.That(result[0].Message, Is.EqualTo("The SPF specification limits the amount of DNS lookups for a record to 10. This record currently has 11 which will likely cause SPF failures."));
        }
        public async Task FiveSpfRecordsShouldGiveInfo()
        {
            SpfPollResult spfPollResult = new SpfPollResult(new SpfRecords(new List <SpfRecord>(), 0), 5, TimeSpan.MaxValue);
            List <Error>  result        = await _rule.Evaluate(spfPollResult);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0].ErrorType, Is.EqualTo(ErrorType.Info));
            Assert.That(result[0].Message, Is.EqualTo("5/10 DNS lookups used. You are likely to experience SPF failures if you exceed this limit of 10."));
        }
        public async Task TenSpfRecordsShouldGiveWarning()
        {
            SpfPollResult spfPollResult = new SpfPollResult(new SpfRecords(new List <SpfRecord>(), 0), 10, TimeSpan.MaxValue);
            List <Error>  result        = await _rule.Evaluate(spfPollResult);

            Assert.That(result.Count, Is.EqualTo(1));
            Assert.That(result[0].ErrorType, Is.EqualTo(ErrorType.Warning));
            Assert.That(result[0].Message, Is.EqualTo("The SPF specification limits the amount of DNS lookups for a record to 10. This record currently has 10 which could be taken over the limit by a third party change. You are likely to experience SPF failures if you exceed the limit of 10."));
        }
        public async Task SpfExceptionNotThrownWhenAllowNullResultsSetAndEmptyResult()
        {
            string domain = "abc.com";

            A.CallTo(() => _config.AllowNullResults).Returns(true);

            SpfPollResult result = await _spfProcessor.Process(domain);

            Assert.AreEqual(0, result.Records.Records.Count);
        }
Exemplo n.º 8
0
        public async Task Handle(SpfPollPending message)
        {
            try
            {
                SpfPollResult spfPollResult = await _processor.Process(message.Id);

                _log.LogInformation("Polled SPF records for {Domain}", message.Id);

                SpfRecordsPolled spfRecordsPolled = spfPollResult.ToSpfRecordsPolled(message.Id);

                _dispatcher.Dispatch(spfRecordsPolled, _config.SnsTopicArn);

                _log.LogInformation("Published SPF records for {Domain}", message.Id);
            }
            catch (System.Exception e)
            {
                string error = $"Error occurred polling domain {message.Id}";
                _log.LogError(e, error);
                throw;
            }
        }