• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2023 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #include "src/core/util/gcp_metadata_query.h"
20 
21 #include <grpc/credentials.h>
22 #include <grpc/grpc.h>
23 #include <grpc/grpc_security.h>
24 #include <grpc/support/port_platform.h>
25 #include <string.h>
26 
27 #include <memory>
28 #include <utility>
29 
30 #include "absl/log/check.h"
31 #include "absl/log/log.h"
32 #include "absl/status/status.h"
33 #include "absl/status/statusor.h"
34 #include "absl/strings/str_format.h"
35 #include "absl/strings/string_view.h"
36 #include "src/core/lib/debug/trace.h"
37 #include "src/core/lib/security/credentials/credentials.h"
38 #include "src/core/util/ref_counted_ptr.h"
39 #include "src/core/util/status_helper.h"
40 #include "src/core/util/time.h"
41 #include "src/core/util/uri.h"
42 
43 namespace grpc_core {
44 
45 constexpr const char GcpMetadataQuery::kZoneAttribute[];
46 constexpr const char GcpMetadataQuery::kClusterNameAttribute[];
47 constexpr const char GcpMetadataQuery::kRegionAttribute[];
48 constexpr const char GcpMetadataQuery::kInstanceIdAttribute[];
49 constexpr const char GcpMetadataQuery::kIPv6Attribute[];
50 
GcpMetadataQuery(std::string attribute,grpc_polling_entity * pollent,absl::AnyInvocable<void (std::string,absl::StatusOr<std::string>)> callback,Duration timeout)51 GcpMetadataQuery::GcpMetadataQuery(
52     std::string attribute, grpc_polling_entity* pollent,
53     absl::AnyInvocable<void(std::string /* attribute */,
54                             absl::StatusOr<std::string> /* result */)>
55         callback,
56     Duration timeout)
57     : GcpMetadataQuery("metadata.google.internal.", std::move(attribute),
58                        pollent, std::move(callback), timeout) {}
59 
GcpMetadataQuery(std::string metadata_server_name,std::string attribute,grpc_polling_entity * pollent,absl::AnyInvocable<void (std::string,absl::StatusOr<std::string>)> callback,Duration timeout)60 GcpMetadataQuery::GcpMetadataQuery(
61     std::string metadata_server_name, std::string attribute,
62     grpc_polling_entity* pollent,
63     absl::AnyInvocable<void(std::string /* attribute */,
64                             absl::StatusOr<std::string> /* result */)>
65         callback,
66     Duration timeout)
67     : InternallyRefCounted<GcpMetadataQuery>(nullptr, 2),
68       attribute_(std::move(attribute)),
69       callback_(std::move(callback)) {
70   GRPC_CLOSURE_INIT(&on_done_, OnDone, this, nullptr);
71   auto uri = URI::Create("http", std::move(metadata_server_name), attribute_,
72                          {} /* query params */, "" /* fragment */);
73   CHECK(uri.ok());  // params are hardcoded
74   grpc_http_request request;
75   memset(&request, 0, sizeof(grpc_http_request));
76   grpc_http_header header = {const_cast<char*>("Metadata-Flavor"),
77                              const_cast<char*>("Google")};
78   request.hdr_count = 1;
79   request.hdrs = &header;
80   http_request_ = HttpRequest::Get(
81       std::move(*uri), nullptr /* channel args */, pollent, &request,
82       Timestamp::Now() + timeout, &on_done_, &response_,
83       RefCountedPtr<grpc_channel_credentials>(
84           grpc_insecure_credentials_create()));
85   http_request_->Start();
86 }
87 
~GcpMetadataQuery()88 GcpMetadataQuery::~GcpMetadataQuery() {
89   grpc_http_response_destroy(&response_);
90 }
91 
Orphan()92 void GcpMetadataQuery::Orphan() {
93   http_request_.reset();
94   Unref();
95 }
96 
OnDone(void * arg,grpc_error_handle error)97 void GcpMetadataQuery::OnDone(void* arg, grpc_error_handle error) {
98   auto* self = static_cast<GcpMetadataQuery*>(arg);
99   GRPC_TRACE_LOG(metadata_query, INFO)
100       << "MetadataServer Query for " << self->attribute_
101       << ": HTTP status: " << self->response_.status
102       << ", error: " << StatusToString(error);
103   absl::StatusOr<std::string> result;
104   if (!error.ok()) {
105     result = absl::UnavailableError(absl::StrFormat(
106         "MetadataServer Query failed for %s: %s", self->attribute_.c_str(),
107         StatusToString(error).c_str()));
108   } else if (self->response_.status != 200) {
109     result = absl::UnavailableError(absl::StrFormat(
110         "MetadataServer Query received non-200 status for %s: %s",
111         self->attribute_.c_str(), StatusToString(error).c_str()));
112   } else if (self->attribute_ == kZoneAttribute) {
113     absl::string_view body(self->response_.body, self->response_.body_length);
114     size_t pos = body.find_last_of('/');
115     if (pos == body.npos) {
116       result = absl::UnavailableError(
117           absl::StrFormat("MetadataServer Could not parse zone: %s",
118                           std::string(body).c_str()));
119       GRPC_TRACE_LOG(metadata_query, INFO) << result.status();
120     } else {
121       result = std::string(body.substr(pos + 1));
122     }
123   } else {
124     result = std::string(self->response_.body, self->response_.body_length);
125   }
126   auto callback = std::move(self->callback_);
127   auto attribute = std::move(self->attribute_);
128   self->Unref();
129   return callback(std::move(attribute), std::move(result));
130 }
131 
132 }  // namespace grpc_core
133