workspaces 0.1.17 → 0.1.19
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/DESIGN.md +23 -21
- data/README.md +103 -8
- data/lib/workspaces/child_env.rb +6 -2
- data/lib/workspaces/cli.rb +18 -4
- data/lib/workspaces/config.rb +4 -0
- data/lib/workspaces/coordinator.rb +28 -0
- data/lib/workspaces/dashboard/app.js +21 -5
- data/lib/workspaces/dashboard/card.html.erb +42 -0
- data/lib/workspaces/dashboard/environment.js +196 -54
- data/lib/workspaces/dashboard/styles.css +172 -85
- data/lib/workspaces/dashboard/template.html.erb +17 -5
- data/lib/workspaces/dashboard_view.rb +165 -27
- data/lib/workspaces/environment_overrides.rb +103 -4
- data/lib/workspaces/front_door.rb +130 -9
- data/lib/workspaces/presentation.rb +1 -1
- data/lib/workspaces/registry.rb +26 -0
- data/lib/workspaces/vault/file_vault.rb +174 -0
- data/lib/workspaces/vault.rb +137 -0
- data/lib/workspaces/version.rb +1 -1
- metadata +5 -2
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require 'erb'
|
|
2
2
|
require 'json'
|
|
3
|
+
require 'time'
|
|
3
4
|
require_relative 'config'
|
|
4
5
|
require_relative 'presentation'
|
|
5
6
|
require_relative 'duration_format'
|
|
@@ -13,22 +14,42 @@ module Workspaces
|
|
|
13
14
|
def self.index(workspaces)
|
|
14
15
|
view = new({})
|
|
15
16
|
view.document('Workspaces', <<~HTML)
|
|
16
|
-
<main class="dashboard"><header class="dashboard__header"><h1>Workspaces</h1></header>
|
|
17
|
-
<section class="status-panel"><h2>Create workspace</h2>
|
|
18
|
-
<p>
|
|
17
|
+
<main class="dashboard"><header class="dashboard__header"><h1>Workspaces</h1><p class="dashboard__note">#{view.inventory_summary(workspaces)}</p></header>
|
|
18
|
+
<section class="status-panel" aria-labelledby="create-heading"><h2 id="create-heading">Create workspace</h2>
|
|
19
|
+
<p>Pick an existing branch, a PR number, or a new branch. Each submission creates its own managed worktree.</p>
|
|
19
20
|
<form method="post" action="/workspaces/create" class="stack">
|
|
20
|
-
<
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
21
|
+
<div class="create-fields">
|
|
22
|
+
<div class="field"><label for="create-branch">Existing branch</label><input id="create-branch" name="branch" placeholder="main" autocomplete="off" spellcheck="false"></div>
|
|
23
|
+
<div class="field"><label for="create-pr">PR number</label><input id="create-pr" name="pr" inputmode="numeric" placeholder="43828" autocomplete="off"></div>
|
|
24
|
+
<div class="field"><label for="create-new-branch">New branch</label><input id="create-new-branch" name="new_branch" placeholder="agent/fix-checkout" autocomplete="off" spellcheck="false"></div>
|
|
25
|
+
<div class="field"><label for="create-from">Start new branch from</label><input id="create-from" name="from" placeholder="HEAD" autocomplete="off" spellcheck="false"></div>
|
|
26
|
+
</div>
|
|
27
|
+
<button type="submit" class="button button--primary">Create workspace</button>
|
|
25
28
|
</form></section>
|
|
26
|
-
<section class="status-panel"><h2>Managed workspaces</h2>
|
|
29
|
+
<section class="status-panel" aria-labelledby="inventory-heading"><h2 id="inventory-heading">Managed workspaces</h2>
|
|
27
30
|
#{view.inventory_table(workspaces)}
|
|
28
31
|
</section></main>
|
|
29
32
|
HTML
|
|
30
33
|
end
|
|
31
34
|
|
|
35
|
+
# The default page for a workspace: one small card with the headline, progress and the single
|
|
36
|
+
# next action. Everything else lives on the settings page.
|
|
37
|
+
def self.card(snapshot, preview: false)
|
|
38
|
+
new(snapshot, preview: preview).render_card
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
PREVIEW_STATUS_KEYS = %w(state busy ready headline summary progress lead_action current_preset sleep open).freeze
|
|
42
|
+
PREVIEW_ERROR = 'Setup failed. Open settings for the full log.'.freeze
|
|
43
|
+
|
|
44
|
+
# What the card polls for on a preview host, where the workspace's own pages share the origin.
|
|
45
|
+
# Only what the card draws: no log, no steps, no environment and no error text.
|
|
46
|
+
def self.preview_status(snapshot)
|
|
47
|
+
view = Presentation.new(snapshot).to_h
|
|
48
|
+
slim = view.slice(*PREVIEW_STATUS_KEYS).merge('error' => view['error'] ? PREVIEW_ERROR : '')
|
|
49
|
+
{ 'presentation' => slim, 'prepare_started_at' => snapshot['prepare_started_at'],
|
|
50
|
+
'completed_at' => snapshot['completed_at'] }
|
|
51
|
+
end
|
|
52
|
+
|
|
32
53
|
def self.pull_request(number, workspaces)
|
|
33
54
|
new({}).render_pull_request(number, workspaces)
|
|
34
55
|
end
|
|
@@ -51,8 +72,9 @@ module Workspaces
|
|
|
51
72
|
SVG
|
|
52
73
|
end
|
|
53
74
|
|
|
54
|
-
def initialize(snapshot)
|
|
75
|
+
def initialize(snapshot, preview: false)
|
|
55
76
|
@snapshot = snapshot
|
|
77
|
+
@preview = preview
|
|
56
78
|
@presentation = Presentation.new(snapshot).to_h
|
|
57
79
|
end
|
|
58
80
|
|
|
@@ -63,6 +85,17 @@ module Workspaces
|
|
|
63
85
|
encoding: 'UTF-8')).result(binding))
|
|
64
86
|
end
|
|
65
87
|
|
|
88
|
+
# On a preview host the card is served from the workspace's own origin, so its controls go to the
|
|
89
|
+
# front door's reserved /__workspaces/ endpoints, it never shows error text, and it reloads the
|
|
90
|
+
# page the visitor asked for once the workspace is ready.
|
|
91
|
+
def render_card
|
|
92
|
+
@id = Config.validate_id!(@snapshot.fetch('workspace_id'))
|
|
93
|
+
@presentation = @presentation.merge('error' => PREVIEW_ERROR) if @preview && @presentation['error']
|
|
94
|
+
document("Workspace #{@id}",
|
|
95
|
+
ERB.new(File.read(File.join(__dir__, 'dashboard/card.html.erb'),
|
|
96
|
+
encoding: 'UTF-8')).result(binding))
|
|
97
|
+
end
|
|
98
|
+
|
|
66
99
|
def render_pull_request(number, workspaces)
|
|
67
100
|
@number = number
|
|
68
101
|
@workspaces = workspaces
|
|
@@ -98,25 +131,56 @@ module Workspaces
|
|
|
98
131
|
return '<p class="muted">No workspaces yet.</p>' if workspaces.empty?
|
|
99
132
|
|
|
100
133
|
<<~HTML
|
|
134
|
+
<div class="table-scroll">
|
|
101
135
|
<table class="workspace-table">
|
|
102
|
-
<thead><tr><th scope="col">Workspace</th><th scope="col">Status</th><th scope="col" class="workspace-table__date">Created</th><th scope="col" class="workspace-table__date">Last startup</th></tr></thead>
|
|
136
|
+
<thead><tr><th scope="col">Workspace</th><th scope="col">Status</th><th scope="col" class="workspace-table__date">Created (UTC)</th><th scope="col" class="workspace-table__date">Last startup (UTC)</th></tr></thead>
|
|
103
137
|
<tbody>
|
|
104
138
|
#{workspaces.map { |workspace| inventory_row(workspace) }.join}
|
|
105
139
|
</tbody>
|
|
106
140
|
</table>
|
|
141
|
+
</div>
|
|
107
142
|
HTML
|
|
108
143
|
end
|
|
109
144
|
|
|
145
|
+
# "8 managed worktrees · none running", counted from the data on the page.
|
|
146
|
+
def inventory_summary(workspaces)
|
|
147
|
+
total = workspaces.length
|
|
148
|
+
running = workspaces.count { |workspace| workspace['running'] }
|
|
149
|
+
noun = total == 1 ? 'managed worktree' : 'managed worktrees'
|
|
150
|
+
"#{total} #{noun} · #{running.zero? ? 'none running' : "#{running} running"}"
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# "Oct 27, 10:48" in UTC, with the full value kept in the title. Anything that is not a timestamp is
|
|
154
|
+
# shown as it came rather than guessed at.
|
|
155
|
+
def short_time(value)
|
|
156
|
+
Time.iso8601(value.to_s).utc.strftime('%b %-d, %H:%M')
|
|
157
|
+
rescue ArgumentError
|
|
158
|
+
value.to_s
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def time_cell(value, empty)
|
|
162
|
+
return "<span class=\"never\">#{empty}</span>" if value.nil? || value.to_s.empty?
|
|
163
|
+
|
|
164
|
+
"<time datetime=\"#{escape(value)}\" title=\"#{escape(value)}\">#{escape(short_time(value))}</time>"
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# The word shown in a status pill. What the workspace is doing is the word; whether a server is
|
|
168
|
+
# up is the dot's job elsewhere, so there is no repeated "Not running ·" prefix.
|
|
169
|
+
def status_text(workspace)
|
|
170
|
+
status = (workspace['status'] || 'idle').to_s
|
|
171
|
+
status = status.capitalize
|
|
172
|
+
workspace['active'] ? "#{status} (busy)" : status
|
|
173
|
+
end
|
|
174
|
+
|
|
110
175
|
def inventory_row(workspace)
|
|
111
176
|
id = escape(Config.validate_id!(workspace.fetch('id')))
|
|
112
177
|
status = escape(workspace['status'] || 'idle')
|
|
113
|
-
running = workspace['running'] ? 'Running' : 'Not running'
|
|
114
178
|
<<~HTML
|
|
115
179
|
<tr>
|
|
116
180
|
<th scope="row"><a href="/workspaces/#{id}">#{id}</a></th>
|
|
117
|
-
<td><span class="status-pill" data-status="#{status}"><span class="status-dot" aria-hidden="true"></span>#{
|
|
118
|
-
<td class="workspace-table__date"
|
|
119
|
-
<td class="workspace-table__date"
|
|
181
|
+
<td><span class="status-pill" data-status="#{status}" data-busy="#{workspace['active'] ? 'true' : 'false'}"><span class="status-dot" aria-hidden="true"></span>#{escape(status_text(workspace))}</span></td>
|
|
182
|
+
<td class="workspace-table__date">#{time_cell(workspace['created_at'], 'Unavailable')}</td>
|
|
183
|
+
<td class="workspace-table__date">#{time_cell(workspace['started_at'], 'Never')}</td>
|
|
120
184
|
</tr>
|
|
121
185
|
HTML
|
|
122
186
|
end
|
|
@@ -138,28 +202,86 @@ module Workspaces
|
|
|
138
202
|
keys.map(&:to_s).sort
|
|
139
203
|
end
|
|
140
204
|
|
|
141
|
-
|
|
205
|
+
# The rows the settings form starts with: visible variables with their values, then secrets by
|
|
206
|
+
# name only. A secret row never carries a value, so there is nothing to leak into the page.
|
|
207
|
+
def environment_rows
|
|
142
208
|
values = @snapshot['editable_environment']
|
|
143
|
-
|
|
209
|
+
plain = values.is_a?(Hash) ? values.sort.map { |name, value| { 'name' => name.to_s, 'value' => value.to_s, 'secret' => false } } : []
|
|
210
|
+
names = @snapshot['secret_names']
|
|
211
|
+
secrets = names.is_a?(Array) ? names.map(&:to_s).sort.map { |name| { 'name' => name, 'value' => '', 'secret' => true } } : []
|
|
212
|
+
plain + secrets
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
# What a preset loads: its rows in the same shape. Secrets are untouched by presets.
|
|
216
|
+
def environment_preset_rows
|
|
217
|
+
environment_presets_raw.transform_values do |entries|
|
|
218
|
+
entries.sort.map { |name, value| { 'name' => name.to_s, 'value' => value.to_s } }
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# One row of the settings form. Plain rows show their value. Secret rows have a password field,
|
|
223
|
+
# never pre-filled, with a placeholder saying a value is stored. The secret checkbox is the only
|
|
224
|
+
# thing that decides which store a row goes to, and it is disabled for a stored secret so a
|
|
225
|
+
# saved secret cannot be quietly turned into a visible variable.
|
|
226
|
+
# Keeps password managers from offering to fill, save or generate anything for these inputs. No
|
|
227
|
+
# single attribute does it: `autocomplete="off"` is ignored on password fields, so each manager's
|
|
228
|
+
# own opt-out goes alongside it (1Password, LastPass, Bitwarden).
|
|
229
|
+
NO_AUTOFILL = 'autocomplete="off" data-1p-ignore data-lpignore="true" data-bwignore'
|
|
230
|
+
|
|
231
|
+
# One row of the settings form.
|
|
232
|
+
#
|
|
233
|
+
# A stored secret has no value input at all: just its name, a note, and a Replace button that
|
|
234
|
+
# adds the input when it is clicked. So the page holds no password field for a password manager
|
|
235
|
+
# to find until someone asks to replace one. (Without script, the input sits inside <noscript> so
|
|
236
|
+
# a secret can still be replaced.) A row posted with no value keeps what is stored.
|
|
237
|
+
#
|
|
238
|
+
# The secret checkbox is disabled for a stored secret, with a hidden field carrying it, so a
|
|
239
|
+
# saved secret cannot be quietly turned into a visible variable.
|
|
240
|
+
def env_row_html(index, row)
|
|
241
|
+
secret = row['secret']
|
|
242
|
+
stored = secret && !row['name'].to_s.empty?
|
|
243
|
+
<<~HTML.delete("\n")
|
|
244
|
+
<div class="env-row" data-env-row#{secret ? ' data-secret="true"' : ''}#{stored ? ' data-stored="true"' : ''}>
|
|
245
|
+
<input class="env-row__name" name="env[#{index}][name]" value="#{escape(row['name'])}" placeholder="NAME" aria-label="Variable name" spellcheck="false" #{NO_AUTOFILL} autocapitalize="off">
|
|
246
|
+
#{stored ? stored_value_html(index) : value_input_html(index, row)}
|
|
247
|
+
<label class="env-row__secret"><input type="checkbox" name="env[#{index}][secret]" value="1" data-env-secret#{secret ? ' checked' : ''}#{stored ? ' disabled' : ''}> Secret</label>
|
|
248
|
+
#{stored ? "<input type=\"hidden\" name=\"env[#{index}][secret]\" value=\"1\">" : ''}
|
|
249
|
+
<button type="button" class="button button--subtle button--small" data-env-remove-row aria-label="Remove variable" hidden>Remove</button>
|
|
250
|
+
</div>
|
|
251
|
+
HTML
|
|
252
|
+
end
|
|
144
253
|
|
|
145
|
-
|
|
254
|
+
def value_input_html(index, row, placeholder: nil)
|
|
255
|
+
secret = row['secret']
|
|
256
|
+
placeholder ||= secret ? 'Value' : 'value'
|
|
257
|
+
%(<input class="env-row__value" name="env[#{index}][value]" type="#{secret ? 'password' : 'text'}" value="#{secret ? '' : escape(row['value'])}" placeholder="#{escape(placeholder)}" aria-label="Value" spellcheck="false" #{NO_AUTOFILL} autocapitalize="off" data-env-value>)
|
|
146
258
|
end
|
|
147
259
|
|
|
148
|
-
def
|
|
260
|
+
def stored_value_html(index)
|
|
261
|
+
'<span class="env-row__stored" data-env-stored><span class="muted">Stored. Write-only.</span> ' \
|
|
262
|
+
'<button type="button" class="button button--subtle button--small" data-env-replace hidden>Replace</button></span>' \
|
|
263
|
+
"<noscript>#{value_input_html(index, { 'secret' => true }, placeholder: 'New value (leave blank to keep)')}</noscript>"
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def environment_presets_raw
|
|
149
267
|
presets = @snapshot['environment_presets']
|
|
150
268
|
return {} unless presets.is_a?(Hash)
|
|
151
269
|
|
|
152
|
-
presets.
|
|
153
|
-
|
|
154
|
-
next unless entries.is_a?(Hash)
|
|
270
|
+
presets.select { |name, entries| name.respond_to?(:to_s) && entries.is_a?(Hash) }.transform_keys(&:to_s)
|
|
271
|
+
end
|
|
155
272
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
273
|
+
def vault_status
|
|
274
|
+
vault = @snapshot['vault']
|
|
275
|
+
vault.is_a?(Hash) ? vault : { 'available' => false, 'problem' => nil }
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# The preset names and values the buttons are built from.
|
|
279
|
+
def environment_presets
|
|
280
|
+
environment_presets_raw
|
|
159
281
|
end
|
|
160
282
|
|
|
161
283
|
def environment_presets_json
|
|
162
|
-
JSON.generate(
|
|
284
|
+
JSON.generate(environment_preset_rows)
|
|
163
285
|
end
|
|
164
286
|
|
|
165
287
|
# The lead's primary action. `open` is rendered separately as a link (it needs the
|
|
@@ -167,10 +289,26 @@ module Workspaces
|
|
|
167
289
|
def lead_action_form(action)
|
|
168
290
|
return '' unless action && action['kind'] == 'post'
|
|
169
291
|
|
|
170
|
-
"<form method=\"post\" action=\"
|
|
292
|
+
"<form method=\"post\" action=\"#{action_base}/#{action.fetch('key')}\">" \
|
|
171
293
|
"<button class=\"button button--primary button--large\">#{escape(action.fetch('label'))}</button></form>"
|
|
172
294
|
end
|
|
173
295
|
|
|
296
|
+
def action_base
|
|
297
|
+
@preview ? '/__workspaces' : "/workspaces/#{@id}"
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def settings_href
|
|
301
|
+
@preview ? Config.settings_url(@id) : "/workspaces/#{@id}/settings"
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Where the card's start form posts. A shared card with presets saves the chosen one through the
|
|
305
|
+
# environment form; a preview card starts through its own origin.
|
|
306
|
+
def start_form_action
|
|
307
|
+
return "#{action_base}/start" if @preview || environment_presets.empty?
|
|
308
|
+
|
|
309
|
+
"/workspaces/#{@id}/environment"
|
|
310
|
+
end
|
|
311
|
+
|
|
174
312
|
def action_form(action)
|
|
175
313
|
style = action['primary'] ? 'button--primary' : 'button--subtle'
|
|
176
314
|
"<form method=\"post\" action=\"/workspaces/#{@id}/#{action.fetch('key')}\">" \
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'json'
|
|
4
4
|
require 'securerandom'
|
|
5
|
+
require_relative 'vault'
|
|
5
6
|
|
|
6
7
|
module Workspaces
|
|
7
8
|
class EnvironmentOverrides
|
|
@@ -13,6 +14,7 @@ module Workspaces
|
|
|
13
14
|
MAX_ENTRIES = 100
|
|
14
15
|
MAX_VALUE_BYTES = 8192
|
|
15
16
|
MAX_BODY_BYTES = 128 * 1024
|
|
17
|
+
MAX_ROWS = 200
|
|
16
18
|
|
|
17
19
|
def self.validate_entries!(entries)
|
|
18
20
|
raise Invalid, 'Environment overrides must be a mapping' unless entries.is_a?(Hash)
|
|
@@ -45,12 +47,26 @@ module Workspaces
|
|
|
45
47
|
@home = home
|
|
46
48
|
end
|
|
47
49
|
|
|
50
|
+
# Everything a step should see: saved variables and secrets. Raises Vault::Error when secrets
|
|
51
|
+
# exist but cannot be read, so a run fails with a reason instead of starting without them.
|
|
48
52
|
def load
|
|
53
|
+
load_plain.merge(secret_values)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# The variables that are safe to show. No secrets, and no vault access.
|
|
57
|
+
def load_plain
|
|
49
58
|
load_file(file_path).merge(load_file(editable_file_path))
|
|
50
59
|
end
|
|
51
60
|
|
|
52
61
|
def keys
|
|
53
|
-
|
|
62
|
+
(load_plain.keys + secret_names).uniq.sort
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Names only, never values. Empty when the vault cannot be opened.
|
|
66
|
+
def secret_names
|
|
67
|
+
secret_values.keys.sort
|
|
68
|
+
rescue Vault::Error
|
|
69
|
+
[]
|
|
54
70
|
end
|
|
55
71
|
|
|
56
72
|
def editable_values(defaults)
|
|
@@ -72,7 +88,28 @@ module Workspaces
|
|
|
72
88
|
raise Invalid, 'Secret-like environment names cannot be stored in the visible editor'
|
|
73
89
|
end
|
|
74
90
|
persist_editable(entries, updated, defaults)
|
|
75
|
-
|
|
91
|
+
load_plain
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Saves the settings form: a list of rows, each { 'name', 'value', 'secret' }. Plain rows are
|
|
95
|
+
# stored as editable settings. Secret rows go to the vault and are write-only: a secret row
|
|
96
|
+
# with no value keeps the value already stored, and a secret that is no longer listed is removed.
|
|
97
|
+
#
|
|
98
|
+
# Nothing is changed unless every row is valid and the vault can be used. Messages name
|
|
99
|
+
# variables, never values.
|
|
100
|
+
def replace_rows(rows, defaults:)
|
|
101
|
+
raise Invalid, 'Environment rows must be a list' unless rows.is_a?(Array)
|
|
102
|
+
raise Invalid, "Environment rows are limited to #{MAX_ROWS}" if rows.length > MAX_ROWS
|
|
103
|
+
|
|
104
|
+
self.class.validate_entries!(defaults)
|
|
105
|
+
plain, secrets = split_rows(rows)
|
|
106
|
+
self.class.validate_entries!(plain)
|
|
107
|
+
self.class.validate_entries!(secrets.compact)
|
|
108
|
+
raise Invalid, 'Environment overrides are limited to 100 entries' if plain.length + secrets.length > MAX_ENTRIES
|
|
109
|
+
|
|
110
|
+
write_secrets(secrets)
|
|
111
|
+
persist_editable(plain, plain.reject { |name, value| defaults[name] == value }, defaults)
|
|
112
|
+
load_plain
|
|
76
113
|
end
|
|
77
114
|
|
|
78
115
|
def apply_preset(name, recipe: Recipe.new)
|
|
@@ -111,9 +148,12 @@ module Workspaces
|
|
|
111
148
|
updated
|
|
112
149
|
end
|
|
113
150
|
|
|
151
|
+
# Removes saved variables and secrets. Returns the secrets that could not be removed (see
|
|
152
|
+
# Vault.purge_everywhere), empty when everything went.
|
|
114
153
|
def clear!
|
|
115
154
|
delete_file!
|
|
116
155
|
delete_file!(editable_file_path)
|
|
156
|
+
Vault.purge_everywhere(workspace_id)
|
|
117
157
|
end
|
|
118
158
|
|
|
119
159
|
def scrub(value)
|
|
@@ -261,10 +301,69 @@ module Workspaces
|
|
|
261
301
|
raise Invalid, 'Environment storage does not allow symlinks' if path.symlink?
|
|
262
302
|
end
|
|
263
303
|
|
|
304
|
+
# Filtering still hides every secret that can be read, even when something else is wrong.
|
|
264
305
|
def scrub_values
|
|
265
|
-
|
|
266
|
-
|
|
306
|
+
values = [safely { load_plain.values }, safely { secret_values.values }].flatten
|
|
307
|
+
values.reject(&:empty?).uniq.sort_by { |value| -value.bytesize }
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def safely
|
|
311
|
+
yield
|
|
312
|
+
rescue Invalid, Vault::Error
|
|
267
313
|
[]
|
|
268
314
|
end
|
|
315
|
+
|
|
316
|
+
def secret_values
|
|
317
|
+
Vault.provider.read(workspace_id)
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
def split_rows(rows)
|
|
321
|
+
plain = {}
|
|
322
|
+
secrets = {}
|
|
323
|
+
rows.each do |row|
|
|
324
|
+
raise Invalid, 'Each environment row needs a name and a value' unless row.is_a?(Hash)
|
|
325
|
+
|
|
326
|
+
name = row['name'].to_s.strip
|
|
327
|
+
value = row['value'].to_s
|
|
328
|
+
next if name.empty? && value.empty?
|
|
329
|
+
|
|
330
|
+
raise Invalid, 'Every variable needs a name' if name.empty?
|
|
331
|
+
raise Invalid, "#{name} is listed more than once" if plain.key?(name) || secrets.key?(name)
|
|
332
|
+
|
|
333
|
+
if [true, '1', 'true', 'on'].include?(row['secret'])
|
|
334
|
+
validate_name!(name)
|
|
335
|
+
secrets[name] = value.empty? ? nil : value
|
|
336
|
+
else
|
|
337
|
+
validate_plain_row!(name, value)
|
|
338
|
+
plain[name] = value
|
|
339
|
+
end
|
|
340
|
+
end
|
|
341
|
+
[plain, secrets]
|
|
342
|
+
end
|
|
343
|
+
|
|
344
|
+
def validate_plain_row!(name, value)
|
|
345
|
+
validate_name!(name)
|
|
346
|
+
validate_value!(value)
|
|
347
|
+
raise Invalid, "#{name} looks like a secret: tick Secret so it is stored write-only" if sensitive_name?(name)
|
|
348
|
+
raise Invalid, "#{name}: values must be a single line" if value.match?(/[\r\n]/)
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
# `submitted` maps each secret's name to its new value, or nil when the row was left blank. A blank
|
|
352
|
+
# row keeps the stored value. Existing secrets are never replaced blindly: if the vault cannot be
|
|
353
|
+
# read (wrong or missing key) an edit is refused rather than overwriting what is stored.
|
|
354
|
+
def write_secrets(submitted)
|
|
355
|
+
vault = Vault.provider
|
|
356
|
+
unless vault.available?
|
|
357
|
+
raise Vault::Unavailable, vault.problem unless submitted.empty?
|
|
358
|
+
|
|
359
|
+
return
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
stored = vault.read(workspace_id)
|
|
363
|
+
result = submitted.to_h do |name, value|
|
|
364
|
+
[name, value || stored.fetch(name) { raise Invalid, "#{name} is a new secret and needs a value" }]
|
|
365
|
+
end
|
|
366
|
+
vault.write(workspace_id, result) unless result == stored
|
|
367
|
+
end
|
|
269
368
|
end
|
|
270
369
|
end
|
|
@@ -12,13 +12,19 @@ require_relative 'sleep_policy'
|
|
|
12
12
|
module Workspaces
|
|
13
13
|
class FrontDoor
|
|
14
14
|
WORKSPACE_ROUTE = %r{\A/workspaces/([a-z0-9]+(?:-[a-z0-9]+)*)
|
|
15
|
-
(?:/(status|start|prepare|restart|resume|stop|remove|update-pr|environment|keep-awake|allow-sleep))?\z}x
|
|
15
|
+
(?:/(status|start|prepare|restart|resume|stop|remove|update-pr|environment|keep-awake|allow-sleep|settings))?\z}x
|
|
16
16
|
|
|
17
17
|
# Rack env keys set on requests that reach a ready workspace backend, for use by
|
|
18
18
|
# middleware registered with Workspaces.configure_proxy.
|
|
19
19
|
WORKSPACE_ID_ENV = 'workspaces.workspace_id'.freeze
|
|
20
20
|
WORKSPACE_ENV = 'workspaces.workspace'.freeze
|
|
21
21
|
|
|
22
|
+
# Paths under this prefix on a preview host belong to the front door, never to the workspace's app.
|
|
23
|
+
# They serve the waiting card's status and its few lifecycle posts (see DashboardView.card).
|
|
24
|
+
RESERVED_PREFIX = '/__workspaces/'.freeze
|
|
25
|
+
RESERVED_ROUTE = %r{\A/__workspaces/(status|start|prepare|restart|resume)\z}
|
|
26
|
+
private_constant :RESERVED_PREFIX, :RESERVED_ROUTE
|
|
27
|
+
|
|
22
28
|
WAKE_POLL_SECONDS = 0.25
|
|
23
29
|
RETRY_AFTER_SECONDS = 5
|
|
24
30
|
private_constant :WAKE_POLL_SECONDS, :RETRY_AFTER_SECONDS
|
|
@@ -99,7 +105,7 @@ module Workspaces
|
|
|
99
105
|
|
|
100
106
|
def action_request(request, id, action)
|
|
101
107
|
return update_environment(request, id) if action == 'environment'
|
|
102
|
-
return status_response(request, id, action) if action.nil? || action
|
|
108
|
+
return status_response(request, id, action) if action.nil? || %w(status settings).include?(action)
|
|
103
109
|
return response(405, 'Use POST') unless request.post?
|
|
104
110
|
return response(403, 'Actions require the configured dashboard origin') unless same_origin?(request)
|
|
105
111
|
return remove(request, id) if action == 'remove'
|
|
@@ -116,8 +122,10 @@ module Workspaces
|
|
|
116
122
|
return response(400, 'Unknown workspace log') unless %w(setup backend).include?(source)
|
|
117
123
|
|
|
118
124
|
snapshot = @coordinator.snapshot(id, log: source)
|
|
119
|
-
|
|
120
|
-
|
|
125
|
+
return response(200, JSON.generate(snapshot), type: 'application/json', head: request.head?) if action == 'status'
|
|
126
|
+
|
|
127
|
+
page = action == 'settings' ? DashboardView.render(snapshot) : DashboardView.card(snapshot)
|
|
128
|
+
response(200, page, type: 'text/html', head: request.head?)
|
|
121
129
|
end
|
|
122
130
|
|
|
123
131
|
# Retries from the first unfinished step by default; `from=<step>` re-runs that step and every
|
|
@@ -148,7 +156,11 @@ module Workspaces
|
|
|
148
156
|
return response(403, 'Actions require the configured dashboard origin') unless same_origin?(request)
|
|
149
157
|
|
|
150
158
|
params = environment_params(request)
|
|
151
|
-
updated = if params.key?('
|
|
159
|
+
updated = if params.key?('preset')
|
|
160
|
+
start_with_preset(id, params['preset'])
|
|
161
|
+
elsif params.key?('env')
|
|
162
|
+
@coordinator.replace_environment_rows(id, rows: parse_environment_rows(params))
|
|
163
|
+
elsif params.key?('editable_env')
|
|
152
164
|
@coordinator.replace_editable_environment(id, text: parse_editable_environment(params))
|
|
153
165
|
else
|
|
154
166
|
@coordinator.update_environment(id, **parse_environment_changes(params))
|
|
@@ -158,6 +170,16 @@ module Workspaces
|
|
|
158
170
|
redirect(Config.dashboard_url(id))
|
|
159
171
|
rescue EnvironmentOverrides::Invalid => e
|
|
160
172
|
response(422, e.message)
|
|
173
|
+
rescue Vault::Error => e
|
|
174
|
+
response(422, e.message)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# The card's start form for a new workspace: one preset (or the defaults, when left blank).
|
|
178
|
+
def start_with_preset(id, preset)
|
|
179
|
+
raise EnvironmentOverrides::Invalid, 'Choose a preset by name' unless preset.is_a?(String)
|
|
180
|
+
return @coordinator.apply_environment_preset(id, name: preset) unless preset.empty?
|
|
181
|
+
|
|
182
|
+
@coordinator.start(id)
|
|
161
183
|
end
|
|
162
184
|
|
|
163
185
|
def remove(request, id)
|
|
@@ -167,18 +189,23 @@ module Workspaces
|
|
|
167
189
|
return response(413, 'Deletion confirmation is too large') if request.content_length.to_i > 512
|
|
168
190
|
return response(422, 'Type the exact workspace ID to confirm deletion') unless request.POST['confirm_id'] == id
|
|
169
191
|
|
|
170
|
-
@registry.remove(id, force: true)
|
|
192
|
+
leftover = @registry.remove(id, force: true)
|
|
171
193
|
@tracker.forget(id)
|
|
172
|
-
redirect("#{Config.public_origin}/workspaces")
|
|
194
|
+
return redirect("#{Config.public_origin}/workspaces") if !leftover.is_a?(Array) || leftover.empty?
|
|
195
|
+
|
|
196
|
+
# The workspace is gone, but its secrets are not, and saying "removed" would be untrue.
|
|
197
|
+
response(500, "The workspace was removed, but its stored secrets could not be: #{leftover.join('; ')}. " \
|
|
198
|
+
'Fix that, then run `workspaces purge-secrets`.')
|
|
173
199
|
rescue ArgumentError, Worktree::CommandFailedError, SystemCallError
|
|
174
200
|
response(409, 'Workspace was not removed. Check the server logs and retry when safe.')
|
|
175
201
|
end
|
|
176
202
|
|
|
203
|
+
# Update from PR is a settings-page control and reports its outcome there.
|
|
177
204
|
def update_pr(id)
|
|
178
205
|
@registry.update_pr(id)
|
|
179
|
-
redirect(Config.
|
|
206
|
+
redirect(Config.settings_url(id))
|
|
180
207
|
rescue Worktree::UpdateError
|
|
181
|
-
redirect(Config.
|
|
208
|
+
redirect(Config.settings_url(id))
|
|
182
209
|
end
|
|
183
210
|
|
|
184
211
|
def create(request)
|
|
@@ -204,6 +231,24 @@ module Workspaces
|
|
|
204
231
|
{ set: normalize_set(params['set']), remove: normalize_remove(params['remove']) }
|
|
205
232
|
end
|
|
206
233
|
|
|
234
|
+
# The form posts env[0][name], env[0][value] and env[0][secret] for each row. Rows are taken in
|
|
235
|
+
# the order of their numbers; anything that is not a mapping of strings is refused.
|
|
236
|
+
def parse_environment_rows(params)
|
|
237
|
+
rows = params['env']
|
|
238
|
+
raise EnvironmentOverrides::Invalid, 'Environment rows must be a numbered list' unless rows.is_a?(Hash)
|
|
239
|
+
unless rows.keys.all? { |key| key.to_s.match?(/\A\d{1,4}\z/) }
|
|
240
|
+
raise EnvironmentOverrides::Invalid, 'Environment rows must be a numbered list'
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
rows.sort_by { |key, _| key.to_i }.map do |_, row|
|
|
244
|
+
unless row.is_a?(Hash) && row.values.all?(String)
|
|
245
|
+
raise EnvironmentOverrides::Invalid, 'Each environment row needs a name and a value'
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
row.slice('name', 'value', 'secret')
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
207
252
|
def parse_editable_environment(params)
|
|
208
253
|
unless params['editable_env'].is_a?(String)
|
|
209
254
|
raise EnvironmentOverrides::Invalid, 'Environment editor requires one editable_env field'
|
|
@@ -252,6 +297,7 @@ module Workspaces
|
|
|
252
297
|
|
|
253
298
|
def proxy(id, request)
|
|
254
299
|
return response(404, 'Unknown managed workspace') unless Worktree.new(id).exists?
|
|
300
|
+
return reserved(id, request) if request.path_info.start_with?(RESERVED_PREFIX)
|
|
255
301
|
|
|
256
302
|
counted = !@sleep_policy.ignored?(request.path_info)
|
|
257
303
|
lease = acquire_lease(id, counted)
|
|
@@ -272,6 +318,7 @@ module Workspaces
|
|
|
272
318
|
phase = @coordinator.sleep_phase(id)
|
|
273
319
|
if phase
|
|
274
320
|
return sleeping_unavailable(id) unless counted
|
|
321
|
+
return waiting_card(id, phase) if navigation?(request)
|
|
275
322
|
|
|
276
323
|
outcome = await_wake(id, phase)
|
|
277
324
|
return wake_failed(id) if outcome == :failed
|
|
@@ -356,11 +403,85 @@ module Workspaces
|
|
|
356
403
|
end
|
|
357
404
|
|
|
358
405
|
def unavailable(id, request)
|
|
406
|
+
return card_response(@coordinator.snapshot(id, include_log: false)) if navigation?(request)
|
|
359
407
|
return redirect(Config.dashboard_url(id)) if safe?(request)
|
|
360
408
|
|
|
361
409
|
response(503, "Workspace not ready. Visit #{Config.dashboard_url(id)}")
|
|
362
410
|
end
|
|
363
411
|
|
|
412
|
+
# A person opening a page in a browser, as opposed to the app's own fetches, assets and API calls.
|
|
413
|
+
# Only they are shown the card; everything else keeps the plain behaviour.
|
|
414
|
+
def navigation?(request)
|
|
415
|
+
return false unless request.get?
|
|
416
|
+
return false unless request.get_header('HTTP_ACCEPT').to_s.include?('text/html')
|
|
417
|
+
return false if request.get_header('HTTP_X_REQUESTED_WITH')
|
|
418
|
+
|
|
419
|
+
mode = request.get_header('HTTP_SEC_FETCH_MODE')
|
|
420
|
+
mode.nil? || mode == 'navigate'
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
# A sleeping workspace is woken at once and the visitor sees the card, not a held request, so the
|
|
424
|
+
# address they asked for stays in the bar and the card reloads it when the workspace is ready.
|
|
425
|
+
def waiting_card(id, phase)
|
|
426
|
+
@coordinator.wake(id) if phase == :sleeping
|
|
427
|
+
card_response(@coordinator.snapshot(id, include_log: false))
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
def card_response(snapshot)
|
|
431
|
+
response(200, DashboardView.card(snapshot, preview: true), type: 'text/html')
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# The waiting card's own endpoints. Not counted as activity (polling must not keep a workspace awake),
|
|
435
|
+
# never forwarded, and limited to what the card does: report status and start, retry or continue.
|
|
436
|
+
def reserved(id, request)
|
|
437
|
+
match = RESERVED_ROUTE.match(request.path_info)
|
|
438
|
+
return response(404, 'Not found') unless match
|
|
439
|
+
|
|
440
|
+
action = match[1]
|
|
441
|
+
return preview_status(id, request) if action == 'status'
|
|
442
|
+
return response(405, 'Use POST') unless request.post?
|
|
443
|
+
return response(403, 'Actions require this workspace origin') unless preview_same_origin?(request, id)
|
|
444
|
+
|
|
445
|
+
preview_action(id, action, request)
|
|
446
|
+
redirect(preview_return_url(request, id))
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
def preview_status(id, request)
|
|
450
|
+
return response(405, 'Use GET or HEAD') unless safe?(request)
|
|
451
|
+
|
|
452
|
+
body = JSON.generate(DashboardView.preview_status(@coordinator.snapshot(id, include_log: false)))
|
|
453
|
+
response(200, body, type: 'application/json', head: request.head?)
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
def preview_action(id, action, request)
|
|
457
|
+
preset = request.POST['preset'] if action == 'start' && request.media_type == 'application/x-www-form-urlencoded'
|
|
458
|
+
return start_with_preset(id, preset) if preset.is_a?(String)
|
|
459
|
+
|
|
460
|
+
perform_action(id, action, request)
|
|
461
|
+
end
|
|
462
|
+
|
|
463
|
+
def preview_same_origin?(request, id)
|
|
464
|
+
expected = Config.preview_url(id).chomp('/')
|
|
465
|
+
origin = request.get_header('HTTP_ORIGIN')
|
|
466
|
+
return origin == expected if origin
|
|
467
|
+
|
|
468
|
+
uri = URI.parse(request.referer.to_s)
|
|
469
|
+
uri.is_a?(URI::HTTP) && uri.userinfo.nil? && "#{uri.scheme}://#{uri.authority}" == expected
|
|
470
|
+
rescue URI::InvalidURIError
|
|
471
|
+
false
|
|
472
|
+
end
|
|
473
|
+
|
|
474
|
+
# Back to the page the visitor was on, so a start or retry does not lose their place.
|
|
475
|
+
def preview_return_url(request, id)
|
|
476
|
+
base = Config.preview_url(id).chomp('/')
|
|
477
|
+
uri = URI.parse(request.referer.to_s)
|
|
478
|
+
path = uri.request_uri if uri.is_a?(URI::HTTP) && "#{uri.scheme}://#{uri.authority}" == base
|
|
479
|
+
path = '/' if path.nil? || path.start_with?(RESERVED_PREFIX)
|
|
480
|
+
"#{base}#{path}"
|
|
481
|
+
rescue URI::InvalidURIError
|
|
482
|
+
"#{base}/"
|
|
483
|
+
end
|
|
484
|
+
|
|
364
485
|
# Innermost app of the proxy stack: sends the request to the workspace backend.
|
|
365
486
|
def forward_to_backend(env)
|
|
366
487
|
forward(env.fetch(WORKSPACE_ID_ENV), Rack::Request.new(env))
|