Google Cloud C++ Client  2.2.1
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/options.h"
19 #include "google/cloud/version.h"
20 #include <chrono>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 namespace google {
26 namespace cloud {
28 namespace internal {
29 class CredentialsVisitor;
30 } // namespace internal
31 
32 /**
33  * The public interface for Google's Unified Auth Client (GUAC) library.
34  *
35  * The Unified Auth Client library allows C++ applications to configure
36  * authentication for both REST-based and gRPC-based client libraries. The
37  * library public interface is (intentionally) very narrow. Applications
38  * describe the type of authentication they want, the libraries used this
39  * description to initialize the internal components used in the authentication
40  * flows.
41  *
42  * A complete overview of authentication and authorization for Google Cloud is
43  * outside the scope of this reference guide. We recommend the [IAM overview]
44  * instead. The following brief introduction may help as you read the reference
45  * documentation for components related to authentication:
46  *
47  * - The `Credentials` class and the factory functions that create
48  * `std::shared_ptr<Credentials>` objects are related to *authentication*.
49  * That is they allow you to define what *principal* is making RPCs to GCP.
50  * - The problem of *authorization*, that is, what principals can perform what
51  * operations, is resolved by the [IAM Service] in GCP. If you need to use
52  * this service, the [C++ IAM client library] may be useful. Some services
53  * embed IAM operations in their APIs, in that case, the C++ client library
54  * for the service may be easier to use.
55  * - There are two types of "principals" in GCP.
56  * - **Service Accounts**: is an account for an application or compute
57  * workload instead of an individual end user.
58  * - **Google Accounts**: represents a developer, an administrator, or any
59  * other person who interacts with Google Cloud.
60  * - Most applications should use `GoogleDefaultCredentials()`. This allows your
61  * administrator to configure the service account used in your workload by
62  * setting the default service account in the GCP environment where your
63  * application is deployed (e.g. GCE, GKE, or Cloud Run).
64  * - During development, `GoogleDefaultCredentials()` uses the
65  * `GOOGLE_APPLICATION_CREDENTIALS` environment variable to load an
66  * alternative service account key. The value of this environment variable is
67  * the full path of a file which contains the service account key.
68  * - During development, if `GOOGLE_APPLICATION_CREDENTIALS` is not set then
69  * `GoogleDefaultCredentials()` will use the account configured via
70  * `gcloud auth application-default login`. This can be either a service
71  * account or a Google Account, such as the developer's account.
72  * - Neither `google auth application-default` nor
73  * `GOOGLE_APPLICATION_CREDENTIALS` are recommended for production workloads.
74  *
75  * @par Limitations
76  * The C++ GUAC library does not allow applications to create their own
77  * credential types. It is not possible to extend the GUAC library without
78  * changing internal components. If you need additional functionality please
79  * file a [feature request] on GitHub. Likewise, creating the components that
80  * implement (as opposed to *describing*) authentication flows are also
81  * considered implementation details. If you would like to use them in your
82  * own libraries please file a [feature request].
83  *
84  * @see https://cloud.google.com/docs/authentication for more information on
85  * authentication in GCP.
86  *
87  * @see https://cloud.google.com/iam for more information on the IAM Service.
88  *
89  * [feature request]: https://github.com/googleapis/google-cloud-cpp/issues
90  * [IAM overview]: https://cloud.google.com/iam/docs/overview
91  * [IAM Service]: https://cloud.google.com/iam/docs
92  * [C++ IAM client library]: https://googleapis.dev/cpp/google-cloud-iam/latest/
93  */
94 class Credentials {
95  public:
96  virtual ~Credentials() = 0;
97 
98  private:
99  friend class internal::CredentialsVisitor;
100  virtual void dispatch(internal::CredentialsVisitor& visitor) = 0;
101 };
102 
103 /**
104  * Create insecure (aka anonymous, aka unauthenticated) credentials.
105  *
106  * These credentials are mostly intended for testing. Integration tests running
107  * against an emulator do not need to authenticate. In fact, it may be
108  * impossible to connect to an emulator using SSL/TLS because the emulators
109  * typically run without secure communication.
110  *
111  * In addition, unit tests may benefit from using these credentials: loading the
112  * default credentials unnecessarily slows down the unit tests, and in some
113  * CI environments the credentials may fail to load, creating confusing warnings
114  * and sometimes even errors.
115  */
117 
118 /**
119  * Creates the default credentials.
120  *
121  * These are the most commonly used credentials, and are expected to meet the
122  * needs of most applications. The Google Default Credentials conform to
123  * [aip/4110]. Consider using these credentials when:
124  *
125  * - Your application is deployed to a GCP environment such as GCE, GKE, or
126  * Cloud Run. Each of these deployment environments provides a default service
127  * account to the application, and offers mechanisms to change the default
128  * credentials without any code changes to your application.
129  * - You are testing or developing the application on a workstation (physical or
130  * virtual). These credentials will use your preferences as set with
131  * [gcloud auth application-default]. These preferences can be your own GCP
132  * user credentials, or some service account.
133  * - Regardless of where your application is running, you can use the
134  * `GOOGLE_APPLICATION_CREDENTIALS` environment variable to override the
135  * defaults. This environment variable should point to a file containing a
136  * service account key file, or a JSON object describing your user
137  * credentials.
138  *
139  * @see https://cloud.google.com/docs/authentication for more information on
140  * authentication in GCP.
141  *
142  * [aip/4110]: https://google.aip.dev/auth/4110
143  * [gcloud auth application-default]:
144  * https://cloud.google.com/sdk/gcloud/reference/auth/application-default
145  */
147 
148 /**
149  * Creates credentials with a fixed access token.
150  *
151  * These credentials are useful when using an out-of-band mechanism to fetch
152  * access tokens. Note that access tokens are time limited, you will need to
153  * manually refresh the tokens created by the
154  *
155  * @see https://cloud.google.com/docs/authentication for more information on
156  * authentication in GCP.
157  */
159  std::string const& access_token,
160  std::chrono::system_clock::time_point expiration);
161 
162 /**
163  * Creates credentials for service account impersonation.
164  *
165  * Service account impersonation allows one account (user or service account)
166  * to *act as* a second account. This can be useful in multi-tenant services,
167  * where the service may perform some actions with an specific account
168  * associated with a tenant. The tenant can grant or restrict permissions to
169  * this tenant account.
170  *
171  * When using service account impersonation is important to distinguish between
172  * the credentials used to *obtain* the target account credentials (the
173  * @p base_credentials) parameter, and the credentials representing the
174  * @p target_service_account.
175  *
176  * Use `AccessTokenLifetimeOption` to configure the maximum lifetime of the
177  * obtained credentials. The default is 1h (3600s), see [IAM quotas] for the
178  * limits set by the platform and how to override them.
179  *
180  * Use `DelegatesOption` to configure a sequence of intermediate service
181  * account, each of which has permissions to impersonate the next and the
182  * last one has permissions to impersonate @p target_service_account.
183  *
184  * Use `ScopesOption` to restrict the authentication scope for the obtained
185  * credentials. See below for possible values.
186  *
187  * [IAM quotas]: https://cloud.google.com/iam/quotas
188  * @see https://cloud.google.com/docs/authentication for more information on
189  * authentication in GCP.
190  * @see https://cloud.google.com/iam/docs/impersonating-service-accounts for
191  * information on managing service account impersonation.
192  * @see https://developers.google.com/identity/protocols/oauth2/scopes for
193  * authentication scopes in Google Cloud Platform.
194  */
196  std::shared_ptr<Credentials> base_credentials,
197  std::string target_service_account, Options opts = {});
198 
199 /**
200  * Creates service account credentials from a service account key.
201  *
202  * A [service account] is an account for an application or compute workload
203  * instead of an individual end user. The recommended practice is to use
204  * Google Default Credentials, which relies on the configuration of the Google
205  * Cloud system hosting your application (GCE, GKE, Cloud Run) to authenticate
206  * your workload or application. But sometimes you may need to create and
207  * download a [service account key], for example, to use a service account
208  * when running your application on a system that is not part of Google Cloud.
209  *
210  * Service account credentials are used in this latter case.
211  *
212  * You can create multiple service account keys for a single service account.
213  * When you create a service account key, the key is returned as string, in the
214  * format described by [aip/4112]. This string contains an id for the service
215  * account, as well as the cryptographical materials (a RSA private key)
216  * required to authenticate the caller.
217  *
218  * Therefore, services account keys should be treated as any other secret
219  * with security implications. Think of them as unencrypted passwords. Do not
220  * store them where unauthorized persons or programs may read them.
221  *
222  * As stated above, most applications should probably use default credentials,
223  * maybe pointing them to a file with these contents. Using this function may be
224  * useful when the service account key is obtained from Cloud Secret Manager or
225  * a similar service.
226  *
227  * [aip/4112]: https://google.aip.dev/auth/4112
228  * [service account]: https://cloud.google.com/iam/docs/overview#service_account
229  * [service account key]:
230  * https://cloud.google.com/iam/docs/creating-managing-service-account-keys#iam-service-account-keys-create-cpp
231  */
233  std::string json_object);
234 
235 /// Configure the delegates for `MakeImpersonateServiceAccountCredentials()`
237  using Type = std::vector<std::string>;
238 };
239 
240 /// Configure the scopes for `MakeImpersonateServiceAccountCredentials()`
241 struct ScopesOption {
242  using Type = std::vector<std::string>;
243 };
244 
245 /// Configure the access token lifetime
247  using Type = std::chrono::seconds;
248 };
249 
250 /// A wrapper to store credentials into an options
252  using Type = std::shared_ptr<Credentials>;
253 };
254 
255 /**
256  * Configures a custom CA (Certificates Authority) certificates file.
257  *
258  * Most applications should use the system's root certificates and should avoid
259  * setting this option unnecessarily. A common exception to this recommendation
260  * are containerized applications. These often deploy without system's root
261  * certificates and need to explicitly configure a root of trust.
262  *
263  * The value of this option should be the name of a file in [PEM format].
264  * Consult your security team and/or system administrator for the contents of
265  * this file. Be aware of the security implications of adding new CA
266  * certificates to this file. Only use trustworthy sources for the CA
267  * certificates.
268  *
269  * For REST-based libraries this configures the [CAINFO option] in libcurl.
270  * These are used for all credentials that require authentication, including the
271  * default credentials.
272  *
273  * For gRPC-based libraries this configures the `pem_roots_cert` parameter in
274  * [grpc::SslCredentialsOptions].
275  *
276  * @warning gRPC does not have a programmatic mechanism to set the CA
277  * certificates for the default credentials. This option only has no effect
278  * with `MakeGoogleDefaultCredentials()`, or
279  * `MakeServiceAccountCredentials()`.
280  * Consider using the `GRPC_DEFAULT_SSL_ROOTS_FILE_PATH` environment
281  * variable in these cases.
282  *
283  * @note CA certificates can be revoked or expire, plan for updates in your
284  * deployment.
285  *
286  * @see https://en.wikipedia.org/wiki/Certificate_authority for a general
287  * introduction to SSL certificate authorities.
288  *
289  * [CAINFO Option]: https://curl.se/libcurl/c/CURLOPT_CAINFO.html
290  * [PEM format]: https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail
291  * [grpc::SslCredentialsOptions]:
292  * https://grpc.github.io/grpc/cpp/structgrpc_1_1_ssl_credentials_options.html
293  */
295  using Type = std::string;
296 };
297 
298 /// A list of GUAC options.
299 using UnifiedCredentialsOptionList =
302 
303 namespace internal {
304 
305 /**
306  * Use an insecure channel for AccessToken authentication.
307  *
308  * This is useful when testing against emulators, where it is impossible to
309  * create a secure channel.
310  */
311 struct UseInsecureChannelOption {
312  using Type = bool;
313 };
314 
315 } // namespace internal
317 } // namespace cloud
318 } // namespace google
319 
320 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_CREDENTIALS_H