Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export encryption package #231

Merged
merged 25 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
fb6cd00
make Drive and ZipService use a consistent interface
john-s-morgan Apr 28, 2023
e39d578
Rename file to match class name
john-s-morgan Apr 28, 2023
6bd8161
Add constants for filenames
john-s-morgan Apr 28, 2023
3f52757
Allow for multiple writes
john-s-morgan Apr 28, 2023
0fdfcaa
update constants and IStorageService
john-s-morgan Apr 28, 2023
74f02d0
implicit conversion for records to string
john-s-morgan Apr 28, 2023
7064bce
export encryption
john-s-morgan Apr 28, 2023
e69d8fd
Storage extract
john-s-morgan May 8, 2023
fcfa4c5
Update Election
john-s-morgan May 8, 2023
2a3c402
Bump actions/setup-node from 2 to 3 (#226)
dependabot[bot] May 1, 2023
860f214
Moved to the CiphertextBallot and removed the deprecated one (#228)
SteveMaier-IRT May 5, 2023
baa0671
remove uneeded files
john-s-morgan May 8, 2023
083b8ff
export election package
john-s-morgan May 8, 2023
23ebd2f
Merge branch 'main' into task/Export-encription-backend
john-s-morgan May 8, 2023
224ee33
resolve Linter issues
john-s-morgan May 8, 2023
c0743b6
Cleanup
john-s-morgan May 8, 2023
804e570
UtcNow not now
john-s-morgan May 8, 2023
d405349
Unneeded using
john-s-morgan May 8, 2023
f48e4d2
Linter updates
john-s-morgan May 8, 2023
e745d4e
constructor update
john-s-morgan May 8, 2023
bf1b053
mark step 1 complete
john-s-morgan May 8, 2023
25135f5
AddBallots Button enabling
john-s-morgan May 8, 2023
179c80c
set DateTime on export
john-s-morgan May 8, 2023
b204c15
Mark election as exported
john-s-morgan May 8, 2023
bcdc8f3
naming
john-s-morgan May 8, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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