Refactor code structure for improved readability and maintainability; optimize performance in key functions.
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
using System.Collections.Immutable;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using StellaOps.AirGap.Bundle.Models;
|
||||
using StellaOps.AirGap.Bundle.Serialization;
|
||||
|
||||
namespace StellaOps.AirGap.Bundle.Services;
|
||||
|
||||
public sealed class BundleBuilder : IBundleBuilder
|
||||
{
|
||||
public async Task<BundleManifest> BuildAsync(
|
||||
BundleBuildRequest request,
|
||||
string outputPath,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
Directory.CreateDirectory(outputPath);
|
||||
|
||||
var feeds = new List<FeedComponent>();
|
||||
var policies = new List<PolicyComponent>();
|
||||
var cryptoMaterials = new List<CryptoComponent>();
|
||||
|
||||
foreach (var feedConfig in request.Feeds)
|
||||
{
|
||||
var component = await CopyComponentAsync(feedConfig, outputPath, ct).ConfigureAwait(false);
|
||||
feeds.Add(new FeedComponent(
|
||||
feedConfig.FeedId,
|
||||
feedConfig.Name,
|
||||
feedConfig.Version,
|
||||
component.RelativePath,
|
||||
component.Digest,
|
||||
component.SizeBytes,
|
||||
feedConfig.SnapshotAt,
|
||||
feedConfig.Format));
|
||||
}
|
||||
|
||||
foreach (var policyConfig in request.Policies)
|
||||
{
|
||||
var component = await CopyComponentAsync(policyConfig, outputPath, ct).ConfigureAwait(false);
|
||||
policies.Add(new PolicyComponent(
|
||||
policyConfig.PolicyId,
|
||||
policyConfig.Name,
|
||||
policyConfig.Version,
|
||||
component.RelativePath,
|
||||
component.Digest,
|
||||
component.SizeBytes,
|
||||
policyConfig.Type));
|
||||
}
|
||||
|
||||
foreach (var cryptoConfig in request.CryptoMaterials)
|
||||
{
|
||||
var component = await CopyComponentAsync(cryptoConfig, outputPath, ct).ConfigureAwait(false);
|
||||
cryptoMaterials.Add(new CryptoComponent(
|
||||
cryptoConfig.ComponentId,
|
||||
cryptoConfig.Name,
|
||||
component.RelativePath,
|
||||
component.Digest,
|
||||
component.SizeBytes,
|
||||
cryptoConfig.Type,
|
||||
cryptoConfig.ExpiresAt));
|
||||
}
|
||||
|
||||
var totalSize = feeds.Sum(f => f.SizeBytes) +
|
||||
policies.Sum(p => p.SizeBytes) +
|
||||
cryptoMaterials.Sum(c => c.SizeBytes);
|
||||
|
||||
var manifest = new BundleManifest
|
||||
{
|
||||
BundleId = Guid.NewGuid().ToString(),
|
||||
SchemaVersion = "1.0.0",
|
||||
Name = request.Name,
|
||||
Version = request.Version,
|
||||
CreatedAt = DateTimeOffset.UtcNow,
|
||||
ExpiresAt = request.ExpiresAt,
|
||||
Feeds = feeds.ToImmutableArray(),
|
||||
Policies = policies.ToImmutableArray(),
|
||||
CryptoMaterials = cryptoMaterials.ToImmutableArray(),
|
||||
TotalSizeBytes = totalSize
|
||||
};
|
||||
|
||||
return BundleManifestSerializer.WithDigest(manifest);
|
||||
}
|
||||
|
||||
private static async Task<CopiedComponent> CopyComponentAsync(
|
||||
BundleComponentSource source,
|
||||
string outputPath,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var targetPath = Path.Combine(outputPath, source.RelativePath);
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(targetPath) ?? outputPath);
|
||||
|
||||
await using var input = File.OpenRead(source.SourcePath);
|
||||
await using var output = File.Create(targetPath);
|
||||
await input.CopyToAsync(output, ct).ConfigureAwait(false);
|
||||
|
||||
await using var digestStream = File.OpenRead(targetPath);
|
||||
var hash = await SHA256.HashDataAsync(digestStream, ct).ConfigureAwait(false);
|
||||
var digest = Convert.ToHexString(hash).ToLowerInvariant();
|
||||
|
||||
var info = new FileInfo(targetPath);
|
||||
return new CopiedComponent(source.RelativePath, digest, info.Length);
|
||||
}
|
||||
|
||||
private sealed record CopiedComponent(string RelativePath, string Digest, long SizeBytes);
|
||||
}
|
||||
|
||||
public interface IBundleBuilder
|
||||
{
|
||||
Task<BundleManifest> BuildAsync(BundleBuildRequest request, string outputPath, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
public sealed record BundleBuildRequest(
|
||||
string Name,
|
||||
string Version,
|
||||
DateTimeOffset? ExpiresAt,
|
||||
IReadOnlyList<FeedBuildConfig> Feeds,
|
||||
IReadOnlyList<PolicyBuildConfig> Policies,
|
||||
IReadOnlyList<CryptoBuildConfig> CryptoMaterials);
|
||||
|
||||
public abstract record BundleComponentSource(string SourcePath, string RelativePath);
|
||||
|
||||
public sealed record FeedBuildConfig(
|
||||
string FeedId,
|
||||
string Name,
|
||||
string Version,
|
||||
string SourcePath,
|
||||
string RelativePath,
|
||||
DateTimeOffset SnapshotAt,
|
||||
FeedFormat Format)
|
||||
: BundleComponentSource(SourcePath, RelativePath);
|
||||
|
||||
public sealed record PolicyBuildConfig(
|
||||
string PolicyId,
|
||||
string Name,
|
||||
string Version,
|
||||
string SourcePath,
|
||||
string RelativePath,
|
||||
PolicyType Type)
|
||||
: BundleComponentSource(SourcePath, RelativePath);
|
||||
|
||||
public sealed record CryptoBuildConfig(
|
||||
string ComponentId,
|
||||
string Name,
|
||||
string SourcePath,
|
||||
string RelativePath,
|
||||
CryptoComponentType Type,
|
||||
DateTimeOffset? ExpiresAt)
|
||||
: BundleComponentSource(SourcePath, RelativePath);
|
||||
@@ -0,0 +1,79 @@
|
||||
using StellaOps.AirGap.Bundle.Models;
|
||||
using StellaOps.AirGap.Bundle.Serialization;
|
||||
using StellaOps.AirGap.Bundle.Validation;
|
||||
|
||||
namespace StellaOps.AirGap.Bundle.Services;
|
||||
|
||||
public sealed class BundleLoader : IBundleLoader
|
||||
{
|
||||
private readonly IBundleValidator _validator;
|
||||
private readonly IFeedRegistry _feedRegistry;
|
||||
private readonly IPolicyRegistry _policyRegistry;
|
||||
private readonly ICryptoProviderRegistry _cryptoRegistry;
|
||||
|
||||
public BundleLoader(
|
||||
IBundleValidator validator,
|
||||
IFeedRegistry feedRegistry,
|
||||
IPolicyRegistry policyRegistry,
|
||||
ICryptoProviderRegistry cryptoRegistry)
|
||||
{
|
||||
_validator = validator;
|
||||
_feedRegistry = feedRegistry;
|
||||
_policyRegistry = policyRegistry;
|
||||
_cryptoRegistry = cryptoRegistry;
|
||||
}
|
||||
|
||||
public async Task LoadAsync(string bundlePath, CancellationToken ct = default)
|
||||
{
|
||||
var manifestPath = Path.Combine(bundlePath, "manifest.json");
|
||||
if (!File.Exists(manifestPath))
|
||||
{
|
||||
throw new FileNotFoundException("Bundle manifest not found", manifestPath);
|
||||
}
|
||||
|
||||
var manifestJson = await File.ReadAllTextAsync(manifestPath, ct).ConfigureAwait(false);
|
||||
var manifest = BundleManifestSerializer.Deserialize(manifestJson);
|
||||
|
||||
var validationResult = await _validator.ValidateAsync(manifest, bundlePath, ct).ConfigureAwait(false);
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var details = string.Join("; ", validationResult.Errors.Select(e => e.Message));
|
||||
throw new InvalidOperationException($"Bundle validation failed: {details}");
|
||||
}
|
||||
|
||||
foreach (var feed in manifest.Feeds)
|
||||
{
|
||||
_feedRegistry.Register(feed, Path.Combine(bundlePath, feed.RelativePath));
|
||||
}
|
||||
|
||||
foreach (var policy in manifest.Policies)
|
||||
{
|
||||
_policyRegistry.Register(policy, Path.Combine(bundlePath, policy.RelativePath));
|
||||
}
|
||||
|
||||
foreach (var crypto in manifest.CryptoMaterials)
|
||||
{
|
||||
_cryptoRegistry.Register(crypto, Path.Combine(bundlePath, crypto.RelativePath));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IBundleLoader
|
||||
{
|
||||
Task LoadAsync(string bundlePath, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
public interface IFeedRegistry
|
||||
{
|
||||
void Register(FeedComponent component, string absolutePath);
|
||||
}
|
||||
|
||||
public interface IPolicyRegistry
|
||||
{
|
||||
void Register(PolicyComponent component, string absolutePath);
|
||||
}
|
||||
|
||||
public interface ICryptoProviderRegistry
|
||||
{
|
||||
void Register(CryptoComponent component, string absolutePath);
|
||||
}
|
||||
Reference in New Issue
Block a user