sshkit-ec2instanceconnect 0.2.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: a74af1e0cbbe916dd6322012215e20ab8d786b3e00bbb87dfe445b3b07fb15f1
4
- data.tar.gz: f53f6fe60fa792a6cc8b84a659e66805748dc85e6281836295b03eb631782c42
3
+ metadata.gz: 4194c123a03b9896006890812057f7ef9ccb1cffbc57acb82f299b3b826dee2e
4
+ data.tar.gz: f81f0405509c32fd8de3c192784ec7cf6850eb1269e0672fc479173164818d6a
5
5
  SHA512:
6
- metadata.gz: d4001376f708e7465119572bb635f61154e2e1b6d604d6e81bb5a8bc19438af07b80aefbe271907a360066120a489c7501b688d9db4c14f915660755cd4b53b4
7
- data.tar.gz: 4c123c65362619651bfaebae8a49fe583501a91cd0afb4962a2d527dcf13441dda67e150a699c173a530e563965a1ddeb3ac261cad5669795ba45345fee88e6c
6
+ metadata.gz: 3a223746da42ec63c6f9785a9c8ea26a8f93caaacda0a60d8404ad587851036c813253ae2d1c84d06a58287aeef027e5a671ebd78681d2367677f8c21265402c
7
+ data.tar.gz: 12503c8de4632e66928f803db047ff79c4f0912d18acbfb81bd7fa4f29122d64f90995248c1ab6ca66a45295036b71a7977078bbfa9a4bb5e66a133e595b5451
@@ -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,13 +76,25 @@ 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
 
@@ -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.2.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.2.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-07-17 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