1212#define LOG_LEVEL_ERROR 3
1313#define LOG_LEVEL_NONE 4
1414
15+ // Key length for wrap key
16+ #define WRAP_KEY_LENGTH 32
17+
1518int app_log_level = LOG_LEVEL_INFO ; // Default to INFO level
1619
1720#define LOG_WITH_TIMESTAMP (fmt , level , associated_level , ...) \
@@ -37,8 +40,29 @@ int app_log_level = LOG_LEVEL_INFO; // Default to INFO level
3740#define LOG_ERROR (fmt , ...) \
3841 LOG_WITH_TIMESTAMP(fmt, "ERROR", LOG_LEVEL_ERROR, ##__VA_ARGS__)
3942
43+ // -----------------------------------------------------------------------------
44+ // Generate a random 32-byte key (alphanumeric and special characters)
45+ // -----------------------------------------------------------------------------
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 ;
4056
41- char * wrap_key = "" ;
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 ;
65+ }
4266
4367int push_wrapkey_to_secret_box (const char * wrapkey ) {
4468 CURL * curl ;
@@ -54,7 +78,7 @@ int push_wrapkey_to_secret_box(const char* wrapkey) {
5478 curl_easy_setopt (curl , CURLOPT_FOLLOWLOCATION , 1L );
5579 curl_easy_setopt (curl , CURLOPT_DEFAULT_PROTOCOL , "http" );
5680
57- strcpy (request_buffer , "key=wrapkey &value=" );
81+ strcpy (request_buffer , "key=WRAP_KEY &value=" );
5882 strcat (request_buffer , wrapkey );
5983 LOG_DEBUG ("Request body is %s" , request_buffer );
6084
@@ -80,15 +104,15 @@ int push_wrapkey_to_secret_box(const char* wrapkey) {
80104
81105int main (int argc , char * * argv ) {
82106 setvbuf (stdout , NULL , _IONBF , 0 );
83-
107+
84108 // Command line options
85109 char * const short_options = "l:h" ;
86110 struct option long_options [] = {
87111 {"log-level" , required_argument , NULL , 'l' },
88112 {"help" , no_argument , NULL , 'h' },
89113 {0 , 0 , 0 , 0 }
90114 };
91-
115+
92116 int opt ;
93117 do {
94118 opt = getopt_long (argc , argv , short_options , long_options , NULL );
@@ -120,20 +144,16 @@ int main(int argc, char** argv) {
120144 exit (-1 );
121145 }
122146 } while (opt != -1 );
123-
124- LOG_INFO ("Try to get key from local" );
125- wrap_key = getenv ("localKey" );
126- if (NULL == wrap_key ) {
127- LOG_ERROR ("local-key does not config" );
128- return -1 ;
129- }
130- if (strlen (wrap_key ) != 32 ) {
131- LOG_ERROR ("Key size is not 32 bytes, please check" );
147+
148+
149+ char * wrap_key = generate_random_key ();
150+ if (wrap_key == NULL ) {
151+ LOG_ERROR ("Failed to generate random wrap key" );
132152 return -1 ;
133153 }
154+ LOG_INFO ("Successfully generated random wrap key" );
155+ LOG_INFO ("Generated random wrap key: %s" , wrap_key );
134156
135- LOG_INFO ("Get wrap_key successful from local" );
136- LOG_DEBUG ("Wrapkey is %s" , wrap_key );
137157 int ret = push_wrapkey_to_secret_box (wrap_key );
138158 if (ret != 0 ) {
139159 LOG_ERROR ("Push wrapkey to secret box failed" );
0 commit comments