1 //
2 //
3 // Copyright 2022 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 <memory>
22 
23 #include "absl/strings/string_view.h"
24 
25 #include <grpc/grpc.h>
26 #include <grpc/grpc_security.h>
27 
28 #include "src/core/lib/config/core_configuration.h"
29 #include "src/core/lib/gprpp/ref_counted_ptr.h"
30 #include "src/core/lib/json/json.h"
31 #include "src/core/lib/security/credentials/channel_creds_registry.h"
32 #include "src/core/lib/security/credentials/credentials.h"
33 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
34 #include "src/core/lib/security/credentials/google_default/google_default_credentials.h"  // IWYU pragma: keep
35 
36 namespace grpc_core {
37 
38 class GoogleDefaultChannelCredsFactory : public ChannelCredsFactory<> {
39  public:
creds_type() const40   absl::string_view creds_type() const override { return "google_default"; }
IsValidConfig(const Json &) const41   bool IsValidConfig(const Json& /*config*/) const override { return true; }
CreateChannelCreds(const Json &) const42   RefCountedPtr<grpc_channel_credentials> CreateChannelCreds(
43       const Json& /*config*/) const override {
44     return RefCountedPtr<grpc_channel_credentials>(
45         grpc_google_default_credentials_create(nullptr));
46   }
47 };
48 
49 class InsecureChannelCredsFactory : public ChannelCredsFactory<> {
50  public:
creds_type() const51   absl::string_view creds_type() const override { return "insecure"; }
IsValidConfig(const Json &) const52   bool IsValidConfig(const Json& /*config*/) const override { return true; }
CreateChannelCreds(const Json &) const53   RefCountedPtr<grpc_channel_credentials> CreateChannelCreds(
54       const Json& /*config*/) const override {
55     return RefCountedPtr<grpc_channel_credentials>(
56         grpc_insecure_credentials_create());
57   }
58 };
59 
60 class FakeChannelCredsFactory : public ChannelCredsFactory<> {
61  public:
creds_type() const62   absl::string_view creds_type() const override { return "fake"; }
IsValidConfig(const Json &) const63   bool IsValidConfig(const Json& /*config*/) const override { return true; }
CreateChannelCreds(const Json &) const64   RefCountedPtr<grpc_channel_credentials> CreateChannelCreds(
65       const Json& /*config*/) const override {
66     return RefCountedPtr<grpc_channel_credentials>(
67         grpc_fake_transport_security_credentials_create());
68   }
69 };
70 
RegisterChannelDefaultCreds(CoreConfiguration::Builder * builder)71 void RegisterChannelDefaultCreds(CoreConfiguration::Builder* builder) {
72   builder->channel_creds_registry()->RegisterChannelCredsFactory(
73       std::make_unique<GoogleDefaultChannelCredsFactory>());
74   builder->channel_creds_registry()->RegisterChannelCredsFactory(
75       std::make_unique<InsecureChannelCredsFactory>());
76   builder->channel_creds_registry()->RegisterChannelCredsFactory(
77       std::make_unique<FakeChannelCredsFactory>());
78 }
79 
80 }  // namespace grpc_core
81