maquina-generators 0.10.0 → 0.11.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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +26 -17
  3. data/lib/generators/maquina/app/app_generator.rb +12 -7
  4. data/lib/generators/maquina/auth_flavor.rb +59 -0
  5. data/lib/generators/maquina/backstage_signin/backstage_signin.rb +94 -19
  6. data/lib/generators/maquina/backstage_signin/templates/test/integration/{backstage_access_test.rb → backstage_access_test.rb.tt} +26 -0
  7. data/lib/generators/maquina/error_context/USAGE +22 -0
  8. data/lib/generators/maquina/error_context/error_context_generator.rb +51 -0
  9. data/lib/generators/maquina/mcp/USAGE +4 -3
  10. data/lib/generators/maquina/mcp/mcp_generator.rb +14 -8
  11. data/lib/generators/maquina/mcp/mcp_sibling_tools.rb +19 -1
  12. data/lib/generators/maquina/mcp/templates/test/test_helpers/{mcp_test_helper.rb → mcp_test_helper.rb.tt} +14 -0
  13. data/lib/generators/maquina/mission_control_jobs/USAGE +5 -3
  14. data/lib/generators/maquina/mission_control_jobs/mission_control_jobs_generator.rb +2 -0
  15. data/lib/generators/maquina/oauth/USAGE +5 -2
  16. data/lib/generators/maquina/oauth/oauth_generator.rb +20 -13
  17. data/lib/generators/maquina/oauth/templates/app/models/access_token.rb +3 -3
  18. data/lib/generators/maquina/oauth/templates/app/models/mcp.rb.tt +12 -0
  19. data/lib/generators/maquina/oauth/templates/app/models/oauth_authorization_code.rb +1 -1
  20. data/lib/generators/maquina/oauth/templates/test/models/{access_token_test.rb → access_token_test.rb.tt} +2 -0
  21. data/lib/generators/maquina/passkeys/USAGE +4 -2
  22. data/lib/generators/maquina/passkeys/passkeys_generator.rb +16 -11
  23. data/lib/generators/maquina/passkeys/templates/app/controllers/session/{passkeys_controller.rb → passkeys_controller.rb.tt} +6 -2
  24. data/lib/generators/maquina/passkeys/templates/config/locales/{passkeys.en.yml → passkeys.en.yml.tt} +15 -0
  25. data/lib/generators/maquina/passkeys/templates/config/locales/{passkeys.es.yml → passkeys.es.yml.tt} +15 -0
  26. data/lib/generators/maquina/passkeys/templates/test/integration/{passkeys_test.rb → passkeys_test.rb.tt} +6 -0
  27. data/lib/generators/maquina/registration/registration_generator.rb +32 -0
  28. data/lib/generators/maquina/registration/templates/app/views/registrations/new.html.erb.tt +4 -0
  29. data/lib/generators/maquina/registration/templates/app/views/sessions/new.html.erb.tt +5 -0
  30. data/lib/generators/maquina/security/USAGE +5 -3
  31. data/lib/generators/maquina/security/security_generator.rb +2 -0
  32. data/lib/generators/maquina/solid_errors/USAGE +9 -4
  33. data/lib/generators/maquina/solid_errors/error_context_files.rb +82 -0
  34. data/lib/generators/maquina/solid_errors/solid_errors_generator.rb +12 -0
  35. data/lib/generators/maquina/solid_errors/templates/app/agents/errors_query.rb +40 -4
  36. data/lib/generators/maquina/solid_errors/templates/app/agents/failure_triage.rb +4 -2
  37. data/lib/generators/maquina/solid_errors/templates/app/agents/list_failures_tool.rb +9 -4
  38. data/lib/generators/maquina/solid_errors/templates/app/models/mcp/tools/solid_errors.rb.tt +24 -6
  39. data/lib/generators/maquina/solid_errors/templates/config/initializers/error_context.rb +33 -0
  40. data/lib/generators/maquina/solid_errors/templates/test/models/mcp/tools/solid_errors_filters_test.rb +68 -0
  41. data/lib/maquina_generators/version.rb +1 -1
  42. data/test/fixtures/error_context/0.10.0/app/agents/errors_query.rb +105 -0
  43. data/test/fixtures/error_context/0.10.0/app/agents/failure_triage.rb +21 -0
  44. data/test/fixtures/error_context/0.10.0/app/agents/list_failures_tool.rb +20 -0
  45. data/test/fixtures/error_context/0.10.0/app/models/mcp/tools/solid_errors.rb +81 -0
  46. data/test/fixtures/error_context/0.10.0/app/models/mcp/tools/solid_errors_write.rb +101 -0
  47. data/test/generators/maquina/app_generator_test.rb +14 -10
  48. data/test/generators/maquina/backstage_signin_test.rb +89 -2
  49. data/test/generators/maquina/error_context_generator_test.rb +188 -0
  50. data/test/generators/maquina/mcp_generator_test.rb +3 -0
  51. data/test/generators/maquina/oauth_generator_test.rb +51 -3
  52. data/test/generators/maquina/passkeys_generator_test.rb +62 -2
  53. data/test/generators/maquina/registration_generator_test.rb +62 -0
  54. data/test/generators/maquina/solid_errors_generator_test.rb +56 -0
  55. data/test/support/rails_auth_app.rb +48 -0
  56. metadata +25 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 490a44073c9b568e54c4b2b83257db4ad4f279ac9e610f022b7556ca81734606
4
- data.tar.gz: dcae3d4b2aa0f3866775c1f162522824614397dd399403c715e38f3fb67cb8cb
3
+ metadata.gz: 4014b01c776f1e37d61dddd592e30a6acac76546082105ad3ca12c8b2233693c
4
+ data.tar.gz: 8eb3326660cd36814d52a6324f3147b2912d1fe412326655dd6bf344e313a9c8
5
5
  SHA512:
6
- metadata.gz: 8dffc54baad6e0b5443de42b77802d2fcd35253ebd82acc155c874d4f254181174a40f82e3cf216c4ba0dcbbd29be0bd50569acccaf17e795e65c1c0de7e1da1
7
- data.tar.gz: 69cf94a55c483bc857d6780d0e89e36f9fb1228959910381ea2e45eb08e07f172f2a6b6676f2b981cdbb61f5c75bf6468e53ed608891eaed459c20fc66d58fb2
6
+ metadata.gz: c547266a74a99a4dd11fd11124ccdd21f59dafe12215e8250060e6e5b11cb6181d6643cd140b1c38262513d9cff3c99889f2bdf3e07437e0479a128d918064b4
7
+ data.tar.gz: 13e67350b2e101c184875128d9c651dc3fd2d7661a1f7ba0ecfb9e50a32b7506de652146b0a9e882e9d17b1a77b12683d9ef0dd150ef6d7eae6cb0557e4a688c
data/README.md CHANGED
@@ -62,9 +62,9 @@ All generated code lives in your app -- edit it directly:
62
62
  - **Email sender:** Edit `app/mailers/verification_mailer.rb`
63
63
  - **Translations:** Edit `config/locales/clave.*.yml`
64
64
 
65
- ### Passkeys -- Passkey Sign-In for Clave
65
+ ### Passkeys -- Passkey Sign-In
66
66
 
67
- **Passkeys** adds WebAuthn passkeys as a second way to sign in, on top of Clave. People add passkeys from a `/passkeys` page and sign in with Face ID, a fingerprint or a device PIN, either with a button or straight from the email field's autofill. The email code stays as the way in without a passkey, so removing every passkey never locks anyone out.
67
+ **Passkeys** adds WebAuthn passkeys as a second way to sign in, beside Clave's email code or Rails' password (`maquina:registration`). People add passkeys from a `/passkeys` page and sign in with Face ID, a fingerprint or a device PIN, either with a button or straight from the email field's autofill. The email code or the password stays as the way in without a passkey, so removing every passkey never locks anyone out.
68
68
 
69
69
  The [`webauthn`](https://github.com/cedarcode/webauthn-ruby) gem handles only the cryptography (attestation and assertion parsing, signature, origin, challenge and user-verification checks). The flow itself, including controllers, model, views and JavaScript, is generated into your app. There is no JavaScript dependency: the Stimulus controllers use the browser's own `PublicKeyCredential.parseCreationOptionsFromJSON` / `parseRequestOptionsFromJSON` / `toJSON()`. The names (`passkeys` table, `credential_id`, `Session::PasskeysController`) follow the passkeys work proposed for Rails itself (rails/rails#57100).
70
70
 
@@ -84,7 +84,7 @@ The [`webauthn`](https://github.com/cedarcode/webauthn-ruby) gem handles only th
84
84
  #### Usage
85
85
 
86
86
  ```bash
87
- rails g maquina:clave # first
87
+ rails g maquina:clave # first (or maquina:registration)
88
88
  rails g maquina:passkeys
89
89
  rails db:migrate
90
90
  ```
@@ -117,7 +117,7 @@ WEBAUTHN_ORIGINS=https://example.com,https://app.example.com
117
117
 
118
118
  ### OAuth -- OAuth 2.1 Sign-In for AI Clients
119
119
 
120
- **OAuth** lets AI clients (the MCP Inspector, Claude, any MCP client) sign people in to your app on top of Clave. It is the authorization server behind `maquina:mcp`. Clients identify themselves with a Client ID Metadata Document (CIMD): the `client_id` is an `https` URL whose JSON names the client and its redirect URIs, so there is no dynamic registration and no client secret. Every flow is PKCE (`S256` only), and people sign in with the email code they already use.
120
+ **OAuth** lets AI clients (the MCP Inspector, Claude, any MCP client) sign people in to your app on top of its own sign-in, Clave's or Rails' (`maquina:registration`). It is the authorization server behind `maquina:mcp`. Clients identify themselves with a Client ID Metadata Document (CIMD): the `client_id` is an `https` URL whose JSON names the client and its redirect URIs, so there is no dynamic registration and no client secret. Every flow is PKCE (`S256` only), and people sign in the way they already do. Tokens and codes go through `Mcp.enabled_for?(user)` in `app/models/mcp.rb`, the gate the app owns; with Clave it turns away a blocked user, and with Rails' sign-in it is where a deactivation check goes.
121
121
 
122
122
  No OAuth gem is added: the endpoints, models and fetcher are generated into your app.
123
123
 
@@ -138,7 +138,7 @@ No OAuth gem is added: the endpoints, models and fetcher are generated into your
138
138
  #### Usage
139
139
 
140
140
  ```bash
141
- rails g maquina:clave # first
141
+ rails g maquina:clave # first (or maquina:registration)
142
142
  rails g maquina:oauth
143
143
  rails db:migrate
144
144
  ```
@@ -206,7 +206,7 @@ Choose the Streamable HTTP transport, enter `http://localhost:3000/mcp` and conn
206
206
 
207
207
  #### Operational tools from other generators
208
208
 
209
- When `app/models/mcp/tools.rb` exists, these generators also write an MCP tool module. Every one is `ADMIN_ONLY`: they read data across every account, so only people on the backstage list (`User#backstage_access?`, see [Backstage access](#backstage-access----clave-sign-in-or-basic-auth)) see or call them. An account's `admin` role is not enough, since registration makes everyone who signs up the admin of their own account. Anyone else's token gets "unknown tool". Lists are capped at 50 rows and times are ISO 8601 UTC.
209
+ When `app/models/mcp/tools.rb` exists, these generators also write an MCP tool module. Every one is `ADMIN_ONLY`: they read data across every account, so only people on the backstage list (`User#backstage_access?`, see [Backstage access](#backstage-access----app-sign-in-or-basic-auth)) see or call them. An account's `admin` role is not enough, since registration makes everyone who signs up the admin of their own account. Anyone else's token gets "unknown tool". Lists are capped at 50 rows and times are ISO 8601 UTC.
210
210
 
211
211
  The tools are read-only unless the generator runs with `--mcp-write`, which adds write tools that need a token with the `write` scope and log each call with the user's id.
212
212
 
@@ -297,14 +297,14 @@ All generated code lives in your app -- edit it directly:
297
297
 
298
298
  ---
299
299
 
300
- ### Backstage Access -- Clave Sign-In or Basic Auth
300
+ ### Backstage Access -- App Sign-In or Basic Auth
301
301
 
302
302
  Solid Errors, Mission Control Jobs and Security share one `BackstageController` and one way in.
303
303
 
304
- **With Clave installed** (`app/models/email_verification.rb` is there), each of them puts the backstage behind the app's own sign-in:
304
+ **With the app's own sign-in installed**, Clave's email code (`app/models/email_verification.rb` is there) or Rails' password (`maquina:registration`: `has_secure_password` and a `Session` model), each of them puts the backstage behind it:
305
305
 
306
306
  - `BackstageController` includes `Authentication` and raises `ActiveRecord::RecordNotFound` unless `Current.user.backstage_access?`. Signed out, a page redirects to the sign-in (303 for PATCH/PUT/DELETE, so the engines' Resolve and Discard buttons are not re-sent); signed in but not listed, it is a 404, so the pages never confirm they exist.
307
- - `User.backstage_emails` and `User#backstage_access?` are added to `app/models/user.rb`. Access is an unblocked user whose email is on the list in credentials:
307
+ - With Clave, `User.backstage_emails` and `User#backstage_access?` are added to `app/models/user.rb`. Access is an unblocked user whose email is on the list in credentials:
308
308
 
309
309
  ```yaml
310
310
  backstage:
@@ -312,12 +312,20 @@ Solid Errors, Mission Control Jobs and Security share one `BackstageController`
312
312
  - you@example.com
313
313
  ```
314
314
 
315
- An empty list is nobody. The same predicate gates the admin-only MCP tools.
315
+ An empty list is nobody.
316
+ - With Rails' sign-in, a migration adds `accounts.allow_backstage` (boolean, off by default) and `User#backstage_access?` is `account.allow_backstage? && admin?`. Signing up makes you the admin of your own account, so the flag, which only the console sets, is what keeps the backstage closed:
317
+
318
+ ```ruby
319
+ Account.find_by!(name: "...").update!(allow_backstage: true)
320
+ ```
321
+
322
+ An app's own `backstage_access?` is kept as it is, and a `User` without `admin?` (no role enum) is left for you to fill in.
323
+ - Either way, the same predicate gates the admin-only MCP tools.
316
324
  - The Basic Auth comes out of the overview, the Security page and both engines' initializers (Mission Control gets `http_basic_auth_enabled = false`). Do not set `backstage.username` or `password`: Solid Errors turns its Basic Auth back on when a password is set.
317
- - `test/integration/backstage_access_test.rb` checks signed out, not listed, blocked, an empty list and a listed user, with `backstage_operator` and `backstage_member` fixtures.
325
+ - `test/integration/backstage_access_test.rb` checks signed out, not listed, blocked, an empty list and a listed user (with Rails' sign-in: signed out, not an admin, the flag off and an admin with it on), with `backstage_operator` and `backstage_member` fixtures.
318
326
  - A generator run later keeps the backstage on the sign-in. A file you changed by hand keeps its Basic Auth, and the post-install message names it.
319
327
 
320
- **Without Clave** (no auth, or `maquina:registration`), the backstage uses HTTP Basic Auth with `backstage.username` / `password` from credentials, or each generator's ENV variables, and answers 503 until they are set.
328
+ **Without a sign-in**, the backstage uses HTTP Basic Auth with `backstage.username` / `password` from credentials, or each generator's ENV variables, and answers 503 until they are set.
321
329
 
322
330
  ---
323
331
 
@@ -328,7 +336,8 @@ Solid Errors, Mission Control Jobs and Security share one `BackstageController`
328
336
  #### What it generates
329
337
 
330
338
  - **BackstageController:** Inherits from `ActionController::Base` (bypasses app's ApplicationController concerns)
331
- - **Initializer:** Database connection config, and Basic Auth from credentials or ENV when the app has no Clave (see [Backstage access](#backstage-access----clave-sign-in-or-basic-auth))
339
+ - **Error context:** `config/initializers/error_context.rb`, a Rails error reporter middleware that tags every reported error with its request id, method, path and the signed-in user's and account's ids (ids only). It reads `Current` when the error is reported, so it sees an account a tenant scope sets late, and covers jobs and controllers outside `ApplicationController`. An app installed before 0.11 gets it from `rails g maquina:error_context`, which changes nothing `maquina:solid_errors` would overwrite and replaces the query and tool files only where they are unedited
340
+ - **Initializer:** Database connection config, and Basic Auth from credentials or ENV when the app has no sign-in (see [Backstage access](#backstage-access----app-sign-in-or-basic-auth))
332
341
  - **Route:** Mounts `SolidErrors::Engine` at `<prefix>/errors`
333
342
  - **Mailer templates:** Self-contained `error_occurred` HTML and text templates, so `send_emails` works without the dashboard's helpers
334
343
  - **Admin navigation:** Shared navigation bar with Overview, Errors, Jobs and Security tabs
@@ -358,14 +367,14 @@ rails g maquina:solid_errors --prefix /backstage \
358
367
 
359
368
  #### Authentication
360
369
 
361
- With Clave, the app's sign-in and the backstage list; see [Backstage access](#backstage-access----clave-sign-in-or-basic-auth). Without it, Basic Auth credentials are resolved in order:
370
+ With the app's sign-in, the backstage gate; see [Backstage access](#backstage-access----app-sign-in-or-basic-auth). Without it, Basic Auth credentials are resolved in order:
362
371
 
363
372
  1. `Rails.application.credentials.backstage.username` / `.password`
364
373
  2. `ENV["SOLID_ERRORS_USER"]` / `ENV["SOLID_ERRORS_PASSWORD"]` (configurable)
365
374
 
366
375
  #### MCP
367
376
 
368
- With `maquina:mcp` installed, it also writes admin-only MCP tools (`list_failures`, `get_failure`, `top_exceptions`) to `app/models/mcp/tools/solid_errors.rb`. `--mcp-write` adds `resolve_failure`, which needs the `write` scope. See [Operational tools from other generators](#operational-tools-from-other-generators). The old `--mcp-http` endpoint is gone; `--agent` still generates `bin/failures` and the stdio `bin/failures-mcp`.
377
+ With `maquina:mcp` installed, it also writes admin-only MCP tools (`list_failures`, `get_failure`, `top_exceptions`) to `app/models/mcp/tools/solid_errors.rb`. `list_failures` takes `user_id`, `account_id` and `since` (ISO 8601; seven days back by default with a user or account), so you can see the errors one person hit. `--mcp-write` adds `resolve_failure`, which needs the `write` scope. See [Operational tools from other generators](#operational-tools-from-other-generators). The old `--mcp-http` endpoint is gone; `--agent` still generates `bin/failures` and the stdio `bin/failures-mcp`.
369
378
 
370
379
  ---
371
380
 
@@ -404,7 +413,7 @@ rails g maquina:mission_control_jobs --prefix /backstage \
404
413
 
405
414
  #### Authentication
406
415
 
407
- With Clave, the app's sign-in and the backstage list; see [Backstage access](#backstage-access----clave-sign-in-or-basic-auth). Without it, Basic Auth credentials are resolved in order:
416
+ With the app's sign-in, the backstage gate; see [Backstage access](#backstage-access----app-sign-in-or-basic-auth). Without it, Basic Auth credentials are resolved in order:
408
417
 
409
418
  1. `Rails.application.credentials.backstage.username` / `.password`
410
419
  2. `ENV["MISSION_CONTROL_JOBS_USER"]` / `ENV["MISSION_CONTROL_JOBS_PASSWORD"]` (configurable)
@@ -525,7 +534,7 @@ rails g maquina:security --prefix /backstage \
525
534
 
526
535
  #### Authentication
527
536
 
528
- With Clave, the app's sign-in and the backstage list; see [Backstage access](#backstage-access----clave-sign-in-or-basic-auth). Without it, HTTP Basic Auth, with credentials resolved in order:
537
+ With the app's sign-in, the backstage gate; see [Backstage access](#backstage-access----app-sign-in-or-basic-auth). Without it, HTTP Basic Auth, with credentials resolved in order:
529
538
 
530
539
  1. `Rails.application.credentials.backstage.username` / `.password`
531
540
  2. `ENV["SECURITY_USER"]` / `ENV["SECURITY_PASSWORD"]` (configurable)
@@ -12,18 +12,18 @@ module Maquina
12
12
  class_option :auth, type: :string, default: "none",
13
13
  desc: "Authentication type: none, clave, or registration"
14
14
  class_option :passkeys, type: :boolean, default: false,
15
- desc: "Add passkey sign-in on top of clave (requires --auth clave)"
15
+ desc: "Add passkey sign-in beside the app's sign-in (requires --auth clave or registration)"
16
16
  class_option :mcp, type: :boolean, default: false,
17
- desc: "Add an OAuth-backed MCP server on top of clave (requires --auth clave)"
17
+ desc: "Add an OAuth-backed MCP server on the app's sign-in (requires --auth clave or registration)"
18
18
  class_option :mcp_flag, type: :string,
19
19
  desc: "Feature flag that gates MCP access (e.g. beta_mcp)"
20
20
 
21
21
  # 0. Check options before touching anything
22
22
  def check_options
23
- return if options[:auth] == "clave"
23
+ return if %w[clave registration].include?(options[:auth])
24
24
 
25
- raise Thor::Error, "--passkeys builds on clave. Run with `--auth clave --passkeys`." if options[:passkeys]
26
- raise Thor::Error, "--mcp builds on clave. Run with `--auth clave --mcp`." if options[:mcp]
25
+ raise Thor::Error, "--passkeys builds on the app's sign-in. Run with `--auth clave` or `--auth registration`." if options[:passkeys]
26
+ raise Thor::Error, "--mcp builds on the app's sign-in. Run with `--auth clave` or `--auth registration`." if options[:mcp]
27
27
  end
28
28
 
29
29
  # 1. Add gems
@@ -361,12 +361,17 @@ module Maquina
361
361
  say "=" * 60, :green
362
362
  say ""
363
363
  say "Next steps:", :yellow
364
- say " 1. Set credentials: bin/rails credentials:edit"
365
- say " backstage:"
366
364
  if options[:auth] == "clave"
365
+ say " 1. Set credentials: bin/rails credentials:edit"
366
+ say " backstage:"
367
367
  say " emails:"
368
368
  say " - you@example.com # who may open #{options[:prefix]} (signed in with Clave)"
369
+ elsif options[:auth] == "registration"
370
+ say " 1. Sign up, then let your account's admins open #{options[:prefix]} (bin/rails console):"
371
+ say " Account.find_by!(name: \"...\").update!(allow_backstage: true)"
369
372
  else
373
+ say " 1. Set credentials: bin/rails credentials:edit"
374
+ say " backstage:"
370
375
  say " username: your_user"
371
376
  say " password: your_password"
372
377
  end
@@ -0,0 +1,59 @@
1
+ module Maquina
2
+ module Generators
3
+ # Which sign-in the app has, for the generators that build on it (oauth,
4
+ # passkeys, mcp and the backstage sign-in):
5
+ #
6
+ # - :clave, maquina:clave's email code. Its User can be blocked (blocked?,
7
+ # block!), and who opens the backstage is a list of emails in credentials.
8
+ # - :rails, Rails' authentication generator (maquina:registration): a
9
+ # password, a Session record and Account-owned users with a role. It has
10
+ # no blocking, and who opens the backstage is an admin of an account with
11
+ # allow_backstage on.
12
+ #
13
+ # Every method is private: a public method on a generator is a Thor step.
14
+ module AuthFlavor
15
+ AUTHENTICATION_CONCERN = "app/controllers/concerns/authentication.rb"
16
+
17
+ private
18
+
19
+ def auth_flavor
20
+ return @auth_flavor if defined?(@auth_flavor)
21
+
22
+ @auth_flavor = if !auth_file?(AUTHENTICATION_CONCERN) then nil
23
+ elsif auth_file?("app/models/email_verification.rb") then :clave
24
+ elsif auth_file?("app/models/session.rb") && auth_file_include?("app/models/user.rb", "has_secure_password") then :rails
25
+ end
26
+ end
27
+
28
+ def clave? = auth_flavor == :clave
29
+
30
+ def rails_auth? = auth_flavor == :rails
31
+
32
+ # The registration generator's `enum :role` (member/admin), which gives
33
+ # User#admin? and the role column a fixture can set.
34
+ def user_role_enum? = auth_file_include?("app/models/user.rb", /enum :role\b/)
35
+
36
+ def require_authentication!(generator)
37
+ return if auth_flavor
38
+
39
+ raise Thor::Error, "maquina:#{generator} needs the app's sign-in. Run `rails g maquina:clave` " \
40
+ "(email code) or `rails g maquina:registration` (password) first."
41
+ end
42
+
43
+ # A fixture user who can sign in: Clave's sign in by email code, Rails'
44
+ # need a password digest, and a role when the User has the enum.
45
+ def auth_fixture_attributes(role:)
46
+ attributes = {}
47
+ attributes["password_digest"] = %(<%= BCrypt::Password.create("password") %>) if rails_auth?
48
+ attributes["role"] = role if clave? || user_role_enum?
49
+ attributes
50
+ end
51
+
52
+ def auth_file?(path) = File.exist?(File.join(destination_root, path))
53
+
54
+ def auth_file_include?(path, text)
55
+ auth_file?(path) && File.read(File.join(destination_root, path)).match?(text)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,13 +1,18 @@
1
+ require "erb"
2
+ require_relative "../auth_flavor"
3
+
1
4
  module Maquina
2
5
  module Generators
3
- # Puts the backstage behind the app's own Clave sign-in instead of the
6
+ # Puts the backstage behind the app's own sign-in instead of the
4
7
  # shared Basic Auth login. The dashboard generators (solid_errors,
5
8
  # mission_control_jobs, security) call install_backstage_signin after they
6
9
  # write their files, so whichever runs, and in whatever order, the backstage
7
- # ends up on the sign-in when Clave is installed.
10
+ # ends up on the sign-in when the app has one (AuthFlavor).
8
11
  #
9
- # Who gets in is User#backstage_access?: an unblocked user whose email is
10
- # listed under backstage.emails in credentials. The same predicate gates
12
+ # Who gets in is User#backstage_access?. With Clave, an unblocked user whose
13
+ # email is listed under backstage.emails in credentials; with Rails'
14
+ # sign-in, an admin of an account whose allow_backstage is on (a column
15
+ # this adds, off by default). The same predicate gates
11
16
  # the admin-only MCP tools, which is why maquina:mcp calls
12
17
  # add_backstage_access_to_user too.
13
18
  #
@@ -17,6 +22,8 @@ module Maquina
17
22
  #
18
23
  # Every method is private: a public method on a generator is a Thor step.
19
24
  module BackstageSignin
25
+ include AuthFlavor
26
+
20
27
  TEMPLATES = File.expand_path("templates", __dir__)
21
28
 
22
29
  USER_MODEL = "app/models/user.rb"
@@ -38,20 +45,29 @@ module Maquina
38
45
  SOLID_ERRORS_BASIC_AUTH = /\n backstage = Rails\.application\.credentials\.backstage \|\| \{\}\n\n config\.solid_errors\.username = .*\n config\.solid_errors\.password = .*\n/
39
46
  MISSION_CONTROL_BASIC_AUTH = / backstage = Rails\.application\.credentials\.backstage \|\| \{\}\n\n MissionControl::Jobs\.http_basic_auth_user = .*\n MissionControl::Jobs\.http_basic_auth_password = .*\n/
40
47
 
41
- # The people the generated access test signs in as.
48
+ # The people the generated access test signs in as, with their role on
49
+ # each sign-in: with Clave both are admins and only the operator is on the
50
+ # list; with Rails' sign-in the member is not an admin.
42
51
  USER_FIXTURES = {
43
- "backstage_operator" => {"email_address" => "backstage-operator@example.com", "name" => "Backstage Operator", "role" => "admin"},
44
- "backstage_member" => {"email_address" => "backstage-member@example.com", "name" => "Backstage Member", "role" => "admin"}
52
+ "backstage_operator" => [{"email_address" => "backstage-operator@example.com", "name" => "Backstage Operator"}, {clave: "admin", rails: "admin"}],
53
+ "backstage_member" => [{"email_address" => "backstage-member@example.com", "name" => "Backstage Member"}, {clave: "admin", rails: "member"}]
45
54
  }.freeze
55
+ ALLOW_BACKSTAGE_MIGRATION = "add_allow_backstage_to_accounts"
46
56
 
47
57
  private
48
58
 
49
- def clave_installed?
50
- # EmailVerification is Clave's own. Rails' authentication generator
51
- # (maquina:registration) also writes the other two, but its User has no
52
- # blocked?, so that app keeps Basic Auth.
53
- [USER_MODEL, "app/models/email_verification.rb", "app/controllers/concerns/authentication.rb"]
54
- .all? { |path| backstage_file?(path) }
59
+ # Clave's or Rails' sign-in, with a User to ask. Without one the
60
+ # backstage keeps its Basic Auth.
61
+ def backstage_signin_available?
62
+ backstage_file?(USER_MODEL) && !auth_flavor.nil?
63
+ end
64
+
65
+ # A file this module already moved to the sign-in: it no longer has the
66
+ # Basic Auth its generator writes. Rendering it again would only put the
67
+ # Basic Auth back (and make Thor ask to overwrite), so the generators keep it.
68
+ def backstage_signin_converted?(path, basic_auth_marker)
69
+ full_path = File.join(destination_root, path)
70
+ File.exist?(full_path) && !File.read(full_path).include?(basic_auth_marker)
55
71
  end
56
72
 
57
73
  def backstage_signin_pending
@@ -60,7 +76,7 @@ module Maquina
60
76
 
61
77
  # Returns true when the backstage is (now) behind the sign-in.
62
78
  def install_backstage_signin
63
- return false unless clave_installed?
79
+ return false unless backstage_signin_available?
64
80
 
65
81
  add_backstage_access_to_user
66
82
  convert_backstage_controller
@@ -90,13 +106,16 @@ module Maquina
90
106
  true
91
107
  end
92
108
 
93
- # User.backstage_emails and User#backstage_access?. Also called by
109
+ # User#backstage_access? (and, with Clave, User.backstage_emails; with
110
+ # Rails' sign-in, the accounts.allow_backstage column). Also called by
94
111
  # maquina:mcp, whose admin-only tools use the same gate.
95
112
  def add_backstage_access_to_user
96
113
  return unless backstage_file?(USER_MODEL)
97
114
 
115
+ add_allow_backstage_column if rails_auth?
98
116
  content = File.read(File.join(destination_root, USER_MODEL))
99
117
  return if content.include?("def backstage_access?")
118
+ return backstage_signin_pending << USER_MODEL if rails_auth? && !user_admin_predicate?(content)
100
119
 
101
120
  if content.match?(/^ private$/)
102
121
  inject_into_file USER_MODEL, backstage_access_methods, before: /^ private$/
@@ -133,10 +152,15 @@ module Maquina
133
152
  def create_backstage_access_test
134
153
  return unless backstage_file?("test/test_helpers/session_test_helper.rb")
135
154
 
136
- create_file ACCESS_TEST, File.read(File.join(TEMPLATES, ACCESS_TEST)) unless backstage_file?(ACCESS_TEST)
137
- append_backstage_fixture "test/fixtures/accounts.yml", "backstage", {"name" => "Backstage"}
138
- USER_FIXTURES.each do |label, attributes|
139
- append_backstage_fixture "test/fixtures/users.yml", label, attributes.merge("account" => "backstage")
155
+ unless backstage_file?(ACCESS_TEST)
156
+ create_file ACCESS_TEST, ERB.new(File.read(File.join(TEMPLATES, "#{ACCESS_TEST}.tt")), trim_mode: "-").result(binding)
157
+ end
158
+ account = {"name" => "Backstage"}
159
+ account["allow_backstage"] = true if rails_auth?
160
+ append_backstage_fixture "test/fixtures/accounts.yml", "backstage", account
161
+ USER_FIXTURES.each do |label, (attributes, roles)|
162
+ append_backstage_fixture "test/fixtures/users.yml", label,
163
+ attributes.merge(auth_fixture_attributes(role: roles.fetch(auth_flavor)), "account" => "backstage")
140
164
  end
141
165
  end
142
166
 
@@ -162,6 +186,8 @@ module Maquina
162
186
  end
163
187
 
164
188
  def say_backstage_access
189
+ return say_rails_backstage_access if rails_auth?
190
+
165
191
  say " - Backstage access: signed-in users listed in credentials (bin/rails credentials:edit)"
166
192
  say " backstage:"
167
193
  say " emails:"
@@ -170,6 +196,14 @@ module Maquina
170
196
  say " turns its Basic Auth back on when a password is set."
171
197
  end
172
198
 
199
+ def say_rails_backstage_access
200
+ say " - Backstage access: admins of an account with the backstage on. Turn it on for"
201
+ say " your own account from the console (bin/rails console):"
202
+ say " Account.find_by!(name: \"...\").update!(allow_backstage: true)"
203
+ say " Anyone else gets a 404. Do not set backstage.username or password: Solid Errors"
204
+ say " turns its Basic Auth back on when a password is set."
205
+ end
206
+
173
207
  def say_backstage_signin_pending
174
208
  return if backstage_signin_pending.empty?
175
209
 
@@ -179,7 +213,36 @@ module Maquina
179
213
  backstage_signin_pending.uniq.each { |path| say " - #{path}" }
180
214
  end
181
215
 
216
+ # accounts.allow_backstage, off by default, unless a migration or the
217
+ # schema already has it.
218
+ def add_allow_backstage_column
219
+ return unless backstage_file?("app/models/account.rb")
220
+ return if Dir.glob(File.join(destination_root, "db/migrate/*_#{ALLOW_BACKSTAGE_MIGRATION}.rb")).any?
221
+ return if backstage_file?("db/schema.rb") && File.read(File.join(destination_root, "db/schema.rb")).include?(%("allow_backstage"))
222
+
223
+ create_file "db/migrate/#{allow_backstage_migration_number}_#{ALLOW_BACKSTAGE_MIGRATION}.rb", <<~RUBY
224
+ class AddAllowBackstageToAccounts < ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]
225
+ def change
226
+ # Admins of an account with this on open the backstage and use the
227
+ # admin-only MCP tools (User#backstage_access?).
228
+ add_column :accounts, :allow_backstage, :boolean, default: false, null: false
229
+ end
230
+ end
231
+ RUBY
232
+ end
233
+
234
+ def allow_backstage_migration_number
235
+ current = Time.now.utc.strftime("%Y%m%d%H%M%S")
236
+ existing = Dir.glob(File.join(destination_root, "db/migrate/[0-9]*_*.rb")).map { |f| File.basename(f).split("_", 2).first }.max
237
+ (existing && existing >= current) ? existing.succ : current
238
+ end
239
+
240
+ # admin? from the registration role enum or the app's own method.
241
+ def user_admin_predicate?(content) = content.match?(/enum :role\b/) || content.include?("def admin?")
242
+
182
243
  def backstage_access_methods
244
+ return rails_backstage_access_methods if rails_auth?
245
+
183
246
  <<-RUBY
184
247
  # Who may open the backstage and use the admin-only MCP tools: an unblocked
185
248
  # user whose email is listed in credentials (backstage.emails). An empty list
@@ -199,6 +262,18 @@ module Maquina
199
262
  RUBY
200
263
  end
201
264
 
265
+ def rails_backstage_access_methods
266
+ <<-RUBY
267
+ # Who may open the backstage and use the admin-only MCP tools: an admin of an
268
+ # account whose allow_backstage is on (off by default; turn it on from the
269
+ # console). Anyone else gets a 404.
270
+ def backstage_access?
271
+ account.allow_backstage? && admin?
272
+ end
273
+
274
+ RUBY
275
+ end
276
+
202
277
  def backstage_controller_header
203
278
  <<-RUBY
204
279
  # The app's sign-in rather than a shared Basic Auth password, for every
@@ -1,15 +1,23 @@
1
1
  require "test_helper"
2
2
  require_relative "../test_helpers/session_test_helper"
3
3
 
4
+ <% if clave? -%>
4
5
  # The backstage is behind the app's sign-in (generated by the maquina dashboard
5
6
  # generators). Only a signed-in, unblocked user whose email is on the backstage
6
7
  # list gets in; anyone else signed in gets a 404, so the pages never confirm
7
8
  # they exist. The list lives in credentials (backstage.emails); these tests set
8
9
  # config.x.backstage_emails, which User.backstage_emails reads first.
10
+ <% else -%>
11
+ # The backstage is behind the app's sign-in (generated by the maquina dashboard
12
+ # generators). Only a signed-in admin of an account whose allow_backstage is on
13
+ # gets in; anyone else signed in gets a 404, so the pages never confirm they
14
+ # exist. The backstage fixture account has it on.
15
+ <% end -%>
9
16
  class BackstageAccessTest < ActionDispatch::IntegrationTest
10
17
  # The engine pages mounted under the backstage prefix, whichever are installed.
11
18
  PAGES = %w[errors jobs security].freeze
12
19
 
20
+ <% if clave? -%>
13
21
  setup do
14
22
  @emails_before = Rails.configuration.x.backstage_emails
15
23
  Rails.configuration.x.backstage_emails = [users(:backstage_operator).email_address]
@@ -19,6 +27,7 @@ class BackstageAccessTest < ActionDispatch::IntegrationTest
19
27
  Rails.configuration.x.backstage_emails = @emails_before
20
28
  end
21
29
 
30
+ <% end -%>
22
31
  test "signed out, every backstage page asks to sign in" do
23
32
  # Before any request: after one into a mounted engine, URL helpers in an
24
33
  # integration test generate under that engine's mount point.
@@ -31,7 +40,11 @@ class BackstageAccessTest < ActionDispatch::IntegrationTest
31
40
  end
32
41
  end
33
42
 
43
+ <% if clave? -%>
34
44
  test "a user not on the list gets not found on every backstage page" do
45
+ <% else -%>
46
+ test "a user who is not an admin gets not found on every backstage page" do
47
+ <% end -%>
35
48
  sign_in_as users(:backstage_member)
36
49
 
37
50
  backstage_paths.each do |path|
@@ -41,6 +54,7 @@ class BackstageAccessTest < ActionDispatch::IntegrationTest
41
54
  end
42
55
  end
43
56
 
57
+ <% if clave? -%>
44
58
  test "a blocked user on the list gets not found" do
45
59
  users(:backstage_operator).block!(reason: "test")
46
60
  sign_in_as users(:backstage_operator)
@@ -60,6 +74,18 @@ class BackstageAccessTest < ActionDispatch::IntegrationTest
60
74
  end
61
75
 
62
76
  test "a user on the list gets in" do
77
+ <% else -%>
78
+ test "an admin of an account with the backstage off gets not found" do
79
+ accounts(:backstage).update!(allow_backstage: false)
80
+ sign_in_as users(:backstage_operator)
81
+
82
+ get backstage_dashboard_path
83
+
84
+ assert_response :not_found
85
+ end
86
+
87
+ test "an admin of an account with the backstage on gets in" do
88
+ <% end -%>
63
89
  sign_in_as users(:backstage_operator)
64
90
 
65
91
  get backstage_dashboard_path
@@ -0,0 +1,22 @@
1
+ Description:
2
+ Tags every error Solid Errors records with who and where it happened: the
3
+ request's id, method and path, and the signed-in user's and account's ids
4
+ (ids only, no email). Needs maquina:solid_errors; a new install of
5
+ maquina:solid_errors already includes it, so run this in an app installed
6
+ before 0.11.
7
+
8
+ Adds config/initializers/error_context.rb, a Rails error reporter
9
+ middleware that reads Current when the error is reported, so it sees a
10
+ Current.account set late by a tenant scope and covers jobs and controllers
11
+ that do not inherit ApplicationController. It gives the MCP tool
12
+ list_failures (and bin/failures) user_id, account_id and since filters.
13
+
14
+ Nothing maquina:solid_errors would overwrite is touched. The query and tool
15
+ files (app/agents/errors_query.rb, failure_triage.rb, list_failures_tool.rb,
16
+ app/models/mcp/tools/solid_errors.rb) are replaced only when they are
17
+ exactly what an earlier release wrote; an edited one is kept and listed
18
+ after the install. The filters' test goes in its own file,
19
+ test/models/mcp/tools/solid_errors_filters_test.rb.
20
+
21
+ Examples:
22
+ rails g maquina:error_context
@@ -0,0 +1,51 @@
1
+ require "rails/generators"
2
+ require_relative "../mcp/mcp_sibling_tools"
3
+ require_relative "../solid_errors/error_context_files"
4
+
5
+ module Maquina
6
+ module Generators
7
+ # Request error context for an app that ran maquina:solid_errors before it
8
+ # existed. Touches nothing maquina:solid_errors would overwrite (views,
9
+ # layout, initializer): it adds config/initializers/error_context.rb,
10
+ # upgrades the query and MCP tool files only where they are unedited, and
11
+ # adds their test.
12
+ class ErrorContextGenerator < Rails::Generators::Base
13
+ include McpSiblingTools
14
+ include ErrorContextFiles
15
+
16
+ source_root File.expand_path("../solid_errors/templates", __dir__)
17
+
18
+ class_option :quiet, type: :boolean, default: false,
19
+ desc: "Suppress post-install instructions"
20
+
21
+ # 1. Solid Errors is where the context goes.
22
+ def require_solid_errors
23
+ return if File.exist?(File.join(destination_root, "config/initializers/solid_errors.rb"))
24
+
25
+ raise Thor::Error, "maquina:error_context tags the errors Solid Errors records. Run `rails g maquina:solid_errors --prefix /admin` first."
26
+ end
27
+
28
+ # 2. The error reporter middleware
29
+ def create_initializer
30
+ install_error_context_initializer
31
+ end
32
+
33
+ # 3. The user/account filters, where the files are as a release wrote them
34
+ def upgrade_query_and_tools
35
+ @error_context_files = upgrade_error_context_files
36
+ end
37
+
38
+ # 4. Their host-app test, once the tool file has the filters
39
+ def create_filters_test
40
+ install_mcp_feature_tests(:solid_errors)
41
+ end
42
+
43
+ def show_post_install
44
+ return if options[:quiet]
45
+
46
+ say ""
47
+ say_error_context_installed(customized: @error_context_files[:customized])
48
+ end
49
+ end
50
+ end
51
+ end
@@ -17,9 +17,10 @@ Description:
17
17
  with a whoami tool, written only if missing. Tools reach the token's own
18
18
  user's data through Mcp::UserScope. If solid_errors, solid_queue, security
19
19
  or csp_reports are already installed, their admin-only MCP tools are added
20
- too. Admin-only tools are for users on the backstage list (backstage.emails
21
- in credentials, User#backstage_access?, added to app/models/user.rb); an
22
- account's admin role is not enough. Generates a test helper (mcp_post, oauth_token_for), an end-to-end
20
+ too. Admin-only tools are for User#backstage_access? (added to
21
+ app/models/user.rb): with Clave, users on the backstage list (backstage.emails
22
+ in credentials); with Rails' sign-in, admins of an account whose
23
+ allow_backstage is on (off by default, so signing up is not enough). Generates a test helper (mcp_post, oauth_token_for), an end-to-end
23
24
  flow test, a protocol conformance test and a registry test. Anything it
24
25
  cannot place safely is listed after the install.
25
26