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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4194c123a03b9896006890812057f7ef9ccb1cffbc57acb82f299b3b826dee2e
|
|
4
|
+
data.tar.gz: f81f0405509c32fd8de3c192784ec7cf6850eb1269e0672fc479173164818d6a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
80
|
-
|
|
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
|
|
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: '
|
|
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
|
-
|
|
20
|
-
|
|
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
|
|
34
|
+
next if ready
|
|
30
35
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
49
|
+
result = queue.pop(timeout: START_TIMEOUT)
|
|
50
|
+
return if result == :ready
|
|
47
51
|
|
|
48
|
-
|
|
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.
|
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.
|
|
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-
|
|
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
|