-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow secrets to be provided from a file or environment variable #5177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 @@ | ||
| // Copyright 2026 Stellar Development Foundation and contributors. Licensed | ||
| // under the Apache License, Version 2.0. See the COPYING file at the root | ||
| // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| #include "util/SecretManager.h" | ||
| #include "util/Logging.h" | ||
|
|
||
| #include <filesystem> | ||
| #include <fstream> | ||
| #include <sstream> | ||
| #include <stdexcept> | ||
|
|
||
| namespace stellar | ||
| { | ||
| namespace secretmanager | ||
| { | ||
|
|
||
| namespace stdfs = std::filesystem; | ||
|
|
||
| static std::string const FILE_PREFIX = "$FILE:"; | ||
|
|
||
| static std::string | ||
| rtrim(std::string s) | ||
| { | ||
| auto end = s.find_last_not_of(" \t\n\r"); | ||
| if (end == std::string::npos) | ||
| { | ||
| return ""; | ||
| } | ||
| return s.substr(0, end + 1); | ||
| } | ||
|
|
||
| static void | ||
| checkFilePermissions(std::string const& filePath) | ||
| { | ||
| stdfs::path p(filePath); | ||
| auto status = stdfs::status(p); | ||
| if (!stdfs::is_regular_file(status)) | ||
| { | ||
| throw std::runtime_error("Secret path is not a regular file: " + | ||
| filePath); | ||
| } | ||
| auto perms = status.permissions(); | ||
sisuresh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Reject if group or others have any access | ||
| auto forbidden = stdfs::perms::group_all | stdfs::perms::others_all; | ||
| if ((perms & forbidden) != stdfs::perms::none) | ||
| { | ||
| throw std::runtime_error( | ||
| "Secret file has overly permissive permissions " | ||
| "(must not be accessible by group or others): " + | ||
| filePath); | ||
| } | ||
| } | ||
|
|
||
| static std::string | ||
| resolveFromFile(std::string const& filePath) | ||
| { | ||
| LOG_INFO(DEFAULT_LOG, "Resolving secret from file"); | ||
| checkFilePermissions(filePath); | ||
| std::ifstream ifs(filePath); | ||
| if (!ifs.is_open()) | ||
| { | ||
| throw std::runtime_error("Cannot open secret file: " + filePath); | ||
| } | ||
| std::stringstream ss; | ||
| ss << ifs.rdbuf(); | ||
| std::string result = rtrim(ss.str()); | ||
| if (result.empty()) | ||
| { | ||
| throw std::runtime_error("Secret file is empty: " + filePath); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| std::string | ||
| resolve(std::string const& configValue) | ||
| { | ||
| if (configValue.substr(0, FILE_PREFIX.size()) == FILE_PREFIX) | ||
| { | ||
| return resolveFromFile(configValue.substr(FILE_PREFIX.size())); | ||
sisuresh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return configValue; | ||
| } | ||
|
|
||
| bool | ||
| isExternalSecret(std::string const& configValue) | ||
| { | ||
| return configValue.substr(0, FILE_PREFIX.size()) == FILE_PREFIX; | ||
| } | ||
|
|
||
| } // namespace secretmanager | ||
| } // namespace stellar | ||
This file contains hidden or 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,29 @@ | ||
| // Copyright 2026 Stellar Development Foundation and contributors. Licensed | ||
| // under the Apache License, Version 2.0. See the COPYING file at the root | ||
| // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string> | ||
|
|
||
| namespace stellar | ||
| { | ||
| namespace secretmanager | ||
| { | ||
|
|
||
| // Resolve a config value that may reference an external secret source. | ||
| // | ||
| // Supported prefixes: | ||
| // "$FILE:/path/file" - read from file (must have permissions 0600 or | ||
| // stricter) | ||
| // no prefix - return the value unchanged (backward compatible) | ||
| // | ||
| // Throws std::runtime_error on failure (unreadable file, bad permissions, | ||
| // empty resolved value). | ||
sisuresh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| std::string resolve(std::string const& configValue); | ||
|
|
||
| // Returns true if the value uses an external secret reference ($FILE: prefix). | ||
| bool isExternalSecret(std::string const& configValue); | ||
|
|
||
| } // namespace secretmanager | ||
| } // namespace stellar | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.