Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue 7277 #7370

Merged
merged 11 commits into from
Jul 22, 2022
17 changes: 14 additions & 3 deletions compute/oslogin/service_account_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
import time
import uuid

from google.auth.exceptions import RefreshError
import googleapiclient.discovery
import requests


# Global variables
SERVICE_ACCOUNT_METADATA_URL = (
'http://metadata.google.internal/computeMetadata/v1/instance/'
'service-accounts/default/email')
Expand Down Expand Up @@ -76,7 +75,19 @@ def create_ssh_key(oslogin, account, private_key_file=None, expire_time=300):
'key': public_key,
'expirationTimeUsec': expiration,
}
oslogin.users().importSshPublicKey(parent=account, body=body).execute()
print(f'Creating key {account} and {body}')
for attempt_no in range(1, 4):
try:
# This method sometimes failed to work causing issues like #7277
# Maybe retrying it with some delay will make things better
oslogin.users().importSshPublicKey(parent=account, body=body).execute()
except RefreshError:
if attempt_no == 3:
break
time.sleep(attempt_no)
else:
break

return private_key_file
# [END create_key]

Expand Down