1 Commits

Author SHA1 Message Date
f86fb76b33 feat: Implemented basic db operation. 2026-04-29 18:16:57 -04:00
3 changed files with 46 additions and 84 deletions

View File

@@ -1,9 +1,7 @@
const std = @import("std");
/// Keys that are available for encryption
keys: []const SSHKeyPair = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"),
},
keys: []const SSHKeyPair,
/// Rules for how to match the scan command
scan: ScanConfig = .default,
@@ -14,11 +12,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,
};
}
@@ -29,7 +27,7 @@ pub const SSHKeyPair = struct {
return .{
.public = path,
.private = path[0 .. path.len - 4],
.private = path[0..path.len - 4],
};
}
};
@@ -121,7 +119,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,13 +1,7 @@
//! 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,
@@ -21,23 +15,26 @@ changed: bool = false,
pub fn open(
io: std.Io,
gpa: std.mem.Allocator,
opts: OpenOptions,
/// The path to the home directory
home: []const u8,
/// The path to the /tmp directory
tmp: []const u8,
) !@This() {
// TODO: Read from config?
const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" });
// TODO: Check if database already exists
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
defer gpa.free(db_path);
var db = try new(opts.config);
var db = try new();
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, &.{ opts.tmp, "envr.db" });
const tmp_db_path = try std.fs.path.join(gpa, &.{ tmp, "envr.db" });
defer gpa.free(tmp_db_path);
// TODO: Pass key(s) from Config
try age.decrypt(io, gpa, &.{"~/.ssh/id_ed25519"}, db_path, tmp_db_path);
// TODO: Fix key
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);
@@ -48,18 +45,8 @@ 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(config: Config) !@This() {
fn new() !@This() {
var db = try sqlite.Db.init(.{
.mode = .Memory,
.open_flags = .{ .write = true, .create = true },
@@ -75,10 +62,7 @@ fn new(config: Config) !@This() {
\\)
, .{}, .{});
return .{
.sql_db = db,
.config = config,
};
return .{ .sql_db = db };
}
/// Returns true if a file exists at ~/.envr/data.age
@@ -142,7 +126,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;
}
@@ -192,7 +176,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,
);
@@ -239,7 +223,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 = home, .tmp = tmp });
var db: @This() = try .open(io, gpa, home, tmp);
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
defer gpa.free(db_path);
@@ -256,7 +240,3 @@ 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,32 +1,24 @@
const std = @import("std");
/// Decrypts the file into output path
/// Returns the decrypted contents of the file.
/// Caller is responsible for freeing the memory.
pub fn decrypt(
io: std.Io,
gpa: std.mem.Allocator,
private_keys: []const []const u8,
private_key: []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 = argv.items,
.argv = &.{
"age",
"-d",
"-i",
private_key,
"-o",
output_path,
input_path,
},
});
defer gpa.free(result.stderr);
defer gpa.free(result.stdout);
@@ -42,33 +34,25 @@ pub fn decrypt(
}
}
/// Encrypts the file into output path
/// Returns the encrypted contents of the file.
/// Caller is responsible for freeing the memory.
pub fn encrypt(
io: std.Io,
gpa: std.mem.Allocator,
// TODO: Accept multiple keys
public_keys: []const []const u8,
public_key: []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 = argv.items,
.argv = &.{
"age",
"-e",
"-R",
public_key,
"-o",
output_path,
input_path,
},
});
defer gpa.free(result.stderr);
defer gpa.free(result.stdout);
@@ -100,7 +84,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,
);
@@ -127,7 +111,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,
);