rom_encrypted_attribute 0.0.1

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: 4fa68f6b6119a75fb160eed6c69ecb341f0966e09b2e1e2d234d0ce20e1f2a1c
4
+ data.tar.gz: 4f33329807bc3337d001ebe8c4840ae10ed70272db064343588fc38c444f0452
5
+ SHA512:
6
+ metadata.gz: 3ca2ba7ffdb4b96fdc97ca403100bd996593724953a9938483619f82916d62ac7a2c7161e19401fef5cad66837ea6571235dc8064cdb0a7aace0aae3a1776453
7
+ data.tar.gz: cc3dc7e8b45d235d5fdf10cd069b84c9ee8a590fcc9247d6df36fedf3529634980c95a32b5978030294cd36ed26858aea4ca4ad97b5debcf1f957dffacfe0957
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,49 @@
1
+ # RomEncryptedAttribute
2
+
3
+ This gem adds support for encrypted fields using [ROM](https://rom-rb.org/).
4
+
5
+ Traditionally ROM suggested 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 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. This gem uses 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 encrypted records as long as you provide the same primary key and salt derivation key.
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
+ RomEncryptedAttribute.define_encrypted_attribute_types(
29
+ primary_key: ENV["ENCRYPTION_PRIMARY_KEY"],
30
+ derivation_salt: ENV["ENCRYPTION_DERIVATION_SALT"]
31
+ )
32
+
33
+ schema(:secret_notes, infer: true) do
34
+ attribute :content, EncryptedString, read: EncryptedStringReader
35
+ end
36
+ end
37
+ ```
38
+
39
+ Of course, you can define it somewhere else and just `include` in the relation or use your custom types code organization.
40
+
41
+ ### Caveats
42
+
43
+ * Due to a bug in `rom-sql`, reading unencrypted data is turned on by default
44
+ * The gem uses SHA256 for key derivation and it's currently not configurable
45
+ * Support for deterministic encryption from `ActiveRecord::Entryption` is not implemented
46
+
47
+ ## Contributing
48
+
49
+ 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,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "json"
5
+ require_relative "key_derivator"
6
+
7
+ module RomEncryptedAttribute
8
+ class Decryptor
9
+ def initialize(secret:, salt:)
10
+ @derivator = KeyDerivator.new(secret: secret, salt: salt)
11
+ end
12
+
13
+ def decrypt(message)
14
+ message = JSON.parse(message)
15
+ data = Base64.strict_decode64(message["p"])
16
+ iv = Base64.strict_decode64(message["h"]["iv"])
17
+ auth_tag = Base64.strict_decode64(message["h"]["at"])
18
+
19
+ cipher = OpenSSL::Cipher.new("aes-256-gcm")
20
+ key = @derivator.derive(cipher.key_len)
21
+
22
+ cipher.decrypt
23
+ cipher.padding = 0
24
+ cipher.key = key
25
+ cipher.iv = iv
26
+ cipher.auth_tag = auth_tag
27
+ cipher.auth_data = ""
28
+ cipher.update(data) + cipher.final
29
+ rescue JSON::ParserError
30
+ # we need to unconditionally support of reading unencrypted data due to a bug in rom-sql
31
+ # https://github.com/rom-rb/rom-sql/issues/423
32
+ message
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "json"
5
+ require "openssl"
6
+ require_relative "key_derivator"
7
+
8
+ module RomEncryptedAttribute
9
+ class Encryptor
10
+ def initialize(secret:, salt:)
11
+ @derivator = KeyDerivator.new(secret: secret, salt: salt)
12
+ end
13
+
14
+ def encrypt(message)
15
+ cipher = OpenSSL::Cipher.new("aes-256-gcm")
16
+ key = @derivator.derive(cipher.key_len)
17
+ iv = cipher.random_iv
18
+
19
+ cipher.encrypt
20
+ cipher.key = key
21
+ cipher.iv = iv
22
+ encrypted = cipher.update(message) + cipher.final
23
+ serialize(encrypted, cipher: cipher, iv: iv)
24
+ end
25
+
26
+ private
27
+
28
+ def serialize(encrypted, iv:, cipher:)
29
+ payload =
30
+ {
31
+ "p" => Base64.strict_encode64(encrypted),
32
+ "h" => {
33
+ "iv" => Base64.strict_encode64(iv),
34
+ "at" => Base64.strict_encode64(cipher.auth_tag)
35
+ }
36
+ }
37
+
38
+ JSON.dump(payload)
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+
5
+ module RomEncryptedAttribute
6
+ class KeyDerivator
7
+ DIGEST_CLASS = OpenSSL::Digest::SHA256
8
+ ITERATIONS = 2**16
9
+
10
+ def initialize(secret:, salt:)
11
+ @secret = secret
12
+ @salt = salt
13
+ end
14
+
15
+ def derive(size)
16
+ OpenSSL::PKCS5.pbkdf2_hmac(@secret, @salt, ITERATIONS, size, DIGEST_CLASS.new)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RomEncryptedAttribute
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rom_encrypted_attribute/decryptor"
4
+ require_relative "rom_encrypted_attribute/encryptor"
5
+ require_relative "rom_encrypted_attribute/version"
6
+
7
+ require "dry/types"
8
+
9
+ module RomEncryptedAttribute
10
+ def self.define_encrypted_attribute_types(primary_key:, derivation_salt:)
11
+ reader_type = Dry.Types.Constructor(String) do |value|
12
+ RomEncryptedAttribute::Decryptor.new(secret: primary_key, salt: derivation_salt).decrypt(value)
13
+ end
14
+
15
+ writer_type = Dry.Types.Constructor(String) do |value|
16
+ RomEncryptedAttribute::Encryptor.new(secret: primary_key, salt: derivation_salt).encrypt(value)
17
+ end
18
+
19
+ [writer_type, reader_type]
20
+ end
21
+ 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 = RomEncryptedAttribute::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,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rom_encrypted_attribute
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paweł Świątkowski
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-07 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/version.rb
86
+ - rom_encrypted_attribute.gemspec
87
+ homepage:
88
+ licenses: []
89
+ metadata:
90
+ source_code_uri: https://github.com/katafrakt/rom_encrypted_attribute
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: 3.0.0
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubygems_version: 3.4.21
107
+ signing_key:
108
+ specification_version: 4
109
+ summary: Encrypted attributes for ROM
110
+ test_files: []