Skip to content

Commit 656307d

Browse files
authored
fix: improve user guide for Impersonation and SA (#1627)
* fix: improve user guide for Impersonation and SA * moved sa to end
1 parent 3fae8f8 commit 656307d

File tree

2 files changed

+60
-52
lines changed

2 files changed

+60
-52
lines changed

docs/user-guide.rst

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -62,57 +62,6 @@ store service account private keys locally.
6262
.. _Google Cloud SDK: https://cloud.google.com/sdk
6363

6464

65-
Service account private key files
66-
+++++++++++++++++++++++++++++++++
67-
68-
A service account private key file can be used to obtain credentials for a
69-
service account. You can create a private key using the `Credentials page of the
70-
Google Cloud Console`_. Once you have a private key you can either obtain
71-
credentials one of three ways:
72-
73-
1. Set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to the full
74-
path to your service account private key file
75-
76-
.. code-block:: bash
77-
78-
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json
79-
80-
Then, use :ref:`application default credentials <application-default>`.
81-
:func:`default` checks for the ``GOOGLE_APPLICATION_CREDENTIALS``
82-
environment variable before all other checks, so this will always use the
83-
credentials you explicitly specify.
84-
85-
2. Use :meth:`service_account.Credentials.from_service_account_file
86-
<google.oauth2.service_account.Credentials.from_service_account_file>`::
87-
88-
from google.oauth2 import service_account
89-
90-
credentials = service_account.Credentials.from_service_account_file(
91-
'/path/to/key.json')
92-
93-
scoped_credentials = credentials.with_scopes(
94-
['https://www.googleapis.com/auth/cloud-platform'])
95-
96-
3. Use :meth:`service_account.Credentials.from_service_account_info
97-
<google.oauth2.service_account.Credentials.from_service_account_info>`::
98-
99-
import json
100-
101-
from google.oauth2 import service_account
102-
103-
json_acct_info = json.loads(function_to_get_json_creds())
104-
credentials = service_account.Credentials.from_service_account_info(
105-
json_acct_info)
106-
107-
scoped_credentials = credentials.with_scopes(
108-
['https://www.googleapis.com/auth/cloud-platform'])
109-
110-
.. warning:: Private keys must be kept secret. If you expose your private key it
111-
is recommended to revoke it immediately from the Google Cloud Console.
112-
113-
.. _Credentials page of the Google Cloud Console:
114-
https://console.cloud.google.com/apis/credentials
115-
11665
Compute Engine, Container Engine, and the App Engine flexible environment
11766
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11867

@@ -231,6 +180,7 @@ You can also use :class:`google_auth_oauthlib.flow.Flow` to perform the OAuth
231180
.. _requests-oauthlib:
232181
https://requests-oauthlib.readthedocs.io/en/latest/
233182

183+
234184
External credentials (Workload identity federation)
235185
+++++++++++++++++++++++++++++++++++++++++++++++++++
236186

@@ -981,7 +931,8 @@ Impersonated credentials
981931
++++++++++++++++++++++++
982932

983933
Impersonated Credentials allows one set of credentials issued to a user or service account
984-
to impersonate another. The source credentials must be granted
934+
to impersonate a service account. Impersonation is the preferred way of using service account for
935+
local development over downloading the service account key. The source credentials must be granted
985936
the "Service Account Token Creator" IAM role. ::
986937

987938
from google.auth import impersonated_credentials
@@ -1006,6 +957,63 @@ In the example above `source_credentials` does not have direct access to list bu
1006957
in the target project. Using `ImpersonatedCredentials` will allow the source_credentials
1007958
to assume the identity of a target_principal that does have access.
1008959

960+
It is possible to provide a delegation chain through `delegates` paramter while
961+
initializing the impersonated credential. Refer `create short lived credentials delegated`_ for more details on delegation chain.
962+
963+
.. _create short lived credentials delegated: https://cloud.google.com/iam/docs/create-short-lived-credentials-delegated
964+
965+
966+
Service account private key files
967+
+++++++++++++++++++++++++++++++++
968+
969+
A service account private key file can be used to obtain credentials for a service account. If you are not
970+
able to use any of the authentication methods listed above, you can create a private key using `Credentials page of the
971+
Google Cloud Console`_. Once you have a private key you can obtain
972+
credentials one of three ways:
973+
974+
1. Set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to the full
975+
path to your service account private key file
976+
977+
.. code-block:: bash
978+
979+
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/key.json
980+
981+
Then, use :ref:`application default credentials <application-default>`.
982+
:func:`default` checks for the ``GOOGLE_APPLICATION_CREDENTIALS``
983+
environment variable before all other checks, so this will always use the
984+
credentials you explicitly specify.
985+
986+
2. Use :meth:`service_account.Credentials.from_service_account_file
987+
<google.oauth2.service_account.Credentials.from_service_account_file>`::
988+
989+
from google.oauth2 import service_account
990+
991+
credentials = service_account.Credentials.from_service_account_file(
992+
'/path/to/key.json')
993+
994+
scoped_credentials = credentials.with_scopes(
995+
['https://www.googleapis.com/auth/cloud-platform'])
996+
997+
3. Use :meth:`service_account.Credentials.from_service_account_info
998+
<google.oauth2.service_account.Credentials.from_service_account_info>`::
999+
1000+
import json
1001+
1002+
from google.oauth2 import service_account
1003+
1004+
json_acct_info = json.loads(function_to_get_json_creds())
1005+
credentials = service_account.Credentials.from_service_account_info(
1006+
json_acct_info)
1007+
1008+
scoped_credentials = credentials.with_scopes(
1009+
['https://www.googleapis.com/auth/cloud-platform'])
1010+
1011+
.. warning:: Private keys must be kept secret. If you expose your private key it
1012+
is recommended to revoke it immediately from the Google Cloud Console.
1013+
1014+
.. _Credentials page of the Google Cloud Console:
1015+
https://console.cloud.google.com/apis/credentials
1016+
10091017

10101018
Downscoped credentials
10111019
++++++++++++++++++++++

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)