mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 10:38:33 -04:00
Compare commits
2 Commits
4b886a80c6
...
17cf22ddfd
| Author | SHA1 | Date | |
|---|---|---|---|
| 17cf22ddfd | |||
| 8f2c241963 |
@@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
|
"db_path": "~/.envr/data.age",
|
||||||
"keys": [
|
"keys": [
|
||||||
{
|
{
|
||||||
"private": "/home/spencer/.ssh/id_ed25519",
|
"private": "~/.ssh/id_ed25519",
|
||||||
"public": "/home/spencer/.ssh/id_ed25519.pub"
|
"public": "~/.ssh/id_ed25519.pub"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"scan": {
|
"scan": {
|
||||||
@@ -17,4 +18,4 @@
|
|||||||
"~"
|
"~"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BIN
fixtures/encrypted-single-file.db.age
Normal file
BIN
fixtures/encrypted-single-file.db.age
Normal file
Binary file not shown.
BIN
fixtures/single-file.db
Normal file
BIN
fixtures/single-file.db
Normal file
Binary file not shown.
@@ -1,5 +1,7 @@
|
|||||||
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"),
|
||||||
@@ -120,11 +122,7 @@ 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();
|
||||||
@@ -148,6 +146,7 @@ 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",
|
||||||
@@ -175,11 +174,7 @@ 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();
|
||||||
@@ -201,6 +196,7 @@ 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",
|
||||||
|
|||||||
118
src/Db.zig
118
src/Db.zig
@@ -23,8 +23,11 @@ pub fn open(
|
|||||||
gpa: std.mem.Allocator,
|
gpa: std.mem.Allocator,
|
||||||
opts: OpenOptions,
|
opts: OpenOptions,
|
||||||
) !@This() {
|
) !@This() {
|
||||||
// TODO: Read from config?
|
// FIXME: cheating here
|
||||||
const db_path = try std.fs.path.join(gpa, &.{ opts.home, ".envr", "data.age" });
|
const db_path = try std.fs.path.join(gpa, &.{
|
||||||
|
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);
|
||||||
@@ -37,6 +40,7 @@ pub fn open(
|
|||||||
defer gpa.free(tmp_db_path);
|
defer gpa.free(tmp_db_path);
|
||||||
|
|
||||||
// TODO: Use std.MultiArrayList? Had json issues
|
// TODO: Use std.MultiArrayList? Had json issues
|
||||||
|
// FIXME: Create a .deinit function to free this memory
|
||||||
var private_keys: std.ArrayList([]const u8) = try .initCapacity(
|
var private_keys: std.ArrayList([]const u8) = try .initCapacity(
|
||||||
gpa,
|
gpa,
|
||||||
opts.config.keys.len,
|
opts.config.keys.len,
|
||||||
@@ -72,7 +76,10 @@ const OpenOptions = struct {
|
|||||||
fn new(config: Config) !@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,
|
||||||
|
},
|
||||||
.threading_mode = .MultiThread,
|
.threading_mode = .MultiThread,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -111,13 +118,21 @@ fn restore(
|
|||||||
.{},
|
.{},
|
||||||
.{path},
|
.{path},
|
||||||
);
|
);
|
||||||
|
std.debug.print("path: {s}\n", .{path});
|
||||||
defer self.sql_db.exec("DETACH DATABASE source", .{}, .{}) catch unreachable;
|
defer self.sql_db.exec("DETACH DATABASE source", .{}, .{}) catch unreachable;
|
||||||
|
|
||||||
try self.sql_db.exec(
|
var diags: sqlite.Diagnostics = .{};
|
||||||
|
self.sql_db.exec(
|
||||||
"INSERT INTO main.envr_env_files SELECT * FROM source.envr_env_files",
|
"INSERT INTO main.envr_env_files SELECT * FROM source.envr_env_files",
|
||||||
|
.{ .diags = &diags },
|
||||||
.{},
|
.{},
|
||||||
.{},
|
) catch |err| {
|
||||||
);
|
std.log.err(
|
||||||
|
"unable to prepare statement, got error {}. diagnostics: {f}",
|
||||||
|
.{ err, diags },
|
||||||
|
);
|
||||||
|
return err;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Finish
|
// TODO: Finish
|
||||||
@@ -166,6 +181,30 @@ pub fn close(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a list of all the .env files present in the database.
|
||||||
|
/// The caller is responsible for freeing memory
|
||||||
|
fn list(self: *@This(), gpa: std.mem.Allocator) ![]EnvFile {
|
||||||
|
var stmt = try self.sql_db.prepare(
|
||||||
|
"select path, remotes, sha256, contents from envr_env_files",
|
||||||
|
);
|
||||||
|
defer stmt.deinit();
|
||||||
|
|
||||||
|
return stmt.all(EnvFile, gpa, .{}, .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
const EnvFile = struct {
|
||||||
|
// TODO: Should use file_name in the struct and derive from the path.
|
||||||
|
path: []const u8,
|
||||||
|
|
||||||
|
// /// dir is derived from Path, and is not stored in the database.
|
||||||
|
// dir: []const u8,
|
||||||
|
|
||||||
|
/// JSON encoded list of strings
|
||||||
|
remotes: []const u8,
|
||||||
|
sha256: []const u8,
|
||||||
|
contents: []const u8,
|
||||||
|
};
|
||||||
|
|
||||||
test {
|
test {
|
||||||
std.testing.refAllDecls(@import("age.zig"));
|
std.testing.refAllDecls(@import("age.zig"));
|
||||||
}
|
}
|
||||||
@@ -245,7 +284,6 @@ test "Closing a fresh database does not create a file" {
|
|||||||
var tmp_dir = std.testing.tmpDir(.{});
|
var tmp_dir = std.testing.tmpDir(.{});
|
||||||
defer tmp_dir.cleanup();
|
defer tmp_dir.cleanup();
|
||||||
|
|
||||||
// @compileLog(@typeInfo(std.Io.File.Permissions));
|
|
||||||
try tmp_dir.dir.createDir(io, "home", .default_dir);
|
try tmp_dir.dir.createDir(io, "home", .default_dir);
|
||||||
try tmp_dir.dir.createDir(io, "tmp", .default_dir);
|
try tmp_dir.dir.createDir(io, "tmp", .default_dir);
|
||||||
|
|
||||||
@@ -257,8 +295,10 @@ 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);
|
||||||
|
|
||||||
@@ -278,3 +318,67 @@ test "Closing a fresh database does not create a file" {
|
|||||||
// test "Closing an unmodified database does not update the file" {}
|
// test "Closing an unmodified database does not update the file" {}
|
||||||
|
|
||||||
// test "Closing a modified database does create a file" {}
|
// test "Closing a modified database does create a file" {}
|
||||||
|
|
||||||
|
test "list displays the database's keys" {
|
||||||
|
const io = std.testing.io;
|
||||||
|
const gpa = std.testing.allocator;
|
||||||
|
|
||||||
|
var tmp_dir = std.testing.tmpDir(.{});
|
||||||
|
defer tmp_dir.cleanup();
|
||||||
|
|
||||||
|
try tmp_dir.dir.createDir(io, "home", .default_dir);
|
||||||
|
try tmp_dir.dir.createDir(io, "home/.envr", .default_dir);
|
||||||
|
try tmp_dir.dir.createDir(io, "tmp", .default_dir);
|
||||||
|
|
||||||
|
const tmp_dir_path = try tmp_dir.dir.realPathFileAlloc(io, ".", gpa);
|
||||||
|
defer gpa.free(tmp_dir_path);
|
||||||
|
|
||||||
|
const home = try std.fs.path.join(gpa, &.{ tmp_dir_path, "home" });
|
||||||
|
defer gpa.free(home);
|
||||||
|
const tmp = try std.fs.path.join(gpa, &.{ tmp_dir_path, "tmp" });
|
||||||
|
defer gpa.free(tmp);
|
||||||
|
|
||||||
|
// TODO: Get rid of direct access
|
||||||
|
const db_path = try std.fs.path.join(gpa, &.{ home, ".envr", "data.age" });
|
||||||
|
defer gpa.free(db_path);
|
||||||
|
|
||||||
|
try std.Io.Dir.cwd().copyFile(
|
||||||
|
"fixtures/encrypted-single-file.db.age",
|
||||||
|
tmp_dir.dir,
|
||||||
|
"home/.envr/data.age",
|
||||||
|
io,
|
||||||
|
.{},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Asserts file existence
|
||||||
|
try tmp_dir.dir.access(io, db_path, .{ .read = true });
|
||||||
|
|
||||||
|
// TODO: Pass testing keys
|
||||||
|
const config: Config = .{
|
||||||
|
.keys = &.{.from_pub_path("fixtures/insecure-test-key.pub")},
|
||||||
|
};
|
||||||
|
var db: @This() = try .open(io, gpa, .{
|
||||||
|
.config = config,
|
||||||
|
.home = home,
|
||||||
|
.tmp = tmp,
|
||||||
|
});
|
||||||
|
|
||||||
|
const env_files = try db.list(gpa);
|
||||||
|
try std.testing.expectEqual(1, env_files.len);
|
||||||
|
|
||||||
|
var hasher = std.crypto.hash.sha2.Sha256.init(.{});
|
||||||
|
|
||||||
|
for (env_files) |file| {
|
||||||
|
try std.testing.expectEqualSlices(u8, "<placeholder>", file.path);
|
||||||
|
try std.testing.expectEqualSlices(u8, "<placeholder>", file.contents);
|
||||||
|
try std.testing.expectEqualSlices(u8, "<placeholder>", file.remotes);
|
||||||
|
|
||||||
|
hasher.update(file.contents);
|
||||||
|
const hash = hasher.finalResult();
|
||||||
|
try std.testing.expectEqualSlices(u8, &hash, file.sha256);
|
||||||
|
} else {
|
||||||
|
return error.TestUnexpectedResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
try db.close(io, gpa, .{ .home = home, .tmp = tmp });
|
||||||
|
}
|
||||||
|
|||||||
@@ -2257,7 +2257,7 @@ pub fn Statement(comptime opts: StatementOptions, comptime query: anytype) type
|
|||||||
pub fn all(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, values: anytype) ![]Type {
|
pub fn all(self: *Self, comptime Type: type, allocator: mem.Allocator, options: QueryOptions, values: anytype) ![]Type {
|
||||||
var iter = try self.iteratorAlloc(Type, allocator, values);
|
var iter = try self.iteratorAlloc(Type, allocator, values);
|
||||||
|
|
||||||
var rows: std.ArrayList(Type) = .{};
|
var rows: std.ArrayList(Type) = .empty;
|
||||||
while (try iter.nextAlloc(allocator, options)) |row| {
|
while (try iter.nextAlloc(allocator, options)) |row| {
|
||||||
try rows.append(allocator, row);
|
try rows.append(allocator, row);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user