Restructure solution layout by module
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace StellaOps.Concelier.Connector.Vndr.Adobe.Internal;
|
||||
|
||||
internal sealed record AdobeBulletinDto(
|
||||
string AdvisoryId,
|
||||
string Title,
|
||||
DateTimeOffset Published,
|
||||
IReadOnlyList<AdobeProductEntry> Products,
|
||||
IReadOnlyList<string> Cves,
|
||||
string DetailUrl,
|
||||
string? Summary)
|
||||
{
|
||||
public static AdobeBulletinDto Create(
|
||||
string advisoryId,
|
||||
string title,
|
||||
DateTimeOffset published,
|
||||
IEnumerable<AdobeProductEntry>? products,
|
||||
IEnumerable<string>? cves,
|
||||
Uri detailUri,
|
||||
string? summary)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(advisoryId);
|
||||
ArgumentException.ThrowIfNullOrEmpty(title);
|
||||
ArgumentNullException.ThrowIfNull(detailUri);
|
||||
|
||||
var productList = products?
|
||||
.Where(static p => !string.IsNullOrWhiteSpace(p.Product))
|
||||
.Select(static p => p with { Product = p.Product.Trim() })
|
||||
.Distinct(AdobeProductEntryComparer.Instance)
|
||||
.OrderBy(static p => p.Product, StringComparer.OrdinalIgnoreCase)
|
||||
.ThenBy(static p => p.Platform, StringComparer.OrdinalIgnoreCase)
|
||||
.ThenBy(static p => p.Track, StringComparer.OrdinalIgnoreCase)
|
||||
.ToList()
|
||||
?? new List<AdobeProductEntry>();
|
||||
|
||||
var cveList = cves?.Where(static c => !string.IsNullOrWhiteSpace(c))
|
||||
.Select(static c => c.Trim().ToUpperInvariant())
|
||||
.Distinct(StringComparer.Ordinal)
|
||||
.OrderBy(static c => c, StringComparer.Ordinal)
|
||||
.ToList() ?? new List<string>();
|
||||
|
||||
return new AdobeBulletinDto(
|
||||
advisoryId.ToUpperInvariant(),
|
||||
title.Trim(),
|
||||
published.ToUniversalTime(),
|
||||
productList,
|
||||
cveList,
|
||||
detailUri.ToString(),
|
||||
string.IsNullOrWhiteSpace(summary) ? null : summary.Trim());
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record AdobeProductEntry(
|
||||
string Product,
|
||||
string Track,
|
||||
string Platform,
|
||||
string? AffectedVersion,
|
||||
string? UpdatedVersion,
|
||||
string? Priority,
|
||||
string? Availability);
|
||||
|
||||
internal sealed class AdobeProductEntryComparer : IEqualityComparer<AdobeProductEntry>
|
||||
{
|
||||
public static AdobeProductEntryComparer Instance { get; } = new();
|
||||
|
||||
public bool Equals(AdobeProductEntry? x, AdobeProductEntry? y)
|
||||
{
|
||||
if (ReferenceEquals(x, y))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (x is null || y is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return string.Equals(x.Product, y.Product, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(x.Track, y.Track, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(x.Platform, y.Platform, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(x.AffectedVersion, y.AffectedVersion, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(x.UpdatedVersion, y.UpdatedVersion, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(x.Priority, y.Priority, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(x.Availability, y.Availability, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public int GetHashCode(AdobeProductEntry obj)
|
||||
{
|
||||
var hash = new HashCode();
|
||||
hash.Add(obj.Product, StringComparer.OrdinalIgnoreCase);
|
||||
hash.Add(obj.Track, StringComparer.OrdinalIgnoreCase);
|
||||
hash.Add(obj.Platform, StringComparer.OrdinalIgnoreCase);
|
||||
hash.Add(obj.AffectedVersion, StringComparer.OrdinalIgnoreCase);
|
||||
hash.Add(obj.UpdatedVersion, StringComparer.OrdinalIgnoreCase);
|
||||
hash.Add(obj.Priority, StringComparer.OrdinalIgnoreCase);
|
||||
hash.Add(obj.Availability, StringComparer.OrdinalIgnoreCase);
|
||||
return hash.ToHashCode();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace StellaOps.Concelier.Connector.Vndr.Adobe.Internal;
|
||||
|
||||
internal sealed record AdobeCursor(
|
||||
DateTimeOffset? LastPublished,
|
||||
IReadOnlyCollection<Guid> PendingDocuments,
|
||||
IReadOnlyCollection<Guid> PendingMappings,
|
||||
IReadOnlyDictionary<string, AdobeFetchCacheEntry>? FetchCache)
|
||||
{
|
||||
public static AdobeCursor Empty { get; } = new(null, Array.Empty<Guid>(), Array.Empty<Guid>(), null);
|
||||
|
||||
public BsonDocument ToBsonDocument()
|
||||
{
|
||||
var document = new BsonDocument();
|
||||
if (LastPublished.HasValue)
|
||||
{
|
||||
document["lastPublished"] = LastPublished.Value.UtcDateTime;
|
||||
}
|
||||
|
||||
document["pendingDocuments"] = new BsonArray(PendingDocuments.Select(id => id.ToString()));
|
||||
document["pendingMappings"] = new BsonArray(PendingMappings.Select(id => id.ToString()));
|
||||
|
||||
if (FetchCache is { Count: > 0 })
|
||||
{
|
||||
var cacheDocument = new BsonDocument();
|
||||
foreach (var (key, entry) in FetchCache)
|
||||
{
|
||||
cacheDocument[key] = entry.ToBson();
|
||||
}
|
||||
|
||||
document["fetchCache"] = cacheDocument;
|
||||
}
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
public static AdobeCursor FromBsonDocument(BsonDocument? document)
|
||||
{
|
||||
if (document is null || document.ElementCount == 0)
|
||||
{
|
||||
return Empty;
|
||||
}
|
||||
|
||||
DateTimeOffset? lastPublished = null;
|
||||
if (document.TryGetValue("lastPublished", out var lastPublishedValue))
|
||||
{
|
||||
lastPublished = ReadDateTime(lastPublishedValue);
|
||||
}
|
||||
|
||||
var pendingDocuments = ReadGuidArray(document, "pendingDocuments");
|
||||
var pendingMappings = ReadGuidArray(document, "pendingMappings");
|
||||
var fetchCache = ReadFetchCache(document);
|
||||
|
||||
return new AdobeCursor(lastPublished, pendingDocuments, pendingMappings, fetchCache);
|
||||
}
|
||||
|
||||
public AdobeCursor WithLastPublished(DateTimeOffset? value)
|
||||
=> this with { LastPublished = value?.ToUniversalTime() };
|
||||
|
||||
public AdobeCursor WithPendingDocuments(IEnumerable<Guid> ids)
|
||||
=> this with { PendingDocuments = ids?.Distinct().ToArray() ?? Array.Empty<Guid>() };
|
||||
|
||||
public AdobeCursor WithPendingMappings(IEnumerable<Guid> ids)
|
||||
=> this with { PendingMappings = ids?.Distinct().ToArray() ?? Array.Empty<Guid>() };
|
||||
|
||||
public AdobeCursor WithFetchCache(IDictionary<string, AdobeFetchCacheEntry>? cache)
|
||||
{
|
||||
if (cache is null)
|
||||
{
|
||||
return this with { FetchCache = null };
|
||||
}
|
||||
|
||||
var target = new Dictionary<string, AdobeFetchCacheEntry>(cache, StringComparer.Ordinal);
|
||||
return this with { FetchCache = target };
|
||||
}
|
||||
|
||||
public bool TryGetFetchCache(string key, out AdobeFetchCacheEntry entry)
|
||||
{
|
||||
var cache = FetchCache;
|
||||
if (cache is null)
|
||||
{
|
||||
entry = AdobeFetchCacheEntry.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (cache.TryGetValue(key, out var value) && value is not null)
|
||||
{
|
||||
entry = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
entry = AdobeFetchCacheEntry.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static DateTimeOffset? ReadDateTime(BsonValue value)
|
||||
{
|
||||
return value.BsonType switch
|
||||
{
|
||||
BsonType.DateTime => DateTime.SpecifyKind(value.ToUniversalTime(), DateTimeKind.Utc),
|
||||
BsonType.String when DateTimeOffset.TryParse(value.AsString, out var parsed) => parsed.ToUniversalTime(),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
private static IReadOnlyCollection<Guid> ReadGuidArray(BsonDocument document, string field)
|
||||
{
|
||||
if (!document.TryGetValue(field, out var value) || value is not BsonArray array)
|
||||
{
|
||||
return Array.Empty<Guid>();
|
||||
}
|
||||
|
||||
var list = new List<Guid>(array.Count);
|
||||
foreach (var element in array)
|
||||
{
|
||||
if (Guid.TryParse(element.ToString(), out var guid))
|
||||
{
|
||||
list.Add(guid);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static IReadOnlyDictionary<string, AdobeFetchCacheEntry>? ReadFetchCache(BsonDocument document)
|
||||
{
|
||||
if (!document.TryGetValue("fetchCache", out var value) || value is not BsonDocument cacheDocument)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var dictionary = new Dictionary<string, AdobeFetchCacheEntry>(StringComparer.Ordinal);
|
||||
foreach (var element in cacheDocument.Elements)
|
||||
{
|
||||
if (element.Value is BsonDocument entryDocument)
|
||||
{
|
||||
dictionary[element.Name] = AdobeFetchCacheEntry.FromBson(entryDocument);
|
||||
}
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed record AdobeFetchCacheEntry(string Sha256)
|
||||
{
|
||||
public static AdobeFetchCacheEntry Empty { get; } = new(string.Empty);
|
||||
|
||||
public BsonDocument ToBson()
|
||||
{
|
||||
var document = new BsonDocument
|
||||
{
|
||||
["sha256"] = Sha256,
|
||||
};
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
public static AdobeFetchCacheEntry FromBson(BsonDocument document)
|
||||
{
|
||||
var sha = document.TryGetValue("sha256", out var shaValue) ? shaValue.AsString : string.Empty;
|
||||
return new AdobeFetchCacheEntry(sha);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,405 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using AngleSharp.Dom;
|
||||
using AngleSharp.Html.Dom;
|
||||
using AngleSharp.Html.Parser;
|
||||
|
||||
namespace StellaOps.Concelier.Connector.Vndr.Adobe.Internal;
|
||||
|
||||
internal static class AdobeDetailParser
|
||||
{
|
||||
private static readonly HtmlParser Parser = new();
|
||||
private static readonly Regex CveRegex = new("CVE-\\d{4}-\\d{4,}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
private static readonly string[] DateMarkers = { "date published", "release date", "published" };
|
||||
|
||||
public static AdobeBulletinDto Parse(string html, AdobeDocumentMetadata metadata)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrEmpty(html);
|
||||
ArgumentNullException.ThrowIfNull(metadata);
|
||||
|
||||
using var document = Parser.ParseDocument(html);
|
||||
var title = metadata.Title ?? document.QuerySelector("h1")?.TextContent?.Trim() ?? metadata.AdvisoryId;
|
||||
var summary = document.QuerySelector("p")?.TextContent?.Trim();
|
||||
|
||||
var published = metadata.PublishedUtc ?? TryExtractPublished(document) ?? DateTimeOffset.UtcNow;
|
||||
|
||||
var cves = ExtractCves(document.Body?.TextContent ?? string.Empty);
|
||||
var products = ExtractProductEntries(title, document);
|
||||
|
||||
return AdobeBulletinDto.Create(
|
||||
metadata.AdvisoryId,
|
||||
title,
|
||||
published,
|
||||
products,
|
||||
cves,
|
||||
metadata.DetailUri,
|
||||
summary);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<string> ExtractCves(string text)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
return Array.Empty<string>();
|
||||
}
|
||||
|
||||
var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
foreach (Match match in CveRegex.Matches(text))
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(match.Value))
|
||||
{
|
||||
set.Add(match.Value.ToUpperInvariant());
|
||||
}
|
||||
}
|
||||
|
||||
return set.Count == 0 ? Array.Empty<string>() : set.OrderBy(static cve => cve, StringComparer.Ordinal).ToArray();
|
||||
}
|
||||
|
||||
private static IReadOnlyList<AdobeProductEntry> ExtractProductEntries(string title, IDocument document)
|
||||
{
|
||||
var builders = new Dictionary<AdobeProductKey, AdobeProductEntryBuilder>(AdobeProductKeyComparer.Instance);
|
||||
|
||||
foreach (var builder in ParseAffectedTable(document))
|
||||
{
|
||||
builders[builder.Key] = builder;
|
||||
}
|
||||
|
||||
foreach (var updated in ParseUpdatedTable(document))
|
||||
{
|
||||
if (builders.TryGetValue(updated.Key, out var builder))
|
||||
{
|
||||
builder.UpdatedVersion ??= updated.UpdatedVersion;
|
||||
builder.Priority ??= updated.Priority;
|
||||
builder.Availability ??= updated.Availability;
|
||||
}
|
||||
else
|
||||
{
|
||||
builders[updated.Key] = updated;
|
||||
}
|
||||
}
|
||||
|
||||
if (builders.Count == 0 && !string.IsNullOrWhiteSpace(title))
|
||||
{
|
||||
var fallback = new AdobeProductEntryBuilder(
|
||||
NormalizeWhitespace(title),
|
||||
string.Empty,
|
||||
string.Empty)
|
||||
{
|
||||
AffectedVersion = null,
|
||||
UpdatedVersion = null,
|
||||
Priority = null,
|
||||
Availability = null
|
||||
};
|
||||
|
||||
builders[fallback.Key] = fallback;
|
||||
}
|
||||
|
||||
return builders.Values
|
||||
.Select(static builder => builder.ToEntry())
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private static IEnumerable<AdobeProductEntryBuilder> ParseAffectedTable(IDocument document)
|
||||
{
|
||||
var table = FindTableByHeader(document, "Affected Versions");
|
||||
if (table is null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
foreach (var row in table.Rows.Skip(1))
|
||||
{
|
||||
var cells = row.Cells;
|
||||
if (cells.Length < 3)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var product = NormalizeWhitespace(cells[0]?.TextContent);
|
||||
var track = NormalizeWhitespace(cells.ElementAtOrDefault(1)?.TextContent);
|
||||
var platformText = NormalizeWhitespace(cells.ElementAtOrDefault(3)?.TextContent);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(product))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var affectedCell = cells[2];
|
||||
foreach (var line in ExtractLines(affectedCell))
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var (platform, versionText) = SplitPlatformLine(line, platformText);
|
||||
var builder = new AdobeProductEntryBuilder(product, track, platform)
|
||||
{
|
||||
AffectedVersion = versionText
|
||||
};
|
||||
|
||||
yield return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<AdobeProductEntryBuilder> ParseUpdatedTable(IDocument document)
|
||||
{
|
||||
var table = FindTableByHeader(document, "Updated Versions");
|
||||
if (table is null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
foreach (var row in table.Rows.Skip(1))
|
||||
{
|
||||
var cells = row.Cells;
|
||||
if (cells.Length < 3)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var product = NormalizeWhitespace(cells[0]?.TextContent);
|
||||
var track = NormalizeWhitespace(cells.ElementAtOrDefault(1)?.TextContent);
|
||||
var platformText = NormalizeWhitespace(cells.ElementAtOrDefault(3)?.TextContent);
|
||||
var priority = NormalizeWhitespace(cells.ElementAtOrDefault(4)?.TextContent);
|
||||
var availability = NormalizeWhitespace(cells.ElementAtOrDefault(5)?.TextContent);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(product))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var updatedCell = cells[2];
|
||||
var lines = ExtractLines(updatedCell);
|
||||
if (lines.Count == 0)
|
||||
{
|
||||
lines.Add(updatedCell.TextContent ?? string.Empty);
|
||||
}
|
||||
|
||||
foreach (var line in lines)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var (platform, versionText) = SplitPlatformLine(line, platformText);
|
||||
var builder = new AdobeProductEntryBuilder(product, track, platform)
|
||||
{
|
||||
UpdatedVersion = versionText,
|
||||
Priority = priority,
|
||||
Availability = availability
|
||||
};
|
||||
|
||||
yield return builder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static IHtmlTableElement? FindTableByHeader(IDocument document, string headerText)
|
||||
{
|
||||
return document
|
||||
.QuerySelectorAll("table")
|
||||
.OfType<IHtmlTableElement>()
|
||||
.FirstOrDefault(table => table.TextContent.Contains(headerText, StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
private static List<string> ExtractLines(IElement? cell)
|
||||
{
|
||||
var lines = new List<string>();
|
||||
if (cell is null)
|
||||
{
|
||||
return lines;
|
||||
}
|
||||
|
||||
var paragraphs = cell.QuerySelectorAll("p").Select(static p => p.TextContent).ToArray();
|
||||
if (paragraphs.Length > 0)
|
||||
{
|
||||
foreach (var paragraph in paragraphs)
|
||||
{
|
||||
var normalized = NormalizeWhitespace(paragraph);
|
||||
if (!string.IsNullOrWhiteSpace(normalized))
|
||||
{
|
||||
lines.Add(normalized);
|
||||
}
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
var items = cell.QuerySelectorAll("li").Select(static li => li.TextContent).ToArray();
|
||||
if (items.Length > 0)
|
||||
{
|
||||
foreach (var item in items)
|
||||
{
|
||||
var normalized = NormalizeWhitespace(item);
|
||||
if (!string.IsNullOrWhiteSpace(normalized))
|
||||
{
|
||||
lines.Add(normalized);
|
||||
}
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
var raw = NormalizeWhitespace(cell.TextContent);
|
||||
if (!string.IsNullOrWhiteSpace(raw))
|
||||
{
|
||||
lines.AddRange(raw.Split(new[] { '\n' }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
private static (string Platform, string? Version) SplitPlatformLine(string line, string? fallbackPlatform)
|
||||
{
|
||||
var separatorIndex = line.IndexOf('-', StringComparison.Ordinal);
|
||||
if (separatorIndex > 0 && separatorIndex < line.Length - 1)
|
||||
{
|
||||
var prefix = line[..separatorIndex].Trim();
|
||||
var versionText = line[(separatorIndex + 1)..].Trim();
|
||||
return (NormalizePlatform(prefix) ?? NormalizePlatform(fallbackPlatform) ?? fallbackPlatform ?? string.Empty, versionText);
|
||||
}
|
||||
|
||||
return (NormalizePlatform(fallbackPlatform) ?? fallbackPlatform ?? string.Empty, line.Trim());
|
||||
}
|
||||
|
||||
private static string? NormalizePlatform(string? platform)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(platform))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var trimmed = platform.Trim();
|
||||
return trimmed.ToLowerInvariant() switch
|
||||
{
|
||||
"win" or "windows" => "Windows",
|
||||
"mac" or "macos" or "mac os" => "macOS",
|
||||
"windows & macos" or "windows & macos" => "Windows & macOS",
|
||||
_ => trimmed
|
||||
};
|
||||
}
|
||||
|
||||
private static DateTimeOffset? TryExtractPublished(IDocument document)
|
||||
{
|
||||
var candidates = new List<string?>();
|
||||
candidates.Add(document.QuerySelector("time")?.GetAttribute("datetime"));
|
||||
candidates.Add(document.QuerySelector("time")?.TextContent);
|
||||
|
||||
foreach (var marker in DateMarkers)
|
||||
{
|
||||
var element = document.All.FirstOrDefault(node => node.TextContent.Contains(marker, StringComparison.OrdinalIgnoreCase));
|
||||
if (element is not null)
|
||||
{
|
||||
candidates.Add(element.TextContent);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var candidate in candidates)
|
||||
{
|
||||
if (TryParseDate(candidate, out var parsed))
|
||||
{
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool TryParseDate(string? value, out DateTimeOffset result)
|
||||
{
|
||||
result = default;
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var trimmed = value.Trim();
|
||||
if (DateTimeOffset.TryParse(trimmed, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out result))
|
||||
{
|
||||
result = result.ToUniversalTime();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (DateTime.TryParse(trimmed, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
|
||||
{
|
||||
result = new DateTimeOffset(date, TimeSpan.Zero).ToUniversalTime();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static string NormalizeWhitespace(string? value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var sanitized = value ?? string.Empty;
|
||||
return string.Join(" ", sanitized.Split((char[]?)null, StringSplitOptions.RemoveEmptyEntries));
|
||||
}
|
||||
|
||||
private sealed record AdobeProductKey(string Product, string Track, string Platform);
|
||||
|
||||
private sealed class AdobeProductKeyComparer : IEqualityComparer<AdobeProductKey>
|
||||
{
|
||||
public static AdobeProductKeyComparer Instance { get; } = new();
|
||||
|
||||
public bool Equals(AdobeProductKey? x, AdobeProductKey? y)
|
||||
{
|
||||
if (ReferenceEquals(x, y))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (x is null || y is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return string.Equals(x.Product, y.Product, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(x.Track, y.Track, StringComparison.OrdinalIgnoreCase)
|
||||
&& string.Equals(x.Platform, y.Platform, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public int GetHashCode(AdobeProductKey obj)
|
||||
{
|
||||
var hash = new HashCode();
|
||||
hash.Add(obj.Product, StringComparer.OrdinalIgnoreCase);
|
||||
hash.Add(obj.Track, StringComparer.OrdinalIgnoreCase);
|
||||
hash.Add(obj.Platform, StringComparer.OrdinalIgnoreCase);
|
||||
return hash.ToHashCode();
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class AdobeProductEntryBuilder
|
||||
{
|
||||
public AdobeProductEntryBuilder(string product, string track, string platform)
|
||||
{
|
||||
Product = NormalizeWhitespace(product);
|
||||
Track = NormalizeWhitespace(track);
|
||||
Platform = NormalizeWhitespace(platform);
|
||||
}
|
||||
|
||||
public AdobeProductKey Key => new(Product, Track, Platform);
|
||||
|
||||
public string Product { get; }
|
||||
public string Track { get; }
|
||||
public string Platform { get; }
|
||||
|
||||
public string? AffectedVersion { get; set; }
|
||||
public string? UpdatedVersion { get; set; }
|
||||
public string? Priority { get; set; }
|
||||
public string? Availability { get; set; }
|
||||
|
||||
public AdobeProductEntry ToEntry()
|
||||
=> new(Product, Track, Platform, AffectedVersion, UpdatedVersion, Priority, Availability);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using StellaOps.Concelier.Storage.Mongo.Documents;
|
||||
|
||||
namespace StellaOps.Concelier.Connector.Vndr.Adobe.Internal;
|
||||
|
||||
internal sealed record AdobeDocumentMetadata(
|
||||
string AdvisoryId,
|
||||
string? Title,
|
||||
DateTimeOffset? PublishedUtc,
|
||||
Uri DetailUri)
|
||||
{
|
||||
private const string AdvisoryIdKey = "advisoryId";
|
||||
private const string TitleKey = "title";
|
||||
private const string PublishedKey = "published";
|
||||
|
||||
public static AdobeDocumentMetadata FromDocument(DocumentRecord document)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(document);
|
||||
|
||||
if (document.Metadata is null)
|
||||
{
|
||||
throw new InvalidOperationException("Adobe document metadata is missing.");
|
||||
}
|
||||
|
||||
var advisoryId = document.Metadata.TryGetValue(AdvisoryIdKey, out var idValue) ? idValue : null;
|
||||
if (string.IsNullOrWhiteSpace(advisoryId))
|
||||
{
|
||||
throw new InvalidOperationException("Adobe document advisoryId metadata missing.");
|
||||
}
|
||||
|
||||
var title = document.Metadata.TryGetValue(TitleKey, out var titleValue) ? titleValue : null;
|
||||
DateTimeOffset? published = null;
|
||||
if (document.Metadata.TryGetValue(PublishedKey, out var publishedValue)
|
||||
&& DateTimeOffset.TryParse(publishedValue, out var parsedPublished))
|
||||
{
|
||||
published = parsedPublished.ToUniversalTime();
|
||||
}
|
||||
|
||||
if (!Uri.TryCreate(document.Uri, UriKind.Absolute, out var detailUri))
|
||||
{
|
||||
throw new InvalidOperationException("Adobe document URI invalid.");
|
||||
}
|
||||
|
||||
return new AdobeDocumentMetadata(advisoryId.Trim(), string.IsNullOrWhiteSpace(title) ? null : title.Trim(), published, detailUri);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
using System;
|
||||
|
||||
namespace StellaOps.Concelier.Connector.Vndr.Adobe.Internal;
|
||||
|
||||
internal sealed record AdobeIndexEntry(string AdvisoryId, Uri DetailUri, DateTimeOffset PublishedUtc, string? Title);
|
||||
@@ -0,0 +1,159 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using AngleSharp.Dom;
|
||||
using AngleSharp.Html.Parser;
|
||||
|
||||
namespace StellaOps.Concelier.Connector.Vndr.Adobe.Internal;
|
||||
|
||||
internal static class AdobeIndexParser
|
||||
{
|
||||
private static readonly HtmlParser Parser = new();
|
||||
private static readonly Regex AdvisoryIdRegex = new("(APSB|APA)\\d{2}-\\d{2,}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
private static readonly string[] ExplicitFormats =
|
||||
{
|
||||
"MMMM d, yyyy",
|
||||
"MMM d, yyyy",
|
||||
"M/d/yyyy",
|
||||
"MM/dd/yyyy",
|
||||
"yyyy-MM-dd",
|
||||
};
|
||||
|
||||
public static IReadOnlyCollection<AdobeIndexEntry> Parse(string html, Uri baseUri)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(html);
|
||||
ArgumentNullException.ThrowIfNull(baseUri);
|
||||
|
||||
var document = Parser.ParseDocument(html);
|
||||
var map = new Dictionary<string, AdobeIndexEntry>(StringComparer.OrdinalIgnoreCase);
|
||||
var anchors = document.QuerySelectorAll("a[href]");
|
||||
|
||||
foreach (var anchor in anchors)
|
||||
{
|
||||
var href = anchor.GetAttribute("href");
|
||||
if (string.IsNullOrWhiteSpace(href))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!href.Contains("/security/products/", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!TryExtractAdvisoryId(anchor.TextContent, href, out var advisoryId))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!Uri.TryCreate(baseUri, href, out var detailUri))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var published = TryResolvePublished(anchor) ?? DateTimeOffset.UtcNow;
|
||||
var entry = new AdobeIndexEntry(advisoryId.ToUpperInvariant(), detailUri, published, anchor.TextContent?.Trim());
|
||||
map[entry.AdvisoryId] = entry;
|
||||
}
|
||||
|
||||
return map.Values
|
||||
.OrderBy(static e => e.PublishedUtc)
|
||||
.ThenBy(static e => e.AdvisoryId, StringComparer.OrdinalIgnoreCase)
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
private static bool TryExtractAdvisoryId(string? text, string href, out string advisoryId)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
var match = AdvisoryIdRegex.Match(text);
|
||||
if (match.Success)
|
||||
{
|
||||
advisoryId = match.Value.ToUpperInvariant();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
var hrefMatch = AdvisoryIdRegex.Match(href);
|
||||
if (hrefMatch.Success)
|
||||
{
|
||||
advisoryId = hrefMatch.Value.ToUpperInvariant();
|
||||
return true;
|
||||
}
|
||||
|
||||
advisoryId = string.Empty;
|
||||
return false;
|
||||
}
|
||||
|
||||
private static DateTimeOffset? TryResolvePublished(IElement anchor)
|
||||
{
|
||||
var row = anchor.Closest("tr");
|
||||
if (row is not null)
|
||||
{
|
||||
var cells = row.GetElementsByTagName("td");
|
||||
if (cells.Length >= 2)
|
||||
{
|
||||
for (var idx = 1; idx < cells.Length; idx++)
|
||||
{
|
||||
if (TryParseDate(cells[idx].TextContent, out var parsed))
|
||||
{
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var sibling = anchor.NextElementSibling;
|
||||
while (sibling is not null)
|
||||
{
|
||||
if (TryParseDate(sibling.TextContent, out var parsed))
|
||||
{
|
||||
return parsed;
|
||||
}
|
||||
|
||||
sibling = sibling.NextElementSibling;
|
||||
}
|
||||
|
||||
if (TryParseDate(anchor.ParentElement?.TextContent, out var parentDate))
|
||||
{
|
||||
return parentDate;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static bool TryParseDate(string? value, out DateTimeOffset result)
|
||||
{
|
||||
result = default;
|
||||
if (string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var trimmed = value.Trim();
|
||||
if (DateTimeOffset.TryParse(trimmed, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out result))
|
||||
{
|
||||
return Normalize(ref result);
|
||||
}
|
||||
|
||||
foreach (var format in ExplicitFormats)
|
||||
{
|
||||
if (DateTime.TryParseExact(trimmed, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date))
|
||||
{
|
||||
result = new DateTimeOffset(date, TimeSpan.Zero);
|
||||
return Normalize(ref result);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool Normalize(ref DateTimeOffset value)
|
||||
{
|
||||
value = value.ToUniversalTime();
|
||||
value = new DateTimeOffset(value.Year, value.Month, value.Day, 0, 0, 0, TimeSpan.Zero);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using Json.Schema;
|
||||
|
||||
namespace StellaOps.Concelier.Connector.Vndr.Adobe.Internal;
|
||||
|
||||
internal static class AdobeSchemaProvider
|
||||
{
|
||||
private static readonly Lazy<JsonSchema> Cached = new(Load, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
|
||||
public static JsonSchema Schema => Cached.Value;
|
||||
|
||||
private static JsonSchema Load()
|
||||
{
|
||||
var assembly = typeof(AdobeSchemaProvider).GetTypeInfo().Assembly;
|
||||
const string resourceName = "StellaOps.Concelier.Connector.Vndr.Adobe.Schemas.adobe-bulletin.schema.json";
|
||||
|
||||
using var stream = assembly.GetManifestResourceStream(resourceName)
|
||||
?? throw new InvalidOperationException($"Embedded schema '{resourceName}' not found.");
|
||||
using var reader = new StreamReader(stream);
|
||||
var schemaText = reader.ReadToEnd();
|
||||
return JsonSchema.FromText(schemaText);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user