def install_defaults(opts): configurator = Configurator('canari.resources.templates:install_plume', '.', {'non_interactive': True, 'remember_answers': False}) configurator.variables['plume.venv'] = os.environ.get('VIRTUAL_ENV') if configurator.variables['plume.venv']: print 'Will use the virtual environment in %r to run Plume...' % configurator.variables['plume.venv'] configurator.variables['plume.enable_ssl'] = 'n' print 'Installing init script to /etc/init.d...' configurator.variables['plume.init'] = check_init_script(configurator, '', '/etc/init.d') print 'Creating Plume root directory at /var/plume...' configurator.variables['plume.dir'] = check_mkdir(configurator, '', '/var/plume') print 'The PID file will be at /var/run/plume.pid...' configurator.variables['plume.run_dir'] = '/var/run' print 'The log files will be at /var/log/plume.log...' configurator.variables['plume.log_dir'] = '/var/log' configurator.variables['plume.user'] = check_uid(configurator, '', 'nobody') configurator.variables['plume.group'] = check_gid(configurator, '', 'nobody') print 'The Plume server will under UID/GID=%s/%s...' % ( configurator.variables['plume.user'], configurator.variables['plume.group']) print 'TLS will be disabled by default...' configurator.variables['plume.certificate'] = '' configurator.variables['plume.private_key'] = '' configurator.ask_questions() configurator.render() finish(configurator)
def create_package(opts): package_name = opts.package capitalized_package_name = package_name.capitalize() variables = { "project.name": package_name, "entity.example_name": "My%sEntity" % capitalized_package_name, "entity.base_name": "%sEntity" % capitalized_package_name, "created.year": datetime.now().year, "canari.version": canari.__version__, } defaults = {"project.create_example": True, "author.name": getuser()} if not path.exists(package_name): print("creating skeleton in %s" % package_name) configurator = Configurator( "canari.resources.templates:create_package", package_name, {"non_interactive": False, "remember_answers": True}, variables=variables, defaults=defaults, ) configurator.ask_questions() configurator.render() else: print("A directory with the name %s already exists... exiting" % package_name) exit(-1) print("done!")
def generate_entities_doc(project, out_path, transform_package): if not out_path: if project.is_valid: out_path = project.root_dir else: out_path = os.getcwd() jinja2_env.filters['entity_properties'] = \ lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and not hasattr(Entity, p)]) def get_property_type(v): if isinstance(v, IntegerEntityField): return 'int' elif isinstance(v, FloatEntityField): return 'float' elif isinstance(v, BooleanEntityField): return 'bool' elif isinstance(v, TimeSpanEntityField): return 'timedelta' elif isinstance(v, DateTimeEntityField): return 'datetime' elif isinstance(v, DateEntityField): return 'date' elif isinstance(v, LongEntityField): return 'long' else: return 'str' jinja2_env.filters['get_property_type'] = get_property_type entity_module = 'canari.maltego.entities' if transform_package.name == 'canari' \ else '%s.transforms.common.entities' % transform_package.name variables = { 'transform.module': entity_module, 'transform.entities': transform_package.entities, 'transform.author': '%s <%s>' % (transform_package.author, transform_package.author_email) } configurator = Configurator( 'canari.resources.templates:generate_entities_doc', out_path, {'non_interactive': True}, variables=variables) configurator.ask_questions() click.echo('Creating entities.rst documentation for %r...' % transform_package.name, err=True) configurator.render() click.echo('done!', err=True)
def config_dir(self): if not os.path.lexists(self._config_dir): click.echo("Initializing Canari configuration: %s" % self._config_dir, err=True) configurator = Configurator( 'canari.resources.templates:init_canari', self._config_dir, {'non_interactive': True}) configurator.ask_questions() configurator.render() return self._config_dir
def install_wizard(opts): configurator = Configurator('canari.resources.templates:install_plume', '.', {'non_interactive': False, 'remember_answers': False}) configurator.ask_questions() if os.environ.get('VIRTUAL_ENV'): run_venv = parse_bool( "--> Canari has detected that you're running this install script from within a virtualenv.\n" "--> Would you like to run Plume from this virtualenv (%r) as well?" % os.environ['VIRTUAL_ENV'], True) configurator.variables['plume.venv'] = os.environ['VIRTUAL_ENV'] if run_venv else False configurator.render() finish(configurator)
def generate_entities_doc(args): args = parse_args(args) transform_package = TransformDistribution(args.package) jinja2_env.filters['entity_properties'] = \ lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and not hasattr(Entity, p)]) def get_property_type(v): if isinstance(v, IntegerEntityField): return 'int' elif isinstance(v, FloatEntityField): return 'float' elif isinstance(v, BooleanEntityField): return 'bool' elif isinstance(v, TimeSpanEntityField): return 'timedelta' elif isinstance(v, DateTimeEntityField): return 'datetime' elif isinstance(v, DateEntityField): return 'date' elif isinstance(v, LongEntityField): return 'long' else: return 'str' jinja2_env.filters['get_property_type'] = get_property_type entity_module = 'canari.maltego.entities' if transform_package.name == 'canari' \ else '%s.transforms.common.entities' % transform_package.name variables = { 'transform.module': entity_module, 'transform.entities': transform_package.entities, 'transform.author': '%s <%s>' % (transform_package.author, transform_package.author_email) } configurator = Configurator( 'canari.resources.templates:generate_entities_doc', args.out_path, {'non_interactive': True}, variables=variables ) configurator.ask_questions() print('Creating entities.rst documentation for %r...' % args.package) configurator.render() print('done!')
def install_wizard(): configurator = Configurator('canari.resources.templates:install_plume', '.', {'non_interactive': False, 'remember_answers': False}) configurator.ask_questions() if os.environ.get('VIRTUAL_ENV'): run_venv = click.prompt( "--> Canari has detected that you're running this install script from within a virtualenv.\n" "--> Would you like to run Plume from this virtualenv (%r) as well?" % os.environ['VIRTUAL_ENV'], default=True ) configurator.variables['plume.venv'] = os.environ['VIRTUAL_ENV'] if run_venv else False else: configurator.variables['plume.venv'] = None configurator.render() finish(configurator)
def create_transform(args): opts = parse_args(args) project = CanariProject() transform_module = (opts.transform if not opts.transform.endswith('.py') else opts.transform[:-3]) transform_name = ''.join( [i[0].upper() + i[1:] for i in transform_module.split('_')]) transform_module = transform_module.lower() if '.' in transform_module: print("Transform name (%r) cannot have a dot ('.')." % transform_name) exit(-1) elif not transform_module: print("You must specify a valid transform name.") exit(-1) target = project.root_dir transform_directory = project.transforms_dir if os.path.exists( os.path.join(transform_directory, '%s.py' % transform_module)): print('Transform %r already exists... quitting' % transform_module) exit(-1) variables = parse_config(os.path.join(target, '.mrbob.ini'))['variables'] variables.update({ 'transform.module': transform_module, 'transform.name': transform_name }) configurator = Configurator(u'canari.resources.templates:create_transform', target, {'non_interactive': True}, variables=variables) configurator.ask_questions() print('Creating transform %r...' % transform_module) configurator.render() print('done!')
def create_transform(args): opts = parse_args(args) project = CanariProject() transform_module = (opts.transform if not opts.transform.endswith('.py') else opts.transform[:-3]) transform_name = ''.join([i[0].upper()+i[1:] for i in transform_module.split('_')]) transform_module = transform_module.lower() if '.' in transform_module: print("Transform name (%r) cannot have a dot ('.')." % transform_name) exit(-1) elif not transform_module: print("You must specify a valid transform name.") exit(-1) target = project.root_dir transform_directory = project.transforms_dir if os.path.exists(os.path.join(transform_directory, '%s.py' % transform_module)): print('Transform %r already exists... quitting' % transform_module) exit(-1) variables = parse_config(os.path.join(target, '.mrbob.ini'))['variables'] variables.update({'transform.module': transform_module, 'transform.name': transform_name}) configurator = Configurator( 'canari.resources.templates:create_transform', target, {'non_interactive': True}, variables=variables ) configurator.ask_questions() print('Creating transform %r...' % transform_module) configurator.render() print('done!')
def install_defaults(opts): configurator = Configurator('canari.resources.templates:install_plume', '.', { 'non_interactive': True, 'remember_answers': False }) configurator.variables['plume.venv'] = os.environ.get('VIRTUAL_ENV') if configurator.variables['plume.venv']: print('Will use the virtual environment in %r to run Plume...' % configurator.variables['plume.venv'], file=sys.stderr) configurator.variables['plume.enable_ssl'] = 'n' print('Installing init script to /etc/init.d...', file=sys.stderr) configurator.variables['plume.init'] = check_init_script( configurator, '', '/etc/init.d') print('Creating Plume root directory at /var/plume...', file=sys.stderr) configurator.variables['plume.dir'] = check_mkdir(configurator, '', '/var/plume') print('The PID file will be at /var/run/plume.pid...', file=sys.stderr) configurator.variables['plume.run_dir'] = '/var/run' print('The log files will be at /var/log/plume.log...', file=sys.stderr) configurator.variables['plume.log_dir'] = '/var/log' configurator.variables['plume.user'] = check_uid(configurator, '', 'nobody') configurator.variables['plume.group'] = check_gid(configurator, '', 'nobody') print('The Plume server will under UID/GID=%s/%s...' % (configurator.variables['plume.user'], configurator.variables['plume.group']), file=sys.stderr) print('TLS will be disabled by default...', file=sys.stderr) configurator.variables['plume.certificate'] = '' configurator.variables['plume.private_key'] = '' configurator.ask_questions() configurator.render() finish(configurator)
def generate_entities(project, output_path, mtz_file, exclude_namespace, namespace, maltego_entities, append, entity): if not output_path: if project.is_valid: output_path = project.common_dir else: output_path = os.getcwd() entities_py = os.path.join(output_path, 'entities.py') if os.path.exists(entities_py) and not append: click.confirm( '{!r} already exists. Are you sure you want to overwrite it?'. format(entities_py), default=False, abort=True) if maltego_entities: namespace.extend(exclude_namespace) exclude_namespace = [] mtz = MtzDistribution(mtz_file) target = output_path variables = project.configuration['variables'] entity_definitions = {} matcher = re.compile('(.+)\.([^.]+)$') for entity_file in mtz.entities: entity = MaltegoEntity.parse(mtz.read_file(entity_file)) namespace, name = matcher.match(entity.id).groups() if namespace in exclude_namespace: continue elif not namespace or namespace in namespace: entity_definitions[(namespace, name)] = entity entity_classes = [] if append: module = project.entities_module for entity_class in dir(module): entity_class = getattr(module, entity_class) if isinstance(entity_class, type) and issubclass(entity_class, Entity) and entity_class is not Entity \ and (entity_class._namespace_, entity_class.__name__) not in entity_definitions: entity_classes.append(entity_class) def get_entity_field_class(v): if v == 'int': return 'IntegerEntityField' elif v == 'float': return 'FloatEntityField' elif v == 'boolean': return 'BooleanEntityField' elif v == 'timespan': return 'TimeSpanEntityField' elif v == 'datetime': return 'DateTimeEntityField' elif v == 'date': return 'DateEntityField' elif v == 'long': return 'LongEntityField' else: return 'StringEntityField' def get_property_name(v): v = v.replace('.', '_').replace('-', '_') return '%s_' % v if keyword.iskeyword(v) else v jinja2_env.filters['entity_properties'] = \ lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and not hasattr(Entity, p)]) jinja2_env.filters['get_entity_field_class'] = get_entity_field_class jinja2_env.filters['get_property_name'] = get_property_name variables.update( { 'entity.definitions': entity_definitions, 'entity.classes': entity_classes }, ) configurator = Configurator('canari.resources.templates:generate_entities', target, {'non_interactive': True}, variables=variables) configurator.ask_questions() click.echo('Generating entities for %r...' % variables['project.name'], err=True) configurator.render() click.echo('done!', err=True)
def generate_entities(args): opts = parse_args(args) mtz = MtzDistribution(opts.mtz_file) target = opts.out_path variables = opts.project.configuration['variables'] entity_definitions = {} matcher = re.compile('(.+)\.([^\.]+)$') for entity_file in mtz.entities: entity = MaltegoEntity.parse(mtz.read_file(entity_file)) namespace, name = matcher.match(entity.id).groups() if namespace in opts.exclude_namespace: continue elif not opts.namespace or namespace in opts.namespace: entity_definitions[(namespace, name)] = entity entity_classes = [] if opts.append: module = opts.project.entities_module for entity_class in dir(module): entity_class = getattr(module, entity_class) if isinstance(entity_class, type) and issubclass(entity_class, Entity) and entity_class is not Entity \ and (entity_class._namespace_, entity_class.__name__) not in entity_definitions: entity_classes.append(entity_class) def get_entity_field_class(v): if v == 'int': return 'IntegerEntityField' elif v == 'float': return 'FloatEntityField' elif v == 'boolean': return 'BooleanEntityField' elif v == 'timespan': return 'TimeSpanEntityField' elif v == 'datetime': return 'DateTimeEntityField' elif v == 'date': return 'DateEntityField' elif v == 'long': return 'LongEntityField' else: return 'StringEntityField' def get_property_name(v): v = v.replace('.', '_').replace('-', '_') return '%s_' % v if keyword.iskeyword(v) else v jinja2_env.filters['entity_properties'] = \ lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and not hasattr(Entity, p)]) jinja2_env.filters['get_entity_field_class'] = get_entity_field_class jinja2_env.filters['get_property_name'] = get_property_name variables.update({ 'entity.definitions': entity_definitions, 'entity.classes': entity_classes }) configurator = Configurator( 'canari.resources.templates:generate_entities', target, {'non_interactive': True}, variables=variables ) configurator.ask_questions() print('Generating entities for %r...' % variables['project.name']) configurator.render() print('done!')
def generate_entities(args): opts = parse_args(args) mtz = MtzDistribution(opts.mtz_file) target = opts.out_path variables = opts.project.configuration['variables'] entity_definitions = {} matcher = re.compile('(.+)\.([^.]+)$') for entity_file in mtz.entities: entity = MaltegoEntity.parse(mtz.read_file(entity_file)) namespace, name = matcher.match(entity.id).groups() if namespace in opts.exclude_namespace: continue elif not opts.namespace or namespace in opts.namespace: entity_definitions[(namespace, name)] = entity entity_classes = [] if opts.append: module = opts.project.entities_module for entity_class in dir(module): entity_class = getattr(module, entity_class) if isinstance(entity_class, type) and issubclass(entity_class, Entity) and entity_class is not Entity \ and (entity_class._namespace_, entity_class.__name__) not in entity_definitions: entity_classes.append(entity_class) def get_entity_field_class(v): if v == 'int': return 'IntegerEntityField' elif v == 'float': return 'FloatEntityField' elif v == 'boolean': return 'BooleanEntityField' elif v == 'timespan': return 'TimeSpanEntityField' elif v == 'datetime': return 'DateTimeEntityField' elif v == 'date': return 'DateEntityField' elif v == 'long': return 'LongEntityField' else: return 'StringEntityField' def get_property_name(v): v = v.replace('.', '_').replace('-', '_') return '%s_' % v if keyword.iskeyword(v) else v jinja2_env.filters['entity_properties'] = \ lambda v: reversed([(p, getattr(v, p)) for p in dir(v) if isinstance(getattr(v, p), StringEntityField) and not hasattr(Entity, p)]) jinja2_env.filters['get_entity_field_class'] = get_entity_field_class jinja2_env.filters['get_property_name'] = get_property_name variables.update( { 'entity.definitions': entity_definitions, 'entity.classes': entity_classes }, ) configurator = Configurator('canari.resources.templates:generate_entities', target, {'non_interactive': True}, variables=variables) configurator.ask_questions() print('Generating entities for %r...' % variables['project.name'], file=sys.stderr) configurator.render() print('done!', file=sys.stderr)