public async Task TestScanResultCreateGet()
        {
            var result = await _resultService.CreateScanResult(null);

            Assert.NotNull(result.Id);

            await _resultService.UpdateScanResultForBackend(
                result.Id, "dummy", new ScanResultMessage
            {
                Duration = 100,
                Status   = ScanResultStatus.Succeeded,
                Threats  = new[] { "Test" }
            });

            result = await _resultService.GetScanResult(result.Id);

            Assert.NotNull(result.Id);
            Assert.That(result.Results, Contains.Key("dummy"));

            var dummyResult = result.Results["dummy"];

            Assert.IsTrue(dummyResult.Status == ScanResultStatus.Succeeded);
            Assert.AreEqual(dummyResult.Duration, 100);
            Assert.IsNotEmpty(dummyResult.Threats);
            Assert.That(dummyResult.Threats, Is.EquivalentTo(new[] { "Test" }));
        }
Esempio n. 2
0
        public async Task <IActionResult> Index(string id)
        {
            var scanResult = await _scanResultService.GetScanResult(id);

            if (scanResult == null)
            {
                return(NotFound());
            }

            return(Ok(scanResult));
        }
        /// <inheritdoc />
        public async Task Report(string resultId, string backendId, ScanResultMessage result)
        {
            _logger.LogInformation(
                $"Received a result from {backendId} for {result} with status {result.Status} " +
                $"and threats {string.Join(",", result.Threats)}");

            await _scanResultService.UpdateScanResultForBackend(resultId, backendId, result);

            var scanResult = await _scanResultService.GetScanResult(resultId);

            if (scanResult?.CallbackUrl == null)
            {
                return;
            }

            _backgroundJobClient.Create <IScanResultJob>(
                x => x.Notify(scanResult.CallbackUrl, resultId, backendId, result),
                new EnqueuedState("default"));
        }
Esempio n. 4
0
        private async Task StoreScanResult(ScanResultMessage message)
        {
            message.Threats ??= new string[] { };

            _logger.LogInformation(
                $"Received a result from {message.Backend} for {message.Id} " +
                $"with threats {string.Join(",", message.Threats)}");

            await _scanResultService.UpdateScanResultForBackend(
                message.Id, message.Backend, message.Duration, true,
                message.Succeeded, message.Threats);

            var result = await _scanResultService.GetScanResult(message.Id);

            if (result?.CallbackUrl == null)
            {
                return;
            }

            var cancellationTokenSource = new CancellationTokenSource(
                TimeSpan.FromSeconds(3));

            using var httpClient = _httpClientFactory.CreateClient();

            try
            {
                var response = await httpClient.PostAsync(
                    result.CallbackUrl,
                    new StringContent(JsonConvert.SerializeObject(result), Encoding.UTF8, "application/json"),
                    cancellationTokenSource.Token);

                response.EnsureSuccessStatusCode();
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, $"Failed to POST to callback URL {result.CallbackUrl}");
            }
        }