GRPC C++  1.32.0
credentials.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_H
20 #define GRPCPP_SECURITY_CREDENTIALS_H
21 
22 #include <map>
23 #include <memory>
24 #include <vector>
25 
27 #include <grpcpp/channel.h>
33 #include <grpcpp/support/status.h>
35 
36 struct grpc_call;
37 
38 namespace grpc {
39 class CallCredentials;
40 class SecureCallCredentials;
41 class SecureChannelCredentials;
42 class ChannelCredentials;
43 
44 std::shared_ptr<Channel> CreateCustomChannel(
45  const grpc::string& target,
46  const std::shared_ptr<grpc::ChannelCredentials>& creds,
47  const grpc::ChannelArguments& args);
48 
49 namespace experimental {
50 std::shared_ptr<grpc::Channel> CreateCustomChannelWithInterceptors(
51  const grpc::string& target,
52  const std::shared_ptr<grpc::ChannelCredentials>& creds,
53  const grpc::ChannelArguments& args,
54  std::vector<
55  std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
56  interceptor_creators);
57 }
58 
66  public:
69 
70  protected:
71  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
72  const std::shared_ptr<ChannelCredentials>& channel_creds,
73  const std::shared_ptr<CallCredentials>& call_creds);
74 
75  virtual SecureChannelCredentials* AsSecureCredentials() = 0;
76 
77  private:
78  friend std::shared_ptr<grpc::Channel> CreateCustomChannel(
79  const grpc::string& target,
80  const std::shared_ptr<grpc::ChannelCredentials>& creds,
81  const grpc::ChannelArguments& args);
82 
83  friend std::shared_ptr<grpc::Channel>
85  const grpc::string& target,
86  const std::shared_ptr<grpc::ChannelCredentials>& creds,
87  const grpc::ChannelArguments& args,
88  std::vector<std::unique_ptr<
90  interceptor_creators);
91 
92  virtual std::shared_ptr<Channel> CreateChannelImpl(
93  const grpc::string& target, const ChannelArguments& args) = 0;
94 
95  // This function should have been a pure virtual function, but it is
96  // implemented as a virtual function so that it does not break API.
97  virtual std::shared_ptr<Channel> CreateChannelWithInterceptors(
98  const grpc::string& /*target*/, const ChannelArguments& /*args*/,
99  std::vector<std::unique_ptr<
101  /*interceptor_creators*/) {
102  return nullptr;
103  }
104 };
105 
111  public:
112  CallCredentials();
114 
116  virtual bool ApplyToCall(grpc_call* call) = 0;
117  virtual grpc::string DebugString() {
118  return "CallCredentials did not provide a debug string";
119  }
120 
121  protected:
122  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
123  const std::shared_ptr<ChannelCredentials>& channel_creds,
124  const std::shared_ptr<CallCredentials>& call_creds);
125 
126  friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
127  const std::shared_ptr<CallCredentials>& creds1,
128  const std::shared_ptr<CallCredentials>& creds2);
129 
130  virtual SecureCallCredentials* AsSecureCredentials() = 0;
131 };
132 
140  grpc::string pem_root_certs;
141 
144  grpc::string pem_private_key;
145 
149  grpc::string pem_cert_chain;
150 };
151 
152 // Factories for building different types of Credentials The functions may
153 // return empty shared_ptr when credentials cannot be created. If a
154 // Credentials pointer is returned, it can still be invalid when used to create
155 // a channel. A lame channel will be created then and all rpcs will fail on it.
156 
163 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
164 
166 std::shared_ptr<ChannelCredentials> SslCredentials(
167  const SslCredentialsOptions& options);
168 
175 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
176 
177 constexpr long kMaxAuthTokenLifetimeSecs = 3600;
178 
184 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
185  const grpc::string& json_key,
186  long token_lifetime_seconds = kMaxAuthTokenLifetimeSecs);
187 
196 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
197  const grpc::string& json_refresh_token);
198 
207 std::shared_ptr<CallCredentials> AccessTokenCredentials(
208  const grpc::string& access_token);
209 
216 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
217  const grpc::string& authorization_token,
218  const grpc::string& authority_selector);
219 
222 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
223  const std::shared_ptr<ChannelCredentials>& channel_creds,
224  const std::shared_ptr<CallCredentials>& call_creds);
225 
227 std::shared_ptr<CallCredentials> CompositeCallCredentials(
228  const std::shared_ptr<CallCredentials>& creds1,
229  const std::shared_ptr<CallCredentials>& creds2);
230 
232 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
233 
236  public:
238 
241  virtual bool IsBlocking() const { return true; }
242 
244  virtual const char* GetType() const { return ""; }
245 
251  virtual grpc::Status GetMetadata(
252  grpc::string_ref service_url, grpc::string_ref method_name,
253  const grpc::AuthContext& channel_auth_context,
254  std::multimap<grpc::string, grpc::string>* metadata) = 0;
255 
256  virtual grpc::string DebugString() {
257  return "MetadataCredentialsPlugin did not provide a debug string";
258  }
259 };
260 
261 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
262  std::unique_ptr<MetadataCredentialsPlugin> plugin);
263 
264 namespace experimental {
265 
272  grpc::string token_exchange_service_uri; // Required.
273  grpc::string resource; // Optional.
274  grpc::string audience; // Optional.
275  grpc::string scope; // Optional.
276  grpc::string requested_token_type; // Optional.
277  grpc::string subject_token_path; // Required.
278  grpc::string subject_token_type; // Required.
279  grpc::string actor_token_path; // Optional.
280  grpc::string actor_token_type; // Optional.
281 };
282 
283 grpc::Status StsCredentialsOptionsFromJson(const std::string& json_string,
284  StsCredentialsOptions* options);
285 
290 
291 std::shared_ptr<CallCredentials> StsCredentials(
292  const StsCredentialsOptions& options);
293 
294 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
295  std::unique_ptr<MetadataCredentialsPlugin> plugin,
296  grpc_security_level min_security_level);
297 
303  std::vector<grpc::string> target_service_accounts;
304 };
305 
307 std::shared_ptr<ChannelCredentials> AltsCredentials(
308  const AltsCredentialsOptions& options);
309 
311 std::shared_ptr<ChannelCredentials> LocalCredentials(
313 
315 std::shared_ptr<ChannelCredentials> TlsCredentials(
316  const TlsCredentialsOptions& options);
317 
318 } // namespace experimental
319 } // namespace grpc
320 
321 #endif // GRPCPP_SECURITY_CREDENTIALS_H
grpc::experimental::StsCredentials
std::shared_ptr< CallCredentials > StsCredentials(const StsCredentialsOptions &options)
grpc::GrpcLibraryCodegen
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:38
grpc::string_ref
This class is a non owning reference to a string.
Definition: string_ref.h:41
grpc::experimental::StsCredentialsOptionsFromJson
grpc::Status StsCredentialsOptionsFromJson(const std::string &json_string, StsCredentialsOptions *options)
tls_credentials_options.h
grpc::SslCredentialsOptions::pem_cert_chain
grpc::string pem_cert_chain
The buffer containing the PEM encoding of the client's certificate chain.
Definition: credentials.h:149
grpc::experimental::StsCredentialsOptions
Options for creating STS Oauth Token Exchange credentials following the IETF draft https://tools....
Definition: credentials.h:271
grpc::SslCredentialsOptions::pem_private_key
grpc::string pem_private_key
The buffer containing the PEM encoding of the client's private key.
Definition: credentials.h:144
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
grpc::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.
grpc::GoogleDefaultCredentials
std::shared_ptr< ChannelCredentials > GoogleDefaultCredentials()
Builds credentials with reasonable defaults.
grpc::SslCredentialsOptions::pem_root_certs
grpc::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials.h:140
grpc::ChannelCredentials::AsSecureCredentials
virtual SecureChannelCredentials * AsSecureCredentials()=0
grpc::experimental::AltsCredentials
std::shared_ptr< ChannelCredentials > AltsCredentials(const AltsCredentialsOptions &options)
Builds ALTS Credentials given ALTS specific options.
grpc::MetadataCredentialsPlugin
User defined metadata credentials.
Definition: credentials.h:235
grpc::MetadataCredentialsPlugin::GetType
virtual const char * GetType() const
Type of credentials this plugin is implementing.
Definition: credentials.h:244
grpc::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.h:241
grpc::MetadataCredentialsPlugin::~MetadataCredentialsPlugin
virtual ~MetadataCredentialsPlugin()
Definition: credentials.h:237
grpc::experimental::StsCredentialsOptionsFromEnv
grpc::Status StsCredentialsOptionsFromEnv(StsCredentialsOptions *options)
Creates STS credentials options from the $STS_CREDENTIALS environment variable.
grpc::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.
grpc::SslCredentialsOptions
Options used to build SslCredentials.
Definition: credentials.h:134
grpc::SslCredentials
std::shared_ptr< ChannelCredentials > SslCredentials(const SslCredentialsOptions &options)
Builds SSL Credentials given SSL specific options.
grpc::experimental::MetadataCredentialsFromPlugin
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin, grpc_security_level min_security_level)
status.h
grpc::experimental::StsCredentialsOptions::scope
grpc::string scope
Definition: credentials.h:275
grpc::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.
grpc::CallCredentials::DebugString
virtual grpc::string DebugString()
Definition: credentials.h:117
grpc::experimental::StsCredentialsOptions::subject_token_path
grpc::string subject_token_path
Definition: credentials.h:277
grpc::ChannelArguments
Options for channel creation.
Definition: channel_arguments.h:39
grpc::experimental::StsCredentialsOptions::token_exchange_service_uri
grpc::string token_exchange_service_uri
Definition: credentials.h:272
grpc::ServiceAccountJWTAccessCredentials
std::shared_ptr< CallCredentials > ServiceAccountJWTAccessCredentials(const grpc::string &json_key, long token_lifetime_seconds=kMaxAuthTokenLifetimeSecs)
Builds Service Account JWT Access credentials.
grpc::CallCredentials::~CallCredentials
~CallCredentials()
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc::experimental::AltsCredentialsOptions
Options used to build AltsCredentials.
Definition: credentials.h:299
grpc::CallCredentials::CallCredentials
CallCredentials()
grpc::CallCredentials
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials.h:110
grpc::experimental::StsCredentialsOptions::subject_token_type
grpc::string subject_token_type
Definition: credentials.h:278
grpc::CallCredentials::AsSecureCredentials
virtual SecureCallCredentials * AsSecureCredentials()=0
channel_arguments.h
grpc::MetadataCredentialsFromPlugin
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin)
grpc::experimental::StsCredentialsOptions::audience
grpc::string audience
Definition: credentials.h:274
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc::experimental::StsCredentialsOptions::resource
grpc::string resource
Definition: credentials.h:273
grpc::AuthContext
Class encapsulating the Authentication Information.
Definition: auth_context.h:65
grpc::experimental::LocalCredentials
std::shared_ptr< ChannelCredentials > LocalCredentials(grpc_local_connect_type type)
Builds Local Credentials.
channel.h
client_interceptor.h
grpc::GoogleComputeEngineCredentials
std::shared_ptr< CallCredentials > GoogleComputeEngineCredentials()
Builds credentials for use when running in GCE.
grpc::experimental::StsCredentialsOptions::actor_token_type
grpc::string actor_token_type
Definition: credentials.h:280
grpc_library.h
grpc::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.h:303
grpc_security_level
grpc_security_level
Definition: grpc_security_constants.h:114
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
grpc::GoogleRefreshTokenCredentials
std::shared_ptr< CallCredentials > GoogleRefreshTokenCredentials(const grpc::string &json_refresh_token)
Builds refresh token credentials.
grpc::experimental::StsCredentialsOptions::requested_token_type
grpc::string requested_token_type
Definition: credentials.h:276
grpc::ChannelCredentials::ChannelCredentials
ChannelCredentials()
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:46
grpc::experimental::CreateCustomChannelWithInterceptors
std::shared_ptr< Channel > CreateCustomChannelWithInterceptors(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
Create a new custom Channel pointing to target with interceptors being invoked per call.
grpc::ChannelCredentials
A channel credentials object encapsulates all the state needed by a client to authenticate with a ser...
Definition: credentials.h:65
grpc::experimental::TlsCredentialsOptions
TLS credentials options, wrapper for grpc_tls_credentials_options.
Definition: tls_credentials_options.h:279
grpc::ChannelCredentials::CreateCustomChannel
friend std::shared_ptr< grpc::Channel > CreateCustomChannel(const grpc::string &target, const std::shared_ptr< grpc::ChannelCredentials > &creds, const grpc::ChannelArguments &args)
grpc::CreateCustomChannel
std::shared_ptr< Channel > CreateCustomChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
Create a new custom Channel pointing to target.
grpc::CallCredentials::ApplyToCall
virtual bool ApplyToCall(grpc_call *call)=0
Apply this instance's credentials to call.
grpc_security_constants.h
grpc::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.
grpc::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.
grpc::kMaxAuthTokenLifetimeSecs
constexpr long kMaxAuthTokenLifetimeSecs
Definition: credentials.h:177
grpc::AccessTokenCredentials
std::shared_ptr< CallCredentials > AccessTokenCredentials(const grpc::string &access_token)
Builds access token credentials.
grpc::experimental::StsCredentialsOptions::actor_token_path
grpc::string actor_token_path
Definition: credentials.h:279
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::ChannelCredentials::~ChannelCredentials
~ChannelCredentials()
grpc::MetadataCredentialsPlugin::DebugString
virtual grpc::string DebugString()
Definition: credentials.h:256
grpc::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::experimental::TlsCredentials
std::shared_ptr< ChannelCredentials > TlsCredentials(const TlsCredentialsOptions &options)
Builds TLS Credentials given TLS options.
string_ref.h
grpc::GoogleIAMCredentials
std::shared_ptr< CallCredentials > GoogleIAMCredentials(const grpc::string &authorization_token, const grpc::string &authority_selector)
Builds IAM credentials.