Skip to content

Commit

Permalink
Merge pull request #4272 from cisagov/feature/CSET-2924
Browse files Browse the repository at this point in the history
Removal of DotNetZip and replaced with SharpZipLib- Feature/cset 2924
  • Loading branch information
randywoods authored Dec 3, 2024
2 parents 8df9571 + d1f007c commit 4113998
Show file tree
Hide file tree
Showing 9 changed files with 193 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
//
//
////////////////////////////////
using CSETWebCore.Business.AssessmentIO.Models.AutoGenerated;
using CSETWebCore.DataLayer.Model;
using CSETWebCore.Helpers;
using CSETWebCore.Model.AssessmentIO;
using Microsoft.EntityFrameworkCore;
using Nelibur.ObjectMapper;
using Newtonsoft.Json;
Expand All @@ -15,13 +18,7 @@
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using CSETWebCore.Model.AssessmentIO;
using CSETWebCore.Helpers;
using Ionic.Zip;
using Microsoft.IdentityModel.Tokens;
using CSETWebCore.Business.AssessmentIO.Models.AutoGenerated;
using System.Collections;
using System.Linq.Expressions;



//using CSETWebCore.Business.ImportAssessment.Models.Version_10_1;
Expand Down Expand Up @@ -49,7 +46,6 @@ public AssessmentExportManager(CSETContext context)

private void SetupBindings()
{

TinyMapper.Bind<ACCESS_KEY_ASSESSMENT, jACCESS_KEY_ASSESSMENT>();
TinyMapper.Bind<ADDRESS, jADDRESS>();
TinyMapper.Bind<ANSWER, jANSWER>();
Expand Down Expand Up @@ -390,7 +386,7 @@ join p in _context.PARAMETER_VALUES on an.Answer_Id equals p.Answer_Id
}

if (scrubData)
{
{
model = RemovePcii(model);
}
return model;
Expand All @@ -407,7 +403,7 @@ private UploadAssessmentModel RemovePcii(UploadAssessmentModel model)
item.Issue = null;
item.Impact = null;
item.Recommendations = null;
item.Vulnerabilities = null;
item.Vulnerabilities = null;
}

foreach (var item in model.jANSWER)
Expand Down Expand Up @@ -487,7 +483,7 @@ private UploadAssessmentModel RemovePcii(UploadAssessmentModel model)
{
item.PCII_Number = null;
}


return model;
}
Expand All @@ -499,16 +495,19 @@ private UploadAssessmentModel RemovePcii(UploadAssessmentModel model)
/// </summary>
private Stream ArchiveStream(int assessmentId, string password, string passwordHint, bool jsonOnly = false, bool scrubData = false)
{
var archiveStream = new MemoryStream();

var model = CopyForExport(assessmentId, scrubData);

using (var archive = new ZipFile())

var archiveStream = new MemoryStream();
using (var zipWrapper = new SharpZipLibWrapper(archiveStream))
{
if (password != null && password != "")
if (!string.IsNullOrEmpty(password))
{
archive.Password = password;
zipWrapper.Password = password;
}


foreach (var standard in model.jAVAILABLE_STANDARDS)
{
if (!standard.Selected)
Expand Down Expand Up @@ -551,7 +550,10 @@ join nqs in _context.NEW_QUESTION_SETS on nq.Question_Id equals nqs.Question_Id
//var standardEntry = archive.CreateEntry($"{setname}.json");
var jsonStandard = JsonConvert.SerializeObject(extStandard, Formatting.Indented);

ZipEntry standardEntry = archive.AddEntry($"{setname}.json", jsonStandard);
////////////////ZipEntry standardEntry = archive.Add$"{setname}.json", jsonStandard);

zipWrapper.AddEntry($"{setname}.json", jsonStandard);



//Set the GUID at time of export so we are sure it's right!!!
Expand All @@ -576,7 +578,7 @@ join nqs in _context.NEW_QUESTION_SETS on nq.Question_Id equals nqs.Question_Id
})).ToList();

model.CustomStandards.Add(extStandard.shortName);




Expand All @@ -594,14 +596,14 @@ join nqs in _context.NEW_QUESTION_SETS on nq.Question_Id equals nqs.Question_Id
var doc = genFile.ToExternalDocument();
var jsonDoc = JsonConvert.SerializeObject(doc, Formatting.Indented);

ZipEntry docEntry = archive.AddEntry($"{doc.ShortName}.json", jsonDoc);

zipWrapper.AddEntry($"{doc.ShortName}.json", jsonDoc);
model.CustomStandardDocs.Add(file.fileName);
}
}
}



model.ExportDateTime = DateTime.UtcNow;

var json = JsonConvert.SerializeObject(model, Formatting.Indented);
Expand All @@ -615,19 +617,20 @@ join nqs in _context.NEW_QUESTION_SETS on nq.Question_Id equals nqs.Question_Id
}
else
{
// Write the ZIP file with the JSON and any artifacts attached.
ZipEntry jsonEntry = archive.AddEntry("model.json", json);
ZipEntry hint = archive.AddEntry($"{passwordHint}.hint", passwordHint);
archive.Save(archiveStream);
zipWrapper.AddEntry("model.json", json);
zipWrapper.AddEntry($"{passwordHint}.hint", passwordHint);
}
}

zipWrapper.Save();
zipWrapper.CloseStream();
}

archiveStream.Seek(0, SeekOrigin.Begin);
return archiveStream;
}



/// <summary>
/// Export an assessment by its ID.
/// Can optionally provide a password and password hint that will be used during import process.
Expand Down Expand Up @@ -667,9 +670,6 @@ public AssessmentExportFile ExportAssessment(int assessmentId, string fileExtens
/// <returns></returns>
public MemoryStream BulkExportAssessments(Guid[] guids, string fileExtension)
{

var archiveStream = new MemoryStream();

var exportAssessments = _context.ASSESSMENTS.Where(a => guids.Contains(a.Assessment_GUID)).ToList();

// Assessments with provided guids do not exist.
Expand All @@ -679,7 +679,8 @@ public MemoryStream BulkExportAssessments(Guid[] guids, string fileExtension)
}

// Zip all the assessments into one archive.
using (var archive = new ZipFile())
var archiveStream = new MemoryStream();
using (var zipWrapper = new SharpZipLibWrapper(archiveStream))
{
// export the assessments
foreach (ASSESSMENTS a in exportAssessments)
Expand All @@ -688,7 +689,7 @@ public MemoryStream BulkExportAssessments(Guid[] guids, string fileExtension)
int duplicateCounter = 1;

// Handle collision cases where assessments have the same name, zip entries must be different.
while (archive.ContainsEntry(exportFile.FileName))
while (zipWrapper.ContainsEntry(exportFile.FileName))
{
if (duplicateCounter == 1)
{
Expand All @@ -702,10 +703,12 @@ public MemoryStream BulkExportAssessments(Guid[] guids, string fileExtension)
duplicateCounter++;
}

archive.AddEntry(exportFile.FileName, exportFile.FileContents);
}

archive.Save(archiveStream);
zipWrapper.AddEntry(exportFile.FileName, exportFile.FileContents);

zipWrapper.Save();
zipWrapper.CloseStream();
}
}

archiveStream.Seek(0, SeekOrigin.Begin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
//
//
////////////////////////////////
using CSETWebCore.Business.Diagram;
//using CSETWebCore.Business.ImportAssessment.Models.Version_10_1;
using CSETWebCore.Business.AssessmentIO.Models.AutoGenerated;
using CSETWebCore.Business.Diagram;
using CSETWebCore.DataLayer.Model;
using CSETWebCore.Helpers;
using CSETWebCore.Interfaces.Helpers;
using CSETWebCore.Model.AssessmentIO;
using CSETWebCore.Model.Diagram;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using System;
Expand All @@ -24,9 +22,7 @@
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using Ionic.Zip;
using System.Collections.Generic;
using Microsoft.AspNetCore.Http;
using ICSharpCode.SharpZipLib.Zip;

namespace CSETWebCore.Business.AssessmentIO.Import
{
Expand Down Expand Up @@ -63,18 +59,23 @@ public async Task ProcessCSETAssessmentImport(byte[] zipFileFromDatabase, int? c
using (Stream fs = new MemoryStream(zipFileFromDatabase))
{
MemoryStream ms = new MemoryStream();
ZipFile zip = ZipFile.Read(fs);
ZipEntry e = zip["model.json"];

if (password == "" || password == null)
var x = new ZipFile(fs);
ZipEntry e = x.GetEntry("model.json");



if (!String.IsNullOrEmpty(password))
{
e.Extract(ms);
x.Password = password;
}
else

using (var zipStream = x.GetInputStream(e))
{
e.ExtractWithPassword(ms, password);
zipStream.CopyTo(ms);
}


ms.Position = 0;
StreamReader sr = new StreamReader(ms);
string jsonObject = sr.ReadToEnd();
Expand Down Expand Up @@ -238,7 +239,11 @@ public async Task ProcessCSETAssessmentImport(byte[] zipFileFromDatabase, int? c
//NOTE THAT THIS ENTRY WILL ONLY COME FROM A OLD .cset file
//IMPORT
//ZipArchiveEntry importLegacyDiagram = zip.GetEntry("Diagram.csetd");
ZipEntry importLegacyDiagram = zip["Diagram.csetd"];
//ZipEntry importLegacyDiagram = zip["Diagram.csetd"];

ZipEntry importLegacyDiagram = x.GetEntry("Diagram.csetd");


if (importLegacyDiagram != null)
{
//StreamReader ldr = new StreamReader(importLegacyDiagram.Open());
Expand Down Expand Up @@ -266,56 +271,28 @@ public async Task BulkImportAssessments(Stream assessmentsZipArchive, bool overw
{
using (assessmentsZipArchive)
{
ZipFile zip = ZipFile.Read(assessmentsZipArchive);
// ZipFile zip = ZipFile.Read(assessmentsZipArchive);

var zip = new ZipFile(assessmentsZipArchive);

foreach (ZipEntry entry in zip)
{
using (MemoryStream stream = new MemoryStream())
{
entry.Extract(stream);
//entry.Extract(stream);

using (var zipStream = zip.GetInputStream(entry))
{
zipStream.CopyTo(stream);
}


await ProcessCSETAssessmentImport(stream.ToArray(), null, null, _context, overwriteAssessment: overwriteAssessments);
}
}
}
}

/// <summary>
///
/// </summary>
/// <param name="entry"></param>
/// <param name="doc"></param>
/*private void SaveFileToDB(ZipArchiveEntry entry, DOCUMENT_FILE doc)
{
var stream = entry.Open();
// determine the content type
var provider = new FileExtensionContentTypeProvider();
string contentType;
if (!provider.TryGetContentType(entry.FullName, out contentType))
{
contentType = "application/octet-stream";
}
string fileHash;
byte[] bytes;
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
bytes = ms.ToArray();
}
// Hash the file so that we can determine if it is already attached to another question
using (var md5 = MD5.Create())
{
var hash = md5.ComputeHash(bytes);
fileHash = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
doc.UpdatedTimestamp = DateTime.Now;
doc.ContentType = contentType;
doc.Name = entry.Name;
doc.Data = bytes;
}*/


/// <summary>
///
Expand Down
Loading

0 comments on commit 4113998

Please sign in to comment.