codecov[bot]: # [Codecov](https://codecov.io/gh/xen0l/aws-gate/pull/117?src=pr&el=h1) Report
> Merging [#117](https://codecov.io/gh/xen0l/aws-gate/pull/117?src=pr&el=desc) into [master](https://codecov.io/gh/xen0l/aws-gate/commit/a51722a58f658f4e6a7994fe4302a5328981262b?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/xen0l/aws-gate/pull/117/graphs/tree.svg?width=650&token=nlxNAKfBE9&height=150&src=pr)](https://codecov.io/gh/xen0l/aws-gate/pull/117?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #117      +/-   ##
==========================================
+ Coverage   99.16%   99.16%   +<.01%     
==========================================
  Files          15       15              
  Lines         715      717       +2     
==========================================
+ Hits          709      711       +2     
  Misses          6        6
```


| [Impacted Files](https://codecov.io/gh/xen0l/aws-gate/pull/117?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [aws\_gate/cli.py](https://codecov.io/gh/xen0l/aws-gate/pull/117/diff?src=pr&el=tree#diff-YXdzX2dhdGUvY2xpLnB5) | `96.93% <100%> (+0.06%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/xen0l/aws-gate/pull/117?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/xen0l/aws-gate/pull/117?src=pr&el=footer). Last update [a51722a...55bc12a](https://codecov.io/gh/xen0l/aws-gate/pull/117?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

diff --git a/aws_gate/cli.py b/aws_gate/cli.py
index 0a8fb33..001661a 100644
--- a/aws_gate/cli.py
+++ b/aws_gate/cli.py
@@ -120,17 +120,6 @@ def parse_arguments():
 
 
 def main():
-    # 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 call to boto3. boto3 will also return'default':
-    # https://github.com/boto/boto3/blob/develop/boto3/session.py#L93
-    default_profile = os.environ.get("AWS_PROFILE") or AWS_DEFAULT_PROFILE
-
     args = parse_arguments()
 
     if not DEBUG:
@@ -158,6 +147,27 @@ def main():
     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)
 

"Invalid profile provided: default" when using environment variables
I'm using environment variables to configure the AWS credentials for aws-gate and don't have a default profile in .aws/config because of that. (I'm using https://github.com/99designs/aws-vault (eg `aws-vault exec profile_name -- aws-gate list`), but any tool that sets environment variables will work the same).

This leads to the error "Invalid profile provided: default" when running aws-gate (even though valid credentials are available in the environment). I can work around this by adding a dummy default profile in the configuration file, but it would be nice if that wasn't needed.


diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py
index b11883c..405f284 100644
--- a/tests/unit/test_cli.py
+++ b/tests/unit/test_cli.py
@@ -1,5 +1,6 @@
 import argparse
 import unittest
+import os
 from unittest.mock import patch, MagicMock, create_autospec, call
 
 from marshmallow import ValidationError
@@ -144,3 +145,22 @@ class TestCli(unittest.TestCase):
             self.assertTrue(parser_mock.print_help.called)
             self.assertTrue(exit_mock.called)
             self.assertEqual(exit_mock.call_args, call(1))
+
+    def test_cli_default_profile_from_aws_vault(self):
+        with patch.dict(os.environ, {"AWS_VAULT": "vault_profile"}), patch(
+            "aws_gate.cli.parse_arguments", return_value=MagicMock(subcommand="list")
+        ), patch("aws_gate.decorators.is_existing_region", return_value=True), patch(
+            "aws_gate.decorators.is_existing_profile", return_value=True
+        ), patch(
+            "aws_gate.cli.list_instances"
+        ), patch(
+            "aws_gate.cli.logging.getLogger"
+        ) as logger_mock, patch(
+            "aws_gate.cli._get_profile"
+        ) as m:
+            main()
+
+            self.assertTrue(m.called)
+            self.assertEqual(m.call_args[1]["default"], "vault_profile")
+
+            self.assertTrue(logger_mock.called)

pip install -e .
parse_log_pytest
requirements.txt
pytest pytest-cov pytest-xdist pytest-mock pytest-asyncio
3.7
requirements/requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
-e git+https://github.com/xen0l/aws-gate.git@3f8731054b1e7f6eb8e8b74b3163b450be850722#egg=aws_gate
boto3==1.10.50
botocore==1.13.50
certifi @ file:///croot/certifi_1671487769961/work/certifi
cffi==1.15.1
chardet==3.0.4
coverage==7.2.7
cryptography==2.8
docutils==0.15.2
exceptiongroup==1.2.2
execnet==2.0.2
idna==2.8
importlib-metadata==6.7.0
iniconfig==2.0.0
jmespath==0.10.0
marshmallow==3.2.1
packaging==24.0
pluggy==1.2.0
pycparser==2.21
pytest==7.4.4
pytest-asyncio==0.21.2
pytest-cov==4.1.0
pytest-mock==3.11.1
pytest-xdist==3.5.0
python-dateutil==2.9.0.post0
PyYAML==5.1.2
requests==2.22.0
s3transfer==0.2.1
six==1.17.0
tomli==2.0.1
typing_extensions==4.7.1
unix-ar==0.2.1
urllib3==1.25.11
wrapt==1.11.2
zipp==3.15.0

name: aws-gate
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - boto3==1.10.50
      - botocore==1.13.50
      - cffi==1.15.1
      - chardet==3.0.4
      - coverage==7.2.7
      - cryptography==2.8
      - docutils==0.15.2
      - exceptiongroup==1.2.2
      - execnet==2.0.2
      - idna==2.8
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - jmespath==0.10.0
      - marshmallow==3.2.1
      - packaging==24.0
      - pluggy==1.2.0
      - pycparser==2.21
      - pytest==7.4.4
      - pytest-asyncio==0.21.2
      - pytest-cov==4.1.0
      - pytest-mock==3.11.1
      - pytest-xdist==3.5.0
      - python-dateutil==2.9.0.post0
      - pyyaml==5.1.2
      - requests==2.22.0
      - s3transfer==0.2.1
      - six==1.17.0
      - tomli==2.0.1
      - typing-extensions==4.7.1
      - unix-ar==0.2.1
      - urllib3==1.25.11
      - wrapt==1.11.2
      - zipp==3.15.0
prefix: /opt/conda/envs/aws-gate

tests/unit/test_cli.py::TestCli::test_cli_default_profile_from_aws_vault
tests/unit/test_cli.py::TestCli::test_cli_bootstrap tests/unit/test_cli.py::TestCli::test_cli_invalid_config tests/unit/test_cli.py::TestCli::test_cli_list tests/unit/test_cli.py::TestCli::test_cli_param_error tests/unit/test_cli.py::TestCli::test_cli_parse_arguments_unknown_subcommand tests/unit/test_cli.py::TestCli::test_cli_session tests/unit/test_cli.py::TestCli::test_cli_ssh_config tests/unit/test_cli.py::TestCli::test_cli_ssh_proxy tests/unit/test_cli.py::TestCli::test_get_profile_from_args tests/unit/test_cli.py::TestCli::test_get_profile_from_config tests/unit/test_cli.py::TestCli::test_get_profile_from_default tests/unit/test_cli.py::TestCli::test_get_region_from_args tests/unit/test_cli.py::TestCli::test_get_region_from_config tests/unit/test_cli.py::TestCli::test_get_region_from_default
aws_gate/cli.py
aws_gate/cli.py