GRPC C++  1.30.0
credentials_impl.h
Go to the documentation of this file.
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 GRPCPP_SECURITY_CREDENTIALS_IMPL_H
20 #define GRPCPP_SECURITY_CREDENTIALS_IMPL_H
21 
22 #include <map>
23 #include <memory>
24 #include <vector>
25 
27 #include <grpcpp/channel_impl.h>
33 #include <grpcpp/support/status.h>
35 
36 struct grpc_call;
37 
38 namespace grpc_impl {
39 
40 class ChannelCredentials;
41 class CallCredentials;
44 
45 std::shared_ptr<Channel> CreateCustomChannelImpl(
46  const grpc::string& target,
47  const std::shared_ptr<ChannelCredentials>& creds,
48  const ChannelArguments& args);
49 
50 namespace experimental {
51 std::shared_ptr<Channel> CreateCustomChannelWithInterceptors(
52  const grpc::string& target,
53  const std::shared_ptr<ChannelCredentials>& creds,
54  const ChannelArguments& args,
55  std::vector<
56  std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
57  interceptor_creators);
58 }
59 
67  public:
70 
71  protected:
72  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
73  const std::shared_ptr<ChannelCredentials>& channel_creds,
74  const std::shared_ptr<CallCredentials>& call_creds);
75 
77 
78  private:
79  friend std::shared_ptr<Channel> CreateCustomChannelImpl(
80  const grpc::string& target,
81  const std::shared_ptr<ChannelCredentials>& creds,
82  const ChannelArguments& args);
83 
84  friend std::shared_ptr<Channel>
86  const grpc::string& target,
87  const std::shared_ptr<ChannelCredentials>& creds,
88  const ChannelArguments& args,
89  std::vector<std::unique_ptr<
91  interceptor_creators);
92 
93  virtual std::shared_ptr<Channel> CreateChannelImpl(
94  const grpc::string& target, const ChannelArguments& args) = 0;
95 
96  // This function should have been a pure virtual function, but it is
97  // implemented as a virtual function so that it does not break API.
98  virtual std::shared_ptr<Channel> CreateChannelWithInterceptors(
99  const grpc::string& /*target*/, const ChannelArguments& /*args*/,
100  std::vector<std::unique_ptr<
102  /*interceptor_creators*/) {
103  return nullptr;
104  }
105 };
106 
112  public:
113  CallCredentials();
115 
117  virtual bool ApplyToCall(grpc_call* call) = 0;
119  return "CallCredentials did not provide a debug string";
120  }
121 
122  protected:
123  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
124  const std::shared_ptr<ChannelCredentials>& channel_creds,
125  const std::shared_ptr<CallCredentials>& call_creds);
126 
127  friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
128  const std::shared_ptr<CallCredentials>& creds1,
129  const std::shared_ptr<CallCredentials>& creds2);
130 
132 };
133 
142 
146 
151 };
152 
153 // Factories for building different types of Credentials The functions may
154 // return empty shared_ptr when credentials cannot be created. If a
155 // Credentials pointer is returned, it can still be invalid when used to create
156 // a channel. A lame channel will be created then and all rpcs will fail on it.
157 
164 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
165 
167 std::shared_ptr<ChannelCredentials> SslCredentials(
168  const SslCredentialsOptions& options);
169 
176 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
177 
178 constexpr long kMaxAuthTokenLifetimeSecs = 3600;
179 
185 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
186  const grpc::string& json_key,
187  long token_lifetime_seconds = grpc_impl::kMaxAuthTokenLifetimeSecs);
188 
197 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
198  const grpc::string& json_refresh_token);
199 
208 std::shared_ptr<CallCredentials> AccessTokenCredentials(
209  const grpc::string& access_token);
210 
217 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
218  const grpc::string& authorization_token,
219  const grpc::string& authority_selector);
220 
223 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
224  const std::shared_ptr<ChannelCredentials>& channel_creds,
225  const std::shared_ptr<CallCredentials>& call_creds);
226 
228 std::shared_ptr<CallCredentials> CompositeCallCredentials(
229  const std::shared_ptr<CallCredentials>& creds1,
230  const std::shared_ptr<CallCredentials>& creds2);
231 
233 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
234 
237  public:
239 
242  virtual bool IsBlocking() const { return true; }
243 
245  virtual const char* GetType() const { return ""; }
246 
252  virtual grpc::Status GetMetadata(
253  grpc::string_ref service_url, grpc::string_ref method_name,
254  const grpc::AuthContext& channel_auth_context,
255  std::multimap<grpc::string, grpc::string>* metadata) = 0;
256 
258  return "MetadataCredentialsPlugin did not provide a debug string";
259  }
260 };
261 
262 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
263  std::unique_ptr<MetadataCredentialsPlugin> plugin);
264 
265 namespace experimental {
266 
274  grpc::string resource; // Optional.
275  grpc::string audience; // Optional.
276  grpc::string scope; // Optional.
282 };
283 
321  StsCredentialsOptions* options);
322 
327 
328 std::shared_ptr<CallCredentials> StsCredentials(
329  const StsCredentialsOptions& options);
330 
331 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
332  std::unique_ptr<MetadataCredentialsPlugin> plugin,
333  grpc_security_level min_security_level);
334 
340  std::vector<grpc::string> target_service_accounts;
341 };
342 
344 std::shared_ptr<ChannelCredentials> AltsCredentials(
345  const AltsCredentialsOptions& options);
346 
348 std::shared_ptr<ChannelCredentials> LocalCredentials(
350 
352 std::shared_ptr<ChannelCredentials> TlsCredentials(
353  const TlsCredentialsOptions& options);
354 
355 } // namespace experimental
356 } // namespace grpc_impl
357 
358 #endif // GRPCPP_SECURITY_CREDENTIALS_IMPL_H
grpc::GrpcLibraryCodegen
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
grpc_impl::ChannelCredentials::CompositeChannelCredentials
friend std::shared_ptr< ChannelCredentials > CompositeChannelCredentials(const std::shared_ptr< ChannelCredentials > &channel_creds, const std::shared_ptr< CallCredentials > &call_creds)
Combines a channel credentials and a call credentials into a composite channel credentials.
Definition: credentials.h:77
grpc_impl::ChannelCredentials::~ChannelCredentials
~ChannelCredentials()
grpc_impl::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
grpc::string_ref
This class is a non owning reference to a string.
Definition: string_ref.h:41
grpc_impl::ChannelCredentials::AsSecureCredentials
virtual SecureChannelCredentials * AsSecureCredentials()=0
tls_credentials_options.h
grpc_impl::CompositeChannelCredentials
std::shared_ptr< ChannelCredentials > CompositeChannelCredentials(const std::shared_ptr< ChannelCredentials > &channel_creds, const std::shared_ptr< CallCredentials > &call_creds)
Combines a channel credentials and a call credentials into a composite channel credentials.
Definition: credentials.h:77
grpc_impl::experimental::StsCredentialsOptions::actor_token_path
grpc::string actor_token_path
Definition: credentials_impl.h:280
grpc_impl::experimental::StsCredentials
std::shared_ptr< CallCredentials > StsCredentials(const StsCredentialsOptions &options)
grpc_impl::ChannelCredentials::ChannelCredentials
ChannelCredentials()
grpc_impl::AccessTokenCredentials
std::shared_ptr< CallCredentials > AccessTokenCredentials(const grpc::string &access_token)
Builds access token credentials.
channel_impl.h
grpc_impl::GoogleComputeEngineCredentials
std::shared_ptr< CallCredentials > GoogleComputeEngineCredentials()
Builds credentials for use when running in GCE.
grpc_impl::kMaxAuthTokenLifetimeSecs
constexpr long kMaxAuthTokenLifetimeSecs
Definition: credentials_impl.h:178
grpc_impl::experimental::MetadataCredentialsFromPlugin
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin, grpc_security_level min_security_level)
grpc_impl::ChannelCredentials::CreateCustomChannelImpl
friend std::shared_ptr< Channel > CreateCustomChannelImpl(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
grpc_impl::experimental::StsCredentialsOptions::subject_token_path
grpc::string subject_token_path
Definition: credentials_impl.h:278
grpc_impl::experimental::TlsCredentials
std::shared_ptr< ChannelCredentials > TlsCredentials(const TlsCredentialsOptions &options)
Builds TLS Credentials given TLS options.
grpc_impl::MetadataCredentialsPlugin::GetMetadata
virtual grpc::Status GetMetadata(grpc::string_ref service_url, grpc::string_ref method_name, const grpc::AuthContext &channel_auth_context, std::multimap< grpc::string, grpc::string > *metadata)=0
Gets the auth metatada produced by this plugin.
grpc_impl::experimental::StsCredentialsOptions::token_exchange_service_uri
grpc::string token_exchange_service_uri
Definition: credentials_impl.h:273
grpc_impl::CallCredentials::CallCredentials
CallCredentials()
grpc_impl::GoogleRefreshTokenCredentials
std::shared_ptr< CallCredentials > GoogleRefreshTokenCredentials(const grpc::string &json_refresh_token)
Builds refresh token credentials.
status.h
grpc_impl::experimental::StsCredentialsOptions::resource
grpc::string resource
Definition: credentials_impl.h:274
grpc::ChannelCredentials
::grpc_impl::ChannelCredentials ChannelCredentials
Definition: credentials.h:26
grpc_impl::MetadataCredentialsPlugin::~MetadataCredentialsPlugin
virtual ~MetadataCredentialsPlugin()
Definition: credentials_impl.h:238
grpc_impl::MetadataCredentialsFromPlugin
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin)
grpc_impl::CallCredentials::~CallCredentials
~CallCredentials()
grpc_impl::experimental::CreateCustomChannelWithInterceptors
std::shared_ptr<::grpc::Channel > CreateCustomChannelWithInterceptors(const grpc::string &target, const std::shared_ptr< grpc::ChannelCredentials > &creds, const ::grpc::ChannelArguments &args, std::vector< std::unique_ptr< grpc::experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
Create a new custom Channel pointing to target with interceptors being invoked per call.
grpc::SecureCallCredentials
::grpc_impl::SecureCallCredentials SecureCallCredentials
Definition: credentials.h:29
grpc_impl::SslCredentialsOptions::pem_private_key
grpc::string pem_private_key
The buffer containing the PEM encoding of the client's private key.
Definition: credentials_impl.h:145
grpc_impl::CallCredentials::AsSecureCredentials
virtual SecureCallCredentials * AsSecureCredentials()=0
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc_impl::CallCredentials::CompositeChannelCredentials
friend std::shared_ptr< ChannelCredentials > CompositeChannelCredentials(const std::shared_ptr< ChannelCredentials > &channel_creds, const std::shared_ptr< CallCredentials > &call_creds)
Combines a channel credentials and a call credentials into a composite channel credentials.
Definition: credentials.h:77
grpc::ChannelArguments
::grpc_impl::ChannelArguments ChannelArguments
Definition: channel_arguments.h:32
grpc_impl::experimental::StsCredentialsOptionsFromEnv
grpc::Status StsCredentialsOptionsFromEnv(StsCredentialsOptions *options)
Creates STS credentials options from the $STS_CREDENTIALS environment variable.
grpc_impl::experimental::TlsCredentialsOptions
TLS credentials options, wrapper for grpc_tls_credentials_options.
Definition: tls_credentials_options.h:279
grpc_impl::experimental::AltsCredentialsOptions::target_service_accounts
std::vector< grpc::string > target_service_accounts
service accounts of target endpoint that will be acceptable by the client.
Definition: credentials_impl.h:340
grpc_impl::MetadataCredentialsPlugin
User defined metadata credentials.
Definition: credentials_impl.h:236
grpc_impl::CallCredentials::DebugString
virtual grpc::string DebugString()
Definition: credentials_impl.h:118
grpc_impl::SslCredentialsOptions
Options used to build SslCredentials.
Definition: credentials_impl.h:135
grpc_impl::SslCredentials
std::shared_ptr< ChannelCredentials > SslCredentials(const SslCredentialsOptions &options)
Builds SSL Credentials given SSL specific options.
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc_impl::experimental::StsCredentialsOptions
Options for creating STS Oauth Token Exchange credentials following the IETF draft https://tools....
Definition: credentials_impl.h:272
grpc_impl::experimental::StsCredentialsOptions::scope
grpc::string scope
Definition: credentials_impl.h:276
grpc_impl::CreateCustomChannelImpl
std::shared_ptr<::grpc::Channel > CreateCustomChannelImpl(const grpc::string &target, const std::shared_ptr<::grpc::ChannelCredentials > &creds, const ::grpc::ChannelArguments &args)
Create a new custom Channel pointing to target.
channel_arguments_impl.h
grpc::AuthContext
Class encapsulating the Authentication Information.
Definition: auth_context.h:65
grpc_impl::ChannelArguments
Options for channel creation.
Definition: channel_arguments_impl.h:43
grpc_impl::MetadataCredentialsPlugin::GetType
virtual const char * GetType() const
Type of credentials this plugin is implementing.
Definition: credentials_impl.h:245
grpc_impl::CallCredentials::CompositeCallCredentials
friend std::shared_ptr< CallCredentials > CompositeCallCredentials(const std::shared_ptr< CallCredentials > &creds1, const std::shared_ptr< CallCredentials > &creds2)
Combines two call credentials objects into a composite call credentials.
Definition: credentials.h:84
grpc_impl::experimental::StsCredentialsOptionsFromJson
grpc::Status StsCredentialsOptionsFromJson(const grpc::string &json_string, StsCredentialsOptions *options)
Creates STS Options from a JSON string.
client_interceptor.h
grpc_library.h
grpc_impl::experimental::AltsCredentialsOptions
Options used to build AltsCredentials.
Definition: credentials_impl.h:336
grpc_impl::MetadataCredentialsPlugin::IsBlocking
virtual bool IsBlocking() const
If this method returns true, the Process function will be scheduled in a different thread from the on...
Definition: credentials_impl.h:242
grpc_security_level
grpc_security_level
Definition: grpc_security_constants.h:114
grpc::SecureChannelCredentials
::grpc_impl::SecureChannelCredentials SecureChannelCredentials
Definition: credentials.h:30
grpc::CallCredentials
::grpc_impl::CallCredentials CallCredentials
Definition: credentials.h:27
grpc_impl::CompositeCallCredentials
std::shared_ptr< CallCredentials > CompositeCallCredentials(const std::shared_ptr< CallCredentials > &creds1, const std::shared_ptr< CallCredentials > &creds2)
Combines two call credentials objects into a composite call credentials.
Definition: credentials.h:84
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:49
grpc_security_constants.h
grpc_impl::GoogleIAMCredentials
std::shared_ptr< CallCredentials > GoogleIAMCredentials(const grpc::string &authorization_token, const grpc::string &authority_selector)
Builds IAM credentials.
grpc::string
std::string string
Definition: config.h:35
grpc_impl::SslCredentialsOptions::pem_root_certs
grpc::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials_impl.h:141
grpc_impl::experimental::StsCredentialsOptions::actor_token_type
grpc::string actor_token_type
Definition: credentials_impl.h:281
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc_impl::ChannelCredentials
A channel credentials object encapsulates all the state needed by a client to authenticate with a ser...
Definition: credentials_impl.h:66
grpc_impl::GoogleDefaultCredentials
std::shared_ptr< ChannelCredentials > GoogleDefaultCredentials()
Builds credentials with reasonable defaults.
grpc_impl::experimental::LocalCredentials
std::shared_ptr< ChannelCredentials > LocalCredentials(grpc_local_connect_type type)
Builds Local Credentials.
grpc_impl::experimental::StsCredentialsOptions::audience
grpc::string audience
Definition: credentials_impl.h:275
grpc_impl::experimental::StsCredentialsOptions::subject_token_type
grpc::string subject_token_type
Definition: credentials_impl.h:279
grpc_impl::MetadataCredentialsPlugin::DebugString
virtual grpc::string DebugString()
Definition: credentials_impl.h:257
grpc_impl::ServiceAccountJWTAccessCredentials
std::shared_ptr< CallCredentials > ServiceAccountJWTAccessCredentials(const grpc::string &json_key, long token_lifetime_seconds=grpc_impl::kMaxAuthTokenLifetimeSecs)
Builds Service Account JWT Access credentials.
grpc_impl::CallCredentials
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials_impl.h:111
grpc_impl::experimental::AltsCredentials
std::shared_ptr< ChannelCredentials > AltsCredentials(const AltsCredentialsOptions &options)
Builds ALTS Credentials given ALTS specific options.
grpc_impl::experimental::StsCredentialsOptions::requested_token_type
grpc::string requested_token_type
Definition: credentials_impl.h:277
auth_context.h
grpc_local_connect_type
grpc_local_connect_type
Type of local connections for which local channel/server credentials will be applied.
Definition: grpc_security_constants.h:140
grpc_impl::CallCredentials::ApplyToCall
virtual bool ApplyToCall(grpc_call *call)=0
Apply this instance's credentials to call.
string_ref.h
grpc_impl::SslCredentialsOptions::pem_cert_chain
grpc::string pem_cert_chain
The buffer containing the PEM encoding of the client's certificate chain.
Definition: credentials_impl.h:150