|
| 1 | +""" |
| 2 | +:mod:`libnmap.plugin.s3` -- S3 Backend Plugin |
| 3 | +=================================== |
| 4 | +
|
| 5 | +.. module:: libnmap.plugin.s3 |
| 6 | +:platform: Linux |
| 7 | +:synopsis: a plugin is representation of a S3 backend using boto |
| 8 | +.. moduleauthor:: Ronald Bister |
| 9 | +.. moduleauthor:: Mike Boutillier |
| 10 | +""" |
| 11 | + |
| 12 | +#!/usr/bin/env python |
| 13 | +import json |
| 14 | +from bson.objectid import ObjectId |
| 15 | +from boto.s3.connection import S3Connection, OrdinaryCallingFormat |
| 16 | +from boto.s3.key import Key |
| 17 | +from boto.s3.bucketlistresultset import bucket_lister |
| 18 | +from boto.exception import S3ResponseError |
| 19 | +from libnmap.reportjson import ReportEncoder |
| 20 | +from libnmap.parser import NmapParser |
| 21 | +from libnmap.plugins.backendplugin import NmapBackendPlugin |
| 22 | + |
| 23 | + |
| 24 | +class NmapS3Plugin(NmapBackendPlugin): |
| 25 | + """ |
| 26 | + This plugin save the reports on S3 and compatible. |
| 27 | + """ |
| 28 | + def __init__(self, **kwargs): |
| 29 | + """ |
| 30 | + - create the conn object |
| 31 | + - create the bucket (if it doesn't exist) |
| 32 | + - if not given, awsaccessKey_nmapreport |
| 33 | + - may raise exception (ie in case of conflict bucket name) |
| 34 | + - sample : |
| 35 | + To connect to walrus: |
| 36 | + from libnmap.plugins.backendpluginFactory import |
| 37 | + BackendPluginFactory |
| 38 | + walrusBackend = |
| 39 | + BackendPluginFactory.create( |
| 40 | + plugin_name='s3', |
| 41 | + host="walrus.ecc.eucalyptus.com", |
| 42 | + path="/services/Walrus",port=8773, |
| 43 | + is_secure=False, |
| 44 | + aws_access_key_id='UU72FLVJCAYRATLXI70YH', |
| 45 | + aws_secret_access_key= |
| 46 | + 'wFg7gP5YFHjVlxakw1g1uCC8UR2xVW5ax9ErZCut') |
| 47 | + To connect to S3: |
| 48 | + mybackend_S3 = |
| 49 | + BackendPluginFactory.create( |
| 50 | + plugin_name='s3', |
| 51 | + is_secure=True, |
| 52 | + aws_access_key_id='MYACCESSKEY', |
| 53 | + aws_secret_access_key='MYSECRET') |
| 54 | + """ |
| 55 | + NmapBackendPlugin.__init__(self) |
| 56 | + try: |
| 57 | + calling_format = OrdinaryCallingFormat() |
| 58 | + if 'bucket' not in kwargs: |
| 59 | + self.bucket_name = ''.join( |
| 60 | + [kwargs['aws_access_key_id'].lower(), |
| 61 | + "_nmapreport"]) |
| 62 | + else: |
| 63 | + self.bucket_name = kwargs['bucket'] |
| 64 | + del kwargs['bucket'] |
| 65 | + kwargs['calling_format'] = calling_format |
| 66 | + self.conn = S3Connection(**kwargs) |
| 67 | + self.bucket = self.conn.lookup(self.bucket_name) |
| 68 | + if self.bucket is None: |
| 69 | + self.bucket = self.conn.create_bucket(self.bucket_name) |
| 70 | + except: |
| 71 | + raise |
| 72 | + |
| 73 | + def insert(self, report): |
| 74 | + """ |
| 75 | + create a json string from an NmapReport instance |
| 76 | + and push it to S3 bucket. |
| 77 | +
|
| 78 | + :param NmapReport: obj to insert |
| 79 | + :rtype: string |
| 80 | + :return: str id |
| 81 | + :todo: Add tagging option |
| 82 | + """ |
| 83 | + try: |
| 84 | + oid = ObjectId() |
| 85 | + mykey = Key(self.bucket) |
| 86 | + mykey.key = str(oid) |
| 87 | + strjsonnmapreport = json.dumps(report, cls=ReportEncoder) |
| 88 | + mykey.set_contents_from_string(strjsonnmapreport) |
| 89 | + except: |
| 90 | + print "Bucket cannot insert" |
| 91 | + raise |
| 92 | + return str(oid) |
| 93 | + |
| 94 | + def get(self, str_report_id=None): |
| 95 | + """ |
| 96 | + select a NmapReport by Id. |
| 97 | +
|
| 98 | + :param str: id |
| 99 | + :rtype: NmapReport |
| 100 | + :return: NmapReport object |
| 101 | + """ |
| 102 | + nmapreport = None |
| 103 | + if str_report_id is not None and isinstance(str_report_id, str): |
| 104 | + try: |
| 105 | + mykey = Key(self.bucket) |
| 106 | + mykey.key = str_report_id |
| 107 | + nmapreportjson = json.loads(mykey.get_contents_as_string()) |
| 108 | + nmapreport = NmapParser.parse_fromdict(nmapreportjson) |
| 109 | + except S3ResponseError: |
| 110 | + print "Not Found" |
| 111 | + return nmapreport |
| 112 | + |
| 113 | + def getall(self, dict_filter=None): |
| 114 | + """ |
| 115 | + :rtype: List of tuple |
| 116 | + :return: list of key/report |
| 117 | + :todo: add a filter capability |
| 118 | + """ |
| 119 | + nmapreportlist = [] |
| 120 | + for key in bucket_lister(self.bucket): |
| 121 | + if isinstance(key, Key): |
| 122 | + nmapreportjson = json.loads(key.get_contents_as_string()) |
| 123 | + nmapreport = NmapParser.parse_fromdict(nmapreportjson) |
| 124 | + nmapreportlist.append((key.key, nmapreport)) |
| 125 | + return nmapreportlist |
| 126 | + |
| 127 | + def delete(self, report_id=None): |
| 128 | + """ |
| 129 | + delete an obj from the backend |
| 130 | +
|
| 131 | + :param str: id |
| 132 | + :return: dict document with result or None |
| 133 | + """ |
| 134 | + rcode = None |
| 135 | + if report_id is not None and isinstance(report_id, str): |
| 136 | + rcode = self.bucket.delete_key(report_id) |
| 137 | + return rcode |
0 commit comments