-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ROAD-1111] Encryption error workaround (#207)
- Loading branch information
Asaf Agami
authored
Aug 24, 2022
1 parent
300eea5
commit d50f65e
Showing
7 changed files
with
57 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,50 @@ | ||
namespace Snyk.Code.Library.Api.Encoding | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.IO.Compression; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Serilog; | ||
using Snyk.Common; | ||
|
||
/// <summary> | ||
/// Snyk Code API requests encoder. | ||
/// </summary> | ||
public class Encoder | ||
{ | ||
private static readonly ILogger Logger = LogManager.ForContext<Encoder>(); | ||
|
||
/// <summary> | ||
/// Encodes payload to base64 and encrypts using Gzip compression. | ||
/// </summary> | ||
/// <param name="payload">content payload to be encoded</param> | ||
/// <returns>Encoded stream</returns> | ||
public static async Task<MemoryStream> EncodeAndCompressAsync(string payload) | ||
{ | ||
var destinationPayload = new MemoryStream(); | ||
try | ||
{ | ||
var destinationPayload = new MemoryStream(); | ||
|
||
using (var originalPayload = new MemoryStream(Encoding.UTF8.GetBytes(payload))) | ||
using (var compressor = new GZipStream(destinationPayload, CompressionMode.Compress, true)) | ||
using (var base64Stream = new CryptoStream(compressor, new ToBase64Transform(), CryptoStreamMode.Write)) | ||
{ | ||
await Task.Run(() => originalPayload.CopyTo(base64Stream)); | ||
|
||
using (var originalPayload = new MemoryStream(Encoding.UTF8.GetBytes(payload))) | ||
using (var compressor = new GZipStream(destinationPayload, CompressionMode.Compress, true)) | ||
using (var base64Stream = new CryptoStream(compressor, new ToBase64Transform(), CryptoStreamMode.Write)) | ||
// Some rare errors about multiple async readers might be related to the next line. | ||
// Removing it for now and using the blocking copy wrapped in a Task.Run() | ||
// await originalPayload.CopyToAsync(base64Stream); | ||
} | ||
|
||
return destinationPayload; | ||
} | ||
catch (Exception exception) | ||
{ | ||
await originalPayload.CopyToAsync(base64Stream); | ||
Logger.Error(exception, "Error when trying to compress and encrypt payload"); | ||
throw; | ||
} | ||
|
||
return destinationPayload; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters