forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_account_ssh_test.py
More file actions
261 lines (226 loc) · 7.93 KB
/
service_account_ssh_test.py
File metadata and controls
261 lines (226 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# Copyright 2019, Google, Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import base64
import json
import os
import random
import time
from google.oauth2 import service_account
import googleapiclient.discovery
from retrying import retry
from service_account_ssh import main
'''
The service account that runs this test must have the following roles:
- roles/compute.instanceAdmin.v1
- roles/compute.securityAdmin
- roles/iam.serviceAccountAdmin
- roles/iam.serviceAccountKeyAdmin
- roles/iam.serviceAccountUser
The Project Editor legacy role is not sufficient because it does not grant
several necessary permissions.
'''
def test_main(capsys):
# Initialize variables.
cmd = 'uname -a'
project = os.environ['GCLOUD_PROJECT']
test_id = 'oslogin-test-{id}'.format(id=str(random.randint(0, 1000000)))
zone = 'us-east1-d'
image_family = 'projects/debian-cloud/global/images/family/debian-9'
machine_type = 'zones/{zone}/machineTypes/f1-micro'.format(zone=zone)
account_email = '{test_id}@{project}.iam.gserviceaccount.com'.format(
test_id=test_id, project=project)
# Initialize the necessary APIs.
iam = googleapiclient.discovery.build(
'iam', 'v1', cache_discovery=False)
compute = googleapiclient.discovery.build(
'compute', 'v1', cache_discovery=False)
# Create the necessary test resources and retrieve the service account
# email and account key.
try:
print('Creating test resources.')
service_account_key = setup_resources(
compute, iam, project, test_id, zone, image_family,
machine_type, account_email)
except Exception:
print('Cleaning up partially created test resources.')
cleanup_resources(compute, iam, project, test_id, zone, account_email)
raise Exception('Could not set up the necessary test resources.')
# Get the target host name for the instance
hostname = compute.instances().get(
project=project,
zone=zone,
instance=test_id,
fields='networkInterfaces/accessConfigs/natIP'
).execute()['networkInterfaces'][0]['accessConfigs'][0]['natIP']
# Create a credentials object and use it to initialize the OS Login API.
credentials = service_account.Credentials.from_service_account_info(
json.loads(base64.b64decode(
service_account_key['privateKeyData']).decode('utf-8')))
oslogin = googleapiclient.discovery.build(
'oslogin', 'v1', cache_discovery=False, credentials=credentials)
account = 'users/' + account_email
@retry(wait_exponential_multiplier=1000, wait_exponential_max=300000,
stop_max_attempt_number=10)
def ssh_login():
main(cmd, project, test_id, zone, oslogin, account, hostname)
out, _ = capsys.readouterr()
assert_value = 'Linux {test_id}'.format(test_id=test_id)
assert assert_value in out
# Test SSH to the instance.
try:
ssh_login()
except Exception:
raise Exception('SSH to the test instance failed.')
finally:
cleanup_resources(compute, iam, project, test_id, zone, account_email)
def setup_resources(
compute, iam, project, test_id, zone,
image_family, machine_type, account_email):
# Create a temporary service account.
iam.projects().serviceAccounts().create(
name='projects/' + project,
body={
'accountId': test_id
}).execute()
# Grant the service account access to itself.
iam.projects().serviceAccounts().setIamPolicy(
resource='projects/' + project + '/serviceAccounts/' + account_email,
body={
'policy': {
'bindings': [
{
'members': [
'serviceAccount:' + account_email
],
'role': 'roles/iam.serviceAccountUser'
}
]
}
}).execute()
# Create a service account key.
service_account_key = iam.projects().serviceAccounts().keys().create(
name='projects/' + project + '/serviceAccounts/' + account_email,
body={}
).execute()
# Create a temporary firewall on the default network to allow SSH tests
# only for instances with the temporary service account.
firewall_config = {
'name': test_id,
'network': '/global/networks/default',
'targetServiceAccounts': [
account_email
],
'sourceRanges': [
'0.0.0.0/0'
],
'allowed': [{
'IPProtocol': 'tcp',
'ports': [
'22'
],
}]
}
compute.firewalls().insert(
project=project,
body=firewall_config).execute()
# Create a new test instance.
instance_config = {
'name': test_id,
'machineType': machine_type,
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': image_family,
}
}
],
'networkInterfaces': [{
'network': 'global/networks/default',
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}],
'serviceAccounts': [{
'email': account_email,
'scopes': [
'https://www.googleapis.com/auth/cloud-platform'
]
}],
'metadata': {
'items': [{
'key': 'enable-oslogin',
'value': 'TRUE'
}]
}
}
operation = compute.instances().insert(
project=project,
zone=zone,
body=instance_config).execute()
# Wait for the instance to start.
while compute.zoneOperations().get(
project=project,
zone=zone,
operation=operation['name']).execute()['status'] != 'DONE':
time.sleep(5)
# Grant the service account osLogin access on the test instance.
compute.instances().setIamPolicy(
project=project,
zone=zone,
resource=test_id,
body={
'bindings': [
{
'members': [
'serviceAccount:' + account_email
],
'role': 'roles/compute.osLogin'
}
]
}).execute()
# Wait for the IAM policy to take effect.
while compute.instances().getIamPolicy(
project=project,
zone=zone,
resource=test_id,
fields='bindings/role'
).execute()['bindings'][0]['role'] != 'roles/compute.osLogin':
time.sleep(5)
return service_account_key
def cleanup_resources(compute, iam, project, test_id, zone, account_email):
# Delete the temporary firewall.
try:
compute.firewalls().delete(
project=project,
firewall=test_id).execute()
except Exception:
pass
# Delete the test instance.
try:
delete = compute.instances().delete(
project=project, zone=zone, instance=test_id).execute()
while compute.zoneOperations().get(
project=project, zone=zone, operation=delete['name']
).execute()['status'] != 'DONE':
time.sleep(5)
except Exception:
pass
# Delete the temporary service account and its associated keys.
try:
iam.projects().serviceAccounts().delete(
name='projects/' + project + '/serviceAccounts/' + account_email
).execute()
except Exception:
pass