context_drop 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +184 -107
- data/SPEC.md +74 -26
- data/app/assets/javascripts/context_drop/context_drop.js +5121 -651
- data/app/controllers/context_drop/agent_controller.rb +170 -0
- data/app/controllers/context_drop/configs_controller.rb +2 -1
- data/app/controllers/context_drop/feedbacks_controller.rb +13 -3
- data/app/controllers/context_drop/mcp_controller.rb +2 -1
- data/app/helpers/context_drop/widget_helper.rb +11 -1
- data/app/javascript/context_drop/__tests__/widget.test.ts +661 -89
- data/app/javascript/context_drop/api-client.ts +130 -11
- data/app/javascript/context_drop/element-picker.ts +185 -33
- data/app/javascript/context_drop/icons.ts +24 -0
- data/app/javascript/context_drop/markdown.ts +146 -0
- data/app/javascript/context_drop/sanitizer.ts +413 -2
- data/app/javascript/context_drop/screenshot.ts +102 -8
- data/app/javascript/context_drop/styles.ts +1528 -252
- data/app/javascript/context_drop/types.ts +147 -0
- data/app/javascript/context_drop/widget.ts +2591 -290
- data/app/models/context_drop/feedback.rb +23 -0
- data/app/views/context_drop/dashboard/feedbacks/index.html.erb +5 -0
- data/app/views/context_drop/dashboard/feedbacks/show.html.erb +10 -0
- data/app/views/layouts/context_drop/application.html.erb +1 -0
- data/config/routes.rb +11 -0
- data/db/migrate/20261112000004_add_environment_to_context_drop_feedbacks.rb +10 -0
- data/lib/context_drop/agent/key_store.rb +290 -0
- data/lib/context_drop/agent/llm_client.rb +413 -0
- data/lib/context_drop/agent/orchestrator.rb +314 -0
- data/lib/context_drop/agent/workspace_sandbox.rb +499 -0
- data/lib/context_drop/configuration.rb +216 -3
- data/lib/context_drop/mcp/dispatcher.rb +71 -29
- data/lib/context_drop/mcp/environment_router.rb +115 -0
- data/lib/context_drop/mcp/prompts/investigate_feedback.rb +87 -8
- data/lib/context_drop/mcp/remote_client.rb +120 -0
- data/lib/context_drop/mcp/server.rb +9 -2
- data/lib/context_drop/mcp/tools/get_feedback.rb +50 -4
- data/lib/context_drop/mcp/tools/get_feedback_attachment.rb +26 -4
- data/lib/context_drop/mcp/tools/list_environments.rb +60 -0
- data/lib/context_drop/mcp/tools/list_feedbacks.rb +174 -22
- data/lib/context_drop/mcp/tools/update_feedback_status.rb +45 -4
- data/lib/context_drop/version.rb +1 -1
- data/lib/context_drop.rb +8 -2
- data/lib/generators/context_drop/install/templates/initializer.rb.tt +29 -2
- metadata +11 -1
|
@@ -25,6 +25,14 @@ module ContextDrop
|
|
|
25
25
|
scope :resolved, -> { where(status: "resolved") }
|
|
26
26
|
scope :dismissed, -> { where(status: "dismissed") }
|
|
27
27
|
scope :recent, -> { order(created_at: :desc, id: :desc) }
|
|
28
|
+
scope :for_environment, lambda { |env|
|
|
29
|
+
normalized = env.to_s.strip.downcase
|
|
30
|
+
if normalized.present? && column_names.include?("environment")
|
|
31
|
+
where(environment: normalized)
|
|
32
|
+
else
|
|
33
|
+
all
|
|
34
|
+
end
|
|
35
|
+
}
|
|
28
36
|
|
|
29
37
|
validates :page_url, presence: true
|
|
30
38
|
validates :status, presence: true, inclusion: { in: STATUSES }
|
|
@@ -45,6 +53,16 @@ module ContextDrop
|
|
|
45
53
|
update!(status: "dismissed")
|
|
46
54
|
end
|
|
47
55
|
|
|
56
|
+
def environment_name
|
|
57
|
+
if has_attribute?(:environment) && self[:environment].present?
|
|
58
|
+
self[:environment].to_s
|
|
59
|
+
elsif metadata.is_a?(Hash) && metadata["environment"].present?
|
|
60
|
+
metadata["environment"].to_s
|
|
61
|
+
else
|
|
62
|
+
ContextDrop.config.resolved_environment
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
48
66
|
def submitter_label
|
|
49
67
|
return "Anonymous" if submitter_type.blank? && submitter_id.blank?
|
|
50
68
|
|
|
@@ -72,6 +90,11 @@ module ContextDrop
|
|
|
72
90
|
def normalize_defaults
|
|
73
91
|
self.status = "new" if status.blank?
|
|
74
92
|
self.metadata = {} unless metadata.is_a?(Hash)
|
|
93
|
+
resolved_env = ContextDrop.config.resolved_environment
|
|
94
|
+
if has_attribute?(:environment) && self[:environment].blank?
|
|
95
|
+
self[:environment] = resolved_env
|
|
96
|
+
end
|
|
97
|
+
self.metadata["environment"] ||= resolved_env
|
|
75
98
|
end
|
|
76
99
|
|
|
77
100
|
def ensure_content_or_context_present
|
|
@@ -86,6 +86,11 @@
|
|
|
86
86
|
<%= fb.message.present? ? truncate(fb.message, length: 120) : content_tag(:span, "(No text message)", style: "color: var(--cd-muted); font-style: italic;") %>
|
|
87
87
|
</div>
|
|
88
88
|
<div class="cd-context-chips">
|
|
89
|
+
<% if fb.respond_to?(:environment) && fb.environment.present? %>
|
|
90
|
+
<span class="cd-context-chip" style="font-family: var(--cd-font-mono);" title="Environment: <%= fb.environment %>">
|
|
91
|
+
<%= fb.environment %>
|
|
92
|
+
</span>
|
|
93
|
+
<% end %>
|
|
89
94
|
<% if fb.attachments.size > 0 %>
|
|
90
95
|
<span class="cd-context-chip" title="<%= fb.attachments.size %> attachment(s)">
|
|
91
96
|
<%= cd_icon_attachment(size: 11) %>
|
|
@@ -83,6 +83,8 @@
|
|
|
83
83
|
</div>
|
|
84
84
|
|
|
85
85
|
<dl class="cd-meta-list">
|
|
86
|
+
<dt>Environment</dt>
|
|
87
|
+
<dd><code class="cd-mono"><%= @feedback.respond_to?(:environment) && @feedback.environment.present? ? @feedback.environment : ContextDrop.configuration.resolved_environment %></code></dd>
|
|
86
88
|
<dt>Page URL</dt>
|
|
87
89
|
<dd>
|
|
88
90
|
<div style="display: inline-flex; align-items: center; gap: 0.35rem; max-width: 100%;">
|
|
@@ -141,8 +143,16 @@
|
|
|
141
143
|
<% end %>
|
|
142
144
|
</div>
|
|
143
145
|
<dl class="cd-meta-list" style="grid-template-columns: 110px 1fr; font-size: 0.75rem;">
|
|
146
|
+
<% if el.metadata.is_a?(Hash) && el.metadata["friendly_label"].present? %>
|
|
147
|
+
<dt>Component</dt>
|
|
148
|
+
<dd style="font-weight: 600; color: var(--cd-text);"><%= el.metadata["friendly_label"] %></dd>
|
|
149
|
+
<% end %>
|
|
144
150
|
<dt>Selector</dt>
|
|
145
151
|
<dd><code class="cd-mono" style="word-break: break-all;"><%= el.selector %></code></dd>
|
|
152
|
+
<% if el.metadata.is_a?(Hash) && el.metadata["original_target_selector"].present? %>
|
|
153
|
+
<dt>Inner Target</dt>
|
|
154
|
+
<dd><code class="cd-mono" style="word-break: break-all;"><%= el.metadata["original_target_selector"] %></code></dd>
|
|
155
|
+
<% end %>
|
|
146
156
|
<dt>DOM Path</dt>
|
|
147
157
|
<dd><code class="cd-mono" style="word-break: break-all;"><%= el.dom_path.presence || "N/A" %></code></dd>
|
|
148
158
|
<dt>Text Content</dt>
|
|
@@ -527,6 +527,7 @@
|
|
|
527
527
|
<a href="<%= dashboard_feedbacks_path %>" class="cd-brand-tag">
|
|
528
528
|
<span>ContextDrop</span>
|
|
529
529
|
<span class="cd-brand-pill">Triage</span>
|
|
530
|
+
<span class="cd-context-chip" style="font-family: var(--cd-font-mono); font-size: 0.6875rem;"><%= ContextDrop.configuration.resolved_environment %></span>
|
|
530
531
|
</a>
|
|
531
532
|
|
|
532
533
|
<% if content_for?(:cd_breadcrumbs) %>
|
data/config/routes.rb
CHANGED
|
@@ -8,6 +8,17 @@ ContextDrop::Engine.routes.draw do
|
|
|
8
8
|
get "health", to: "health#show"
|
|
9
9
|
post "mcp", to: "mcp#create"
|
|
10
10
|
|
|
11
|
+
scope "agent" do
|
|
12
|
+
get "status", to: "agent#status"
|
|
13
|
+
get "feedbacks", to: "agent#feedbacks"
|
|
14
|
+
post "keys", to: "agent#save_keys"
|
|
15
|
+
delete "keys", to: "agent#destroy_keys"
|
|
16
|
+
post "chat", to: "agent#chat"
|
|
17
|
+
post "edits/:id/apply", to: "agent#apply_edit"
|
|
18
|
+
post "edits/:id/reject", to: "agent#reject_edit"
|
|
19
|
+
post "edits/:id/revert", to: "agent#revert_edit"
|
|
20
|
+
end
|
|
21
|
+
|
|
11
22
|
namespace :dashboard do
|
|
12
23
|
root to: "feedbacks#index"
|
|
13
24
|
resources :feedbacks, only: %i[index show update]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class AddEnvironmentToContextDropFeedbacks < ActiveRecord::Migration[7.1]
|
|
4
|
+
def change
|
|
5
|
+
return if column_exists?(:context_drop_feedbacks, :environment)
|
|
6
|
+
|
|
7
|
+
add_column :context_drop_feedbacks, :environment, :string, null: false, default: "development"
|
|
8
|
+
add_index :context_drop_feedbacks, :environment
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "openssl"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
require "fileutils"
|
|
7
|
+
|
|
8
|
+
module ContextDrop
|
|
9
|
+
module Agent
|
|
10
|
+
class KeyStore
|
|
11
|
+
PROVIDERS = {
|
|
12
|
+
"anthropic" => {
|
|
13
|
+
label: "Anthropic Claude",
|
|
14
|
+
env_keys: %w[ANTHROPIC_API_KEY],
|
|
15
|
+
default_model: "claude-sonnet-4-20250514",
|
|
16
|
+
models: %w[claude-sonnet-4-20250514 claude-3-7-sonnet-latest claude-3-5-haiku-latest],
|
|
17
|
+
supports_base_url: false
|
|
18
|
+
},
|
|
19
|
+
"gemini" => {
|
|
20
|
+
label: "Google Gemini",
|
|
21
|
+
env_keys: %w[GEMINI_API_KEY GOOGLE_API_KEY],
|
|
22
|
+
default_model: "gemini-2.5-pro",
|
|
23
|
+
models: %w[gemini-2.5-pro gemini-2.5-flash],
|
|
24
|
+
supports_base_url: false
|
|
25
|
+
},
|
|
26
|
+
"openai" => {
|
|
27
|
+
label: "OpenAI",
|
|
28
|
+
env_keys: %w[OPENAI_API_KEY],
|
|
29
|
+
default_model: "gpt-4.1",
|
|
30
|
+
models: %w[gpt-4.1 gpt-4.1-mini o4-mini],
|
|
31
|
+
supports_base_url: false
|
|
32
|
+
},
|
|
33
|
+
"custom" => {
|
|
34
|
+
label: "OpenAI-Compatible (OpenRouter / Ollama)",
|
|
35
|
+
env_keys: %w[CONTEXT_DROP_LLM_API_KEY OPENROUTER_API_KEY],
|
|
36
|
+
env_base_url: "CONTEXT_DROP_LLM_BASE_URL",
|
|
37
|
+
default_base_url: "https://openrouter.ai/api/v1",
|
|
38
|
+
default_model: "anthropic/claude-sonnet-4",
|
|
39
|
+
models: %w[anthropic/claude-sonnet-4 google/gemini-2.5-pro openai/gpt-4.1 llama3.3],
|
|
40
|
+
supports_base_url: true
|
|
41
|
+
}
|
|
42
|
+
}.freeze
|
|
43
|
+
|
|
44
|
+
CIPHER_NAME = "aes-256-gcm"
|
|
45
|
+
|
|
46
|
+
def initialize(store_path: nil, env_vars: ENV)
|
|
47
|
+
@store_path = (store_path || ContextDrop.config.resolved_key_store_path).to_s
|
|
48
|
+
@master_key_path = "#{@store_path}.key"
|
|
49
|
+
@env_vars = env_vars
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Returns safe, masked provider metadata for the browser UI. Never exposes raw API keys.
|
|
53
|
+
def providers_status
|
|
54
|
+
vault = read_vault_hash
|
|
55
|
+
active = vault["_active_provider"].to_s
|
|
56
|
+
|
|
57
|
+
list = PROVIDERS.map do |id, meta|
|
|
58
|
+
vault_entry = vault[id].is_a?(Hash) ? vault[id] : {}
|
|
59
|
+
vault_key = vault_entry["api_key"].to_s.strip
|
|
60
|
+
env_key_name, env_key_val = resolve_env_key(meta[:env_keys])
|
|
61
|
+
|
|
62
|
+
source = if !vault_key.empty?
|
|
63
|
+
"vault"
|
|
64
|
+
elsif !env_key_val.empty?
|
|
65
|
+
"env"
|
|
66
|
+
elsif id == "custom" && loopback_base_url?(vault_entry["base_url"] || @env_vars[meta[:env_base_url].to_s])
|
|
67
|
+
"local_no_key"
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
raw_key = !vault_key.empty? ? vault_key : env_key_val
|
|
71
|
+
resolved_base_url = if meta[:supports_base_url]
|
|
72
|
+
(vault_entry["base_url"].to_s.strip.empty? ? nil : vault_entry["base_url"].to_s.strip) ||
|
|
73
|
+
(@env_vars[meta[:env_base_url].to_s].to_s.strip.empty? ? nil : @env_vars[meta[:env_base_url].to_s].to_s.strip) ||
|
|
74
|
+
meta[:default_base_url]
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
{
|
|
78
|
+
id: id,
|
|
79
|
+
label: meta[:label],
|
|
80
|
+
configured: !source.nil?,
|
|
81
|
+
source: source,
|
|
82
|
+
env_var_hint: env_key_name || meta[:env_keys].first,
|
|
83
|
+
masked_key: mask_secret(raw_key),
|
|
84
|
+
model: (vault_entry["model"].to_s.strip.empty? ? meta[:default_model] : vault_entry["model"].to_s.strip),
|
|
85
|
+
default_model: meta[:default_model],
|
|
86
|
+
models: meta[:models],
|
|
87
|
+
supports_base_url: meta[:supports_base_url],
|
|
88
|
+
base_url: resolved_base_url
|
|
89
|
+
}
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
if active.empty? || !PROVIDERS.key?(active)
|
|
93
|
+
first_configured = list.find { |p| p[:configured] }
|
|
94
|
+
active = first_configured ? first_configured[:id] : "anthropic"
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
{
|
|
98
|
+
active_provider: active,
|
|
99
|
+
providers: list
|
|
100
|
+
}
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def save_provider_config!(provider:, api_key: nil, model: nil, base_url: nil, make_active: true)
|
|
104
|
+
prov = provider.to_s.strip.downcase
|
|
105
|
+
unless PROVIDERS.key?(prov)
|
|
106
|
+
raise ContextDrop::Error, "Unsupported LLM provider '#{prov}'. Supported: #{PROVIDERS.keys.join(', ')}"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
vault = read_vault_hash
|
|
110
|
+
entry = vault[prov].is_a?(Hash) ? vault[prov].dup : {}
|
|
111
|
+
|
|
112
|
+
unless api_key.nil?
|
|
113
|
+
trimmed_key = api_key.to_s.strip
|
|
114
|
+
if trimmed_key.empty?
|
|
115
|
+
entry.delete("api_key")
|
|
116
|
+
else
|
|
117
|
+
entry["api_key"] = trimmed_key
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
unless model.nil?
|
|
122
|
+
trimmed_model = model.to_s.strip
|
|
123
|
+
if trimmed_model.empty?
|
|
124
|
+
entry.delete("model")
|
|
125
|
+
else
|
|
126
|
+
entry["model"] = trimmed_model
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
if PROVIDERS[prov][:supports_base_url] && !base_url.nil?
|
|
131
|
+
trimmed_url = base_url.to_s.strip
|
|
132
|
+
if trimmed_url.empty?
|
|
133
|
+
entry.delete("base_url")
|
|
134
|
+
else
|
|
135
|
+
entry["base_url"] = trimmed_url
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
vault[prov] = entry
|
|
140
|
+
vault["_active_provider"] = prov if make_active
|
|
141
|
+
write_vault_hash!(vault)
|
|
142
|
+
providers_status
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def clear_provider_key!(provider:)
|
|
146
|
+
prov = provider.to_s.strip.downcase
|
|
147
|
+
vault = read_vault_hash
|
|
148
|
+
if prov == "all"
|
|
149
|
+
PROVIDERS.each_key { |k| vault.delete(k) }
|
|
150
|
+
elsif vault[prov].is_a?(Hash)
|
|
151
|
+
vault[prov].delete("api_key")
|
|
152
|
+
end
|
|
153
|
+
write_vault_hash!(vault)
|
|
154
|
+
providers_status
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Internal-only credential resolution for server-side LLM API calls.
|
|
158
|
+
def resolve_credentials!(provider: nil, model_override: nil)
|
|
159
|
+
status = providers_status
|
|
160
|
+
prov = (provider.to_s.strip.empty? ? status[:active_provider] : provider.to_s.strip.downcase)
|
|
161
|
+
meta = PROVIDERS[prov]
|
|
162
|
+
raise ContextDrop::Error, "Unsupported LLM provider '#{prov}'" unless meta
|
|
163
|
+
|
|
164
|
+
vault = read_vault_hash
|
|
165
|
+
vault_entry = vault[prov].is_a?(Hash) ? vault[prov] : {}
|
|
166
|
+
vault_key = vault_entry["api_key"].to_s.strip
|
|
167
|
+
_env_name, env_val = resolve_env_key(meta[:env_keys])
|
|
168
|
+
|
|
169
|
+
api_key = !vault_key.empty? ? vault_key : env_val
|
|
170
|
+
base_url = if meta[:supports_base_url]
|
|
171
|
+
(vault_entry["base_url"].to_s.strip.empty? ? nil : vault_entry["base_url"].to_s.strip) ||
|
|
172
|
+
(@env_vars[meta[:env_base_url].to_s].to_s.strip.empty? ? nil : @env_vars[meta[:env_base_url].to_s].to_s.strip) ||
|
|
173
|
+
meta[:default_base_url]
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
if api_key.empty? && !(prov == "custom" && loopback_base_url?(base_url))
|
|
177
|
+
raise ContextDrop::Error,
|
|
178
|
+
"No API key configured for #{meta[:label]}. Add a BYOK key in the sidebar settings or export #{meta[:env_keys].first}."
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
resolved_model = model_override.to_s.strip
|
|
182
|
+
if resolved_model.empty?
|
|
183
|
+
resolved_model = vault_entry["model"].to_s.strip
|
|
184
|
+
end
|
|
185
|
+
resolved_model = meta[:default_model] if resolved_model.empty?
|
|
186
|
+
|
|
187
|
+
{
|
|
188
|
+
provider: prov,
|
|
189
|
+
api_key: api_key,
|
|
190
|
+
model: resolved_model,
|
|
191
|
+
base_url: base_url
|
|
192
|
+
}
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
private
|
|
196
|
+
|
|
197
|
+
def resolve_env_key(keys)
|
|
198
|
+
Array(keys).each do |k|
|
|
199
|
+
val = @env_vars[k].to_s.strip
|
|
200
|
+
return [k, val] unless val.empty?
|
|
201
|
+
end
|
|
202
|
+
[nil, ""]
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def loopback_base_url?(url_str)
|
|
206
|
+
return false if url_str.to_s.strip.empty?
|
|
207
|
+
|
|
208
|
+
uri = URI.parse(url_str.to_s.strip)
|
|
209
|
+
%w[localhost 127.0.0.1 ::1 [::1]].include?(uri.host.to_s.downcase)
|
|
210
|
+
rescue StandardError
|
|
211
|
+
false
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def mask_secret(raw)
|
|
215
|
+
str = raw.to_s.strip
|
|
216
|
+
return nil if str.empty?
|
|
217
|
+
return "••••••••" if str.length <= 6
|
|
218
|
+
|
|
219
|
+
"••••#{str[-4, 4]}"
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def ensure_master_key!
|
|
223
|
+
dir = File.dirname(@master_key_path)
|
|
224
|
+
FileUtils.mkdir_p(dir)
|
|
225
|
+
|
|
226
|
+
if File.exist?(@master_key_path)
|
|
227
|
+
raw = File.binread(@master_key_path)
|
|
228
|
+
return raw if raw.bytesize == 32
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
key = SecureRandom.random_bytes(32)
|
|
232
|
+
File.open(@master_key_path, File::WRONLY | File::CREAT | File::TRUNC, 0o600) do |f|
|
|
233
|
+
f.binmode
|
|
234
|
+
f.write(key)
|
|
235
|
+
end
|
|
236
|
+
File.chmod(0o600, @master_key_path) rescue nil
|
|
237
|
+
key
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def read_vault_hash
|
|
241
|
+
return {} unless File.exist?(@store_path)
|
|
242
|
+
|
|
243
|
+
payload = JSON.parse(File.read(@store_path))
|
|
244
|
+
iv = ContextDrop::Base64Util.strict_decode64(payload.fetch("iv"))
|
|
245
|
+
auth_tag = ContextDrop::Base64Util.strict_decode64(payload.fetch("tag"))
|
|
246
|
+
ciphertext = ContextDrop::Base64Util.strict_decode64(payload.fetch("data"))
|
|
247
|
+
|
|
248
|
+
decipher = OpenSSL::Cipher.new(CIPHER_NAME)
|
|
249
|
+
decipher.decrypt
|
|
250
|
+
decipher.key = ensure_master_key!
|
|
251
|
+
decipher.iv = iv
|
|
252
|
+
decipher.auth_tag = auth_tag
|
|
253
|
+
|
|
254
|
+
plaintext = decipher.update(ciphertext) + decipher.final
|
|
255
|
+
parsed = JSON.parse(plaintext)
|
|
256
|
+
parsed.is_a?(Hash) ? parsed : {}
|
|
257
|
+
rescue StandardError
|
|
258
|
+
{}
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def write_vault_hash!(hash)
|
|
262
|
+
dir = File.dirname(@store_path)
|
|
263
|
+
FileUtils.mkdir_p(dir)
|
|
264
|
+
|
|
265
|
+
cipher = OpenSSL::Cipher.new(CIPHER_NAME)
|
|
266
|
+
cipher.encrypt
|
|
267
|
+
cipher.key = ensure_master_key!
|
|
268
|
+
iv = cipher.random_iv
|
|
269
|
+
|
|
270
|
+
plaintext = JSON.generate(hash)
|
|
271
|
+
ciphertext = cipher.update(plaintext) + cipher.final
|
|
272
|
+
auth_tag = cipher.auth_tag
|
|
273
|
+
|
|
274
|
+
envelope = JSON.generate(
|
|
275
|
+
v: 1,
|
|
276
|
+
iv: ContextDrop::Base64Util.strict_encode64(iv),
|
|
277
|
+
tag: ContextDrop::Base64Util.strict_encode64(auth_tag),
|
|
278
|
+
data: ContextDrop::Base64Util.strict_encode64(ciphertext)
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
tmp_path = "#{@store_path}.tmp.#{SecureRandom.hex(4)}"
|
|
282
|
+
File.open(tmp_path, File::WRONLY | File::CREAT | File::TRUNC, 0o600) do |f|
|
|
283
|
+
f.write(envelope)
|
|
284
|
+
end
|
|
285
|
+
File.chmod(0o600, tmp_path) rescue nil
|
|
286
|
+
File.rename(tmp_path, @store_path)
|
|
287
|
+
end
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
end
|