Compare commits

..

No commits in common. "9c403d0e18f8328c116e4fd61210f2ac3f14335d" and "591f4c43e095f582228907a101fe46c91f9128d6" have entirely different histories.

3 changed files with 64 additions and 135 deletions

View File

@ -27,19 +27,18 @@ pub const services = servicemodel.services;
/// This will give you a constant with service data for sts, ec2, s3 and ddb only /// This will give you a constant with service data for sts, ec2, s3 and ddb only
pub const Services = servicemodel.Services; pub const Services = servicemodel.Services;
pub const ClientOptions = struct { pub const ClientOptions = struct {};
proxy: ?std.http.Client.HttpProxy = null,
};
pub const Client = struct { pub const Client = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
aws_http: awshttp.AwsHttp, aws_http: awshttp.AwsHttp,
const Self = @This(); const Self = @This();
pub fn init(allocator: std.mem.Allocator, options: ClientOptions) Self { pub fn init(allocator: std.mem.Allocator, options: ClientOptions) !Self {
_ = options;
return Self{ return Self{
.allocator = allocator, .allocator = allocator,
.aws_http = awshttp.AwsHttp.init(allocator, options.proxy), .aws_http = try awshttp.AwsHttp.init(allocator),
}; };
} }
pub fn deinit(self: *Client) void { pub fn deinit(self: *Client) void {
@ -479,13 +478,6 @@ pub fn Request(comptime request_action: anytype) type {
const xml_options = xml_shaper.ParseOptions{ .allocator = options.client.allocator }; const xml_options = xml_shaper.ParseOptions{ .allocator = options.client.allocator };
var body: []const u8 = result.body; var body: []const u8 = result.body;
var free_body = false; var free_body = false;
if (result.body.len < 20) {
std.log.err(
"Unexpected response from server. Looking for XML that ends in 'Response' or 'Result'. Found:\n{s}␃\n===",
.{result.body},
);
return error.UnexpectedResponse;
}
if (std.mem.lastIndexOf(u8, result.body[result.body.len - 20 ..], "Response>") == null and if (std.mem.lastIndexOf(u8, result.body[result.body.len - 20 ..], "Response>") == null and
std.mem.lastIndexOf(u8, result.body[result.body.len - 20 ..], "Result>") == null) std.mem.lastIndexOf(u8, result.body[result.body.len - 20 ..], "Result>") == null)
{ {
@ -1330,7 +1322,6 @@ const TestOptions = struct {
server_remaining_requests: usize = 1, server_remaining_requests: usize = 1,
server_response: []const u8 = "unset", server_response: []const u8 = "unset",
server_response_headers: [][2][]const u8 = &[_][2][]const u8{}, server_response_headers: [][2][]const u8 = &[_][2][]const u8{},
server_response_transfer_encoding: ?std.http.TransferEncoding = null,
request_body: []u8 = "", request_body: []u8 = "",
request_method: std.http.Method = undefined, request_method: std.http.Method = undefined,
request_target: []const u8 = undefined, request_target: []const u8 = undefined,
@ -1446,11 +1437,7 @@ fn processRequest(options: *TestOptions, server: *std.http.Server) !void {
} }
break :brk "Unexpected error generating request to lambda"; break :brk "Unexpected error generating request to lambda";
}; };
if (options.server_response_transfer_encoding == null) res.transfer_encoding = .{ .content_length = response_bytes.len };
res.transfer_encoding = .{ .content_length = response_bytes.len }
else
res.transfer_encoding = .chunked;
try res.do(); try res.do();
_ = try res.writer().writeAll(response_bytes); _ = try res.writer().writeAll(response_bytes);
try res.finish(); try res.finish();
@ -1509,7 +1496,7 @@ const TestSetup = struct {
null, null,
); );
aws_creds.static_credentials = self.creds; aws_creds.static_credentials = self.creds;
var client = Client.init(self.allocator, .{}); var client = try Client.init(self.allocator, .{});
self.client = &client; self.client = &client;
return .{ return .{
.region = "us-west-2", .region = "us-west-2",
@ -1811,7 +1798,6 @@ test "ec2_query_no_input: EC2 describe regions" {
.{ "Content-Type", "text/xml;charset=UTF-8" }, .{ "Content-Type", "text/xml;charset=UTF-8" },
.{ "x-amzn-RequestId", "4cdbdd69-800c-49b5-8474-ae4c17709782" }, .{ "x-amzn-RequestId", "4cdbdd69-800c-49b5-8474-ae4c17709782" },
}), }),
.server_response_transfer_encoding = .chunked,
}); });
defer test_harness.deinit(); defer test_harness.deinit();
const options = try test_harness.start(); const options = try test_harness.start();

View File

@ -61,14 +61,14 @@ const EndPoint = struct {
}; };
pub const AwsHttp = struct { pub const AwsHttp = struct {
allocator: std.mem.Allocator, allocator: std.mem.Allocator,
proxy: ?std.http.Client.HttpProxy,
const Self = @This(); const Self = @This();
pub fn init(allocator: std.mem.Allocator, proxy: ?std.http.Client.HttpProxy) Self { /// Recommend usage is init(allocator, awshttp.default_root_ca)
/// Passing null for root_pem will result in no TLS verification
pub fn init(allocator: std.mem.Allocator) !Self {
return Self{ return Self{
.allocator = allocator, .allocator = allocator,
.proxy = proxy,
// .credentialsProvider = // creds provider could be useful // .credentialsProvider = // creds provider could be useful
}; };
} }
@ -171,7 +171,7 @@ pub const AwsHttp = struct {
const url = try std.fmt.allocPrint(self.allocator, "{s}{s}{s}", .{ endpoint.uri, request_cp.path, request_cp.query }); const url = try std.fmt.allocPrint(self.allocator, "{s}{s}{s}", .{ endpoint.uri, request_cp.path, request_cp.query });
defer self.allocator.free(url); defer self.allocator.free(url);
log.debug("Request url: {s}", .{url}); log.debug("Request url: {s}", .{url});
var cl = std.http.Client{ .allocator = self.allocator, .proxy = self.proxy }; var cl = std.http.Client{ .allocator = self.allocator };
defer cl.deinit(); // TODO: Connection pooling defer cl.deinit(); // TODO: Connection pooling
// //
// var req = try zfetch.Request.init(self.allocator, url, self.trust_chain); // var req = try zfetch.Request.init(self.allocator, url, self.trust_chain);
@ -211,16 +211,9 @@ pub const AwsHttp = struct {
content_length = std.fmt.parseInt(usize, h.value, 10) catch 0; content_length = std.fmt.parseInt(usize, h.value, 10) catch 0;
} }
var response_data: []u8 = var response_data = try self.allocator.alloc(u8, content_length);
if (req.response.transfer_encoding) |_| // the only value here is "chunked" errdefer self.allocator.free(response_data);
try req.reader().readAllAlloc(self.allocator, std.math.maxInt(usize)) _ = try req.readAll(response_data);
else blk: {
// content length
var tmp_data = try self.allocator.alloc(u8, content_length);
errdefer self.allocator.free(tmp_data);
_ = try req.readAll(tmp_data);
break :blk tmp_data;
};
log.debug("raw response body:\n{s}", .{response_data}); log.debug("raw response body:\n{s}", .{response_data});
const rc = HttpResult{ const rc = HttpResult{

View File

@ -38,9 +38,6 @@ pub fn log(
nosuspend stderr.print(prefix ++ format ++ "\n", args) catch return; nosuspend stderr.print(prefix ++ format ++ "\n", args) catch return;
} }
pub const std_options = struct {
pub const logFn = log;
};
const Tests = enum { const Tests = enum {
query_no_input, query_no_input,
query_with_input, query_with_input,
@ -66,30 +63,7 @@ pub fn main() anyerror!void {
defer tests.deinit(); defer tests.deinit();
var args = try std.process.argsWithAllocator(allocator); var args = try std.process.argsWithAllocator(allocator);
defer args.deinit(); defer args.deinit();
const stdout_raw = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_raw);
defer bw.flush() catch unreachable;
const stdout = bw.writer();
var arg0: ?[]const u8 = null;
var proxy: ?std.http.Client.HttpProxy = null;
while (args.next()) |arg| { while (args.next()) |arg| {
if (arg0 == null) arg0 = arg;
if (std.mem.eql(u8, "-h", arg) or std.mem.eql(u8, "--help", arg)) {
try stdout.print(
\\usage: {?s} [-h|--help] [-v][-v][-v] [-x|--proxy <proxy url>] [tests...]
\\
\\Where tests are one of the following:
\\
, .{arg0});
inline for (std.meta.fields(Tests)) |enumfield| {
try stdout.print("* {s}\n", .{enumfield.name});
}
return;
}
if (std.mem.eql(u8, "-x", arg) or std.mem.eql(u8, "--proxy", arg)) {
proxy = try proxyFromString(args.next().?); // parse stuff
continue;
}
if (std.mem.eql(u8, "-v", arg)) { if (std.mem.eql(u8, "-v", arg)) {
verbose += 1; verbose += 1;
continue; continue;
@ -107,8 +81,7 @@ pub fn main() anyerror!void {
} }
std.log.info("Start\n", .{}); std.log.info("Start\n", .{});
const client_options = aws.ClientOptions{ .proxy = proxy }; var client = try aws.Client.init(allocator, .{});
var client = aws.Client.init(allocator, client_options);
const options = aws.Options{ const options = aws.Options{
.region = "us-west-2", .region = "us-west-2",
.client = client, .client = client,
@ -126,17 +99,17 @@ pub fn main() anyerror!void {
const call = try aws.Request(services.sts.get_caller_identity).call(.{}, options); const call = try aws.Request(services.sts.get_caller_identity).call(.{}, options);
// const call = try client.call(services.sts.get_caller_identity.Request{}, options); // const call = try client.call(services.sts.get_caller_identity.Request{}, options);
defer call.deinit(); defer call.deinit();
std.log.info("arn: {s}", .{call.response.arn.?}); std.log.info("arn: {any}", .{call.response.arn});
std.log.info("id: {s}", .{call.response.user_id.?}); std.log.info("id: {any}", .{call.response.user_id});
std.log.info("account: {s}", .{call.response.account.?}); std.log.info("account: {any}", .{call.response.account});
std.log.info("requestId: {s}", .{call.response_metadata.request_id}); std.log.info("requestId: {any}", .{call.response_metadata.request_id});
}, },
.query_with_input => { .query_with_input => {
const call = try client.call(services.sqs.list_queues.Request{ const call = try client.call(services.sqs.list_queues.Request{
.queue_name_prefix = "s", .queue_name_prefix = "s",
}, options); }, options);
defer call.deinit(); defer call.deinit();
std.log.info("request id: {s}", .{call.response_metadata.request_id}); std.log.info("request id: {any}", .{call.response_metadata.request_id});
std.log.info("account has queues with prefix 's': {}", .{call.response.queue_urls != null}); std.log.info("account has queues with prefix 's': {}", .{call.response.queue_urls != null});
}, },
.json_1_0_query_with_input => { .json_1_0_query_with_input => {
@ -144,7 +117,7 @@ pub fn main() anyerror!void {
.limit = 1, .limit = 1,
}, options); }, options);
defer call.deinit(); defer call.deinit();
std.log.info("request id: {s}", .{call.response_metadata.request_id}); std.log.info("request id: {any}", .{call.response_metadata.request_id});
std.log.info("account has tables: {}", .{call.response.table_names.?.len > 0}); std.log.info("account has tables: {}", .{call.response.table_names.?.len > 0});
}, },
.json_1_0_query_no_input => { .json_1_0_query_no_input => {
@ -157,13 +130,13 @@ pub fn main() anyerror!void {
.max_results = 1, .max_results = 1,
}, options); }, options);
defer call.deinit(); defer call.deinit();
std.log.info("request id: {s}", .{call.response_metadata.request_id}); std.log.info("request id: {any}", .{call.response_metadata.request_id});
std.log.info("account has clusters: {}", .{call.response.cluster_arns.?.len > 0}); std.log.info("account has clusters: {}", .{call.response.cluster_arns.?.len > 0});
}, },
.json_1_1_query_no_input => { .json_1_1_query_no_input => {
const call = try client.call(services.ecs.list_clusters.Request{}, options); const call = try client.call(services.ecs.list_clusters.Request{}, options);
defer call.deinit(); defer call.deinit();
std.log.info("request id: {s}", .{call.response_metadata.request_id}); std.log.info("request id: {any}", .{call.response_metadata.request_id});
std.log.info("account has clusters: {}", .{call.response.cluster_arns.?.len > 0}); std.log.info("account has clusters: {}", .{call.response.cluster_arns.?.len > 0});
}, },
.rest_json_1_query_with_input => { .rest_json_1_query_with_input => {
@ -171,51 +144,48 @@ pub fn main() anyerror!void {
.max_items = 1, .max_items = 1,
}, options); }, options);
defer call.deinit(); defer call.deinit();
std.log.info("request id: {s}", .{call.response_metadata.request_id}); std.log.info("request id: {any}", .{call.response_metadata.request_id});
std.log.info("account has functions: {}", .{call.response.functions.?.len > 0}); std.log.info("account has functions: {}", .{call.response.functions.?.len > 0});
}, },
.rest_json_1_query_no_input => { .rest_json_1_query_no_input => {
const call = try client.call(services.lambda.list_functions.Request{}, options); const call = try client.call(services.lambda.list_functions.Request{}, options);
defer call.deinit(); defer call.deinit();
std.log.info("request id: {s}", .{call.response_metadata.request_id}); std.log.info("request id: {any}", .{call.response_metadata.request_id});
std.log.info("account has functions: {}", .{call.response.functions.?.len > 0}); std.log.info("account has functions: {}", .{call.response.functions.?.len > 0});
}, },
.rest_json_1_work_with_lambda => { .rest_json_1_work_with_lambda => {
// const call = try client.call(services.lambda.list_functions.Request{}, options); const call = try client.call(services.lambda.list_functions.Request{}, options);
// defer call.deinit(); defer call.deinit();
// std.log.info("list request id: {s}", .{call.response_metadata.request_id}); std.log.info("list request id: {any}", .{call.response_metadata.request_id});
// if (call.response.functions) |fns| { if (call.response.functions) |fns| {
// if (fns.len > 0) { if (fns.len > 0) {
// const func = fns[0]; const func = fns[0];
// const arn = func.function_arn.?; const arn = func.function_arn.?;
// // This is a bit ugly. Maybe a helper function in the library would help? // This is a bit ugly. Maybe a helper function in the library would help?
// var tags = try std.ArrayList(@typeInfo(try typeForField(services.lambda.tag_resource.Request, "tags")).Pointer.child).initCapacity(allocator, 1); var tags = try std.ArrayList(@typeInfo(try typeForField(services.lambda.tag_resource.Request, "tags")).Pointer.child).initCapacity(allocator, 1);
// defer tags.deinit(); defer tags.deinit();
// tags.appendAssumeCapacity(.{ .key = "Foo", .value = "Bar" }); tags.appendAssumeCapacity(.{ .key = "Foo", .value = "Bar" });
// const req = services.lambda.tag_resource.Request{ .resource = arn, .tags = tags.items }; const req = services.lambda.tag_resource.Request{ .resource = arn, .tags = tags.items };
// const addtag = try aws.Request(services.lambda.tag_resource).call(req, options); const addtag = try aws.Request(services.lambda.tag_resource).call(req, options);
// TODO: Something is up with signature calculation. I believe it's with the encoding, because the url used defer addtag.deinit();
// here is totally crazy with the arn of the resource directly in it // const addtag = try client.call(services.lambda.tag_resource.Request{ .resource = arn, .tags = &.{.{ .key = "Foo", .value = "Bar" }} }, options);
// Example: https://lambda.us-west-2.amazonaws.com/2017-03-31/tags/arn%253Aaws%253Alambda%253Aus-west-2%253A550620852718%253Afunction%253ADevelopmentFrontendStack--amplifyassetdeploymentha-aZqB9IbZLIKU std.log.info("add tag request id: {any}", .{addtag.response_metadata.request_id});
// defer addtag.deinit(); var keys = [_][]const u8{"Foo"}; // Would love to have a way to express this without burning a var here
// // const addtag = try client.call(services.lambda.tag_resource.Request{ .resource = arn, .tags = &.{.{ .key = "Foo", .value = "Bar" }} }, options); const deletetag = try aws.Request(services.lambda.untag_resource).call(.{ .tag_keys = keys[0..], .resource = arn }, options);
// std.log.info("add tag request id: {s}", .{addtag.response_metadata.request_id}); defer deletetag.deinit();
// var keys = [_][]const u8{"Foo"}; // Would love to have a way to express this without burning a var here std.log.info("delete tag request id: {any}", .{deletetag.response_metadata.request_id});
// const deletetag = try aws.Request(services.lambda.untag_resource).call(.{ .tag_keys = keys[0..], .resource = arn }, options); } else {
// defer deletetag.deinit(); std.log.err("no functions to work with", .{});
// std.log.info("delete tag request id: {s}", .{deletetag.response_metadata.request_id}); }
// } else { } else {
// std.log.err("no functions to work with", .{}); std.log.err("no functions to work with", .{});
// } }
// } else {
// std.log.err("no functions to work with", .{});
// }
}, },
.ec2_query_no_input => { .ec2_query_no_input => {
// Describe regions is a simpler request and easier to debug // Describe regions is a simpler request and easier to debug
const result = try client.call(services.ec2.describe_regions.Request{}, options); const result = try client.call(services.ec2.describe_regions.Request{}, options);
defer result.deinit(); defer result.deinit();
std.log.info("request id: {s}", .{result.response_metadata.request_id}); std.log.info("request id: {any}", .{result.response_metadata.request_id});
std.log.info("region count: {d}", .{result.response.regions.?.len}); std.log.info("region count: {d}", .{result.response.regions.?.len});
}, },
.ec2_query_with_input => { .ec2_query_with_input => {
@ -253,13 +223,13 @@ pub fn main() anyerror!void {
.rest_xml_no_input => { .rest_xml_no_input => {
const result = try client.call(services.s3.list_buckets.Request{}, options); const result = try client.call(services.s3.list_buckets.Request{}, options);
defer result.deinit(); defer result.deinit();
std.log.info("request id: {s}", .{result.response_metadata.request_id}); std.log.info("request id: {any}", .{result.response_metadata.request_id});
std.log.info("bucket count: {d}", .{result.response.buckets.?.len}); std.log.info("bucket count: {d}", .{result.response.buckets.?.len});
}, },
.rest_xml_anything_but_s3 => { .rest_xml_anything_but_s3 => {
const result = try client.call(services.cloudfront.list_key_groups.Request{}, options); const result = try client.call(services.cloudfront.list_key_groups.Request{}, options);
defer result.deinit(); defer result.deinit();
std.log.info("request id: {s}", .{result.response_metadata.request_id}); std.log.info("request id: {any}", .{result.response_metadata.request_id});
const list = result.response.key_group_list.?; const list = result.response.key_group_list.?;
std.log.info("key group list max: {?d}", .{list.max_items}); std.log.info("key group list max: {?d}", .{list.max_items});
std.log.info("key group quantity: {d}", .{list.quantity}); std.log.info("key group quantity: {d}", .{list.quantity});
@ -272,8 +242,8 @@ pub fn main() anyerror!void {
const result = try client.call(services.s3.list_buckets.Request{}, options); const result = try client.call(services.s3.list_buckets.Request{}, options);
defer result.deinit(); defer result.deinit();
const bucket = result.response.buckets.?[result.response.buckets.?.len - 1]; const bucket = result.response.buckets.?[result.response.buckets.?.len - 1];
std.log.info("ListBuckets request id: {s}", .{result.response_metadata.request_id}); std.log.info("ListBuckets request id: {any}", .{result.response_metadata.request_id});
std.log.info("bucket name: {s}", .{bucket.name.?}); std.log.info("bucket name: {any}", .{bucket.name.?});
break :blk try allocator.dupe(u8, bucket.name.?); break :blk try allocator.dupe(u8, bucket.name.?);
}; };
defer allocator.free(bucket); defer allocator.free(bucket);
@ -283,8 +253,8 @@ pub fn main() anyerror!void {
}, options); }, options);
defer result.deinit(); defer result.deinit();
const location = result.response.location_constraint.?; const location = result.response.location_constraint.?;
std.log.info("GetBucketLocation request id: {s}", .{result.response_metadata.request_id}); std.log.info("GetBucketLocation request id: {any}", .{result.response_metadata.request_id});
std.log.info("location: {s}", .{location}); std.log.info("location: {any}", .{location});
break :blk try allocator.dupe(u8, location); break :blk try allocator.dupe(u8, location);
}; };
defer allocator.free(location); defer allocator.free(location);
@ -301,8 +271,8 @@ pub fn main() anyerror!void {
.body = "bar", .body = "bar",
.storage_class = "STANDARD", .storage_class = "STANDARD",
}, s3opts); }, s3opts);
std.log.info("PutObject Request id: {s}", .{result.response_metadata.request_id}); std.log.info("PutObject Request id: {any}", .{result.response_metadata.request_id});
std.log.info("PutObject etag: {s}", .{result.response.e_tag.?}); std.log.info("PutObject etag: {any}", .{result.response.e_tag.?});
defer result.deinit(); defer result.deinit();
} }
{ {
@ -312,9 +282,9 @@ pub fn main() anyerror!void {
.bucket = bucket, .bucket = bucket,
.key = key, .key = key,
}, s3opts); }, s3opts);
std.log.info("GetObject Request id: {s}", .{result.response_metadata.request_id}); std.log.info("GetObject Request id: {any}", .{result.response_metadata.request_id});
std.log.info("GetObject Body: {s}", .{result.response.body.?}); std.log.info("GetObject Body: {any}", .{result.response.body});
std.log.info("GetObject etag: {s}", .{result.response.e_tag.?}); std.log.info("GetObject etag: {any}", .{result.response.e_tag.?});
std.log.info("GetObject last modified (seconds since epoch): {d}", .{result.response.last_modified.?}); std.log.info("GetObject last modified (seconds since epoch): {d}", .{result.response.last_modified.?});
defer result.deinit(); defer result.deinit();
} }
@ -323,14 +293,14 @@ pub fn main() anyerror!void {
.bucket = bucket, .bucket = bucket,
.key = key, .key = key,
}, s3opts); }, s3opts);
std.log.info("DeleteObject Request id: {s}", .{result.response_metadata.request_id}); std.log.info("DeleteObject Request id: {any}", .{result.response_metadata.request_id});
defer result.deinit(); defer result.deinit();
} }
{ {
const result = try aws.Request(services.s3.list_objects).call(.{ const result = try aws.Request(services.s3.list_objects).call(.{
.bucket = bucket, .bucket = bucket,
}, s3opts); }, s3opts);
std.log.info("ListObject Request id: {s}", .{result.response_metadata.request_id}); std.log.info("ListObject Request id: {any}", .{result.response_metadata.request_id});
std.log.info("Object count: {d}", .{result.response.contents.?.len}); std.log.info("Object count: {d}", .{result.response.contents.?.len});
defer result.deinit(); defer result.deinit();
} }
@ -351,26 +321,6 @@ pub fn main() anyerror!void {
std.log.info("===== Tests complete =====", .{}); std.log.info("===== Tests complete =====", .{});
} }
fn proxyFromString(string: []const u8) !std.http.Client.HttpProxy {
var rc = std.http.Client.HttpProxy{
.protocol = undefined,
.host = undefined,
};
var remaining: []const u8 = string;
if (std.mem.startsWith(u8, string, "http://")) {
remaining = remaining["http://".len..];
rc.protocol = .plain;
} else if (std.mem.startsWith(u8, string, "https://")) {
remaining = remaining["https://".len..];
rc.protocol = .tls;
} else return error.InvalidScheme;
var split_iterator = std.mem.split(u8, remaining, ":");
rc.host = std.mem.trimRight(u8, split_iterator.first(), "/");
if (split_iterator.next()) |port|
rc.port = try std.fmt.parseInt(u16, port, 10);
return rc;
}
fn typeForField(comptime T: type, comptime field_name: []const u8) !type { fn typeForField(comptime T: type, comptime field_name: []const u8) !type {
const ti = @typeInfo(T); const ti = @typeInfo(T);
switch (ti) { switch (ti) {