Skip to content

Commit 56f3db6

Browse files
authored
Always use own version of session-manager-plugin (#42)
1 parent 66bf0af commit 56f3db6

File tree

12 files changed

+57
-35
lines changed

12 files changed

+57
-35
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
------------------
33

44
* Use default region and profile in list command
5+
* Always use own version of session-manager-plugin if available
56

67
0.4.1 (2019-08-12)
78
------------------

aws_gate/bootstrap.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,11 @@
66
import platform
77
import requests
88

9-
from aws_gate.config import DEFAULT_GATE_BIN_PATH, PLUGIN_INSTALL_PATH, PLUGIN_NAME
9+
from aws_gate.constants import MAC_PLUGIN_URL, DEFAULT_GATE_BIN_PATH, PLUGIN_INSTALL_PATH, PLUGIN_NAME
1010
from aws_gate.exceptions import UnsupportedPlatormError
1111
from aws_gate.utils import execute
1212

1313

14-
MAC_PLUGIN_URL = 'https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip'
15-
16-
1714
logger = logging.getLogger(__name__)
1815

1916

aws_gate/config.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66
from yaml.parser import ParserError
77
from marshmallow import Schema, fields, post_load, ValidationError, validates_schema
88

9+
from aws_gate.constants import DEFAULT_GATE_CONFIG_PATH, DEFAULT_GATE_CONFIGD_PATH
910
from aws_gate.utils import is_existing_profile, is_existing_region
1011

11-
DEFAULT_GATE_DIR = os.path.expanduser('~/.aws-gate')
12-
DEFAULT_GATE_CONFIG_PATH = os.path.join(DEFAULT_GATE_DIR, 'config')
13-
DEFAULT_GATE_CONFIGD_PATH = os.path.join(DEFAULT_GATE_DIR, 'config.d')
14-
15-
PLUGIN_NAME = 'session-manager-plugin'
16-
DEFAULT_GATE_BIN_PATH = os.path.join(DEFAULT_GATE_DIR, 'bin')
17-
PLUGIN_INSTALL_PATH = os.path.join(DEFAULT_GATE_BIN_PATH, PLUGIN_NAME)
1812

1913
logger = logging.getLogger(__name__)
2014

aws_gate/constants.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
3+
DEFAULT_GATE_DIR = os.path.expanduser('~/.aws-gate')
4+
DEFAULT_GATE_CONFIG_PATH = os.path.join(DEFAULT_GATE_DIR, 'config')
5+
DEFAULT_GATE_CONFIGD_PATH = os.path.join(DEFAULT_GATE_DIR, 'config.d')
6+
7+
PLUGIN_NAME = 'session-manager-plugin'
8+
DEFAULT_GATE_BIN_PATH = os.path.join(DEFAULT_GATE_DIR, 'bin')
9+
PLUGIN_INSTALL_PATH = os.path.join(DEFAULT_GATE_BIN_PATH, PLUGIN_NAME)
10+
11+
MAC_PLUGIN_URL = 'https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip'

aws_gate/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import os
22
import functools
33

4+
from aws_gate.constants import PLUGIN_INSTALL_PATH
45
from aws_gate.utils import execute
5-
from aws_gate.config import PLUGIN_INSTALL_PATH
66

77

88
def _plugin_exists(plugin_path):

aws_gate/session.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import errno
22
import json
33
import logging
4-
import subprocess
54

65
from aws_gate.query import query_instance
76
from aws_gate.decorators import plugin_version, plugin_required
8-
from aws_gate.utils import get_aws_client, get_aws_resource, deferred_signals, is_existing_profile, is_existing_region
7+
from aws_gate.utils import get_aws_client, get_aws_resource, deferred_signals, is_existing_profile, \
8+
is_existing_region, execute
99

1010
logger = logging.getLogger(__name__)
1111

@@ -44,12 +44,9 @@ def terminate(self):
4444
def open(self):
4545
with deferred_signals():
4646
try:
47-
logger.debug('Executing session-manager-plugin')
48-
subprocess.check_call(['session-manager-plugin',
49-
json.dumps(self._response),
50-
self._region_name,
51-
'StartSession'])
52-
logger.debug('session-manager plugin finished successfully')
47+
execute('session-manager-plugin', [json.dumps(self._response),
48+
self._region_name,
49+
'StartSession'])
5350
except OSError as ex:
5451
if ex.errno == errno.ENOENT:
5552
raise ValueError('session-manager-plugin cannot be found')

aws_gate/utils.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import boto3
88

9+
from aws_gate.constants import DEFAULT_GATE_BIN_PATH
910

1011
logger = logging.getLogger(__name__)
1112

@@ -87,15 +88,18 @@ def deferred_signals(signal_list=None):
8788
signal.signal(deferred_signal, signal.SIG_DFL)
8889

8990

90-
def execute(path, args, capture_output=True):
91-
ret = None
91+
def execute(cmd, args, **kwargs):
92+
ret, result = None, None
93+
94+
env = DEFAULT_GATE_BIN_PATH + os.pathsep + os.environ['PATH']
95+
9296
try:
93-
logger.debug('Executing "%s"', ' '.join([path] + args))
94-
result = subprocess.run([path] + args, capture_output=capture_output)
97+
logger.debug('Executing "%s"', ' '.join([cmd] + args))
98+
result = subprocess.run([cmd] + args, env={'PATH': env}, check=True, **kwargs)
9599
except subprocess.CalledProcessError as e:
96-
logger.error('Command "%s" exited with %s', ' '.join([path] + args), e.returncode)
100+
logger.error('Command "%s" exited with %s', ' '.join([cmd] + args), e.returncode)
97101

98-
if result.stdout:
102+
if result and result.stdout:
99103
ret = result.stdout.decode()
100104
ret = ret.rstrip()
101105

requirements/requirements_dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ pytest-flake8==1.0.4
66
pytest-pylint==0.14.1
77
pytest==5.0.1
88
zest.releaser==6.19.0
9+
hypothesis==4.32.3

test/unit/test_bootstrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import unittest
22
from unittest.mock import patch, MagicMock, call, mock_open
33

4+
from aws_gate.constants import DEFAULT_GATE_BIN_PATH, PLUGIN_INSTALL_PATH
45
from aws_gate.exceptions import UnsupportedPlatormError
5-
from aws_gate.config import DEFAULT_GATE_BIN_PATH, PLUGIN_INSTALL_PATH
66
from aws_gate.bootstrap import Plugin, MacPlugin, bootstrap
77

88

test/unit/test_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from unittest.mock import patch
55
from marshmallow import ValidationError
66

7-
from aws_gate.config import GateConfig, EmptyConfigurationError, load_config_from_files, _locate_config_files, \
8-
DEFAULT_GATE_CONFIG_PATH, DEFAULT_GATE_CONFIGD_PATH
7+
from aws_gate.constants import DEFAULT_GATE_CONFIG_PATH, DEFAULT_GATE_CONFIGD_PATH
8+
from aws_gate.config import GateConfig, EmptyConfigurationError, load_config_from_files, _locate_config_files
99

1010

1111
class TestConfig(unittest.TestCase):

0 commit comments

Comments
 (0)