mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 18:48:33 -04:00
Compare commits
3 Commits
f86fb76b33
...
0fd0968fee
| Author | SHA1 | Date | |
|---|---|---|---|
| 0fd0968fee | |||
| fd0f8bba03 | |||
| 6557139380 |
@@ -1,7 +1,9 @@
|
|||||||
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,
|
||||||
@@ -12,11 +14,11 @@ pub const SSHKeyPair = struct {
|
|||||||
|
|
||||||
/// Caller owns the returned memory
|
/// Caller owns the returned memory
|
||||||
pub fn from_path(gpa: std.mem.Allocator, path: []const u8) !SSHKeyPair {
|
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);
|
return from_pub_path(path);
|
||||||
} else {
|
} else {
|
||||||
return .{
|
return .{
|
||||||
.public = try std.mem.concat(gpa, u8, &.{path, ".pub"}),
|
.public = try std.mem.concat(gpa, u8, &.{ path, ".pub" }),
|
||||||
.private = path,
|
.private = path,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -27,7 +29,7 @@ pub const SSHKeyPair = struct {
|
|||||||
|
|
||||||
return .{
|
return .{
|
||||||
.public = path,
|
.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() = .{
|
var cfg: @This() = .{
|
||||||
.keys = &.{
|
.keys = &.{
|
||||||
.from_pub_path("~/.ssh/id_ed25519.pub"),
|
.from_pub_path("~/.ssh/id_ed25519.pub"),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
50
src/Db.zig
50
src/Db.zig
@@ -1,7 +1,13 @@
|
|||||||
|
//! 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,
|
||||||
@@ -15,26 +21,23 @@ changed: bool = false,
|
|||||||
pub fn open(
|
pub fn open(
|
||||||
io: std.Io,
|
io: std.Io,
|
||||||
gpa: std.mem.Allocator,
|
gpa: std.mem.Allocator,
|
||||||
/// The path to the home directory
|
opts: OpenOptions,
|
||||||
home: []const u8,
|
|
||||||
/// The path to the /tmp directory
|
|
||||||
tmp: []const u8,
|
|
||||||
) !@This() {
|
) !@This() {
|
||||||
// TODO: Check if database already exists
|
// TODO: Read from config?
|
||||||
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
|
const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" });
|
||||||
defer gpa.free(db_path);
|
defer gpa.free(db_path);
|
||||||
|
|
||||||
var db = try new();
|
var db = try new(opts.config);
|
||||||
|
|
||||||
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, &.{ 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: Fix key
|
// TODO: Pass key(s) from Config
|
||||||
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);
|
||||||
@@ -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
|
/// Create a new instance of the database in-memory
|
||||||
fn new() !@This() {
|
fn new(config: Config) !@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 },
|
||||||
@@ -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
|
/// Returns true if a file exists at ~/.envr/data.age
|
||||||
@@ -126,7 +142,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;
|
||||||
}
|
}
|
||||||
@@ -176,7 +192,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,
|
||||||
);
|
);
|
||||||
@@ -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" });
|
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, 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" });
|
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
|
||||||
defer gpa.free(db_path);
|
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 }),
|
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,24 +1,32 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
/// Returns the decrypted contents of the file.
|
/// Decrypts the file into output path
|
||||||
/// 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_key: []const u8,
|
private_keys: []const []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 = argv.items,
|
||||||
"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);
|
||||||
@@ -34,25 +42,33 @@ pub fn decrypt(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the encrypted contents of the file.
|
/// Encrypts the file into output path
|
||||||
/// 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,
|
||||||
public_key: []const u8,
|
// TODO: Accept multiple keys
|
||||||
|
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 = argv.items,
|
||||||
"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);
|
||||||
@@ -84,7 +100,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,
|
||||||
);
|
);
|
||||||
@@ -111,7 +127,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