legion-crypt 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: 07b24418ca073e0f27ac8b1971b3dff861e245f9e469a1d78bf14a500e02ded8
4
+ data.tar.gz: 73c531446637ff282583e5e74db187077dd0431493919db84b00348d592b262d
5
+ SHA512:
6
+ metadata.gz: 4bee5884abd1308aad74fd342168f3622bd19aa213be6908e42178c02172a3682a44398bbad7a484533cd70b6c6e3203ae8589b4b79d9455a971cfda9dc61035
7
+ data.tar.gz: a4770c968abc0a5f8743a042238f2aa0225ee6189ae49854d1db45e3e6b2b7b442f1b40862d0135e3f901f731a57d4017765d8846c0888b61a014b118eff3849
@@ -0,0 +1,61 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@0.2.1
4
+
5
+ jobs:
6
+ "rubocop":
7
+ docker:
8
+ - image: circleci/ruby:2.5-node
9
+ steps:
10
+ - checkout
11
+ - ruby/load-cache
12
+ - ruby/install-deps
13
+ - run:
14
+ name: Run Rubocop
15
+ command: bundle exec rubocop
16
+ - ruby/save-cache
17
+ "ruby-two-five":
18
+ docker:
19
+ - image: circleci/ruby:2.5
20
+ - image: memcached:1.5-alpine
21
+ steps:
22
+ - checkout
23
+ - ruby/load-cache
24
+ - ruby/install-deps
25
+ - ruby/run-tests
26
+ - ruby/save-cache
27
+ "ruby-two-six":
28
+ docker:
29
+ - image: circleci/ruby:2.6
30
+ - image: memcached:1.5-alpine
31
+ steps:
32
+ - checkout
33
+ - ruby/load-cache
34
+ - ruby/install-deps
35
+ - ruby/run-tests
36
+ - ruby/save-cache
37
+ "ruby-two-seven":
38
+ docker:
39
+ - image: circleci/ruby:2.7
40
+ - image: memcached:1.5-alpine
41
+ steps:
42
+ - checkout
43
+ - ruby/load-cache
44
+ - ruby/install-deps
45
+ - ruby/run-tests
46
+ - ruby/save-cache
47
+
48
+ workflows:
49
+ version: 2
50
+ rubocop-rspec:
51
+ jobs:
52
+ - rubocop
53
+ - ruby-two-five:
54
+ requires:
55
+ - rubocop
56
+ - ruby-two-six:
57
+ requires:
58
+ - ruby-two-five
59
+ - ruby-two-seven:
60
+ requires:
61
+ - ruby-two-five
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.idea/.rakeTasks ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build legion-crypt-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install legion-crypt-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install legion-crypt-0.1.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.0 and build and push legion-crypt-0.1.0.gem to rubygems.org" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="Run RSpec code examples" fullCmd="spec" taksId="spec" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
@@ -0,0 +1,45 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$" />
8
+ <orderEntry type="inheritedJdk" />
9
+ <orderEntry type="sourceFolder" forTests="false" />
10
+ <orderEntry type="library" scope="PROVIDED" name="amq-protocol (v2.3.0, RVM: ruby-2.6.3) [gem]" level="application" />
11
+ <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.0, RVM: ruby-2.6.3) [gem]" level="application" />
12
+ <orderEntry type="library" scope="PROVIDED" name="aws-eventstream (v1.0.3, RVM: ruby-2.6.3) [gem]" level="application" />
13
+ <orderEntry type="library" scope="PROVIDED" name="aws-sigv4 (v1.1.1, RVM: ruby-2.6.3) [gem]" level="application" />
14
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.1.4, RVM: ruby-2.6.3) [gem]" level="application" />
15
+ <orderEntry type="library" scope="PROVIDED" name="bunny (v2.14.4, RVM: ruby-2.6.3) [gem]" level="application" />
16
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.3, RVM: ruby-2.6.3) [gem]" level="application" />
17
+ <orderEntry type="library" scope="PROVIDED" name="docile (v1.3.2, RVM: ruby-2.6.3) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="ffi (v1.12.2, RVM: ruby-2.6.3) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="jaro_winkler (v1.5.4, RVM: ruby-2.6.3) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="json (v2.3.0, RVM: ruby-2.6.3) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="legion (v0.1.2, RVM: ruby-2.6.3) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="legion-json (v0.1.6, RVM: ruby-2.6.3) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="legion-logging (v1.0.0, RVM: ruby-2.6.3) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="legion-settings (v1.0.0, RVM: ruby-2.6.3) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="legion-transport (v0.1.0, RVM: ruby-2.6.3) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="oj (v3.10.5, RVM: ruby-2.6.3) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="parallel (v1.19.1, RVM: ruby-2.6.3) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="parser (v2.7.0.4, RVM: ruby-2.6.3) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.0.0, RVM: ruby-2.6.3) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.0.1, RVM: ruby-2.6.3) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="rbnacl (v7.1.1, RVM: ruby-2.6.3) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="rexml (v3.2.4, RVM: ruby-2.6.3) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v3.9.0, RVM: ruby-2.6.3) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.9.1, RVM: ruby-2.6.3) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.9.0, RVM: ruby-2.6.3) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.9.1, RVM: ruby-2.6.3) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.9.2, RVM: ruby-2.6.3) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="rubocop (v0.80.1, RVM: ruby-2.6.3) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.10.1, RVM: ruby-2.6.3) [gem]" level="application" />
40
+ <orderEntry type="library" scope="PROVIDED" name="simplecov (v0.18.5, RVM: ruby-2.6.3) [gem]" level="application" />
41
+ <orderEntry type="library" scope="PROVIDED" name="simplecov-html (v0.12.2, RVM: ruby-2.6.3) [gem]" level="application" />
42
+ <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v1.6.1, RVM: ruby-2.6.3) [gem]" level="application" />
43
+ <orderEntry type="library" scope="PROVIDED" name="vault (v0.13.0, RVM: ruby-2.6.3) [gem]" level="application" />
44
+ </component>
45
+ </module>
data/.idea/misc.xml ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="JavaScriptSettings">
4
+ <option name="languageLevel" value="ES6" />
5
+ </component>
6
+ <component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-2.6.3" project-jdk-type="RUBY_SDK" />
7
+ </project>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/legion-crypt.iml" filepath="$PROJECT_DIR$/.idea/legion-crypt.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
data/.idea/vagrant.xml ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VagrantProjectSettings">
4
+ <option name="instanceFolder" value="" />
5
+ <option name="provider" value="" />
6
+ </component>
7
+ </project>
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="CoverageOptionsProvider">
4
+ <option name="myAddOrReplace" value="0" />
5
+ </component>
6
+ <component name="Git.Settings">
7
+ <option name="PUSH_AUTO_UPDATE" value="true" />
8
+ <option name="ROOT_SYNC" value="DONT_SYNC" />
9
+ </component>
10
+ <component name="ProjectId" id="1Yk09ZatgP1aKTE1VrPrnkK2STE" />
11
+ <component name="PropertiesComponent">
12
+ <property name="settings.editor.selected.configurable" value="reference.settingsdialog.project.vagrant" />
13
+ </component>
14
+ </project>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ Layout/LineLength:
2
+ Max: 140
3
+ Metrics/MethodLength:
4
+ Max: 50
5
+ Metrics/ClassLength:
6
+ Max: 1500
7
+ Metrics/BlockLength:
8
+ Max: 50
9
+ Layout/SpaceAroundEqualsInParameterDefault:
10
+ EnforcedStyle: space
11
+ Style/SymbolArray:
12
+ Enabled: true
13
+ Layout/HashAlignment:
14
+ EnforcedHashRocketStyle: table
15
+ EnforcedColonStyle: table
16
+ Style/Documentation:
17
+ Enabled: false
18
+ AllCops:
19
+ TargetRubyVersion: 2.5
20
+ Style/FrozenStringLiteralComment:
21
+ Enabled: false
22
+ Naming/FileName:
23
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Esity
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,40 @@
1
+ # Legion::Crypt
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/legion/crypt`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'legion-crypt'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install legion-crypt
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/legion-crypt.
36
+
37
+
38
+ ## License
39
+
40
+ 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,8 @@
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
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'legion/crypt'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/legion/crypt/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'legion-crypt'
7
+ spec.version = Legion::Crypt::VERSION
8
+ spec.authors = ['Esity']
9
+ spec.email = ['matthewdiverson@gmail.com']
10
+
11
+ spec.summary = 'Legion::Vault is used to keep things safe'
12
+ spec.description = 'Integrates with Hashicorps vault and other encryption type things'
13
+ spec.homepage = 'https://bitbucket.org/legion-io/legion-vault/'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://bitbucket.org/legion-io/legion/'
19
+ spec.metadata['changelog_uri'] = 'https://bitbucket.org/legion-io/legion/src/master/CHANGELOG.md'
20
+
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'rbnacl'
29
+ spec.add_dependency 'vault'
30
+
31
+ spec.add_development_dependency 'legion'
32
+ spec.add_development_dependency 'legion-logging', '>= 1.0.0'
33
+ spec.add_development_dependency 'legion-settings', '>= 1.0.0'
34
+ spec.add_development_dependency 'legion-transport', '>= 0.1.0'
35
+ spec.add_development_dependency 'rake'
36
+ spec.add_development_dependency 'rspec'
37
+ spec.add_development_dependency 'rubocop'
38
+ spec.add_development_dependency 'simplecov'
39
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'legion/crypt/version'
4
+ require 'legion/crypt/settings'
5
+ require 'rbnacl'
6
+ require 'base64'
7
+
8
+ require 'legion/crypt/box'
9
+ require 'legion/crypt/vault'
10
+
11
+ module Legion
12
+ module Crypt
13
+ class << self
14
+ attr_reader :public_key, :sessions
15
+ include Legion::Crypt::Box
16
+ include Legion::Crypt::Vault if Legion::Settings[:crypt][:vault][:enabled]
17
+
18
+ def start
19
+ Legion::Logging.debug 'Legion::Crypt is running start'
20
+ if Dir.exist?('./settings') && File.exist?('./settings/private.key') && File.exist?('./settings/public.key')
21
+ load_keys
22
+ else
23
+ delete_keys if Dir.exist? './settings'
24
+ create_keys
25
+ end
26
+
27
+ connect_vault
28
+ end
29
+
30
+ def shutdown
31
+ shutdown_renewer
32
+ close_sessions
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Crypt
5
+ module Box
6
+ def create_keys
7
+ Legion::Logging.debug 'Legion::Crypt::Box.create_keys has been called'
8
+ @private_key = RbNaCl::PrivateKey.generate
9
+ @public_key = @private_key.public_key
10
+ return unless Dir.exist? './settings'
11
+
12
+ File.open('./settings/private.key', 'w').write(@private_key.to_s)
13
+ File.open('./settings/public.key', 'w').write(@public_key.to_s)
14
+ end
15
+
16
+ def delete_keys
17
+ File.delete('./settings/private.key') if File.exist? './settings/private.key'
18
+ File.delete('./settings/public.key') if File.exist? './settings/public.key'
19
+ end
20
+
21
+ def load_keys
22
+ return unless Dir.exist? './settings'
23
+
24
+ @private_key = RbNaCl::PrivateKey.new(File.read('./settings/private.key').force_encoding('BINARY'))
25
+ @public_key = RbNaCl::PrivateKey.new(File.read('./settings/public.key').force_encoding('BINARY'))
26
+ end
27
+
28
+ def encrypt_from_keypair(public_key:, message:, **_opts)
29
+ Legion::Logging.debug('encrypt_from_keypair')
30
+ Base64.encode64(RbNaCl::SimpleBox.from_keypair(Base64.decode64(public_key), @private_key).encrypt(message))
31
+ end
32
+
33
+ def decrypt_from_keypair(public_key, enciphered_message)
34
+ Legion::Logging.debug 'decrypt_from_keypair'
35
+ RbNaCl::SimpleBox
36
+ .from_keypair(Base64.decode64(public_key), @private_key)
37
+ .decrypt(Base64.decode64(enciphered_message))
38
+ end
39
+
40
+ def encrypt(message)
41
+ Legion::Logging.debug 'encrypting message'
42
+ Base64.encode64(@box.encrypt(message))
43
+ end
44
+
45
+ def decrypt(message)
46
+ Legion::Logging.debug 'decrypting message'
47
+ @box.decrypt(Base64.decode64(message))
48
+ end
49
+
50
+ def setup_safe # rubocop:disable Metrics/CyclomaticComplexity,Metrics/AbcSize,Metrics/PerceivedComplexity
51
+ Legion::Logging.debug 'Setting up Legion::Crypt safe'
52
+ if Legion::Settings[:crypt][:cluster_secret].nil?
53
+ if Legion::Settings[:crypt][:vault][:connected] && Legion::Crypt.exist?('crypt')
54
+ Legion::Settings[:crypt][:cluster_secret] = Base64.decode64(Legion::Crypt.get('crypt')[:cluster_secret])
55
+ elsif Legion::Transport::Queue.new('node.crypt', passive: true).consumer_count.zero?
56
+ Legion::Logging.info 'Legion::Crypt Generating new cluster_secret since this is the first node'
57
+ Legion::Settings[:crypt][:bootstrapped] = true
58
+ Legion::Settings[:crypt][:cluster_secret] = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
59
+ if Legion::Settings[:crypt][:vault][:connected]
60
+ Legion::Crypt.write('crypt', :cluster_secret, Base64.encode64(Legion::Settings[:crypt][:cluster_secret]))
61
+ end
62
+ else
63
+ require 'legion/transport/messages/request_cluster_secret'
64
+ Legion::Logging.info 'Requesting cluster secret via public key'
65
+ start = Time.now
66
+ Legion::Transport::Messages::RequestClusterSecret.new.publish
67
+ sleep_time = 0.001
68
+ until !Legion::Settings[:crypt][:cluster_secret].nil? || (Time.now - start) > Legion::Settings[:crypt][:cluster_secret_timeout]
69
+ sleep(sleep_time)
70
+ sleep_time *= 2
71
+ end
72
+ unless Legion::Settings[:crypt][:cluster_secret].nil?
73
+ Legion::Logging.info "Received cluster secret in #{((Time.new - start) * 1000.0).round}ms"
74
+ end
75
+ Legion::Logging.warn 'Cluster secret is still nil' if Legion::Settings[:crypt][:cluster_secret].nil?
76
+ end
77
+ end
78
+
79
+ @key = Legion::Settings[:crypt][:cluster_secret].to_s
80
+ @box = RbNaCl::SimpleBox.from_secret_key(@key) unless @key.empty?
81
+ if !Legion::Settings[:crypt].key?(:encrypted_string) || !Legion::Settings[:crypt].key?(:validation_string)
82
+ unless Legion::Settings[:crypt][:bootstrapped]
83
+ Legion::Logging.warn 'Legion::Crypt has been set up but wasn\'t testing with a validation string!'
84
+ end
85
+ Legion::Settings[:crypt][:cs_encrypt_ready] = true
86
+ elsif Legion::Crypt.decrypt(Legion::Settings[:crypt][:encrypted_string]) == Legion::Settings[:crypt][:validation_string]
87
+ Legion::Logging.info 'Legion::Crypt was set up correctly after string match'
88
+ Legion::Settings[:crypt][:cs_encrypt_ready] = true
89
+ else
90
+ Legion::Logging.fatal 'idk wtf happened'
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,30 @@
1
+ module Legion
2
+ module Crypt
3
+ module Settings
4
+ def self.default
5
+ {
6
+ vault: vault,
7
+ cs_encrypt_ready: false,
8
+ dynamic_keys: true
9
+ }
10
+ end
11
+
12
+ def self.vault
13
+ {
14
+ enabled: !Gem::Specification.find_by_name('vault').nil?,
15
+ protocol: 'http',
16
+ address: 'localhost',
17
+ port: 8200,
18
+ token: ENV['VAULT_DEV_ROOT_TOKEN_ID'] || ENV['VAULT_TOKEN_ID'] || nil,
19
+ connected: false,
20
+ renewer_time: 5,
21
+ renewer: true,
22
+ push_cluster_secret: false,
23
+ read_cluster_secret: false
24
+ }
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Legion::Settings.merge_settings('crypt', Legion::Crypt::Settings.default) if Legion.const_defined?('Settings')
@@ -0,0 +1,80 @@
1
+ require 'vault'
2
+
3
+ module Legion
4
+ module Crypt
5
+ module Vault
6
+ attr_accessor :sessions
7
+ def settings
8
+ Legion::Settings[:crypt][:vault]
9
+ end
10
+
11
+ def connect_vault # rubocop:disable Metrics/AbcSize
12
+ @sessions = []
13
+ ::Vault.address = "#{Legion::Settings[:crypt][:vault][:protocol]}://#{Legion::Settings[:crypt][:vault][:address]}:#{Legion::Settings[:crypt][:vault][:port]}" # rubocop:disable Layout/LineLength
14
+
15
+ Legion::Settings[:crypt][:vault][:token] = ENV['VAULT_DEV_ROOT_TOKEN_ID'] if ENV.key? 'VAULT_DEV_ROOT_TOKEN_ID'
16
+ return nil if Legion::Settings[:crypt][:vault][:token].nil?
17
+
18
+ ::Vault.token = Legion::Settings[:crypt][:vault][:token]
19
+ Legion::Settings[:crypt][:vault][:connected] = true if ::Vault.sys.health_status.initialized?
20
+ return unless Legion.const_defined? 'Extensions::Actors::Every'
21
+
22
+ require_relative 'vault_renewer'
23
+ @renewer = Legion::Crypt::Vault::Renewer.new
24
+ end
25
+
26
+ def read(path, type = 'kv-v2')
27
+ lease = ::Vault.logical.read(type + '/' + path)
28
+ add_session(path: lease.lease_id) if lease.respond_to? :lease_id
29
+ lease.data
30
+ end
31
+
32
+ def get(path)
33
+ ::Vault.kv('kv-v2').read(path).data
34
+ end
35
+
36
+ def write(path, key, value)
37
+ hash = {}
38
+ hash[key.to_sym] = value
39
+ ::Vault.kv('kv-v2').write(path, **hash)
40
+ end
41
+
42
+ def exist?(path)
43
+ !::Vault.kv('kv-v2').read_metadata(path).nil?
44
+ end
45
+
46
+ def add_session(path:)
47
+ @sessions.push(path)
48
+ end
49
+
50
+ def close_sessions
51
+ Legion::Logging.info 'Closing all Legion::Crypt vault sessions'
52
+ @sessions.each do |session|
53
+ close_session(session: session)
54
+ end
55
+ end
56
+
57
+ def shutdown_renewer
58
+ return unless Legion::Settings[:crypt][:vault][:connected]
59
+ return if @renewer.nil?
60
+
61
+ Legion::Logging.debug 'Shutdown down Legion::Crypt::Vault::Renewer'
62
+ @renewer.cancel
63
+ end
64
+
65
+ def close_session(session:)
66
+ ::Vault.sys.revoke(session)
67
+ end
68
+
69
+ def renew_session(session:)
70
+ ::Vault.sys.renew(session)
71
+ end
72
+
73
+ def renew_sessions
74
+ @sessions.each do |session|
75
+ renew_session(session: session)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,29 @@
1
+ require 'legion/extensions/actors/every'
2
+
3
+ module Legion
4
+ module Crypt
5
+ module Vault
6
+ class Renewer < Legion::Extensions::Actors::Every
7
+ def runner_function
8
+ 'renew_sessions'
9
+ end
10
+
11
+ def klass
12
+ Legion::Crypt
13
+ end
14
+
15
+ def time
16
+ 5
17
+ end
18
+
19
+ def check_subtask?
20
+ false
21
+ end
22
+
23
+ def generate_task?
24
+ false
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Legion
4
+ module Crypt
5
+ VERSION = '0.1.0'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,209 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: legion-crypt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Esity
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rbnacl
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: vault
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: legion
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: legion-logging
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.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.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: legion-settings
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: legion-transport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 0.1.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 0.1.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Integrates with Hashicorps vault and other encryption type things
154
+ email:
155
+ - matthewdiverson@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".circleci/config.yml"
161
+ - ".gitignore"
162
+ - ".idea/.rakeTasks"
163
+ - ".idea/legion-crypt.iml"
164
+ - ".idea/misc.xml"
165
+ - ".idea/modules.xml"
166
+ - ".idea/vagrant.xml"
167
+ - ".idea/workspace.xml"
168
+ - ".rspec"
169
+ - ".rubocop.yml"
170
+ - Gemfile
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - bin/console
175
+ - bin/setup
176
+ - legion-crypt.gemspec
177
+ - lib/legion/crypt.rb
178
+ - lib/legion/crypt/box.rb
179
+ - lib/legion/crypt/settings.rb
180
+ - lib/legion/crypt/vault.rb
181
+ - lib/legion/crypt/vault_renewer.rb
182
+ - lib/legion/crypt/version.rb
183
+ homepage: https://bitbucket.org/legion-io/legion-vault/
184
+ licenses:
185
+ - MIT
186
+ metadata:
187
+ homepage_uri: https://bitbucket.org/legion-io/legion-vault/
188
+ source_code_uri: https://bitbucket.org/legion-io/legion/
189
+ changelog_uri: https://bitbucket.org/legion-io/legion/src/master/CHANGELOG.md
190
+ post_install_message:
191
+ rdoc_options: []
192
+ require_paths:
193
+ - lib
194
+ required_ruby_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: 2.5.0
199
+ required_rubygems_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ requirements: []
205
+ rubygems_version: 3.0.8
206
+ signing_key:
207
+ specification_version: 4
208
+ summary: Legion::Vault is used to keep things safe
209
+ test_files: []