Add a script to download SA keys in case user looses them

Signed-off-by: lzzy12 <jhashivam2020@gmail.com>
This commit is contained in:
lzzy12 2020-03-25 15:19:46 +05:30
parent 08a71e4bd4
commit 50f819d27e
2 changed files with 67 additions and 0 deletions

View File

@ -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

View File

@ -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])