mirror of
https://github.com/sbrow/envr.git
synced 2026-06-28 02:58:33 -04:00
Compare commits
1 Commits
0fd0968fee
...
f86fb76b33
| Author | SHA1 | Date | |
|---|---|---|---|
| f86fb76b33 |
@@ -1,9 +1,7 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
/// 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"),
|
|
||||||
},
|
|
||||||
|
|
||||||
/// Rules for how to match the scan command
|
/// Rules for how to match the scan command
|
||||||
scan: ScanConfig = .default,
|
scan: ScanConfig = .default,
|
||||||
|
|||||||
50
src/Db.zig
50
src/Db.zig
@@ -1,13 +1,7 @@
|
|||||||
//! Db interacts with an age encrypted sqlite database.
|
|
||||||
//!
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const sqlite = @import("sqlite");
|
const sqlite = @import("sqlite");
|
||||||
|
|
||||||
const age = @import("age.zig");
|
const age = @import("age.zig");
|
||||||
const Config = @import("Config.zig");
|
|
||||||
|
|
||||||
/// controls the keys and filepaths used for saving
|
|
||||||
config: Config,
|
|
||||||
|
|
||||||
/// The underlying data store.
|
/// The underlying data store.
|
||||||
sql_db: sqlite.Db,
|
sql_db: sqlite.Db,
|
||||||
@@ -21,23 +15,26 @@ changed: bool = false,
|
|||||||
pub fn open(
|
pub fn open(
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
gpa: std.mem.Allocator,
|
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() {
|
) !@This() {
|
||||||
// TODO: Read from config?
|
// TODO: Check if database already exists
|
||||||
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);
|
||||||
|
|
||||||
var db = try new(opts.config);
|
var db = try new();
|
||||||
|
|
||||||
if (db_exists(io, db_path)) {
|
if (db_exists(io, db_path)) {
|
||||||
// const tmp_dir = try std.Io.Dir.cwd().openDir(io, tmp, .{});
|
// const tmp_dir = try std.Io.Dir.cwd().openDir(io, tmp, .{});
|
||||||
// defer tmp_dir.deleteFile(io, "envr.db");
|
// 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);
|
defer gpa.free(tmp_db_path);
|
||||||
|
|
||||||
// TODO: Pass key(s) from Config
|
// TODO: Fix key
|
||||||
try age.decrypt(io, gpa, &.{"~/.ssh/id_ed25519"}, 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);
|
||||||
@@ -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
|
/// Create a new instance of the database in-memory
|
||||||
fn new(config: Config) !@This() {
|
fn new() !@This() {
|
||||||
var db = try sqlite.Db.init(.{
|
var db = try sqlite.Db.init(.{
|
||||||
.mode = .Memory,
|
.mode = .Memory,
|
||||||
.open_flags = .{ .write = true, .create = true },
|
.open_flags = .{ .write = true, .create = true },
|
||||||
@@ -75,10 +62,7 @@ fn new(config: Config) !@This() {
|
|||||||
\\)
|
\\)
|
||||||
, .{}, .{});
|
, .{}, .{});
|
||||||
|
|
||||||
return .{
|
return .{ .sql_db = db };
|
||||||
.sql_db = db,
|
|
||||||
.config = config,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if a file exists at ~/.envr/data.age
|
/// Returns true if a file exists at ~/.envr/data.age
|
||||||
@@ -142,7 +126,7 @@ pub fn close(
|
|||||||
defer gpa.free(db_path);
|
defer gpa.free(db_path);
|
||||||
|
|
||||||
// FIXME: Use real key
|
// 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;
|
self.changed = false;
|
||||||
}
|
}
|
||||||
@@ -192,7 +176,7 @@ test "encrypted database can be opened" {
|
|||||||
try age.decrypt(
|
try age.decrypt(
|
||||||
io,
|
io,
|
||||||
gpa,
|
gpa,
|
||||||
&.{"./fixtures/insecure-test-key"},
|
"./fixtures/insecure-test-key",
|
||||||
"./fixtures/encrypted-example.db.age",
|
"./fixtures/encrypted-example.db.age",
|
||||||
decrypted_path,
|
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" });
|
const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" });
|
||||||
defer gpa.free(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" });
|
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
|
||||||
defer gpa.free(db_path);
|
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 }),
|
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" {}
|
|
||||||
|
|||||||
68
src/age.zig
68
src/age.zig
@@ -1,32 +1,24 @@
|
|||||||
const std = @import("std");
|
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(
|
pub fn decrypt(
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
gpa: std.mem.Allocator,
|
gpa: std.mem.Allocator,
|
||||||
private_keys: []const []const u8,
|
private_key: []const u8,
|
||||||
input_path: []const u8,
|
input_path: []const u8,
|
||||||
output_path: []const u8,
|
output_path: []const u8,
|
||||||
) !void {
|
) !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, .{
|
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.stderr);
|
||||||
defer gpa.free(result.stdout);
|
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(
|
pub fn encrypt(
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
gpa: std.mem.Allocator,
|
gpa: std.mem.Allocator,
|
||||||
// TODO: Accept multiple keys
|
public_key: []const u8,
|
||||||
public_keys: []const []const u8,
|
|
||||||
input_path: []const u8,
|
input_path: []const u8,
|
||||||
output_path: []const u8,
|
output_path: []const u8,
|
||||||
) !void {
|
) !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, .{
|
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.stderr);
|
||||||
defer gpa.free(result.stdout);
|
defer gpa.free(result.stdout);
|
||||||
@@ -100,7 +84,7 @@ test "sample file can be decrypted" {
|
|||||||
try decrypt(
|
try decrypt(
|
||||||
io,
|
io,
|
||||||
gpa,
|
gpa,
|
||||||
&.{"./fixtures/insecure-test-key"},
|
"./fixtures/insecure-test-key",
|
||||||
"./fixtures/hello-world.age",
|
"./fixtures/hello-world.age",
|
||||||
output_path,
|
output_path,
|
||||||
);
|
);
|
||||||
@@ -127,7 +111,7 @@ test "sample file can be encrypted" {
|
|||||||
try encrypt(
|
try encrypt(
|
||||||
io,
|
io,
|
||||||
gpa,
|
gpa,
|
||||||
&.{"./fixtures/insecure-test-key.pub"},
|
"./fixtures/insecure-test-key.pub",
|
||||||
"./fixtures/hello-world.txt",
|
"./fixtures/hello-world.txt",
|
||||||
output_path,
|
output_path,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user