vault-transit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 004a37c752276bf2aa4ac70971d89a239ed7965d
4
+ data.tar.gz: 6d7c43569b20fd47394f4482a1b9b7de39eb7475
5
+ SHA512:
6
+ metadata.gz: c30339bb1cd88551ee2c68a68154776045815ef25f7982d6dabacf943ef937d8568efb6816d4fbef415c537083b7932009d11331e476c1dc5bfdfdddffc5623e
7
+ data.tar.gz: 699a031bb508b49ffdad3ab9c39649d97f1da43e0757c3cfdcee1eba64d626baf93e791c9617e471fb79fcfea3aef8816d016b4c48e282f879d99bdabdf982f9
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.14.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vault-transit.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 John Atkinson
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.
@@ -0,0 +1,39 @@
1
+ # Vault::Transit
2
+
3
+ This gem wraps the endpoints for [HashiCorp's Vault Transit secret backend](https://www.vaultproject.io/docs/secrets/transit/). It is dependent upon the [vault gem](https://github.com/hashicorp/vault-ruby). This gem has patterns and code copied from HashiCorp's [vault-ruby gem](https://github.com/hashicorp/vault-rails). Use this gem when you simply want to use the Transit secret backend and you don't need the Rails integration.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'vault-transit'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install vault-transit
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ 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.
28
+
29
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jgaxn/vault-transit.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "vault/transit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,302 @@
1
+
2
+ require "base64"
3
+ require "vault"
4
+ require "vault/transit/configurable"
5
+ require "vault/transit/version"
6
+
7
+ #require_relative "transit/configurable"
8
+
9
+ module Vault
10
+ module Transit
11
+
12
+ # The default encoding.
13
+ #
14
+ # @return [String]
15
+ DEFAULT_ENCODING = "utf-8".freeze
16
+
17
+ # The warning string to print when running in development mode.
18
+ DEV_WARNING = "[vault-transit] Using in-memory cipher - this is not secure " \
19
+ "and should never be used in production-like environments!".freeze
20
+
21
+ class << self
22
+ attr_reader :client
23
+
24
+ def setup!
25
+ ::Vault.setup!
26
+ @client = ::Vault.client
27
+ @client.class.instance_eval do
28
+ include ::Vault::Transit::Configurable
29
+ end
30
+
31
+ self
32
+ end
33
+
34
+ # Delegate all methods to the client object, essentially making the module
35
+ # object behave like a {Vault::Client}.
36
+ def method_missing(m, *args, &block)
37
+ if client.respond_to?(m)
38
+ client.public_send(m, *args, &block)
39
+ else
40
+ super
41
+ end
42
+ end
43
+
44
+ # Delegating `respond_to` to the {Vault::Client}.
45
+ def respond_to_missing?(m, include_private = false)
46
+ client.respond_to?(m, include_private) || super
47
+ end
48
+
49
+ # Decrypt the given ciphertext data using the provided key.
50
+ #
51
+ # @param [String] key
52
+ # the key to decrypt at
53
+ # @param [String] ciphertext
54
+ # the ciphertext to decrypt
55
+ # @param [Vault::Client] client
56
+ # the Vault client to use
57
+ #
58
+ # @return [String]
59
+ # the decrypted plaintext text
60
+ def decrypt(key, ciphertext, client = self.client)
61
+ if ciphertext.nil? || ciphertext.empty?
62
+ return ciphertext
63
+ end
64
+
65
+ key = key.to_s if !key.is_a?(String)
66
+
67
+ with_retries_and_reauthentication do
68
+ if self.enabled?
69
+ result = self.vault_decrypt(key, ciphertext, client)
70
+ else
71
+ result = self.memory_decrypt(key, ciphertext, client)
72
+ end
73
+
74
+ return self.force_encoding(result)
75
+ end
76
+ end
77
+
78
+ # Encrypt the given plaintext data using the provided key.
79
+ #
80
+ # @param [String] key
81
+ # the key to encrypt at
82
+ # @param [String] plaintext
83
+ # the plaintext to encrypt
84
+ # @param [Vault::Client] client
85
+ # the Vault client to use
86
+ #
87
+ # @return [String]
88
+ # the encrypted cipher text
89
+ def encrypt(key, plaintext, client = self.client)
90
+ if plaintext.nil? || plaintext.empty?
91
+ return plaintext
92
+ end
93
+
94
+ key = key.to_s if !key.is_a?(String)
95
+
96
+ with_retries_and_reauthentication do
97
+ if self.enabled?
98
+ result = self.vault_encrypt(key, plaintext, client)
99
+ else
100
+ result = self.memory_encrypt(key, plaintext, client)
101
+ end
102
+
103
+ return self.force_encoding(result)
104
+ end
105
+ end
106
+
107
+ # Rewrap the given ciphertext data using the provided key.
108
+ #
109
+ # @param [String] key
110
+ # the key to rewrap at
111
+ # @param [String] ciphertext
112
+ # the ciphertext to rewrap
113
+ # @param [Vault::Client] client
114
+ # the Vault client to use
115
+ #
116
+ # @return [String]
117
+ # the rewrapped ciphertext text
118
+ def rewrap(key, ciphertext, client = self.client)
119
+ if ciphertext.nil? || ciphertext.empty?
120
+ return ciphertext
121
+ end
122
+
123
+ key = key.to_s unless key.is_a?(String)
124
+ route = File.join("transit", "rewrap", key)
125
+
126
+ with_retries_and_reauthentication do
127
+ if self.enabled?
128
+ secret = client.logical.write(route,
129
+ ciphertext: ciphertext,
130
+ )
131
+ result = secret.data[:ciphertext]
132
+ else
133
+ result = ciphertext
134
+ end
135
+ return self.force_encoding(result)
136
+ end
137
+ end
138
+
139
+ # Rotate the key to a new version
140
+ #
141
+ # @param [String] key
142
+ # the key to rotate
143
+ # @param [Vault::Client] client
144
+ # the Vault client to use
145
+ #
146
+ def rotate(key, client = self.client)
147
+ key = key.to_s unless key.is_a?(String)
148
+ route = File.join("transit", "keys", key, "rotate")
149
+
150
+ with_retries_and_reauthentication do
151
+ if self.enabled?
152
+ client.logical.write(route)
153
+ end
154
+ end
155
+ end
156
+
157
+ # Set the minimum decryption version a using the provided key.
158
+ #
159
+ # @param [String] key
160
+ # the key to configure
161
+ # @param [int] min_decryption_version
162
+ # the new minimum decryption version
163
+ # @param [Vault::Client] client
164
+ # the Vault client to use
165
+ #
166
+ def set_min_decryption_version(key, min_decryption_version, client = self.client)
167
+ key = key.to_s unless key.is_a?(String)
168
+
169
+ with_retries_and_reauthentication do
170
+ if self.enabled?
171
+ route = File.join("transit", "keys", key, "config")
172
+ client.logical.write(route,
173
+ min_decryption_version: min_decryption_version,
174
+ )
175
+ end
176
+ end
177
+ end
178
+
179
+ protected
180
+
181
+ # Perform in-memory decryption. This is useful for testing and development.
182
+ def memory_decrypt(key, ciphertext, client)
183
+ log_warning(DEV_WARNING)
184
+
185
+ return nil if ciphertext.nil?
186
+
187
+ cipher = OpenSSL::Cipher::AES.new(128, :CBC)
188
+ cipher.decrypt
189
+ cipher.key = memory_key_for(key)
190
+ ciphertext = ciphertext.gsub("vault:v0:", "")
191
+ return cipher.update(Base64.strict_decode64(ciphertext)) + cipher.final
192
+ end
193
+
194
+ # Perform in-memory encryption. This is useful for testing and development.
195
+ def memory_encrypt(key, plaintext, client)
196
+ log_warning(DEV_WARNING)
197
+
198
+ return nil if plaintext.nil?
199
+
200
+ cipher = OpenSSL::Cipher::AES.new(128, :CBC)
201
+ cipher.encrypt
202
+ cipher.key = memory_key_for(key)
203
+ return "vault:v0:" + Base64.strict_encode64(cipher.update(plaintext) + cipher.final)
204
+ end
205
+
206
+ # Perform decryption using Vault. This will raise exceptions if Vault is
207
+ # unavailable.
208
+ def vault_decrypt(key, ciphertext, client)
209
+ return nil if ciphertext.nil?
210
+
211
+ route = File.join("transit", "decrypt", key)
212
+ secret = client.logical.write(route, ciphertext: ciphertext)
213
+ return Base64.strict_decode64(secret.data[:plaintext])
214
+ end
215
+
216
+ # Perform encryption using Vault. This will raise exceptions if Vault is
217
+ # unavailable.
218
+ def vault_encrypt(key, plaintext, client)
219
+ return nil if plaintext.nil?
220
+
221
+ route = File.join("transit", "encrypt", key)
222
+ secret = client.logical.write(route,
223
+ plaintext: Base64.strict_encode64(plaintext),
224
+ )
225
+ return secret.data[:ciphertext]
226
+ end
227
+
228
+ # The symmetric key for the given params.
229
+ # @return [String]
230
+ def memory_key_for(key)
231
+ return Base64.strict_encode64(key.ljust(32, "x"))
232
+ end
233
+
234
+ # Forces the encoding into the default Rails encoding and returns the
235
+ # newly encoded string.
236
+ # @return [String]
237
+ def force_encoding(str)
238
+ encoding = ::Rails.application.config.encoding if defined? ::Rails
239
+ encoding ||= DEFAULT_ENCODING
240
+ str.force_encoding(encoding).encode(encoding)
241
+ end
242
+
243
+ private
244
+
245
+ def log_warning(msg)
246
+ if defined?(::Rails) && ::Rails.logger != nil
247
+ ::Rails.logger.warn { msg }
248
+ end
249
+ end
250
+
251
+ def permission_denied?(error)
252
+ error.errors.include? "permission denied"
253
+ end
254
+
255
+ def reauthenticate!
256
+ return nil unless ENV["VAULT_APP_ID"] && ENV["VAULT_SYSTEM_ID"]
257
+ secret = ::Vault::Transit.auth.app_id(ENV["VAULT_APP_ID"], ENV["VAULT_SYSTEM_ID"])
258
+ ::Vault::Transit.token = secret.auth.client_token
259
+ end
260
+
261
+ def with_reauthentication(client = self.client, &block)
262
+ retries ||= 0
263
+ reauthenticate! if self.enabled? && self.token.nil?
264
+ yield
265
+ rescue ::Vault::HTTPError => error
266
+ raise unless permission_denied?(error)
267
+
268
+ reauthenticate!
269
+ retry if (retries += 1) < 2
270
+ raise
271
+ end
272
+
273
+ def with_retries(client = self.client, &block)
274
+ exceptions = [Vault::HTTPConnectionError, Vault::HTTPServerError]
275
+ options = {
276
+ attempts: self.retry_attempts,
277
+ base: self.retry_base,
278
+ max_wait: self.retry_max_wait,
279
+ }
280
+
281
+ client.with_retries(*exceptions, options) do |i, e|
282
+ if !e.nil?
283
+ log_warning "[vault-transit] (#{i}) An error occurred when trying to " \
284
+ "communicate with Vault: #{e.message}"
285
+ end
286
+
287
+ yield
288
+ end
289
+ end
290
+
291
+ def with_retries_and_reauthentication(client = self.client, &block)
292
+ with_reauthentication do
293
+ with_retries do
294
+ yield
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end
300
+ end
301
+
302
+ ::Vault::Transit.setup!
@@ -0,0 +1,78 @@
1
+ module Vault
2
+ module Transit
3
+ module Configurable
4
+ include Vault::Configurable
5
+
6
+ # Whether the connection to Vault is enabled. The default value is `false`,
7
+ # which means vault-rails will perform in-memory encryption/decryption and
8
+ # not attempt to talk to a real Vault server. This is useful for
9
+ # development and testing.
10
+ #
11
+ # @return [true, false]
12
+ def enabled?
13
+ if !defined?(@enabled) || @enabled.nil?
14
+ return false
15
+ end
16
+ return @enabled
17
+ end
18
+
19
+ # Sets whether Vault is enabled. Users can set this in an initializer
20
+ # depending on their Rails environment.
21
+ #
22
+ # @example
23
+ # Vault.configure do |vault|
24
+ # vault.enabled = Rails.env.production?
25
+ # end
26
+ #
27
+ # @return [true, false]
28
+ def enabled=(val)
29
+ @enabled = !!val
30
+ end
31
+
32
+ # Gets the number of retry attempts.
33
+ #
34
+ # @return [Fixnum]
35
+ def retry_attempts
36
+ @retry_attempts ||= 0
37
+ end
38
+
39
+ # Sets the number of retry attempts. Please see the Vault documentation
40
+ # for more information.
41
+ #
42
+ # @param [Fixnum] val
43
+ def retry_attempts=(val)
44
+ @retry_attempts = val
45
+ end
46
+
47
+ # Gets the number of retry attempts.
48
+ #
49
+ # @return [Fixnum]
50
+ def retry_base
51
+ @retry_base ||= Vault::Defaults::RETRY_BASE
52
+ end
53
+
54
+ # Sets the retry interval. Please see the Vault documentation for more
55
+ # information.
56
+ #
57
+ # @param [Fixnum] val
58
+ def retry_base=(val)
59
+ @retry_base = val
60
+ end
61
+
62
+ # Gets the retry maximum wait.
63
+ #
64
+ # @return [Fixnum]
65
+ def retry_max_wait
66
+ @retry_max_wait ||= Vault::Defaults::RETRY_MAX_WAIT
67
+ end
68
+
69
+ # Sets the naximum amount of time for a single retry. Please see the Vault
70
+ # documentation for more information.
71
+ #
72
+ # @param [Fixnum] val
73
+ def retry_max_wait=(val)
74
+ @retry_max_wait = val
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,5 @@
1
+ module Vault
2
+ module Transit
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vault/transit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vault-transit"
8
+ spec.version = Vault::Transit::VERSION
9
+ spec.authors = ["John Atkinson"]
10
+ spec.email = ["jgaxn0@gmail.com"]
11
+
12
+ spec.summary = "Ruby API client for interacting with the Vault Transit secret backend"
13
+ spec.homepage = "https://github.com/jgaxn/vault-transit"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "vault", "~> 0.8"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vault-transit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Atkinson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: vault
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description:
70
+ email:
71
+ - jgaxn0@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/vault/transit.rb
86
+ - lib/vault/transit/configurable.rb
87
+ - lib/vault/transit/version.rb
88
+ - vault-transit.gemspec
89
+ homepage: https://github.com/jgaxn/vault-transit
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Ruby API client for interacting with the Vault Transit secret backend
113
+ test_files: []