From 8361431754c0aa6220af4be75b229c968f56d5af Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Sun, 27 Aug 2023 15:24:58 -0700 Subject: [PATCH] clean up/add credential handling --- src/aws.zig | 21 ++++++++++++++++----- src/aws_credentials.zig | 6 ++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/aws.zig b/src/aws.zig index 98cafa2..1d735f0 100644 --- a/src/aws.zig +++ b/src/aws.zig @@ -1339,8 +1339,8 @@ const TestOptions = struct { fn deinit(self: Self) void { if (self.request_body.len > 0) self.allocator.free(self.request_body); - // if (self.test_server_runtime_uri) |_| - // self.allocator.free(self.test_server_runtime_uri.?); + if (self.test_server_runtime_uri) |_| + self.allocator.free(self.test_server_runtime_uri.?); } }; @@ -1357,7 +1357,6 @@ fn threadMain(options: *TestOptions) !void { options.server_port = server.socket.listen_address.in.getPort(); options.test_server_runtime_uri = try std.fmt.allocPrint(options.allocator, "http://127.0.0.1:{d}", .{options.server_port.?}); - defer options.allocator.free(options.test_server_runtime_uri.?); log.debug("server listening at {s}", .{options.test_server_runtime_uri.?}); defer server.deinit(); log.info("starting server thread, tid {d}", .{std.Thread.getCurrentId()}); @@ -1440,8 +1439,6 @@ fn serve(options: *TestOptions, res: *std.http.Server.Response) ![]const u8 { test "sts get_caller_identity comptime" { // std.testing.log_level = .debug; const allocator = std.testing.allocator; - // [debug] (awshttp): x-amzn-RequestId: 8f0d54da-1230-40f7-b4ac-95015c4b84cd - // [debug] (awshttp): Content-Type: application/json var requestOptions: TestOptions = .{ .allocator = allocator, .server_response = @@ -1463,6 +1460,20 @@ test "sts get_caller_identity comptime" { awshttp.endpoint_override = requestOptions.test_server_runtime_uri; defer awshttp.endpoint_override = null; + + const creds = @import("aws_authentication.zig").Credentials.init( + allocator, + try allocator.dupe(u8, "ACCESS"), + try allocator.dupe(u8, "SECRET"), + null, + ); + const aws_creds = @import("aws_credentials.zig"); + aws_creds.static_credentials = creds; + defer { + // creds.deinit(); Creds will get deinited in the course of the call. We don't want to do it twice + aws_creds.static_credentials = null; // we do need to reset the static creds for the next user though + } + var client = try Client.init(allocator, .{}); const options = Options{ .region = "us-west-2", diff --git a/src/aws_credentials.zig b/src/aws_credentials.zig index 5febea5..4a63bfe 100644 --- a/src/aws_credentials.zig +++ b/src/aws_credentials.zig @@ -4,6 +4,9 @@ //! 3. Credentials/config files //! 4. ECS Container credentials, using AWS_CONTAINER_CREDENTIALS_RELATIVE_URI //! 5. EC2 instance profile credentials +//! +//! For testing purposes, you can also set the static credentials object, which +//! will override all of the above const std = @import("std"); const builtin = @import("builtin"); const auth = @import("aws_authentication.zig"); @@ -23,7 +26,10 @@ pub const Options = struct { profile: Profile = .{}, }; +pub var static_credentials: ?auth.Credentials = null; + pub fn getCredentials(allocator: std.mem.Allocator, options: Options) !auth.Credentials { + if (static_credentials) |c| return c; if (try getEnvironmentCredentials(allocator)) |cred| { log.debug("Found credentials in environment. Access key: {s}", .{cred.access_key}); return cred;