cloudx-proxy 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from ._version import __version__
2
+
3
+ __all__ = ['__version__']
@@ -0,0 +1,16 @@
1
+ # file generated by setuptools_scm
2
+ # don't change, don't track in version control
3
+ TYPE_CHECKING = False
4
+ if TYPE_CHECKING:
5
+ from typing import Tuple, Union
6
+ VERSION_TUPLE = Tuple[Union[int, str], ...]
7
+ else:
8
+ VERSION_TUPLE = object
9
+
10
+ version: str
11
+ __version__: str
12
+ __version_tuple__: VERSION_TUPLE
13
+ version_tuple: VERSION_TUPLE
14
+
15
+ __version__ = version = '0.1.1'
16
+ __version_tuple__ = version_tuple = (0, 1, 1)
cloudx_proxy/cli.py ADDED
@@ -0,0 +1,93 @@
1
+ import os
2
+ import sys
3
+ import click
4
+ from . import __version__
5
+ from .core import CloudXClient
6
+ from .setup import CloudXSetup
7
+
8
+ @click.group()
9
+ @click.version_option(version=__version__)
10
+ def cli():
11
+ """CloudX Client - Connect to EC2 instances via SSM for VSCode Remote SSH."""
12
+ pass
13
+
14
+ @cli.command()
15
+ @click.argument('instance_id')
16
+ @click.argument('port', type=int, default=22)
17
+ @click.option('--profile', default='vscode', help='AWS profile to use (default: vscode)')
18
+ @click.option('--region', help='AWS region (default: from profile, or eu-west-1 if not set)')
19
+ @click.option('--key-path', help='Path to SSH public key (default: ~/.ssh/vscode/vscode.pub)')
20
+ @click.option('--aws-env', help='AWS environment directory (default: ~/.aws, use name of directory in ~/.aws/aws-envs/)')
21
+ def connect(instance_id: str, port: int, profile: str, region: str, key_path: str, aws_env: str):
22
+ """Connect to an EC2 instance via SSM.
23
+
24
+ INSTANCE_ID is the EC2 instance ID to connect to (e.g., i-0123456789abcdef0)
25
+
26
+ Example usage:
27
+ cloudx-proxy i-0123456789abcdef0 22
28
+ cloudx-proxy i-0123456789abcdef0 22 --profile myprofile --region eu-west-1
29
+ cloudx-proxy i-0123456789abcdef0 22 --aws-env prod
30
+ """
31
+ try:
32
+ client = CloudXClient(
33
+ instance_id=instance_id,
34
+ port=port,
35
+ profile=profile,
36
+ region=region,
37
+ public_key_path=key_path,
38
+ aws_env=aws_env
39
+ )
40
+
41
+ if not client.connect():
42
+ sys.exit(1)
43
+
44
+ except Exception as e:
45
+ print(f"Error: {str(e)}", file=sys.stderr)
46
+ sys.exit(1)
47
+
48
+ @cli.command()
49
+ @click.option('--profile', default='vscode', help='AWS profile to use (default: vscode)')
50
+ @click.option('--ssh-key', default='vscode', help='SSH key name to use (default: vscode)')
51
+ @click.option('--aws-env', help='AWS environment directory (default: ~/.aws, use name of directory in ~/.aws/aws-envs/)')
52
+ def setup(profile: str, ssh_key: str, aws_env: str):
53
+ """Set up AWS profile, SSH keys, and configuration for CloudX.
54
+
55
+ This command will:
56
+ 1. Set up AWS profile with credentials
57
+ 2. Create or use existing SSH key
58
+ 3. Configure SSH for CloudX instances
59
+ 4. Check instance setup status
60
+
61
+ Example usage:
62
+ cloudx-proxy setup
63
+ cloudx-proxy setup --profile myprofile --ssh-key mykey
64
+ """
65
+ try:
66
+ setup = CloudXSetup(profile=profile, ssh_key=ssh_key, aws_env=aws_env)
67
+
68
+ # Set up AWS profile
69
+ if not setup.setup_aws_profile():
70
+ sys.exit(1)
71
+
72
+ # Set up SSH key
73
+ if not setup.setup_ssh_key():
74
+ sys.exit(1)
75
+
76
+ # Get CloudX environment and instance details
77
+ cloudx_env = click.prompt("Enter CloudX environment (e.g., dev, prod)", type=str)
78
+ instance_id = click.prompt("Enter EC2 instance ID (e.g., i-0123456789abcdef0)", type=str)
79
+ hostname = click.prompt("Enter hostname for the instance", type=str)
80
+
81
+ # Set up SSH config
82
+ if not setup.setup_ssh_config(cloudx_env, instance_id, hostname):
83
+ sys.exit(1)
84
+
85
+ # Check instance setup status
86
+ setup.wait_for_setup_completion(instance_id)
87
+
88
+ except Exception as e:
89
+ print(f"Error: {str(e)}", file=sys.stderr)
90
+ sys.exit(1)
91
+
92
+ if __name__ == '__main__':
93
+ cli()
cloudx_proxy/core.py ADDED
@@ -0,0 +1,189 @@
1
+ import os
2
+ import sys
3
+ import time
4
+ from pathlib import Path
5
+ import boto3
6
+ from botocore.exceptions import ClientError
7
+
8
+ class CloudXClient:
9
+ def __init__(self, instance_id: str, port: int = 22, profile: str = "vscode",
10
+ region: str = None, public_key_path: str = None, aws_env: str = None):
11
+ """Initialize CloudX client for SSH tunneling via AWS SSM.
12
+
13
+ Args:
14
+ instance_id: EC2 instance ID to connect to
15
+ port: SSH port number (default: 22)
16
+ profile: AWS profile to use (default: "vscode")
17
+ region: AWS region (default: from profile)
18
+ public_key_path: Path to SSH public key (default: ~/.ssh/vscode/vscode.pub)
19
+ aws_env: AWS environment directory (default: None, uses ~/.aws)
20
+ """
21
+ self.instance_id = instance_id
22
+ self.port = port
23
+ self.profile = profile
24
+
25
+ # Configure AWS environment
26
+ if aws_env:
27
+ aws_env_dir = os.path.expanduser(f"~/.aws/aws-envs/{aws_env}")
28
+ os.environ["AWS_CONFIG_FILE"] = os.path.join(aws_env_dir, "config")
29
+ os.environ["AWS_SHARED_CREDENTIALS_FILE"] = os.path.join(aws_env_dir, "credentials")
30
+
31
+ # Set up AWS session with eu-west-1 as default region
32
+ if not region:
33
+ # Try to get region from profile first
34
+ session = boto3.Session(profile_name=profile)
35
+ region = session.region_name or 'eu-west-1'
36
+
37
+ self.session = boto3.Session(profile_name=profile, region_name=region)
38
+ self.ssm = self.session.client('ssm')
39
+ self.ec2 = self.session.client('ec2')
40
+ self.ec2_connect = self.session.client('ec2-instance-connect')
41
+
42
+ # Default public key path if not provided
43
+ if not public_key_path:
44
+ public_key_path = os.path.expanduser("~/.ssh/vscode/vscode.pub")
45
+ self.public_key_path = Path(public_key_path)
46
+
47
+ def log(self, message: str) -> None:
48
+ """Log message to stderr to avoid interfering with SSH connection."""
49
+ print(message, file=sys.stderr)
50
+
51
+ def get_instance_status(self) -> str:
52
+ """Check if instance is online in SSM."""
53
+ try:
54
+ response = self.ssm.describe_instance_information(
55
+ Filters=[{'Key': 'InstanceIds', 'Values': [self.instance_id]}]
56
+ )
57
+ if response['InstanceInformationList']:
58
+ return response['InstanceInformationList'][0]['PingStatus']
59
+ return 'Offline'
60
+ except ClientError:
61
+ return 'Offline'
62
+
63
+ def start_instance(self) -> bool:
64
+ """Start the EC2 instance if it's stopped."""
65
+ try:
66
+ self.ec2.start_instances(InstanceIds=[self.instance_id])
67
+ return True
68
+ except ClientError as e:
69
+ self.log(f"Error starting instance: {e}")
70
+ return False
71
+
72
+ def wait_for_instance(self, max_attempts: int = 30, delay: int = 3) -> bool:
73
+ """Wait for instance to come online.
74
+
75
+ Args:
76
+ max_attempts: Maximum number of status checks
77
+ delay: Seconds between checks
78
+
79
+ Returns:
80
+ bool: True if instance came online, False if timeout
81
+ """
82
+ for _ in range(max_attempts):
83
+ if self.get_instance_status() == 'Online':
84
+ return True
85
+ time.sleep(delay)
86
+ return False
87
+
88
+ def push_ssh_key(self) -> bool:
89
+ """Push SSH public key to instance via EC2 Instance Connect."""
90
+ try:
91
+ with open(self.public_key_path) as f:
92
+ public_key = f.read()
93
+
94
+ self.ec2_connect.send_ssh_public_key(
95
+ InstanceId=self.instance_id,
96
+ InstanceOSUser='ec2-user',
97
+ SSHPublicKey=public_key
98
+ )
99
+ return True
100
+ except (ClientError, FileNotFoundError) as e:
101
+ self.log(f"Error pushing SSH key: {e}")
102
+ return False
103
+
104
+ def start_session(self) -> None:
105
+ """Start SSM session with SSH port forwarding.
106
+
107
+ Uses AWS CLI directly to ensure proper stdin/stdout handling for SSH ProxyCommand.
108
+ The session manager plugin will automatically handle the data transfer.
109
+
110
+ When used as a ProxyCommand, we need to:
111
+ 1. Pass through stdin/stdout directly to AWS CLI
112
+ 2. Only use stderr for logging
113
+ 3. Let the session manager plugin handle the actual data transfer
114
+ """
115
+ import subprocess
116
+ import platform
117
+
118
+ try:
119
+ # Build environment with AWS credentials configuration
120
+ env = os.environ.copy()
121
+ if 'AWS_CONFIG_FILE' in os.environ:
122
+ env['AWS_CONFIG_FILE'] = os.environ['AWS_CONFIG_FILE']
123
+ if 'AWS_SHARED_CREDENTIALS_FILE' in os.environ:
124
+ env['AWS_SHARED_CREDENTIALS_FILE'] = os.environ['AWS_SHARED_CREDENTIALS_FILE']
125
+
126
+ # Determine AWS CLI command based on platform
127
+ aws_cmd = 'aws.exe' if platform.system() == 'Windows' else 'aws'
128
+
129
+ # Build command as list (works for both Windows and Unix)
130
+ cmd = [
131
+ aws_cmd, 'ssm', 'start-session',
132
+ '--target', self.instance_id,
133
+ '--document-name', 'AWS-StartSSHSession',
134
+ '--parameters', f'portNumber={self.port}',
135
+ '--profile', self.profile,
136
+ '--region', self.session.region_name
137
+ ]
138
+
139
+ # Start AWS CLI process with direct stdin/stdout pass-through
140
+ process = subprocess.Popen(
141
+ cmd,
142
+ env=env,
143
+ stdin=sys.stdin,
144
+ stdout=sys.stdout,
145
+ stderr=subprocess.PIPE, # Capture stderr for our logging
146
+ shell=platform.system() == 'Windows' # shell=True only on Windows
147
+ )
148
+
149
+ # Monitor stderr for logging while process runs
150
+ while True:
151
+ err_line = process.stderr.readline()
152
+ if not err_line and process.poll() is not None:
153
+ break
154
+ if err_line:
155
+ self.log(err_line.decode().strip())
156
+
157
+ if process.returncode != 0:
158
+ raise subprocess.CalledProcessError(process.returncode, cmd)
159
+
160
+ except subprocess.CalledProcessError as e:
161
+ self.log(f"Error starting session: {e}")
162
+ raise
163
+
164
+ def connect(self) -> bool:
165
+ """Main connection flow:
166
+ 1. Check instance status
167
+ 2. Start if needed and wait for online
168
+ 3. Push SSH key
169
+ 4. Start SSM session
170
+ """
171
+ status = self.get_instance_status()
172
+
173
+ if status != 'Online':
174
+ self.log(f"Instance {self.instance_id} is {status}, starting...")
175
+ if not self.start_instance():
176
+ return False
177
+
178
+ self.log("Waiting for instance to come online...")
179
+ if not self.wait_for_instance():
180
+ self.log("Instance failed to come online")
181
+ return False
182
+
183
+ self.log("Pushing SSH public key...")
184
+ if not self.push_ssh_key():
185
+ return False
186
+
187
+ self.log("Starting SSM session...")
188
+ self.start_session()
189
+ return True
cloudx_proxy/setup.py ADDED
@@ -0,0 +1,336 @@
1
+ import os
2
+ import time
3
+ import json
4
+ import subprocess
5
+ from pathlib import Path
6
+ from typing import Optional, Tuple
7
+ import boto3
8
+ from botocore.exceptions import ClientError
9
+
10
+ class CloudXSetup:
11
+ def __init__(self, profile: str = "vscode", ssh_key: str = "vscode", aws_env: str = None):
12
+ """Initialize CloudX setup.
13
+
14
+ Args:
15
+ profile: AWS profile name (default: "vscode")
16
+ ssh_key: SSH key name (default: "vscode")
17
+ aws_env: AWS environment directory (default: None)
18
+ """
19
+ self.profile = profile
20
+ self.ssh_key = ssh_key
21
+ self.aws_env = aws_env
22
+ self.home_dir = str(Path.home())
23
+ self.ssh_dir = Path(self.home_dir) / ".ssh" / "vscode"
24
+ self.ssh_config_file = self.ssh_dir / "config"
25
+ self.ssh_key_file = self.ssh_dir / f"{ssh_key}"
26
+ self.using_1password = False
27
+
28
+ def setup_aws_profile(self) -> bool:
29
+ """Set up AWS profile using aws configure command.
30
+
31
+ Returns:
32
+ bool: True if profile was set up successfully
33
+ """
34
+ try:
35
+ # Configure AWS environment if specified
36
+ if self.aws_env:
37
+ aws_env_dir = os.path.expanduser(f"~/.aws/aws-envs/{self.aws_env}")
38
+ os.environ["AWS_CONFIG_FILE"] = os.path.join(aws_env_dir, "config")
39
+ os.environ["AWS_SHARED_CREDENTIALS_FILE"] = os.path.join(aws_env_dir, "credentials")
40
+
41
+ # Check if profile exists
42
+ session = boto3.Session(profile_name=self.profile)
43
+ try:
44
+ session.client('sts').get_caller_identity()
45
+ print(f"AWS profile '{self.profile}' already exists and is valid.")
46
+ return True
47
+ except ClientError:
48
+ pass
49
+
50
+ # Profile doesn't exist or is invalid, set it up
51
+ print(f"Setting up AWS profile '{self.profile}'...")
52
+ print("Please enter your AWS credentials:")
53
+
54
+ # Use aws configure command
55
+ subprocess.run([
56
+ 'aws', 'configure',
57
+ '--profile', self.profile
58
+ ], check=True)
59
+
60
+ # Verify the profile works
61
+ session = boto3.Session(profile_name=self.profile)
62
+ identity = session.client('sts').get_caller_identity()
63
+ user_arn = identity['Arn']
64
+
65
+ if not any(part.startswith('cloudX-') for part in user_arn.split('/')):
66
+ print(f"Warning: User ARN '{user_arn}' does not match expected format cloudX-{{env}}-{{user}}")
67
+
68
+ return True
69
+
70
+ except Exception as e:
71
+ print(f"Error setting up AWS profile: {e}")
72
+ return False
73
+
74
+ def setup_ssh_key(self) -> bool:
75
+ """Set up SSH key pair.
76
+
77
+ Returns:
78
+ bool: True if key was set up successfully
79
+ """
80
+ try:
81
+ # Create .ssh/vscode directory if it doesn't exist
82
+ self.ssh_dir.mkdir(parents=True, exist_ok=True)
83
+
84
+ key_exists = self.ssh_key_file.exists() and (self.ssh_key_file.with_suffix('.pub')).exists()
85
+
86
+ if key_exists:
87
+ print(f"SSH key '{self.ssh_key}' already exists.")
88
+ self.using_1password = input("Would you like to use 1Password SSH agent? (y/N): ").lower() == 'y'
89
+ if not self.using_1password:
90
+ store_in_1password = input("Would you like to store the private key in 1Password? (y/N): ").lower() == 'y'
91
+ if store_in_1password:
92
+ self._store_key_in_1password()
93
+ else:
94
+ print(f"Generating new SSH key '{self.ssh_key}'...")
95
+ subprocess.run([
96
+ 'ssh-keygen',
97
+ '-t', 'ed25519',
98
+ '-f', str(self.ssh_key_file),
99
+ '-N', '' # Empty passphrase
100
+ ], check=True)
101
+
102
+ self.using_1password = input("Would you like to use 1Password SSH agent? (y/N): ").lower() == 'y'
103
+ if not self.using_1password:
104
+ store_in_1password = input("Would you like to store the private key in 1Password? (y/N): ").lower() == 'y'
105
+ if store_in_1password:
106
+ self._store_key_in_1password()
107
+
108
+ return True
109
+
110
+ except Exception as e:
111
+ print(f"Error setting up SSH key: {e}")
112
+ return False
113
+
114
+ def _store_key_in_1password(self) -> bool:
115
+ """Store SSH private key in 1Password.
116
+
117
+ Returns:
118
+ bool: True if key was stored successfully
119
+ """
120
+ try:
121
+ subprocess.run(['op', '--version'], check=True, capture_output=True)
122
+ print("Storing private key in 1Password...")
123
+ subprocess.run([
124
+ 'op', 'document', 'create',
125
+ str(self.ssh_key_file),
126
+ '--title', f'CloudX SSH Key - {self.ssh_key}'
127
+ ], check=True)
128
+ return True
129
+ except subprocess.CalledProcessError:
130
+ print("Error: 1Password CLI not installed or not signed in.")
131
+ return False
132
+
133
+ def setup_ssh_config(self, cloudx_env: str, instance_id: str, hostname: str) -> bool:
134
+ """Set up SSH config for the instance.
135
+
136
+ This method manages the SSH configuration in ~/.ssh/vscode/config, with the following behavior:
137
+ 1. For a new environment (if cloudx-{env}-* doesn't exist):
138
+ Creates a base config with:
139
+ - User and key configuration
140
+ - 1Password SSH agent integration if selected
141
+ - ProxyCommand using uvx cloudx-proxy with proper parameters
142
+
143
+ 2. For an existing environment:
144
+ - Skips creating duplicate environment config
145
+ - Only adds the new host entry
146
+
147
+ Example config structure:
148
+ ```
149
+ # Base environment config (created only once per environment)
150
+ Host cloudx-{env}-*
151
+ User ec2-user
152
+ IdentityAgent ~/.1password/agent.sock # If using 1Password
153
+ IdentityFile ~/.ssh/vscode/key.pub # .pub for 1Password, no .pub otherwise
154
+ IdentitiesOnly yes # If using 1Password
155
+ ProxyCommand uvx cloudx-proxy connect %h %p --profile profile --aws-env env
156
+
157
+ # Host entries (added for each instance)
158
+ Host cloudx-{env}-hostname
159
+ HostName i-1234567890
160
+ ```
161
+
162
+ Args:
163
+ cloudx_env: CloudX environment (e.g., dev, prod)
164
+ instance_id: EC2 instance ID
165
+ hostname: Hostname for the instance
166
+
167
+ Returns:
168
+ bool: True if config was set up successfully
169
+ """
170
+ try:
171
+ # Check if we need to create base config
172
+ need_base_config = True
173
+ if self.ssh_config_file.exists():
174
+ current_config = self.ssh_config_file.read_text()
175
+ # Check if configuration for this environment already exists
176
+ if f"Host cloudx-{cloudx_env}-*" in current_config:
177
+ need_base_config = False
178
+
179
+ if need_base_config:
180
+ # Build ProxyCommand with all necessary parameters
181
+ proxy_command = f"uvx cloudx-proxy connect %h %p --profile {self.profile}"
182
+ if self.aws_env:
183
+ proxy_command += f" --aws-env {self.aws_env}"
184
+ if self.ssh_key != "vscode":
185
+ proxy_command += f" --key-path {self.ssh_key_file}.pub"
186
+
187
+ # Build base configuration
188
+ base_config = f"""# CloudX SSH Configuration
189
+ Host cloudx-{cloudx_env}-*
190
+ User ec2-user
191
+ """
192
+ # Add 1Password or standard key configuration
193
+ if self.using_1password:
194
+ base_config += f""" IdentityAgent ~/.1password/agent.sock
195
+ IdentityFile {self.ssh_key_file}.pub
196
+ IdentitiesOnly yes
197
+ """
198
+ else:
199
+ base_config += f""" IdentityFile {self.ssh_key_file}
200
+ """
201
+ # Add ProxyCommand
202
+ base_config += f""" ProxyCommand {proxy_command}
203
+ """
204
+
205
+ # If file exists, append the new config, otherwise create it
206
+ if self.ssh_config_file.exists():
207
+ with open(self.ssh_config_file, 'a') as f:
208
+ f.write("\n" + base_config)
209
+ else:
210
+ self.ssh_config_file.write_text(base_config)
211
+
212
+ # Add specific host entry
213
+ host_entry = f"""
214
+ Host cloudx-{cloudx_env}-{hostname}
215
+ HostName {instance_id}
216
+ """
217
+ with open(self.ssh_config_file, 'a') as f:
218
+ f.write(host_entry)
219
+
220
+ # Ensure main SSH config includes our config
221
+ main_config = Path(self.home_dir) / ".ssh" / "config"
222
+ include_line = f"Include {self.ssh_config_file}\n"
223
+
224
+ if main_config.exists():
225
+ content = main_config.read_text()
226
+ if include_line not in content:
227
+ with open(main_config, 'a') as f:
228
+ f.write(f"\n{include_line}")
229
+ else:
230
+ main_config.write_text(include_line)
231
+
232
+ print(f"\nSSH configuration has been set up:")
233
+ print(f"- Main config file: {main_config}")
234
+ print(f"- CloudX config file: {self.ssh_config_file}")
235
+ print(f"\nYou can now connect using: ssh cloudx-{cloudx_env}-{hostname}")
236
+
237
+ return True
238
+
239
+ except Exception as e:
240
+ print(f"Error setting up SSH config: {e}")
241
+ return False
242
+
243
+ def check_instance_setup(self, instance_id: str) -> Tuple[bool, bool]:
244
+ """Check if instance setup is complete.
245
+
246
+ Args:
247
+ instance_id: EC2 instance ID
248
+
249
+ Returns:
250
+ Tuple[bool, bool]: (is_running, is_setup_complete)
251
+ """
252
+ try:
253
+ session = boto3.Session(profile_name=self.profile)
254
+ ssm = session.client('ssm')
255
+
256
+ # Check if instance is online in SSM
257
+ response = ssm.describe_instance_information(
258
+ Filters=[{'Key': 'InstanceIds', 'Values': [instance_id]}]
259
+ )
260
+ is_running = bool(response['InstanceInformationList'])
261
+
262
+ if not is_running:
263
+ return False, False
264
+
265
+ # Check setup status using SSM command
266
+ response = ssm.send_command(
267
+ InstanceIds=[instance_id],
268
+ DocumentName='AWS-RunShellScript',
269
+ Parameters={
270
+ 'commands': [
271
+ 'test -f /home/ec2-user/.install-done && echo "DONE" || '
272
+ 'test -f /home/ec2-user/.install-running && echo "RUNNING" || '
273
+ 'echo "NOT_STARTED"'
274
+ ]
275
+ }
276
+ )
277
+
278
+ command_id = response['Command']['CommandId']
279
+
280
+ # Wait for command completion
281
+ for _ in range(10): # 10 second timeout
282
+ time.sleep(1)
283
+ result = ssm.get_command_invocation(
284
+ CommandId=command_id,
285
+ InstanceId=instance_id
286
+ )
287
+ if result['Status'] in ['Success', 'Failed']:
288
+ break
289
+
290
+ is_setup_complete = result['Status'] == 'Success' and result['StandardOutputContent'].strip() == 'DONE'
291
+
292
+ return True, is_setup_complete
293
+
294
+ except Exception as e:
295
+ print(f"Error checking instance setup: {e}")
296
+ return False, False
297
+
298
+ def wait_for_setup_completion(self, instance_id: str) -> bool:
299
+ """Wait for instance setup to complete.
300
+
301
+ Args:
302
+ instance_id: EC2 instance ID
303
+
304
+ Returns:
305
+ bool: True if setup completed successfully
306
+ """
307
+ print(f"Checking setup status for instance {instance_id}...")
308
+
309
+ is_running, is_complete = self.check_instance_setup(instance_id)
310
+
311
+ if not is_running:
312
+ print("Error: Instance is not running or not accessible via SSM")
313
+ return False
314
+
315
+ if is_complete:
316
+ print("Instance setup is already complete")
317
+ return True
318
+
319
+ wait = input("Instance setup is not complete. Would you like to wait? (Y/n): ").lower() != 'n'
320
+ if not wait:
321
+ return False
322
+
323
+ print("Waiting for setup to complete", end='', flush=True)
324
+ while True:
325
+ is_running, is_complete = self.check_instance_setup(instance_id)
326
+
327
+ if not is_running:
328
+ print("\nError: Instance is no longer running or accessible")
329
+ return False
330
+
331
+ if is_complete:
332
+ print("\nInstance setup completed successfully")
333
+ return True
334
+
335
+ print(".", end='', flush=True)
336
+ time.sleep(10)
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 easytocloud
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,253 @@
1
+ Metadata-Version: 2.2
2
+ Name: cloudx-proxy
3
+ Version: 0.1.1
4
+ Summary: SSH proxy command to connect VSCode with Cloud9/CloudX instance using AWS Systems Manager
5
+ Author-email: easytocloud <info@easytocloud.com>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 easytocloud
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/easytocloud/cloudX-proxy
29
+ Project-URL: Repository, https://github.com/easytocloud/cloudX-proxy
30
+ Project-URL: Issues, https://github.com/easytocloud/cloudX-proxy/issues
31
+ Project-URL: Changelog, https://github.com/easytocloud/cloudX-proxy/blob/main/CHANGELOG.md
32
+ Keywords: aws,vscode,cloud9,cloudX,ssm,ssh,proxy
33
+ Classifier: Development Status :: 5 - Production/Stable
34
+ Classifier: Environment :: Console
35
+ Classifier: Intended Audience :: Developers
36
+ Classifier: License :: OSI Approved :: MIT License
37
+ Classifier: Operating System :: OS Independent
38
+ Classifier: Programming Language :: Python :: 3
39
+ Classifier: Programming Language :: Python :: 3.8
40
+ Classifier: Programming Language :: Python :: 3.9
41
+ Classifier: Programming Language :: Python :: 3.10
42
+ Classifier: Programming Language :: Python :: 3.11
43
+ Classifier: Topic :: Software Development :: Build Tools
44
+ Classifier: Topic :: System :: Systems Administration
45
+ Requires-Python: >=3.8
46
+ Description-Content-Type: text/markdown
47
+ License-File: LICENSE
48
+ Requires-Dist: boto3>=1.34.0
49
+ Requires-Dist: click>=8.1.0
50
+
51
+ # cloudX-proxy
52
+
53
+ A cross-platform SSH proxy command for connecting VSCode to CloudX/Cloud9 EC2 instances using AWS Systems Manager Session Manager.
54
+
55
+ ## Overview
56
+
57
+ cloudX-proxy enables seamless SSH connections from VSCode to EC2 instances using AWS Systems Manager Session Manager, eliminating the need for direct SSH access or public IP addresses. It handles:
58
+
59
+ - Automatic instance startup if stopped
60
+ - SSH key distribution via EC2 Instance Connect
61
+ - SSH tunneling through AWS Systems Manager
62
+ - Cross-platform support (Windows, macOS, Linux)
63
+
64
+ ## Prerequisites
65
+
66
+ 1. **AWS CLI v2** - [Installation Guide](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
67
+ 2. **AWS Session Manager Plugin** - [Installation Guide](https://docs.aws.amazon.com/systems-manager/latest/userguide/session-manager-working-with-install-plugin.html)
68
+ 3. **OpenSSH Client**
69
+ - Windows: [Microsoft's OpenSSH Installation Guide](https://learn.microsoft.com/en-us/windows-server/administration/openssh/openssh_install_firstuse?tabs=gui)
70
+ - macOS/Linux: Usually pre-installed
71
+ 4. **uv** - Python package installer and resolver
72
+ ```bash
73
+ pip install uv
74
+ ```
75
+ 5. **VSCode with Remote SSH Extension** installed
76
+
77
+ ## AWS Credentials Setup
78
+
79
+ The proxy expects to find AWS credentials in a profile named 'vscode' by default. These credentials should be the Access Key and Secret Key that were created by deploying the cloudX-user stack in your AWS account. The cloudX-user stack creates an IAM user with the minimal permissions required for:
80
+ - Starting/stopping EC2 instances
81
+ - Establishing SSM sessions
82
+ - Pushing SSH keys via EC2 Instance Connect
83
+
84
+ Once the SSH session is established, the user has to further configure the instance using `generate-sso-config` tool. This is a one-time setup unless the user's access to AWS accounts changes, in which case the user should re-run the `generate-sso-config` tool.
85
+
86
+ It is recommended to use --generate-directories and --use-ou-structure to create working directories for each account the user has access to.
87
+
88
+ Everytime the user connects to the instance, `ssostart` will authenticate the user with AWS SSO and generate temporary credentials.
89
+
90
+ This ensures you have the appropriate AWS access both for connecting to the instance and for working within it.
91
+
92
+ The proxy also supports easytocloud's AWS profile organizer. If you use multiple AWS environments, you can store your AWS configuration and credentials in `~/.aws/aws-envs/<environment>` directories and use the `--aws-env` option to specify which environment to use.
93
+
94
+ ## Setup
95
+
96
+ cloudX-proxy now includes a setup command that automates the entire configuration process:
97
+
98
+ ```bash
99
+ # Basic setup with defaults (vscode profile and key)
100
+ uvx cloudx-proxy setup
101
+
102
+ # Setup with custom profile and key
103
+ uvx cloudx-proxy setup --profile myprofile --ssh-key mykey
104
+
105
+ # Setup with AWS environment
106
+ uvx cloudx-proxy setup --aws-env prod
107
+ ```
108
+
109
+ The setup command will:
110
+
111
+ 1. Configure AWS Profile:
112
+ - Creates/validates AWS profile with cloudX-{env}-{user} format
113
+ - Supports AWS environment directories via --aws-env
114
+ - Uses aws configure for credential input
115
+
116
+ 2. Manage SSH Keys:
117
+ - Creates new SSH key pair if needed
118
+ - Offers 1Password integration options:
119
+ * Using 1Password SSH agent
120
+ * Storing private key as 1Password document
121
+
122
+ 3. Configure SSH:
123
+ - Creates ~/.ssh/vscode/config with proper settings
124
+ - Sets up environment-specific configurations
125
+ - Configures ProxyCommand with all necessary parameters
126
+ - Ensures main ~/.ssh/config includes the configuration
127
+
128
+ 4. Verify Instance Setup:
129
+ - Checks instance setup status
130
+ - Offers to wait for setup completion
131
+ - Monitors setup progress
132
+
133
+ ### Example SSH Configuration
134
+
135
+ The setup command generates a configuration structure like this:
136
+
137
+ ```
138
+ # Base environment config (created once per environment)
139
+ Host cloudx-{env}-*
140
+ User ec2-user
141
+ IdentityAgent ~/.1password/agent.sock # If using 1Password
142
+ IdentityFile ~/.ssh/vscode/key.pub # .pub for 1Password, no .pub otherwise
143
+ IdentitiesOnly yes # If using 1Password
144
+ ProxyCommand uvx cloudx-proxy connect %h %p --profile profile --aws-env env
145
+
146
+ # Host entries (added for each instance)
147
+ Host cloudx-{env}-hostname
148
+ HostName i-1234567890
149
+ ```
150
+
151
+ When adding new instances to an existing environment, the setup command will only add the specific host entry, preserving the existing environment configuration.
152
+
153
+ ### VSCode Configuration
154
+
155
+ 1. Install the "Remote - SSH" extension in VSCode
156
+ 2. Configure VSCode settings:
157
+ ```json
158
+ {
159
+ "remote.SSH.configFile": "~/.ssh/vscode/config",
160
+ "remote.SSH.connectTimeout": 90
161
+ }
162
+ ```
163
+
164
+ ## Usage
165
+
166
+ ### Command Line
167
+
168
+ ```bash
169
+ # Setup new environment and instance
170
+ uvx cloudx-proxy setup --profile myprofile --aws-env prod
171
+
172
+ # Add instance to existing environment
173
+ uvx cloudx-proxy setup --profile myprofile --aws-env prod
174
+
175
+ # Connect to instance
176
+ uvx cloudx-proxy connect i-0123456789abcdef0 22 --profile myprofile --aws-env prod
177
+
178
+ # Connect with custom port
179
+ uvx cloudx-proxy connect i-0123456789abcdef0 2222 --profile myprofile
180
+
181
+ # Connect with different region
182
+ uvx cloudx-proxy connect i-0123456789abcdef0 22 --region us-east-1
183
+
184
+ # Connect with custom key
185
+ uvx cloudx-proxy connect i-0123456789abcdef0 22 --key-path ~/.ssh/custom_key.pub
186
+ ```
187
+
188
+ ### VSCode
189
+
190
+ 1. Click the "Remote Explorer" icon in the VSCode sidebar
191
+ 2. Select "SSH Targets" from the dropdown
192
+ 3. Your configured hosts will appear (e.g., cloudx-dev)
193
+ 4. Click the "+" icon next to a host to connect
194
+ 5. VSCode will handle the rest, using cloudX-proxy to establish the connection
195
+
196
+ ## AWS Permissions
197
+
198
+ The AWS user/role needs these permissions:
199
+
200
+ ```json
201
+ {
202
+ "Version": "2012-10-17",
203
+ "Statement": [
204
+ {
205
+ "Effect": "Allow",
206
+ "Action": [
207
+ "ec2:StartInstances",
208
+ "ec2:DescribeInstances",
209
+ "ssm:StartSession",
210
+ "ssm:DescribeInstanceInformation",
211
+ "ec2-instance-connect:SendSSHPublicKey"
212
+ ],
213
+ "Resource": "*"
214
+ }
215
+ ]
216
+ }
217
+ ```
218
+
219
+ ## Troubleshooting
220
+
221
+ 1. **Setup Issues**
222
+ - If AWS profile validation fails, ensure your user ARN matches the cloudX-{env}-{user} format
223
+ - For 1Password integration, ensure the CLI is installed and you're signed in
224
+ - Check that ~/.ssh/vscode directory has proper permissions (700)
225
+ - Verify main ~/.ssh/config is writable
226
+
227
+ 2. **Connection Timeout**
228
+ - Ensure the instance has the SSM agent installed and running
229
+ - Check that your AWS credentials have the required permissions
230
+ - Verify the instance ID is correct
231
+ - Increase the VSCode SSH timeout if needed
232
+
233
+ 3. **SSH Key Issues**
234
+ - If using 1Password SSH agent, verify agent is running (~/.1password/agent.sock exists)
235
+ - Check file permissions (600 for private key, 644 for public key)
236
+ - Verify the public key is being successfully pushed to the instance
237
+ - For stored keys in 1Password, ensure you can access them via the CLI
238
+
239
+ 4. **AWS Configuration**
240
+ - Confirm AWS CLI is configured with valid credentials
241
+ - Default region is eu-west-1 if not specified in profile or command line
242
+ - If using AWS profile organizer, ensure your environment directory exists at `~/.aws/aws-envs/<environment>/`
243
+ - Verify the Session Manager plugin is installed correctly
244
+ - Check that the instance has the required IAM role for SSM
245
+
246
+ 5. **Instance Setup Status**
247
+ - If setup appears stuck, check /home/ec2-user/.install-running exists
248
+ - Verify /home/ec2-user/.install-done is created upon completion
249
+ - Check instance system logs for setup script errors
250
+
251
+ ## License
252
+
253
+ MIT License - see LICENSE file for details
@@ -0,0 +1,11 @@
1
+ cloudx_proxy/__init__.py,sha256=ZZ2O_m9OFJm18AxMSuYJt4UjSuSqyJlYRaZMoets498,61
2
+ cloudx_proxy/_version.py,sha256=PKIMyjdUACH4-ONvtunQCnYE2UhlMfp9su83e3HXl5E,411
3
+ cloudx_proxy/cli.py,sha256=zqOIynqNr76JE6VRF7ifn3Fr0Z5C6CjnAHQgupF30BU,3374
4
+ cloudx_proxy/core.py,sha256=j6CUKdg2Ikcoi-05ceXMGA_c1aGWBhN9_JevbkLkaUY,7383
5
+ cloudx_proxy/setup.py,sha256=5GGfPgIiEWwkFEY-HnnPvT_8iD94WqjoFjJ5tGSHz8o,13157
6
+ cloudx_proxy-0.1.1.dist-info/LICENSE,sha256=i7P2OR4zsJYsMWcCUDe_B9ZfGi9bU0K5I2nKfDrW_N8,1068
7
+ cloudx_proxy-0.1.1.dist-info/METADATA,sha256=tBwYeyqWuPOsREgWQylDRn6je6FZ1REbBQu5Pgk6I1s,10216
8
+ cloudx_proxy-0.1.1.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
9
+ cloudx_proxy-0.1.1.dist-info/entry_points.txt,sha256=HGt743N2lVlKd7O1qWq3C0aEHyS5PjPnxzDHh7hwtSg,54
10
+ cloudx_proxy-0.1.1.dist-info/top_level.txt,sha256=2wtEote1db21j-VvkCJFfT-dLlauuG5indjggYh3xDg,13
11
+ cloudx_proxy-0.1.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (75.8.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ cloudx-proxy = cloudx_proxy.cli:cli
@@ -0,0 +1 @@
1
+ cloudx_proxy