-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.c
More file actions
223 lines (175 loc) · 6.4 KB
/
main.c
File metadata and controls
223 lines (175 loc) · 6.4 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <event2/buffer.h>
#include <event2/event.h>
#include <event2/http.h>
#include <event2/keyvalq_struct.h>
#include "arena.h"
#include "auth.h"
#include "commands.h"
#include "utils.h"
#define PORT 8000
#define AUTH_HEADER_KEY "access_token"
// -- Route Definition --
typedef struct {
const char *path;
enum evhttp_cmd_type method;
void (*callback)(struct evhttp_request *req, void *ctx);
} route;
// -- Forward Declarations --
void health_callback(struct evhttp_request *req, void *ctx);
void reboot_callback(struct evhttp_request *req, void *ctx);
void restart_callback(struct evhttp_request *req, void *ctx);
void sync_upstream_callback(struct evhttp_request *req, void *ctx);
void deploy_branch_callback(struct evhttp_request *req, void *ctx);
void teardown_branch_callback(struct evhttp_request *req, void *ctx);
void logs_callback(struct evhttp_request *req, void *ctx);
// -- Route Table --
route ROUTES_CONFIG[] = {
{"/health", EVHTTP_REQ_GET, health_callback},
{"/reboot", EVHTTP_REQ_POST, reboot_callback},
{"/restart", EVHTTP_REQ_POST, restart_callback},
{"/sync_upstream", EVHTTP_REQ_PUT, sync_upstream_callback},
{"/deploy_branch", EVHTTP_REQ_GET, deploy_branch_callback},
{"/teardown_branch", EVHTTP_REQ_DELETE, teardown_branch_callback},
{"/logs", EVHTTP_REQ_GET, logs_callback},
};
const size_t NUM_ROUTES = sizeof(ROUTES_CONFIG) / sizeof(route);
// -- Middleware --
void auth_middleware(struct evhttp_request *req, void *ctx) {
size_t i = (size_t)(intptr_t)ctx;
if (i >= NUM_ROUTES) {
log_error("Dispatch error: invalid route index");
send_json_error(req, 500, "Internal Server Error");
return;
}
// Extract value from request header
struct evkeyvalq *headers = evhttp_request_get_input_headers(req);
const char *client_auth_key = evhttp_find_header(headers, AUTH_HEADER_KEY);
if (!client_auth_key) {
log_error("Middleware: Authentication Error (Missing Header)");
send_json_error(req, 401, "Authentication Error");
return;
}
if (!authenticate(client_auth_key)) {
log_request(req, ROUTES_CONFIG[i].path);
// DISPATCH TO ROUTE CALLBACK
ROUTES_CONFIG[i].callback(req, ctx);
} else {
log_error("Middleware: Authentication Error (Invalid Key)");
send_json_error(req, 401, "Authentication Error");
}
}
// -- Helpers --
void validate_and_run(struct evhttp_request *req, void *ctx, char *(*runner)(int *)) {
size_t i = (size_t)(intptr_t)ctx;
if (evhttp_request_get_command(req) != ROUTES_CONFIG[i].method) {
send_json_error(req, 405, "Method Not Allowed");
return;
}
int exit_code = 0;
char *output = runner(&exit_code);
if (exit_code == 0) {
send_json_response(req, 200, "ok", "Command executed", output);
} else {
send_json_response(req, 500, "error", "Command failed", output ? output : "Unknown error");
}
if (output)
deallocate(output);
}
void validate_and_run_arg(struct evhttp_request *req, void *ctx,
char *(*runner)(const char *, int *), const char *arg_key) {
size_t i = (size_t)(intptr_t)ctx;
if (evhttp_request_get_command(req) != ROUTES_CONFIG[i].method) {
send_json_error(req, 405, "Method Not Allowed");
return;
}
char *arg = get_query_param(req, arg_key);
int exit_code = 0;
char *output = runner(arg, &exit_code);
if (arg)
free(arg);
if (exit_code == 0) {
send_json_response(req, 200, "ok", "Command executed", output);
} else {
send_json_response(req, 500, "error", "Command failed", output ? output : "Unknown error");
}
if (output)
deallocate(output);
}
// -- Callbacks --
void health_callback(struct evhttp_request *req, void *ctx) {
validate_and_run(req, ctx, run_health);
}
void reboot_callback(struct evhttp_request *req, void *ctx) {
validate_and_run(req, ctx, run_reboot);
}
void restart_callback(struct evhttp_request *req, void *ctx) {
validate_and_run(req, ctx, run_restart);
}
void logs_callback(struct evhttp_request *req, void *ctx) { validate_and_run(req, ctx, run_logs); }
void sync_upstream_callback(struct evhttp_request *req, void *ctx) {
validate_and_run_arg(req, ctx, run_git_pull, "branch");
}
void deploy_branch_callback(struct evhttp_request *req, void *ctx) {
validate_and_run_arg(req, ctx, run_deploy_branch, "branch");
}
void teardown_branch_callback(struct evhttp_request *req, void *ctx) {
validate_and_run_arg(req, ctx, run_teardown_branch, "branch");
}
// -- Setup --
static void signal_cb(evutil_socket_t fd, short event, void *arg) {
(void)event;
printf("%s Shutting down server...\n", strsignal(fd));
event_base_loopbreak(arg);
}
static void generic_request_handler(struct evhttp_request *req, void *ctx) {
(void)ctx;
log_info("Got request for unallowed path");
send_json_error(req, 404, "Route not found");
}
int main() {
prealloc_arena();
if (init_auth()) {
log_error("init auth failed \n");
return 1;
} else {
log_info("Auth module loaded");
}
openlog("cmon", LOG_PID | LOG_CONS, LOG_LOCAL0);
struct event_base *base = event_base_new();
if (!base) {
fprintf(stderr, "Error: Event base is null\n");
return 1;
}
struct evhttp *http_server = evhttp_new(base);
if (!http_server) {
fprintf(stderr, "Error: Server is null\n");
return 1;
}
if (evhttp_bind_socket(http_server, "0.0.0.0", PORT) != 0) {
perror("Bind");
return 1;
}
evhttp_set_allowed_methods(http_server, EVHTTP_REQ_GET | EVHTTP_REQ_POST | EVHTTP_REQ_PUT |
EVHTTP_REQ_DELETE);
for (size_t i = 0; i < NUM_ROUTES; ++i) {
evhttp_set_cb(http_server, ROUTES_CONFIG[i].path, auth_middleware, (void *)(intptr_t)i);
}
evhttp_set_gencb(http_server, generic_request_handler, NULL);
struct event *sig_int = evsignal_new(base, SIGINT, signal_cb, base);
event_add(sig_int, NULL);
printf("Listening requests on http://0.0.0.0:%d\n", PORT);
event_base_dispatch(base);
syslog(LOG_INFO, "Server stopping");
closelog();
teardown_arena();
evhttp_free(http_server);
event_free(sig_int);
event_base_free(base);
return 0;
}