blob: 32dfe8309e88b8b3f0af733cffcc549f7ce38179 [file] [log] [blame]
salrashid1231fbc6792018-11-09 11:05:34 -08001# Copyright 2018 Google Inc.
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# http://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"""Google Cloud Impersonated credentials.
16
17This module provides authentication for applications where local credentials
18impersonates a remote service account using `IAM Credentials API`_.
19
20This class can be used to impersonate a service account as long as the original
21Credential object has the "Service Account Token Creator" role on the target
22service account.
23
24 .. _IAM Credentials API:
25 https://cloud.google.com/iam/credentials/reference/rest/
26"""
27
28import copy
29from datetime import datetime
30import json
31
32import six
33from six.moves import http_client
34
35from google.auth import _helpers
36from google.auth import credentials
37from google.auth import exceptions
38
39_DEFAULT_TOKEN_LIFETIME_SECS = 3600 # 1 hour in seconds
40
41_IAM_SCOPE = ['https://www.googleapis.com/auth/iam']
42
43_IAM_ENDPOINT = ('https://iamcredentials.googleapis.com/v1/projects/-' +
44 '/serviceAccounts/{}:generateAccessToken')
45
46_REFRESH_ERROR = 'Unable to acquire impersonated credentials'
salrashid1231fbc6792018-11-09 11:05:34 -080047
48
49def _make_iam_token_request(request, principal, headers, body):
50 """Makes a request to the Google Cloud IAM service for an access token.
51 Args:
52 request (Request): The Request object to use.
53 principal (str): The principal to request an access token for.
54 headers (Mapping[str, str]): Map of headers to transmit.
55 body (Mapping[str, str]): JSON Payload body for the iamcredentials
56 API call.
57
58 Raises:
59 TransportError: Raised if there is an underlying HTTP connection
60 Error
61 DefaultCredentialsError: Raised if the impersonated credentials
62 are not available. Common reasons are
63 `iamcredentials.googleapis.com` is not enabled or the
64 `Service Account Token Creator` is not assigned
65 """
66 iam_endpoint = _IAM_ENDPOINT.format(principal)
67
68 body = json.dumps(body)
69
70 response = request(
71 url=iam_endpoint,
72 method='POST',
73 headers=headers,
74 body=body)
75
76 response_body = response.data.decode('utf-8')
77
78 if response.status != http_client.OK:
79 exceptions.RefreshError(_REFRESH_ERROR, response_body)
80
81 try:
82 token_response = json.loads(response.data.decode('utf-8'))
83 token = token_response['accessToken']
84 expiry = datetime.strptime(
85 token_response['expireTime'], '%Y-%m-%dT%H:%M:%SZ')
86
87 return token, expiry
88
89 except (KeyError, ValueError) as caught_exc:
90 new_exc = exceptions.RefreshError(
91 '{}: No access token or invalid expiration in response.'.format(
92 _REFRESH_ERROR),
93 response_body)
94 six.raise_from(new_exc, caught_exc)
95
96
97class Credentials(credentials.Credentials):
98 """This module defines impersonated credentials which are essentially
99 impersonated identities.
100
101 Impersonated Credentials allows credentials issued to a user or
102 service account to impersonate another. The target service account must
103 grant the originating credential principal the
104 `Service Account Token Creator`_ IAM role:
105
106 For more information about Token Creator IAM role and
107 IAMCredentials API, see
108 `Creating Short-Lived Service Account Credentials`_.
109
110 .. _Service Account Token Creator:
111 https://cloud.google.com/iam/docs/service-accounts#the_service_account_token_creator_role
112
113 .. _Creating Short-Lived Service Account Credentials:
114 https://cloud.google.com/iam/docs/creating-short-lived-service-account-credentials
115
116 Usage:
117
118 First grant source_credentials the `Service Account Token Creator`
119 role on the target account to impersonate. In this example, the
120 service account represented by svc_account.json has the
121 token creator role on
122 `impersonated-account@_project_.iam.gserviceaccount.com`.
123
salrashid123b29f2622018-11-12 09:49:16 -0800124 Enable the IAMCredentials API on the source project:
125 `gcloud services enable iamcredentials.googleapis.com`.
126
salrashid1231fbc6792018-11-09 11:05:34 -0800127 Initialize a source credential which does not have access to
128 list bucket::
129
130 from google.oauth2 import service_acccount
131
132 target_scopes = [
133 'https://www.googleapis.com/auth/devstorage.read_only']
134
135 source_credentials = (
136 service_account.Credentials.from_service_account_file(
137 '/path/to/svc_account.json',
138 scopes=target_scopes))
139
140 Now use the source credentials to acquire credentials to impersonate
141 another service account::
142
143 from google.auth import impersonated_credentials
144
145 target_credentials = impersonated_credentials.Credentials(
146 source_credentials=source_credentials,
147 target_principal='impersonated-account@_project_.iam.gserviceaccount.com',
148 target_scopes = target_scopes,
149 lifetime=500)
150
151 Resource access is granted::
152
153 client = storage.Client(credentials=target_credentials)
154 buckets = client.list_buckets(project='your_project')
155 for bucket in buckets:
156 print bucket.name
157 """
158
159 def __init__(self, source_credentials, target_principal,
160 target_scopes, delegates=None,
salrashid123b29f2622018-11-12 09:49:16 -0800161 lifetime=_DEFAULT_TOKEN_LIFETIME_SECS):
salrashid1231fbc6792018-11-09 11:05:34 -0800162 """
163 Args:
164 source_credentials (google.auth.Credentials): The source credential
165 used as to acquire the impersonated credentials.
166 target_principal (str): The service account to impersonate.
167 target_scopes (Sequence[str]): Scopes to request during the
168 authorization grant.
169 delegates (Sequence[str]): The chained list of delegates required
170 to grant the final access_token. If set, the sequence of
171 identities must have "Service Account Token Creator" capability
172 granted to the prceeding identity. For example, if set to
173 [serviceAccountB, serviceAccountC], the source_credential
174 must have the Token Creator role on serviceAccountB.
175 serviceAccountB must have the Token Creator on serviceAccountC.
176 Finally, C must have Token Creator on target_principal.
177 If left unset, source_credential must have that role on
178 target_principal.
179 lifetime (int): Number of seconds the delegated credential should
salrashid123b29f2622018-11-12 09:49:16 -0800180 be valid for (upto 3600).
salrashid1231fbc6792018-11-09 11:05:34 -0800181 """
182
183 super(Credentials, self).__init__()
184
185 self._source_credentials = copy.copy(source_credentials)
186 self._source_credentials._scopes = _IAM_SCOPE
187 self._target_principal = target_principal
188 self._target_scopes = target_scopes
189 self._delegates = delegates
190 self._lifetime = lifetime
191 self.token = None
192 self.expiry = _helpers.utcnow()
193
194 @_helpers.copy_docstring(credentials.Credentials)
195 def refresh(self, request):
salrashid1231fbc6792018-11-09 11:05:34 -0800196 self._update_token(request)
197
198 @property
199 def expired(self):
200 return _helpers.utcnow() >= self.expiry
201
202 def _update_token(self, request):
203 """Updates credentials with a new access_token representing
204 the impersonated account.
205
206 Args:
207 request (google.auth.transport.requests.Request): Request object
208 to use for refreshing credentials.
209 """
210
211 # Refresh our source credentials.
212 self._source_credentials.refresh(request)
213
salrashid1231fbc6792018-11-09 11:05:34 -0800214 body = {
215 "delegates": self._delegates,
216 "scope": self._target_scopes,
salrashid123b29f2622018-11-12 09:49:16 -0800217 "lifetime": str(self._lifetime) + "s"
salrashid1231fbc6792018-11-09 11:05:34 -0800218 }
219
220 headers = {
221 'Content-Type': 'application/json',
222 }
223
224 # Apply the source credentials authentication info.
225 self._source_credentials.apply(headers)
226
227 self.token, self.expiry = _make_iam_token_request(
228 request=request,
229 principal=self._target_principal,
230 headers=headers,
231 body=body)