Skip to content

Commit

Permalink
Missing Code Review parts from SharpZipLib implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander van Delft committed Oct 23, 2024
1 parent 8eebf87 commit a50a889
Show file tree
Hide file tree
Showing 15 changed files with 96 additions and 53 deletions.
8 changes: 7 additions & 1 deletion CDP4Common/CDP4Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[ADD] SharpZipLibUtils
[ADD] SharpZipLibExtension Methods
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Encryption\**" />
<EmbeddedResource Remove="Encryption\**" />
<None Remove="Encryption\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
</ItemGroup>
Expand Down
51 changes: 51 additions & 0 deletions CDP4Common/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="StreamExtensions.cs" company="Starion Group S.A.">
// Copyright (c) 2015-2024 Starion Group S.A.
//
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary, Jaime Bernar
//
// This file is part of CDP4-COMET SDK Community Edition
//
// The CDP4-COMET SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-COMET SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// -------------------------------------------------------------------------------------------------------------------------------

namespace CDP4Common.Extensions
{
using System.IO;

using ICSharpCode.SharpZipLib.Zip;

/// <summary>
/// A class containing extension methods for <see cref="Stream"/> objects
/// </summary>
public static class StreamExtensions
{
/// <summary>
/// Create a <see cref="ZipOutputStream"/> to create an encrypted zip file
/// </summary>
/// <param name="inputStream">The input <see cref="Stream"/> to write to</param>
/// <param name="password">The password to be used to protect the data</param>
/// <returns>The created <see cref="ZipOutputStream"/></returns>
public static ZipOutputStream CreateEncryptableZipOutputStream(this Stream inputStream, string password)
{
var zipStream = new ZipOutputStream(inputStream);
zipStream.Password = password;
zipStream.IsStreamOwner = true; // underlying streams will be forcibly closed

return zipStream;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="SharpZipLibHelper.cs" company="Starion Group S.A.">
// <copyright file="ZipOutputStreamExtensions.cs" company="Starion Group S.A.">
// Copyright (c) 2015-2024 Starion Group S.A.
//
// Author: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary, Jaime Bernar
Expand All @@ -22,39 +22,29 @@
// </copyright>
// -------------------------------------------------------------------------------------------------------------------------------

namespace CDP4Common.Encryption
namespace CDP4Common.Extensions
{
using System.IO;

using ICSharpCode.SharpZipLib.Zip;

/// <summary>
/// A class containing Helper Methods for working with SharpZipLib and creating AES 256 encrypted zip files
/// A class containing Extension Methods for working with SharpZipLib and creating AES 256 encrypted zip files
/// </summary>
public static class SharpZipLibUtils
public static class ZipOutputStreamExtensions
{
/// <summary>
/// Create a <see cref="ZipOutputStream"/> to create a AES 256 encrypted zip file
/// The AESKeySize setting for new <see cref="ZipEntry"/>s
/// </summary>
/// <param name="inputStream">The input <see cref="Stream"/> to write to</param>
/// <param name="password">The password to be used to protect the data</param>
/// <returns>The created <see cref="ZipOutputStream"/></returns>
public static ZipOutputStream CreateZipOutputStream(Stream inputStream, string password)
{
var zipStream = new ZipOutputStream(inputStream);
zipStream.Password = password;
zipStream.IsStreamOwner = true; // underlying streams will be forcibly closed

return zipStream;
}
private const int AESKeySize = 256;

/// <summary>
/// Add a <see cref="ZipEntry"/> and add it to a <see cref="ZipOutputStream"/>
/// </summary>
/// <param name="zipOutputStream">The <see cref="ZipOutputStream"/></param>
/// <param name="stream">The input <see cref="Stream"/> from which to create the <see cref="ZipEntry"/></param>
/// <param name="name">The name of the entry, including (sub)folder information</param>
public static void AddEntryFromStream(ZipOutputStream zipOutputStream, Stream stream, string name)
public static void AddEntryFromStream(this ZipOutputStream zipOutputStream, Stream stream, string name)
{
if (stream.Length == 0)
{
Expand All @@ -63,7 +53,7 @@ public static void AddEntryFromStream(ZipOutputStream zipOutputStream, Stream st

var entry = new ZipEntry(name)
{
AESKeySize = 256, // Set AES encryption to 256 bits for each individual entry
AESKeySize = AESKeySize, // Set AES encryption for each individual entry
};

zipOutputStream.PutNextEntry(entry);
Expand All @@ -79,11 +69,11 @@ public static void AddEntryFromStream(ZipOutputStream zipOutputStream, Stream st
/// <param name="zipOutputStream">The <see cref="ZipOutputStream"/></param>
/// <param name="extraFile">The location of the file to add</param>
/// <param name="entryLocation">The location (fullname) of the file to create in the <see cref="ZipOutputStream"/>, including (sub)folder information</param>
public static void AddEntryFromFile(ZipOutputStream zipOutputStream, string extraFile, string entryLocation)
public static void AddEntryFromFile(this ZipOutputStream zipOutputStream, string extraFile, string entryLocation)
{
var entry = new ZipEntry(entryLocation)
{
AESKeySize = 256, // Set AES encryption to 256 bits
AESKeySize = AESKeySize, // Set AES encryption for each individual entry
};

zipOutputStream.PutNextEntry(entry);
Expand Down
2 changes: 1 addition & 1 deletion CDP4Dal/CDP4Dal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion CDP4DalCommon/CDP4DalCommon.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
36 changes: 16 additions & 20 deletions CDP4JsonFileDal/JsonFileDal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace CDP4JsonFileDal

using CDP4Common.CommonData;
using CDP4Common.Comparers;
using CDP4Common.Encryption;
using CDP4Common.EngineeringModelData;
using CDP4Common.Exceptions;
using CDP4Common.Extensions;
Expand All @@ -56,7 +55,10 @@ namespace CDP4JsonFileDal

using NLog;

using Alias = CDP4Common.DTO.Alias;
using Definition = CDP4Common.DTO.Definition;
using File = System.IO.File;
using HyperLink = CDP4Common.DTO.HyperLink;
using Person = CDP4Common.SiteDirectoryData.Person;
using SiteDirectory = CDP4Common.SiteDirectoryData.SiteDirectory;
using Thing = CDP4Common.DTO.Thing;
Expand Down Expand Up @@ -311,7 +313,7 @@ public override Task<IEnumerable<Thing>> Write(IEnumerable<OperationContainer> o
{
var password = this.Session.Credentials.Password;

using (var zipArchive = SharpZipLibUtils.CreateZipOutputStream(file, password))
using (var zipArchive = file.CreateEncryptableZipOutputStream(password))
{
this.WriteHeaderToZipFile(exchangeFileHeader, zipArchive);

Expand Down Expand Up @@ -499,7 +501,7 @@ public override async Task<IEnumerable<Thing>> Read<T>(T thing, CancellationToke
}

// make sure that the uri is of the correct format
UriExtensions.AssertUriIsFileSchema(this.Credentials.Uri);
this.Credentials.Uri.AssertUriIsFileSchema();

var filePath = this.Credentials.Uri.LocalPath;

Expand Down Expand Up @@ -657,9 +659,7 @@ private List<Thing> RetrieveDomainOfExpertiseThings(CDP4Common.DTO.DomainOfExper

foreach (var refThing in domain.Alias.ToList())
{
var thingDto = siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) as CDP4Common.DTO.Alias;

if (thingDto != null)
if (siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) is Alias thingDto)
{
thingDto.ExcludedPerson.Clear();
thingDto.ExcludedDomain.Clear();
Expand All @@ -673,9 +673,7 @@ private List<Thing> RetrieveDomainOfExpertiseThings(CDP4Common.DTO.DomainOfExper

foreach (var refThing in domain.HyperLink.ToList())
{
var thingDto = siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) as CDP4Common.DTO.HyperLink;

if (thingDto != null)
if (siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) is HyperLink thingDto)
{
thingDto.ExcludedPerson.Clear();
thingDto.ExcludedDomain.Clear();
Expand All @@ -692,9 +690,7 @@ private List<Thing> RetrieveDomainOfExpertiseThings(CDP4Common.DTO.DomainOfExper

foreach (var refThing in domain.Definition.ToList())
{
var thingDto = siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) as CDP4Common.DTO.Definition;

if (thingDto != null)
if (siteDirectoryData.FirstOrDefault(s => s.Iid.Equals(refThing)) is Definition thingDto)
{
thingDto.ExcludedDomain.Clear();
thingDto.ExcludedPerson.Clear();
Expand Down Expand Up @@ -903,7 +899,7 @@ public override async Task<IEnumerable<Thing>> Open(Credentials credentials, Can
}

// make sure that the uri is of the correct format
UriExtensions.AssertUriIsFileSchema(credentials.Uri);
credentials.Uri.AssertUriIsFileSchema();

var filePath = credentials.Uri.LocalPath;

Expand Down Expand Up @@ -1055,7 +1051,7 @@ private void WriteHeaderToZipFile(ExchangeFileHeader echExchangeFileHeader, ZipO
using (var memoryStream = new MemoryStream())
{
this.Serializer.SerializeToStream(echExchangeFileHeader, memoryStream);
SharpZipLibUtils.AddEntryFromStream(zipArchive, memoryStream, "Header.json");
zipArchive.AddEntryFromStream(memoryStream, "Header.json");
}
}

Expand All @@ -1076,7 +1072,7 @@ private void WriteSiteDirectoryToZipFile(IEnumerable<Thing> prunedSiteDirectoryC

this.Serializer.SerializeToStream(orderedContents, memoryStream);

SharpZipLibUtils.AddEntryFromStream(zipOutputStream, memoryStream, "SiteDirectory.json");
zipOutputStream.AddEntryFromStream(memoryStream, "SiteDirectory.json");
}
}

Expand Down Expand Up @@ -1129,7 +1125,7 @@ private void WriteSiteReferenceDataLibraryToZipFile(Dictionary<SiteReferenceData
{
this.Serializer.SerializeToStream(siteReferenceDataLibrary.Value, memoryStream);

SharpZipLibUtils.AddEntryFromStream(zipOutputStream, memoryStream, siteReferenceDataLibraryFilename);
zipOutputStream.AddEntryFromStream(memoryStream, siteReferenceDataLibraryFilename);
}
}
}
Expand Down Expand Up @@ -1183,7 +1179,7 @@ private void WriteModelReferenceDataLibraryToZipFile(Dictionary<ModelReferenceDa
using (var memoryStream = new MemoryStream())
{
this.Serializer.SerializeToStream(modelReferenceDataLibrary.Value, memoryStream);
SharpZipLibUtils.AddEntryFromStream(zipOutputStream, memoryStream, modelReferenceDataLibraryFilename);
zipOutputStream.AddEntryFromStream(memoryStream, modelReferenceDataLibraryFilename);
}
}
}
Expand Down Expand Up @@ -1240,7 +1236,7 @@ private void WriteIterationsToZipFile(Dictionary<Iteration, IEnumerable<Thing>>
using (var memoryStream = new MemoryStream())
{
this.Serializer.SerializeToStream(new[] { engineeringModelDto }, memoryStream);
SharpZipLibUtils.AddEntryFromStream(zipOutputStream, memoryStream, engineeringModelFilename);
zipOutputStream.AddEntryFromStream(memoryStream, engineeringModelFilename);
}

engineeringModels.Add(engineeringModel);
Expand All @@ -1252,7 +1248,7 @@ private void WriteIterationsToZipFile(Dictionary<Iteration, IEnumerable<Thing>>
{
this.Serializer.SerializeToStream(iteration.Value, memoryStream);

SharpZipLibUtils.AddEntryFromStream(zipOutputStream, memoryStream, iterationFilename);
zipOutputStream.AddEntryFromStream(memoryStream, iterationFilename);
}
}
}
Expand All @@ -1274,7 +1270,7 @@ private void WriteExtensionFilesToZipFile(IEnumerable<string> extraFilesPath, Zi
var extraFileName = Path.GetFileName(extraFile);
var entryLocation = Path.Combine(ExtensionsZipLocation, extraFileName);

SharpZipLibUtils.AddEntryFromFile(zipOutputStream, extraFile, entryLocation);
zipOutputStream.AddEntryFromFile(extraFile, entryLocation);
}
}

Expand Down
2 changes: 1 addition & 1 deletion CDP4JsonSerializer/CDP4JsonSerializer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25 JSON</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion CDP4MessagePackSerializer/CDP4MessagePackSerializer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25 MessagePack</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
[Update] To MessagePack 2.5.187
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion CDP4Reporting/CDP4Reporting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<LangVersion>latest</LangVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion CDP4Rules/CDP4Rules.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion CDP4ServicesDal/CDP4ServicesDal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion CDP4ServicesMessaging/CDP4ServicesMessaging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
<LangVersion>latest</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion CDP4Web/CDP4Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
[Update] to Microsoft.Extensions.Logging.Abstractions 8.0.2
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion CDP4WspDal/CDP4WspDal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageTags>CDP COMET ECSS-E-TM-10-25</PackageTags>
<PackageLicenseExpression>LGPL-3.0-only</PackageLicenseExpression>
<PackageReleaseNotes>
[BUMP] To CDP4Common 27.3.5
[BUMP] To CDP4Common 27.4.0
</PackageReleaseNotes>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>
Expand Down

0 comments on commit a50a889

Please sign in to comment.