Skip to content

Commit 77e8bae

Browse files
authored
doc: a gentler introduction to IAM and service accounts (#9602)
I think the previous description assumed the reader already knew what service accounts are and why you would use them.
1 parent 155a785 commit 77e8bae

File tree

1 file changed

+62
-13
lines changed

1 file changed

+62
-13
lines changed

google/cloud/credentials.h

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,39 @@ class CredentialsVisitor;
3939
* description to initialize the internal components used in the authentication
4040
* flows.
4141
*
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+
*
4275
* @par Limitations
4376
* The C++ GUAC library does not allow applications to create their own
4477
* credential types. It is not possible to extend the GUAC library without
@@ -51,7 +84,12 @@ class CredentialsVisitor;
5184
* @see https://cloud.google.com/docs/authentication for more information on
5285
* authentication in GCP.
5386
*
87+
* @see https://cloud.google.com/iam for more information on the IAM Service.
88+
*
5489
* [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/
5593
*/
5694
class Credentials {
5795
public:
@@ -159,26 +197,37 @@ std::shared_ptr<Credentials> MakeImpersonateServiceAccountCredentials(
159197
std::string target_service_account, Options opts = {});
160198

161199
/**
162-
* Creates service account credentials from a JSON object in string form.
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.
163211
*
164-
* The @p json_object is expected to be in the format described by [aip/4112].
165-
* Such an object contains the identity of a service account, as well as a
166-
* private key that can be used to sign tokens, showing the caller was holding
167-
* the private key.
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.
168217
*
169-
* In GCP one can create several "keys" for each service account, and these
170-
* keys are downloaded as a JSON "key file". The contents of such a file are
171-
* in the format required by this function. Remember that key files and their
172-
* contents should be treated as any other secret with security implications,
173-
* think of them as passwords (because they are!), don't store them or output
174-
* them where unauthorized persons may read them.
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.
175221
*
176222
* As stated above, most applications should probably use default credentials,
177223
* maybe pointing them to a file with these contents. Using this function may be
178-
* useful when the json object is obtained from a Cloud Secret Manager or a
179-
* similar service.
224+
* useful when the service account key is obtained from Cloud Secret Manager or
225+
* a similar service.
180226
*
181227
* [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
182231
*/
183232
std::shared_ptr<Credentials> MakeServiceAccountCredentials(
184233
std::string json_object);

0 commit comments

Comments
 (0)