Class: CredentialStore
- Inherits:
-
Object
- Object
- CredentialStore
- Defined in:
- lib/credential_store.rb
Overview
Low-level read/write operations on Rails encrypted credentials. Wraps the merge-and-write pattern used by SessionChannel#write_anthropic_token in a reusable helper. All namespacing (e.g. mcp, anthropic) is the caller’s responsibility — this class operates on raw top-level keys.
Class Method Summary collapse
-
.list(namespace) ⇒ Array<String>
Lists all keys under a namespace.
-
.read(namespace, key) ⇒ String?
Reads a single credential value from a namespace.
-
.remove(namespace, key) ⇒ void
Removes a single key from a namespace.
-
.write(namespace, pairs) ⇒ void
Writes one or more key-value pairs under a top-level namespace.
Class Method Details
.list(namespace) ⇒ Array<String>
Lists all keys under a namespace.
45 46 47 48 49 50 |
# File 'lib/credential_store.rb', line 45 def list(namespace) section = Rails.application.credentials.dig(namespace.to_sym) return [] unless section.is_a?(Hash) section.keys.map(&:to_s) end |
.read(namespace, key) ⇒ String?
Reads a single credential value from a namespace. Busts the Rails credentials cache first so cross-process writes (e.g. token saved in the web process, read in the SolidQueue worker) are always visible.
36 37 38 39 |
# File 'lib/credential_store.rb', line 36 def read(namespace, key) bust_credentials_cache! Rails.application.credentials.dig(namespace.to_sym, key.to_sym) end |
.remove(namespace, key) ⇒ void
This method returns an undefined value.
Removes a single key from a namespace. No-op if the key does not exist.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/credential_store.rb', line 58 def remove(namespace, key) existing = load_credentials section = existing[namespace] return unless section.is_a?(Hash) return unless section.key?(key) section.delete(key) existing.delete(namespace) if section.empty? save_credentials(existing) end |
.write(namespace, pairs) ⇒ void
This method returns an undefined value.
Writes one or more key-value pairs under a top-level namespace. Merges into existing credentials, preserving sibling keys.
21 22 23 24 25 26 |
# File 'lib/credential_store.rb', line 21 def write(namespace, pairs) existing = load_credentials section = existing[namespace] ||= {} section.merge!(pairs) save_credentials(existing) end |