Exemplo n.º 1
0
def load_plume_package(transform_package, plume_dir, accept_defaults):

    with PushDir(plume_dir):
        if not os.path.exists('canari.conf'):
            click.echo('Plume does not appear to be installed in %s.' %
                       plume_dir,
                       err=True)
            click.echo("Please run 'canari install-plume' and try again.",
                       err=True)
            exit(-1)

        if transform_package.has_remote_transforms:
            try:
                transform_package.configure(plume_dir,
                                            remote=True,
                                            defaults=accept_defaults)
            except ImportError as e:
                click.echo(
                    'An error occurred while trying to import %r from %s: %s' %
                    (transform_package.name, plume_dir, e),
                    err=True)
                click.echo('Please make sure that %r is importable from %s' %
                           (transform_package.name, plume_dir),
                           err=True)
                exit(-1)
            click.echo('Please restart plume for changes to take effect.',
                       err=True)
            exit(0)

    click.echo(
        'Error: no remote transforms found. Please make sure that at least one transform has remote=True '
        'set before retrying.',
        err=True)
    exit(-1)
Exemplo n.º 2
0
def load_plume_package(opts):

    with PushDir(opts.plume_dir):
        if not os.path.exists('canari.conf'):
            print('Plume does not appear to be installed in %s.' % opts.plume_dir, file=sys.stderr)
            print("Please run 'canari install-plume' and try again.", file=sys.stderr)
            exit(-1)

        transform_package = None
        try:
            transform_package = TransformDistribution(opts.package)
        except ValueError as e:
            print('An error occurred', e, file=sys.stderr)
            exit(-1)

        if transform_package.has_remote_transforms:
            try:
                transform_package.configure(opts.plume_dir, remote=True, defaults=opts.accept_defaults)
            except ImportError as e:
                print('An error occurred while trying to import %r from %s: %s' % (
                    transform_package.name, opts.plume_dir, e
                ), file=sys.stderr)
                print('Please make sure that %r is importable from %s' % (transform_package.name, opts.plume_dir),
                      file=sys.stderr)
                exit(-1)
            print('Please restart plume for changes to take effect.', file=sys.stderr)
            exit(0)

    print('Error: no remote transforms found. '
          'Please make sure that at least one transform has remote=True set before retrying.', file=sys.stderr)
    exit(-1)
Exemplo n.º 3
0
def create_profile(args):

    set_canari_mode(CanariMode.Local)

    opts = parse_args(args)
    current_dir = os.getcwd()
    try:
        with PushDir(opts.working_dir or current_dir):
            transform_package = TransformDistribution(opts.package)
            transform_package.create_profile(opts.working_dir, current_dir)
    except ValueError as e:
        print(str(e), file=sys.stderr)
        exit(-1)
Exemplo n.º 4
0
def shell(opts):

    set_canari_mode(CanariMode.LocalShellDebug)

    if not opts.package.endswith('transforms'):
        opts.package = '%s.transforms' % opts.package

    try:
        transform_package = TransformDistribution(opts.package)
        with PushDir(opts.working_dir or transform_package.default_prefix):
            mtg_console = MtgConsole(transform_package.transforms, auto_sudo=opts.sudo)
            mtg_console.interact(highlight('Welcome to Canari %s.' % canari.__version__, 'green', True))
    except ValueError, e:
        print str(e)
        exit(-1)
Exemplo n.º 5
0
def unload_plume_package(opts):
    with PushDir(opts.plume_dir):
        if not os.path.exists('canari.conf'):
            print('Plume does not appear to be installed in %s.' %
                  opts.plume_dir)
            print("Please run 'canari install-plume' and try again.")
            exit(-1)
        try:
            TransformDistribution(opts.package).configure(opts.plume_dir,
                                                          load=False,
                                                          remote=True)
        except ImportError:
            pass

    exit(0)
Exemplo n.º 6
0
def unload_plume_package(package, plume_dir):
    with PushDir(plume_dir):
        if not os.path.exists('canari.conf'):
            click.echo('Plume does not appear to be installed in %s.' %
                       plume_dir,
                       err=True)
            click.echo("Please run 'canari install-plume' and try again.",
                       err=True)
            exit(-1)
        try:
            package.configure(plume_dir, load=False, remote=True)
        except ImportError:
            pass

    exit(0)
Exemplo n.º 7
0
    def project_tree(self, path):
        with PushDir(path):
            root = self._project_root()
            self._configuration = parse_config(os.path.join(
                root, '.mrbob.ini'))

            tree = dict(root=root,
                        src=os.path.join(root, 'src'),
                        pkg=os.path.join(root, 'src', self.name),
                        resources=os.path.join(root, 'src', self.name,
                                               'resources'),
                        transforms=os.path.join(root, 'src', self.name,
                                                'transforms'),
                        common=os.path.join(root, 'src', self.name,
                                            'transforms', 'common'))

            return tree
Exemplo n.º 8
0
def list_transforms(opts):

    try:
        with PushDir(opts.working_dir or CanariProject().src_dir):
            transform_package = TransformDistribution(opts.package)
            for transform_class in transform_package.transforms:
                transform = transform_class()
                print('`- %s: %s' % (highlight(transform.name, 'green', True), transform.description))
                print(highlight('  `- Maltego identifiers:', 'black', True))
                print('    `- %s applies to %s in set %s' % (
                    highlight(transform.name, 'red', False),
                    highlight(transform.input_type._type_, 'red', False),
                    highlight(transform.transform_set, 'red', False)
                ))
                print('')
    except ValueError, e:
        print(e)
        exit(-1)
Exemplo n.º 9
0
def load_config(config_file=None, recursive_load=True):
    if not config_file:
        config_file = os.path.join(os.getcwd(), 'canari.conf')
        if not os.path.lexists(config_file):
            config_file = os.path.join(os.path.expanduser('~'), '.canari',
                                       'canari.conf')

    with PushDir(os.path.dirname(config_file)):
        config_parser = CanariConfigParser()
        config_parser.read([global_config, config_file])
        if recursive_load:
            if is_remote_exec_mode(
            ) and OPTION_REMOTE_CONFIGS in config_parser:
                config_parser.read(config_parser[OPTION_REMOTE_CONFIGS])
            elif OPTION_LOCAL_CONFIGS in config_parser:
                config_parser.read(config_parser[OPTION_LOCAL_CONFIGS])

        return config_parser
Exemplo n.º 10
0
    def _update_config(self, canari_config, load=True, remote=False, **kwargs):

        with PushDir(os.path.dirname(canari_config)):

            config = CanariConfigParser()
            config.read(canari_config)

            configs_option = OPTION_REMOTE_CONFIGS if remote else OPTION_LOCAL_CONFIGS
            config_section = SECTION_REMOTE if remote else SECTION_LOCAL

            if configs_option not in config:
                if config_section not in config:
                    config.add_section(config_section)
                config[configs_option] = ''

            configs = config.get_as_list(configs_option)

            if load:
                for (k, v) in kwargs.get('additional_options', {}).items():
                    config['.'.join([config_section, k])] = v

                if self.config_file not in configs:
                    print('Updating %s...' % canari_config, file=sys.stderr)
                    configs.append(self.config_file)
                    config[configs_option] = configs

                if remote:
                    packages = config.get_as_list(OPTION_REMOTE_PACKAGES)
                    if self.name not in packages:
                        packages.append(self.name)
                        config[OPTION_REMOTE_PACKAGES] = packages
            else:
                if self.config_file in configs:
                    print('Updating %s...' % canari_config, file=sys.stderr)
                    configs.remove(self.config_file)
                    config[configs_option] = configs

                if remote:
                    packages = config.get_as_list(OPTION_REMOTE_PACKAGES)
                    if self.name in packages:
                        packages.remove(self.name)
                        config[OPTION_REMOTE_PACKAGES] = packages

            config.write(open(canari_config, mode='w'))
Exemplo n.º 11
0
    def _update_config(self, canari_config, load=True, remote=False):
        with PushDir(os.path.dirname(canari_config)):

            config = CanariConfigParser()
            config.read(canari_config)

            configs_option = OPTION_REMOTE_CONFIGS if remote else OPTION_LOCAL_CONFIGS
            config_section = SECTION_REMOTE if remote else SECTION_LOCAL

            if configs_option not in config:
                if config_section not in config:
                    config.add_section(config_section)
                config[configs_option] = ''

            configs = config.get_as_list(configs_option)

            if load:
                if self.config_file not in configs:
                    print ('Updating %s...' % canari_config)
                    configs.append(self.config_file)
                    config[configs_option] = configs

                if remote:
                    packages = config.get_as_list(OPTION_REMOTE_PACKAGES)
                    if self.name not in packages:
                        packages.append(self.name)
                        config[OPTION_REMOTE_PACKAGES] = packages
            else:
                if self.config_file in configs:
                    print ('Updating %s...' % canari_config)
                    configs.remove(self.config_file)
                    config[configs_option] = configs

                if remote:
                    packages = config.get_as_list(OPTION_REMOTE_PACKAGES)
                    if self.name in packages:
                        packages.remove(self.name)
                        config[OPTION_REMOTE_PACKAGES] = packages

            config.write(file(canari_config, mode='wb'))
Exemplo n.º 12
0
    def project_tree(self, path):
        with PushDir(path):
            root = self._project_root()

            tree = dict(root=root,
                        src=None,
                        pkg=None,
                        resources=None,
                        transforms=None)

            for base, dirs, files in os.walk(root):
                if base.endswith('src'):
                    tree['src'] = base
                elif 'resources' in dirs:
                    tree['pkg'] = base
                elif base.endswith('resources'):
                    tree['resources'] = base
                elif base.endswith('transforms'):
                    tree['transforms'] = base
                    tree['common'] = os.path.join(base, 'common')

            return tree
Exemplo n.º 13
0
def run_transform(transform, value, fields, params, project, config):
    with PushDir(project.src_dir):
        local_transform_runner(transform, value, fields, params, config,
                               message)
Exemplo n.º 14
0
def dockerize_package(project, os_, host):
    if sys.version_info[0] > 2:
        os_ += '-py3'

    click.echo('Dockerizing %s transform package...' % project.name, err=True)

    configurator = Configurator('canari.resources.templates:dockerize_package',
                                project.root_dir, {'non_interactive': True},
                                variables={
                                    'project.name': project.name,
                                    'canari.version': version
                                })

    click.echo('Creating Dockerfile for %s...' % project.name, err=True)
    configurator.render()
    click.echo('done!', err=True)

    if not find_executable('docker'):
        click.echo(
            "Could not find 'docker' in your system path. Please download and install Docker from "
            "http://docker.com and rerun this command again.",
            err=True)
        exit(-1)

    if not host:
        if os.name == 'nt':
            host = ['']
        elif os.path.exists('/var/run/docker.sock'):
            host = ['unix:///var/run/docker.sock']

    docker_hosts = [
        j for sublist in [('-H', i) for i in host] for j in sublist
    ]
    container = '%s/%s:%s' % (project.name, project.name, os_)

    if not host:
        if not find_executable('docker-machine'):
            click.echo(
                "Could not find 'docker-machine' in your system path. Please download and install Docker "
                "Machine from http://docker.com and rerun this command again or manually specify a Docker host "
                "using the '-H' parameter, instead.",
                err=True)
            exit(-1)

        click.echo('Attempting to discover available Docker machines.',
                   err=True)
        machines = get_output(
            run_command(['docker-machine', 'ls', '-q'],
                        stdout=subprocess.PIPE)).split('\n')
        machines.remove('')

        if not machines:
            click.echo('No machines found :(\nExiting...', err=True)
            exit(-1)

        machine = prompt_menu(
            'More than one Docker machine was detected. Which one would you like to use to'
            'build and run this container?', machines)

        click.echo('Setting up environment for Docker machine %s' %
                   machines[machine],
                   err=True)

        # Inject docker environment variables
        env = get_output(
            run_command(['docker-machine', 'env', machines[machine]],
                        stdout=subprocess.PIPE))
        os.environ.update(re.findall(r'export ([^=]+)="([^"]+)', env))

    with PushDir(project.root_dir):
        p = run_command(
            ['docker'] + docker_hosts +
            ['build', '-t', container, '-f',
             'Dockerfile-%s' % os_, '.'])
        p.communicate()
        if p.returncode:
            click.echo(
                'An error occurred while building the Docker container.',
                err=True)
            exit(-1)

    if click.confirm('Would you like to run this container now?',
                     default=False):
        port = click.prompt(
            'Which port would you like Plume to listen on externally?',
            default=8080,
            type=click.IntRange(8080, 65535))
        click.echo('Plume will be listening on http://%s:%s' % (re.findall(
            '://([^:]+)', os.environ.get('DOCKER_HOST',
                                         'http://0.0.0.0'))[0], port),
                   err=True)
        run_command(['docker'] + docker_hosts +
                    ['run', '-it', '-p',
                     '8080:%s' % port, container]).communicate()

    click.echo('done!', err=True)
Exemplo n.º 15
0
def debug_transform(opts):
    set_canari_mode(CanariMode.LocalDebug)
    with PushDir(CanariProject().src_dir):
        local_transform_runner(opts.transform, opts.value, opts.fields,
                               opts.params, load_config(), console_writer)
Exemplo n.º 16
0
def debug_transform(transform, value, fields, params, project, config):
    with PushDir(project.src_dir):
        local_transform_runner(transform, value, fields, params, config,
                               console_writer)
Exemplo n.º 17
0
def run_transform(opts):
    set_canari_mode(CanariMode.LocalDispatch)
    with PushDir(CanariProject().src_dir):
        local_transform_runner(opts.transform, opts.value, opts.fields,
                               opts.params, load_config(), message)
Exemplo n.º 18
0
class CanariContext(object):
    def __init__(self):
        self._config_dir = click.get_app_dir('canari', False, True)
        self._config_file = os.path.join(self.config_dir, 'canari.conf')
        self._config = None
        self._project = None
        self._working_dir = None

    @property
    def mode(self):
        return get_canari_mode()

    @mode.setter
    def mode(self, value):
        return set_canari_mode(value)

    @property
    def project(self):
        if not self._project:
            self._project = CanariProject()
        return self._project

    @property
    def debug(self):
        return is_debug_exec_mode()

    @debug.setter
    def debug(self, value):
        if value:
            click.echo('Debugging is enabled')
        set_debug_mode(value)

    @property
    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

    @property
    def config_file(self):
        if not self._config_file:
            self._config_file = os.path.join(self.config_dir, 'canari.conf')
        return self._config_file

    @property
    def config(self):
        if not self._config:
            click.echo("Loading Canari configuration file %r" %
                       self.config_file,
                       err=True)
            self._config = load_config(self.config_file)
        return self._config

    @property
    def working_dir(self):
        return self._working_dir.cwd

    @working_dir.setter
    def working_dir(self, path):
        self._working_dir = PushDir(path)
        self._working_dir.__enter__()
Exemplo n.º 19
0
 def working_dir(self, path):
     self._working_dir = PushDir(path)
     self._working_dir.__enter__()
Exemplo n.º 20
0
def dockerize_package(args):
    project = CanariProject()

    print('Dockerizing %s transform package...' % project.name)

    configurator = Configurator('canari.resources.templates:dockerize_package',
                                project.root_dir, {'non_interactive': True},
                                variables={
                                    'project.name': project.name,
                                    'canari.version': version
                                })

    print('Creating Dockerfile for %s...' % project.name)
    configurator.render()
    print('done!')

    if not find_executable('docker'):
        print """Could not find 'docker' in your system path. Please download and install Docker from http://docker.com
        and rerun this command again.
        """
        exit(-1)

    if not args.host and os.path.exists('/var/run/docker.sock'):
        args.host = ['unix:///var/run/docker.sock']

    docker_hosts = [
        j for sublist in [('-H', i) for i in args.host] for j in sublist
    ]
    container = '%s/%s:%s' % (project.name, project.name, args.os)

    if not args.host:
        if not find_executable('docker-machine'):
            print """Could not find 'docker-machine' in your system path. Please download and install Docker Machine from
            http://docker.com and rerun this command again or manually specify a Docker host using the '-H' parameter,
            instead.
            """
            exit(-1)

        print 'Attempting to discover available Docker machines.'
        machines = run_command(
            ['docker-machine', 'ls', '-q'],
            stdout=subprocess.PIPE).communicate()[0].split('\n')
        machines.remove('')

        machine = question.parse_int(
            'More than one Docker machine was detected. Which one would you like to use to'
            'build and run this container?',
            machines) if len(machines) != 1 else 0

        print 'Setting up environment for Docker machine %s' % machines[machine]

        # Inject docker environment variables
        env = run_command(['docker-machine', 'env', machines[machine]],
                          stdout=subprocess.PIPE).communicate()[0]
        os.environ.update(re.findall(r'export ([^=]+)="([^"]+)', env))

    with PushDir(project.root_dir):
        p = run_command(
            ['docker'] + docker_hosts +
            ['build', '-t', container, '-f',
             'Dockerfile-%s' % args.os, '.'])
        p.communicate()
        if p.returncode:
            print 'An error occurred while building the Docker container.'
            exit(-1)

    if question.parse_bool('Would you like to run this container now?'):
        port = question.parse_int_range(
            'Which port would you like Plume to listen on externally?', 0,
            65535, 8080)
        print 'Plume will be listening on http://%s:%s' % (re.findall(
            '://([^:]+)', os.environ.get('DOCKER_HOST',
                                         'http://0.0.0.0'))[0], port)
        run_command(['docker'] + docker_hosts +
                    ['run', '-it', '-p',
                     '8080:%s' % port, container]).communicate()

    print 'done!'
Exemplo n.º 21
0
def shell(transform_package, working_dir, sudo):
    with PushDir(working_dir or transform_package.default_prefix):
        mtg_console = MtgConsole(transform_package.transforms, auto_sudo=sudo)
        mtg_console.interact(
            click.style('Welcome to Canari %s.' % canari.__version__,
                        fg='yellow'))