sshkit-ec2instanceconnect 0.1.0 → 0.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4457259f1ad225bf3974f58d58c4b77e5141b94c66506423101dc874438e13d0
4
- data.tar.gz: 2b98209f24ec5f55837b4e8e1f790e0fc1f47dc909083dd52c3d80396d059c23
3
+ metadata.gz: 4194c123a03b9896006890812057f7ef9ccb1cffbc57acb82f299b3b826dee2e
4
+ data.tar.gz: f81f0405509c32fd8de3c192784ec7cf6850eb1269e0672fc479173164818d6a
5
5
  SHA512:
6
- metadata.gz: 2a754e8d2ffb490deb94b01fe7378d8fcd10d02722d406882d2cb95266e6af8335325a01ef3af690cfe3dea2dd73343b94d4639423f1abf47837d7e5c8aede91
7
- data.tar.gz: c9c8696b44a6c310031b5981c3406309d6e68ca296ba3ae1e7a8cd61ad65ed257e6ae1da4095154d46d4cd00006e741484dda1e33dcd0ac50191d640d4d992a2
6
+ metadata.gz: 3a223746da42ec63c6f9785a9c8ea26a8f93caaacda0a60d8404ad587851036c813253ae2d1c84d06a58287aeef027e5a671ebd78681d2367677f8c21265402c
7
+ data.tar.gz: 12503c8de4632e66928f803db047ff79c4f0912d18acbfb81bd7fa4f29122d64f90995248c1ab6ca66a45295036b71a7977078bbfa9a4bb5e66a133e595b5451
data/README.md CHANGED
@@ -67,6 +67,8 @@ config.tunnel_enabled = true
67
67
 
68
68
  **Note**: Tunneling requires the AWS CLI (`aws ec2-instance-connect`) to be installed and configured.
69
69
 
70
+ Since the host's `hostname`/`port` are overwritten to point at the local tunnel, the original values remain available on `host.properties.fetch(:original_hostname)` / `host.properties.fetch(:original_port)` for logging or diagnostics.
71
+
70
72
  ## IAM Policy
71
73
 
72
74
  Sending ephemeral SSH keys and opening tunnels require the correct IAM permissions.
@@ -17,6 +17,9 @@ module SSHKit
17
17
  # so refresh them a bit earlier.
18
18
  EXPIRES_IN = 50
19
19
 
20
+ # Number of attempts to start a tunnel when the chosen port is already in use.
21
+ TUNNEL_START_ATTEMPTS = 3
22
+
20
23
  # Store SSH keys per instance with expiration.
21
24
  @ssh_keys = SSHKit::EC2InstanceConnect::TimedHash.new(expires_in: EXPIRES_IN)
22
25
 
@@ -73,21 +76,44 @@ module SSHKit
73
76
  end
74
77
  end
75
78
 
79
+ # Start a tunnel on a random local port, retrying with another port if it is already in use.
76
80
  def setup_tunnel
77
81
  logger.debug { "Setting up tunnel for instance #{instance_id}" }
78
82
 
79
- Tunnel.new(instance_id: instance_id, port: random_tunnel_port).tap do |tunnel|
80
- tunnel.start
83
+ attempts = 0
84
+
85
+ begin
86
+ attempts += 1
87
+
88
+ Tunnel.new(instance_id: instance_id, port: random_tunnel_port).tap do |tunnel|
89
+ tunnel.start
90
+
91
+ logger.debug { "Tunnel (#{tunnel}) started for instance #{instance_id}" }
92
+ end
93
+ rescue TunnelError => e
94
+ raise unless e.message.include?('Address already in use') && attempts < TUNNEL_START_ATTEMPTS
81
95
 
82
- logger.debug { "Tunnel (#{tunnel}) started for instance #{instance_id}" }
96
+ logger.debug { "Tunnel port already in use for instance #{instance_id}, retrying with another port" }
97
+ retry
83
98
  end
84
99
  end
85
100
 
86
101
  def update_host_and_port(tunnel:)
102
+ set_host_property_once(:original_hostname, host.hostname)
103
+ set_host_property_once(:original_port, host.port)
104
+
87
105
  host.hostname = tunnel.host
88
106
  host.port = tunnel.port
89
107
  end
90
108
 
109
+ # Sets a host property only if it hasn't been set before, so the original
110
+ # value is preserved. host.properties isn't a Hash, so use #keys instead of #key?.
111
+ def set_host_property_once(key, value)
112
+ return if host.properties.keys.include?(key) # rubocop:disable Performance/InefficientHashSearch
113
+
114
+ host.properties.set(key, value)
115
+ end
116
+
91
117
  def refresh_ssh_key
92
118
  logger.debug { "Refreshing SSH key for instance #{instance_id}" }
93
119
 
@@ -4,11 +4,16 @@ require 'pty'
4
4
 
5
5
  module SSHKit
6
6
  module EC2InstanceConnect
7
+ # Raised when the tunnel process exits or times out before listening for connections.
8
+ class TunnelError < StandardError; end
9
+
7
10
  # Manages a tunnel using the EC2 Instance Connect Endpoint Service, for secure SSH access to EC2 instances.
8
11
  class Tunnel
12
+ START_TIMEOUT = 30
13
+
9
14
  attr_reader :host, :port
10
15
 
11
- def initialize(instance_id:, host: 'localhost', port: '8000')
16
+ def initialize(instance_id:, host: '127.0.0.1', port: '8000')
12
17
  @instance_id = instance_id
13
18
  @host = host
14
19
  @port = port
@@ -16,36 +21,36 @@ module SSHKit
16
21
 
17
22
  # Start the tunnel subprocess and wait until it is listening for connections.
18
23
  def start
19
- mutex = Mutex.new
20
- condition = ConditionVariable.new
24
+ queue = Thread::Queue.new
25
+ output = []
21
26
  ready = false
22
- error = nil
23
27
 
24
28
  @thread = Thread.new do
25
29
  PTY.spawn(command) do |stdout, _stdin, pid|
26
30
  @pid = pid
27
31
 
32
+ # Keep draining stdout after startup so the PTY buffer doesn't fill up, but stop collecting output.
28
33
  stdout.each_line do |line|
29
- next unless line.start_with?('Listening for connections')
34
+ next if ready
30
35
 
31
- mutex.synchronize do
32
- ready = true
33
- condition.signal
34
- end
36
+ output << line
37
+ ready = line.start_with?('Listening for connections')
38
+ queue << :ready if ready
35
39
  end
36
40
  rescue Errno::EIO
37
41
  # PTY closed — safe to ignore
38
42
  end
39
- rescue Errno::ENOENT => e
40
- mutex.synchronize do
41
- error = e
42
- condition.signal
43
- end
43
+
44
+ queue << TunnelError.new("Tunnel for instance #{@instance_id} exited before listening on #{self}: #{output.join.strip}") unless ready
45
+ rescue SystemCallError => e
46
+ queue << e
44
47
  end
45
48
 
46
- mutex.synchronize { condition.wait(mutex) until ready || error }
49
+ result = queue.pop(timeout: START_TIMEOUT)
50
+ return if result == :ready
47
51
 
48
- raise error if error
52
+ stop
53
+ raise result || TunnelError.new("Tunnel for instance #{@instance_id} did not start listening on #{self} within #{START_TIMEOUT}s")
49
54
  end
50
55
 
51
56
  # Stop the tunnel process.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SSHKit
4
4
  module EC2InstanceConnect
5
- VERSION = '0.1.0'
5
+ VERSION = '0.3.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sshkit-ec2instanceconnect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kentaa
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2026-01-05 00:00:00.000000000 Z
12
+ date: 2026-11-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk-ec2instanceconnect
@@ -143,14 +143,14 @@ dependencies:
143
143
  requirements:
144
144
  - - "~>"
145
145
  - !ruby/object:Gem::Version
146
- version: '0.22'
146
+ version: '1.0'
147
147
  type: :development
148
148
  prerelease: false
149
149
  version_requirements: !ruby/object:Gem::Requirement
150
150
  requirements:
151
151
  - - "~>"
152
152
  - !ruby/object:Gem::Version
153
- version: '0.22'
153
+ version: '1.0'
154
154
  - !ruby/object:Gem::Dependency
155
155
  name: webmock
156
156
  requirement: !ruby/object:Gem::Requirement