GRPC C++  1.31.1
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 std::string& target, const std::shared_ptr<ChannelCredentials>& creds,
47  const ChannelArguments& args);
48 
49 namespace experimental {
50 std::shared_ptr<Channel> CreateCustomChannelWithInterceptors(
51  const std::string& target, const std::shared_ptr<ChannelCredentials>& creds,
52  const ChannelArguments& args,
53  std::vector<
54  std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
55  interceptor_creators);
56 }
57 
65  public:
68 
69  protected:
70  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
71  const std::shared_ptr<ChannelCredentials>& channel_creds,
72  const std::shared_ptr<CallCredentials>& call_creds);
73 
75 
76  private:
77  friend std::shared_ptr<Channel> CreateCustomChannelImpl(
78  const std::string& target,
79  const std::shared_ptr<ChannelCredentials>& creds,
80  const ChannelArguments& args);
81 
82  friend std::shared_ptr<Channel>
84  const std::string& target,
85  const std::shared_ptr<ChannelCredentials>& creds,
86  const ChannelArguments& args,
87  std::vector<std::unique_ptr<
89  interceptor_creators);
90 
91  virtual std::shared_ptr<Channel> CreateChannelImpl(
92  const std::string& target, const ChannelArguments& args) = 0;
93 
94  // This function should have been a pure virtual function, but it is
95  // implemented as a virtual function so that it does not break API.
96  virtual std::shared_ptr<Channel> CreateChannelWithInterceptors(
97  const std::string& /*target*/, const ChannelArguments& /*args*/,
98  std::vector<std::unique_ptr<
100  /*interceptor_creators*/) {
101  return nullptr;
102  }
103 };
104 
110  public:
111  CallCredentials();
113 
115  virtual bool ApplyToCall(grpc_call* call) = 0;
116  virtual std::string DebugString() {
117  return "CallCredentials did not provide a debug string";
118  }
119 
120  protected:
121  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
122  const std::shared_ptr<ChannelCredentials>& channel_creds,
123  const std::shared_ptr<CallCredentials>& call_creds);
124 
125  friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
126  const std::shared_ptr<CallCredentials>& creds1,
127  const std::shared_ptr<CallCredentials>& creds2);
128 
130 };
131 
139  std::string pem_root_certs;
140 
143  std::string pem_private_key;
144 
148  std::string pem_cert_chain;
149 };
150 
151 // Factories for building different types of Credentials The functions may
152 // return empty shared_ptr when credentials cannot be created. If a
153 // Credentials pointer is returned, it can still be invalid when used to create
154 // a channel. A lame channel will be created then and all rpcs will fail on it.
155 
162 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
163 
165 std::shared_ptr<ChannelCredentials> SslCredentials(
166  const SslCredentialsOptions& options);
167 
174 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
175 
176 constexpr long kMaxAuthTokenLifetimeSecs = 3600;
177 
183 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
184  const std::string& json_key,
185  long token_lifetime_seconds = grpc_impl::kMaxAuthTokenLifetimeSecs);
186 
195 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
196  const std::string& json_refresh_token);
197 
206 std::shared_ptr<CallCredentials> AccessTokenCredentials(
207  const std::string& access_token);
208 
215 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
216  const std::string& authorization_token,
217  const std::string& authority_selector);
218 
221 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
222  const std::shared_ptr<ChannelCredentials>& channel_creds,
223  const std::shared_ptr<CallCredentials>& call_creds);
224 
226 std::shared_ptr<CallCredentials> CompositeCallCredentials(
227  const std::shared_ptr<CallCredentials>& creds1,
228  const std::shared_ptr<CallCredentials>& creds2);
229 
231 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
232 
235  public:
237 
240  virtual bool IsBlocking() const { return true; }
241 
243  virtual const char* GetType() const { return ""; }
244 
250  virtual grpc::Status GetMetadata(
251  grpc::string_ref service_url, grpc::string_ref method_name,
252  const grpc::AuthContext& channel_auth_context,
253  std::multimap<std::string, std::string>* metadata) = 0;
254 
255  virtual std::string DebugString() {
256  return "MetadataCredentialsPlugin did not provide a debug string";
257  }
258 };
259 
260 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
261  std::unique_ptr<MetadataCredentialsPlugin> plugin);
262 
263 namespace experimental {
264 
271  std::string token_exchange_service_uri; // Required.
272  std::string resource; // Optional.
273  std::string audience; // Optional.
274  std::string scope; // Optional.
275  std::string requested_token_type; // Optional.
276  std::string subject_token_path; // Required.
277  std::string subject_token_type; // Required.
278  std::string actor_token_path; // Optional.
279  std::string actor_token_type; // Optional.
280 };
281 
318 grpc::Status StsCredentialsOptionsFromJson(const std::string& json_string,
319  StsCredentialsOptions* options);
320 
325 
326 std::shared_ptr<CallCredentials> StsCredentials(
327  const StsCredentialsOptions& options);
328 
329 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
330  std::unique_ptr<MetadataCredentialsPlugin> plugin,
331  grpc_security_level min_security_level);
332 
338  std::vector<std::string> target_service_accounts;
339 };
340 
342 std::shared_ptr<ChannelCredentials> AltsCredentials(
343  const AltsCredentialsOptions& options);
344 
346 std::shared_ptr<ChannelCredentials> LocalCredentials(
348 
350 std::shared_ptr<ChannelCredentials> TlsCredentials(
351  const TlsCredentialsOptions& options);
352 
353 } // namespace experimental
354 } // namespace grpc_impl
355 
356 #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::SslCredentialsOptions::pem_root_certs
std::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials_impl.h:139
grpc_impl::experimental::StsCredentialsOptionsFromJson
grpc::Status StsCredentialsOptionsFromJson(const std::string &json_string, StsCredentialsOptions *options)
Creates STS Options from a JSON string.
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::StsCredentials
std::shared_ptr< CallCredentials > StsCredentials(const StsCredentialsOptions &options)
grpc_impl::ChannelCredentials::ChannelCredentials
ChannelCredentials()
grpc_impl::experimental::StsCredentialsOptions::actor_token_type
std::string actor_token_type
Definition: credentials_impl.h:279
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< std::string, std::string > *metadata)=0
Gets the auth metatada produced by this plugin.
channel_impl.h
grpc_impl::GoogleComputeEngineCredentials
std::shared_ptr< CallCredentials > GoogleComputeEngineCredentials()
Builds credentials for use when running in GCE.
grpc_impl::experimental::AltsCredentialsOptions::target_service_accounts
std::vector< std::string > target_service_accounts
service accounts of target endpoint that will be acceptable by the client.
Definition: credentials_impl.h:338
grpc_impl::kMaxAuthTokenLifetimeSecs
constexpr long kMaxAuthTokenLifetimeSecs
Definition: credentials_impl.h:176
grpc_impl::SslCredentialsOptions::pem_private_key
std::string pem_private_key
The buffer containing the PEM encoding of the client's private key.
Definition: credentials_impl.h:143
grpc_impl::experimental::MetadataCredentialsFromPlugin
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin, grpc_security_level min_security_level)
grpc_impl::experimental::TlsCredentials
std::shared_ptr< ChannelCredentials > TlsCredentials(const TlsCredentialsOptions &options)
Builds TLS Credentials given TLS options.
grpc_impl::CallCredentials::CallCredentials
CallCredentials()
grpc_impl::GoogleIAMCredentials
std::shared_ptr< CallCredentials > GoogleIAMCredentials(const std::string &authorization_token, const std::string &authority_selector)
Builds IAM credentials.
status.h
grpc_impl::experimental::StsCredentialsOptions::requested_token_type
std::string requested_token_type
Definition: credentials_impl.h:275
grpc::ChannelCredentials
::grpc_impl::ChannelCredentials ChannelCredentials
Definition: credentials.h:26
grpc_impl::experimental::StsCredentialsOptions::actor_token_path
std::string actor_token_path
Definition: credentials_impl.h:278
grpc_impl::MetadataCredentialsPlugin::~MetadataCredentialsPlugin
virtual ~MetadataCredentialsPlugin()
Definition: credentials_impl.h:236
grpc_impl::GoogleRefreshTokenCredentials
std::shared_ptr< CallCredentials > GoogleRefreshTokenCredentials(const std::string &json_refresh_token)
Builds refresh token credentials.
grpc_impl::MetadataCredentialsFromPlugin
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin)
grpc_impl::CallCredentials::~CallCredentials
~CallCredentials()
grpc::SecureCallCredentials
::grpc_impl::SecureCallCredentials SecureCallCredentials
Definition: credentials.h:29
grpc_impl::CallCredentials::AsSecureCredentials
virtual SecureCallCredentials * AsSecureCredentials()=0
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc_impl::MetadataCredentialsPlugin::DebugString
virtual std::string DebugString()
Definition: credentials_impl.h:255
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::MetadataCredentialsPlugin
User defined metadata credentials.
Definition: credentials_impl.h:234
grpc_impl::experimental::StsCredentialsOptions::token_exchange_service_uri
std::string token_exchange_service_uri
Definition: credentials_impl.h:271
grpc_impl::SslCredentialsOptions
Options used to build SslCredentials.
Definition: credentials_impl.h:133
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:270
grpc_impl::experimental::StsCredentialsOptions::subject_token_type
std::string subject_token_type
Definition: credentials_impl.h:277
channel_arguments_impl.h
grpc::AuthContext
Class encapsulating the Authentication Information.
Definition: auth_context.h:65
grpc_impl::experimental::StsCredentialsOptions::subject_token_path
std::string subject_token_path
Definition: credentials_impl.h:276
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:243
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
client_interceptor.h
grpc_library.h
grpc_impl::experimental::AltsCredentialsOptions
Options used to build AltsCredentials.
Definition: credentials_impl.h:334
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:240
grpc_impl::ChannelCredentials::CreateCustomChannelImpl
friend std::shared_ptr< Channel > CreateCustomChannelImpl(const std::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
grpc_security_level
grpc_security_level
Definition: grpc_security_constants.h:114
grpc_impl::SslCredentialsOptions::pem_cert_chain
std::string pem_cert_chain
The buffer containing the PEM encoding of the client's certificate chain.
Definition: credentials_impl.h:148
grpc::SecureChannelCredentials
::grpc_impl::SecureChannelCredentials SecureChannelCredentials
Definition: credentials.h:30
grpc_impl::CreateCustomChannelImpl
std::shared_ptr<::grpc::Channel > CreateCustomChannelImpl(const std::string &target, const std::shared_ptr<::grpc::ChannelCredentials > &creds, const ::grpc::ChannelArguments &args)
Create a new custom Channel pointing to target.
grpc_impl::ServiceAccountJWTAccessCredentials
std::shared_ptr< CallCredentials > ServiceAccountJWTAccessCredentials(const std::string &json_key, long token_lifetime_seconds=grpc_impl::kMaxAuthTokenLifetimeSecs)
Builds Service Account JWT Access credentials.
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
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc_impl::experimental::StsCredentialsOptions::audience
std::string audience
Definition: credentials_impl.h:273
grpc_impl::ChannelCredentials
A channel credentials object encapsulates all the state needed by a client to authenticate with a ser...
Definition: credentials_impl.h:64
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::CreateCustomChannelWithInterceptors
std::shared_ptr<::grpc::Channel > CreateCustomChannelWithInterceptors(const std::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_impl::CallCredentials
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials_impl.h:109
grpc_impl::experimental::AltsCredentials
std::shared_ptr< ChannelCredentials > AltsCredentials(const AltsCredentialsOptions &options)
Builds ALTS Credentials given ALTS specific options.
grpc_impl::AccessTokenCredentials
std::shared_ptr< CallCredentials > AccessTokenCredentials(const std::string &access_token)
Builds access token credentials.
grpc_impl::experimental::StsCredentialsOptions::scope
std::string scope
Definition: credentials_impl.h:274
grpc_impl::CallCredentials::DebugString
virtual std::string DebugString()
Definition: credentials_impl.h:116
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.
grpc_impl::experimental::StsCredentialsOptions::resource
std::string resource
Definition: credentials_impl.h:272
string_ref.h