#! /usr/bin/python # # Find a security group amongst all profile configured. Note that it makes multiple calls # to the FindSecurityGroupInProfile.py in parallel. # # Unbuffered, no CRLF print: from __future__ import print_function import re, sys, os, subprocess import boto3 import boto3.session import threading # We may not do it yet, but developing with threadsafe in mind, as best I can try: # Python 3 import builtins except ImportError: # Python 2 import __builtin__ as builtins # Debug levels: # 1 = Show progress # 2 = Informational # 5 = Include boto3 logging DEBUG=0 if len(sys.argv) != 2: print("Usage: " + os.path.basename(sys.argv[0]) + " ") exit(1) SEARCHSTRING=sys.argv[1] # Load profiles try: cfile = open(os.path.expanduser("~") + "/.aws/credentials", "r") except: print("You must have a ~/.aws/credentials file with profiles configured.") exit(3) profiles = set() for line in cfile: profile = re.match('^\[(.+)\]', line) if(profile): profiles.add(profile.group(1)) # End of for line FOUND=0 processes = set() for profile in profiles: if DEBUG >= 2: print("Searching profile " + profile) process = subprocess.Popen(['FindSecurityGroupInProfile.py', SEARCHSTRING, profile]) if DEBUG >= 2: print("PID = " + str(process.pid)) processes.add(process) # We should have spawned all child processes. Let's wait for them. for process in processes: if DEBUG >= 2: print("Waiting on process " + str(process.pid)) returncode = process.wait() if(returncode == 0): # Found one FOUND = FOUND + 1 # Searched all the profiles if(FOUND > 0): exit(0) exit(255)