From 6e16a2948da97e0a6e3a83daa38bf8abf3f4499d Mon Sep 17 00:00:00 2001 From: steven zhu Date: Tue, 21 Jul 2020 22:50:53 -0400 Subject: [PATCH 1/6] Add sa email export script --- print_sa.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 print_sa.py diff --git a/print_sa.py b/print_sa.py new file mode 100644 index 000000000..9af6dab71 --- /dev/null +++ b/print_sa.py @@ -0,0 +1,43 @@ +# auto rclone +# Print service account emails and store it into txt file +# +# Author Telegram https://t.me/szhu25 +# Inbox github@stevenz.net + +from __future__ import print_function + +import os, argparse, time, json, glob + +stt = time.time() + +parse = argparse.ArgumentParser( + description='A tool to add service accounts to groups for your organization from a folder containing credential ' + 'files.') +parse.add_argument('--path', '-p', default='accounts', + help='Specify an alternative path to the service accounts folder.') +# service-account@googlegroups.com + +args = parse.parse_args() +acc_dir = args.path + +sa = glob.glob('%s/*.json' % acc_dir) + +saList = [] + +print('Generating List') +for i in sa: + ce = json.loads(open(i, 'r').read())['client_email'] + saList.append(ce) + +print('List generated') +print('Deduping...') +saList1 = list(dict.fromkeys(saList)) +print('Complete.') + +hours, rem = divmod((time.time() - stt), 3600) +minutes, sec = divmod(rem, 60) +print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec)) + +with open('salist.txt', 'w') as f: + for i in saList1: + print(i, file=f) From 0f2757a3e98bf496048e0e0a074b6480c7a8ff97 Mon Sep 17 00:00:00 2001 From: steven zhu Date: Tue, 21 Jul 2020 23:10:17 -0400 Subject: [PATCH 2/6] add line seperator & preventive fix --- print_sa.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/print_sa.py b/print_sa.py index 9af6dab71..bf8ad45f8 100644 --- a/print_sa.py +++ b/print_sa.py @@ -15,10 +15,17 @@ 'files.') parse.add_argument('--path', '-p', default='accounts', help='Specify an alternative path to the service accounts folder.') +parse.add_argument('--sa', '-sa', default='salist.txt', + help='Specify an alternative path to generate list of your service accounts.') +parse.add_argument('--seperator', '-sp', default='0', + help='Insert a empty line for every x lines to better seperate email adding process.') + # service-account@googlegroups.com args = parse.parse_args() acc_dir = args.path +sa_file = args.sa +sp_num = args.seperator sa = glob.glob('%s/*.json' % acc_dir) @@ -38,6 +45,8 @@ minutes, sec = divmod(rem, 60) print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec)) -with open('salist.txt', 'w') as f: - for i in saList1: - print(i, file=f) +with open(sa_file, 'w') as f: + for i in range(len(saList1)): + if ((int(sp_num) <= len(saList1)+1) and (int(sp_num) != 0) and (i % int(sp_num) == 0)): + print("", file=f) + print(saList1[i], file=f) From d7818f666b8b5b3c57e87b5c488342d8629c56a8 Mon Sep 17 00:00:00 2001 From: steven zhu Date: Wed, 22 Jul 2020 00:01:52 -0400 Subject: [PATCH 3/6] Add project filtering --- print_sa.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/print_sa.py b/print_sa.py index bf8ad45f8..a82b5cf0a 100644 --- a/print_sa.py +++ b/print_sa.py @@ -6,7 +6,7 @@ from __future__ import print_function -import os, argparse, time, json, glob +import os, argparse, time, json, glob, re stt = time.time() @@ -19,19 +19,21 @@ help='Specify an alternative path to generate list of your service accounts.') parse.add_argument('--seperator', '-sp', default='0', help='Insert a empty line for every x lines to better seperate email adding process.') - -# service-account@googlegroups.com +parse.add_argument('--project', '-project', default='', + help='Project filter, specify this will only output emails that belong to the project you specified.') args = parse.parse_args() acc_dir = args.path sa_file = args.sa sp_num = args.seperator +sa_project = args.project sa = glob.glob('%s/*.json' % acc_dir) saList = [] print('Generating List') + for i in sa: ce = json.loads(open(i, 'r').read())['client_email'] saList.append(ce) @@ -39,6 +41,9 @@ print('List generated') print('Deduping...') saList1 = list(dict.fromkeys(saList)) +if (sa_project != ""): + print("Filtering email from %s." % sa_project) + saList1 = [n for n in saList1 if sa_project in n] print('Complete.') hours, rem = divmod((time.time() - stt), 3600) From 860847c4932f4968c5921b1be9d1d670215ec64e Mon Sep 17 00:00:00 2001 From: steven zhu Date: Wed, 22 Jul 2020 00:02:55 -0400 Subject: [PATCH 4/6] Add project filtering --- print_sa.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/print_sa.py b/print_sa.py index a82b5cf0a..a01e393d4 100644 --- a/print_sa.py +++ b/print_sa.py @@ -3,10 +3,11 @@ # # Author Telegram https://t.me/szhu25 # Inbox github@stevenz.net +# Version 0.3 from __future__ import print_function -import os, argparse, time, json, glob, re +import os, argparse, time, json, glob stt = time.time() From 3fac0e989bb2e6c79f1bfee1e43b14e3c5bcadc7 Mon Sep 17 00:00:00 2001 From: Steven Zhu Date: Wed, 22 Jul 2020 00:21:13 -0400 Subject: [PATCH 5/6] Code cleanup --- print_sa.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/print_sa.py b/print_sa.py index a01e393d4..3e2b081b6 100644 --- a/print_sa.py +++ b/print_sa.py @@ -7,7 +7,10 @@ from __future__ import print_function -import os, argparse, time, json, glob +import argparse +import glob +import json +import time stt = time.time() @@ -41,10 +44,10 @@ print('List generated') print('Deduping...') -saList1 = list(dict.fromkeys(saList)) +saList = list(dict.fromkeys(saList)) if (sa_project != ""): - print("Filtering email from %s." % sa_project) - saList1 = [n for n in saList1 if sa_project in n] + print("Filtering email from %s." % sa_project) + saList = [n for n in saList if sa_project in n] print('Complete.') hours, rem = divmod((time.time() - stt), 3600) @@ -52,7 +55,7 @@ print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec)) with open(sa_file, 'w') as f: - for i in range(len(saList1)): - if ((int(sp_num) <= len(saList1)+1) and (int(sp_num) != 0) and (i % int(sp_num) == 0)): - print("", file=f) - print(saList1[i], file=f) + for i in range(len(saList)): + if ((int(sp_num) <= len(saList) + 1) and (int(sp_num) != 0) and (i % int(sp_num) == 0)): + print("", file=f) + print(saList[i], file=f) From 589bfd23054a3034c82e1b8d35dc3dcbede8be3f Mon Sep 17 00:00:00 2001 From: Steven Zhu Date: Wed, 5 Aug 2020 15:53:17 -0400 Subject: [PATCH 6/6] Add color coding and show warning when duplicate found --- print_sa.py | 62 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/print_sa.py b/print_sa.py index 3e2b081b6..cf424bc85 100644 --- a/print_sa.py +++ b/print_sa.py @@ -10,8 +10,37 @@ import argparse import glob import json +import sys import time + +# Colored printing functions for strings that use universal ANSI escape sequences. +# fail: bold red, pass: bold green, warn: bold yellow, +# info: bold blue, bold: bold white + +class ColorPrint: + + @staticmethod + def print_fail(message, end='\n'): + sys.stderr.write('\x1b[1;31m' + message.strip() + '\x1b[0m' + end) + + @staticmethod + def print_pass(message, end='\n'): + sys.stdout.write('\x1b[1;32m' + message.strip() + '\x1b[0m' + end) + + @staticmethod + def print_warn(message, end='\n'): + sys.stderr.write('\x1b[1;33m' + message.strip() + '\x1b[0m' + end) + + @staticmethod + def print_info(message, end='\n'): + sys.stdout.write('\x1b[1;34m' + message.strip() + '\x1b[0m' + end) + + @staticmethod + def print_bold(message, end='\n'): + sys.stdout.write('\x1b[1;37m' + message.strip() + '\x1b[0m' + end) + + stt = time.time() parse = argparse.ArgumentParser( @@ -36,26 +65,41 @@ saList = [] -print('Generating List') +ColorPrint.print_info('Generating List') for i in sa: ce = json.loads(open(i, 'r').read())['client_email'] saList.append(ce) -print('List generated') -print('Deduping...') +initSASize = len(saList) +if (initSASize == 0): + raise ValueError('No Service Account json file found under %s directory' % acc_dir) +else: + ColorPrint.print_info('List generated') + +ColorPrint.print_info('Deduping...') saList = list(dict.fromkeys(saList)) +deduSASize = len(saList) + if (sa_project != ""): - print("Filtering email from %s." % sa_project) + ColorPrint.print_info("Filtering email from %s." % sa_project) saList = [n for n in saList if sa_project in n] -print('Complete.') - -hours, rem = divmod((time.time() - stt), 3600) -minutes, sec = divmod(rem, 60) -print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec)) + if (len(saList) == 0): + raise ValueError('No Service Account json file matches %s' % sa_project) +print("Writing SA to file.") with open(sa_file, 'w') as f: for i in range(len(saList)): if ((int(sp_num) <= len(saList) + 1) and (int(sp_num) != 0) and (i % int(sp_num) == 0)): print("", file=f) print(saList[i], file=f) + +ColorPrint.print_pass('Complete.\n %i SA write to file' % len(saList)) +ColorPrint.print_info('Your file is located at %s' % sa_file) + +hours, rem = divmod((time.time() - stt), 3600) +minutes, sec = divmod(rem, 60) +ColorPrint.print_info("Elapsed Time: {:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec)) + +if (initSASize != deduSASize): + ColorPrint.print_warn("Your SA directory contains %i duplicate." % (initSASize - deduSASize))