3 Commits

3 changed files with 84 additions and 46 deletions

View File

@@ -1,7 +1,9 @@
const std = @import("std");
/// Keys that are available for encryption
keys: []const SSHKeyPair,
keys: []const SSHKeyPair = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"),
},
/// Rules for how to match the scan command
scan: ScanConfig = .default,
@@ -12,11 +14,11 @@ pub const SSHKeyPair = struct {
/// Caller owns the returned memory
pub fn from_path(gpa: std.mem.Allocator, path: []const u8) !SSHKeyPair {
if (std.mem.eql(u8, std.fs.path.extension(path), ".pub")){
if (std.mem.eql(u8, std.fs.path.extension(path), ".pub")) {
return from_pub_path(path);
} else {
return .{
.public = try std.mem.concat(gpa, u8, &.{path, ".pub"}),
.public = try std.mem.concat(gpa, u8, &.{ path, ".pub" }),
.private = path,
};
}
@@ -27,7 +29,7 @@ pub const SSHKeyPair = struct {
return .{
.public = path,
.private = path[0..path.len - 4],
.private = path[0 .. path.len - 4],
};
}
};
@@ -119,7 +121,7 @@ test "saving to a new file upserts the file" {
var cfg: @This() = .{
.keys = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"),
.from_pub_path("~/.ssh/id_ed25519.pub"),
},
};

View File

@@ -1,7 +1,13 @@
//! Db interacts with an age encrypted sqlite database.
//!
const std = @import("std");
const sqlite = @import("sqlite");
const age = @import("age.zig");
const Config = @import("Config.zig");
/// controls the keys and filepaths used for saving
config: Config,
/// The underlying data store.
sql_db: sqlite.Db,
@@ -15,26 +21,23 @@ changed: bool = false,
pub fn open(
io: std.Io,
gpa: std.mem.Allocator,
/// The path to the home directory
home: []const u8,
/// The path to the /tmp directory
tmp: []const u8,
opts: OpenOptions,
) !@This() {
// TODO: Check if database already exists
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
// TODO: Read from config?
const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" });
defer gpa.free(db_path);
var db = try new();
var db = try new(opts.config);
if (db_exists(io, db_path)) {
// const tmp_dir = try std.Io.Dir.cwd().openDir(io, tmp, .{});
// defer tmp_dir.deleteFile(io, "envr.db");
const tmp_db_path = try std.fs.path.join(gpa, &.{ tmp, "envr.db" });
const tmp_db_path = try std.fs.path.join(gpa, &.{ opts.tmp, "envr.db" });
defer gpa.free(tmp_db_path);
// TODO: Fix key
try age.decrypt(io, gpa, "~/.ssh/id_ed25519", db_path, tmp_db_path);
// TODO: Pass key(s) from Config
try age.decrypt(io, gpa, &.{"~/.ssh/id_ed25519"}, db_path, tmp_db_path);
try db.restore(tmp_db_path);
try std.Io.Dir.cwd().deleteFile(io, tmp_db_path);
@@ -45,8 +48,18 @@ pub fn open(
}
}
const OpenOptions = struct {
config: Config = .{},
/// The path to the home directory
home: []const u8 = "~/",
/// The path to the /tmp directory
// FIXME: Support windows
tmp: []const u8 = "/tmp",
};
/// Create a new instance of the database in-memory
fn new() !@This() {
fn new(config: Config) !@This() {
var db = try sqlite.Db.init(.{
.mode = .Memory,
.open_flags = .{ .write = true, .create = true },
@@ -62,7 +75,10 @@ fn new() !@This() {
\\)
, .{}, .{});
return .{ .sql_db = db };
return .{
.sql_db = db,
.config = config,
};
}
/// Returns true if a file exists at ~/.envr/data.age
@@ -126,7 +142,7 @@ pub fn close(
defer gpa.free(db_path);
// FIXME: Use real key
try age.encrypt(io, gpa, "~/.ssh/id_ed25519.pub", tmp_db_path, db_path);
try age.encrypt(io, gpa, &.{"~/.ssh/id_ed25519.pub"}, tmp_db_path, db_path);
self.changed = false;
}
@@ -176,7 +192,7 @@ test "encrypted database can be opened" {
try age.decrypt(
io,
gpa,
"./fixtures/insecure-test-key",
&.{"./fixtures/insecure-test-key"},
"./fixtures/encrypted-example.db.age",
decrypted_path,
);
@@ -223,7 +239,7 @@ test "Closing a fresh database does not create a file" {
const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" });
defer gpa.free(tmp);
var db: @This() = try .open(io, gpa, home, tmp);
var db: @This() = try .open(io, gpa, .{ .home = home, .tmp = tmp });
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
defer gpa.free(db_path);
@@ -240,3 +256,7 @@ test "Closing a fresh database does not create a file" {
tmp_dir.dir.access(io, db_path, .{ .read = true }),
);
}
// test "Closing an unmodified database does not update the file" {}
// test "Closing a modified database does create a file" {}

View File

@@ -1,24 +1,32 @@
const std = @import("std");
/// Returns the decrypted contents of the file.
/// Caller is responsible for freeing the memory.
/// Decrypts the file into output path
pub fn decrypt(
io: std.Io,
gpa: std.mem.Allocator,
private_key: []const u8,
private_keys: []const []const u8,
input_path: []const u8,
output_path: []const u8,
) !void {
// TODO: use raw array?
var argv: std.ArrayList([]const u8) = try .initCapacity(gpa, 2 + (2 * private_keys.len) + 3);
defer argv.deinit(gpa);
argv.appendAssumeCapacity("age");
argv.appendAssumeCapacity("-d");
for (private_keys) |key| {
argv.appendAssumeCapacity("-i");
argv.appendAssumeCapacity(key);
}
argv.appendAssumeCapacity("-o");
argv.appendAssumeCapacity(output_path);
argv.appendAssumeCapacity(input_path);
const result = try std.process.run(gpa, io, .{
.argv = &.{
"age",
"-d",
"-i",
private_key,
"-o",
output_path,
input_path,
},
.argv = argv.items,
});
defer gpa.free(result.stderr);
defer gpa.free(result.stdout);
@@ -34,25 +42,33 @@ pub fn decrypt(
}
}
/// Returns the encrypted contents of the file.
/// Caller is responsible for freeing the memory.
/// Encrypts the file into output path
pub fn encrypt(
io: std.Io,
gpa: std.mem.Allocator,
public_key: []const u8,
// TODO: Accept multiple keys
public_keys: []const []const u8,
input_path: []const u8,
output_path: []const u8,
) !void {
var argv: std.ArrayList([]const u8) = try .initCapacity(gpa, 2 + (2 * public_keys.len) + 3);
defer argv.deinit(gpa);
argv.appendAssumeCapacity("age");
argv.appendAssumeCapacity("-e");
for (public_keys) |key| {
argv.appendAssumeCapacity("-R");
argv.appendAssumeCapacity(key);
}
argv.appendAssumeCapacity("-o");
argv.appendAssumeCapacity(output_path);
argv.appendAssumeCapacity(input_path);
const result = try std.process.run(gpa, io, .{
.argv = &.{
"age",
"-e",
"-R",
public_key,
"-o",
output_path,
input_path,
},
.argv = argv.items,
});
defer gpa.free(result.stderr);
defer gpa.free(result.stdout);
@@ -84,7 +100,7 @@ test "sample file can be decrypted" {
try decrypt(
io,
gpa,
"./fixtures/insecure-test-key",
&.{"./fixtures/insecure-test-key"},
"./fixtures/hello-world.age",
output_path,
);
@@ -111,7 +127,7 @@ test "sample file can be encrypted" {
try encrypt(
io,
gpa,
"./fixtures/insecure-test-key.pub",
&.{"./fixtures/insecure-test-key.pub"},
"./fixtures/hello-world.txt",
output_path,
);