• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2015 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 #ifndef GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_COMPOSITE_COMPOSITE_CREDENTIALS_H
20 #define GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_COMPOSITE_COMPOSITE_CREDENTIALS_H
21 
22 #include <grpc/credentials.h>
23 #include <grpc/grpc.h>
24 #include <grpc/grpc_security.h>
25 #include <grpc/grpc_security_constants.h>
26 #include <grpc/support/port_platform.h>
27 
28 #include <algorithm>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 #include "absl/status/statusor.h"
34 #include "src/core/lib/channel/channel_args.h"
35 #include "src/core/lib/promise/arena_promise.h"
36 #include "src/core/lib/security/credentials/credentials.h"
37 #include "src/core/lib/security/security_connector/security_connector.h"
38 #include "src/core/lib/transport/transport.h"
39 #include "src/core/util/ref_counted_ptr.h"
40 #include "src/core/util/unique_type_name.h"
41 #include "src/core/util/useful.h"
42 
43 // -- Composite channel credentials. --
44 
45 class grpc_composite_channel_credentials : public grpc_channel_credentials {
46  public:
grpc_composite_channel_credentials(grpc_core::RefCountedPtr<grpc_channel_credentials> channel_creds,grpc_core::RefCountedPtr<grpc_call_credentials> call_creds)47   grpc_composite_channel_credentials(
48       grpc_core::RefCountedPtr<grpc_channel_credentials> channel_creds,
49       grpc_core::RefCountedPtr<grpc_call_credentials> call_creds)
50       : inner_creds_(std::move(channel_creds)),
51         call_creds_(std::move(call_creds)) {}
52 
53   ~grpc_composite_channel_credentials() override = default;
54 
55   grpc_core::RefCountedPtr<grpc_channel_credentials>
duplicate_without_call_credentials()56   duplicate_without_call_credentials() override {
57     return inner_creds_->duplicate_without_call_credentials();
58   }
59 
60   grpc_core::RefCountedPtr<grpc_channel_security_connector>
61   create_security_connector(
62       grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
63       const char* target, grpc_core::ChannelArgs* args) override;
64 
update_arguments(grpc_core::ChannelArgs args)65   grpc_core::ChannelArgs update_arguments(
66       grpc_core::ChannelArgs args) override {
67     return inner_creds_->update_arguments(std::move(args));
68   }
69 
70   static grpc_core::UniqueTypeName Type();
71 
type()72   grpc_core::UniqueTypeName type() const override { return Type(); }
73 
inner_creds()74   const grpc_channel_credentials* inner_creds() const {
75     return inner_creds_.get();
76   }
call_creds()77   const grpc_call_credentials* call_creds() const { return call_creds_.get(); }
mutable_call_creds()78   grpc_call_credentials* mutable_call_creds() { return call_creds_.get(); }
79 
80  private:
cmp_impl(const grpc_channel_credentials * other)81   int cmp_impl(const grpc_channel_credentials* other) const override {
82     auto* o = static_cast<const grpc_composite_channel_credentials*>(other);
83     int r = inner_creds_->cmp(o->inner_creds_.get());
84     if (r != 0) return r;
85     return call_creds_->cmp(o->call_creds_.get());
86   }
87 
88   grpc_core::RefCountedPtr<grpc_channel_credentials> inner_creds_;
89   grpc_core::RefCountedPtr<grpc_call_credentials> call_creds_;
90 };
91 
92 // -- Composite call credentials. --
93 
94 class grpc_composite_call_credentials : public grpc_call_credentials {
95  public:
96   using CallCredentialsList =
97       std::vector<grpc_core::RefCountedPtr<grpc_call_credentials>>;
98 
99   grpc_composite_call_credentials(
100       grpc_core::RefCountedPtr<grpc_call_credentials> creds1,
101       grpc_core::RefCountedPtr<grpc_call_credentials> creds2);
102   ~grpc_composite_call_credentials() override = default;
103 
Orphaned()104   void Orphaned() override { inner_.clear(); }
105 
106   grpc_core::ArenaPromise<absl::StatusOr<grpc_core::ClientMetadataHandle>>
107   GetRequestMetadata(grpc_core::ClientMetadataHandle initial_metadata,
108                      const GetRequestMetadataArgs* args) override;
109 
min_security_level()110   grpc_security_level min_security_level() const override {
111     return min_security_level_;
112   }
113 
inner()114   const CallCredentialsList& inner() const { return inner_; }
115   std::string debug_string() override;
116 
117   static grpc_core::UniqueTypeName Type();
118 
type()119   grpc_core::UniqueTypeName type() const override { return Type(); }
120 
121  private:
cmp_impl(const grpc_call_credentials * other)122   int cmp_impl(const grpc_call_credentials* other) const override {
123     // TODO(yashykt): Check if we can do something better here
124     return grpc_core::QsortCompare(
125         static_cast<const grpc_call_credentials*>(this), other);
126   }
127 
128   void push_to_inner(grpc_core::RefCountedPtr<grpc_call_credentials> creds,
129                      bool is_composite);
130   grpc_security_level min_security_level_;
131   CallCredentialsList inner_;
132 };
133 
134 #endif  // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_COMPOSITE_COMPOSITE_CREDENTIALS_H
135