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 <grpc/support/port_platform.h>
20
21 #include "src/core/ext/gcp/metadata_query.h"
22
23 #include <string.h>
24
25 #include <memory>
26 #include <utility>
27
28 #include "absl/status/status.h"
29 #include "absl/status/statusor.h"
30 #include "absl/strings/str_format.h"
31 #include "absl/strings/string_view.h"
32
33 #include <grpc/grpc.h>
34 #include <grpc/grpc_security.h>
35 #include <grpc/support/log.h>
36
37 #include "src/core/lib/debug/trace.h"
38 #include "src/core/lib/gprpp/ref_counted_ptr.h"
39 #include "src/core/lib/gprpp/status_helper.h"
40 #include "src/core/lib/gprpp/time.h"
41 #include "src/core/lib/security/credentials/credentials.h"
42 #include "src/core/lib/uri/uri_parser.h"
43
44 namespace grpc_core {
45
46 TraceFlag grpc_metadata_query_trace(false, "metadata_query");
47
48 constexpr const char MetadataQuery::kZoneAttribute[];
49 constexpr const char MetadataQuery::kClusterNameAttribute[];
50 constexpr const char MetadataQuery::kRegionAttribute[];
51 constexpr const char MetadataQuery::kInstanceIdAttribute[];
52 constexpr const char MetadataQuery::kIPv6Attribute[];
53
MetadataQuery(std::string attribute,grpc_polling_entity * pollent,absl::AnyInvocable<void (std::string,absl::StatusOr<std::string>)> callback,Duration timeout)54 MetadataQuery::MetadataQuery(
55 std::string attribute, grpc_polling_entity* pollent,
56 absl::AnyInvocable<void(std::string /* attribute */,
57 absl::StatusOr<std::string> /* result */)>
58 callback,
59 Duration timeout)
60 : MetadataQuery("metadata.google.internal.", std::move(attribute), pollent,
61 std::move(callback), timeout) {}
62
MetadataQuery(std::string metadata_server_name,std::string attribute,grpc_polling_entity * pollent,absl::AnyInvocable<void (std::string,absl::StatusOr<std::string>)> callback,Duration timeout)63 MetadataQuery::MetadataQuery(
64 std::string metadata_server_name, std::string attribute,
65 grpc_polling_entity* pollent,
66 absl::AnyInvocable<void(std::string /* attribute */,
67 absl::StatusOr<std::string> /* result */)>
68 callback,
69 Duration timeout)
70 : InternallyRefCounted<MetadataQuery>(nullptr, 2),
71 attribute_(std::move(attribute)),
72 callback_(std::move(callback)) {
73 GRPC_CLOSURE_INIT(&on_done_, OnDone, this, nullptr);
74 auto uri = URI::Create("http", std::move(metadata_server_name), attribute_,
75 {} /* query params */, "" /* fragment */);
76 GPR_ASSERT(uri.ok()); // params are hardcoded
77 grpc_http_request request;
78 memset(&request, 0, sizeof(grpc_http_request));
79 grpc_http_header header = {const_cast<char*>("Metadata-Flavor"),
80 const_cast<char*>("Google")};
81 request.hdr_count = 1;
82 request.hdrs = &header;
83 http_request_ = HttpRequest::Get(
84 std::move(*uri), nullptr /* channel args */, pollent, &request,
85 Timestamp::Now() + timeout, &on_done_, &response_,
86 RefCountedPtr<grpc_channel_credentials>(
87 grpc_insecure_credentials_create()));
88 http_request_->Start();
89 }
90
~MetadataQuery()91 MetadataQuery::~MetadataQuery() { grpc_http_response_destroy(&response_); }
92
Orphan()93 void MetadataQuery::Orphan() {
94 http_request_.reset();
95 Unref();
96 }
97
OnDone(void * arg,grpc_error_handle error)98 void MetadataQuery::OnDone(void* arg, grpc_error_handle error) {
99 auto* self = static_cast<MetadataQuery*>(arg);
100 if (GRPC_TRACE_FLAG_ENABLED(grpc_metadata_query_trace)) {
101 gpr_log(GPR_INFO, "MetadataServer Query for %s: HTTP status: %d, error: %s",
102 self->attribute_.c_str(), self->response_.status,
103 StatusToString(error).c_str());
104 }
105 absl::StatusOr<std::string> result;
106 if (!error.ok()) {
107 result = absl::UnavailableError(absl::StrFormat(
108 "MetadataServer Query failed for %s: %s", self->attribute_.c_str(),
109 StatusToString(error).c_str()));
110 } else if (self->response_.status != 200) {
111 result = absl::UnavailableError(absl::StrFormat(
112 "MetadataServer Query received non-200 status for %s: %s",
113 self->attribute_.c_str(), StatusToString(error).c_str()));
114 } else if (self->attribute_ == kZoneAttribute) {
115 absl::string_view body(self->response_.body, self->response_.body_length);
116 size_t pos = body.find_last_of('/');
117 if (pos == body.npos) {
118 result = absl::UnavailableError(
119 absl::StrFormat("MetadataServer Could not parse zone: %s",
120 std::string(body).c_str()));
121 if (GRPC_TRACE_FLAG_ENABLED(grpc_metadata_query_trace)) {
122 gpr_log(GPR_INFO, "%s", result.status().ToString().c_str());
123 }
124 } else {
125 result = std::string(body.substr(pos + 1));
126 }
127 } else {
128 result = std::string(self->response_.body, self->response_.body_length);
129 }
130 auto callback = std::move(self->callback_);
131 auto attribute = std::move(self->attribute_);
132 self->Unref();
133 return callback(std::move(attribute), std::move(result));
134 }
135
136 } // namespace grpc_core
137