Exemple #1
0
def test_exec_session_exception_invalid_profile(mocker, instance_id, config):
    mocker.patch("aws_gate.exec.get_aws_client")
    mocker.patch("aws_gate.exec.get_aws_resource")
    mocker.patch("aws_gate.exec.query_instance", return_value=None)
    mocker.patch("aws_gate.decorators._plugin_exists", return_value=True)
    mocker.patch("aws_gate.decorators.execute_plugin", return_value="1.1.23.0")

    with pytest.raises(ValueError):
        exec(
            config=config,
            profile_name="invalid-profile",
            instance_name=instance_id,
            command=["ls", "-l"],
        )
Exemple #2
0
def test_exec_session_without_config(mocker, instance_id, empty_config):
    mocker.patch("aws_gate.exec.get_aws_client")
    mocker.patch("aws_gate.exec.get_aws_resource")
    mocker.patch("aws_gate.exec.query_instance", return_value=None)
    mocker.patch("aws_gate.decorators._plugin_exists", return_value=True)
    mocker.patch("aws_gate.decorators.execute_plugin", return_value="1.1.23.0")

    with pytest.raises(ValueError):
        exec(
            config=empty_config,
            instance_name=instance_id,
            command=["ls", "-l"],
            profile_name="profile",
            region_name="eu-west-1",
        )
Exemple #3
0
def test_exec_session(mocker, instance_id, config):
    mocker.patch("aws_gate.exec.get_aws_client")
    mocker.patch("aws_gate.exec.get_aws_resource")
    mocker.patch("aws_gate.exec.query_instance", return_value=instance_id)
    session_mock = mocker.patch("aws_gate.exec.ExecSession",
                                return_value=mocker.MagicMock())
    mocker.patch("aws_gate.decorators._plugin_exists", return_value=True)
    mocker.patch("aws_gate.decorators.execute_plugin", return_value="1.1.23.0")
    mocker.patch("aws_gate.decorators.is_existing_profile", return_value=True)

    exec(
        config=config,
        instance_name=instance_id,
        command={"ls", "-l"},
        profile_name="profile",
        region_name="eu-west-1",
    )
    assert session_mock.called
Exemple #4
0
def main(args=None, argument_parser=None):
    if not args:
        args = parse_arguments(argument_parser)

    if not DEBUG:
        sys.excepthook = lambda exc_type, exc_value, traceback: logger.error(exc_value)

    log_level = logging.ERROR
    log_format = "%(message)s"

    # We want to silence dependencies
    logging.getLogger("botocore").setLevel(logging.CRITICAL)
    logging.getLogger("boto3").setLevel(logging.CRITICAL)
    logging.getLogger("urllib3").setLevel(logging.CRITICAL)

    if args.verbose:
        log_level = logging.INFO

    if DEBUG:
        log_level = logging.DEBUG
        log_format = "%(asctime)s - %(name)-28s - %(levelname)-5s - %(message)s"

    logging.basicConfig(level=log_level, stream=sys.stderr, format=log_format)

    try:
        config = load_config_from_files()
    except (ValidationError, ScannerError) as e:
        raise ValueError("Invalid configuration provided: {}".format(e))

    # We want to provide default values in cases they are not configured
    # in ~/.aws/config or availabe a environment variables
    default_region = get_default_region()
    if default_region is None:
        default_region = AWS_DEFAULT_REGION

    # We try to obtain default profile from the environment or use 'default' to
    # save a call to boto3. In the environment, we check if we are being called
    # from aws-vault first or not. Then we return 'default' as boto3 will
    # https://github.com/boto/boto3/blob/develop/boto3/session.py#L93
    if "AWS_VAULT" in os.environ:
        logger.debug(
            "aws-vault usage detected, defaulting to the AWS profile from $AWS_VAULT"
        )

    default_profile = (
        os.environ.get("AWS_VAULT")
        or os.environ.get("AWS_PROFILE")
        or AWS_DEFAULT_PROFILE
    )

    profile = _get_profile(args=args, config=config, default=default_profile)
    region = _get_region(args=args, config=config, default=default_region)

    logger.debug('Using AWS profile "%s" in region "%s"', profile, region)

    if args.subcommand == "bootstrap":
        bootstrap(force=args.force)
    elif args.subcommand == "exec":
        exec(
            config=config,
            instance_name=args.instance_name,
            command=args.command,
            region_name=region,
            profile_name=profile,
        )
    elif args.subcommand == "session":
        session(
            config=config,
            instance_name=args.instance_name,
            region_name=region,
            profile_name=profile,
        )
    elif args.subcommand == "ssh":
        ssh(
            config=config,
            instance_name=args.instance_name,
            region_name=region,
            profile_name=profile,
            user=args.os_user,
            port=args.port,
            key_type=args.key_type,
            key_size=args.key_size,
            command=args.command,
        )
    elif args.subcommand == "ssh-config":
        ssh_config(
            region_name=region, profile_name=profile, user=args.os_user, port=args.port
        )
    elif args.subcommand == "ssh-proxy":
        ssh_proxy(
            config=config,
            instance_name=args.instance_name,
            region_name=region,
            profile_name=profile,
            user=args.os_user,
            port=args.port,
            key_type=args.key_type,
            key_size=args.key_size,
        )
    elif args.subcommand in ["ls", "list"]:
        fields = args.output.split(",")
        list_instances(
            region_name=region,
            profile_name=profile,
            output_format=args.format,
            fields=fields,
        )