Ejemplo n.º 1
0
def test_args_config_file():
    cli = Cli()
    args = ["--config", "config_file.yml"]
    arguments = cli.parse_args(args)
    assert arguments.config == "config_file.yml"

    args = ["-c", "config_file.yml"]
    arguments = cli.parse_args(args)
    assert arguments.config == "config_file.yml"
Ejemplo n.º 2
0
def test_args_server():
    cli = Cli()
    args = ["--server", "app.example.com"]
    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"

    args = ["-s", "app.example.com"]
    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"
Ejemplo n.º 3
0
def test_args_server():
    cli = Cli()
    args = ["--server", "app.example.com"]
    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"

    args = ["-s", "app.example.com"]
    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"
Ejemplo n.º 4
0
def test_args_config_file():
    cli = Cli()
    args = ["--config", "config_file.yml"]
    arguments = cli.parse_args(args)
    assert arguments.config == "config_file.yml"

    args = ["-c", "config_file.yml"]
    arguments = cli.parse_args(args)
    assert arguments.config == "config_file.yml"
Ejemplo n.º 5
0
def test_jump_host_args():
    cli = Cli()
    args = ["--server", "172.16.180.10", "--port", "22", "--username", "vagrant",
            "--password", "vagrant", "--jump-server", "172.16.180.20",
            "--jump-username", "vagrant", "--jump-password", "vagrant"]
    arguments = cli.parse_args(args)
    cli.configure_args(arguments)

    args = ["--server", "172.16.180.10", "--port", "22", "--username", "vagrant",
            "--password", "vagrant", "--jump-server", "172.16.180.20",
            "--jump-username", "vagrant", "--jump-password", "vagrant",
            "--jump-port", "22"]
    arguments = cli.parse_args(args)
    cli.configure_args(arguments)
Ejemplo n.º 6
0
def test_configure_args():
    cli = Cli()
    args = ["-c", "tests/files/validate_passing.yml"]
    arguments = cli.parse_args(args)
    cli.configure_args(arguments)

    args = ["-c", "tests/files/yml_failing.yml"]
    arguments = cli.parse_args(args)
    with pytest.raises(YAMLError):
        cli.configure_args(arguments)

    args = ["-c", "tests/files/validate_failing_aws.yml"]
    arguments = cli.parse_args(args)
    with pytest.raises(InvalidConfigurationError):
        cli.configure_args(arguments)
Ejemplo n.º 7
0
def test_configure_args():
    cli = Cli()
    args = ["-c", "tests/files/validate_passing.yml"]
    arguments = cli.parse_args(args)
    cli.configure_args(arguments)

    args = ["-c", "tests/files/yml_failing.yml"]
    arguments = cli.parse_args(args)
    with pytest.raises(YAMLError):
        cli.configure_args(arguments)

    args = ["-c", "tests/files/validate_failing_aws.yml"]
    arguments = cli.parse_args(args)
    with pytest.raises(InvalidConfigurationError):
        cli.configure_args(arguments)
Ejemplo n.º 8
0
def test_jump_host_args():
    cli = Cli()
    args = [
        "--server", "172.16.180.10", "--port", "22", "--username", "vagrant",
        "--password", "vagrant", "--jump-server", "172.16.180.20",
        "--jump-username", "vagrant", "--jump-password", "vagrant"
    ]
    arguments = cli.parse_args(args)
    cli.configure_args(arguments)

    args = [
        "--server", "172.16.180.10", "--port", "22", "--username", "vagrant",
        "--password", "vagrant", "--jump-server", "172.16.180.20",
        "--jump-username", "vagrant", "--jump-password", "vagrant",
        "--jump-port", "22"
    ]
    arguments = cli.parse_args(args)
    cli.configure_args(arguments)
Ejemplo n.º 9
0
def test_configure_args():
    cli = Cli()
    args = ["-c", "tests/files/validate_passing.yml"]
    arguments = cli.parse_args(args)

    cli.configure_args(arguments)
    args = ["--server", "tests/files/validate_passing.yml", "--repository-url",
            "https://www.example.com/repo"]
    arguments = cli.parse_args(args)
    cli.configure_args(arguments)

    args = ["-c", "tests/files/yml_failing.yml"]
    arguments = cli.parse_args(args)
    with pytest.raises(YAMLError):
        cli.configure_args(arguments)

    args = ["-c", "tests/files/validate_failing_aws.yml"]
    arguments = cli.parse_args(args)
    with pytest.raises(InvalidConfigurationError):
        cli.configure_args(arguments)
Ejemplo n.º 10
0
def test_args_optional():
    cli = Cli()
    args = [
        "-s", "app.example.com", "--port", '2222', "--username", "ec2-user",
        "--module", "lime.ko", "--password", "hunter2", "--key", "rsa.key",
        "--filename", "mem.lime", "--workers", "auto", "--bucket", "marsho",
        "--log_dir", "logs", "--log_prefix", "case_num"
    ]
    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"
    assert arguments.port == "2222"
    assert arguments.username == "ec2-user"
    assert arguments.module == "lime.ko"
    assert arguments.password == "hunter2"
    assert arguments.key == "rsa.key"
    assert arguments.filename == "mem.lime"
    assert arguments.workers == "auto"
    assert arguments.bucket == "marsho"
    assert arguments.log_dir == "logs"
    assert arguments.log_prefix == "case_num"

    args = [
        "-s", "app.example.com", "-P", '2222', "-u", "ec2-user", "-m",
        "lime.ko", "-p", "hunter2", "-k", "rsa.key", "-f", "mem.lime", "-w",
        "2", "-o", "dump", "-d", "logs", "--log_prefix", "case_num"
    ]

    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"
    assert arguments.port == "2222"
    assert arguments.username == "ec2-user"
    assert arguments.module == "lime.ko"
    assert arguments.password == "hunter2"
    assert arguments.key == "rsa.key"
    assert arguments.filename == "mem.lime"
    assert arguments.workers == "2"
    assert arguments.output_dir == "dump"
    assert arguments.log_dir == "logs"
    assert arguments.log_prefix == "case_num"
Ejemplo n.º 11
0
def test_configure_args():
    cli = Cli()
    args = ["-c", "tests/files/validate_passing.yml"]
    arguments = cli.parse_args(args)

    cli.configure_args(arguments)
    args = [
        "--server", "tests/files/validate_passing.yml", "--repository-url",
        "https://www.example.com/repo"
    ]
    arguments = cli.parse_args(args)
    cli.configure_args(arguments)

    args = ["-c", "tests/files/yml_failing.yml"]
    arguments = cli.parse_args(args)
    with pytest.raises(YAMLError):
        cli.configure_args(arguments)

    args = ["-c", "tests/files/validate_failing_aws.yml"]
    arguments = cli.parse_args(args)
    with pytest.raises(InvalidConfigurationError):
        cli.configure_args(arguments)
Ejemplo n.º 12
0
def test_args_optional():
    cli = Cli()
    args = ["-s", "app.example.com", "--port", '2222', "--username", "ec2-user",
            "--module", "lime.ko", "--password", "hunter2", "--key", "rsa.key",
            "--filename", "mem.lime", "--workers", "auto", "--bucket", "marsho",
            "--log_dir", "logs", "--log_prefix", "case_num"]
    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"
    assert arguments.port == "2222"
    assert arguments.username == "ec2-user"
    assert arguments.module == "lime.ko"
    assert arguments.password == "hunter2"
    assert arguments.key == "rsa.key"
    assert arguments.filename == "mem.lime"
    assert arguments.workers == "auto"
    assert arguments.bucket == "marsho"
    assert arguments.log_dir == "logs"
    assert arguments.log_prefix == "case_num"

    args = ["-s", "app.example.com", "-P", '2222', "-u", "ec2-user",
            "-m", "lime.ko", "-p", "hunter2", "-k", "rsa.key", "-f",
            "mem.lime", "-w", "2", "-o", "dump", "-d", "logs",
            "--log_prefix", "case_num"]

    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"
    assert arguments.port == "2222"
    assert arguments.username == "ec2-user"
    assert arguments.module == "lime.ko"
    assert arguments.password == "hunter2"
    assert arguments.key == "rsa.key"
    assert arguments.filename == "mem.lime"
    assert arguments.workers == "2"
    assert arguments.output_dir == "dump"
    assert arguments.log_dir == "logs"
    assert arguments.log_prefix == "case_num"
Ejemplo n.º 13
0
def test_args_optional():
    cli = Cli()
    args = ["--server", "app.example.com", "--port", '2222', "--username", "ec2-user",
            "--module", "lime.ko", "--password", "hunter2", "--key", "rsa.key",
            "--filename", "mem.lime", "--workers", "auto", "--bucket", "marsho",
            "--log-dir", "logs", "--log-prefix", "case_num", "--repository-url",
            "https://www.example.com/repo"]
    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"
    assert arguments.port == "2222"
    assert arguments.username == "ec2-user"
    assert arguments.module == "lime.ko"
    assert arguments.password == "hunter2"
    assert arguments.key == "rsa.key"
    assert arguments.filename == "mem.lime"
    assert arguments.workers == "auto"
    assert arguments.bucket == "marsho"
    assert arguments.log_dir == "logs"
    assert arguments.log_prefix == "case_num"
    assert arguments.repository_url == "https://www.example.com/repo"
Ejemplo n.º 14
0
def test_args_optional():
    cli = Cli()
    args = [
        "--server", "app.example.com", "--port", '2222', "--username",
        "ec2-user", "--module", "lime.ko", "--password", "hunter2", "--key",
        "rsa.key", "--filename", "mem.lime", "--workers", "auto", "--bucket",
        "marsho", "--log-dir", "logs", "--log-prefix", "case_num",
        "--repository-url", "https://www.example.com/repo"
    ]
    arguments = cli.parse_args(args)
    assert arguments.server == "app.example.com"
    assert arguments.port == "2222"
    assert arguments.username == "ec2-user"
    assert arguments.module == "lime.ko"
    assert arguments.password == "hunter2"
    assert arguments.key == "rsa.key"
    assert arguments.filename == "mem.lime"
    assert arguments.workers == "auto"
    assert arguments.bucket == "marsho"
    assert arguments.log_dir == "logs"
    assert arguments.log_prefix == "case_num"
    assert arguments.repository_url == "https://www.example.com/repo"
Ejemplo n.º 15
0
class Client():
    """
    Client for parallel memory capture with LiME
    """

    def __init__(self, config=None, library=True, name=None, verbose=False):
        """
        :type library: bool
        :param library: Toggle for command line features
        :type config: dict
        :param config: Client configuration
        """

        self.name = name
        self.verbose = verbose
        self.cli = Cli()
        self.library = library
        if self.library is False:
            args = self.cli.parse_args(sys.argv[1:])
            self.config = self.cli.configure(arguments=args)
            if args.verbose is True:
                self.verbose = True
        else:
            if config is None:
                raise NoConfigurationError
            self.config = self.cli.configure(config=config)

        if self.verbose is True:
            margaritashotgun.set_stream_logger(name=self.name, level=logging.DEBUG)
        else:
            margaritashotgun.set_stream_logger(name=self.name, level=logging.INFO)



    def run(self):
        """
        Captures remote hosts memory
        """
        logger = logging.getLogger(__name__)
        try:
            conf = self.map_config()
            workers = Workers(conf, self.config['workers'], name=self.name, library=self.library)
            description = 'memory capture action'
            results = workers.spawn(description)

            self.statistics(results)
            if self.library is True:
                return dict([('total', self.total),
                             ('completed', self.completed_addresses),
                             ('failed', self.failed_addresses)])
            else:
                logger.info(("{0} hosts processed. completed: {1} "
                             "failed {2}".format(self.total, self.completed,
                                                 self.failed)))
                logger.info("completed_hosts: {0}".format(self.completed_addresses))
                logger.info("failed_hosts: {0}".format(self.failed_addresses))
                quit()
        except KeyboardInterrupt:
            workers.cleanup(terminate=True)
            if self.library:
                raise
            else:
                quit(1)

    def map_config(self):
        config_list = []
        keys = ['aws', 'host', 'logging', 'repository']
        for host in self.config['hosts']:
            values = [self.config['aws'], host, self.config['logging'],
                      self.config['repository']]
            conf = dict(zip(keys, values))
            config_list.append(conf)
        return config_list

    def statistics(self, results):
        self.total = len(results)
        self.completed = 0
        self.completed_addresses = []
        self.failed = 0
        self.failed_addresses = []

        for result in results:
            if result[1] is False:
                self.failed += 1
                self.failed_addresses.append(result[0])
            else:
                self.completed += 1
                self.completed_addresses.append(result[0])
Ejemplo n.º 16
0
def test_environment_var_override():
    cli = Cli()
    #TODO: set environment variable here
    os.environ['LIME_REPOSITORY'] = 'enabled'
    arguments = cli.parse_args(['-c', 'tests/files/validate_passing.yml'])
    cli.configure_args(arguments)
Ejemplo n.º 17
0
class Client():
    """
    Client for parallel memory capture with LiME
    """
    def __init__(self, config=None, library=True, name=None, verbose=False):
        """
        :type library: bool
        :param library: Toggle for command line features
        :type config: dict
        :param config: Client configuration
        """

        self.name = name
        self.verbose = verbose
        self.cli = Cli()
        self.library = library
        if self.library is False:
            args = self.cli.parse_args(sys.argv[1:])
            try:
                self.config = self.cli.configure(arguments=args)
            except Exception as ex:
                print("Error parsing config file: {0}".format(ex))
                quit(1)
            if args.verbose is True:
                self.verbose = True
        else:
            if config is None:
                raise NoConfigurationError
            self.config = self.cli.configure(config=config)

        if self.verbose is True:
            margaritashotgun.set_stream_logger(name=self.name,
                                               level=logging.DEBUG)
        else:
            margaritashotgun.set_stream_logger(name=self.name,
                                               level=logging.INFO)

    def run(self):
        """
        Captures remote hosts memory
        """
        logger = logging.getLogger(__name__)
        try:
            # Check repository GPG settings before starting workers
            # Handling this here prevents subprocesses from needing stdin access
            repo_conf = self.config['repository']
            repo = None
            if repo_conf['enabled'] and repo_conf['gpg_verify']:
                try:
                    repo = Repository(repo_conf['url'],
                                      repo_conf['gpg_verify'])
                    repo.init_gpg()
                except Exception as ex:
                    # Do not prompt to install gpg keys unless running interactively
                    if repo is not None and self.library is False:
                        if isinstance(ex, RepositoryUntrustedSigningKeyError):
                            installed = repo.prompt_for_install()
                            if installed is False:
                                logger.critical(("repository signature not "
                                                 "installed, install the "
                                                 "signature manually or use "
                                                 "the --gpg-no-verify flag "
                                                 "to bypass this check"))
                                quit(1)
                    else:
                        logger.critical(ex)
                        quit(1)

            conf = self.map_config()
            workers = Workers(conf,
                              self.config['workers'],
                              name=self.name,
                              library=self.library)
            description = 'memory capture action'
            results = workers.spawn(description)

            self.statistics(results)
            if self.library is True:
                return dict([('total', self.total),
                             ('completed', self.completed_addresses),
                             ('failed', self.failed_addresses)])
            else:
                logger.info(("{0} hosts processed. completed: {1} "
                             "failed {2}".format(self.total, self.completed,
                                                 self.failed)))
                logger.info("completed_hosts: {0}".format(
                    self.completed_addresses))
                logger.info("failed_hosts: {0}".format(self.failed_addresses))
                quit()
        except KeyboardInterrupt:
            workers.cleanup(terminate=True)
            if self.library:
                raise
            else:
                quit(1)

    def map_config(self):
        config_list = []
        keys = ['aws', 'host', 'logging', 'repository']
        for host in self.config['hosts']:
            values = [
                self.config['aws'], host, self.config['logging'],
                self.config['repository']
            ]
            conf = dict(zip(keys, values))
            config_list.append(conf)
        return config_list

    def statistics(self, results):
        self.total = len(results)
        self.completed = 0
        self.completed_addresses = []
        self.failed = 0
        self.failed_addresses = []

        for result in results:
            if result[1] is False:
                self.failed += 1
                self.failed_addresses.append(result[0])
            else:
                self.completed += 1
                self.completed_addresses.append(result[0])
Ejemplo n.º 18
0
class Client():
    """
    Client for parallel memory capture with LiME
    """

    def __init__(self, config=None, library=True, name=None, verbose=False):
        """
        :type library: bool
        :param library: Toggle for command line features
        :type config: dict
        :param config: Client configuration
        """

        self.name = name
        self.verbose = verbose
        self.cli = Cli()
        self.library = library
        if self.library is False:
            args = self.cli.parse_args(sys.argv[1:])
            try:
                self.config = self.cli.configure(arguments=args)
            except Exception as ex:
                print("Error parsing config file: {0}".format(ex))
                quit(1)
            if args.verbose is True:
                self.verbose = True
        else:
            if config is None:
                raise NoConfigurationError
            self.config = self.cli.configure(config=config)

        if self.verbose is True:
            margaritashotgun.set_stream_logger(name=self.name, level=logging.DEBUG)
        else:
            margaritashotgun.set_stream_logger(name=self.name, level=logging.INFO)



    def run(self):
        """
        Captures remote hosts memory
        """
        logger = logging.getLogger(__name__)
        try:
            # Check repository GPG settings before starting workers
            # Handling this here prevents subprocesses from needing stdin access
            repo_conf = self.config['repository']
            repo = None
            if repo_conf['enabled'] and repo_conf['gpg_verify']:
                try:
                    repo = Repository(repo_conf['url'],
                                      repo_conf['gpg_verify'])
                    repo.init_gpg()
                except Exception as ex:
                    # Do not prompt to install gpg keys unless running interactively
                    if repo is not None and self.library is False:
                        if isinstance(ex, RepositoryUntrustedSigningKeyError):
                            installed = repo.prompt_for_install()
                            if installed is False:
                                logger.critical(("repository signature not "
                                                 "installed, install the "
                                                 "signature manually or use "
                                                 "the --gpg-no-verify flag "
                                                 "to bypass this check"))
                                quit(1)
                    else:
                        logger.critical(ex)
                        quit(1)

            conf = self.map_config()
            workers = Workers(conf, self.config['workers'], name=self.name, library=self.library)
            description = 'memory capture action'
            results = workers.spawn(description)

            self.statistics(results)
            if self.library is True:
                return dict([('total', self.total),
                             ('completed', self.completed_addresses),
                             ('failed', self.failed_addresses)])
            else:
                logger.info(("{0} hosts processed. completed: {1} "
                             "failed {2}".format(self.total, self.completed,
                                                 self.failed)))
                logger.info("completed_hosts: {0}".format(self.completed_addresses))
                logger.info("failed_hosts: {0}".format(self.failed_addresses))
                quit()
        except KeyboardInterrupt:
            workers.cleanup(terminate=True)
            if self.library:
                raise
            else:
                quit(1)

    def map_config(self):
        config_list = []
        keys = ['aws', 'host', 'logging', 'repository']
        for host in self.config['hosts']:
            values = [self.config['aws'], host, self.config['logging'],
                      self.config['repository']]
            conf = dict(zip(keys, values))
            config_list.append(conf)
        return config_list

    def statistics(self, results):
        self.total = len(results)
        self.completed = 0
        self.completed_addresses = []
        self.failed = 0
        self.failed_addresses = []

        for result in results:
            if result[1] is False:
                self.failed += 1
                self.failed_addresses.append(result[0])
            else:
                self.completed += 1
                self.completed_addresses.append(result[0])
Ejemplo n.º 19
0
def test_environment_var_override():
    cli = Cli()
    #TODO: set environment variable here
    os.environ['LIME_REPOSITORY'] = 'enabled'
    arguments = cli.parse_args(['-c', 'tests/files/validate_passing.yml'])
    cli.configure_args(arguments)