1 Commits

Author SHA1 Message Date
0fd0968fee wip: feat: accept config in Db 2026-04-30 22:37:34 -04:00
3 changed files with 23 additions and 44 deletions

View File

@@ -1,9 +1,8 @@
{ {
"db_path": "~/.envr/data.age",
"keys": [ "keys": [
{ {
"private": "~/.ssh/id_ed25519", "private": "/home/spencer/.ssh/id_ed25519",
"public": "~/.ssh/id_ed25519.pub" "public": "/home/spencer/.ssh/id_ed25519.pub"
} }
], ],
"scan": { "scan": {

View File

@@ -1,7 +1,5 @@
const std = @import("std"); const std = @import("std");
db_path: []const u8 = "~/.envr/data.age",
/// Keys that are available for encryption /// Keys that are available for encryption
keys: []const SSHKeyPair = &.{ keys: []const SSHKeyPair = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"), .from_pub_path("~/.ssh/id_ed25519.pub"),
@@ -10,7 +8,6 @@ keys: []const SSHKeyPair = &.{
/// Rules for how to match the scan command /// Rules for how to match the scan command
scan: ScanConfig = .default, scan: ScanConfig = .default,
// TODO: Allow incomplete pairs
pub const SSHKeyPair = struct { pub const SSHKeyPair = struct {
private: []const u8, private: []const u8,
public: []const u8, public: []const u8,
@@ -122,7 +119,11 @@ test "loading the default config from disk matches expected values" {
test "saving to a new file upserts the file" { test "saving to a new file upserts the file" {
const io = std.testing.io; const io = std.testing.io;
var cfg: @This() = .{}; var cfg: @This() = .{
.keys = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"),
},
};
var tmp = std.testing.tmpDir(.{}); var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup(); defer tmp.cleanup();
@@ -146,7 +147,6 @@ test "saving to a new file upserts the file" {
const want = const want =
\\{ \\{
\\ "db_path": "~/.envr/data.age",
\\ "keys": [ \\ "keys": [
\\ { \\ {
\\ "private": "~/.ssh/id_ed25519", \\ "private": "~/.ssh/id_ed25519",
@@ -174,7 +174,11 @@ test "saving to a new file upserts the file" {
test "saving to an existing file updates the file" { test "saving to an existing file updates the file" {
const io = std.testing.io; const io = std.testing.io;
var cfg: @This() = .{}; var cfg: @This() = .{
.keys = &.{
.from_pub_path("~/.ssh/id_ed25519.pub"),
},
};
var tmp = std.testing.tmpDir(.{}); var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup(); defer tmp.cleanup();
@@ -196,7 +200,6 @@ test "saving to an existing file updates the file" {
const want = const want =
\\{ \\{
\\ "db_path": "~/.envr/data.age",
\\ "keys": [ \\ "keys": [
\\ { \\ {
\\ "private": "~/.ssh/id_ed25519", \\ "private": "~/.ssh/id_ed25519",

View File

@@ -23,11 +23,8 @@ pub fn open(
gpa: std.mem.Allocator, gpa: std.mem.Allocator,
opts: OpenOptions, opts: OpenOptions,
) !@This() { ) !@This() {
// FIXME: cheating here // TODO: Read from config?
const db_path = try std.fs.path.join(gpa, &.{ const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" });
opts.home,
opts.config.db_path[2..],
});
defer gpa.free(db_path); defer gpa.free(db_path);
var db = try new(opts.config); var db = try new(opts.config);
@@ -39,18 +36,8 @@ pub fn open(
const tmp_db_path = try std.fs.path.join(gpa, &.{ opts.tmp, "envr.db" }); const tmp_db_path = try std.fs.path.join(gpa, &.{ opts.tmp, "envr.db" });
defer gpa.free(tmp_db_path); defer gpa.free(tmp_db_path);
// TODO: Use std.MultiArrayList? Had json issues
var private_keys: std.ArrayList([]const u8) = try .initCapacity(
gpa,
opts.config.keys.len,
);
for (opts.config.keys) |key| {
private_keys.appendAssumeCapacity(key.private);
}
// TODO: Pass key(s) from Config // TODO: Pass key(s) from Config
try age.decrypt(io, gpa, private_keys.items, db_path, tmp_db_path); try age.decrypt(io, gpa, &.{"~/.ssh/id_ed25519"}, db_path, tmp_db_path);
try db.restore(tmp_db_path); try db.restore(tmp_db_path);
try std.Io.Dir.cwd().deleteFile(io, tmp_db_path); try std.Io.Dir.cwd().deleteFile(io, tmp_db_path);
@@ -140,30 +127,22 @@ pub fn close(
self: *@This(), self: *@This(),
io: std.Io, io: std.Io,
gpa: std.mem.Allocator, gpa: std.mem.Allocator,
opts: OpenOptions, home: []const u8,
tmp: []const u8,
) !void { ) !void {
defer self.sql_db.deinit(); defer self.sql_db.deinit();
if (self.changed) { if (self.changed) {
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); defer gpa.free(tmp_db_path);
try self.sql_db.exec("VACUUM INTO ?", .{}, .{tmp_db_path}); try self.sql_db.exec("VACUUM INTO ?", .{}, .{tmp_db_path});
const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" }); const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
defer gpa.free(db_path); defer gpa.free(db_path);
// TODO: Use std.MultiArrayList? Had json issues // FIXME: Use real key
var public_keys: std.ArrayList([]const u8) = try .initCapacity( try age.encrypt(io, gpa, &.{"~/.ssh/id_ed25519.pub"}, tmp_db_path, db_path);
gpa,
opts.config.keys.len,
);
for (opts.config.keys) |key| {
public_keys.appendAssumeCapacity(key.private);
}
try age.encrypt(io, gpa, public_keys.items, tmp_db_path, db_path);
self.changed = false; self.changed = false;
} }
@@ -260,10 +239,8 @@ test "Closing a fresh database does not create a file" {
const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" }); const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" });
defer gpa.free(tmp); defer gpa.free(tmp);
// TODO: Pass testing keys
var db: @This() = try .open(io, gpa, .{ .home = home, .tmp = tmp }); var db: @This() = try .open(io, gpa, .{ .home = home, .tmp = tmp });
// TODO: Get rid of direct access
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" }); const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
defer gpa.free(db_path); defer gpa.free(db_path);
@@ -272,7 +249,7 @@ test "Closing a fresh database does not create a file" {
tmp_dir.dir.access(io, db_path, .{ .read = true }), tmp_dir.dir.access(io, db_path, .{ .read = true }),
); );
try db.close(io, gpa, .{ .home = home, .tmp = tmp }); try db.close(io, gpa, home, tmp);
try std.testing.expectError( try std.testing.expectError(
error.FileNotFound, error.FileNotFound,