get password from file

This commit is contained in:
Jeffrey C. Ollie 2023-10-14 22:16:04 -05:00
parent b93db148f6
commit 50ac6f1310
Signed by: jeff
GPG key ID: 6F86035A6D97044E
2 changed files with 16 additions and 10 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@
/zig-cache
/zig-out
/config.json
/password

View file

@ -90,15 +90,29 @@ pub fn main() !void {
const b64 = std.base64.standard.Encoder;
var password_buffer: [128]u8 = undefined;
var password: []const u8 = undefined;
if (config.value.loki.password_file) |password_file_path| {
password = try std.fs.cwd().readFile(password_file_path, &password_buffer);
password = std.mem.trimRight(u8, password, "\r\n");
} else if (config.value.loki.password) |password_data| {
@memcpy(&password_buffer, password_data);
password = password_buffer[0..password_data.len];
} else {
iperf3_log.err("unable to determine password!", .{});
return;
}
var auth_buf: [256]u8 = undefined;
const auth = try std.fmt.bufPrint(
&auth_buf,
"{s}:{s}",
.{
config.value.loki.username,
config.value.loki.password.?,
password,
},
);
var auth_encoded_buf: [b64.calcSize(auth_buf.len)]u8 = undefined;
var auth_encoded = b64.encode(&auth_encoded_buf, auth);
var auth_header_buf: [256]u8 = undefined;
@ -106,18 +120,9 @@ pub fn main() !void {
const uri = try std.Uri.parse(config.value.loki.url);
// var headers = std.http.Headers{ .allocator = allocator };
// try headers.append("Authorization", auth_header);
// try headers.append("Content-Type", "application/json");
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();
// const stderr_file = std.io.getStdErr().writer();
// var stderr_bw = std.io.bufferedWriter(stderr_file);
// const stderr = stderr_bw.writer();
// _ = stderr;
var port_buf: [16]u8 = undefined;
var port: []u8 = undefined;
if (config.value.iperf3.port) |p| {