workspaces 0.1.9 → 0.1.10
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 +4 -4
- data/README.md +59 -0
- data/lib/workspaces/cli.rb +4 -1
- data/lib/workspaces/front_door.rb +15 -1
- data/lib/workspaces/proxy_middleware.rb +77 -0
- data/lib/workspaces/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1974af7e7acc6d19fe556efa4af5e2f9e05635d8df564aa6b3e7e7f8a9096370
|
|
4
|
+
data.tar.gz: d819cb05a41259c8f9b0c7e49d42155f7b072daa88bd839b44351ddec7445b51
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ad1115622e9f3c85fb12c63ba8268161054cca32675a081b6d43b5a11b1b5f1a55c0491a143631277c12aa2d019e10eb228acfcdb26fecce719a45f0336c8f3f
|
|
7
|
+
data.tar.gz: 236ca0df90dc5e0403fca4d494bcbeabf90117aabbd12c12175d3eecf4d2475fc36e432430e3ece7c61c651188c234daeef041ed148812d80ef71e983fda5afe
|
data/README.md
CHANGED
|
@@ -143,6 +143,64 @@ The front door is not an authentication boundary. Its host and same-origin check
|
|
|
143
143
|
protect routing and POST actions, but a publicly reachable instance still needs
|
|
144
144
|
access control such as VPN or SSO. Do not expose workspace backend ports directly.
|
|
145
145
|
|
|
146
|
+
### Proxy middleware
|
|
147
|
+
|
|
148
|
+
The front door can run Rack middleware around traffic that is forwarded to a workspace
|
|
149
|
+
backend. The gem knows nothing about what a middleware does: the container or host
|
|
150
|
+
that runs `serve` registers its own. Typical uses are injecting a script tag, adding a
|
|
151
|
+
header, or logging.
|
|
152
|
+
|
|
153
|
+
```ruby
|
|
154
|
+
# my_middleware.rb
|
|
155
|
+
Workspaces.configure_proxy do
|
|
156
|
+
use MyMiddleware, option: 'value'
|
|
157
|
+
end
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Load it with `serve --require FILE` (repeatable) or by setting `WORKSPACES_REQUIRE` to
|
|
161
|
+
one or more paths separated like `PATH` entries. Environment entries load first, then
|
|
162
|
+
`--require` files in order. A missing file or an error while loading stops `serve` with
|
|
163
|
+
a non-zero exit instead of silently running without it. These files are executed with
|
|
164
|
+
the front door's privileges, so only name files the operator controls. With nothing
|
|
165
|
+
registered, the front door behaves exactly as before.
|
|
166
|
+
|
|
167
|
+
Blocks run in registration order and the first `use` is outermost. The stack wraps only
|
|
168
|
+
the step that forwards a request to a ready workspace backend. The dashboard, redirects
|
|
169
|
+
for workspaces that are not ready, and error pages never pass through it. For each
|
|
170
|
+
proxied request, two Rack env keys are available:
|
|
171
|
+
|
|
172
|
+
| Key | Value |
|
|
173
|
+
| --- | --- |
|
|
174
|
+
| `workspaces.workspace_id` | The workspace ID, such as `main-1ae4815f` |
|
|
175
|
+
| `workspaces.workspace` | The workspace snapshot hash (branch, status, related PR, and so on) |
|
|
176
|
+
|
|
177
|
+
Two details matter when rewriting response bodies. The proxy passes the browser's
|
|
178
|
+
`Accept-Encoding` to the backend, so a compressed response arrives compressed; set
|
|
179
|
+
`env['HTTP_ACCEPT_ENCODING'] = 'identity'` before calling the app, or skip responses
|
|
180
|
+
with a `Content-Encoding` header. If you change the body, drop the stale `Content-Length`
|
|
181
|
+
header. An example that appends a script tag to HTML pages:
|
|
182
|
+
|
|
183
|
+
```ruby
|
|
184
|
+
class ScriptInjector
|
|
185
|
+
TAG = '<script src="https://example.test/widget.js" data-thread-id="%<id>s"></script>'
|
|
186
|
+
|
|
187
|
+
def initialize(app)
|
|
188
|
+
@app = app
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def call(env)
|
|
192
|
+
env['HTTP_ACCEPT_ENCODING'] = 'identity'
|
|
193
|
+
status, headers, body = @app.call(env)
|
|
194
|
+
return [status, headers, body] unless headers['content-type'].to_s.start_with?('text/html')
|
|
195
|
+
|
|
196
|
+
html = body.each.to_a.join.sub('</body>', format(TAG, id: env['workspaces.workspace_id']) + '</body>')
|
|
197
|
+
[status, headers.reject { |key, _| key.casecmp?('content-length') }, [html]]
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
Workspaces.configure_proxy { use ScriptInjector }
|
|
202
|
+
```
|
|
203
|
+
|
|
146
204
|
### Local HTTPS
|
|
147
205
|
|
|
148
206
|
To serve TLS directly, install/trust a local CA and create a certificate covering
|
|
@@ -198,6 +256,7 @@ and forwarded scheme/host headers; it does not change the internal listener to T
|
|
|
198
256
|
| `WORKSPACES_PUBLIC_ORIGIN` | `http://localhost:4747` | Browser-facing dashboard origin and scheme |
|
|
199
257
|
| `WORKSPACES_BASE_DOMAIN` | `localhost` | Preview hostname suffix |
|
|
200
258
|
| `WORKSPACES_SUBDOMAIN_PREFIX` | `ws-` | Prefix before the workspace ID on preview hosts |
|
|
259
|
+
| `WORKSPACES_REQUIRE` | none | Ruby files, separated like `PATH`, loaded by `serve` to register proxy middleware |
|
|
201
260
|
|
|
202
261
|
Keep the project root, public origin, preview domain, and storage settings consistent
|
|
203
262
|
across CLI and server processes. Changing the browser-facing origin requires restarting
|
data/lib/workspaces/cli.rb
CHANGED
|
@@ -5,6 +5,7 @@ require 'rack/handler/puma'
|
|
|
5
5
|
require_relative 'registry'
|
|
6
6
|
require_relative 'coordinator'
|
|
7
7
|
require_relative 'front_door'
|
|
8
|
+
require_relative 'proxy_middleware'
|
|
8
9
|
require_relative 'tls'
|
|
9
10
|
require_relative 'child_env'
|
|
10
11
|
|
|
@@ -86,6 +87,7 @@ module Workspaces
|
|
|
86
87
|
end
|
|
87
88
|
|
|
88
89
|
def parse_tls_options(parser, options)
|
|
90
|
+
parser.on('--require PATH') { |value| (options[:require] ||= []) << value }
|
|
89
91
|
parser.on('--secure') { options[:secure] = true }
|
|
90
92
|
parser.on('--tls-cert PATH') { |value| options[:tls_cert] = value }
|
|
91
93
|
parser.on('--tls-key PATH') { |value| options[:tls_key] = value }
|
|
@@ -93,6 +95,7 @@ module Workspaces
|
|
|
93
95
|
|
|
94
96
|
def serve(_id, options)
|
|
95
97
|
tls = Tls.from_serve_options(options)&.validate!
|
|
98
|
+
ProxyMiddleware.load_files(ProxyMiddleware.env_paths + options.fetch(:require, []))
|
|
96
99
|
|
|
97
100
|
if tls
|
|
98
101
|
tls.apply_env_defaults { serve_with_tls(tls) }
|
|
@@ -196,7 +199,7 @@ module Workspaces
|
|
|
196
199
|
end
|
|
197
200
|
|
|
198
201
|
def usage
|
|
199
|
-
'Usage: bin/workspaces serve | create (--branch REF | --pr NUMBER | --new-branch NAME [--from REF]) [--preset NAME] [--set NAME=VALUE] [--unset NAME] [--json] | ' \
|
|
202
|
+
'Usage: bin/workspaces serve [--require FILE] | create (--branch REF | --pr NUMBER | --new-branch NAME [--from REF]) [--preset NAME] [--set NAME=VALUE] [--unset NAME] [--json] | ' \
|
|
200
203
|
'list [--pr NUMBER] [--json] | show/start/prepare/restart ID [--preset NAME] [--set NAME=VALUE] [--unset NAME] [--json] | stop ID | ' \
|
|
201
204
|
'exec ID -- COMMAND... | remove ID [--force]'
|
|
202
205
|
end
|
|
@@ -5,15 +5,22 @@ require 'net/http'
|
|
|
5
5
|
require_relative 'coordinator'
|
|
6
6
|
require_relative 'dashboard_view'
|
|
7
7
|
require_relative 'environment_overrides'
|
|
8
|
+
require_relative 'proxy_middleware'
|
|
8
9
|
|
|
9
10
|
module Workspaces
|
|
10
11
|
class FrontDoor
|
|
11
12
|
WORKSPACE_ROUTE = %r{\A/workspaces/([a-z0-9]+(?:-[a-z0-9]+)*)
|
|
12
13
|
(?:/(status|start|prepare|restart|stop|remove|update-pr|environment))?\z}x
|
|
13
14
|
|
|
15
|
+
# Rack env keys set on requests that reach a ready workspace backend, for use by
|
|
16
|
+
# middleware registered with Workspaces.configure_proxy.
|
|
17
|
+
WORKSPACE_ID_ENV = 'workspaces.workspace_id'.freeze
|
|
18
|
+
WORKSPACE_ENV = 'workspaces.workspace'.freeze
|
|
19
|
+
|
|
14
20
|
def initialize(coordinator: Coordinator.new, registry: Registry.new)
|
|
15
21
|
@coordinator = coordinator
|
|
16
22
|
@registry = registry
|
|
23
|
+
@forwarder = ProxyMiddleware.build(method(:forward_to_backend))
|
|
17
24
|
end
|
|
18
25
|
|
|
19
26
|
def call(env)
|
|
@@ -230,7 +237,9 @@ module Workspaces
|
|
|
230
237
|
snapshot = @coordinator.snapshot(id, include_log: false)
|
|
231
238
|
return unavailable(id, request) unless snapshot['status'] == 'ready' && !snapshot['active']
|
|
232
239
|
|
|
233
|
-
|
|
240
|
+
request.env[WORKSPACE_ID_ENV] = id
|
|
241
|
+
request.env[WORKSPACE_ENV] = snapshot
|
|
242
|
+
@forwarder.call(request.env)
|
|
234
243
|
rescue IOError, SystemCallError, Timeout::Error => e
|
|
235
244
|
response(502, "Workspace backend unavailable: #{e.class}")
|
|
236
245
|
end
|
|
@@ -241,6 +250,11 @@ module Workspaces
|
|
|
241
250
|
response(503, "Workspace not ready. Visit #{Config.dashboard_url(id)}")
|
|
242
251
|
end
|
|
243
252
|
|
|
253
|
+
# Innermost app of the proxy stack: sends the request to the workspace backend.
|
|
254
|
+
def forward_to_backend(env)
|
|
255
|
+
forward(env.fetch(WORKSPACE_ID_ENV), Rack::Request.new(env))
|
|
256
|
+
end
|
|
257
|
+
|
|
244
258
|
def forward(id, request)
|
|
245
259
|
uri = URI("http://127.0.0.1:#{Backend.new(id).port}#{request.fullpath}")
|
|
246
260
|
upstream = upstream_request(id, request, uri)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
require 'rack'
|
|
2
|
+
|
|
3
|
+
module Workspaces
|
|
4
|
+
# Adds a Rack middleware stack around workspace preview traffic.
|
|
5
|
+
#
|
|
6
|
+
# The gem stays unaware of what any middleware does: the process hosting the front
|
|
7
|
+
# door registers its own, for example from a file named by WORKSPACES_REQUIRE:
|
|
8
|
+
#
|
|
9
|
+
# Workspaces.configure_proxy do
|
|
10
|
+
# use MyMiddleware, option: 'value'
|
|
11
|
+
# end
|
|
12
|
+
#
|
|
13
|
+
# Blocks are replayed in registration order into a Rack::Builder, so the first
|
|
14
|
+
# `use` is the outermost middleware. The stack wraps only the step that forwards a
|
|
15
|
+
# request to a ready workspace backend. The dashboard, redirects for workspaces that
|
|
16
|
+
# are not ready, and error responses never pass through it.
|
|
17
|
+
module ProxyMiddleware
|
|
18
|
+
ENV_VAR = 'WORKSPACES_REQUIRE'
|
|
19
|
+
|
|
20
|
+
# Raised for unusable configuration; the CLI reports it and exits non-zero.
|
|
21
|
+
class ConfigurationError < ArgumentError; end
|
|
22
|
+
|
|
23
|
+
@blocks = []
|
|
24
|
+
|
|
25
|
+
def self.register(&block)
|
|
26
|
+
raise ConfigurationError, 'Workspaces.configure_proxy requires a block' unless block
|
|
27
|
+
|
|
28
|
+
@blocks << block
|
|
29
|
+
self
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.any?
|
|
33
|
+
!@blocks.empty?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.reset!
|
|
37
|
+
@blocks.clear
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Returns the endpoint untouched when nothing is registered, so an unconfigured
|
|
41
|
+
# front door behaves exactly as it did before this seam existed.
|
|
42
|
+
def self.build(endpoint)
|
|
43
|
+
blocks = @blocks.dup
|
|
44
|
+
return endpoint if blocks.empty?
|
|
45
|
+
|
|
46
|
+
Rack::Builder.new do
|
|
47
|
+
blocks.each { |block| instance_exec(&block) }
|
|
48
|
+
run endpoint
|
|
49
|
+
end.to_app
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Paths from WORKSPACES_REQUIRE, separated like PATH entries.
|
|
53
|
+
def self.env_paths
|
|
54
|
+
ENV.fetch(ENV_VAR, '').split(File::PATH_SEPARATOR).map(&:strip).reject(&:empty?)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Executes the given Ruby files so they can register middleware. These files run
|
|
58
|
+
# with the privileges of the front door; only name files the operator controls.
|
|
59
|
+
def self.load_files(paths)
|
|
60
|
+
paths.each do |path|
|
|
61
|
+
full_path = File.expand_path(path)
|
|
62
|
+
raise ConfigurationError, "Proxy middleware file not found: #{path}" unless File.file?(full_path)
|
|
63
|
+
|
|
64
|
+
begin
|
|
65
|
+
require full_path
|
|
66
|
+
rescue ScriptError, StandardError => e
|
|
67
|
+
raise ConfigurationError, "Could not load proxy middleware #{path}: #{e.class}: #{e.message}"
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
paths
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def self.configure_proxy(&block)
|
|
75
|
+
ProxyMiddleware.register(&block)
|
|
76
|
+
end
|
|
77
|
+
end
|
data/lib/workspaces/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: workspaces
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.10
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Workspaces contributors
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-10-
|
|
11
|
+
date: 2026-10-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rack
|
|
@@ -88,6 +88,7 @@ files:
|
|
|
88
88
|
- lib/workspaces/paths.rb
|
|
89
89
|
- lib/workspaces/port_allocator.rb
|
|
90
90
|
- lib/workspaces/presentation.rb
|
|
91
|
+
- lib/workspaces/proxy_middleware.rb
|
|
91
92
|
- lib/workspaces/recipe.rb
|
|
92
93
|
- lib/workspaces/registry.rb
|
|
93
94
|
- lib/workspaces/state_store.rb
|