-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathssh.py
199 lines (173 loc) · 5.55 KB
/
ssh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import json
import logging
import shlex
from aws_gate.constants import (
AWS_DEFAULT_PROFILE,
AWS_DEFAULT_REGION,
DEFAULT_OS_USER,
DEFAULT_SSH_PORT,
DEFAULT_KEY_ALGORITHM,
DEFAULT_KEY_SIZE,
PLUGIN_INSTALL_PATH,
DEBUG,
DEFAULT_GATE_KEY_PATH,
)
from aws_gate.decorators import (
plugin_version,
plugin_required,
valid_aws_profile,
valid_aws_region,
)
from aws_gate.query import query_instance
from aws_gate.session_common import BaseSession
from aws_gate.ssh_common import SshKey, SshKeyUploader
from aws_gate.utils import (
get_aws_client,
get_aws_resource,
fetch_instance_details_from_config,
get_instance_details,
execute,
)
logger = logging.getLogger(__name__)
class SshSession(BaseSession):
def __init__(
self,
instance_id,
ssm=None,
region_name=AWS_DEFAULT_REGION,
profile_name=AWS_DEFAULT_PROFILE,
port=DEFAULT_SSH_PORT,
user=DEFAULT_OS_USER,
command=None,
local_forward=None,
remote_forward=None,
dynamic_forward=None,
):
self._instance_id = instance_id
self._region_name = region_name
self._profile_name = profile_name if profile_name is not None else ""
self._ssm = ssm
self._port = port
self._user = user
self._command = command
self._local_forward = local_forward
self._remote_forward = remote_forward
self._dynamic_forward = dynamic_forward
self._ssh_cmd = None
self._session_parameters = {
"Target": self._instance_id,
"DocumentName": "AWS-StartSSHSession",
"Parameters": {"portNumber": [str(self._port)]},
}
def _build_ssh_command(self):
cmd = [
"ssh",
"-l",
self._user,
"-p",
str(self._port),
"-F",
"/dev/null",
"-i",
DEFAULT_GATE_KEY_PATH,
]
if self._local_forward or self._remote_forward or self._dynamic_forward:
cmd.append("-N")
if self._local_forward:
cmd.extend(["-L", self._local_forward])
if self._remote_forward:
cmd.extend(["-R", self._remote_forward])
if self._dynamic_forward:
cmd.extend(["-D", self._dynamic_forward])
if DEBUG:
cmd.append("-vv")
else:
cmd.append("-q")
proxy_command_args = [
PLUGIN_INSTALL_PATH,
json.dumps(self._response),
self._region_name,
"StartSession",
self._profile_name,
json.dumps(self._session_parameters),
self._ssm.meta.endpoint_url,
]
proxy_command = " ".join(shlex.quote(i) for i in proxy_command_args)
ssh_options = [
"IdentitiesOnly=yes",
"UserKnownHostsFile=/dev/null",
"StrictHostKeyChecking=no",
f"ProxyCommand={proxy_command}",
]
for ssh_option in ssh_options:
cmd.append("-o")
cmd.append(ssh_option)
cmd.append(self._instance_id)
if self._command:
cmd.append("--")
cmd.extend(self._command)
return cmd
def open(self):
self._ssh_cmd = self._build_ssh_command()
return execute(self._ssh_cmd[0], self._ssh_cmd[1:])
@plugin_required
@plugin_version("1.1.23.0")
@valid_aws_profile
@valid_aws_region
def ssh(
config,
instance_name,
user=DEFAULT_OS_USER,
port=DEFAULT_SSH_PORT,
key_type=DEFAULT_KEY_ALGORITHM,
key_size=DEFAULT_KEY_SIZE,
profile_name=AWS_DEFAULT_PROFILE,
region_name=AWS_DEFAULT_REGION,
command=None,
local_forward=None,
remote_forward=None,
dynamic_forward=None,
):
instance, profile, region = fetch_instance_details_from_config(
config, instance_name, profile_name, region_name
)
ssm = get_aws_client("ssm", region_name=region, profile_name=profile)
ec2 = get_aws_resource("ec2", region_name=region, profile_name=profile)
ec2_ic = get_aws_client(
"ec2-instance-connect", region_name=region, profile_name=profile
)
instance_id = query_instance(name=instance, ec2=ec2)
if instance_id is None:
raise ValueError(f"No instance could be found for name: {instance}")
az = get_instance_details(instance_id=instance_id, ec2=ec2)["availability_zone"]
logger.info(
"Opening SSH session on instance %s (%s) via profile %s",
instance_id,
region,
profile,
)
if local_forward: # pragma: no cover
logger.info("SSH session will do a local port forwarding: %s", local_forward)
if remote_forward: # pragma: no cover
logger.info("SSH session will do a remote port forwarding: %s", remote_forward)
if dynamic_forward: # pragma: no cover
logger.info(
"SSH session will do a dynamic port forwarding: %s", dynamic_forward
)
with SshKey(key_type=key_type, key_size=key_size) as ssh_key:
with SshKeyUploader(
instance_id=instance_id, az=az, user=user, ssh_key=ssh_key, ec2_ic=ec2_ic
):
with SshSession(
instance_id,
region_name=region,
profile_name=profile,
ssm=ssm,
port=port,
user=user,
command=command,
local_forward=local_forward,
remote_forward=remote_forward,
dynamic_forward=dynamic_forward,
) as ssh_session:
ssh_session.open()