Skip to content

Commit f6c673f

Browse files
authored
refactor(provider-agent): reformat secret-provider-agent and key-provider-agent using clang-format (#47)
* refactor(secret-provider-agent): apply clang-format to secret-provider-agent * refactor(key-provider-agent): apply clang-format to key-provider-agent
1 parent e14320b commit f6c673f

File tree

2 files changed

+501
-489
lines changed

2 files changed

+501
-489
lines changed

cvmassistants/keyprovider/key-provider-agent/src/key_provider_agent.c

Lines changed: 132 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -7,155 +7,157 @@
77
#include <time.h>
88
// Log levels
99
#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
1212
#define LOG_LEVEL_ERROR 3
13-
#define LOG_LEVEL_NONE 4
13+
#define LOG_LEVEL_NONE 4
1414

1515
// Key length for wrap key
1616
#define WRAP_KEY_LENGTH 32
1717

18-
int app_log_level = LOG_LEVEL_INFO; // Default to INFO level
18+
int app_log_level = LOG_LEVEL_INFO; // Default to INFO level
1919

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)
3031

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__)
3334

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__)
3637

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__)
3940

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__)
4243

4344
// -----------------------------------------------------------------------------
4445
// Generate a random 32-byte key (alphanumeric and special characters)
4546
// -----------------------------------------------------------------------------
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;
6568
}
6669

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;
10192
}
102-
}
10393

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;
152100
}
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+
}
154108

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);
159147
}
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;
161163
}

0 commit comments

Comments
 (0)