From 50f819d27ef994fb48274cde6ce552206a6940ee Mon Sep 17 00:00:00 2001 From: lzzy12 Date: Wed, 25 Mar 2020 15:19:46 +0530 Subject: [PATCH] Add a script to download SA keys in case user looses them Signed-off-by: lzzy12 --- README.md | 5 +++ download_service_accounts.py | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 download_service_accounts.py diff --git a/README.md b/README.md index f0db9b9..30e91ff 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ Fill up rest of the fields. Meaning of each fields are discussed below: - AUTO_DELETE_MESSAGE_DURATION : Interval of time (in seconds), after which the bot deletes it's message (and command message) which is expected to be viewed instantly. Note: Set to -1 to never automatically delete messages - IS_TEAM_DRIVE : (Optional field) Set to "True" if GDRIVE_FOLDER_ID is from a Team Drive else False or Leave it empty. - INDEX_URL : (Optional field) Refer to https://github.com/maple3142/GDIndex/ The URL should not have any trailing '/' +- API_KEY : This is to authenticate to your telegram account for downloading Telegram files. You can get this from https://my.telegram.org DO NOT put this in quotes. +- API_HASH : This is to authenticate to your telegram account for downloading Telegram files. You can get this from https://my.telegram.org - USER_SESSION_STRING : Session string generated by running: ``` python3 generate_string_session.py @@ -106,6 +108,9 @@ Note: 1 service account can copy around 750gb a day, 1 project makes 100 service A folder named accounts will be created which will contain keys for the service accounts created ``` We highly recommend to zip this folder and store it somewhere safe, so that you do not have to create a new project everytime you want to deploy the bot + +**However** If you lose it anyway, you can redownload it by running script: +python3 download_service_accounts.py project_id ``` ### Adding service accounts to Google Groups: We use Google Groups to manager our service accounts considering the diff --git a/download_service_accounts.py b/download_service_accounts.py new file mode 100644 index 0000000..4fe1abb --- /dev/null +++ b/download_service_accounts.py @@ -0,0 +1,62 @@ +import os +import sys +import pickle + +from google_auth_oauthlib.flow import InstalledAppFlow +from google.auth.transport.requests import Request +from googleapiclient import discovery + +OAUTH_SCOPE = ['https://www.googleapis.com/auth/cloud-platform'] + + +# noinspection DuplicatedCode +def authorize(): + # Get credentials + credentials = None + if os.path.exists('token-iam.pickle'): + with open('token-iam.pickle', 'rb') as f: + credentials = pickle.load(f) + if credentials is None or not credentials.valid: + if credentials and credentials.expired and credentials.refresh_token: + credentials.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + 'credentials.json', OAUTH_SCOPE) + credentials = flow.run_console(port=0) + + # Save the credentials for the next run + with open('token-iam.pickle', 'wb') as token: + pickle.dump(credentials, token) + return discovery.build('iam', 'v1', credentials=credentials) + + +def get_service_accounts(project_id): + """Returns a list of service accounts in project with id == project_id""" + accounts = service.projects().serviceAccounts().list( + name=f'projects/{project_id}').execute() + return accounts['accounts'] + + +def download_keys(project_id): + accounts = get_service_accounts(project_id) + print(f'{len(accounts)} service accounts found! Dumping now') + i = 0 + for acc in accounts: + if not os.path.exists('accounts/'): + os.mkdir('accounts/') + if os.path.isfile('accounts/'): + os.remove('accounts') + os.mkdir('accounts/') + print("Dumping " + acc['email']) + key = service.projects().serviceAccounts().keys().create( + name='projects/-/serviceAccounts/' + acc['email'], body={} + ).execute() + with open(f'accounts/{i}.json', 'w') as f: + f.write(str(key)) + i += 1 + + +service = authorize() + +if __name__ == '__main__': + download_keys(sys.argv[1])