forked from EmilLaursen/ec2scp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powah_scp.py
266 lines (199 loc) · 7.77 KB
/
powah_scp.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import boto3
from botocore.exceptions import ClientError
import click
from common.aws import (
get_instance_info,
resolve_instance_info_and_paths,
valid_available_instances,
retrieve_os_users,
)
from common.config import update_config, load_config
import os
import json
# Learn about the amazing query language for json structures.
# http://jmespath.org/tutorial.html
from pathlib import Path
APP_NAME = "ec2"
class EmptyObj:
pass
@click.group()
@click.pass_context
def app(ctx):
"""
Use the EC-instance connect tools, mssh and msftp, with instance names instead of
instance ids with the commands ec2 ssh and ec2 sftp.
Boto3 is used to retrieve instance ids and correct os user given instance names.
This only works if your instances have unique names, not containing whitespace.
"""
# Entry point for CLI.
# We just want some object for the context, to carry variables.
ctx.obj = EmptyObj()
public_key = Path.home() / ".ssh/id_rsa.pub"
if not public_key.exists():
public_key = None
click.echo(
"Rsa key not found. Please generate one with: ssh-keygen -t rsa -f ~/.ssh/id_rsa. Some commands will be unavailable."
)
else:
public_key = public_key.read_text()
ctx.obj.public_key = public_key
# Load custom config.
try:
ctx.obj.cfg, ctx.obj.config_file = load_config(Path(click.get_app_dir(APP_NAME)))
except json.decoder.JSONDecodeError:
raise click.ClickException(
f"Your configuration file is invalid JSON. Please fix: {Path(click.get_app_dir(APP_NAME))}"
)
# Confirm AWS credentials.
ctx.obj.ec2 = boto3.client("ec2")
ctx.obj.ec2ic = boto3.client("ec2-instance-connect")
try:
_ = ctx.obj.ec2.describe_instances(DryRun=True)
except ClientError as e:
if "DryRunOperation" not in str(e):
raise click.ClickException(
"Failure. Your AWS credentials do not authorize: describe_instances"
)
try:
_ = ctx.obj.ec2.describe_images(DryRun=True)
except ClientError as e:
if "DryRunOperation" not in str(e):
raise click.ClickException(
"Failure. Your AWS credentials do not authorize: describe_images"
)
def push_ssh_key(instance_info, public_key, client=None):
client = client if client is not None else boto3.client("ec2-instance-connect")
try:
resp = client.send_ssh_public_key(
InstanceId=instance_info["id"],
InstanceOSUser=instance_info["user"],
SSHPublicKey=public_key,
AvailabilityZone=instance_info["avz"],
)
except ClientError as e:
raise click.ClickException(f"{e}")
return resp
def lookup_instance_name(obj, name):
instance_info = obj.cfg.get(name)
if instance_info is None:
click.echo("Not found in config. Consider running make-config.")
instance_info = get_instance_info(name=name, inst_id=None)
update_config(obj.cfg, instance_info)
if instance_info is None:
raise click.ClickException("Instance not found")
return instance_info
@app.command()
@click.argument("src", type=click.STRING)
@click.argument("dst", type=click.STRING)
@click.pass_obj
def scp(obj, src, dst):
"""
\b
Use SCP to upload files to EC2 instances using aws IAM credentials instead of ssh keys.
Usage:
upload: ec2 scp source_file INSTANCE-ID:dest_file
download: ec2 scp INSTANCE-NAME:source_file dest_file
Instances can be referenced by instance id or Name. Setup a configuration file and edit it, to give custom aliases to your instances.
\b
You can use relative paths on the remote. Thus
ec2 scp t.txt NAME:file.txt
is equivalent to
ec2 scp t.txt NAME:/home/ubuntu/file.txt
if instance is running ubuntu.
"""
instance_info, src_path, dst_path = resolve_instance_info_and_paths(src, dst, obj)
# Push public key for 60 seconds.
_ = push_ssh_key(instance_info, obj.public_key, client=obj.ec2ic)
args = f"scp -i ~/.ssh/id_rsa {src_path} {dst_path}"
os.system(args)
@app.command()
@click.argument("name", type=click.STRING)
@click.pass_obj
def iid(obj: EmptyObj, name: str):
""" Usage: ec2 iid INSTANCE_NAME. Outputs os_user@instance id, given instance name."""
# Is name from config, or actual instance name?
if obj.cfg is not None:
# name found in config
if obj.cfg.get(name) is not None:
instance = obj.cfg.get(name)
output = f'{instance["os_user"]}@{instance["id"]}'
else:
instance_info = get_instance_info(name=name, inst_id=None)
output = f'{instance_info["os_user"]}@{instance_info["id"]}'
click.echo(output)
@app.command()
@click.argument("name", type=click.STRING)
@click.pass_obj
def ssh(obj: EmptyObj, name: str):
""" Usage: ec2 ssh INSTANCE-NAME to use EC2 instance connect to ssh to instance."""
instance_info = lookup_instance_name(obj, name)
# Call mssh with correct os_user and instance id.
os.system(f'mssh {instance_info["os_user"]}@{instance_info["id"]}')
@app.command()
@click.argument("name", type=click.STRING)
@click.pass_obj
def sftp(obj: EmptyObj, name: str):
""" Usage: ec2 sftp INSTANCE-NAME to use EC2 instance connect to sftp to instance."""
instance_info = lookup_instance_name(obj, name)
# Call msftp with correct os_user and instance id.
os.system(f'msftp {instance_info["os_user"]}@{instance_info["id"]}')
@app.command()
@click.argument("name", type=click.STRING)
@click.pass_obj
def setup_remote_dev(obj, name):
if not obj.public_key:
click.ClickException(
"Rsa key not found. Please generate one with: ssh-keygen -t rsa -f ~/.ssh/id_rsa. Must be PEM format."
)
instance_info = lookup_instance_name(obj, name)
ssh_config = Path.home() / ".ssh/config"
if ssh_config.exists():
entry_exists = any(
name in line for line in ssh_config.read_text().split("\n")
)
if entry_exists:
return
remote_auth_file = f"/home/{instance_info['os_user']}/.ssh/authorized_keys"
args = f'echo "echo \'{obj.public_key}\' >> \'{remote_auth_file}\'" | mssh {instance_info["os_user"]}@{instance_info["id"]}'
click.echo(args)
os.system(args)
ec2_entry = f"""Host {name}
HostName {instance_info['ip']}
User {instance_info['os_user']}
Port 22
IdentityFile {Path.home() / '.ssh/id_rsa'}"""
with ssh_config.open(mode="a") as file:
file.write(f"{ec2_entry}\n")
@app.command()
@click.option("--edit", "-e", is_flag=True, help="Launch config file in editor.")
@click.pass_obj
def make_config(obj: EmptyObj, edit: bool):
""" Usage: ec2 make-config --edit """
ec2 = obj.ec2
try:
valid_instances, invalid_instances = valid_available_instances(ec2)
except ClientError as e:
raise click.ClickException(f"AWS client failed: {e}")
if invalid_instances:
click.echo(
"Warning: Unamed instances, and instances with whitespace in their names, are ignored."
)
try:
instance_dics = retrieve_os_users(ec2, valid_instances)
except ClientError as e:
raise click.ClickException(f"AWS client failed: {e}")
update_config(obj, instance_dics)
if obj.cfg is None:
obj.cfg = {}
obj.cfg.update(
{instance_dic["name"]: instance_dic for instance_dic in instance_dics}
)
json.dump(obj.cfg, obj.config_file.open("w"), indent=4)
if edit:
click.launch(str(obj.config_file))
else:
click.echo(
f"Wrote instance aliases to {obj.config_file}. Change the aliases as you please."
)
if __name__ == "__main__":
app()