2023-10-22 20:26:57 +00:00
|
|
|
const std = @import("std");
|
2024-05-15 20:16:24 +00:00
|
|
|
const universal_lambda_build = @import("universal-lambda-zig");
|
2023-10-22 20:26:57 +00:00
|
|
|
|
2024-02-24 23:48:18 +00:00
|
|
|
// This seems to fail for some reason. zig-sqlite does a lot of messing with
|
|
|
|
// the target. So instead, we will handle this in the CI/CD system at the
|
|
|
|
// command line
|
2024-02-24 19:34:05 +00:00
|
|
|
const test_targets = [_]std.zig.CrossTarget{
|
|
|
|
.{}, // native
|
|
|
|
// .{
|
|
|
|
// .cpu_arch = .x86_64,
|
|
|
|
// .os_tag = .linux,
|
|
|
|
// },
|
|
|
|
// .{
|
|
|
|
// .cpu_arch = .aarch64,
|
|
|
|
// .os_tag = .linux,
|
|
|
|
// },
|
|
|
|
// .{
|
|
|
|
// .cpu_arch = .riscv64,
|
|
|
|
// .os_tag = .linux,
|
|
|
|
// },
|
2024-02-24 23:48:18 +00:00
|
|
|
// will not work
|
2024-02-24 19:34:05 +00:00
|
|
|
// .{
|
|
|
|
// .cpu_arch = .arm,
|
|
|
|
// .os_tag = .linux,
|
|
|
|
// },
|
|
|
|
// .{
|
|
|
|
// .cpu_arch = .x86_64,
|
|
|
|
// .os_tag = .windows,
|
|
|
|
// },
|
|
|
|
// .{
|
|
|
|
// .cpu_arch = .aarch64,
|
|
|
|
// .os_tag = .macos,
|
|
|
|
// },
|
|
|
|
// .{
|
|
|
|
// .cpu_arch = .x86_64,
|
|
|
|
// .os_tag = .macos,
|
|
|
|
// },
|
|
|
|
// Since we are using sqlite, we cannot use wasm32/wasi at this time. Even
|
|
|
|
// with compile errors above, I do not believe wasi will be easily supported
|
|
|
|
// .{
|
|
|
|
// .cpu_arch = .wasm32,
|
|
|
|
// .os_tag = .wasi,
|
|
|
|
// },
|
|
|
|
};
|
|
|
|
|
2023-10-22 20:26:57 +00:00
|
|
|
// Although this function looks imperative, note that its job is to
|
|
|
|
// declaratively construct a build graph that will be executed by an external
|
|
|
|
// runner.
|
|
|
|
pub fn build(b: *std.Build) !void {
|
|
|
|
// Standard target options allows the person running `zig build` to choose
|
|
|
|
// what target to build for. Here we do not override the defaults, which
|
|
|
|
// means any target is allowed, and the default is native. Other options
|
|
|
|
// for restricting supported target set are available.
|
|
|
|
const target = b.standardTargetOptions(.{});
|
|
|
|
|
|
|
|
// Standard optimization options allow the person running `zig build` to select
|
|
|
|
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
|
|
|
|
// set a preferred release mode, allowing the user to decide how to optimize.
|
|
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
|
|
|
|
|
|
const exe = b.addExecutable(.{
|
|
|
|
.name = "ddblocal",
|
|
|
|
// In this case the main source file is merely a path, however, in more
|
|
|
|
// complicated build scripts, this could be a generated file.
|
2024-06-08 18:17:06 +00:00
|
|
|
.root_source_file = b.path("src/main.zig"),
|
2023-10-22 20:26:57 +00:00
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
|
|
|
|
|
|
|
// This declares intent for the executable to be installed into the
|
|
|
|
// standard location when the user invokes the "install" step (the default
|
|
|
|
// step when running `zig build`).
|
|
|
|
b.installArtifact(exe);
|
|
|
|
|
|
|
|
// This *creates* a Run step in the build graph, to be executed when another
|
|
|
|
// step is evaluated that depends on it. The next line below will establish
|
|
|
|
// such a dependency.
|
|
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
|
|
|
|
|
|
// By making the run step depend on the install step, it will be run from the
|
|
|
|
// installation directory rather than directly from within the cache directory.
|
|
|
|
// This is not necessary, however, if the application depends on other installed
|
|
|
|
// files, this ensures they will be present and in the expected location.
|
|
|
|
run_cmd.step.dependOn(b.getInstallStep());
|
|
|
|
|
|
|
|
// This allows the user to pass arguments to the application in the build
|
|
|
|
// command itself, like this: `zig build run -- arg1 arg2 etc`
|
|
|
|
if (b.args) |args| {
|
|
|
|
run_cmd.addArgs(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This creates a build step. It will be visible in the `zig build --help` menu,
|
|
|
|
// and can be selected like this: `zig build run`
|
|
|
|
// This will evaluate the `run` step rather than the default, which is "install".
|
|
|
|
const run_step = b.step("run", "Run the app");
|
|
|
|
run_step.dependOn(&run_cmd.step);
|
|
|
|
|
2024-05-15 20:16:24 +00:00
|
|
|
const universal_lambda_zig_dep = b.dependency("universal-lambda-zig", .{
|
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
|
|
|
// All modules should be added before this is called
|
|
|
|
try universal_lambda_build.configureBuild(b, exe, universal_lambda_zig_dep);
|
|
|
|
_ = universal_lambda_build.addImports(b, exe, universal_lambda_zig_dep);
|
2023-10-22 20:26:57 +00:00
|
|
|
|
2024-05-15 21:20:10 +00:00
|
|
|
const exe_aws_dep = b.dependency("aws", .{
|
2023-10-22 20:26:57 +00:00
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
2024-02-24 23:48:18 +00:00
|
|
|
const exe_aws_signing_module = exe_aws_dep.module("aws-signing");
|
|
|
|
const exe_sqlite_dep = b.dependency("sqlite", .{
|
2023-10-22 23:56:02 +00:00
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
.use_bundled = true,
|
|
|
|
});
|
2024-02-24 23:48:18 +00:00
|
|
|
const exe_sqlite_module = exe_sqlite_dep.module("sqlite");
|
2024-05-15 20:16:24 +00:00
|
|
|
exe.root_module.addImport("aws-signing", exe_aws_signing_module);
|
|
|
|
exe.root_module.addImport("sqlite", exe_sqlite_module);
|
2024-06-08 18:17:06 +00:00
|
|
|
// exe.addIncludePath(.{ .path = "c" });
|
2024-02-24 23:48:18 +00:00
|
|
|
exe.linkLibrary(exe_sqlite_dep.artifact("sqlite"));
|
2024-02-24 19:34:05 +00:00
|
|
|
// Similar to creating the run step earlier, this exposes a `test` step to
|
|
|
|
// the `zig build --help` menu, providing a way for the user to request
|
|
|
|
// running the unit tests.
|
|
|
|
const test_step = b.step("test", "Run unit tests");
|
2024-05-15 20:16:24 +00:00
|
|
|
for (test_targets) |ct| {
|
|
|
|
const t = b.resolveTargetQuery(ct);
|
2024-05-15 21:20:10 +00:00
|
|
|
const aws_dep = b.dependency("aws", .{
|
2024-02-24 23:48:18 +00:00
|
|
|
.target = t,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
|
|
|
const aws_signing_module = aws_dep.module("aws-signing");
|
|
|
|
const sqlite_dep = b.dependency("sqlite", .{
|
|
|
|
.target = t,
|
|
|
|
.optimize = optimize,
|
|
|
|
.use_bundled = true,
|
|
|
|
});
|
|
|
|
const sqlite_module = sqlite_dep.module("sqlite");
|
2024-02-24 19:34:05 +00:00
|
|
|
// Creates a step for unit testing. This only builds the test executable
|
|
|
|
// but does not run it.
|
|
|
|
const unit_tests = b.addTest(.{
|
2024-06-08 18:17:06 +00:00
|
|
|
.root_source_file = b.path("src/main.zig"),
|
2024-02-24 19:34:05 +00:00
|
|
|
.target = t,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
2024-05-15 20:16:24 +00:00
|
|
|
_ = universal_lambda_build.addImports(b, unit_tests, universal_lambda_zig_dep);
|
2024-02-24 19:34:05 +00:00
|
|
|
|
|
|
|
const run_unit_tests = b.addRunArtifact(unit_tests);
|
|
|
|
// run_unit_tests.skip_foreign_checks = true;
|
|
|
|
|
|
|
|
test_step.dependOn(&run_unit_tests.step);
|
|
|
|
|
2024-05-15 20:16:24 +00:00
|
|
|
unit_tests.root_module.addImport("aws-signing", aws_signing_module);
|
|
|
|
unit_tests.root_module.addImport("sqlite", sqlite_module);
|
2024-06-08 18:17:06 +00:00
|
|
|
// unit_tests.addIncludePath(b.path ("c" ));
|
2024-02-24 23:48:18 +00:00
|
|
|
unit_tests.linkLibrary(sqlite_dep.artifact("sqlite"));
|
2023-10-22 20:26:57 +00:00
|
|
|
}
|
2024-02-24 01:19:32 +00:00
|
|
|
|
|
|
|
var creds_step = b.step("generate_credentials", "Generate credentials for access_keys.csv");
|
|
|
|
creds_step.makeFn = generateCredentials;
|
|
|
|
}
|
|
|
|
|
2024-06-08 18:17:06 +00:00
|
|
|
fn generateCredentials(s: *std.Build.Step, prog_node: std.Progress.Node) error{ MakeFailed, MakeSkipped }!void {
|
2024-03-04 21:09:58 +00:00
|
|
|
_ = s;
|
2024-02-24 19:15:13 +00:00
|
|
|
// Account id:
|
|
|
|
// Documentation describes account id as a 12 digit number:
|
|
|
|
// https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-identifiers.html
|
2024-03-04 21:09:58 +00:00
|
|
|
// This can be a random number, but must be in a 12 digit range.
|
2024-02-24 19:15:13 +00:00
|
|
|
//
|
2024-03-04 21:09:58 +00:00
|
|
|
// The access key is 32 bit encoded, which leaves us with
|
|
|
|
// 8 * 5 = 40 bits of information to work with. The maximum value of
|
|
|
|
// a u40 in decimal is 1099511627775, a 13 digit number. So our maximum
|
|
|
|
// decimal is below, and fits into u40.
|
|
|
|
//
|
|
|
|
// Min: 0x0000000000 (0d000000000000)
|
|
|
|
// Max: 0xe8d4a50fff (0d999999999999)
|
2024-02-24 19:15:13 +00:00
|
|
|
//
|
|
|
|
// Access key:
|
2024-03-04 21:09:58 +00:00
|
|
|
// This page shows how the access key is put together:
|
|
|
|
// https://medium.com/@TalBeerySec/a-short-note-on-aws-key-id-f88cc4317489
|
|
|
|
// tl;dr
|
|
|
|
// * First 4 characters: designates type of key: We will use "ELAK" for access key
|
|
|
|
// * Next 8 characters: Account ID, base32 encoded, shifted by one bit
|
|
|
|
// * Next 8 characters: Unknown. Assume random base32, which would give us 8 * 5 = u40;
|
2024-02-24 19:15:13 +00:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Secret Access Key:
|
|
|
|
// In the wild, these are 40 characters and appear to be base64 encoded.
|
|
|
|
// Base64 encoding of 30 bytes is always exactly 40 characters and have
|
|
|
|
// no padding, which is exactly what we observe
|
2024-02-24 01:19:32 +00:00
|
|
|
_ = prog_node;
|
|
|
|
const encryption = @import("src/encryption.zig");
|
|
|
|
var key: [encryption.encoded_key_length]u8 = undefined;
|
|
|
|
encryption.randomEncodedKey(&key);
|
|
|
|
|
|
|
|
const seed = @as(u64, @truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))));
|
|
|
|
var prng = std.rand.DefaultPrng.init(seed);
|
|
|
|
var rand = prng.random();
|
2024-03-04 21:09:58 +00:00
|
|
|
const account_number = rand.intRangeAtMost(u40, 0, 999999999999); // 100000000000, 999999999999);
|
|
|
|
const access_key_random_suffix = rand.int(u39);
|
2024-03-04 22:55:46 +00:00
|
|
|
// We need the most significant bit as a 1 to make the key compatible with
|
|
|
|
// AWS. Like...you can literally send these keys to public AWS `aws sts get-access-key-info --access-key-id <blah>`
|
|
|
|
// and get your account number (after changing ELAK to AKIA!
|
|
|
|
//
|
|
|
|
// Without this bit set, AWS' sts will complain that this is not a valid key
|
|
|
|
const access_key_suffix: u80 = (1 << 79) | (@as(u80, account_number) << 39) + @as(u80, access_key_random_suffix);
|
2024-03-05 16:37:33 +00:00
|
|
|
var access_key_suffix_encoded: [16]u8 = undefined;
|
|
|
|
base32Encode(u80, access_key_suffix, &access_key_suffix_encoded);
|
2024-03-04 21:09:58 +00:00
|
|
|
// std.debug.assert(access_key_suffix_encoded.len == 16);
|
2024-02-24 01:19:32 +00:00
|
|
|
var secret_key: [30]u8 = undefined;
|
|
|
|
rand.bytes(&secret_key); // The rest don't need to be cryptographically secure...does this?
|
|
|
|
var encoded_secret: [40]u8 = undefined;
|
|
|
|
_ = std.base64.standard.Encoder.encode(&encoded_secret, secret_key[0..]);
|
|
|
|
|
2024-02-24 19:15:13 +00:00
|
|
|
const stdout_raw = std.io.getStdOut().writer();
|
|
|
|
var stdout_writer = std.io.bufferedWriter(stdout_raw);
|
|
|
|
const stdout = stdout_writer.writer();
|
2024-03-04 21:09:58 +00:00
|
|
|
// stdout.print(
|
|
|
|
// \\# account_number: {b:0>80}
|
|
|
|
// \\# random_suffix : {b:0>80}
|
|
|
|
// \\# access_key_suffix: {b:0>80}
|
|
|
|
// \\
|
|
|
|
// ,
|
|
|
|
// .{
|
|
|
|
// @as(u80, account_number) << 39,
|
|
|
|
// @as(u80, access_key_random_suffix),
|
|
|
|
// access_key_suffix,
|
|
|
|
// },
|
|
|
|
// ) catch return error.MakeFailed;
|
2024-02-24 19:15:13 +00:00
|
|
|
stdout.print(
|
2024-03-04 21:35:57 +00:00
|
|
|
"# access_key: ELAK{s}, secret_key: {s}, account_number: {d:0>12}, db_encryption_key: {s}",
|
2024-02-24 01:19:32 +00:00
|
|
|
.{
|
|
|
|
access_key_suffix_encoded,
|
|
|
|
encoded_secret,
|
|
|
|
account_number,
|
|
|
|
key,
|
|
|
|
},
|
2024-02-24 19:15:13 +00:00
|
|
|
) catch return error.MakeFailed;
|
|
|
|
stdout.print(
|
2024-03-04 21:35:57 +00:00
|
|
|
"\n#\n# You can copy/paste the following line into access_keys.csv:\nELAK{s},{s},{d:0>12},{s}\n",
|
2024-02-24 19:15:13 +00:00
|
|
|
.{
|
|
|
|
access_key_suffix_encoded,
|
|
|
|
encoded_secret,
|
|
|
|
account_number,
|
|
|
|
key,
|
|
|
|
},
|
|
|
|
) catch return error.MakeFailed;
|
|
|
|
stdout_writer.flush() catch return error.MakeFailed;
|
2024-02-24 01:19:32 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 16:37:33 +00:00
|
|
|
/// encodes an unsigned integer into base36. Caller owns the memory returned
|
2024-03-04 21:09:58 +00:00
|
|
|
pub fn base36encode(comptime T: type, allocator: std.mem.Allocator, data: T) ![]const u8 {
|
2024-02-24 01:19:32 +00:00
|
|
|
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
2024-03-04 21:09:58 +00:00
|
|
|
std.debug.assert(alphabet.len == 36);
|
2024-02-24 01:19:32 +00:00
|
|
|
const ti = @typeInfo(T);
|
|
|
|
if (ti != .Int or ti.Int.signedness != .unsigned)
|
|
|
|
@compileError("encode only works with unsigned integers");
|
|
|
|
const bits = ti.Int.bits;
|
|
|
|
// We cannot have more than 6 bits (2^6 = 64) represented per byte in our final output
|
|
|
|
var al = try std.ArrayList(u8).initCapacity(allocator, bits / 6);
|
|
|
|
defer al.deinit();
|
|
|
|
|
|
|
|
var remaining = data;
|
2024-03-04 21:09:58 +00:00
|
|
|
while (remaining > 0) : (remaining /= @as(T, @intCast(alphabet.len))) {
|
|
|
|
al.appendAssumeCapacity(alphabet[@as(usize, @intCast(remaining % alphabet.len))]);
|
2024-02-24 01:19:32 +00:00
|
|
|
}
|
|
|
|
// This is not exact, but 6 bits
|
2024-05-15 20:16:24 +00:00
|
|
|
const rc = try al.toOwnedSlice();
|
2024-02-24 01:19:32 +00:00
|
|
|
std.mem.reverse(u8, rc);
|
|
|
|
return rc;
|
2023-10-22 20:26:57 +00:00
|
|
|
}
|
2024-03-04 21:09:58 +00:00
|
|
|
|
|
|
|
/// Because Base32 is a power of 2, we can directly return an array and avoid
|
2024-03-05 16:37:33 +00:00
|
|
|
/// allocations entirely. A pointer to the output array must be bits/5 long
|
2024-03-04 21:09:58 +00:00
|
|
|
/// To trim leading 0s, simply std.mem.trimLeft(u8, encoded_data, "A");
|
2024-03-05 16:37:33 +00:00
|
|
|
pub fn base32Encode(comptime T: type, data: T, encoded: *[@typeInfo(T).Int.bits / 5]u8) void {
|
2024-03-04 21:09:58 +00:00
|
|
|
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
|
|
std.debug.assert(alphabet.len == 32);
|
|
|
|
const ti = @typeInfo(T);
|
|
|
|
if (ti != .Int or ti.Int.signedness != .unsigned)
|
|
|
|
@compileError("encode only works with unsigned integers");
|
|
|
|
const bits = ti.Int.bits;
|
|
|
|
// We will have exactly 5 bits (2^5 = 32) represented per byte in our final output
|
2024-03-05 16:37:33 +00:00
|
|
|
// var rc: [bits / 5]u8 = undefined;
|
2024-03-04 21:09:58 +00:00
|
|
|
var inx: usize = 0;
|
|
|
|
const Shift_type = @Type(.{ .Int = .{
|
|
|
|
.signedness = .unsigned,
|
|
|
|
.bits = @ceil(@log2(@as(f128, @floatFromInt(bits)))),
|
|
|
|
} });
|
|
|
|
// TODO: I think we need a table here to determine the size below
|
2024-03-05 16:37:33 +00:00
|
|
|
while (inx < encoded.len) : (inx += 1) {
|
2024-03-04 21:09:58 +00:00
|
|
|
const char_bits: u5 = @as(u5, @truncate(data >> (@as(Shift_type, @intCast(inx * 5)))));
|
2024-03-05 16:37:33 +00:00
|
|
|
encoded[encoded.len - @as(usize, @intCast(inx)) - 1] = alphabet[@as(usize, @intCast(char_bits))]; // 5 bits from inx
|
2024-03-04 21:09:58 +00:00
|
|
|
}
|
|
|
|
}
|