Skip to content

Instantly share code, notes, and snippets.

@josh-wrale
Forked from fengler/LICENSE.txt
Created November 18, 2013 04:36
Show Gist options
  • Save josh-wrale/7522622 to your computer and use it in GitHub Desktop.
Save josh-wrale/7522622 to your computer and use it in GitHub Desktop.
sg-creator.py generates JSON to describe Security Group rules using a Security Group csv file.
This method simplifies collaboration in creating and documentation of Security Groups. It speeds up the tedious process of creating rules and decreases the potential for typographical errors since the data only has to be entered once.
To generate the .csv file, import the security-group-template.csv to a Google Docs spreadsheet and compile ingress and egress rules.
To fill in the template correctly, follow these rules:
- in the ingress or egress column, enter either ingress or egress
- in the CidrIp column, enter the source or destination Cidr block or enter "sg" if the rule references a Security Group
- enter an 'x' and no other characters to choose which protocol(s) the rule applies to
- leave blank if the rule does not apply to the protocol
From Google Docs, download as csv.
Run the sg-creator inputting (-i) the .csv file and outputting (-o) a filename of your choice. The resulting file will be the Ingress and Egress Properties of your Security Group resource. For example:
sg-creator.py -i sample.csv -o sample.template
Limitations:
sg-creator cannot check for dependencies between Security Groups so if they reference each other, you may be required to create a separate ingress or egress resource that modifies the base group.
sg-creator will create a rule that references itself since the version in this branch is unaware of the name of the resource to which it will be added.
The MIT License (MIT)
Copyright (c) [2013] [Fanya Engler]
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ingress or egress CidrIp FromPort ToPort TCP UDP ICMP SecurityGroup
ingress 10.0.0.0/24 22 22 x
ingress 10.0.10.0/24 137 138 x
egress 10.0.20.0/24 25 25 x
egress 10.0.30.0/24 389 389 x x
ingress 10.0.40.0/24 -1 -1 x
egress 10.0.40.0/24 -1 -1 x
ingress sg 443 443 x SampleSecGr1
egress sg 443 443 x x SampleSecGr2
#!/usr/bin/env python
#import modules
import argparse
import csv
import json
#parse the arguments
parser = argparse.ArgumentParser(description="generates JSON to describe Security Group rules from a Security Group csv")
parser.add_argument("-i", "--input", help="the csv file created from spreadsheet")
parser.add_argument("-o", "--output", help="name the template file for JSON output")
args = parser.parse_args()
sg_csvfile = args.input
outfile = open(args.output, 'w')
#create Ingress and Egress lists
ingress_list = []
egress_list = []
#open csv file and read each row as dictionary
sg_file = open(sg_csvfile, 'rb')
sg_reader = csv.DictReader(sg_file)
#write a new rule with a Cidr block reference
def new_rule(listname):
listname.append({'CidrIp':sg_dict['CidrIp'],
'FromPort':sg_dict['FromPort'],
"IpProtocol": proto,
'ToPort':sg_dict['ToPort']})
#write a new rule with Security Group reference
def new_sg_rule(listname, sg_ref):
if sg_dict['SecurityGroup']=='self':
print "**Additional resources necessary** You must use a base Security Group and add a resource for Ingress or Egress for a Security Group to reference itself."
else:
listname.append({sg_ref:sg_dict['SecurityGroup'],
'FromPort':sg_dict['FromPort'],
"IpProtocol": proto,
'ToPort':sg_dict['ToPort']})
#iterate through rows checking for protocols
for sg_dict in sg_reader:
if sg_dict['ingress or egress']=='ingress':
if sg_dict['TCP']=='x' and sg_dict['CidrIp']!='sg':
proto='tcp'
new_rule(ingress_list)
if sg_dict['UDP']=='x' and sg_dict['CidrIp']!='sg':
proto='udp'
new_rule(ingress_list)
if sg_dict['ICMP']=='x' and sg_dict['CidrIp']!='sg':
proto='-1'
new_rule(ingress_list)
if sg_dict['TCP']=='x' and sg_dict['CidrIp']=='sg':
proto='tcp'
new_sg_rule(ingress_list, 'SourceSecurityGroupId')
if sg_dict['UDP']=='x' and sg_dict['CidrIp']=='sg':
proto='udp'
new_sg_rule(ingress_list, 'SourceSecurityGroupId')
if sg_dict['ICMP']=='x' and sg_dict['CidrIp']=='sg':
proto='-1'
new_sg_rule(ingress_list, 'SourceSecurityGroupId')
if sg_dict['ingress or egress']=='egress':
if sg_dict['TCP']=='x' and sg_dict['CidrIp']!='sg':
proto='tcp'
new_rule(egress_list)
if sg_dict['UDP']=='x' and sg_dict['CidrIp']!='sg':
proto='udp'
new_rule(egress_list)
if sg_dict['ICMP']=='x' and sg_dict['CidrIp']!='sg':
proto='-1'
new_rule(egress_list)
if sg_dict['TCP']=='x' and sg_dict['CidrIp']=='sg':
proto='tcp'
new_sg_rule(egress_list, 'DestinationSecurityGroupId')
if sg_dict['UDP']=='x' and sg_dict['CidrIp']=='sg':
proto='udp'
new_sg_rule(egress_list, 'DestinationSecurityGroupId')
if sg_dict['ICMP']=='x' and sg_dict['CidrIp']=='sg':
proto='-1'
new_sg_rule(egress_list, 'DestinationSecurityGroupId')
#create a text file and write the Security Group rules to it
outfile.write(json.dumps({"SecurityGroupIngress":ingress_list, "SecurityGroupEgress":egress_list}))
outfile.close
print "done writing " + args.output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment