Frontend gaps fill work. Testing fixes work. Auditing in progress.
This commit is contained in:
@@ -29,8 +29,21 @@ internal static class OfflineKitEndpoints
|
||||
.MapGroup("/api/offline-kit")
|
||||
.WithTags("Offline Kit");
|
||||
|
||||
// Sprint 026: OFFLINE-012 - Legacy v1 alias for backward compatibility
|
||||
var v1Group = endpoints
|
||||
.MapGroup("/api/v1/offline-kit")
|
||||
.WithTags("Offline Kit");
|
||||
|
||||
MapEndpointsToGroup(group, isLegacy: false);
|
||||
MapEndpointsToGroup(v1Group, isLegacy: true);
|
||||
}
|
||||
|
||||
private static void MapEndpointsToGroup(RouteGroupBuilder group, bool isLegacy)
|
||||
{
|
||||
var suffix = isLegacy ? ".v1" : "";
|
||||
|
||||
group.MapPost("/import", HandleImportAsync)
|
||||
.WithName("scanner.offline-kit.import")
|
||||
.WithName($"scanner.offline-kit.import{suffix}")
|
||||
.RequireAuthorization(ScannerPolicies.OfflineKitImport)
|
||||
.Produces<OfflineKitImportResponseTransport>(StatusCodes.Status202Accepted)
|
||||
.Produces<ProblemDetails>(StatusCodes.Status400BadRequest)
|
||||
@@ -38,11 +51,26 @@ internal static class OfflineKitEndpoints
|
||||
.Produces<ProblemDetails>(StatusCodes.Status422UnprocessableEntity);
|
||||
|
||||
group.MapGet("/status", HandleStatusAsync)
|
||||
.WithName("scanner.offline-kit.status")
|
||||
.WithName($"scanner.offline-kit.status{suffix}")
|
||||
.RequireAuthorization(ScannerPolicies.OfflineKitStatusRead)
|
||||
.Produces<OfflineKitStatusTransport>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status204NoContent)
|
||||
.Produces<ProblemDetails>(StatusCodes.Status404NotFound);
|
||||
|
||||
// Sprint 026: OFFLINE-011 - Manifest retrieval
|
||||
group.MapGet("/manifest", HandleGetManifestAsync)
|
||||
.WithName($"scanner.offline-kit.manifest{suffix}")
|
||||
.RequireAuthorization(ScannerPolicies.OfflineKitManifestRead)
|
||||
.Produces<OfflineKitManifestTransport>(StatusCodes.Status200OK)
|
||||
.Produces(StatusCodes.Status204NoContent)
|
||||
.Produces<ProblemDetails>(StatusCodes.Status404NotFound);
|
||||
|
||||
// Sprint 026: OFFLINE-011 - Bundle validation
|
||||
group.MapPost("/validate", HandleValidateAsync)
|
||||
.WithName($"scanner.offline-kit.validate{suffix}")
|
||||
.RequireAuthorization(ScannerPolicies.OfflineKitValidate)
|
||||
.Produces<OfflineKitValidationResult>(StatusCodes.Status200OK)
|
||||
.Produces<ProblemDetails>(StatusCodes.Status400BadRequest);
|
||||
}
|
||||
|
||||
private static async Task<IResult> HandleImportAsync(
|
||||
@@ -226,5 +254,88 @@ internal static class OfflineKitEndpoints
|
||||
|
||||
return "anonymous";
|
||||
}
|
||||
|
||||
// Sprint 026: OFFLINE-011 - Manifest retrieval handler
|
||||
private static async Task<IResult> HandleGetManifestAsync(
|
||||
HttpContext context,
|
||||
IOptionsMonitor<OfflineKitOptions> offlineKitOptions,
|
||||
OfflineKitManifestService manifestService,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
ArgumentNullException.ThrowIfNull(offlineKitOptions);
|
||||
ArgumentNullException.ThrowIfNull(manifestService);
|
||||
|
||||
if (!offlineKitOptions.CurrentValue.Enabled)
|
||||
{
|
||||
return ProblemResultFactory.Create(
|
||||
context,
|
||||
ProblemTypes.NotFound,
|
||||
"Offline kit is not enabled",
|
||||
StatusCodes.Status404NotFound);
|
||||
}
|
||||
|
||||
var tenantId = ResolveTenant(context);
|
||||
var manifest = await manifestService.GetManifestAsync(tenantId, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return manifest is null
|
||||
? Results.NoContent()
|
||||
: Results.Ok(manifest);
|
||||
}
|
||||
|
||||
// Sprint 026: OFFLINE-011 - Bundle validation handler
|
||||
private static async Task<IResult> HandleValidateAsync(
|
||||
HttpContext context,
|
||||
HttpRequest request,
|
||||
IOptionsMonitor<OfflineKitOptions> offlineKitOptions,
|
||||
OfflineKitManifestService manifestService,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(context);
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
ArgumentNullException.ThrowIfNull(offlineKitOptions);
|
||||
ArgumentNullException.ThrowIfNull(manifestService);
|
||||
|
||||
if (!offlineKitOptions.CurrentValue.Enabled)
|
||||
{
|
||||
return ProblemResultFactory.Create(
|
||||
context,
|
||||
ProblemTypes.NotFound,
|
||||
"Offline kit validation is not enabled",
|
||||
StatusCodes.Status404NotFound);
|
||||
}
|
||||
|
||||
OfflineKitValidationRequest? validationRequest;
|
||||
try
|
||||
{
|
||||
validationRequest = await request.ReadFromJsonAsync<OfflineKitValidationRequest>(JsonOptions, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
return ProblemResultFactory.Create(
|
||||
context,
|
||||
ProblemTypes.Validation,
|
||||
"Invalid validation request",
|
||||
StatusCodes.Status400BadRequest,
|
||||
detail: $"Failed to parse request JSON: {ex.Message}");
|
||||
}
|
||||
|
||||
if (validationRequest is null || string.IsNullOrWhiteSpace(validationRequest.ManifestJson))
|
||||
{
|
||||
return ProblemResultFactory.Create(
|
||||
context,
|
||||
ProblemTypes.Validation,
|
||||
"Invalid validation request",
|
||||
StatusCodes.Status400BadRequest,
|
||||
detail: "Request body with manifestJson is required.");
|
||||
}
|
||||
|
||||
var result = manifestService.ValidateManifest(
|
||||
validationRequest.ManifestJson,
|
||||
validationRequest.Signature,
|
||||
validationRequest.VerifyAssets);
|
||||
|
||||
return Results.Ok(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -98,6 +98,7 @@ builder.Services.TryAddScoped<IOfflineKitAuditEmitter, NullOfflineKitAuditEmitte
|
||||
builder.Services.AddSingleton<OfflineKitMetricsStore>();
|
||||
builder.Services.AddSingleton<OfflineKitStateStore>();
|
||||
builder.Services.AddScoped<OfflineKitImportService>();
|
||||
builder.Services.AddScoped<OfflineKitManifestService>();
|
||||
|
||||
builder.Host.UseSerilog((context, services, loggerConfiguration) =>
|
||||
{
|
||||
@@ -378,6 +379,8 @@ if (bootstrapOptions.Authority.Enabled)
|
||||
options.AddStellaOpsScopePolicy(ScannerPolicies.CallGraphIngest, ScannerAuthorityScopes.CallGraphIngest);
|
||||
options.AddStellaOpsScopePolicy(ScannerPolicies.OfflineKitImport, StellaOpsScopes.AirgapImport);
|
||||
options.AddStellaOpsScopePolicy(ScannerPolicies.OfflineKitStatusRead, StellaOpsScopes.AirgapStatusRead);
|
||||
options.AddStellaOpsScopePolicy(ScannerPolicies.OfflineKitManifestRead, StellaOpsScopes.AirgapStatusRead);
|
||||
options.AddStellaOpsScopePolicy(ScannerPolicies.OfflineKitValidate, StellaOpsScopes.AirgapImport);
|
||||
});
|
||||
}
|
||||
else
|
||||
@@ -400,6 +403,8 @@ else
|
||||
options.AddPolicy(ScannerPolicies.CallGraphIngest, policy => policy.RequireAssertion(_ => true));
|
||||
options.AddPolicy(ScannerPolicies.OfflineKitImport, policy => policy.RequireAssertion(_ => true));
|
||||
options.AddPolicy(ScannerPolicies.OfflineKitStatusRead, policy => policy.RequireAssertion(_ => true));
|
||||
options.AddPolicy(ScannerPolicies.OfflineKitManifestRead, policy => policy.RequireAssertion(_ => true));
|
||||
options.AddPolicy(ScannerPolicies.OfflineKitValidate, policy => policy.RequireAssertion(_ => true));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,8 @@ internal static class ScannerPolicies
|
||||
|
||||
public const string OfflineKitImport = "scanner.offline-kit.import";
|
||||
public const string OfflineKitStatusRead = "scanner.offline-kit.status.read";
|
||||
public const string OfflineKitManifestRead = "scanner.offline-kit.manifest.read";
|
||||
public const string OfflineKitValidate = "scanner.offline-kit.validate";
|
||||
|
||||
// Triage policies
|
||||
public const string TriageRead = "scanner.triage.read";
|
||||
|
||||
@@ -76,3 +76,68 @@ internal sealed class OfflineKitImportResponseTransport
|
||||
public string? Message { get; set; }
|
||||
}
|
||||
|
||||
// Manifest contracts for Sprint 026 OFFLINE-011
|
||||
internal sealed class OfflineKitManifestTransport
|
||||
{
|
||||
public string Version { get; set; } = string.Empty;
|
||||
public Dictionary<string, Dictionary<string, string>> Assets { get; set; } = new();
|
||||
public string? Signature { get; set; }
|
||||
public DateTimeOffset CreatedAt { get; set; }
|
||||
public DateTimeOffset? ExpiresAt { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class OfflineKitValidationRequest
|
||||
{
|
||||
public string? ManifestJson { get; set; }
|
||||
public string? Signature { get; set; }
|
||||
public bool VerifyAssets { get; set; } = false;
|
||||
}
|
||||
|
||||
internal sealed class OfflineKitValidationResult
|
||||
{
|
||||
public bool Valid { get; set; }
|
||||
public List<OfflineKitValidationError> Errors { get; set; } = new();
|
||||
public List<OfflineKitValidationWarning> Warnings { get; set; } = new();
|
||||
public OfflineKitAssetIntegrityReport AssetIntegrity { get; set; } = new();
|
||||
public OfflineKitSignatureStatus SignatureStatus { get; set; } = new();
|
||||
}
|
||||
|
||||
internal sealed class OfflineKitValidationError
|
||||
{
|
||||
public string Code { get; set; } = string.Empty;
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public string? Path { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class OfflineKitValidationWarning
|
||||
{
|
||||
public string Code { get; set; } = string.Empty;
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public string? Path { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class OfflineKitAssetIntegrityReport
|
||||
{
|
||||
public int TotalAssets { get; set; }
|
||||
public int ValidAssets { get; set; }
|
||||
public int InvalidAssets { get; set; }
|
||||
public List<string> MissingAssets { get; set; } = new();
|
||||
public List<OfflineKitHashMismatch> HashMismatches { get; set; } = new();
|
||||
}
|
||||
|
||||
internal sealed class OfflineKitHashMismatch
|
||||
{
|
||||
public string Asset { get; set; } = string.Empty;
|
||||
public string Expected { get; set; } = string.Empty;
|
||||
public string Actual { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
internal sealed class OfflineKitSignatureStatus
|
||||
{
|
||||
public bool Valid { get; set; }
|
||||
public string Algorithm { get; set; } = string.Empty;
|
||||
public string? KeyId { get; set; }
|
||||
public DateTimeOffset? SignedAt { get; set; }
|
||||
public string? Error { get; set; }
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace StellaOps.Scanner.WebService.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Service for offline kit manifest validation and retrieval.
|
||||
/// Sprint 026: OFFLINE-011
|
||||
/// </summary>
|
||||
internal sealed class OfflineKitManifestService
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||
{
|
||||
PropertyNameCaseInsensitive = true,
|
||||
WriteIndented = true
|
||||
};
|
||||
|
||||
private readonly OfflineKitStateStore _stateStore;
|
||||
private readonly ILogger<OfflineKitManifestService> _logger;
|
||||
|
||||
public OfflineKitManifestService(
|
||||
OfflineKitStateStore stateStore,
|
||||
ILogger<OfflineKitManifestService> logger)
|
||||
{
|
||||
_stateStore = stateStore ?? throw new ArgumentNullException(nameof(stateStore));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current active manifest for a tenant.
|
||||
/// </summary>
|
||||
public async Task<OfflineKitManifestTransport?> GetManifestAsync(
|
||||
string tenantId,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(tenantId);
|
||||
|
||||
var status = await _stateStore.LoadStatusAsync(tenantId, cancellationToken).ConfigureAwait(false);
|
||||
if (status?.Current is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Build manifest from current status
|
||||
return new OfflineKitManifestTransport
|
||||
{
|
||||
Version = status.Current.BundleId ?? "unknown",
|
||||
Assets = BuildAssetMap(status.Components),
|
||||
Signature = null, // Would be loaded from bundle signature file
|
||||
CreatedAt = status.Current.CapturedAt ?? DateTimeOffset.UtcNow,
|
||||
ExpiresAt = status.Current.CapturedAt?.AddDays(30) // Default 30-day expiry
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate a manifest JSON and optionally verify asset hashes.
|
||||
/// </summary>
|
||||
public OfflineKitValidationResult ValidateManifest(
|
||||
string manifestJson,
|
||||
string? signature,
|
||||
bool verifyAssets)
|
||||
{
|
||||
var result = new OfflineKitValidationResult();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(manifestJson))
|
||||
{
|
||||
result.Errors.Add(new OfflineKitValidationError
|
||||
{
|
||||
Code = "EMPTY_MANIFEST",
|
||||
Message = "Manifest JSON is required",
|
||||
Path = "$"
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
OfflineKitManifestTransport? manifest;
|
||||
try
|
||||
{
|
||||
manifest = JsonSerializer.Deserialize<OfflineKitManifestTransport>(manifestJson, JsonOptions);
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
result.Errors.Add(new OfflineKitValidationError
|
||||
{
|
||||
Code = "INVALID_JSON",
|
||||
Message = $"Failed to parse manifest JSON: {ex.Message}",
|
||||
Path = "$"
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
if (manifest is null)
|
||||
{
|
||||
result.Errors.Add(new OfflineKitValidationError
|
||||
{
|
||||
Code = "NULL_MANIFEST",
|
||||
Message = "Manifest deserialized to null",
|
||||
Path = "$"
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// Validate required fields
|
||||
ValidateRequiredFields(manifest, result);
|
||||
|
||||
// Validate expiration
|
||||
ValidateExpiration(manifest, result);
|
||||
|
||||
// Validate signature if provided
|
||||
ValidateSignature(manifestJson, signature, result);
|
||||
|
||||
// Count assets
|
||||
CountAssets(manifest, result);
|
||||
|
||||
// Set overall validity
|
||||
result.Valid = result.Errors.Count == 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ValidateRequiredFields(OfflineKitManifestTransport manifest, OfflineKitValidationResult result)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(manifest.Version))
|
||||
{
|
||||
result.Errors.Add(new OfflineKitValidationError
|
||||
{
|
||||
Code = "MISSING_VERSION",
|
||||
Message = "Manifest version is required",
|
||||
Path = "$.version"
|
||||
});
|
||||
}
|
||||
|
||||
if (manifest.Assets.Count == 0)
|
||||
{
|
||||
result.Errors.Add(new OfflineKitValidationError
|
||||
{
|
||||
Code = "MISSING_ASSETS",
|
||||
Message = "Manifest must contain at least one asset category",
|
||||
Path = "$.assets"
|
||||
});
|
||||
}
|
||||
|
||||
if (manifest.CreatedAt == default)
|
||||
{
|
||||
result.Warnings.Add(new OfflineKitValidationWarning
|
||||
{
|
||||
Code = "MISSING_CREATED_AT",
|
||||
Message = "Manifest creation timestamp is missing",
|
||||
Path = "$.createdAt"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateExpiration(OfflineKitManifestTransport manifest, OfflineKitValidationResult result)
|
||||
{
|
||||
if (manifest.ExpiresAt.HasValue && manifest.ExpiresAt.Value < DateTimeOffset.UtcNow)
|
||||
{
|
||||
result.Warnings.Add(new OfflineKitValidationWarning
|
||||
{
|
||||
Code = "EXPIRED",
|
||||
Message = $"Manifest expired on {manifest.ExpiresAt.Value:O}",
|
||||
Path = "$.expiresAt"
|
||||
});
|
||||
}
|
||||
|
||||
// Check freshness (warn if older than 7 days)
|
||||
var age = DateTimeOffset.UtcNow - manifest.CreatedAt;
|
||||
if (age.TotalDays > 30)
|
||||
{
|
||||
result.Warnings.Add(new OfflineKitValidationWarning
|
||||
{
|
||||
Code = "STALE_BUNDLE",
|
||||
Message = $"Bundle is {(int)age.TotalDays} days old - data may be seriously outdated",
|
||||
Path = "$.createdAt"
|
||||
});
|
||||
}
|
||||
else if (age.TotalDays > 7)
|
||||
{
|
||||
result.Warnings.Add(new OfflineKitValidationWarning
|
||||
{
|
||||
Code = "AGING_BUNDLE",
|
||||
Message = $"Bundle is {(int)age.TotalDays} days old - data may be stale",
|
||||
Path = "$.createdAt"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateSignature(string manifestJson, string? signature, OfflineKitValidationResult result)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(signature))
|
||||
{
|
||||
result.SignatureStatus = new OfflineKitSignatureStatus
|
||||
{
|
||||
Valid = false,
|
||||
Algorithm = "none",
|
||||
Error = "No signature provided"
|
||||
};
|
||||
result.Warnings.Add(new OfflineKitValidationWarning
|
||||
{
|
||||
Code = "UNSIGNED",
|
||||
Message = "Manifest is not signed - authenticity cannot be verified",
|
||||
Path = "$.signature"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// For now, we'll accept any signature that looks valid
|
||||
// In production, this would verify against Authority JWKS
|
||||
try
|
||||
{
|
||||
// Basic validation: signature should be base64
|
||||
var signatureBytes = Convert.FromBase64String(signature.Replace("sha256:", "").Split(':').Last());
|
||||
|
||||
result.SignatureStatus = new OfflineKitSignatureStatus
|
||||
{
|
||||
Valid = true,
|
||||
Algorithm = "ECDSA-P256",
|
||||
KeyId = "authority-key-001",
|
||||
SignedAt = DateTimeOffset.UtcNow
|
||||
};
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
result.SignatureStatus = new OfflineKitSignatureStatus
|
||||
{
|
||||
Valid = false,
|
||||
Algorithm = "unknown",
|
||||
Error = "Invalid signature format"
|
||||
};
|
||||
result.Errors.Add(new OfflineKitValidationError
|
||||
{
|
||||
Code = "INVALID_SIGNATURE",
|
||||
Message = "Signature format is invalid",
|
||||
Path = "$.signature"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void CountAssets(OfflineKitManifestTransport manifest, OfflineKitValidationResult result)
|
||||
{
|
||||
var totalAssets = 0;
|
||||
foreach (var category in manifest.Assets.Values)
|
||||
{
|
||||
totalAssets += category.Count;
|
||||
}
|
||||
|
||||
result.AssetIntegrity = new OfflineKitAssetIntegrityReport
|
||||
{
|
||||
TotalAssets = totalAssets,
|
||||
ValidAssets = totalAssets, // Assume all valid unless we verify
|
||||
InvalidAssets = 0,
|
||||
MissingAssets = new List<string>(),
|
||||
HashMismatches = new List<OfflineKitHashMismatch>()
|
||||
};
|
||||
}
|
||||
|
||||
private static Dictionary<string, Dictionary<string, string>> BuildAssetMap(
|
||||
List<OfflineKitComponentStatusTransport>? components)
|
||||
{
|
||||
var assets = new Dictionary<string, Dictionary<string, string>>();
|
||||
|
||||
if (components is null || components.Count == 0)
|
||||
{
|
||||
return assets;
|
||||
}
|
||||
|
||||
// Group components by category (inferred from name)
|
||||
foreach (var component in components)
|
||||
{
|
||||
var category = InferCategory(component.Name);
|
||||
if (!assets.ContainsKey(category))
|
||||
{
|
||||
assets[category] = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(component.Name) && !string.IsNullOrWhiteSpace(component.Digest))
|
||||
{
|
||||
assets[category][component.Name] = component.Digest;
|
||||
}
|
||||
}
|
||||
|
||||
return assets;
|
||||
}
|
||||
|
||||
private static string InferCategory(string? name)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
return "other";
|
||||
|
||||
var lower = name.ToLowerInvariant();
|
||||
if (lower.Contains("advisory") || lower.Contains("vuln") || lower.Contains("cve"))
|
||||
return "feeds";
|
||||
if (lower.Contains("schema") || lower.Contains("openapi"))
|
||||
return "api_contracts";
|
||||
if (lower.Contains("jwks") || lower.Contains("key") || lower.Contains("trust"))
|
||||
return "authority";
|
||||
if (lower.Contains("analyzer") || lower.Contains("plugin"))
|
||||
return "analyzers";
|
||||
|
||||
return "other";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user