Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ If you need these, use MinIO or AWS.
## Quick Start

```bash
zig build -Doptimize=ReleaseFast
zig build -Doptimize=ReleaseFast -Dacl-list="admin:minioadmin:minioadmin"
./zig-out/bin/zs3
```

Expand Down Expand Up @@ -146,8 +146,6 @@ Edit `main.zig`:
const ctx = S3Context{
.allocator = allocator,
.data_dir = "data",
.access_key = "minioadmin",
.secret_key = "minioadmin",
};

const address = net.Address.parseIp4("0.0.0.0", 9000)
Expand Down
72 changes: 72 additions & 0 deletions acl.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const std = @import("std");

pub const Role = enum {
Admin,
Reader,
Writer,
Unknown,
};

pub const Credential = struct {
access_key: []const u8,
secret_key: []const u8,
role: Role,
};

// Function to convert string to Role enum
pub fn stringToRole(role_str: []const u8) !Role {
if (std.mem.eql(u8, role_str, "admin")) {
return Role.Admin;
} else if (std.mem.eql(u8, role_str, "reader")) {
return Role.Reader;
} else if (std.mem.eql(u8, role_str, "writer")) {
return Role.Writer;
} else {
return error.BadCredentialRole;
}
}

// Function to parse a single credential string: "role:access_key:secret_key"
pub fn parseCredential(cred_str: []const u8) !Credential {
var itr = std.mem.splitScalar(u8, cred_str, ':');

const role_str = itr.next() orelse return error.BadCredentialFormat;
const access_key = itr.next() orelse return error.BadCredentialFormat;
const secret_key = itr.next() orelse return error.BadCredentialFormat;

// Reject extra fields
if (itr.next() != null) return error.BadCredentialFormat;

// Reject empty fields
if (role_str.len == 0 or access_key.len == 0 or secret_key.len == 0)
return error.BadCredentialFormat;

return Credential{
.role = try stringToRole(role_str),
.access_key = access_key,
.secret_key = secret_key,
};
}

// Function to parse a list of credential strings
pub fn parseCredentials(allocator: std.mem.Allocator, input: []const u8) ![]Credential {
if (input.len == 0) {
return error.BadCredentialInputFormat;
}

var credentials = std.ArrayListUnmanaged(Credential){};
errdefer credentials.deinit(allocator);

var itr = std.mem.splitScalar(u8, input, ',');
while (itr.next()) |record| {
if (record.len == 0) continue;

const pc = try parseCredential(record);

try credentials.append(allocator, pc);
}

if (credentials.items.len == 0) return error.BadCredentialInputFormat;

return credentials.toOwnedSlice(allocator);
}
11 changes: 10 additions & 1 deletion build.zig
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const std = @import("std");
const acl = @import("acl.zig");

pub fn build(b: *std.Build) void {
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const strip = b.option(bool, "strip", "Strip debug symbols") orelse (optimize != .Debug);

const acl_list = b.option([]const u8, "acl-list", "Admin credentials") orelse "admin:minioadmin:minioadmin";
_ = try acl.parseCredentials(b.allocator, acl_list);

const options = b.addOptions();
options.addOption([]const u8, "acl_list", acl_list);

const exe = b.addExecutable(.{
.name = "zs3",
.root_module = b.createModule(.{
Expand All @@ -15,6 +22,8 @@ pub fn build(b: *std.Build) void {
}),
});

exe.root_module.addOptions("build_options", options);

b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
Expand Down
Loading