-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathorchestration.zig
More file actions
194 lines (166 loc) · 7.42 KB
/
orchestration.zig
File metadata and controls
194 lines (166 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const std = @import("std");
const Allocator = std.mem.Allocator;
const Response = struct {
status: []const u8,
content_type: []const u8,
body: []const u8,
};
const prefix = "/api/orchestration";
const store_prefix = "/api/orchestration/store";
pub const Config = struct {
boiler_url: ?[]const u8 = null,
boiler_token: ?[]const u8 = null,
tickets_url: ?[]const u8 = null,
tickets_token: ?[]const u8 = null,
};
const Backend = enum {
boiler,
tickets,
fn notConfiguredBody(self: Backend) []const u8 {
return switch (self) {
.boiler => "{\"error\":\"NullBoiler not configured\"}",
.tickets => "{\"error\":\"NullTickets not configured\"}",
};
}
fn unreachableBody(self: Backend) []const u8 {
return switch (self) {
.boiler => "{\"error\":\"NullBoiler unreachable\"}",
.tickets => "{\"error\":\"NullTickets unreachable\"}",
};
}
};
pub fn isProxyPath(target: []const u8) bool {
return std.mem.eql(u8, target, prefix) or std.mem.startsWith(u8, target, prefix ++ "/");
}
fn isStorePath(target: []const u8) bool {
return std.mem.eql(u8, target, store_prefix) or std.mem.startsWith(u8, target, store_prefix ++ "/");
}
const ProxyTarget = struct {
backend: Backend,
base_url: []const u8,
token: ?[]const u8,
};
fn backendForPath(target: []const u8) ?Backend {
if (!isProxyPath(target)) return null;
return if (isStorePath(target)) .tickets else .boiler;
}
fn resolveProxyTarget(target: []const u8, cfg: Config) ?ProxyTarget {
const backend = backendForPath(target) orelse return null;
return switch (backend) {
.tickets => blk: {
const base_url = cfg.tickets_url orelse return null;
break :blk .{
.backend = .tickets,
.base_url = base_url,
.token = cfg.tickets_token,
};
},
.boiler => blk: {
const base_url = cfg.boiler_url orelse return null;
break :blk .{
.backend = .boiler,
.base_url = base_url,
.token = cfg.boiler_token,
};
},
};
}
/// Proxies orchestration API requests to the local orchestration stack.
/// `/api/orchestration/store/*` goes to NullTickets; all other orchestration
/// routes go to NullBoiler. The shared prefix is stripped before forwarding.
pub fn handle(allocator: Allocator, method: []const u8, target: []const u8, body: []const u8, cfg: Config) Response {
if (!isProxyPath(target)) {
return .{ .status = "404 Not Found", .content_type = "application/json", .body = "{\"error\":\"not found\"}" };
}
const backend = backendForPath(target) orelse
return .{ .status = "404 Not Found", .content_type = "application/json", .body = "{\"error\":\"not found\"}" };
const resolved = resolveProxyTarget(target, cfg) orelse
return .{ .status = "503 Service Unavailable", .content_type = "application/json", .body = backend.notConfiguredBody() };
const proxied_path = target[prefix.len..];
const path = if (proxied_path.len == 0) "/" else proxied_path;
const url = std.fmt.allocPrint(allocator, "{s}{s}", .{ resolved.base_url, path }) catch
return .{ .status = "500 Internal Server Error", .content_type = "application/json", .body = "{\"error\":\"internal error\"}" };
const http_method = parseMethod(method) orelse
return .{ .status = "405 Method Not Allowed", .content_type = "application/json", .body = "{\"error\":\"method not allowed\"}" };
var auth_header: ?[]const u8 = null;
defer if (auth_header) |value| allocator.free(value);
var header_buf: [1]std.http.Header = undefined;
const extra_headers: []const std.http.Header = if (resolved.token) |token| blk: {
auth_header = std.fmt.allocPrint(allocator, "Bearer {s}", .{token}) catch
return .{ .status = "500 Internal Server Error", .content_type = "application/json", .body = "{\"error\":\"internal error\"}" };
header_buf[0] = .{ .name = "Authorization", .value = auth_header.? };
break :blk header_buf[0..1];
} else &.{};
var client: std.http.Client = .{ .allocator = allocator };
defer client.deinit();
var response_body: std.io.Writer.Allocating = .init(allocator);
defer response_body.deinit();
const result = client.fetch(.{
.location = .{ .url = url },
.method = http_method,
.payload = if (body.len > 0) body else null,
.response_writer = &response_body.writer,
.extra_headers = extra_headers,
}) catch {
return .{ .status = "502 Bad Gateway", .content_type = "application/json", .body = resolved.backend.unreachableBody() };
};
const status_code: u10 = @intFromEnum(result.status);
const resp_body = response_body.toOwnedSlice() catch
return .{ .status = "500 Internal Server Error", .content_type = "application/json", .body = "{\"error\":\"internal error\"}" };
const status = mapStatus(status_code);
return .{
.status = status,
.content_type = "application/json",
.body = resp_body,
};
}
fn parseMethod(method: []const u8) ?std.http.Method {
if (std.mem.eql(u8, method, "GET")) return .GET;
if (std.mem.eql(u8, method, "POST")) return .POST;
if (std.mem.eql(u8, method, "PUT")) return .PUT;
if (std.mem.eql(u8, method, "DELETE")) return .DELETE;
if (std.mem.eql(u8, method, "PATCH")) return .PATCH;
return null;
}
fn mapStatus(code: u10) []const u8 {
return switch (code) {
200 => "200 OK",
201 => "201 Created",
204 => "204 No Content",
400 => "400 Bad Request",
401 => "401 Unauthorized",
403 => "403 Forbidden",
404 => "404 Not Found",
405 => "405 Method Not Allowed",
409 => "409 Conflict",
422 => "422 Unprocessable Entity",
500 => "500 Internal Server Error",
502 => "502 Bad Gateway",
503 => "503 Service Unavailable",
else => if (code >= 200 and code < 300) "200 OK" else if (code >= 400 and code < 500) "400 Bad Request" else "500 Internal Server Error",
};
}
test "isProxyPath matches orchestration namespace" {
try std.testing.expect(isProxyPath("/api/orchestration"));
try std.testing.expect(isProxyPath("/api/orchestration/runs"));
try std.testing.expect(isProxyPath("/api/orchestration/store/search"));
try std.testing.expect(!isProxyPath("/api/instances"));
}
test "backendForPath routes store requests to tickets backend" {
try std.testing.expectEqual(Backend.tickets, backendForPath("/api/orchestration/store/search").?);
try std.testing.expectEqual(Backend.boiler, backendForPath("/api/orchestration/runs").?);
}
test "handle routes store paths to NullTickets config" {
const resp = handle(std.testing.allocator, "GET", "/api/orchestration/store/search", "", .{
.boiler_url = "http://127.0.0.1:8080",
});
try std.testing.expectEqualStrings("503 Service Unavailable", resp.status);
try std.testing.expectEqualStrings("{\"error\":\"NullTickets not configured\"}", resp.body);
}
test "handle routes non-store paths to NullBoiler config" {
const resp = handle(std.testing.allocator, "GET", "/api/orchestration/runs", "", .{
.tickets_url = "http://127.0.0.1:7711",
});
try std.testing.expectEqualStrings("503 Service Unavailable", resp.status);
try std.testing.expectEqualStrings("{\"error\":\"NullBoiler not configured\"}", resp.body);
}