public DeltaFileTransaction CreateDelta(FileNode node) { var orderedLogEntries = node.LogEntries.OrderBy(x => x.CreatedAt); var deltaName = $"{Guid.NewGuid().ToString()}.delta"; var logEntry = orderedLogEntries.First(); var signature = _archiveManager.Read(logEntry.SignatureEntry, node); var delta = _deltaCreator.BuildDelta(signature, node); String newSignatureName = $"{Guid.NewGuid().ToString()}.signature"; Byte[] newSignature = _signatureCreator.CreateSignature(node); var newLogEntry = new LogEntry { CreatedAt = DateTime.Now, Type = LogEntryTypes.Normal, DiffName = deltaName, SignatureEntry = newSignatureName }; node.LogEntries.Add(newLogEntry); var transaction = new DeltaFileTransaction(node, new Signature(newSignatureName, newSignature), new Delta(deltaName, delta)); return(transaction); }
// Methods public (Byte[] Result, DeltaFileTransaction Transaction) Restore(FileNode node, LogEntry entry) { Byte[] result = null; Byte[] lastRead = null; var logEntries = node.LogEntries.Where(x => x.CreatedAt <= entry.CreatedAt); var strategy = _fileHandlingStrategySelector.Select(node); foreach (var current in logEntries) { switch (current.Type) { case LogEntryTypes.Genesis: result = _archiveManager.Read(node.FileName, node); break; case LogEntryTypes.Normal: var delta = _archiveManager.Read(current.DiffName, node); lastRead = result; result = _deltaPatcher.Patch(result, delta); break; default: break; } } var newSignature = _signatureCreator.CreateSignature(result); var newDelta = _deltaCreator.BuildDelta(newSignature, lastRead); var transaction = new DeltaFileTransaction(node, new Signature($"{Guid.NewGuid().ToString()}.signature", newSignature), new Delta($"{Guid.NewGuid().ToString()}.delta", newDelta)); return(result, transaction); }
public async Task Sign(HttpRequestMessage request, DateTimeOffset timeOfSigning, TimeSpan expires) { if (request == null) { throw new ArgumentNullException(nameof(request)); } try { if (timeOfSigning > _systemClock.UtcNow) { throw new HttpMessageSigningException("Cannot create a signature that is created in the future."); } if (timeOfSigning.Add(expires) < _systemClock.UtcNow) { throw new HttpMessageSigningException("Cannot create a signature that has already expired."); } var clonedSettings = (SigningSettings)_signingSettings.Clone(); var onRequestSigningTask = _signingSettings.Events?.OnRequestSigning?.Invoke(request, clonedSettings); if (onRequestSigningTask != null) { await onRequestSigningTask.ConfigureAwait(continueOnCapturedContext : false); } _signingSettingsSanitizer.SanitizeHeaderNamesToInclude(clonedSettings, request); clonedSettings.Validate(); await _signatureHeaderEnsurer.EnsureHeader(request, clonedSettings, timeOfSigning).ConfigureAwait(continueOnCapturedContext: false); var signature = await _signatureCreator.CreateSignature(request, clonedSettings, timeOfSigning, expires).ConfigureAwait(continueOnCapturedContext: false); var onSignatureCreatedTask = _signingSettings.Events?.OnSignatureCreated?.Invoke(request, signature, clonedSettings); if (onSignatureCreatedTask != null) { await onSignatureCreatedTask.ConfigureAwait(continueOnCapturedContext : false); } var authParam = _authorizationHeaderParamCreator.CreateParam(signature); _logger?.LogDebug("Setting Authorization scheme to '{0}' and param to '{1}'.", clonedSettings.AuthorizationScheme, authParam); request.Headers.Authorization = new AuthenticationHeaderValue(clonedSettings.AuthorizationScheme, authParam); var onRequestSignedTask = _signingSettings.Events?.OnRequestSigned?.Invoke(request, signature, clonedSettings); if (onRequestSignedTask != null) { await onRequestSignedTask.ConfigureAwait(continueOnCapturedContext : false); } } catch (Exception ex) { _logger?.LogError(ex, "Could not sign the specified request. See inner exception."); throw; } }
public override void Handle(FileNode node) { _archiveManager.AppendFile(node); var signatureName = $"{Guid.NewGuid().ToString()}.signature"; var logEntry = new LogEntry { CreatedAt = DateTime.Now, DiffName = String.Empty, Type = LogEntryTypes.Genesis, SignatureEntry = signatureName }; var signature = _signatureCreator.CreateSignature(node); //_archiveManager.Append(signatureName, node, signature); node.LogEntries.Add(logEntry); var transaction = new DeltaFileTransaction(node, new Signature(signatureName, signature), null); OnHandled(transaction); }
public DeltaFileTransaction CreateDelta(FileNode node) { var logEntry = node.LogEntries.First(x => x.Type == LogEntryTypes.Genesis); var original = _archiveManager.Read(node.FileName, node); var firstDelta = node.LogEntries.First(x => x.Type == LogEntryTypes.Normal); var delta = _archiveManager.Read(firstDelta.DiffName, node); var patched = _deltaPatcher.Patch(original, delta); foreach (var current in node.LogEntries.Where(x => x.Type == LogEntryTypes.Normal).Skip(1)) { var rollingDelta = _archiveManager.Read(current.DiffName, node); patched = _deltaPatcher.Patch(patched, rollingDelta); } var deltaName = $"{Guid.NewGuid().ToString()}.delta"; var mostRecentLogEntry = node.LogEntries.OrderBy(x => x.CreatedAt).Last(); var signature = _archiveManager.Read(logEntry.SignatureEntry, node); var newDelta = _deltaCreator.BuildDelta(signature, node); String newSignatureName = $"{Guid.NewGuid().ToString()}.signature"; Byte[] newSignature = _signatureCreator.CreateSignature(node); var newLogEntry = new LogEntry { CreatedAt = DateTime.Now, Type = LogEntryTypes.Normal, DiffName = deltaName, SignatureEntry = newSignatureName }; node.LogEntries.Add(newLogEntry); var transaction = new DeltaFileTransaction(node, new Signature(newSignatureName, newSignature), new Delta(deltaName, newDelta)); return(transaction); }
public async Task Sign(HttpRequestMessage request) { try { if (request == null) { throw new ArgumentNullException(nameof(request)); } var clonedSettings = (SigningSettings)_signingSettings.Clone(); var onRequestSigningTask = _signingSettings.Events?.OnRequestSigning?.Invoke(request, clonedSettings); if (onRequestSigningTask != null) { await onRequestSigningTask; } clonedSettings.Validate(); var timeOfSigning = _systemClock.UtcNow; await _dateHeaderEnsurer.EnsureHeader(request, clonedSettings, timeOfSigning); await _digestHeaderEnsurer.EnsureHeader(request, clonedSettings, timeOfSigning); var signature = await _signatureCreator.CreateSignature(request, clonedSettings, timeOfSigning); var authParam = _authorizationHeaderParamCreator.CreateParam(signature); _logger?.LogDebug("Setting Authorization scheme to '{0}' and param to '{1}'.", AuthorizationScheme, authParam); request.Headers.Authorization = new AuthenticationHeaderValue(AuthorizationScheme, authParam); var onRequestSignedTask = _signingSettings.Events?.OnRequestSigned?.Invoke(request, signature, clonedSettings); if (onRequestSignedTask != null) { await onRequestSignedTask; } } catch (Exception ex) { _logger?.LogError(ex, "Could not sign the specified request. See inner exception."); throw; } }