rom-encrypted_attribute 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 26555bd77c9580cd7889520b0cd9737065ac1be7b66572770ddae63cd4ffe5df
4
+ data.tar.gz: 5a7b73206a06cadec42c6ad584ad239a3f81bcb9e30790641d4bb1868fd75d22
5
+ SHA512:
6
+ metadata.gz: cf70faabb89dad5e071bae34ad030c87816656a27384c3aed6f4247f8d7d5eef9cb67ce365ec6d5e62a0e4341cc604ab8169e2a6fb641a31fbe71ffa7d02a1ae
7
+ data.tar.gz: 335fe66bfa7993e3a4dd22f2f40b86fd0e6ab85ec94e288804f3abad89a166036da8a62351b495f111437ea7084b46ad32b25d4ae86ab7ecd734fc80c3cf7784
data/.standard.yml ADDED
@@ -0,0 +1,6 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 3.0
4
+ ignore:
5
+ - "test/test_data.rb":
6
+ - Naming/ConstantName
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.4.0, available at [https://www.contributor-covenant.org/version/1/4/code-of-conduct](https://www.contributor-covenant.org/version/1/4/code-of-conduct)
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Paweł Świątkowski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # ROM::EncryptedAttribute
2
+
3
+ This gem adds support for encrypted attributes to [ROM](https://rom-rb.org/).
4
+
5
+ Traditionally ROM team [suggested](https://discourse.rom-rb.org/t/question-encryption-support-thoughts/387) to put encryption logic in repository code (more precisely, in the mapper from database struct to an entity). I personally think this is not the greatest idea. Repository lies logically in the application layer (or even domain layer), while encryption and decryption of data is a purely infrastructure concern. As such, it should be as low-level and hidden as possible.
6
+
7
+ In ROM terms it means doing it in a relation. The gem leverages custom types to achieve encryption and decryption.
8
+
9
+ The scheme is compatible with Rails' default settings for ActiveRecord encryption, so you can still read records encrypted with ActiveRecord from ROM (and vice versa) as long as you provide the same primary key and key derivation salt.
10
+
11
+ ## Installation
12
+
13
+ Install the gem and add to the application's Gemfile by executing:
14
+
15
+ $ bundle add rom-encrypted_attribute
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ $ gem install rom-encrypted_attribute
20
+
21
+ ## Usage
22
+
23
+ In your relation, define custom types using a helper method from the gem. You need to provide the credentials to it somehow. This might be done via environmental variables, Hanami settings (if you're using Hanami) or any other means, really.
24
+
25
+ ```ruby
26
+ class SecretNotes < ROM::Relation[:sql]
27
+ EncryptedString, EncryptedStringReader =
28
+ ROM::EncryptedAttribute.define_encrypted_attribute_types(
29
+ primary_key: ENV["ENCRYPTION_PRIMARY_KEY"],
30
+ key_derivation_salt: ENV["ENCRYPTION_KEY_DERIVATION_SALT"]
31
+ )
32
+
33
+ schema(:secret_notes, infer: true) do
34
+ attribute :content, EncryptedString, read: EncryptedStringReader
35
+ end
36
+ end
37
+ ```
38
+
39
+ By default the gem uses SHA1 for key derivation (same as Rails' default), but you can configure it by passing custom `has_digest_class` option.
40
+
41
+ ``` ruby
42
+ class SecretNotes < ROM::Relation[:sql]
43
+ EncryptedString, EncryptedStringReader =
44
+ ROM::EncryptedAttribute.define_encrypted_attribute_types(
45
+ primary_key: ENV["ENCRYPTION_PRIMARY_KEY"],
46
+ key_derivation_salt: ENV["ENCRYPTION_KEY_DERIVATION_SALT"],
47
+ hash_digest_class: OpenSSL::Digest::SHA256
48
+ )
49
+
50
+ schema(:secret_notes, infer: true) do
51
+ attribute :content, EncryptedString, read: EncryptedStringReader
52
+ end
53
+ end
54
+
55
+ ```
56
+
57
+ ### Caveats
58
+
59
+ * Due to [a bug](https://github.com/rom-rb/rom-sql/issues/423) in `rom-sql`, reading unencrypted data is always supported, which means that if there's a plain not-encrypted data in your database already, it will be read correctly. This might or might not be desirable, but for the time being there's no choice in cofiguring this behaviour.
60
+ * Support for deterministic encryption from `ActiveRecord::Encryption` is not implemented
61
+ * Support for key rotation is not implemented
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/katafrakt/rom-encrypted_attribute.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "standard/rake"
13
+
14
+ task default: %i[test standard]
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "json"
5
+ require_relative "payload"
6
+
7
+ module ROM
8
+ module EncryptedAttribute
9
+ class Decryptor
10
+ def initialize(derivator:)
11
+ @derivator = derivator
12
+ end
13
+
14
+ def decrypt(message)
15
+ payload = ROM::EncryptedAttribute::Payload.decode(message)
16
+
17
+ cipher = OpenSSL::Cipher.new("aes-256-gcm")
18
+ key = @derivator.derive(cipher.key_len)
19
+
20
+ cipher.decrypt
21
+ cipher.padding = 0
22
+ cipher.key = key
23
+ cipher.iv = payload.initialization_vector
24
+ cipher.auth_tag = payload.auth_tag
25
+ cipher.auth_data = ""
26
+ cipher.update(payload.message) + cipher.final
27
+ rescue JSON::ParserError
28
+ # we need to unconditionally support of reading unencrypted data due to a bug in rom-sql
29
+ # https://github.com/rom-rb/rom-sql/issues/423
30
+ message
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "json"
5
+ require "openssl"
6
+ require_relative "payload"
7
+
8
+ module ROM
9
+ module EncryptedAttribute
10
+ class Encryptor
11
+ def initialize(derivator:)
12
+ @derivator = derivator
13
+ end
14
+
15
+ def encrypt(message)
16
+ cipher = OpenSSL::Cipher.new("aes-256-gcm")
17
+ key = @derivator.derive(cipher.key_len)
18
+ iv = cipher.random_iv
19
+
20
+ cipher.encrypt
21
+ cipher.key = key
22
+ cipher.iv = iv
23
+ encrypted = cipher.update(message) + cipher.final
24
+ Payload.new(message: encrypted, initialization_vector: iv, auth_tag: cipher.auth_tag).encode
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+
5
+ module ROM
6
+ module EncryptedAttribute
7
+ class KeyDerivator
8
+ DEFAULT_DIGEST_CLASS = OpenSSL::Digest::SHA1
9
+ ITERATIONS = 2**16
10
+
11
+ def initialize(secret:, salt:, hash_digest_class: DEFAULT_DIGEST_CLASS)
12
+ @secret = secret
13
+ @salt = salt
14
+ @hash_digest_class = hash_digest_class
15
+ end
16
+
17
+ def derive(size)
18
+ OpenSSL::PKCS5.pbkdf2_hmac(@secret, @salt, ITERATIONS, size, @hash_digest_class.new)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/types"
4
+ require "dry/struct"
5
+ require "base64"
6
+
7
+ module ROM
8
+ class EncryptedAttribute::Payload < Dry::Struct
9
+ class Types
10
+ include Dry.Types()
11
+ end
12
+
13
+ attribute :message, Types::Strict::String
14
+ attribute :initialization_vector, Types::Strict::String
15
+ attribute :auth_tag, Types::Strict::String
16
+
17
+ def self.decode(database_value)
18
+ payload = JSON.parse(database_value)
19
+ new(
20
+ message: decode64(payload["p"]),
21
+ initialization_vector: decode64(payload.dig("h", "iv")),
22
+ auth_tag: decode64(payload.dig("h", "at"))
23
+ )
24
+ end
25
+
26
+ def self.decode64(value)
27
+ Base64.strict_decode64(value)
28
+ end
29
+
30
+ def encode
31
+ payload =
32
+ {
33
+ "p" => encode64(message),
34
+ "h" => {
35
+ "iv" => encode64(initialization_vector),
36
+ "at" => encode64(auth_tag)
37
+ }
38
+ }
39
+
40
+ JSON.dump(payload)
41
+ end
42
+
43
+ private
44
+
45
+ def encode64(value)
46
+ Base64.strict_encode64(value)
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ROM
4
+ module EncryptedAttribute
5
+ VERSION = "0.0.3"
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "encrypted_attribute/key_derivator"
4
+ require_relative "encrypted_attribute/decryptor"
5
+ require_relative "encrypted_attribute/encryptor"
6
+ require_relative "encrypted_attribute/version"
7
+
8
+ require "dry/types"
9
+
10
+ module ROM
11
+ module EncryptedAttribute
12
+ def self.define_encrypted_attribute_types(primary_key:, key_derivation_salt:, hash_digest_class: OpenSSL::Digest::SHA1)
13
+ key_derivator = KeyDerivator.new(salt: key_derivation_salt, secret: primary_key,
14
+ hash_digest_class: hash_digest_class)
15
+
16
+ reader_type = Dry.Types.Constructor(String) do |value|
17
+ ROM::EncryptedAttribute::Decryptor.new(derivator: key_derivator).decrypt(value)
18
+ end
19
+
20
+ writer_type = Dry.Types.Constructor(String) do |value|
21
+ ROM::EncryptedAttribute::Encryptor.new(derivator: key_derivator).encrypt(value)
22
+ end
23
+
24
+ [writer_type, reader_type]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rom/encrypted_attribute/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rom-encrypted_attribute"
7
+ spec.version = ROM::EncryptedAttribute::VERSION
8
+ spec.authors = ["Paweł Świątkowski"]
9
+ spec.email = ["katafrakt@vivaldi.net"]
10
+
11
+ spec.summary = "Encrypted attributes for ROM"
12
+ spec.required_ruby_version = ">= 3.0.0"
13
+ spec.metadata["source_code_uri"] = "https://github.com/katafrakt/rom-encrypted_attribute"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(__dir__) do
18
+ `git ls-files -z`.split("\x0").reject do |f|
19
+ (File.expand_path(f) == __FILE__) ||
20
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
21
+ end
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ # Uncomment to register a new dependency of your gem
28
+ spec.add_dependency "dry-types", "~> 1.5"
29
+
30
+ spec.add_development_dependency "activerecord", ">= 7.0"
31
+ spec.add_development_dependency "rom-sql", ">= 3.0"
32
+ spec.add_development_dependency "sqlite3", ">= 1.0"
33
+
34
+ # For more information and examples about making a new gem, check out our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rom-encrypted_attribute
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Świątkowski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-types
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '7.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '7.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rom-sql
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description:
70
+ email:
71
+ - katafrakt@vivaldi.net
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".standard.yml"
77
+ - CODE_OF_CONDUCT.md
78
+ - LICENSE
79
+ - README.md
80
+ - Rakefile
81
+ - lib/rom/encrypted_attribute.rb
82
+ - lib/rom/encrypted_attribute/decryptor.rb
83
+ - lib/rom/encrypted_attribute/encryptor.rb
84
+ - lib/rom/encrypted_attribute/key_derivator.rb
85
+ - lib/rom/encrypted_attribute/payload.rb
86
+ - lib/rom/encrypted_attribute/version.rb
87
+ - rom-encrypted_attribute.gemspec
88
+ homepage:
89
+ licenses: []
90
+ metadata:
91
+ source_code_uri: https://github.com/katafrakt/rom-encrypted_attribute
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 3.0.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.5.16
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Encrypted attributes for ROM
111
+ test_files: []