Skip to content

Commit

Permalink
Export encryption package (#231)
Browse files Browse the repository at this point in the history
* make Drive and ZipService use a consistent interface

* Rename file to match class name

* Add constants for filenames

* Allow for multiple writes

* update constants and IStorageService

* implicit conversion for records to string

* export encryption

* Storage extract

* Update Election

* Bump actions/setup-node from 2 to 3 (#226)

* Moved to the CiphertextBallot and removed the deprecated one (#228)

Fixed the order of the number of guardians and quorum for creating a new key ceremony

* remove uneeded files

* export election package

* resolve Linter issues

* Cleanup

* UtcNow not now

* Unneeded using

* Linter updates

* constructor update

* mark step 1 complete

* AddBallots Button enabling

* set DateTime on export

* Mark election as exported

* naming

---------

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Steve Maier <82616727+SteveMaier-IRT@users.noreply.github.com>
  • Loading branch information
3 people committed May 8, 2023
1 parent d632cbb commit 3b75787
Show file tree
Hide file tree
Showing 23 changed files with 463 additions and 402 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ElectionGuard.Encryption.Utils.Converters;
using ElectionGuard.Encryption.Utils.Converters;
using ElectionGuard.UI.Lib.Models;
using ElectionGuard.UI.Lib.Services;
using Newtonsoft.Json;
Expand Down Expand Up @@ -79,6 +79,7 @@ public static void Save(this Guardian self, string keyCeremonyId)
var basePath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var filePath = Path.Combine(basePath, PrivateKeyFolder, keyCeremonyId);

storage.ToFile(filePath, filename, dataJson);
storage.UpdatePath(filePath);
storage.ToFile(filename, dataJson);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public ConstantsRecord() : base(nameof(ConstantsRecord))
{
}

public override string ToString() => ConstantsData ?? string.Empty;
public static implicit operator string(ConstantsRecord record) => record.ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public ContextRecord() : base(nameof(ContextRecord))
{
}

public override string ToString() => ContextData ?? string.Empty;
public static implicit operator string(ContextRecord record) => record.ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public ManifestRecord(string electionId, string manifestData) : base(nameof(Mani
public ManifestRecord() : base(nameof(ManifestRecord))
{
}

public override string ToString() => ManifestData ?? string.Empty;
public static implicit operator string(ManifestRecord record) => record.ToString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task<bool> ElectionNameExists(string electionName)
/// </summary>
/// <param name="electionId">election id to use</param>
/// <param name="date">date to save</param>
virtual public async Task UpdateExportDateAsync(string electionId, DateTime date)
virtual public async Task UpdateEncryptionExportDateAsync(string electionId, DateTime date)
{
var filterBuilder = Builders<Election>.Filter;
var filter = filterBuilder.And(filterBuilder.Eq(Constants.ElectionId, electionId));
Expand All @@ -52,5 +52,4 @@ virtual public async Task UpdateExportDateAsync(string electionId, DateTime date

await UpdateAsync(filter, update);
}

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
namespace ElectionGuard.UI.Lib.Services;
using System.IO.Compression;
using ElectionGuard.UI.Lib.Models;

namespace ElectionGuard.UI.Lib.Services;

/// <summary>
/// Class to read and write file to the hard drive
/// </summary>
public class DriveService : IStorageService
{
/// <summary>
/// Read in the contents of a file
/// </summary>
/// <param name="filename">filename including path</param>
/// <returns>the string data from the file</returns>
public string FromFile(string filename)
private string _rootDirectoryPath;

public DriveService(string rootDirectoryPath)
{
_rootDirectoryPath = rootDirectoryPath;
}

public DriveService() : this(Path.GetTempPath())
{
}

public void ToFile(string fileName, string content)
{
var filePath = Path.Combine(_rootDirectoryPath, fileName);
File.WriteAllText(filePath, content);
}

public void ToFiles(List<FileContents> fileContents)
{
return File.ReadAllText(filename);
Parallel.ForEach(fileContents, fileContent =>
{
ToFile(fileContent.FileName, fileContent.Contents);
});
}

/// <summary>
/// Write a string to a file on a drive
/// </summary>
/// <param name="path">folder where the file will be created</param>
/// <param name="filename">name of the file</param>
/// <param name="data">data to save into file</param>
public void ToFile(string path, string filename, string data)
public string FromFile(string fileName)
{
Directory.CreateDirectory(path);
var name = Path.Combine(path, filename);
File.WriteAllText(name, data);
if (string.IsNullOrEmpty(fileName)) throw new ArgumentNullException(nameof(fileName));

var filePath = Path.Combine(_rootDirectoryPath, fileName);

if (!File.Exists(filePath)) throw new FileNotFoundException(nameof(filePath));

return File.ReadAllText(filePath);
}

public void UpdatePath(string path)
{
if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path));

if (! Directory.Exists(path))
{
// create directory
Directory.CreateDirectory(path);

_rootDirectoryPath = path;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,58 @@
using ElectionGuard.UI.Lib.Models;

namespace ElectionGuard.UI.Lib.Services;

/// <summary>
/// Service to allow simple creation and managing of zip files
/// </summary>
public static class ZipService
public class ZipStorageService : IStorageService
{
/// <summary>
/// Creates a zip file if it does not exist
/// </summary>
/// <param name="zipFileName">file name and path where to make the zip file</param>
private static void CreateZip(string zipFileName)
private string _zipFile;

private ZipArchiveMode ZipMode => File.Exists(_zipFile) ? ZipArchiveMode.Update : ZipArchiveMode.Create;

public ZipStorageService(string zipFile)
{
if (!File.Exists(zipFileName))
UpdatePath(zipFile);
}

public ZipStorageService() : this(Path.GetTempFileName()) { }

public void ToFile(string fileName, string content)
{
if (string.IsNullOrEmpty(fileName))
{
// Create the empty zip file
ZipFile.CreateFromDirectory(".", zipFileName, CompressionLevel.Optimal, false);
throw new ArgumentNullException(nameof(fileName));
}

using var zipArchive = ZipFile.Open(_zipFile, ZipMode);

var entry = zipArchive.CreateEntry(fileName);
using var stream = entry.Open();
using var writer = new StreamWriter(stream);

writer.Write(content);
}

/// <summary>
/// Adds a file to the given zip file. If the zip file does not exist, it creates it first
/// </summary>
/// <param name="zipFileName">filename and path of the zip file to use / make</param>
/// <param name="fileList">list of file data to add to the zip file</param>
public static void AddFiles(string zipFileName, List<FileContents> fileList)
public void ToFiles(List<FileContents> fileContents)
{
CreateZip(zipFileName);
using var zipArchive = ZipFile.Open(_zipFile, ZipMode);
_ = Parallel.ForEach(fileContents, fileContent =>
{
var entry = zipArchive.CreateEntry(fileContent.FileName);
using var stream = entry.Open();
using var writer = new StreamWriter(stream);
writer.Write(fileContent.Contents);
});
}

using var archive = ZipFile.Open(zipFileName, ZipArchiveMode.Update);
public string FromFile(string fileName)
{
using var zipArchive = ZipFile.OpenRead(_zipFile);
var entry = zipArchive.GetEntry(fileName) ?? throw new FileNotFoundException(nameof(fileName));
using var stream = entry.Open();
using var reader = new StreamReader(stream);
return reader.ReadToEnd();
}

foreach (var file in fileList)
{
// Create a new entry for the file in the zip file
var entry = archive.CreateEntry(file.FileName.Replace('\\', '/'), CompressionLevel.Optimal);
using var writer = new StreamWriter(entry.Open());
writer.Write(file.Contents);
}
public void UpdatePath(string zipFile)
{
_zipFile = zipFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ElectionGuard.UI.Lib.Models;

namespace ElectionGuard.UI.Lib.Services
{
Expand All @@ -17,14 +18,30 @@ public interface IStorageService
/// <param name="path">folder where the file will be created</param>
/// <param name="filename">name of the file</param>
/// <param name="data">data to save into file</param>
public void ToFile(string path, string filename, string data);
public void ToFile(string filename, string content);

/// <summary>
/// Write multiple files to a drive
/// </summary>
/// <param name="contents"></param>
public void ToFiles(List<FileContents> files);

/// <summary>
/// Read in the contents of a file
/// </summary>
/// <param name="filename">filename including path</param>
/// <returns>the string data from the file</returns>
/// <exception cref="FileNotFoundException">File does not exist</exception>
/// <exception cref="ArgumentNullException">filename not provided</exception>
public string FromFile(string filename);

/// <summary>
/// Update the path for a file
/// </summary>
/// <param name="path">new path</param>
/// <exception cref="ArgumentNullException">Path is null</exception>
/// <exception cref="ArgumentException">Path does not exist</exception>
public void UpdatePath(string path);
}

/// <summary>
Expand Down
3 changes: 0 additions & 3 deletions src/electionguard-ui/ElectionGuard.UI/ElectionGuard.UI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,6 @@
<AutoGen>True</AutoGen>
<DependentUpon>AppResources.resx</DependentUpon>
</Compile>
<Compile Update="Views\EncryptionExportPage.xaml.cs">
<DependentUpon>EncryptionExportPage.xaml</DependentUpon>
</Compile>
<Compile Update="Views\CreateElectionAdminPage.xaml.cs">
<DependentUpon>CreateElectionAdminPage.xaml</DependentUpon>
</Compile>
Expand Down
Loading

0 comments on commit 3b75787

Please sign in to comment.