cryptoform 0.1.0

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: 0dd3d188f2a8b12f155a5eeb733f63bbef247161aa0d2baf33801bdd5e814bc1
4
+ data.tar.gz: c2d99e642a3f8a5e30e4d38665d23c9f16625466e6891f362f6d9f6fef1b6b3b
5
+ SHA512:
6
+ metadata.gz: d04ed3018bffb1ad526736f2314f349b76fafe6cfc539217d6afb920dc7c1e2070bfab35a23c111c37578206ec318569549f72ed867cb4b56ee40863135eb413
7
+ data.tar.gz: dd201f144278c47154265a017ee1409c19fc77bf2b62ad59f1880275f387a1a9181b0729fec3019e46a002c331244c6c44d8ff6b6ee7b576a1f2b5ee015e87ee
data/.overcommit.yml ADDED
@@ -0,0 +1,4 @@
1
+ PreCommit:
2
+ RuboCop:
3
+ enabled: true
4
+ on_warn: fail # Treat all warnings as failures
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,54 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+ NewCops: enable
4
+ SuggestExtensions: true
5
+
6
+ Include:
7
+ - spec/**/*.rb
8
+ - lib/**/*.rb
9
+
10
+ Exclude:
11
+ - vendor/**/*
12
+
13
+ require:
14
+ - rubocop-performance
15
+ - rubocop-rspec
16
+ - rubocop-rake
17
+
18
+ Layout/LineLength:
19
+ Max: 120
20
+
21
+ Metrics/BlockLength:
22
+ Exclude:
23
+ - spec/**/*_spec.rb
24
+ - "*.gemspec"
25
+
26
+ RSpec/NamedSubject:
27
+ Enabled: false
28
+
29
+ Style/StringLiterals:
30
+ Enabled: true
31
+ EnforcedStyle: double_quotes
32
+
33
+ Style/StringLiteralsInInterpolation:
34
+ Enabled: true
35
+ EnforcedStyle: double_quotes
36
+
37
+ Style/Documentation:
38
+ Enabled: false
39
+
40
+ Style/SymbolArray:
41
+ EnforcedStyle: brackets
42
+
43
+ Style/WordArray:
44
+ EnforcedStyle: brackets
45
+
46
+ Style/ClassAndModuleChildren:
47
+ EnforcedStyle: compact
48
+
49
+ Style/NumberedParametersLimit:
50
+ Max: 2
51
+
52
+ Naming/FileName:
53
+ Exclude:
54
+ - cryptoform
data/.tool-versions ADDED
@@ -0,0 +1,2 @@
1
+ ruby 3.1.6
2
+ terraform 1.9.2
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders 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, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at zhulik.gleb@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Cryptofile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ port 3000
4
+
5
+ state :state1 do
6
+ storage_backend :file, name: "state1.tfstate.enc"
7
+ encryption_backend :lockbox, key: -> { ENV.fetch("CRYPTOFORM_KEY") }
8
+ end
9
+
10
+ state :state2 do
11
+ storage_backend :file, name: "state2.tfstate.enc"
12
+ encryption_backend :lockbox, key: -> { ENV.fetch("CRYPTOFORM_KEY") }
13
+ end
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cryptoform.gemspec
6
+ gemspec
7
+
8
+ gem "overcommit"
9
+ gem "rake"
10
+ gem "rspec"
11
+ gem "solargraph"
12
+
13
+ gem "rubocop"
14
+ gem "rubocop-performance"
15
+ gem "rubocop-rake"
16
+ gem "rubocop-rspec"
data/Gemfile.lock ADDED
@@ -0,0 +1,159 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cryptoform (0.1.0)
5
+ async-http (~> 0.69)
6
+ base64 (~> 0.2)
7
+ lockbox (~> 1.3)
8
+ zeitwerk (~> 2.6)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ ast (2.4.2)
14
+ async (2.12.1)
15
+ console (~> 1.25, >= 1.25.2)
16
+ fiber-annotation
17
+ io-event (~> 1.6, >= 1.6.5)
18
+ async-http (0.69.0)
19
+ async (>= 2.10.2)
20
+ async-pool (~> 0.7)
21
+ io-endpoint (~> 0.11)
22
+ io-stream (~> 0.4)
23
+ protocol-http (~> 0.26)
24
+ protocol-http1 (~> 0.19)
25
+ protocol-http2 (~> 0.18)
26
+ traces (>= 0.10)
27
+ async-pool (0.7.0)
28
+ async (>= 1.25)
29
+ backport (1.2.0)
30
+ base64 (0.2.0)
31
+ benchmark (0.3.0)
32
+ childprocess (5.0.0)
33
+ console (1.25.2)
34
+ fiber-annotation
35
+ fiber-local (~> 1.1)
36
+ json
37
+ diff-lcs (1.5.1)
38
+ e2mmap (0.1.0)
39
+ fiber-annotation (0.2.0)
40
+ fiber-local (1.1.0)
41
+ fiber-storage
42
+ fiber-storage (0.1.2)
43
+ iniparse (1.5.0)
44
+ io-endpoint (0.11.0)
45
+ io-event (1.6.5)
46
+ io-stream (0.4.0)
47
+ jaro_winkler (1.6.0)
48
+ json (2.7.2)
49
+ kramdown (2.4.0)
50
+ rexml
51
+ kramdown-parser-gfm (1.1.0)
52
+ kramdown (~> 2.0)
53
+ language_server-protocol (3.17.0.3)
54
+ lockbox (1.3.3)
55
+ mini_portile2 (2.8.7)
56
+ nokogiri (1.16.6)
57
+ mini_portile2 (~> 2.8.2)
58
+ racc (~> 1.4)
59
+ nokogiri (1.16.6-x86_64-linux)
60
+ racc (~> 1.4)
61
+ overcommit (0.63.0)
62
+ childprocess (>= 0.6.3, < 6)
63
+ iniparse (~> 1.4)
64
+ rexml (~> 3.2)
65
+ parallel (1.25.1)
66
+ parser (3.3.3.0)
67
+ ast (~> 2.4.1)
68
+ racc
69
+ protocol-hpack (1.4.3)
70
+ protocol-http (0.26.8)
71
+ protocol-http1 (0.19.1)
72
+ protocol-http (~> 0.22)
73
+ protocol-http2 (0.18.0)
74
+ protocol-hpack (~> 1.4)
75
+ protocol-http (~> 0.18)
76
+ racc (1.8.0)
77
+ rainbow (3.1.1)
78
+ rake (13.2.1)
79
+ rbs (2.8.4)
80
+ regexp_parser (2.9.2)
81
+ reverse_markdown (2.1.1)
82
+ nokogiri
83
+ rexml (3.3.1)
84
+ strscan
85
+ rspec (3.13.0)
86
+ rspec-core (~> 3.13.0)
87
+ rspec-expectations (~> 3.13.0)
88
+ rspec-mocks (~> 3.13.0)
89
+ rspec-core (3.13.0)
90
+ rspec-support (~> 3.13.0)
91
+ rspec-expectations (3.13.1)
92
+ diff-lcs (>= 1.2.0, < 2.0)
93
+ rspec-support (~> 3.13.0)
94
+ rspec-mocks (3.13.1)
95
+ diff-lcs (>= 1.2.0, < 2.0)
96
+ rspec-support (~> 3.13.0)
97
+ rspec-support (3.13.1)
98
+ rubocop (1.64.1)
99
+ json (~> 2.3)
100
+ language_server-protocol (>= 3.17.0)
101
+ parallel (~> 1.10)
102
+ parser (>= 3.3.0.2)
103
+ rainbow (>= 2.2.2, < 4.0)
104
+ regexp_parser (>= 1.8, < 3.0)
105
+ rexml (>= 3.2.5, < 4.0)
106
+ rubocop-ast (>= 1.31.1, < 2.0)
107
+ ruby-progressbar (~> 1.7)
108
+ unicode-display_width (>= 2.4.0, < 3.0)
109
+ rubocop-ast (1.31.3)
110
+ parser (>= 3.3.1.0)
111
+ rubocop-performance (1.21.1)
112
+ rubocop (>= 1.48.1, < 2.0)
113
+ rubocop-ast (>= 1.31.1, < 2.0)
114
+ rubocop-rake (0.6.0)
115
+ rubocop (~> 1.0)
116
+ rubocop-rspec (3.0.2)
117
+ rubocop (~> 1.61)
118
+ ruby-progressbar (1.13.0)
119
+ solargraph (0.50.0)
120
+ backport (~> 1.2)
121
+ benchmark
122
+ bundler (~> 2.0)
123
+ diff-lcs (~> 1.4)
124
+ e2mmap
125
+ jaro_winkler (~> 1.5)
126
+ kramdown (~> 2.3)
127
+ kramdown-parser-gfm (~> 1.1)
128
+ parser (~> 3.0)
129
+ rbs (~> 2.0)
130
+ reverse_markdown (~> 2.0)
131
+ rubocop (~> 1.38)
132
+ thor (~> 1.0)
133
+ tilt (~> 2.0)
134
+ yard (~> 0.9, >= 0.9.24)
135
+ strscan (3.1.0)
136
+ thor (1.3.1)
137
+ tilt (2.4.0)
138
+ traces (0.11.1)
139
+ unicode-display_width (2.5.0)
140
+ yard (0.9.36)
141
+ zeitwerk (2.6.16)
142
+
143
+ PLATFORMS
144
+ ruby
145
+ x86_64-linux
146
+
147
+ DEPENDENCIES
148
+ cryptoform!
149
+ overcommit
150
+ rake
151
+ rspec
152
+ rubocop
153
+ rubocop-performance
154
+ rubocop-rake
155
+ rubocop-rspec
156
+ solargraph
157
+
158
+ BUNDLED WITH
159
+ 2.5.11
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Gleb Sinyavskiy
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,63 @@
1
+ # Cryptoform
2
+
3
+ Implemented as an http backend, cryptoform encypts your state using one of the providers and stores in one
4
+ on the backends. Currently only [lockbox](https://github.com/ankane/lockbox) provider and file backend
5
+ are supported. The tool designed to be modular so other encryption providers and backends may be added
6
+ later on.
7
+
8
+ ## Why
9
+
10
+ Even though it's strongly recommended to use S3 or some other 3rd party service to store the state, and
11
+ using git to store it is discouraged, sometimes it's still very handy to not have any external services
12
+ and store the state in git, encryted for better safety. For instance: you work on the project alone and
13
+ you don't need locking(an external lock mechanism can be supported in the future) and you don't want to
14
+ bother configuring an external state store.
15
+
16
+ ## Installation
17
+
18
+ 1. Install one of supported ruby versions: 3.1 or newer
19
+ 2. In your terraform project, create 2 files:
20
+
21
+ *Gemfile*:
22
+ ```ruby
23
+ # frozen_string_literal: true
24
+
25
+ source "https://rubygems.org"
26
+
27
+ gem "cryptoform"
28
+ ```
29
+ 3. Run `bundle install`
30
+ 4. TODO: Run `bundle exec rake cryptoform:init` and save the printed key.
31
+ 5. Run `CRYPTOFORM_KEY=<key from the previous step> bundle exec cryptoform`
32
+ 6. Configure your terraform backend:
33
+ ```hcl
34
+ terraform {
35
+ backend "http" {
36
+ address = "http://127.0.0.1:3000/state"
37
+ }
38
+ }
39
+ ```
40
+
41
+ TODO: ship in docker?
42
+
43
+ ## Cryptofile
44
+
45
+ TODO: Write Cryptofile details here
46
+
47
+ ## Development
48
+
49
+ 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.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cryptoform. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/cryptoform/blob/main/CODE_OF_CONDUCT.md).
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
60
+
61
+ ## Code of Conduct
62
+
63
+ Everyone interacting in the Cryptoform project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cryptoform/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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 "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/exe/cryptoform ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "cryptoform"
5
+
6
+ begin
7
+ Sync do
8
+ Cryptoform.run("Cryptofile")
9
+ end
10
+ rescue Interrupt
11
+ puts("Interrupted")
12
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Cryptoform::Config::Builder
4
+ Config = Data.define(:port, :states)
5
+
6
+ def initialize
7
+ @port = 3000
8
+ @states = {}
9
+ end
10
+
11
+ def port(port) = @port = port
12
+
13
+ def state(name, &)
14
+ name = name.to_sym
15
+
16
+ raise ArgumentError, "state #{name} already configured" if @states.key?(name)
17
+
18
+ @states[name] = Cryptoform::Config::StateConfigBuilder.new(name, &)
19
+ end
20
+
21
+ def validate!
22
+ @states.each_value(&:validate!)
23
+
24
+ raise Cryptoform::ConfigValidationError, "port must be an integer" if @key.is_a?(Numeric)
25
+ end
26
+
27
+ def config = Config.new(@port, @states.transform_values(&:config))
28
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Cryptoform::Config::StateConfigBuilder
4
+ Config = Data.define(:storage_backend, :encryption_backend)
5
+
6
+ STORAGE_BACKENDS = {
7
+ file: Cryptoform::StorageBackends::File
8
+ }.freeze
9
+
10
+ ENCRYPTION_BACKENDS = {
11
+ lockbox: Cryptoform::EncryptionBackends::Lockbox
12
+ }.freeze
13
+
14
+ def initialize(name, &)
15
+ @name = name
16
+
17
+ instance_exec(&)
18
+ end
19
+
20
+ def storage_backend(name, **params)
21
+ name = name.to_sym
22
+ raise ArgumentError, "unknown storage backend #{name}" unless STORAGE_BACKENDS.key?(name)
23
+
24
+ @storage_backend = STORAGE_BACKENDS[name].new(@name, **params)
25
+ end
26
+
27
+ def encryption_backend(name, **params)
28
+ name = name.to_sym
29
+ raise ArgumentError, "unknown encryption backend #{name}" unless ENCRYPTION_BACKENDS.key?(name)
30
+
31
+ @encryption_backend = ENCRYPTION_BACKENDS[name].new(@name, **params)
32
+ end
33
+
34
+ def config = Config.new(@storage_backend, @encryption_backend)
35
+
36
+ def validate!
37
+ if @storage_backend.nil?
38
+ raise Cryptoform::ConfigValidationError, "storage backend must be specified for state '#{@name}'"
39
+ end
40
+ return unless @encryption_backend.nil?
41
+
42
+ raise Cryptoform::ConfigValidationError, "encryption backend must be specified for state '#{@name}'"
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Cryptoform::EncryptionBackends::Backend
4
+ def initialize(state_name, **params)
5
+ @state_name = state_name
6
+ @params = params
7
+ end
8
+
9
+ def decrypt(ciphertext) = raise NotImplementedError
10
+ def encrypt(json) = raise NotImplementedError
11
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Cryptoform::EncryptionBackends::Lockbox < Cryptoform::EncryptionBackends::Backend
4
+ def encrypt(json) = lockbox.encrypt(json.to_json)
5
+
6
+ def decrypt(ciphertext) = JSON.parse(lockbox.decrypt(ciphertext))
7
+
8
+ def lockbox = ::Lockbox.new(key: @params[:key].call, encode: true)
9
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Cryptoform::Server
4
+ def initialize(config)
5
+ @config = config
6
+ end
7
+
8
+ def run
9
+ logger.info { "Cryptoform is listening on #{endpoint.url}..." }
10
+ Async::HTTP::Server.for(endpoint) do |request|
11
+ log_request(request) { handle_request(request) }
12
+ end.run
13
+ end
14
+
15
+ private
16
+
17
+ def handle_request(request) # rubocop:disable Metrics/AbcSize
18
+ name = request.path.split("/")&.[](1)&.to_sym
19
+ return ::Protocol::HTTP::Response[404, {}, []] unless @config.states.key?(name)
20
+
21
+ handler_name = :"#{request.method.downcase}_state"
22
+ return ::Protocol::HTTP::Response[405, {}, []] unless respond_to?(handler_name, true)
23
+
24
+ send(handler_name, @config.states[name], request:)
25
+ rescue Cryptoform::StateMissingError
26
+ ::Protocol::HTTP::Response[404, {}, []]
27
+ rescue StandardError => e
28
+ logger.error(e)
29
+ ::Protocol::HTTP::Response[500, {}, []]
30
+ end
31
+
32
+ def get_state(state_config, *)
33
+ state = state_config.encryption_backend.decrypt(state_config.storage_backend.read)
34
+ ::Protocol::HTTP::Response[200, {}, [JSON.pretty_generate(state)]]
35
+ end
36
+
37
+ def post_state(state_config, request:)
38
+ state_config.storage_backend.write(state_config.encryption_backend.encrypt(request.body.read))
39
+
40
+ ::Protocol::HTTP::Response[201, {}, []]
41
+ end
42
+
43
+ def logger = @logger ||= Logger.new($stdout)
44
+
45
+ def endpoint = @endpoint ||= Async::HTTP::Endpoint.parse("http://localhost:#{@config.port}")
46
+
47
+ def log_request(request)
48
+ yield.tap do |response|
49
+ logger.info { "#{request.method} #{request.path}: #{response.status}" }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Cryptoform::StorageBackends::Backend
4
+ def initialize(state_name, **params)
5
+ @state_name = state_name
6
+ @params = params
7
+ end
8
+
9
+ def read = raise NotImplementedError
10
+ def write(data) = raise NotImplementedError
11
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Cryptoform::StorageBackends::File < Cryptoform::StorageBackends::Backend
4
+ def read
5
+ ::File.read(filename)
6
+ rescue Errno::ENOENT
7
+ raise Cryptoform::StateMissingError, "state '#{@state_name}' is configured but missing"
8
+ end
9
+
10
+ def write(data) = ::File.write(filename, data)
11
+
12
+ private
13
+
14
+ def filename = @filename ||= @params[:name] || "#{@state_name}.tfstate.enc"
15
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cryptoform
4
+ VERSION = "0.1.0"
5
+ end
data/lib/cryptoform.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+ require "logger"
5
+
6
+ require "lockbox"
7
+ require "async/http"
8
+
9
+ loader = Zeitwerk::Loader.for_gem
10
+
11
+ loader.setup
12
+
13
+ module Cryptoform
14
+ class Error < StandardError; end
15
+ class ConfigValidationError < Cryptoform::Error; end
16
+ class StateMissingError < Cryptoform::Error; end
17
+
18
+ class << self
19
+ def run(path)
20
+ config = Cryptoform::Config::Builder.new.tap { _1.instance_eval(File.read(path)) }
21
+ config.validate!
22
+ Cryptoform::Server.new(config.config).run
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ # This file is maintained automatically by "terraform init".
2
+ # Manual edits may be lost in future updates.
3
+
4
+ provider "registry.terraform.io/hashicorp/random" {
5
+ version = "3.6.2"
6
+ hashes = [
7
+ "h1:wmG0QFjQ2OfyPy6BB7mQ57WtoZZGGV07uAPQeDmIrAE=",
8
+ "zh:0ef01a4f81147b32c1bea3429974d4d104bbc4be2ba3cfa667031a8183ef88ec",
9
+ "zh:1bcd2d8161e89e39886119965ef0f37fcce2da9c1aca34263dd3002ba05fcb53",
10
+ "zh:37c75d15e9514556a5f4ed02e1548aaa95c0ecd6ff9af1119ac905144c70c114",
11
+ "zh:4210550a767226976bc7e57d988b9ce48f4411fa8a60cd74a6b246baf7589dad",
12
+ "zh:562007382520cd4baa7320f35e1370ffe84e46ed4e2071fdc7e4b1a9b1f8ae9b",
13
+ "zh:5efb9da90f665e43f22c2e13e0ce48e86cae2d960aaf1abf721b497f32025916",
14
+ "zh:6f71257a6b1218d02a573fc9bff0657410404fb2ef23bc66ae8cd968f98d5ff6",
15
+ "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3",
16
+ "zh:9647e18f221380a85f2f0ab387c68fdafd58af6193a932417299cdcae4710150",
17
+ "zh:bb6297ce412c3c2fa9fec726114e5e0508dd2638cad6a0cb433194930c97a544",
18
+ "zh:f83e925ed73ff8a5ef6e3608ad9225baa5376446349572c2449c0c0b3cf184b7",
19
+ "zh:fbef0781cb64de76b1df1ca11078aecba7800d82fd4a956302734999cfd9a4af",
20
+ ]
21
+ }
@@ -0,0 +1,5 @@
1
+ terraform {
2
+ backend "http" {
3
+ address = "http://127.0.0.1:3000/state1"
4
+ }
5
+ }
data/terraform/data.tf ADDED
@@ -0,0 +1,3 @@
1
+ resource "random_password" "password" {
2
+ length = 20
3
+ }
@@ -0,0 +1,3 @@
1
+ terraform {
2
+ required_version = "1.9.2"
3
+ }
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cryptoform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gleb Sinyavskiy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: async-http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.69'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.69'
27
+ - !ruby/object:Gem::Dependency
28
+ name: base64
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: lockbox
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: zeitwerk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.6'
69
+ description: Save your encypted terraform state in git.
70
+ email:
71
+ - zhulik.gleb@gmail.com
72
+ executables:
73
+ - cryptoform
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".overcommit.yml"
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - ".tool-versions"
81
+ - CODE_OF_CONDUCT.md
82
+ - Cryptofile
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - LICENSE.txt
86
+ - README.md
87
+ - Rakefile
88
+ - exe/cryptoform
89
+ - lib/cryptoform.rb
90
+ - lib/cryptoform/config/builder.rb
91
+ - lib/cryptoform/config/state_config_builder.rb
92
+ - lib/cryptoform/encryption_backends/backend.rb
93
+ - lib/cryptoform/encryption_backends/lockbox.rb
94
+ - lib/cryptoform/server.rb
95
+ - lib/cryptoform/storage_backends/backend.rb
96
+ - lib/cryptoform/storage_backends/file.rb
97
+ - lib/cryptoform/version.rb
98
+ - terraform/.terraform.lock.hcl
99
+ - terraform/backend.tf
100
+ - terraform/data.tf
101
+ - terraform/providers.tf
102
+ homepage: https://github.com/zhulik/cryptoform
103
+ licenses:
104
+ - MIT
105
+ metadata:
106
+ homepage_uri: https://github.com/zhulik/cryptoform
107
+ source_code_uri: https://github.com/zhulik/cryptoform
108
+ changelog_uri: https://github.com/zhulik/cryptoform
109
+ rubygems_mfa_required: 'true'
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 3.1.0
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubygems_version: 3.3.27
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: Save your encypted terraform state in git.
129
+ test_files: []