Google Cloud C++ Client  2.7.0
C++ Client Library for Google Cloud Platform
credentials.h
Go to the documentation of this file.
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_CREDENTIALS_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_CREDENTIALS_H
17 
18 #include "google/cloud/common_options.h"
19 #include "google/cloud/options.h"
20 #include "google/cloud/version.h"
21 #include <chrono>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 namespace google {
27 namespace cloud {
29 namespace internal {
30 class CredentialsVisitor;
31 } // namespace internal
32 
33 /**
34  * An opaque representation of the authentication configuration.
35  *
36  * Applications use factory functions to provide the authentication parameters
37  * (for example, a raw access token). The factory function encapsulates the
38  * parameters in an instance of this class. The interface in this class is
39  * (intentionally) very narrow. Only the internal components in the client
40  * libraries should need to access the details of this class.
41  *
42  * @see https://cloud.google.com/docs/authentication for more information on
43  * authentication in GCP.
44  *
45  * @see https://cloud.google.com/iam for more information on the IAM Service.
46  *
47  * [IAM overview]: https://cloud.google.com/iam/docs/overview
48  * [IAM Service]: https://cloud.google.com/iam/docs
49  * [C++ IAM client library]: https://googleapis.dev/cpp/google-cloud-iam/latest/
50  *
51  * @ingroup guac
52  */
53 class Credentials {
54  public:
55  virtual ~Credentials() = 0;
56 
57  private:
58  friend class internal::CredentialsVisitor;
59  virtual void dispatch(internal::CredentialsVisitor& visitor) = 0;
60 };
61 
62 /**
63  * A wrapper to store credentials into an options
64  *
65  * @ingroup options
66  * @ingroup guac
67  */
69  using Type = std::shared_ptr<Credentials>;
70 };
71 
72 /**
73  * Create insecure (aka anonymous, aka unauthenticated) credentials.
74  *
75  * These credentials are mostly intended for testing. Integration tests running
76  * against an emulator do not need to authenticate. In fact, it may be
77  * impossible to connect to an emulator using SSL/TLS because the emulators
78  * typically run without secure communication.
79  *
80  * In addition, unit tests may benefit from using these credentials: loading the
81  * default credentials unnecessarily slows down the unit tests, and in some
82  * CI environments the credentials may fail to load, creating confusing warnings
83  * and sometimes even errors.
84  *
85  * @ingroup guac
86  *
87  * @param opts optional configuration values. Note that the effect of these
88  * parameters depends on the underlying transport. For example,
89  * `TracingComponentsOption` is ignored by gRPC-based services.
90  */
92 
93 /**
94  * Creates the default credentials.
95  *
96  * These are the most commonly used credentials, and are expected to meet the
97  * needs of most applications. The Google Default Credentials conform to
98  * [aip/4110]. Consider using these credentials when:
99  *
100  * - Your application is deployed to a GCP environment such as GCE, GKE, or
101  * Cloud Run. Each of these deployment environments provides a default service
102  * account to the application, and offers mechanisms to change the default
103  * credentials without any code changes to your application.
104  * - You are testing or developing the application on a workstation (physical or
105  * virtual). These credentials will use your preferences as set with
106  * [gcloud auth application-default]. These preferences can be your own GCP
107  * user credentials, or some service account.
108  * - Regardless of where your application is running, you can use the
109  * `GOOGLE_APPLICATION_CREDENTIALS` environment variable to override the
110  * defaults. This environment variable should point to a file containing a
111  * service account key file, or a JSON object describing your user
112  * credentials.
113  *
114  * @see https://cloud.google.com/docs/authentication for more information on
115  * authentication in GCP.
116  *
117  * [aip/4110]: https://google.aip.dev/auth/4110
118  * [gcloud auth application-default]:
119  * https://cloud.google.com/sdk/gcloud/reference/auth/application-default
120  *
121  * @ingroup guac
122  *
123  * @param opts optional configuration values. Note that the effect of these
124  * parameters depends on the underlying transport. For example,
125  * `TracingComponentsOption` is ignored by gRPC-based services.
126  */
128 
129 /**
130  * Creates credentials with a fixed access token.
131  *
132  * These credentials are useful when using an out-of-band mechanism to fetch
133  * access tokens. Note that access tokens are time limited, you will need to
134  * manually refresh the tokens created by the
135  *
136  * @see https://cloud.google.com/docs/authentication for more information on
137  * authentication in GCP.
138  *
139  * @ingroup guac
140  *
141  * @param access_token the access token to be used by the client library.
142  * @param expiration the expiration time for the token.
143  * @param opts optional configuration values. Note that the effect of these
144  * parameters depends on the underlying transport. For example,
145  * `TracingComponentsOption` is ignored by gRPC-based services.
146  */
148  std::string const& access_token,
149  std::chrono::system_clock::time_point expiration, Options opts = {});
150 
151 /**
152  * Creates credentials for service account impersonation.
153  *
154  * Service account impersonation allows one account (user or service account)
155  * to *act as* a second account. This can be useful in multi-tenant services,
156  * where the service may perform some actions with an specific account
157  * associated with a tenant. The tenant can grant or restrict permissions to
158  * this tenant account.
159  *
160  * When using service account impersonation is important to distinguish between
161  * the credentials used to *obtain* the target account credentials (the
162  * @p base_credentials) parameter, and the credentials representing the
163  * @p target_service_account.
164  *
165  * Use `AccessTokenLifetimeOption` to configure the maximum lifetime of the
166  * obtained credentials. The default is 1h (3600s), see [IAM quotas] for the
167  * limits set by the platform and how to override them.
168  *
169  * Use `DelegatesOption` to configure a sequence of intermediate service
170  * account, each of which has permissions to impersonate the next and the
171  * last one has permissions to impersonate @p target_service_account.
172  *
173  * Use `ScopesOption` to restrict the authentication scope for the obtained
174  * credentials. See below for possible values.
175  *
176  * [IAM quotas]: https://cloud.google.com/iam/quotas
177  * @see https://cloud.google.com/docs/authentication for more information on
178  * authentication in GCP.
179  * @see https://cloud.google.com/iam/docs/impersonating-service-accounts for
180  * information on managing service account impersonation.
181  * @see https://developers.google.com/identity/protocols/oauth2/scopes for
182  * authentication scopes in Google Cloud Platform.
183  *
184  * @ingroup guac
185  *
186  * @param base_credentials the credentials used to contact the IAM Credentials
187  * services.
188  * @param target_service_account the email address of the service account to
189  * impersonate.
190  * @param opts optional configuration values. Note that the effect of these
191  * parameters depends on the underlying transport. For example,
192  * `TracingComponentsOption` is ignored by gRPC-based services.
193  */
195  std::shared_ptr<Credentials> base_credentials,
196  std::string target_service_account, Options opts = {});
197 
198 /**
199  * Creates service account credentials from a service account key.
200  *
201  * A [service account] is an account for an application or compute workload
202  * instead of an individual end user. The recommended practice is to use
203  * Google Default Credentials, which relies on the configuration of the Google
204  * Cloud system hosting your application (GCE, GKE, Cloud Run) to authenticate
205  * your workload or application. But sometimes you may need to create and
206  * download a [service account key], for example, to use a service account
207  * when running your application on a system that is not part of Google Cloud.
208  *
209  * Service account credentials are used in this latter case.
210  *
211  * You can create multiple service account keys for a single service account.
212  * When you create a service account key, the key is returned as string, in the
213  * format described by [aip/4112]. This string contains an id for the service
214  * account, as well as the cryptographical materials (a RSA private key)
215  * required to authenticate the caller.
216  *
217  * Therefore, services account keys should be treated as any other secret
218  * with security implications. Think of them as unencrypted passwords. Do not
219  * store them where unauthorized persons or programs may read them.
220  *
221  * As stated above, most applications should probably use default credentials,
222  * maybe pointing them to a file with these contents. Using this function may be
223  * useful when the service account key is obtained from Cloud Secret Manager or
224  * a similar service.
225  *
226  * [aip/4112]: https://google.aip.dev/auth/4112
227  * [service account]: https://cloud.google.com/iam/docs/overview#service_account
228  * [service account key]:
229  * https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-create-cpp
230  *
231  * @ingroup guac
232  *
233  * @param json_object the service account configuration as a JSON string.
234  * Typically applications read this from a file, or download the contents from
235  * something like Google's secret manager service.
236  * @param opts optional configuration values. Note that the effect of these
237  * parameters depends on the underlying transport. For example,
238  * `TracingComponentsOption` is ignored by gRPC-based services.
239  */
241  std::string json_object, Options opts = {});
242 
243 /**
244  * Creates credentials based on external accounts.
245  *
246  * [Workload Identity Federation] can grant on-premises or multi-cloud workloads
247  * access to Google Cloud resources, without using a service account key. You
248  * can use identity federation with Amazon Web Services (AWS), or with any
249  * identity provider that supports OpenID Connect (OIDC), such as Microsoft
250  * Azure, or SAML 2.0.
251  *
252  * @see https://cloud.google.com/docs/authentication for more information on
253  * authentication in GCP.
254  *
255  * @ingroup guac
256  *
257  * @param json_object the external account configuration as a JSON string.
258  * Typically applications read this from a file, or download the contents
259  * from something like Google's secret manager service. The configuration
260  * file can be created using the [create-cred-config] `gcloud` subcommand.
261  * @param opts optional configuration values. Note that the effect of these
262  * parameters depends on the underlying transport. For example,
263  * `TracingComponentsOption` is ignored by gRPC-based services.
264  *
265  * [create-cred-config]:
266  * https://cloud.google.com/sdk/gcloud/reference/iam/workload-identity-pools/create-cred-config
267  * [IAM quotas]: https://cloud.google.com/iam/quotas
268  * [Workload Identity Federation]:
269  * https://cloud.google.com/iam/docs/workload-identity-federation
270  */
272  std::string json_object, Options opts = {});
273 
274 /**
275  * Configure the delegates for `MakeImpersonateServiceAccountCredentials()`
276  *
277  * @ingroup options
278  * @ingroup guac
279  */
281  using Type = std::vector<std::string>;
282 };
283 
284 /**
285  * Configure the scopes for `MakeImpersonateServiceAccountCredentials()`
286  *
287  * @ingroup options
288  * @ingroup guac
289  */
290 struct ScopesOption {
291  using Type = std::vector<std::string>;
292 };
293 
294 /**
295  * Configure the access token lifetime
296  *
297  * @ingroup options
298  * @ingroup guac
299  */
301  using Type = std::chrono::seconds;
302 };
303 
304 /**
305  * Configures a custom CA (Certificates Authority) certificates file.
306  *
307  * Most applications should use the system's root certificates and should avoid
308  * setting this option unnecessarily. A common exception to this recommendation
309  * are containerized applications. These often deploy without system's root
310  * certificates and need to explicitly configure a root of trust.
311  *
312  * The value of this option should be the name of a file in [PEM format].
313  * Consult your security team and/or system administrator for the contents of
314  * this file. Be aware of the security implications of adding new CA
315  * certificates to this file. Only use trustworthy sources for the CA
316  * certificates.
317  *
318  * For REST-based libraries this configures the [CAINFO option] in libcurl.
319  * These are used for all credentials that require authentication, including the
320  * default credentials.
321  *
322  * For gRPC-based libraries this configures the `pem_roots_cert` parameter in
323  * [grpc::SslCredentialsOptions].
324  *
325  * @warning gRPC does not have a programmatic mechanism to set the CA
326  * certificates for the default credentials. This option only has no effect
327  * with `MakeGoogleDefaultCredentials()`, or
328  * `MakeServiceAccountCredentials()`.
329  * Consider using the `GRPC_DEFAULT_SSL_ROOTS_FILE_PATH` environment
330  * variable in these cases.
331  *
332  * @note CA certificates can be revoked or expire, plan for updates in your
333  * deployment.
334  *
335  * @see https://en.wikipedia.org/wiki/Certificate_authority for a general
336  * introduction to SSL certificate authorities.
337  *
338  * [CAINFO Option]: https://curl.se/libcurl/c/CURLOPT_CAINFO.html
339  * [PEM format]: https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail
340  * [grpc::SslCredentialsOptions]:
341  * https://grpc.github.io/grpc/cpp/structgrpc_1_1_ssl_credentials_options.html
342  *
343  * @ingroup options
344  * @ingroup guac
345  */
347  using Type = std::string;
348 };
349 
350 /// A list of options related to authentication.
351 using UnifiedCredentialsOptionList =
355 
356 namespace internal {
357 
358 /**
359  * Use an insecure channel for AccessToken authentication.
360  *
361  * This is useful when testing against emulators, where it is impossible to
362  * create a secure channel.
363  */
364 struct UseInsecureChannelOption {
365  using Type = bool;
366 };
367 
368 } // namespace internal
370 } // namespace cloud
371 } // namespace google
372 
373 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_CREDENTIALS_H