Class: SshTresor::Vault
- Inherits:
-
Object
- Object
- SshTresor::Vault
- Defined in:
- lib/ssh_tresor/vault.rb
Overview
Public high-level API for encrypting and decrypting tresors from another Ruby application or gem.
‘Vault` is intentionally a small facade over the lower-level SSH agent, crypto, and wire-format objects. It connects to `SSH_AUTH_SOCK` by default, but accepts an injected agent object for tests or alternate transports.
Instance Method Summary collapse
-
#add_all_keys(encrypted, armor: nil) ⇒ Array(String, Integer)
Adds slots for all available SSH agent keys not already present.
-
#add_key(encrypted, fingerprint:, armor: nil) ⇒ String
Adds one SSH key slot to an existing tresor.
-
#decrypt(encrypted) ⇒ String
Decrypts an encrypted tresor using any matching key in the SSH agent.
-
#encrypt(plaintext, fingerprints: [], armor: false) ⇒ String
Encrypts plaintext for one or more keys available in the SSH agent.
-
#initialize(agent: Agent.connect) ⇒ SshTresor::Vault
constructor
Creates a vault bound to an SSH agent.
-
#list_keys ⇒ Array<SshTresor::AgentKey>
Lists keys currently available through the configured SSH agent.
-
#list_slots(encrypted) ⇒ Array<String>
Lists key slot fingerprints present in encrypted tresor content.
-
#remove_key(encrypted, fingerprint:, armor: nil) ⇒ String
Removes one key slot from an existing tresor.
Constructor Details
#initialize(agent: Agent.connect) ⇒ SshTresor::Vault
34 35 36 |
# File 'lib/ssh_tresor/vault.rb', line 34 def initialize(agent: Agent.connect) @agent = agent end |
Instance Method Details
#add_all_keys(encrypted, armor: nil) ⇒ Array(String, Integer)
Adds slots for all available SSH agent keys not already present.
Keys that are already present or cannot sign are skipped.
95 96 97 98 99 100 |
# File 'lib/ssh_tresor/vault.rb', line 95 def add_all_keys(encrypted, armor: nil) input_was_armored = armored?(encrypted) blob = TresorBlob.from_bytes(encrypted) updated, added = Tresor.add_all_keys_with_agent(@agent, blob) [serialize(updated, armor.nil? ? input_was_armored : armor), added] end |
#add_key(encrypted, fingerprint:, armor: nil) ⇒ String
Adds one SSH key slot to an existing tresor.
The current agent must be able to decrypt an existing slot before adding a new one, because the master key must be recovered and re-wrapped for the new key.
80 81 82 83 84 85 |
# File 'lib/ssh_tresor/vault.rb', line 80 def add_key(encrypted, fingerprint:, armor: nil) input_was_armored = armored?(encrypted) blob = TresorBlob.from_bytes(encrypted) updated = Tresor.add_key_with_agent(@agent, blob, fingerprint) serialize(updated, armor.nil? ? input_was_armored : armor) end |
#decrypt(encrypted) ⇒ String
Decrypts an encrypted tresor using any matching key in the SSH agent.
The input may be binary ‘SSHTRESR` v3 data or armored text. The agent is asked to sign the stored slot challenge for matching key fingerprints.
64 65 66 |
# File 'lib/ssh_tresor/vault.rb', line 64 def decrypt(encrypted) Tresor.decrypt_with_agent(@agent, TresorBlob.from_bytes(encrypted)) end |
#encrypt(plaintext, fingerprints: [], armor: false) ⇒ String
Encrypts plaintext for one or more keys available in the SSH agent.
When no fingerprints are given, the first key returned by the agent is used. Fingerprints may be full ‘SHA256:…` values or unambiguous prefixes.
49 50 51 52 |
# File 'lib/ssh_tresor/vault.rb', line 49 def encrypt(plaintext, fingerprints: [], armor: false) blob = Tresor.encrypt_with_agent(@agent, plaintext, fingerprints: fingerprints) armor ? blob.to_armored : blob.to_bytes end |
#list_keys ⇒ Array<SshTresor::AgentKey>
Lists keys currently available through the configured SSH agent.
124 125 126 |
# File 'lib/ssh_tresor/vault.rb', line 124 def list_keys @agent.list_keys end |
#list_slots(encrypted) ⇒ Array<String>
Lists key slot fingerprints present in encrypted tresor content.
This does not require access to an SSH agent because slot fingerprints are stored in the tresor header.
136 137 138 |
# File 'lib/ssh_tresor/vault.rb', line 136 def list_slots(encrypted) TresorBlob.from_bytes(encrypted).slot_fingerprints end |
#remove_key(encrypted, fingerprint:, armor: nil) ⇒ String
Removes one key slot from an existing tresor.
This operation only edits metadata and does not require the SSH agent to hold the removed key. Removing the final slot is rejected.
113 114 115 116 117 118 |
# File 'lib/ssh_tresor/vault.rb', line 113 def remove_key(encrypted, fingerprint:, armor: nil) input_was_armored = armored?(encrypted) blob = TresorBlob.from_bytes(encrypted) updated = Tresor.remove_key(blob, fingerprint) serialize(updated, armor.nil? ? input_was_armored : armor) end |