|
7 | 7 | #include <time.h> |
8 | 8 | // Log levels |
9 | 9 | #define LOG_LEVEL_DEBUG 0 |
10 | | -#define LOG_LEVEL_INFO 1 |
11 | | -#define LOG_LEVEL_WARN 2 |
| 10 | +#define LOG_LEVEL_INFO 1 |
| 11 | +#define LOG_LEVEL_WARN 2 |
12 | 12 | #define LOG_LEVEL_ERROR 3 |
13 | | -#define LOG_LEVEL_NONE 4 |
| 13 | +#define LOG_LEVEL_NONE 4 |
14 | 14 |
|
15 | 15 | // Key length for wrap key |
16 | 16 | #define WRAP_KEY_LENGTH 32 |
17 | 17 |
|
18 | | -int app_log_level = LOG_LEVEL_INFO; // Default to INFO level |
| 18 | +int app_log_level = LOG_LEVEL_INFO; // Default to INFO level |
19 | 19 |
|
20 | | -#define LOG_WITH_TIMESTAMP(fmt, level, associated_level, ...) \ |
21 | | - do { \ |
22 | | - if (app_log_level <= associated_level) { \ |
23 | | - time_t now = time(NULL); \ |
24 | | - struct tm *t = gmtime(&now); \ |
25 | | - char ts[24]; \ |
26 | | - strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S UTC", t); \ |
27 | | - printf("%-29s [%-5s] [%s:%d] " fmt "\n", ts, level, __FILE__, __LINE__, ##__VA_ARGS__); \ |
28 | | - } \ |
29 | | - } while (0) |
| 20 | +#define LOG_WITH_TIMESTAMP(fmt, level, associated_level, ...) \ |
| 21 | + do { \ |
| 22 | + if (app_log_level <= associated_level) { \ |
| 23 | + time_t now = time(NULL); \ |
| 24 | + struct tm *t = gmtime(&now); \ |
| 25 | + char ts[24]; \ |
| 26 | + strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S UTC", t); \ |
| 27 | + printf("%-29s [%-5s] [%s:%d] " fmt "\n", ts, level, __FILE__, __LINE__, \ |
| 28 | + ##__VA_ARGS__); \ |
| 29 | + } \ |
| 30 | + } while (0) |
30 | 31 |
|
31 | | -#define LOG_DEBUG(fmt, ...) \ |
32 | | - LOG_WITH_TIMESTAMP(fmt, "DEBUG", LOG_LEVEL_DEBUG, ##__VA_ARGS__) |
| 32 | +#define LOG_DEBUG(fmt, ...) \ |
| 33 | + LOG_WITH_TIMESTAMP(fmt, "DEBUG", LOG_LEVEL_DEBUG, ##__VA_ARGS__) |
33 | 34 |
|
34 | | -#define LOG_INFO(fmt, ...) \ |
35 | | - LOG_WITH_TIMESTAMP(fmt, "INFO", LOG_LEVEL_INFO, ##__VA_ARGS__) |
| 35 | +#define LOG_INFO(fmt, ...) \ |
| 36 | + LOG_WITH_TIMESTAMP(fmt, "INFO", LOG_LEVEL_INFO, ##__VA_ARGS__) |
36 | 37 |
|
37 | | -#define LOG_WARN(fmt, ...) \ |
38 | | - LOG_WITH_TIMESTAMP(fmt, "WARN", LOG_LEVEL_WARN, ##__VA_ARGS__) |
| 38 | +#define LOG_WARN(fmt, ...) \ |
| 39 | + LOG_WITH_TIMESTAMP(fmt, "WARN", LOG_LEVEL_WARN, ##__VA_ARGS__) |
39 | 40 |
|
40 | | -#define LOG_ERROR(fmt, ...) \ |
41 | | - LOG_WITH_TIMESTAMP(fmt, "ERROR", LOG_LEVEL_ERROR, ##__VA_ARGS__) |
| 41 | +#define LOG_ERROR(fmt, ...) \ |
| 42 | + LOG_WITH_TIMESTAMP(fmt, "ERROR", LOG_LEVEL_ERROR, ##__VA_ARGS__) |
42 | 43 |
|
43 | 44 | // ----------------------------------------------------------------------------- |
44 | 45 | // Generate a random 32-byte key (alphanumeric and special characters) |
45 | 46 | // ----------------------------------------------------------------------------- |
46 | | -char* generate_random_key(void) { |
47 | | - char* key = malloc(WRAP_KEY_LENGTH + 1); |
48 | | - |
49 | | - if (!key) { |
50 | | - LOG_ERROR("Memory allocation failed"); |
51 | | - return NULL; |
52 | | - } |
53 | | - |
54 | | - const char charset[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.~"; |
55 | | - size_t charset_size = sizeof(charset) - 1; |
56 | | - |
57 | | - // Seed the random number generator with current time to ensure different keys on each run |
58 | | - srand((unsigned int)time(NULL)); |
59 | | - |
60 | | - for (size_t i = 0; i < WRAP_KEY_LENGTH; i++) { |
61 | | - key[i] = charset[rand() % charset_size]; |
62 | | - } |
63 | | - key[WRAP_KEY_LENGTH] = '\0'; |
64 | | - return key; |
| 47 | +char *generate_random_key(void) { |
| 48 | + char *key = malloc(WRAP_KEY_LENGTH + 1); |
| 49 | + |
| 50 | + if (!key) { |
| 51 | + LOG_ERROR("Memory allocation failed"); |
| 52 | + return NULL; |
| 53 | + } |
| 54 | + |
| 55 | + const char charset[] = |
| 56 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-.~"; |
| 57 | + size_t charset_size = sizeof(charset) - 1; |
| 58 | + |
| 59 | + // Seed the random number generator with current time to ensure different keys |
| 60 | + // on each run |
| 61 | + srand((unsigned int)time(NULL)); |
| 62 | + |
| 63 | + for (size_t i = 0; i < WRAP_KEY_LENGTH; i++) { |
| 64 | + key[i] = charset[rand() % charset_size]; |
| 65 | + } |
| 66 | + key[WRAP_KEY_LENGTH] = '\0'; |
| 67 | + return key; |
65 | 68 | } |
66 | 69 |
|
67 | | -int push_wrapkey_to_secret_box(const char* wrapkey) { |
68 | | - CURL* curl; |
69 | | - CURLcode res; |
70 | | - char request_buffer[64]; |
71 | | - long http_code = 0; |
72 | | - |
73 | | - curl = curl_easy_init(); |
74 | | - if (curl) { |
75 | | - // get token |
76 | | - curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); |
77 | | - curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:9090/secret"); |
78 | | - curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); |
79 | | - curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http"); |
80 | | - |
81 | | - strcpy(request_buffer, "key=WRAP_KEY&value="); |
82 | | - strcat(request_buffer, wrapkey); |
83 | | - |
84 | | - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_buffer); |
85 | | - res = curl_easy_perform(curl); |
86 | | - if (res != CURLE_OK) { |
87 | | - LOG_ERROR("curl_easy_perform() failed: %s", curl_easy_strerror(res)); |
88 | | - return -1; |
89 | | - } |
90 | | - |
91 | | - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); |
92 | | - if (http_code != 200) { |
93 | | - LOG_ERROR("Failed to push wrap key to secret box, HTTP response code: %ld", http_code); |
94 | | - return -1; |
95 | | - } |
96 | | - curl_easy_cleanup(curl); |
97 | | - return 0; |
98 | | - } else { |
99 | | - LOG_ERROR("Init curl failed"); |
100 | | - return -1; |
| 70 | +int push_wrapkey_to_secret_box(const char *wrapkey) { |
| 71 | + CURL *curl; |
| 72 | + CURLcode res; |
| 73 | + char request_buffer[64]; |
| 74 | + long http_code = 0; |
| 75 | + |
| 76 | + curl = curl_easy_init(); |
| 77 | + if (curl) { |
| 78 | + // get token |
| 79 | + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); |
| 80 | + curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:9090/secret"); |
| 81 | + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 82 | + curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "http"); |
| 83 | + |
| 84 | + strcpy(request_buffer, "key=WRAP_KEY&value="); |
| 85 | + strcat(request_buffer, wrapkey); |
| 86 | + |
| 87 | + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request_buffer); |
| 88 | + res = curl_easy_perform(curl); |
| 89 | + if (res != CURLE_OK) { |
| 90 | + LOG_ERROR("curl_easy_perform() failed: %s", curl_easy_strerror(res)); |
| 91 | + return -1; |
101 | 92 | } |
102 | | -} |
103 | 93 |
|
104 | | -int main(int argc, char** argv) { |
105 | | - setvbuf(stdout, NULL, _IONBF, 0); |
106 | | - |
107 | | - // Command line options |
108 | | - char* const short_options = "l:h"; |
109 | | - struct option long_options[] = { |
110 | | - {"log-level", required_argument, NULL, 'l'}, |
111 | | - {"help", no_argument, NULL, 'h'}, |
112 | | - {0, 0, 0, 0} |
113 | | - }; |
114 | | - |
115 | | - int opt; |
116 | | - do { |
117 | | - opt = getopt_long(argc, argv, short_options, long_options, NULL); |
118 | | - switch (opt) { |
119 | | - case 'l': |
120 | | - if (!strcasecmp(optarg, "debug")) |
121 | | - app_log_level = LOG_LEVEL_DEBUG; |
122 | | - else if (!strcasecmp(optarg, "info")) |
123 | | - app_log_level = LOG_LEVEL_INFO; |
124 | | - else if (!strcasecmp(optarg, "warn")) |
125 | | - app_log_level = LOG_LEVEL_WARN; |
126 | | - else if (!strcasecmp(optarg, "error")) |
127 | | - app_log_level = LOG_LEVEL_ERROR; |
128 | | - else if (!strcasecmp(optarg, "off")) |
129 | | - app_log_level = LOG_LEVEL_NONE; |
130 | | - break; |
131 | | - case 'h': |
132 | | - puts( |
133 | | - " Usage:\n\n" |
134 | | - " key-provider-agent [options]\n\n" |
135 | | - " Options:\n\n" |
136 | | - " --log-level/-l value set the log level (debug, info, warn, error, off)\n" |
137 | | - " --help/-h show the usage\n"); |
138 | | - exit(0); |
139 | | - case -1: |
140 | | - break; |
141 | | - default: |
142 | | - puts("Use --help for usage information"); |
143 | | - exit(-1); |
144 | | - } |
145 | | - } while (opt != -1); |
146 | | - |
147 | | - |
148 | | - char* wrap_key = generate_random_key(); |
149 | | - if (wrap_key == NULL) { |
150 | | - LOG_ERROR("Failed to generate random wrap key"); |
151 | | - return -1; |
| 94 | + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); |
| 95 | + if (http_code != 200) { |
| 96 | + LOG_ERROR( |
| 97 | + "Failed to push wrap key to secret box, HTTP response code: %ld", |
| 98 | + http_code); |
| 99 | + return -1; |
152 | 100 | } |
153 | | - LOG_INFO("Successfully generated random wrap key"); |
| 101 | + curl_easy_cleanup(curl); |
| 102 | + return 0; |
| 103 | + } else { |
| 104 | + LOG_ERROR("Init curl failed"); |
| 105 | + return -1; |
| 106 | + } |
| 107 | +} |
154 | 108 |
|
155 | | - int ret = push_wrapkey_to_secret_box(wrap_key); |
156 | | - if (ret != 0) { |
157 | | - LOG_ERROR("Push wrapkey to secret box failed"); |
158 | | - return -1; |
| 109 | +int main(int argc, char **argv) { |
| 110 | + setvbuf(stdout, NULL, _IONBF, 0); |
| 111 | + |
| 112 | + // Command line options |
| 113 | + char *const short_options = "l:h"; |
| 114 | + struct option long_options[] = {{"log-level", required_argument, NULL, 'l'}, |
| 115 | + {"help", no_argument, NULL, 'h'}, |
| 116 | + {0, 0, 0, 0}}; |
| 117 | + |
| 118 | + int opt; |
| 119 | + do { |
| 120 | + opt = getopt_long(argc, argv, short_options, long_options, NULL); |
| 121 | + switch (opt) { |
| 122 | + case 'l': |
| 123 | + if (!strcasecmp(optarg, "debug")) |
| 124 | + app_log_level = LOG_LEVEL_DEBUG; |
| 125 | + else if (!strcasecmp(optarg, "info")) |
| 126 | + app_log_level = LOG_LEVEL_INFO; |
| 127 | + else if (!strcasecmp(optarg, "warn")) |
| 128 | + app_log_level = LOG_LEVEL_WARN; |
| 129 | + else if (!strcasecmp(optarg, "error")) |
| 130 | + app_log_level = LOG_LEVEL_ERROR; |
| 131 | + else if (!strcasecmp(optarg, "off")) |
| 132 | + app_log_level = LOG_LEVEL_NONE; |
| 133 | + break; |
| 134 | + case 'h': |
| 135 | + puts(" Usage:\n\n" |
| 136 | + " key-provider-agent [options]\n\n" |
| 137 | + " Options:\n\n" |
| 138 | + " --log-level/-l value set the log level (debug, info, " |
| 139 | + "warn, error, off)\n" |
| 140 | + " --help/-h show the usage\n"); |
| 141 | + exit(0); |
| 142 | + case -1: |
| 143 | + break; |
| 144 | + default: |
| 145 | + puts("Use --help for usage information"); |
| 146 | + exit(-1); |
159 | 147 | } |
160 | | - return 0; |
| 148 | + } while (opt != -1); |
| 149 | + |
| 150 | + char *wrap_key = generate_random_key(); |
| 151 | + if (wrap_key == NULL) { |
| 152 | + LOG_ERROR("Failed to generate random wrap key"); |
| 153 | + return -1; |
| 154 | + } |
| 155 | + LOG_INFO("Successfully generated random wrap key"); |
| 156 | + |
| 157 | + int ret = push_wrapkey_to_secret_box(wrap_key); |
| 158 | + if (ret != 0) { |
| 159 | + LOG_ERROR("Push wrapkey to secret box failed"); |
| 160 | + return -1; |
| 161 | + } |
| 162 | + return 0; |
161 | 163 | } |
0 commit comments