doc_vault 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fda91ff32053768d255c4761dae6e0650a7a9fbf04a36a0d25fe20f240ed5fcb
4
+ data.tar.gz: 74aeecbb04a088aeaea7c2f76e60eb3930165a044b13e3e62a7ca6c73c3de163
5
+ SHA512:
6
+ metadata.gz: 7ed12ed1b2a51797ed200e281509c23097ab07bec11006dec0d7ba33ed3bec3c7dc9bafd74bf7a221e8ba961e99552a9be7842b039ed0acd54b6902751bfc09c
7
+ data.tar.gz: bc8d81a0230d353a7d348390b76a6af6ca659145dc80c369bd3364587c6e7fdc26b70a8ad0c75d835840d3401f80fa8460932113b6624c707bad18b1ca289ca9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Jared Fraser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,172 @@
1
+ # DocVault
2
+
3
+ NOTE: THIS IS IN-DEVELOPMENT SOFTWARE, PLEASE DO NOT USE FOR ANYTHING SENSITIVE
4
+
5
+ DocVault is a Ruby gem that provides a simple interface for storing and retrieving encrypted data or files in one or multiple SQLite databases.
6
+
7
+ This gem is useful for ephemeral storage of sensitive data in text or file format that needs to be readily accessible. It enables seperation of data and per-user encryption keys.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'doc_vault'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```bash
20
+ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```bash
26
+ gem install doc_vault
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Basic Usage
32
+
33
+ Store a document:
34
+
35
+ ```ruby
36
+ require 'doc_vault'
37
+
38
+ # Store a document
39
+ DocVault.store(
40
+ "This is my secret document",
41
+ id: "document_123",
42
+ bucket: "my_database",
43
+ key: "my_encryption_key"
44
+ )
45
+ ```
46
+
47
+ Retrieve a document:
48
+
49
+ ```ruby
50
+ # Retrieve the document
51
+ document = DocVault.retrieve(
52
+ "document_123",
53
+ bucket: "my_database",
54
+ key: "my_encryption_key"
55
+ )
56
+
57
+ puts document # => "This is my secret document"
58
+ ```
59
+
60
+ ### Multiple Databases
61
+
62
+ You can organize documents across different databases:
63
+
64
+ ```ruby
65
+ # Store in different databases
66
+ DocVault.store("Personal note", id: "note1", bucket: "personal", key: "personal_key")
67
+ DocVault.store("Work document", id: "doc1", bucket: "work", key: "work_key")
68
+
69
+ # Retrieve from specific databases
70
+ personal_note = DocVault.retrieve("note1", bucket: "personal", key: "personal_key")
71
+ work_doc = DocVault.retrieve("doc1", bucket: "work", key: "work_key")
72
+ ```
73
+
74
+ ### File Storage
75
+
76
+ DocVault can store and retrieve File and Tempfile objects while preserving metadata:
77
+
78
+ ```ruby
79
+ # Store a File object
80
+ file = File.open("document.pdf", "rb")
81
+ DocVault.store(file, id: "pdf_doc", bucket: "files", key: "file_key")
82
+ file.close
83
+
84
+ # Retrieve returns a Tempfile with the same content
85
+ retrieved_file = DocVault.retrieve("pdf_doc", bucket: "files", key: "file_key")
86
+ puts retrieved_file.class # => Tempfile
87
+ puts retrieved_file.read # => Original PDF content
88
+
89
+ # Store a Tempfile - upload is a JPEG File object
90
+ DocVault.store(upload, id: "photo", bucket: "uploads", key: "upload_key")
91
+ upload.close
92
+
93
+ # Retrieve with preserved metadata
94
+ photo = DocVault.retrieve("photo", bucket: "uploads", key: "upload_key")
95
+ puts photo.original_filename # => "photo.jpg"
96
+ puts photo.content_type # => "image/jpeg"
97
+ puts File.extname(photo.path) # => ".jpg"
98
+ ```
99
+
100
+ ### Document Updates
101
+
102
+ Documents with the same ID will be replaced:
103
+
104
+ ```ruby
105
+ # Store initial document
106
+ DocVault.store("Version 1", id: "doc", bucket: "db", key: "key")
107
+
108
+ # Update the document
109
+ DocVault.store("Version 2", id: "doc", bucket: "db", key: "key")
110
+
111
+ # Retrieve returns the latest version
112
+ doc = DocVault.retrieve("doc", bucket: "db", key: "key")
113
+ puts doc # => "Version 2"
114
+ ```
115
+
116
+ ### Error Handling
117
+
118
+ DocVault provides specific error types for different failure scenarios:
119
+
120
+ ```ruby
121
+ begin
122
+ DocVault.retrieve("nonexistent", bucket: "db", key: "key")
123
+ rescue DocVault::DocumentNotFoundError => e
124
+ puts "Document not found: #{e.message}"
125
+ end
126
+
127
+ begin
128
+ DocVault.retrieve("doc_id", bucket: "db", key: "wrong_key")
129
+ rescue DocVault::EncryptionError => e
130
+ puts "Decryption failed: #{e.message}"
131
+ end
132
+
133
+ begin
134
+ DocVault.store("", id: "", bucket: "db", key: "key")
135
+ rescue ArgumentError => e
136
+ puts "Invalid arguments: #{e.message}"
137
+ end
138
+ ```
139
+
140
+ ### Configuration
141
+
142
+ You can configure where DocVault stores its database files:
143
+
144
+ ```ruby
145
+ # Configure database path using a block
146
+ DocVault.configure do |config|
147
+ config.database_path = "/path/to/your/databases"
148
+ end
149
+
150
+ # Or configure directly
151
+ DocVault.configuration.database_path = "/path/to/your/databases"
152
+ ```
153
+
154
+ ## Security
155
+
156
+ - Documents are encrypted using AES-256-GCM
157
+ - SQLite databases are created locally and can be secured using standard file system permissions
158
+ - Database files can be culled with standard file retention tools
159
+
160
+ ## Development
161
+
162
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
163
+
164
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
165
+
166
+ ## Contributing
167
+
168
+ Bug reports and pull requests are welcome on GitHub at https://github.com/modsognir/doc_vault.
169
+
170
+ ## License
171
+
172
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocVault
4
+ class Configuration
5
+ attr_reader :database_path
6
+
7
+ def initialize
8
+ @database_path = Dir.pwd
9
+ end
10
+
11
+ def database_path=(path)
12
+ raise ArgumentError, "Database path cannot be nil" if path.nil?
13
+ raise ArgumentError, "Database path must be a valid directory" unless Dir.exist?(path) || path == Dir.pwd
14
+
15
+ @database_path = File.expand_path(path)
16
+ end
17
+
18
+ def full_database_path(db_name)
19
+ File.join(@database_path, "#{db_name}.db")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "sqlite3"
4
+
5
+ module DocVault
6
+ class Database
7
+ def initialize(db_name)
8
+ @db_name = db_name
9
+ @db_path = DocVault.configuration.full_database_path(db_name)
10
+ create_table_if_not_exists
11
+ end
12
+
13
+ def store(id, encrypted_document)
14
+ result = db.execute("INSERT OR REPLACE INTO documents (id, content) VALUES (?, ?)", [id, encrypted_document])
15
+ id if result
16
+ rescue SQLite3::Exception => e
17
+ raise DatabaseError, "Failed to store document: #{e.message}"
18
+ end
19
+
20
+ def retrieve(id)
21
+ result = db.execute("SELECT content FROM documents WHERE id = ?", [id])
22
+ result.empty? ? nil : result[0][0]
23
+ rescue SQLite3::Exception => e
24
+ raise DatabaseError, "Failed to retrieve document: #{e.message}"
25
+ end
26
+
27
+ private
28
+
29
+ def db
30
+ @db ||= SQLite3::Database.new(@db_path)
31
+ end
32
+
33
+ def create_table_if_not_exists
34
+ db.execute <<-SQL
35
+ CREATE TABLE IF NOT EXISTS documents (
36
+ id TEXT PRIMARY KEY,
37
+ content TEXT NOT NULL,
38
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
39
+ updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
40
+ )
41
+ SQL
42
+ rescue SQLite3::Exception => e
43
+ raise DatabaseError, "Failed to create table: #{e.message}"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "tempfile"
5
+
6
+ module DocVault
7
+ class DocumentSerializer
8
+ def self.serialize(document)
9
+ case document
10
+ when File, Tempfile
11
+ serialize_file(document)
12
+ when String
13
+ serialize_string(document)
14
+ else
15
+ raise ArgumentError, "Document must be a String, File, or Tempfile"
16
+ end
17
+ end
18
+
19
+ def self.deserialize(data)
20
+ parsed = JSON.parse(data)
21
+
22
+ case parsed["type"]
23
+ when "file"
24
+ deserialize_file(parsed)
25
+ when "string"
26
+ parsed["content"]
27
+ else
28
+ raise ArgumentError, "Unknown document type: #{parsed["type"]}"
29
+ end
30
+ rescue JSON::ParserError
31
+ raise ArgumentError, "Invalid serialized document data"
32
+ end
33
+
34
+ class << self
35
+ private
36
+
37
+ def serialize_file(file)
38
+ file.rewind
39
+ content = file.read
40
+ file.rewind
41
+
42
+ metadata = {
43
+ type: "file",
44
+ content: content,
45
+ original_filename: file.respond_to?(:original_filename) ? file.original_filename : nil,
46
+ content_type: file.respond_to?(:content_type) ? file.content_type : nil,
47
+ path: file.respond_to?(:path) ? file.path : nil,
48
+ size: content.length
49
+ }
50
+
51
+ JSON.generate(metadata)
52
+ end
53
+
54
+ def serialize_string(string)
55
+ JSON.generate({
56
+ type: "string",
57
+ content: string
58
+ })
59
+ end
60
+
61
+ def deserialize_file(parsed)
62
+ tempfile = Tempfile.new(["doc_vault", extract_extension(parsed)])
63
+ tempfile.binmode
64
+ tempfile.write(parsed["content"])
65
+ tempfile.rewind
66
+
67
+ # Add metadata as singleton methods if available
68
+ if parsed["original_filename"]
69
+ tempfile.define_singleton_method(:original_filename) { parsed["original_filename"] }
70
+ end
71
+
72
+ if parsed["content_type"]
73
+ tempfile.define_singleton_method(:content_type) { parsed["content_type"] }
74
+ end
75
+
76
+ tempfile
77
+ end
78
+
79
+ def extract_extension(parsed)
80
+ return "" unless parsed["original_filename"] || parsed["path"]
81
+
82
+ filename = parsed["original_filename"] || parsed["path"]
83
+ File.extname(filename)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require "base64"
5
+
6
+ module DocVault
7
+ class Encryption
8
+ ALGORITHM = "AES-256-GCM"
9
+ IV_SIZE = 12
10
+ AUTH_TAG_SIZE = 16
11
+
12
+ def self.encrypt(data, key)
13
+ cipher = OpenSSL::Cipher.new(ALGORITHM)
14
+ cipher.encrypt
15
+
16
+ salt = OpenSSL::Random.random_bytes(16)
17
+ derived_key = OpenSSL::PKCS5.pbkdf2_hmac(key, salt, 100_000, 32, OpenSSL::Digest.new("SHA256"))
18
+ cipher.key = derived_key
19
+
20
+ iv = cipher.random_iv
21
+ encrypted = cipher.update(data) + cipher.final
22
+ auth_tag = cipher.auth_tag
23
+
24
+ combined = salt + iv + auth_tag + encrypted
25
+ Base64.strict_encode64(combined)
26
+ rescue OpenSSL::Cipher::CipherError
27
+ raise EncryptionError, "Encryption failed"
28
+ end
29
+
30
+ def self.decrypt(encrypted_data, key)
31
+ combined = Base64.strict_decode64(encrypted_data)
32
+
33
+ cipher = OpenSSL::Cipher.new(ALGORITHM)
34
+ cipher.decrypt
35
+
36
+ salt = combined[0, 16]
37
+ iv = combined[16, IV_SIZE]
38
+ auth_tag = combined[16 + IV_SIZE, AUTH_TAG_SIZE]
39
+ encrypted = combined[16 + IV_SIZE + AUTH_TAG_SIZE..]
40
+
41
+ derived_key = OpenSSL::PKCS5.pbkdf2_hmac(key, salt, 100_000, 32, OpenSSL::Digest.new("SHA256"))
42
+ cipher.key = derived_key
43
+ cipher.iv = iv
44
+ cipher.auth_tag = auth_tag
45
+
46
+ cipher.update(encrypted) + cipher.final
47
+ rescue OpenSSL::Cipher::CipherError, ArgumentError
48
+ raise EncryptionError, "Decryption failed"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocVault
4
+ class RetrieveDocument
5
+ def self.call(id:, bucket:, key:)
6
+ new(id:, bucket:, key:).call
7
+ end
8
+
9
+ def initialize(id:, bucket:, key:)
10
+ @id = id
11
+ @bucket = bucket
12
+ @key = key
13
+ validate_params
14
+ end
15
+
16
+ def call
17
+ database = Database.new(@bucket)
18
+ encrypted_doc = database.retrieve(@id)
19
+ raise DocumentNotFoundError, "Document with id '#{@id}' not found" unless encrypted_doc
20
+
21
+ decrypted_doc = Encryption.decrypt(encrypted_doc, @key)
22
+ DocumentSerializer.deserialize(decrypted_doc)
23
+ end
24
+
25
+ private
26
+
27
+ def validate_params
28
+ raise ArgumentError, "Document ID cannot be nil or empty" if @id.nil? || @id.to_s.strip.empty?
29
+ raise ArgumentError, "Database name cannot be nil or empty" if @bucket.nil? || @bucket.to_s.strip.empty?
30
+ raise ArgumentError, "Encryption key cannot be nil or empty" if @key.nil? || @key.to_s.strip.empty?
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocVault
4
+ class StoreDocument
5
+ def self.call(document:, id:, bucket:, key:)
6
+ new(document:, id:, bucket:, key:).call
7
+ end
8
+
9
+ def initialize(document:, id:, bucket:, key:)
10
+ @document = document
11
+ @id = id
12
+ @bucket = bucket
13
+ @key = key
14
+ validate_params
15
+ end
16
+
17
+ def call
18
+ database = Database.new(@bucket)
19
+ serialized_doc = DocumentSerializer.serialize(@document)
20
+ encrypted_doc = Encryption.encrypt(serialized_doc, @key)
21
+ database.store(@id, encrypted_doc)
22
+ end
23
+
24
+ private
25
+
26
+ def validate_params
27
+ raise ArgumentError, "Document cannot be nil" if @document.nil?
28
+ raise ArgumentError, "Document ID cannot be nil or empty" if @id.nil? || @id.to_s.strip.empty?
29
+ raise ArgumentError, "Database name cannot be nil or empty" if @bucket.nil? || @bucket.to_s.strip.empty?
30
+ raise ArgumentError, "Encryption key cannot be nil or empty" if @key.nil? || @key.to_s.strip.empty?
31
+
32
+ unless @document.is_a?(String) || @document.is_a?(File) || @document.is_a?(Tempfile)
33
+ raise ArgumentError, "Document must be a String, File, or Tempfile"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocVault
4
+ VERSION = "0.1.0"
5
+ end
data/lib/doc_vault.rb ADDED
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "doc_vault/version"
4
+ require_relative "doc_vault/configuration"
5
+ require_relative "doc_vault/database"
6
+ require_relative "doc_vault/encryption"
7
+ require_relative "doc_vault/document_serializer"
8
+ require_relative "doc_vault/store_document"
9
+ require_relative "doc_vault/retrieve_document"
10
+ require "sqlite3"
11
+
12
+ module DocVault
13
+ class Error < StandardError; end
14
+
15
+ class DatabaseError < Error; end
16
+
17
+ class EncryptionError < Error; end
18
+
19
+ class DocumentNotFoundError < Error; end
20
+
21
+ def self.store(document, id:, bucket:, key:)
22
+ StoreDocument.call(document:, id:, bucket:, key:)
23
+ end
24
+
25
+ def self.retrieve(id, bucket:, key:)
26
+ RetrieveDocument.call(id:, bucket:, key:)
27
+ end
28
+
29
+ def self.configuration
30
+ @configuration ||= Configuration.new
31
+ end
32
+
33
+ def self.configure
34
+ yield(configuration)
35
+ end
36
+
37
+ def self.reset_configuration!
38
+ @configuration = Configuration.new
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doc_vault
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jared Fraser
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 2025-08-27 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: sqlite3
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '1.6'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '1.6'
26
+ - !ruby/object:Gem::Dependency
27
+ name: base64
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '0.1'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.1'
40
+ description: Store and retrieve encrypted documents in SQLite
41
+ email:
42
+ - dev@jsf.io
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".rspec"
48
+ - ".standard.yml"
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - lib/doc_vault.rb
53
+ - lib/doc_vault/configuration.rb
54
+ - lib/doc_vault/database.rb
55
+ - lib/doc_vault/document_serializer.rb
56
+ - lib/doc_vault/encryption.rb
57
+ - lib/doc_vault/retrieve_document.rb
58
+ - lib/doc_vault/store_document.rb
59
+ - lib/doc_vault/version.rb
60
+ homepage: https://github.com/modsognir/doc_vault
61
+ licenses:
62
+ - MIT
63
+ metadata:
64
+ homepage_uri: https://github.com/modsognir/doc_vault
65
+ source_code_uri: https://github.com/modsognir/doc_vault
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 3.1.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.6.2
81
+ specification_version: 4
82
+ summary: Store and retrieve encrypted documents in SQLite
83
+ test_files: []