Compare commits

..

3 Commits

Author SHA1 Message Date
ce115ff485
complete src/main.zig zig 0.12.0 upgrade
Some checks failed
AWS-Zig Build / build (push) Has been cancelled
2024-05-15 14:21:01 -07:00
3a1208d6a9
update aws-zig dependency 2024-05-15 14:20:10 -07:00
2ac82209b9
run build before build test...in this case, build test is not a superset... 2024-05-15 14:19:27 -07:00
4 changed files with 41 additions and 18 deletions

View File

@ -17,6 +17,8 @@ jobs:
with: with:
version: 0.12.0 version: 0.12.0
- uses: elerch/zig-action-cache@v1.1.6 - uses: elerch/zig-action-cache@v1.1.6
- name: Standard build
run: zig build --verbose
- name: Run tests - name: Run tests
run: zig build test --verbose run: zig build test --verbose
- name: Build other platforms - name: Build other platforms

View File

@ -103,7 +103,7 @@ pub fn build(b: *std.Build) !void {
try universal_lambda_build.configureBuild(b, exe, universal_lambda_zig_dep); try universal_lambda_build.configureBuild(b, exe, universal_lambda_zig_dep);
_ = universal_lambda_build.addImports(b, exe, universal_lambda_zig_dep); _ = universal_lambda_build.addImports(b, exe, universal_lambda_zig_dep);
const exe_aws_dep = b.dependency("aws-zig", .{ const exe_aws_dep = b.dependency("aws", .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
@ -124,7 +124,7 @@ pub fn build(b: *std.Build) !void {
const test_step = b.step("test", "Run unit tests"); const test_step = b.step("test", "Run unit tests");
for (test_targets) |ct| { for (test_targets) |ct| {
const t = b.resolveTargetQuery(ct); const t = b.resolveTargetQuery(ct);
const aws_dep = b.dependency("aws-zig", .{ const aws_dep = b.dependency("aws", .{
.target = t, .target = t,
.optimize = optimize, .optimize = optimize,
}); });

View File

@ -11,9 +11,9 @@
.url = "https://git.lerch.org/lobo/universal-lambda-zig/archive/f3d80b4afe8c13031b6cff051e93deaeadb1d268.tar.gz", .url = "https://git.lerch.org/lobo/universal-lambda-zig/archive/f3d80b4afe8c13031b6cff051e93deaeadb1d268.tar.gz",
.hash = "122093d59b28dcd0201eaa1587ca78e11c06fb563b2cb9b554aaa5f7bab0ae34432e", .hash = "122093d59b28dcd0201eaa1587ca78e11c06fb563b2cb9b554aaa5f7bab0ae34432e",
}, },
.@"aws-zig" = .{ .aws = .{
.url = "https://git.lerch.org/api/packages/lobo/generic/aws-sdk-with-models/007f2f588aa30fd2c62d6a037fb87bace8a83710/007f2f588aa30fd2c62d6a037fb87bace8a83710-with-models.tar.gz", .url = "https://git.lerch.org/api/packages/lobo/generic/aws-sdk-with-models/c60c40f627983b35cc4ba762f510d729c80b47c3/c60c40f627983b35cc4ba762f510d729c80b47c3-with-models.tar.gz",
.hash = "1220f128554146e2f58f223a51f1839f49012294a8163a445e8320889539edcda9b3", .hash = "12208171bfd8fccdb28beab1978cf63b8557c9d210afb15ee59960342fd6b5476640",
}, },
}, },
.paths = .{ .paths = .{

View File

@ -9,8 +9,8 @@ const Account = @import("Account.zig");
const log = std.log.scoped(.dynamodb); const log = std.log.scoped(.dynamodb);
pub const std_options = struct { pub const std_options = .{
pub const log_scope_levels = &[_]std.log.ScopeLevel{.{ .scope = .aws_signing, .level = .info }}; .log_scope_levels = &.{.{ .scope = .aws_signing, .level = .info }},
}; };
pub fn main() !u8 { pub fn main() !u8 {
@ -32,12 +32,16 @@ pub fn handler(allocator: std.mem.Allocator, event_data: []const u8, context: un
var fis = std.io.fixedBufferStream(event_data); var fis = std.io.fixedBufferStream(event_data);
try authenticateUser(allocator, context, context.request.target, context.request.headers, fis.reader()); try authenticateUser(allocator, context, context.request.target, context.request.headers, fis.reader());
try setContentType(&context.headers, "application/x-amz-json-1.0", false); try setContentType(allocator, &context.headers, "application/x-amz-json-1.0", false);
// https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html#API_CreateTable_Examples // https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html#API_CreateTable_Examples
// Operation is in X-Amz-Target // Operation is in X-Amz-Target
// event_data is json // event_data is json
// X-Amz-Target: DynamoDB_20120810.CreateTable // X-Amz-Target: DynamoDB_20120810.CreateTable
const target_value_or_null = context.request.headers.getFirstValue("X-Amz-Target"); const target_value_or_null = blk: {
for (context.request.headers) |h|
if (std.ascii.eqlIgnoreCase(h.name, "X-Amz-Target")) break :blk h.value;
break :blk null;
};
const target_value = if (target_value_or_null) |t| t else { const target_value = if (target_value_or_null) |t| t else {
context.status = .bad_request; context.status = .bad_request;
context.reason = "Missing X-Amz-Target header"; context.reason = "Missing X-Amz-Target header";
@ -76,12 +80,24 @@ pub fn handler(allocator: std.mem.Allocator, event_data: []const u8, context: un
context.status = .bad_request; context.status = .bad_request;
return error.OperationUnsupported; return error.OperationUnsupported;
} }
fn setContentType(headers: *std.http.Headers, content_type: []const u8, overwrite: bool) !void { fn setContentType(allocator: std.mem.Allocator, headers: *[]const std.http.Header, content_type: []const u8, overwrite: bool) !void {
if (headers.contains("content-type")) { for (headers.*, 0..) |h, i| {
if (!overwrite) return; if (std.ascii.eqlIgnoreCase(h.name, "content-type")) {
_ = headers.delete("content-type"); if (overwrite) {
const new_headers = try allocator.dupe(std.http.Header, headers.*);
errdefer allocator.free(new_headers);
new_headers[i] = .{ .name = "Content-Type", .value = content_type };
headers.* = new_headers;
} }
try headers.append("Content-Type", content_type); return;
}
}
// need to add to the array
const new_headers = try allocator.alloc(std.http.Header, headers.len + 1);
errdefer allocator.free(new_headers);
@memcpy(new_headers[0..headers.len], headers.*);
new_headers[new_headers.len - 1] = .{ .name = "Content-Type", .value = content_type };
headers.* = new_headers;
} }
fn executeOperation( fn executeOperation(
request: *AuthenticatedRequest, request: *AuthenticatedRequest,
@ -98,8 +114,9 @@ fn executeOperation(
return err; return err;
}; };
} }
fn authenticateUser(allocator: std.mem.Allocator, context: universal_lambda_interface.Context, target: []const u8, headers: std.http.Headers, body_reader: anytype) !void { fn authenticateUser(allocator: std.mem.Allocator, context: universal_lambda_interface.Context, target: []const u8, headers: []const std.http.Header, body_reader: anytype) !void {
const request = signing.UnverifiedRequest{ const request = signing.UnverifiedRequest{
.allocator = allocator,
.method = std.http.Method.POST, .method = std.http.Method.POST,
.target = target, .target = target,
.headers = headers, .headers = headers,
@ -215,7 +232,7 @@ fn fillRootCreds(allocator: std.mem.Allocator) !void {
} }
const NullAllocator = struct { const NullAllocator = struct {
const thing = 0; const thing: u8 = 0;
const vtable = std.mem.Allocator.VTable{ const vtable = std.mem.Allocator.VTable{
.alloc = alloc, .alloc = alloc,
.resize = resize, .resize = resize,
@ -268,8 +285,12 @@ fn accountForAccessKey(allocator: std.mem.Allocator, access_key: []const u8) !u4
/// Function assumes an authenticated request, so signing.verify must be called /// Function assumes an authenticated request, so signing.verify must be called
/// and returned true before calling this function. If authentication header /// and returned true before calling this function. If authentication header
/// is not found, environment variable will be used /// is not found, environment variable will be used
fn accountId(allocator: std.mem.Allocator, headers: std.http.Headers) !u40 { fn accountId(allocator: std.mem.Allocator, headers: []const std.http.Header) !u40 {
const auth_header = headers.getFirstValue("Authorization"); const auth_header = blk: {
for (headers) |h|
if (std.ascii.eqlIgnoreCase(h.name, "Authorization")) break :blk h.value;
break :blk null;
};
if (auth_header) |h| { if (auth_header) |h| {
// AWS4-HMAC-SHA256 Credential=ACCESS/20230908/us-west-2/s3/aws4_request, SignedHeaders=accept;content-length;content-type;host;x-amz-content-sha256;x-amz-date;x-amz-storage-class, Signature=fcc43ce73a34c9bd1ddf17e8a435f46a859812822f944f9eeb2aabcd64b03523 // AWS4-HMAC-SHA256 Credential=ACCESS/20230908/us-west-2/s3/aws4_request, SignedHeaders=accept;content-length;content-type;host;x-amz-content-sha256;x-amz-date;x-amz-storage-class, Signature=fcc43ce73a34c9bd1ddf17e8a435f46a859812822f944f9eeb2aabcd64b03523
const start = std.mem.indexOf(u8, h, "Credential=").? + "Credential=".len; const start = std.mem.indexOf(u8, h, "Credential=").? + "Credential=".len;