Skip to content

Commit 0172f3b

Browse files
committed
fix env, secrets handling
1 parent c981e4a commit 0172f3b

File tree

1 file changed

+65
-40
lines changed

1 file changed

+65
-40
lines changed

src/open_prompt_extension.cpp

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -151,47 +151,72 @@ namespace duckdb {
151151

152152
// Settings management
153153
static std::string GetConfigValue(ClientContext &context, const string &var_name, const string &default_value) {
154+
// Try environment variables
155+
{
156+
// Create uppercase ENV version: OPEN_PROMPT_SETTING
157+
std::string stripped_name = var_name;
158+
const std::string prefix = "openprompt_";
159+
if (stripped_name.substr(0, prefix.length()) == prefix) {
160+
stripped_name = stripped_name.substr(prefix.length());
161+
}
162+
std::string env_var_name = "OPEN_PROMPT_" + stripped_name;
163+
std::transform(env_var_name.begin(), env_var_name.end(), env_var_name.begin(), ::toupper);
164+
// std::cout << "SEARCH ENV FOR " << env_var_name << "\n";
165+
166+
const char* env_value = std::getenv(env_var_name.c_str());
167+
if (env_value != nullptr && strlen(env_value) > 0) {
168+
// std::cout << "USING ENV FOR " << var_name << "\n";
169+
std::string result(env_value);
170+
return result;
171+
}
172+
}
173+
174+
// Try to get from secrets
175+
{
176+
// Create lowercase secret version: open_prompt_setting
177+
std::string secret_key = var_name;
178+
const std::string prefix = "openprompt_";
179+
if (secret_key.substr(0, prefix.length()) == prefix) {
180+
secret_key = secret_key.substr(prefix.length());
181+
}
182+
// secret_key = "open_prompt_" + secret_key;
183+
std::transform(secret_key.begin(), secret_key.end(), secret_key.begin(), ::tolower);
184+
185+
auto &secret_manager = SecretManager::Get(context);
186+
try {
187+
// std::cout << "SEARCH SECRET FOR " << secret_key << "\n";
188+
auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context);
189+
auto secret_match = secret_manager.LookupSecret(transaction, "open_prompt", "open_prompt");
190+
if (secret_match.HasMatch()) {
191+
auto &secret = secret_match.GetSecret();
192+
if (secret.GetType() != "open_prompt") {
193+
throw InvalidInputException("Invalid secret type. Expected 'open_prompt', got '%s'", secret.GetType());
194+
}
195+
const auto *kv_secret = dynamic_cast<const KeyValueSecret*>(&secret);
196+
if (!kv_secret) {
197+
throw InvalidInputException("Invalid secret format for 'open_prompt' secret");
198+
}
199+
Value secret_value;
200+
if (kv_secret->TryGetValue(secret_key, secret_value)) {
201+
// std::cout << "USING SECRET FOR " << var_name << "\n";
202+
return secret_value.ToString();
203+
}
204+
}
205+
} catch (...) {
206+
// If secret lookup fails, fall back to user variables
207+
}
208+
}
209+
210+
// Fall back to user variables if secret not found (using original var_name)
211+
Value value;
212+
auto &config = ClientConfig::GetConfig(context);
213+
if (!config.GetUserVariable(var_name, value) || value.IsNull()) {
214+
// std::cout << "USING SET FOR " << var_name << "\n";
215+
return default_value;
216+
}
154217

155-
// Try environment variables
156-
std::string env_var_name = "OPEN_PROMPT_" + var_name;
157-
std::transform(env_var_name.begin(), env_var_name.end(), env_var_name.begin(), ::toupper);
158-
const char* env_value = std::getenv(env_var_name.c_str());
159-
if (env_value != nullptr && strlen(env_value) > 0) {
160-
return std::string(env_value);
161-
}
162-
163-
// Try to get from secrets
164-
auto &secret_manager = SecretManager::Get(context);
165-
try {
166-
auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context);
167-
auto secret_match = secret_manager.LookupSecret(transaction, "open_prompt", "open_prompt");
168-
if (secret_match.HasMatch()) {
169-
auto &secret = secret_match.GetSecret();
170-
if (secret.GetType() != "open_prompt") {
171-
throw InvalidInputException("Invalid secret type. Expected 'open_prompt', got '%s'", secret.GetType());
172-
}
173-
174-
const auto *kv_secret = dynamic_cast<const KeyValueSecret*>(&secret);
175-
if (!kv_secret) {
176-
throw InvalidInputException("Invalid secret format for 'open_prompt' secret");
177-
}
178-
179-
Value secret_value;
180-
if (kv_secret->TryGetValue(var_name, secret_value)) {
181-
return secret_value.ToString();
182-
}
183-
}
184-
} catch (...) {
185-
// If secret lookup fails, fall back to user variables
186-
}
187-
188-
// Fall back to user variables if secret not found
189-
Value value;
190-
auto &config = ClientConfig::GetConfig(context);
191-
if (!config.GetUserVariable(var_name, value) || value.IsNull()) {
192-
return default_value;
193-
}
194-
return value.ToString();
218+
// std::cout << "USING DEFAULT FOR " << var_name << "\n";
219+
return value.ToString();
195220
}
196221

197222

0 commit comments

Comments
 (0)