|
34 | 34 | #include "src/core/xds/grpc/xds_common_types.h"
|
35 | 35 | #include "src/core/xds/grpc/xds_common_types_parser.h"
|
36 | 36 | #include "upb/base/string_view.h"
|
| 37 | +#include "upb/message/array.h" |
| 38 | +#include "upb/message/map.h" |
| 39 | +#include "upb/message/message.h" |
37 | 40 | #include "upb/text/encode.h"
|
38 | 41 |
|
39 | 42 | namespace grpc_core {
|
@@ -126,56 +129,66 @@ XdsMetadataMap ParseXdsMetadataMap(
|
126 | 129 | XdsMetadataMap metadata_map;
|
127 | 130 | if (metadata == nullptr) return metadata_map; // Not present == empty.
|
128 | 131 | // First, try typed_filter_metadata.
|
129 |
| - size_t iter = kUpb_Map_Begin; |
130 |
| - const envoy_config_core_v3_Metadata_TypedFilterMetadataEntry* typed_entry; |
131 |
| - while ( |
132 |
| - (typed_entry = envoy_config_core_v3_Metadata_typed_filter_metadata_next( |
133 |
| - metadata, &iter)) != nullptr) { |
134 |
| - absl::string_view key = UpbStringToAbsl( |
135 |
| - envoy_config_core_v3_Metadata_TypedFilterMetadataEntry_key( |
136 |
| - typed_entry)); |
137 |
| - ValidationErrors::ScopedField field( |
138 |
| - errors, absl::StrCat(".typed_filter_metadata[", key, "]")); |
139 |
| - auto extension = ExtractXdsExtension( |
140 |
| - context, |
141 |
| - envoy_config_core_v3_Metadata_TypedFilterMetadataEntry_value( |
142 |
| - typed_entry), |
143 |
| - errors); |
144 |
| - if (!extension.has_value()) continue; |
145 |
| - // TODO(roth): If we start to need a lot of types here, refactor |
146 |
| - // this into a separate registry. |
147 |
| - std::unique_ptr<XdsMetadataValue> metadata_value; |
148 |
| - if (XdsGcpAuthFilterEnabled() && |
149 |
| - extension->type == XdsGcpAuthnAudienceMetadataValue::Type()) { |
150 |
| - metadata_value = |
151 |
| - ParseGcpAuthnAudience(context, std::move(*extension), errors); |
152 |
| - } else if (XdsHttpConnectEnabled() && |
153 |
| - extension->type == XdsAddressMetadataValue::Type()) { |
154 |
| - metadata_value = ParseAddress(context, std::move(*extension), errors); |
155 |
| - } |
156 |
| - if (metadata_value != nullptr) { |
157 |
| - metadata_map.Insert(key, std::move(metadata_value)); |
| 132 | + // TODO(b/397931390): Clean up the code after gRPC OSS migrates to proto |
| 133 | + // v30.0. |
| 134 | + envoy_config_core_v3_Metadata* metadata_upb = |
| 135 | + (envoy_config_core_v3_Metadata*)metadata; |
| 136 | + const upb_Map* typed_filter_metadata_upb_map = |
| 137 | + _envoy_config_core_v3_Metadata_typed_filter_metadata_upb_map( |
| 138 | + metadata_upb); |
| 139 | + if (typed_filter_metadata_upb_map) { |
| 140 | + size_t iter = kUpb_Map_Begin; |
| 141 | + upb_MessageValue k, v; |
| 142 | + while (upb_Map_Next(typed_filter_metadata_upb_map, &k, &v, &iter)) { |
| 143 | + upb_StringView typed_filter_metadata_key_view = k.str_val; |
| 144 | + const google_protobuf_Any* typed_filter_metadata_val = |
| 145 | + (google_protobuf_Any*)v.msg_val; |
| 146 | + absl::string_view key = UpbStringToAbsl(typed_filter_metadata_key_view); |
| 147 | + ValidationErrors::ScopedField field( |
| 148 | + errors, absl::StrCat(".typed_filter_metadata[", key, "]")); |
| 149 | + auto extension = |
| 150 | + ExtractXdsExtension(context, typed_filter_metadata_val, errors); |
| 151 | + if (!extension.has_value()) continue; |
| 152 | + // TODO(roth): If we start to need a lot of types here, refactor |
| 153 | + // this into a separate registry. |
| 154 | + std::unique_ptr<XdsMetadataValue> metadata_value; |
| 155 | + if (XdsGcpAuthFilterEnabled() && |
| 156 | + extension->type == XdsGcpAuthnAudienceMetadataValue::Type()) { |
| 157 | + metadata_value = |
| 158 | + ParseGcpAuthnAudience(context, std::move(*extension), errors); |
| 159 | + } else if (XdsHttpConnectEnabled() && |
| 160 | + extension->type == XdsAddressMetadataValue::Type()) { |
| 161 | + metadata_value = ParseAddress(context, std::move(*extension), errors); |
| 162 | + } |
| 163 | + if (metadata_value != nullptr) { |
| 164 | + metadata_map.Insert(key, std::move(metadata_value)); |
| 165 | + } |
158 | 166 | }
|
159 | 167 | }
|
160 | 168 | // Then, try filter_metadata.
|
161 |
| - iter = kUpb_Map_Begin; |
162 |
| - const envoy_config_core_v3_Metadata_FilterMetadataEntry* entry; |
163 |
| - while ((entry = envoy_config_core_v3_Metadata_filter_metadata_next( |
164 |
| - metadata, &iter)) != nullptr) { |
165 |
| - absl::string_view key = UpbStringToAbsl( |
166 |
| - envoy_config_core_v3_Metadata_FilterMetadataEntry_key(entry)); |
167 |
| - auto json = ParseProtobufStructToJson( |
168 |
| - context, |
169 |
| - envoy_config_core_v3_Metadata_FilterMetadataEntry_value(entry)); |
170 |
| - if (!json.ok()) { |
171 |
| - ValidationErrors::ScopedField field( |
172 |
| - errors, absl::StrCat(".filter_metadata[", key, "]")); |
173 |
| - errors->AddError(json.status().message()); |
174 |
| - } |
175 |
| - // Add only if not already added from typed_filter_metadata. |
176 |
| - else if (metadata_map.Find(key) == nullptr) { |
177 |
| - metadata_map.Insert( |
178 |
| - key, std::make_unique<XdsStructMetadataValue>(std::move(*json))); |
| 169 | + // TODO(b/397931390): Clean up the code after gRPC OSS migrates to proto |
| 170 | + // v30.0. |
| 171 | + const upb_Map* filter_metadata_upb_map = |
| 172 | + _envoy_config_core_v3_Metadata_filter_metadata_upb_map(metadata_upb); |
| 173 | + if (filter_metadata_upb_map) { |
| 174 | + size_t iter = kUpb_Map_Begin; |
| 175 | + upb_MessageValue k, v; |
| 176 | + while (upb_Map_Next(filter_metadata_upb_map, &k, &v, &iter)) { |
| 177 | + upb_StringView filter_metadata_key_view = k.str_val; |
| 178 | + const google_protobuf_Struct* filter_metadata_val = |
| 179 | + (google_protobuf_Struct*)v.msg_val; |
| 180 | + absl::string_view key = UpbStringToAbsl(filter_metadata_key_view); |
| 181 | + auto json = ParseProtobufStructToJson(context, filter_metadata_val); |
| 182 | + if (!json.ok()) { |
| 183 | + ValidationErrors::ScopedField field( |
| 184 | + errors, absl::StrCat(".filter_metadata[", key, "]")); |
| 185 | + errors->AddError(json.status().message()); |
| 186 | + } |
| 187 | + // Add only if not already added from typed_filter_metadata. |
| 188 | + else if (metadata_map.Find(key) == nullptr) { |
| 189 | + metadata_map.Insert( |
| 190 | + key, std::make_unique<XdsStructMetadataValue>(std::move(*json))); |
| 191 | + } |
179 | 192 | }
|
180 | 193 | }
|
181 | 194 | return metadata_map;
|
|
0 commit comments