Skip to content
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

SECRET support #20

Merged
merged 9 commits into from
Jan 7, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix env, secrets handling
lmangani committed Jan 7, 2025
commit 0172f3b2a3f363cf7a69765ec2832c821859f325
105 changes: 65 additions & 40 deletions src/open_prompt_extension.cpp
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@
res->json_system_prompt_idx = json_system_prompt_idx;
return unique_ptr<FunctionData>(std::move(res));
};
bool Equals(const FunctionData &other) const {

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_threads, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_threads, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_eh, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_eh, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_mvp, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_mvp, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_arm64, arm64, arm64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / MacOS (osx_amd64, x86_64, x64-osx)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_eh, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_eh, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_threads, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]

Check warning on line 48 in src/open_prompt_extension.cpp

GitHub Actions / Build extension binaries / DuckDB-Wasm (wasm_threads, wasm32-emscripten)

'Equals' overrides a member function but is not marked 'override' [-Winconsistent-missing-override]
return model_idx == other.Cast<OpenPromptData>().model_idx &&
json_schema_idx == other.Cast<OpenPromptData>().json_schema_idx &&
json_system_prompt_idx==other.Cast<OpenPromptData>().json_system_prompt_idx;
@@ -151,47 +151,72 @@

// Settings management
static std::string GetConfigValue(ClientContext &context, const string &var_name, const string &default_value) {
// Try environment variables
{
// Create uppercase ENV version: OPEN_PROMPT_SETTING
std::string stripped_name = var_name;
const std::string prefix = "openprompt_";
if (stripped_name.substr(0, prefix.length()) == prefix) {
stripped_name = stripped_name.substr(prefix.length());
}
std::string env_var_name = "OPEN_PROMPT_" + stripped_name;
std::transform(env_var_name.begin(), env_var_name.end(), env_var_name.begin(), ::toupper);
// std::cout << "SEARCH ENV FOR " << env_var_name << "\n";

const char* env_value = std::getenv(env_var_name.c_str());
if (env_value != nullptr && strlen(env_value) > 0) {
// std::cout << "USING ENV FOR " << var_name << "\n";
std::string result(env_value);
return result;
}
}

// Try to get from secrets
{
// Create lowercase secret version: open_prompt_setting
std::string secret_key = var_name;
const std::string prefix = "openprompt_";
if (secret_key.substr(0, prefix.length()) == prefix) {
secret_key = secret_key.substr(prefix.length());
}
// secret_key = "open_prompt_" + secret_key;
std::transform(secret_key.begin(), secret_key.end(), secret_key.begin(), ::tolower);

auto &secret_manager = SecretManager::Get(context);
try {
// std::cout << "SEARCH SECRET FOR " << secret_key << "\n";
auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context);
auto secret_match = secret_manager.LookupSecret(transaction, "open_prompt", "open_prompt");
if (secret_match.HasMatch()) {
auto &secret = secret_match.GetSecret();
if (secret.GetType() != "open_prompt") {
throw InvalidInputException("Invalid secret type. Expected 'open_prompt', got '%s'", secret.GetType());
}
const auto *kv_secret = dynamic_cast<const KeyValueSecret*>(&secret);
if (!kv_secret) {
throw InvalidInputException("Invalid secret format for 'open_prompt' secret");
}
Value secret_value;
if (kv_secret->TryGetValue(secret_key, secret_value)) {
// std::cout << "USING SECRET FOR " << var_name << "\n";
return secret_value.ToString();
}
}
} catch (...) {
// If secret lookup fails, fall back to user variables
}
}

// Fall back to user variables if secret not found (using original var_name)
Value value;
auto &config = ClientConfig::GetConfig(context);
if (!config.GetUserVariable(var_name, value) || value.IsNull()) {
// std::cout << "USING SET FOR " << var_name << "\n";
return default_value;
}

// Try environment variables
std::string env_var_name = "OPEN_PROMPT_" + var_name;
std::transform(env_var_name.begin(), env_var_name.end(), env_var_name.begin(), ::toupper);
const char* env_value = std::getenv(env_var_name.c_str());
if (env_value != nullptr && strlen(env_value) > 0) {
return std::string(env_value);
}

// Try to get from secrets
auto &secret_manager = SecretManager::Get(context);
try {
auto transaction = CatalogTransaction::GetSystemCatalogTransaction(context);
auto secret_match = secret_manager.LookupSecret(transaction, "open_prompt", "open_prompt");
if (secret_match.HasMatch()) {
auto &secret = secret_match.GetSecret();
if (secret.GetType() != "open_prompt") {
throw InvalidInputException("Invalid secret type. Expected 'open_prompt', got '%s'", secret.GetType());
}

const auto *kv_secret = dynamic_cast<const KeyValueSecret*>(&secret);
if (!kv_secret) {
throw InvalidInputException("Invalid secret format for 'open_prompt' secret");
}

Value secret_value;
if (kv_secret->TryGetValue(var_name, secret_value)) {
return secret_value.ToString();
}
}
} catch (...) {
// If secret lookup fails, fall back to user variables
}

// Fall back to user variables if secret not found
Value value;
auto &config = ClientConfig::GetConfig(context);
if (!config.GetUserVariable(var_name, value) || value.IsNull()) {
return default_value;
}
return value.ToString();
// std::cout << "USING DEFAULT FOR " << var_name << "\n";
return value.ToString();
}



Unchanged files with check annotations Beta