-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support to Filesystem key manager (not suitable for production)
- Loading branch information
Showing
11 changed files
with
129 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef FILESYSTEM_KEY_MANAGER_H | ||
#define FILESYSTEM_KEY_MANAGER_H | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace filesystem_key_manager { | ||
std::vector<uint8_t> get_seed(); | ||
} // namespace key_manager | ||
|
||
#endif // FILESYSTEM_KEY_MANAGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "hashicorp_key_manager.h" | ||
|
||
#include <vector> | ||
#include <stdexcept> | ||
#include <openssl/rand.h> | ||
#include <filesystem> | ||
#include <fstream> | ||
#include "utils.h" | ||
#include <toml++/toml.h> | ||
|
||
namespace filesystem_key_manager { | ||
|
||
std::string getSeedFilePath() { | ||
const char* value = std::getenv("SEED_FILEPATH"); | ||
|
||
if (value == nullptr) { | ||
auto config = toml::parse_file("Settings.toml"); | ||
return config["filesystem"]["seed_filepath"].as_string()->get(); | ||
} else { | ||
return std::string(value); | ||
} | ||
} | ||
|
||
std::vector<uint8_t> get_seed() { | ||
const std::string seed_file = getSeedFilePath(); | ||
|
||
if (std::filesystem::exists(seed_file)) { | ||
// The seed file exists | ||
std::ifstream seed_in(seed_file, std::ios::binary); | ||
if (!seed_in) { | ||
throw std::runtime_error("Error opening seed file for reading."); | ||
} | ||
|
||
// Read the contents into a vector | ||
std::vector<uint8_t> key((std::istreambuf_iterator<char>(seed_in)), | ||
std::istreambuf_iterator<char>()); | ||
|
||
// Check if the key is 32 bytes | ||
if (key.size() != 32) { | ||
throw std::runtime_error("Seed file has invalid size."); | ||
} | ||
|
||
return key; | ||
} else { | ||
// Seed file does not exist, generate a new seed | ||
std::vector<uint8_t> key(32); // 256-bit key | ||
|
||
// Generate cryptographically secure random bytes | ||
if (RAND_bytes(key.data(), key.size()) != 1) { | ||
throw std::runtime_error("Error generating random bytes."); | ||
} | ||
|
||
// Write the key to the seed file | ||
std::ofstream seed_out(seed_file, std::ios::binary | std::ios::trunc); | ||
if (!seed_out) { | ||
throw std::runtime_error("Error opening seed file for writing."); | ||
} | ||
|
||
seed_out.write(reinterpret_cast<const char*>(key.data()), key.size()); | ||
if (!seed_out) { | ||
throw std::runtime_error("Error writing seed to file."); | ||
} | ||
|
||
// Optionally, set file permissions to owner read/write only (platform-dependent) | ||
#ifdef __unix__ | ||
seed_out.close(); // Close the file before changing permissions | ||
std::filesystem::permissions(seed_file, | ||
std::filesystem::perms::owner_read | std::filesystem::perms::owner_write, | ||
std::filesystem::perm_options::replace); | ||
#endif | ||
|
||
return key; | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
lockbox/src/key_manager.cpp → lockbox/src/google_key_manager.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters