GRPC C++  1.76.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 
24 #include <grpcpp/channel.h>
31 #include <grpcpp/support/status.h>
33 
34 #include <map>
35 #include <memory>
36 #include <vector>
37 
38 struct grpc_call;
39 
40 namespace grpc {
41 class CallCredentials;
42 class ChannelCredentials;
43 namespace testing {
44 std::string GetOauth2AccessToken();
45 }
46 
47 std::shared_ptr<Channel> CreateCustomChannel(
48  const grpc::string& target,
49  const std::shared_ptr<grpc::ChannelCredentials>& creds,
50  const grpc::ChannelArguments& args);
51 
52 namespace experimental {
53 std::shared_ptr<grpc::Channel> CreateCustomChannelWithInterceptors(
54  const grpc::string& target,
55  const std::shared_ptr<grpc::ChannelCredentials>& creds,
56  const grpc::ChannelArguments& args,
57  std::vector<
58  std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
59  interceptor_creators);
60 } // namespace experimental
61 
63 std::shared_ptr<ChannelCredentials> XdsCredentials(
64  const std::shared_ptr<ChannelCredentials>& fallback_creds);
65 
73  public:
74  ~ChannelCredentials() override;
75 
76  protected:
78 
79  grpc_channel_credentials* c_creds() { return c_creds_; }
80 
81  private:
82  friend std::shared_ptr<grpc::Channel>
84  std::unique_ptr<grpc_event_engine::experimental::EventEngine::Endpoint>
85  endpoint,
86  const std::shared_ptr<ChannelCredentials>& creds,
87  const ChannelArguments& args);
88 #ifdef GPR_SUPPORT_CHANNELS_FROM_FD
89  friend std::shared_ptr<grpc::Channel> grpc::experimental::CreateChannelFromFd(
90  int fd, const std::shared_ptr<ChannelCredentials>& creds,
91  const ChannelArguments& args);
92 #endif // GPR_SUPPORT_CHANNELS_FROM_FD
93  friend std::shared_ptr<grpc::Channel> CreateCustomChannel(
94  const grpc::string& target,
95  const std::shared_ptr<grpc::ChannelCredentials>& creds,
96  const grpc::ChannelArguments& args);
97  friend std::shared_ptr<grpc::Channel>
99  const grpc::string& target,
100  const std::shared_ptr<grpc::ChannelCredentials>& creds,
101  const grpc::ChannelArguments& args,
102  std::vector<std::unique_ptr<
104  interceptor_creators);
105  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
106  const std::shared_ptr<ChannelCredentials>& channel_creds,
107  const std::shared_ptr<CallCredentials>& call_creds);
109 
110  virtual std::shared_ptr<Channel> CreateChannelImpl(
111  const grpc::string& target, const ChannelArguments& args) {
112  return CreateChannelWithInterceptors(target, args, {});
113  }
114 
115  virtual std::shared_ptr<Channel> CreateChannelWithInterceptors(
116  const grpc::string& target, const ChannelArguments& args,
117  std::vector<std::unique_ptr<
119  interceptor_creators);
120 
121  grpc_channel_credentials* const c_creds_;
122 };
123 
129  public:
130  ~CallCredentials() override;
131 
133  bool ApplyToCall(grpc_call* call);
134 
135  grpc::string DebugString();
136 
137  protected:
138  explicit CallCredentials(grpc_call_credentials* creds);
139 
140  private:
141  friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
142  const std::shared_ptr<ChannelCredentials>& channel_creds,
143  const std::shared_ptr<CallCredentials>& call_creds);
145  friend std::string grpc::testing::GetOauth2AccessToken();
146 
147  grpc_call_credentials* c_creds_ = nullptr;
148 };
149 
157  grpc::string pem_root_certs;
158 
161  grpc::string pem_private_key;
162 
166  grpc::string pem_cert_chain;
167 };
168 
174 };
175 
176 // Factories for building different types of Credentials The functions may
177 // return empty shared_ptr when credentials cannot be created. If a
178 // Credentials pointer is returned, it can still be invalid when used to create
179 // a channel. A lame channel will be created then and all rpcs will fail on it.
180 
187 std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials(
188  const GoogleDefaultCredentialsOptions& options =
190 
192 std::shared_ptr<ChannelCredentials> SslCredentials(
193  const SslCredentialsOptions& options);
194 
201 std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
202 
203 constexpr long kMaxAuthTokenLifetimeSecs = 3600;
204 
210 std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
211  const grpc::string& json_key,
212  long token_lifetime_seconds = kMaxAuthTokenLifetimeSecs);
213 
222 std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
223  const grpc::string& json_refresh_token);
224 
233 std::shared_ptr<CallCredentials> AccessTokenCredentials(
234  const grpc::string& access_token);
235 
242 std::shared_ptr<CallCredentials> GoogleIAMCredentials(
243  const grpc::string& authorization_token,
244  const grpc::string& authority_selector);
245 
248 std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
249  const std::shared_ptr<ChannelCredentials>& channel_creds,
250  const std::shared_ptr<CallCredentials>& call_creds);
251 
253 std::shared_ptr<CallCredentials> CompositeCallCredentials(
254  const std::shared_ptr<CallCredentials>& creds1,
255  const std::shared_ptr<CallCredentials>& creds2);
256 
258 std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
259 
262  public:
264 
267  virtual bool IsBlocking() const { return true; }
268 
270  virtual const char* GetType() const { return ""; }
271 
277  virtual grpc::Status GetMetadata(
278  grpc::string_ref service_url, grpc::string_ref method_name,
279  const grpc::AuthContext& channel_auth_context,
280  std::multimap<grpc::string, grpc::string>* metadata) = 0;
281 
282  virtual grpc::string DebugString() {
283  return "MetadataCredentialsPlugin did not provide a debug string";
284  }
285 };
286 
287 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
288  std::unique_ptr<MetadataCredentialsPlugin> plugin);
289 
293 std::shared_ptr<CallCredentials> ExternalAccountCredentials(
294  const grpc::string& json_string, const std::vector<grpc::string>& scopes);
295 
296 namespace experimental {
297 
304  grpc::string token_exchange_service_uri; // Required.
305  grpc::string resource; // Optional.
306  grpc::string audience; // Optional.
307  grpc::string scope; // Optional.
308  grpc::string requested_token_type; // Optional.
309  grpc::string subject_token_path; // Required.
310  grpc::string subject_token_type; // Required.
311  grpc::string actor_token_path; // Optional.
312  grpc::string actor_token_type; // Optional.
313 };
314 
315 grpc::Status StsCredentialsOptionsFromJson(const std::string& json_string,
316  StsCredentialsOptions* options);
317 
322 
323 std::shared_ptr<CallCredentials> StsCredentials(
324  const StsCredentialsOptions& options);
325 
326 std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
327  std::unique_ptr<MetadataCredentialsPlugin> plugin,
328  grpc_security_level min_security_level);
329 
335  std::vector<grpc::string> target_service_accounts;
336 };
337 
339 std::shared_ptr<ChannelCredentials> AltsCredentials(
340  const AltsCredentialsOptions& options);
341 
343 std::shared_ptr<ChannelCredentials> LocalCredentials(
345 
347 std::shared_ptr<ChannelCredentials> TlsCredentials(
348  const TlsChannelCredentialsOptions& options);
349 
350 } // namespace experimental
351 } // namespace grpc
352 
353 #endif // GRPCPP_SECURITY_CREDENTIALS_H
grpc::experimental::StsCredentials
std::shared_ptr< CallCredentials > StsCredentials(const StsCredentialsOptions &options)
grpc::experimental::TlsCredentials
std::shared_ptr< ChannelCredentials > TlsCredentials(const TlsChannelCredentialsOptions &options)
Builds TLS Credentials given TLS options.
grpc::XdsCredentials
std::shared_ptr< ChannelCredentials > XdsCredentials(const std::shared_ptr< ChannelCredentials > &fallback_creds)
Builds XDS Credentials.
grpc::string_ref
This class is a non owning reference to a string.
Definition: string_ref.h:40
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:166
grpc::experimental::StsCredentialsOptions
Options for creating STS Oauth Token Exchange credentials following the IETF draft https://tools....
Definition: credentials.h:303
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:161
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::CallCredentials::ApplyToCall
bool ApplyToCall(grpc_call *call)
Apply this instance's credentials to call.
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::SslCredentialsOptions::pem_root_certs
grpc::string pem_root_certs
The buffer containing the PEM encoding of the server root certificates.
Definition: credentials.h:157
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:261
event_engine.h
grpc::MetadataCredentialsPlugin::GetType
virtual const char * GetType() const
Type of credentials this plugin is implementing.
Definition: credentials.h:270
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:267
grpc::MetadataCredentialsPlugin::~MetadataCredentialsPlugin
virtual ~MetadataCredentialsPlugin()
Definition: credentials.h:263
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:151
grpc::CallCredentials::DebugString
grpc::string DebugString()
grpc::SslCredentials
std::shared_ptr< ChannelCredentials > SslCredentials(const SslCredentialsOptions &options)
Builds SSL Credentials given SSL specific options.
grpc::GoogleDefaultCredentialsOptions::use_alts_call_credentials
bool use_alts_call_credentials
Indicates if the created credentials should also contain ALTS specific credentials.
Definition: credentials.h:173
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:307
grpc::GoogleDefaultCredentialsOptions
Options used to build GoogleDefaultCredentials.
Definition: credentials.h:170
grpc::testing::GetOauth2AccessToken
std::string GetOauth2AccessToken()
grpc::ChannelCredentials::XdsChannelCredentialsImpl
friend class XdsChannelCredentialsImpl
Definition: credentials.h:108
grpc::experimental::StsCredentialsOptions::subject_token_path
grpc::string subject_token_path
Definition: credentials.h:309
grpc::CallCredentials::~CallCredentials
~CallCredentials() override
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:304
grpc::ServiceAccountJWTAccessCredentials
std::shared_ptr< CallCredentials > ServiceAccountJWTAccessCredentials(const grpc::string &json_key, long token_lifetime_seconds=kMaxAuthTokenLifetimeSecs)
Builds Service Account JWT Access credentials.
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:34
client_interceptor.h
grpc::experimental::AltsCredentialsOptions
Options used to build AltsCredentials.
Definition: credentials.h:331
grpc::CallCredentials::CompositeCallCredentialsImpl
friend class CompositeCallCredentialsImpl
Definition: credentials.h:144
grpc::CallCredentials
A call credentials object encapsulates the state needed by a client to authenticate with a server for...
Definition: credentials.h:128
grpc::experimental::StsCredentialsOptions::subject_token_type
grpc::string subject_token_type
Definition: credentials.h:310
grpc::ChannelCredentials::c_creds
grpc_channel_credentials * c_creds()
Definition: credentials.h:79
channel_arguments.h
grpc_channel_credentials
struct grpc_channel_credentials grpc_channel_credentials
— grpc_channel_credentials object.
Definition: grpc.h:286
grpc::experimental::TlsChannelCredentialsOptions
Definition: tls_credentials_options.h:154
grpc::ChannelCredentials::~ChannelCredentials
~ChannelCredentials() override
grpc::MetadataCredentialsFromPlugin
std::shared_ptr< CallCredentials > MetadataCredentialsFromPlugin(std::unique_ptr< MetadataCredentialsPlugin > plugin)
grpc::experimental::StsCredentialsOptions::audience
grpc::string audience
Definition: credentials.h:306
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:68
grpc_call_credentials
struct grpc_call_credentials grpc_call_credentials
— grpc_call_credentials object —
Definition: credentials.h:37
grpc::ChannelCredentials::ChannelCredentials
ChannelCredentials(grpc_channel_credentials *creds)
grpc::experimental::StsCredentialsOptions::resource
grpc::string resource
Definition: credentials.h:305
grpc::AuthContext
Class encapsulating the Authentication Information.
Definition: auth_context.h:70
grpc::experimental::LocalCredentials
std::shared_ptr< ChannelCredentials > LocalCredentials(grpc_local_connect_type type)
Builds Local Credentials.
channel.h
grpc::internal::GrpcLibrary
Classes that require gRPC to be initialized should inherit from this class.
Definition: grpc_library.h:32
grpc::ExternalAccountCredentials
std::shared_ptr< CallCredentials > ExternalAccountCredentials(const grpc::string &json_string, const std::vector< grpc::string > &scopes)
Builds External Account credentials.
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:312
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:335
grpc_security_level
grpc_security_level
Definition: grpc_security_constants.h:131
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:308
grpc_library.h
grpc::experimental::ClientInterceptorFactoryInterface
Definition: client_interceptor.h:48
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:72
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_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::experimental::CreateChannelFromEndpoint
std::shared_ptr< grpc::Channel > CreateChannelFromEndpoint(std::unique_ptr< grpc_event_engine::experimental::EventEngine::Endpoint > endpoint, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
Creates a new Channel from an EventEngine endpoint.
grpc::kMaxAuthTokenLifetimeSecs
constexpr long kMaxAuthTokenLifetimeSecs
Definition: credentials.h:203
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:311
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:143
grpc::MetadataCredentialsPlugin::DebugString
virtual grpc::string DebugString()
Definition: credentials.h:282
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 metadata produced by this plugin.
create_channel_posix.h
string_ref.h
grpc::GoogleDefaultCredentials
std::shared_ptr< ChannelCredentials > GoogleDefaultCredentials(const GoogleDefaultCredentialsOptions &options=GoogleDefaultCredentialsOptions())
Builds google default credentials with the given options.
grpc::GoogleIAMCredentials
std::shared_ptr< CallCredentials > GoogleIAMCredentials(const grpc::string &authorization_token, const grpc::string &authority_selector)
Builds IAM credentials.
grpc::CallCredentials::CallCredentials
CallCredentials(grpc_call_credentials *creds)