Skip to content

Commit dd9a85f

Browse files
feat: key generation from key-provider-agent (#36)
1 parent a887aa7 commit dd9a85f

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

cvmassistants/disktool/encryptedDisk.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# This script partitions, formats, and mounts disk devices. Supports both
77
# encrypted (LUKS) and unencrypted disks. Environment variables control behavior:
88
# `MOUNT_PATH` (mount point), `DISK` (device name), `KEY_TYPE` (only wrapkey supported),
9-
# and `wrapkey` (encryption key).
9+
# and `WRAP_KEY` (encryption key).
1010
#
1111
# Requirements:
1212
# - Must be run as root
@@ -42,11 +42,11 @@ detect_or_create_partition() {
4242
fi
4343
done
4444

45-
log_info "Creating partition on $disk_dev with the following passed fdisk parameters:
46-
n = new partition
47-
p = primary partition
48-
1 = partition number 1
49-
<Enter><Enter> = default start and end sectors
45+
log_info "Creating partition on $disk_dev with the following passed fdisk parameters:
46+
n = new partition
47+
p = primary partition
48+
1 = partition number 1
49+
<Enter><Enter> = default start and end sectors
5050
w = write changes"
5151
# Create the partition using fdisk
5252
# fdisk may return non-zero due to partition table re-read warning, but partition is created
@@ -65,7 +65,7 @@ detect_or_create_partition() {
6565
# Try both possible partition naming schemes
6666
for suffix in "1" "p1"; do
6767
part_disk="${disk_dev}${suffix}"
68-
if [[ -e "$part_disk" ]]; then
68+
if [[ -e "$part_disk" ]]; then
6969
mappername="${mappername}${suffix}"
7070
log_info "Partition $part_disk successfully created on $disk_dev"
7171
return 0
@@ -81,7 +81,7 @@ format_and_encrypt_partition() {
8181
local key="$1"
8282
local part_dev="$2"
8383
local mapper="$3"
84-
84+
8585
echo "$key" | cryptsetup luksFormat --key-file=- "$part_dev"
8686
[[ $? -ne 0 ]] && log_fatal "Failed to format partition $part_dev in luks format"
8787
log_info "Partition $part_dev formatted successfully in luks format"
@@ -93,7 +93,7 @@ format_and_encrypt_partition() {
9393
mkfs.ext4 "/dev/mapper/$mapper"
9494
[[ $? -ne 0 ]] && log_fatal "Failed to format partition /dev/mapper/$mapper in ext4 format"
9595
log_info "Partition /dev/mapper/$mapper successfully formatted in ext4 format"
96-
96+
9797
cryptsetup close "$mapper"
9898
[[ $? -ne 0 ]] && log_fatal "Failed to close partition /dev/mapper/$mapper"
9999
log_info "Partition /dev/mapper/$mapper closed successfully"
@@ -104,7 +104,7 @@ format_and_encrypt_partition() {
104104
mount_device() {
105105
local device="$1"
106106
local mount_point="$2"
107-
107+
108108
mount "$device" "$mount_point"
109109
[[ $? -ne 0 ]] && log_fatal "Failed to mount $device to $mount_point"
110110
log_info "Mounted $device to $mount_point"
@@ -119,7 +119,7 @@ log_info "Starting encrypted disk configuration..."
119119
[ "$KEY_TYPE" != "wrapkey" ] && log_fatal "KEY_TYPE $KEY_TYPE is not supported"
120120

121121
log_info "Handling encrypted disk case"
122-
[[ -z "$wrapkey" ]] && log_fatal "wrapkey is null"
122+
[[ -z "$WRAP_KEY" ]] && log_fatal "WRAP_KEY is null"
123123

124124
if [ ! -d "$MOUNT_PATH" ]; then
125125
log_info "Mount directory $MOUNT_PATH does not exist"
@@ -137,10 +137,10 @@ device_to_mount="/dev/mapper/$mappername"
137137
[ -e "$device_to_mount" ] && log_fatal "Mapper $device_to_mount already exists"
138138

139139
# Format and encrypt the partition (and check if it opens correctly)
140-
format_and_encrypt_partition "$wrapkey" "$part_disk" "$mappername"
140+
format_and_encrypt_partition "$WRAP_KEY" "$part_disk" "$mappername"
141141

142142
# Open the encrypted device in its mapper
143-
echo "$wrapkey" | cryptsetup open --key-file=- "$part_disk" "$mappername"
143+
echo "$WRAP_KEY" | cryptsetup open --key-file=- "$part_disk" "$mappername"
144144
[[ $? -ne 0 ]] && log_fatal "cryptsetup open --key-file=- "$part_disk" "$mappername": failed"
145145
log_info "cryptsetup open --key-file=- "$part_disk" "$mappername": success"
146146

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
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+
1518
int 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

4367
int 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

81105
int 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

Comments
 (0)