env_bro 0.1.1 → 0.1.3

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: 9df4fc28acfe1ed6dd8893dbcdbfc44c179f29d38182250b400b01972d1e156b
4
- data.tar.gz: 2e2237a10f5337e0aabb232431096ea6352af08dedc367656212093cc98f1870
3
+ metadata.gz: 1dd3cfc3e26b10b59aacd5151817151c3f280869e025a4d1b94a77527732ce9f
4
+ data.tar.gz: '048a2b36daf9be8c210e62a97bd67fffc64ec0976cb8ed01d24ffddbf67f170e'
5
5
  SHA512:
6
- metadata.gz: 9deb98e680ecad5343371d9a075ce5b312837185508543d3d62120547f877434ce490c52dad7e6f1f1750cced9cd50fd9c0d3ee62dc6ffbdb713054bdac10430
7
- data.tar.gz: e1d0586feacd8e660c0d31cc6c167fef6dd6284fd8125c69aa07c5343d2e66d00523f20a9b2835ca60536da1ef06f3323ffe40a54144cb6fb9abda0a93705e9c
6
+ metadata.gz: 58096324cc25fbe8cde3ceddbdc9decc5d4d7a516953116e96b1911073a11ecdc0867a72333a9c8f433e03753aacd5d7a8847d45786fbc0ebf2f534dde39d35c
7
+ data.tar.gz: 14e355f2df5f727a109b9a9cc67775525bd154e008c67b6181448551c7af983bec0d53be8ca1cc308eab4471cd7e674c5e5322f86173b39cd0e0f8bc3bec4867
data/README.md CHANGED
@@ -51,9 +51,11 @@ Then set the permissions in `config/initializers/env_bro.rb`. Everything is deni
51
51
  ```ruby
52
52
  EnvBro.configure do |config|
53
53
  # Who may do what. Each lambda gets the current user.
54
- config.can_propose = ->(user) { user.developer? }
55
- config.can_review = ->(user) { user.admin? }
56
- config.can_reveal = ->(user) { user.admin? }
54
+ config.can_create = ->(user) { user.developer? } # propose new keys
55
+ config.can_update = ->(user) { user.developer? } # propose new values for existing keys
56
+ config.can_destroy = ->(user) { user.lead? } # propose removing keys
57
+ config.can_review = ->(user) { user.lead? } # upload a proposal, see what changed (masked)
58
+ config.can_reveal = ->(user) { user.admin? } # see the actual values in a review (audited)
57
59
 
58
60
  # Defaults shown:
59
61
  # config.parent_controller = "::ApplicationController" # gives access to your auth
@@ -65,6 +67,17 @@ EnvBro.configure do |config|
65
67
  end
66
68
  ```
67
69
 
70
+ Any of `can_create`, `can_update` and `can_destroy` opens the propose page, which then only
71
+ offers what the user may do (no "add keys" panel without `can_create`, no value fields without
72
+ `can_update`, no remove toggles without `can_destroy`). The server checks every change too.
73
+
74
+ Higher permissions include the lower ones, so you only grant each person one:
75
+
76
+ - `can_review` also grants create, update and destroy.
77
+ - `can_reveal` also grants everything else.
78
+
79
+ `config.can_propose = ->(user) { ... }` is a shorthand that sets create, update and destroy together.
80
+
68
81
  ## One environment per deployment
69
82
 
70
83
  EnvBro uses `Rails.application.credentials`, so it finds the file and key exactly like Rails does
@@ -103,13 +116,16 @@ The dummy app comes with sample `development` and `test` credentials and keys in
103
116
  home page, or append `?as=viewer|developer|admin` to any URL:
104
117
 
105
118
  - `viewer`: no access
106
- - `developer`: can propose changes
107
- - `admin`: can propose, review and reveal values
119
+ - `creator`: can add keys
120
+ - `updater`: can change existing values
121
+ - `developer`: can create, update and remove keys
122
+ - `reviewer`: can review, plus everything a developer can do
123
+ - `admin`: can reveal values, plus everything else
108
124
 
109
125
  To try the full flow:
110
126
 
111
127
  1. As a developer, propose a change and download the file.
112
- 2. As an admin, review that file.
128
+ 2. As a reviewer or admin, review that file.
113
129
 
114
130
  To check the stale-base warning, upload a different proposal as the base file.
115
131
 
@@ -18,10 +18,10 @@ module EnvBro
18
18
  @env_bro_user = EnvBro.config.current_user.call(self)
19
19
  end
20
20
 
21
- # permission: :propose, :review or :reveal
21
+ # permission: :create, :update, :destroy, :review or :reveal (higher ones include
22
+ # lower ones), or :propose for "any of create, update, destroy".
22
23
  def env_bro_can?(permission)
23
- return false if env_bro_user.nil?
24
- !!EnvBro.config.public_send(:"can_#{permission}").call(env_bro_user)
24
+ EnvBro.config.allows?(env_bro_user, permission)
25
25
  end
26
26
 
27
27
  def env_bro_authorize!(permission)
@@ -1,8 +1,10 @@
1
1
  module EnvBro
2
2
  class ProposalsController < ApplicationController
3
- before_action { env_bro_authorize!(:propose) }
3
+ before_action { env_bro_authorize!(:propose) } # any of create, update, destroy
4
4
  before_action :env_bro_require_ready
5
5
 
6
+ helper_method :can_create?, :can_update?, :can_destroy?
7
+
6
8
  def new
7
9
  @new_keys = [ Editor::Change.new(action: "set") ]
8
10
  @edits = {}
@@ -14,6 +16,8 @@ module EnvBro
14
16
  @edits = edit_params.index_by(&:key)
15
17
  changes = @new_keys + @edits.values.reject { |c| c.action == "set" && c.value.blank? }
16
18
 
19
+ authorize_changes!(changes)
20
+
17
21
  result = Editor.new(@environment).apply(changes, author: EnvBro.config.user_label.call(env_bro_user))
18
22
 
19
23
  Audit.record(:proposal_created, user: env_bro_user,
@@ -31,6 +35,23 @@ module EnvBro
31
35
  end
32
36
 
33
37
  private
38
+ def can_create? = env_bro_can?(:create)
39
+ def can_update? = env_bro_can?(:update)
40
+ def can_destroy? = env_bro_can?(:destroy)
41
+
42
+ # Each change needs the matching permission: removing a key is destroy, setting a key
43
+ # that already exists is update, setting a new one is create. Checked on the server, so
44
+ # a hand-built request can't do more than the form offers.
45
+ def authorize_changes!(changes)
46
+ existing = @environment.current_credentials
47
+ changes.each do |change|
48
+ key = change.key.to_s.strip
49
+ next if key.empty?
50
+ permission = change.action == "delete" ? :destroy : (existing.key?(key) ? :update : :create)
51
+ env_bro_authorize!(permission)
52
+ end
53
+ end
54
+
34
55
  # new_keys[0][key], new_keys[0][secret_value], ...
35
56
  def new_key_params
36
57
  indexed_rows(:new_keys).map do |row|
@@ -2,7 +2,12 @@
2
2
  <div class="page-head">
3
3
  <div>
4
4
  <h1>Propose a change to <%= @environment.name %></h1>
5
- <p class="muted">Existing values stay hidden. Type a value to replace one, tick <em>remove</em> to drop a key, or add new keys.</p>
5
+ <p class="muted">Existing values stay hidden.
6
+ <% abilities = [] %>
7
+ <% abilities << "add new keys" if can_create? %>
8
+ <% abilities << "type a value to replace one" if can_update? %>
9
+ <% abilities << "tick <em>remove</em> to drop a key".html_safe if can_destroy? %>
10
+ You can <%= safe_join(abilities, ", ") %>.</p>
6
11
  </div>
7
12
  </div>
8
13
 
@@ -11,6 +16,7 @@
11
16
  <% end %>
12
17
 
13
18
  <%= form_with url: proposal_path, method: :post, local: true, data: { proposal_form: true, turbo: false } do %>
19
+ <% if can_create? %>
14
20
  <section class="panel">
15
21
  <div class="toolbar">
16
22
  <h2>Add new keys</h2>
@@ -23,6 +29,7 @@
23
29
  </div>
24
30
  <p class="hint">Nest keys with dots: <code>stripe.publishable_key</code> goes under <code>stripe:</code>. A new group is created if needed.</p>
25
31
  </section>
32
+ <% end %>
26
33
 
27
34
  <section class="panel panel-flush">
28
35
  <div class="toolbar panel-pad">
@@ -31,7 +38,7 @@
31
38
  </div>
32
39
 
33
40
  <% if @outline.empty? %>
34
- <p class="empty">This file has no keys yet. Add some above.</p>
41
+ <p class="empty">This file has no keys yet.<%= " Add some above." if can_create? %></p>
35
42
  <% else %>
36
43
  <div class="yaml">
37
44
  <% index = 0 %>
@@ -45,12 +52,20 @@
45
52
  <% removed = edit&.action == "delete" %>
46
53
  <div class="yaml-line yaml-leaf<%= ' is-removed' if removed %><%= ' is-changed' if edit&.value.present? %>" data-leaf="<%= line.path %>">
47
54
  <label class="yaml-key depth-<%= [ line.depth, 6 ].min %>" for="edit_<%= index %>"><%= line.name %>:</label>
48
- <input type="hidden" name="edits[<%= index %>][key]" value="<%= line.path %>">
49
- <textarea name="edits[<%= index %>][secret_value]" id="edit_<%= index %>" rows="1" placeholder="•••••• unchanged"
50
- autocomplete="off" spellcheck="false" data-track data-autogrow<%= ' disabled' if removed %>><%= edit&.value unless removed %></textarea>
51
- <label class="remove-toggle" title="Remove <%= line.path %>">
52
- <input type="checkbox" name="edits[<%= index %>][remove]" value="1" data-track data-remove-toggle<%= ' checked' if removed %>> remove
53
- </label>
55
+ <% if can_update? || can_destroy? %>
56
+ <input type="hidden" name="edits[<%= index %>][key]" value="<%= line.path %>">
57
+ <% end %>
58
+ <% if can_update? %>
59
+ <textarea name="edits[<%= index %>][secret_value]" id="edit_<%= index %>" rows="1" placeholder="•••••• unchanged"
60
+ autocomplete="off" spellcheck="false" data-track data-autogrow<%= ' disabled' if removed %>><%= edit&.value unless removed %></textarea>
61
+ <% else %>
62
+ <span class="muted small">••••••</span>
63
+ <% end %>
64
+ <% if can_destroy? %>
65
+ <label class="remove-toggle" title="Remove <%= line.path %>">
66
+ <input type="checkbox" name="edits[<%= index %>][remove]" value="1" data-track data-remove-toggle<%= ' checked' if removed %>> remove
67
+ </label>
68
+ <% end %>
54
69
  </div>
55
70
  <% index += 1 %>
56
71
  <% end %>
@@ -17,9 +17,9 @@
17
17
  each("[data-leaf]", function (line) {
18
18
  var value = line.querySelector("textarea");
19
19
  var remove = line.querySelector("[data-remove-toggle]");
20
- var isRemoved = remove.checked;
21
- var isChanged = !isRemoved && value.value.trim() !== "";
22
- value.disabled = isRemoved;
20
+ var isRemoved = !!remove && remove.checked;
21
+ var isChanged = !isRemoved && !!value && value.value.trim() !== "";
22
+ if (value) value.disabled = isRemoved;
23
23
  line.classList.toggle("is-removed", isRemoved);
24
24
  line.classList.toggle("is-changed", isChanged);
25
25
  if (isRemoved) removed++;
@@ -97,7 +97,8 @@
97
97
  document.addEventListener("change", function (event) {
98
98
  if (!event.target.matches("[data-remove-toggle]")) return;
99
99
  refresh();
100
- if (!event.target.checked) event.target.closest("[data-leaf]").querySelector("textarea").focus();
100
+ var textarea = event.target.closest("[data-leaf]").querySelector("textarea");
101
+ if (!event.target.checked && textarea) textarea.focus();
101
102
  });
102
103
 
103
104
  // The proposal form answers with a file download, so the page itself does not change.
@@ -4,7 +4,8 @@ class CreateEnvBroAuditEvents < ActiveRecord::Migration[7.1]
4
4
  t.string :event, null: false
5
5
  t.string :user_label
6
6
  t.string :environment, null: false
7
- t.json :details, null: false, default: {}
7
+ # No column default: MySQL rejects defaults on JSON columns. Audit.record always sets it.
8
+ t.json :details, null: false
8
9
 
9
10
  # proposal_created: the id embedded in the file's metadata and the SHA-256 of the file handed out.
10
11
  # proposal_deployed: the SHA-256 of the file found running.
@@ -11,10 +11,32 @@ module EnvBro
11
11
  attr_accessor :user_label
12
12
 
13
13
  # Permission checks, each ->(user) { true/false }.
14
- # can_propose - see key names, build a new encrypted file with changes
14
+ # can_create - propose new keys
15
+ # can_update - propose new values for existing keys
16
+ # can_destroy - propose removing keys
15
17
  # can_review - upload a proposed file and see which keys changed (values masked)
16
18
  # can_reveal - reveal the actual values in a review diff (every reveal is audited)
17
- attr_accessor :can_propose, :can_review, :can_reveal
19
+ #
20
+ # Any of them gets a user into the propose page (which only offers what they may do).
21
+ # Higher permissions include the lower ones, so you don't have to repeat yourself:
22
+ # review includes create, update and destroy; reveal includes everything.
23
+ attr_accessor :can_create, :can_update, :can_destroy, :can_review, :can_reveal
24
+
25
+ PERMISSIONS = %i[create update destroy review reveal].freeze
26
+
27
+ # Permissions that also grant the key.
28
+ IMPLIED_BY = {
29
+ create: %i[review reveal],
30
+ update: %i[review reveal],
31
+ destroy: %i[review reveal],
32
+ review: %i[reveal],
33
+ reveal: []
34
+ }.freeze
35
+
36
+ # Shorthand: grant create, update and destroy with one check.
37
+ def can_propose=(check)
38
+ @can_create = @can_update = @can_destroy = check
39
+ end
18
40
 
19
41
  # Reject plain HTTP requests (secret values travel through the browser).
20
42
  attr_accessor :require_ssl
@@ -32,7 +54,9 @@ module EnvBro
32
54
  @parent_controller = "::ApplicationController"
33
55
  @current_user = ->(controller) { controller.respond_to?(:current_user, true) ? controller.send(:current_user) : nil }
34
56
  @user_label = ->(user) { user.respond_to?(:email) ? user.email : user.to_s }
35
- @can_propose = ->(_user) { false }
57
+ @can_create = ->(_user) { false }
58
+ @can_update = ->(_user) { false }
59
+ @can_destroy = ->(_user) { false }
36
60
  @can_review = ->(_user) { false }
37
61
  @can_reveal = ->(_user) { false }
38
62
  @require_ssl = defined?(Rails) && Rails.env.production?
@@ -40,5 +64,15 @@ module EnvBro
40
64
  @max_upload_bytes = 1024 * 1024
41
65
  @track_deployments = true
42
66
  end
67
+
68
+ # Does the user hold this permission, directly or through a higher one?
69
+ # :propose means any of create, update or destroy.
70
+ def allows?(user, permission)
71
+ return false if user.nil?
72
+ return %i[create update destroy].any? { |p| allows?(user, p) } if permission == :propose
73
+ raise ArgumentError, "Unknown permission #{permission.inspect}" unless PERMISSIONS.include?(permission)
74
+
75
+ [ permission, *IMPLIED_BY.fetch(permission) ].any? { |p| !!public_send(:"can_#{p}").call(user) }
76
+ end
43
77
  end
44
78
  end
@@ -1,3 +1,3 @@
1
1
  module EnvBro
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -3,9 +3,13 @@
3
3
  EnvBro.configure do |config|
4
4
  # Who may do what. Each lambda gets the signed in user (nil when signed out).
5
5
  # Everything is denied until you change these.
6
- config.can_propose = ->(user) { false } # e.g. user&.developer?
7
- config.can_review = ->(user) { false } # e.g. user&.admin?
8
- config.can_reveal = ->(user) { false } # see actual values in a review (audited)
6
+ config.can_create = ->(user) { false } # propose new keys, e.g. user&.developer?
7
+ config.can_update = ->(user) { false } # propose new values for existing keys
8
+ config.can_destroy = ->(user) { false } # propose removing keys
9
+ config.can_review = ->(user) { false } # upload a proposal and see what changed, e.g. user&.lead?
10
+ config.can_reveal = ->(user) { false } # see actual values in a review (audited), e.g. user&.admin?
11
+ # Higher includes lower: review includes create/update/destroy, reveal includes everything.
12
+ # config.can_propose = ->(user) { ... } sets create, update and destroy in one go.
9
13
 
10
14
  # How EnvBro finds the signed in user. Engine controllers inherit from
11
15
  # parent_controller, so its authentication (before_actions) applies too.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: env_bro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ruby Dev SRL
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  requirements: []
94
- rubygems_version: 4.0.22
94
+ rubygems_version: 4.0.10
95
95
  specification_version: 4
96
96
  summary: Propose changes to Rails encrypted credentials without holding the keys.
97
97
  test_files: []