|
| 1 | +/* |
| 2 | + * Copyright 2018- The Pixie Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | + |
| 19 | +#include "src/vizier/services/agent/pem/direct_query_server.h" |
| 20 | + |
| 21 | +#include <chrono> |
| 22 | +#include <cstdint> |
| 23 | +#include <cstring> |
| 24 | +#include <iterator> |
| 25 | +#include <string> |
| 26 | +#include <string_view> |
| 27 | +#include <utility> |
| 28 | +#include <vector> |
| 29 | + |
| 30 | +#include <absl/strings/str_split.h> |
| 31 | +#include <absl/strings/string_view.h> |
| 32 | +#include <absl/strings/substitute.h> |
| 33 | +#include <jwt/base64.hpp> |
| 34 | +#include <sole.hpp> |
| 35 | + |
| 36 | +#include "src/carnot/carnot.h" |
| 37 | +#include "src/carnot/carnotpb/carnot.pb.h" |
| 38 | +#include "src/carnot/engine_state.h" |
| 39 | +#include "src/carnot/exec/local_grpc_result_server.h" |
| 40 | +#include "src/carnot/planner/compiler/compiler.h" |
| 41 | +#include "src/carnot/planpb/plan.pb.h" |
| 42 | +#include "src/common/base/base.h" |
| 43 | +#include "src/shared/services/jwt/service_token.h" |
| 44 | +#include "src/shared/types/typespb/wrapper/types_pb_wrapper.h" |
| 45 | +#include "src/vizier/services/agent/shared/vizier_results/result_conversion.h" |
| 46 | + |
| 47 | +namespace px { |
| 48 | +namespace vizier { |
| 49 | +namespace agent { |
| 50 | + |
| 51 | +namespace { |
| 52 | + |
| 53 | +constexpr char kAuthorizationMetadataKey[] = "authorization"; |
| 54 | + |
| 55 | +} // namespace |
| 56 | + |
| 57 | +::grpc::Status AuthenticateRequest(::grpc::ServerContext* ctx, const std::string& jwt_signing_key) { |
| 58 | + if (jwt_signing_key.empty()) { |
| 59 | + return ::grpc::Status(::grpc::StatusCode::UNAUTHENTICATED, |
| 60 | + "direct-query: signing key not configured"); |
| 61 | + } |
| 62 | + const auto& md = ctx->client_metadata(); |
| 63 | + const auto range = md.equal_range(kAuthorizationMetadataKey); |
| 64 | + if (range.first == range.second) { |
| 65 | + return ::grpc::Status(::grpc::StatusCode::UNAUTHENTICATED, |
| 66 | + "direct-query: missing authorization metadata"); |
| 67 | + } |
| 68 | + if (std::next(range.first) != range.second) { |
| 69 | + return ::grpc::Status(::grpc::StatusCode::UNAUTHENTICATED, |
| 70 | + "direct-query: duplicate authorization metadata"); |
| 71 | + } |
| 72 | + const std::string_view raw(range.first->second.data(), range.first->second.size()); |
| 73 | + const std::string_view token = ::px::services::StripBearerPrefix(raw); |
| 74 | + if (token.empty()) { |
| 75 | + return ::grpc::Status(::grpc::StatusCode::UNAUTHENTICATED, |
| 76 | + "direct-query: authorization is not a Bearer token"); |
| 77 | + } |
| 78 | + const auto s = ::px::services::VerifyServiceJWT(token, jwt_signing_key); |
| 79 | + if (!s.ok()) { |
| 80 | + VLOG(1) << "direct-query: rejecting bearer token: " << s.msg(); |
| 81 | + return ::grpc::Status(::grpc::StatusCode::UNAUTHENTICATED, |
| 82 | + "direct-query: invalid bearer token"); |
| 83 | + } |
| 84 | + return ::grpc::Status::OK; |
| 85 | +} |
| 86 | + |
| 87 | +namespace { |
| 88 | + |
| 89 | +constexpr int64_t kMaxOutputRowsPerTable = 10000; |
| 90 | + |
| 91 | +std::vector<::px::api::vizierpb::ExecuteScriptResponse> collectResponses( |
| 92 | + ::px::carnot::exec::LocalGRPCResultSinkServer* result_server, const std::string& query_id) { |
| 93 | + std::vector<::px::api::vizierpb::ExecuteScriptResponse> out; |
| 94 | + for (const auto& chunk : result_server->raw_query_results()) { |
| 95 | + auto resp = ExecuteScriptResponseFromChunk(chunk, query_id); |
| 96 | + if (!resp.has_value()) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + out.push_back(std::move(*resp)); |
| 100 | + } |
| 101 | + return out; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace |
| 105 | + |
| 106 | +::grpc::Status DirectQueryServer::ExecuteScript( |
| 107 | + ::grpc::ServerContext* context, const ::px::api::vizierpb::ExecuteScriptRequest* request, |
| 108 | + ::grpc::ServerWriter<::px::api::vizierpb::ExecuteScriptResponse>* writer) { |
| 109 | + if (auto s = AuthenticateRequest(context, jwt_signing_key_); !s.ok()) { |
| 110 | + return s; |
| 111 | + } |
| 112 | + if (request->mutation()) { |
| 113 | + return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, |
| 114 | + "direct-query: mutations are out of scope (read-only endpoint)"); |
| 115 | + } |
| 116 | + if (carnot_ == nullptr || engine_state_ == nullptr || result_server_ == nullptr) { |
| 117 | + return ::grpc::Status(::grpc::StatusCode::FAILED_PRECONDITION, |
| 118 | + "direct-query: server not wired with a live Carnot"); |
| 119 | + } |
| 120 | + const auto query_id = sole::uuid4(); |
| 121 | + const std::string query_id_str = query_id.str(); |
| 122 | + |
| 123 | + const auto time_now = ::px::CurrentTimeNS(); |
| 124 | + |
| 125 | + auto compiler_state = |
| 126 | + engine_state_->CreateLocalExecutionCompilerState(time_now, kMaxOutputRowsPerTable); |
| 127 | + auto plan_or = ::px::carnot::planner::compiler::Compiler().Compile(request->query_str(), |
| 128 | + compiler_state.get()); |
| 129 | + if (!plan_or.ok()) { |
| 130 | + auto msg = absl::Substitute("direct-query: PxL compile failed ($0)", plan_or.msg()); |
| 131 | + VLOG(1) << msg; |
| 132 | + return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, msg); |
| 133 | + } |
| 134 | + const auto plan = plan_or.ConsumeValueOrDie(); |
| 135 | + EmitSchemaResponses(plan, query_id_str, writer); |
| 136 | + |
| 137 | + std::vector<::px::api::vizierpb::ExecuteScriptResponse> responses; |
| 138 | + { |
| 139 | + absl::MutexLock lk(&exec_mu_); |
| 140 | + result_server_->ResetQueryResults(); |
| 141 | + auto exec_s = carnot_->ExecuteQuery(request->query_str(), query_id, time_now); |
| 142 | + if (!exec_s.ok()) { |
| 143 | + auto msg = absl::Substitute("direct-query: PxL execute failed ($0)", exec_s.msg()); |
| 144 | + VLOG(1) << msg; |
| 145 | + return ::grpc::Status(::grpc::StatusCode::INTERNAL, msg); |
| 146 | + } |
| 147 | + responses = collectResponses(result_server_, query_id_str); |
| 148 | + } |
| 149 | + |
| 150 | + for (const auto& resp : responses) { |
| 151 | + if (context->IsCancelled()) { |
| 152 | + return ::grpc::Status(::grpc::StatusCode::CANCELLED, "direct-query: client cancelled"); |
| 153 | + } |
| 154 | + if (!writer->Write(resp)) { |
| 155 | + VLOG(1) << "direct-query: client stream closed mid-response, abandoning query " |
| 156 | + << query_id_str; |
| 157 | + break; |
| 158 | + } |
| 159 | + } |
| 160 | + return ::grpc::Status::OK; |
| 161 | +} |
| 162 | + |
| 163 | +} // namespace agent |
| 164 | +} // namespace vizier |
| 165 | +} // namespace px |
0 commit comments