Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 0 additions & 2 deletions src/include/utils/secret.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#pragma once

#include "duckdb/main/client_context.hpp"
#include "duckdb/main/secret/secret.hpp"
#include "duckdb/main/secret/secret_manager.hpp"

namespace duckdb {
namespace sheets {

const SecretMatch GetSecretMatch(ClientContext &ctx, const std::string &path, const std::string &type);
const KeyValueSecret *GetGSheetSecret(ClientContext &ctx);

} // namespace sheets
} // namespace duckdb
41 changes: 21 additions & 20 deletions src/sheets/auth_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,29 @@ namespace duckdb {
namespace sheets {

std::unique_ptr<IAuthProvider> CreateAuthFromSecret(ClientContext &ctx, IHttpClient &http) {
auto *secret = GetGSheetSecret(ctx);
if (!secret) {
return nullptr;
}

auto provider = secret->GetProvider();
if (provider == "key_file") {
Value emailValue, keyValue;
if (!secret->TryGetValue("email", emailValue)) {
throw InvalidInputException("'email' not found in gsheet secret");
}
if (!secret->TryGetValue("secret", keyValue)) {
throw InvalidInputException("'secret' not found in gsheet secret");
}
return make_uniq<ServiceAccountAuth>(http, emailValue.ToString(), keyValue.ToString());
} else {
Value tokenValue;
if (!secret->TryGetValue("token", tokenValue)) {
throw InvalidInputException("'token' not found in gsheet secret");
auto match = GetSecretMatch(ctx, "gsheet", "gsheet");
if (match.HasMatch()) {
auto &secret = match.GetSecret();
auto gsheet_secret = dynamic_cast<const KeyValueSecret *>(&secret);
auto provider = gsheet_secret->GetProvider();
if (provider == "key_file") {
Value emailValue, keyValue;
if (!gsheet_secret->TryGetValue("email", emailValue)) {
throw InvalidInputException("'email' not found in gsheet secret");
}
if (!gsheet_secret->TryGetValue("secret", keyValue)) {
throw InvalidInputException("'secret' not found in gsheet secret");
}
return make_uniq<ServiceAccountAuth>(http, emailValue.ToString(), keyValue.ToString());
} else {
Value tokenValue;
if (!gsheet_secret->TryGetValue("token", tokenValue)) {
throw InvalidInputException("'token' not found in gsheet secret");
}
return make_uniq<BearerTokenAuth>(tokenValue.ToString());
}
return make_uniq<BearerTokenAuth>(tokenValue.ToString());
}
return nullptr;
}

} // namespace sheets
Expand Down
10 changes: 0 additions & 10 deletions src/utils/secret.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "utils/secret.hpp"

#include "duckdb/main/secret/secret.hpp"
#include "duckdb/main/secret/secret_manager.hpp"
#include "duckdb/catalog/catalog_transaction.hpp"

Expand All @@ -13,14 +12,5 @@ const SecretMatch GetSecretMatch(ClientContext &ctx, const std::string &path, co
return manager.LookupSecret(transaction, path, type);
}

const KeyValueSecret *GetGSheetSecret(ClientContext &ctx) {
auto match = GetSecretMatch(ctx, "gsheet", "gsheet");
if (!match.HasMatch()) {
return nullptr;
}
auto &secret = match.GetSecret();
return dynamic_cast<const KeyValueSecret *>(&secret);
}

} // namespace sheets
} // namespace duckdb
Loading