1 //
2 // Copyright 2024 gRPC 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
17 #include "src/core/ext/filters/gcp_authentication/gcp_authentication_service_config_parser.h"
18
19 #include <grpc/support/port_platform.h>
20
21 #include <vector>
22
23 #include "absl/types/optional.h"
24 #include "src/core/lib/channel/channel_args.h"
25
26 namespace grpc_core {
27
JsonLoader(const JsonArgs &)28 const JsonLoaderInterface* GcpAuthenticationParsedConfig::Config::JsonLoader(
29 const JsonArgs&) {
30 static const auto* loader =
31 JsonObjectLoader<Config>()
32 .Field("filter_instance_name", &Config::filter_instance_name)
33 .OptionalField("cache_size", &Config::cache_size)
34 .Finish();
35 return loader;
36 }
37
JsonPostLoad(const Json &,const JsonArgs &,ValidationErrors * errors)38 void GcpAuthenticationParsedConfig::Config::JsonPostLoad(
39 const Json&, const JsonArgs&, ValidationErrors* errors) {
40 if (cache_size == 0) {
41 ValidationErrors::ScopedField field(errors, ".cache_size");
42 errors->AddError("must be non-zero");
43 }
44 }
45
JsonLoader(const JsonArgs &)46 const JsonLoaderInterface* GcpAuthenticationParsedConfig::JsonLoader(
47 const JsonArgs&) {
48 static const auto* loader =
49 JsonObjectLoader<GcpAuthenticationParsedConfig>()
50 .OptionalField("gcp_authentication",
51 &GcpAuthenticationParsedConfig::configs_)
52 .Finish();
53 return loader;
54 }
55
56 std::unique_ptr<ServiceConfigParser::ParsedConfig>
ParseGlobalParams(const ChannelArgs & args,const Json & json,ValidationErrors * errors)57 GcpAuthenticationServiceConfigParser::ParseGlobalParams(
58 const ChannelArgs& args, const Json& json, ValidationErrors* errors) {
59 // Only parse config if the following channel arg is enabled.
60 if (!args.GetBool(GRPC_ARG_PARSE_GCP_AUTHENTICATION_METHOD_CONFIG)
61 .value_or(false)) {
62 return nullptr;
63 }
64 // Parse config from json.
65 return LoadFromJson<std::unique_ptr<GcpAuthenticationParsedConfig>>(
66 json, JsonArgs(), errors);
67 }
68
Register(CoreConfiguration::Builder * builder)69 void GcpAuthenticationServiceConfigParser::Register(
70 CoreConfiguration::Builder* builder) {
71 builder->service_config_parser()->RegisterParser(
72 std::make_unique<GcpAuthenticationServiceConfigParser>());
73 }
74
ParserIndex()75 size_t GcpAuthenticationServiceConfigParser::ParserIndex() {
76 return CoreConfiguration::Get().service_config_parser().GetParserIndex(
77 parser_name());
78 }
79
80 } // namespace grpc_core
81