encrypted_settings 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a8b0b2a65890a02884e727f8dce6fbba39b67037d7c5d6ac7e3fd4f5f206f6db
4
+ data.tar.gz: aa36aa5aa1659467ff85e451fd7414dc2a1f37c9b540c99b025e4b3017a9d1f9
5
+ SHA512:
6
+ metadata.gz: 6310e897d16401c67d3f02a6de8f1d1f430dce66c9ff5c4decac96de508654e8735e857bb0193ffcd378a9f55899320fa18443e1fc4ba834230083953e97638a
7
+ data.tar.gz: 0b77a382747724f2ef1c091f594b1e37f1c832e0a7f8d4a097820de7d083e7e3b6a862abe0c34c7ca1c5d5c667cfcae4fa83ac3532d40678293b8743ce7b9925
@@ -0,0 +1,37 @@
1
+ config
2
+ *.gem
3
+ *.rbc
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+
14
+ ## Specific to RubyMotion:
15
+ .dat*
16
+ .repl_history
17
+ build/
18
+
19
+ ## Documentation cache and generated files:
20
+ /.yardoc/
21
+ /_yardoc/
22
+ /doc/
23
+ /rdoc/
24
+
25
+ ## Environment normalization:
26
+ /.bundle/
27
+ /vendor/bundle
28
+ /lib/bundler/man/
29
+
30
+ # for a library or gem, you might want to ignore these files since the code is
31
+ # intended to run in multiple environments; otherwise, check them in:
32
+ # Gemfile.lock
33
+ # .ruby-version
34
+ # .ruby-gemset
35
+
36
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
37
+ .rvmrc
@@ -0,0 +1 @@
1
+ 2.6.3
@@ -0,0 +1 @@
1
+ 18167e5c72e6408075cfd60f416cea88
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+
3
+ branches:
4
+ only: master
5
+
6
+ language: ruby
7
+
8
+ cache: bundler
9
+
10
+ script: ./bin/test
11
+
12
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ encrypted_settings (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ansi (1.5.0)
10
+ builder (3.2.2)
11
+ coderay (1.1.0)
12
+ method_source (0.8.2)
13
+ minitest (5.8.4)
14
+ minitest-reporters (1.1.7)
15
+ ansi
16
+ builder
17
+ minitest (>= 5.0)
18
+ ruby-progressbar
19
+ pry (0.10.3)
20
+ coderay (~> 1.1.0)
21
+ method_source (~> 0.8.1)
22
+ slop (~> 3.4)
23
+ rake (10.5.0)
24
+ ruby-progressbar (1.7.5)
25
+ slop (3.6.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ bundler
32
+ encrypted_settings!
33
+ minitest-reporters
34
+ pry
35
+ rake
36
+
37
+ BUNDLED WITH
38
+ 1.17.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Krzysztof K.
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 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,
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 THE
21
+ SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # Encrypted Settings
2
+ A gem like Rails 5 settings but with multiple environments.
3
+
4
+ # Usage
5
+
6
+ Implemented a library similar to rails 5 encrypted settings.
7
+
8
+ To read the settings and print out to STDOUT:
9
+ ```bash
10
+ rake settings:read
11
+ ```
12
+
13
+ To edit settings:
14
+ ```bash
15
+ rake settings:edit
16
+ ```
17
+
18
+ You might need to pass some environment variables:
19
+ ```bash
20
+ RACK_ENV=development SETTINGS_ENCRYPTOION_KEY=DevelopmentSecretIsHardToBreak!! rake settings:edit
21
+ ```
22
+
23
+ The key can also be put in a `.settings_encryption_key` file. In that case just:
24
+ ```bash
25
+ RACK_ENV=production rake settings:edit
26
+ ```
27
+
28
+ By default, it would use sublime (`sub --wait`) to edit the settings. If you prefer another editor you can pass the command in `EDITOR` env variable, eg.
29
+ ```bash
30
+ EDITOR=vim rake settings:edit
31
+ ```
32
+
33
+ The environment will be determined like this:
34
+ ```ruby
35
+ environment = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || ENV["ENV"] || ENV["ENVIRONMENT"] || "development"
36
+ ```
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift("./lib")
4
+
5
+ environment = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || ENV["ENV"] || ENV["ENVIRONMENT"] || "development"
6
+
7
+ Encoding.default_external = "UTF-8"
8
+
9
+ require "encrypted_settings"
10
+ require "pry"
11
+ require "rake/testtask"
12
+
13
+ Rake::TestTask.new do |task|
14
+ task.libs << %w(test lib)
15
+ task.pattern = "test/test_*.rb"
16
+ end
17
+
18
+ task default: :test
19
+
20
+ namespace :settings do
21
+ desc "Initialize or edit encrypted settings file."
22
+ task :edit do
23
+ EncryptedSettings.new(environment).edit
24
+ end
25
+
26
+ desc "Read settings and print ot STDOUT"
27
+ task :read do
28
+ EncryptedSettings.new(environment).read
29
+ .then(&OpenStructToHash.method(:parse))
30
+ .then(&YAML.method(:dump))
31
+ .then(&self.method(:puts))
32
+ end
33
+ end
34
+
35
+ module OpenStructToHash
36
+ def self.parse(object)
37
+ object.marshal_dump.transform_values do |val|
38
+ val.is_a?(OpenStruct) ? parse(val) : val
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # frozen_string_literal: true
4
+
5
+ require "bundler/setup"
6
+ require "settings"
7
+
8
+ # You can add fixtures and/or initialization code here to make experimenting
9
+ # with your gem easier. You can also use a different console, if you like.
10
+
11
+ # (If you use this, don"t forget to add pry to your Gemfile!)
12
+ # require "pry"
13
+ # Pry.start
14
+
15
+ require "irb"
16
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+ IFS=$'\n\t'
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+ set -e
3
+ cd `dirname $0`/..
4
+
5
+ set -v
6
+ eval "bundle exec rake test"
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "encrypted_settings/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "encrypted_settings"
9
+ spec.version = SignedApiRequest::VERSION
10
+ spec.authors = ["Krzysztof Kaczmarczyk"]
11
+
12
+ spec.summary = "Encrypts settings so that they can be stored in the repository."
13
+ spec.description = "Similar to Rails encrypted secrets but with multiple environments."
14
+ spec.homepage = "https://github.com/krzyczak/encrypted_settings"
15
+ spec.license = "MIT"
16
+
17
+ # # Prevent pushing this gem to RubyGems.org by setting "allowed_push_host", or
18
+ # # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to "http://mygemserver.com""
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "bin"
27
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler"
31
+ spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "minitest-reporters"
33
+ spec.add_development_dependency "pry"
34
+ end
@@ -0,0 +1,79 @@
1
+ # signed_api_request
2
+ # encrypted_settings
3
+
4
+ # SignedApiRequest
5
+ # EncryptedSettings
6
+
7
+ # frozen_string_literal: true
8
+
9
+ require "openssl"
10
+ require "yaml"
11
+ require "tempfile"
12
+ require "ostruct"
13
+ require "fileutils"
14
+ require "json"
15
+
16
+ class EncryptedSettings
17
+ KEY_FILE_PATH = ".settings_encryption_key"
18
+
19
+ def initialize(env, editor: ENV["EDITOR"], key: nil)
20
+ key ||= ENV["SETTINGS_ENCRYPTION_KEY"]
21
+ key = File.read(KEY_FILE_PATH).strip if key.nil? && File.exist?(KEY_FILE_PATH)
22
+ raise "Invalid Encryption Key" if key.nil? || key.length != 32
23
+
24
+ @key = key
25
+ @editor = editor || "sub --wait"
26
+
27
+ @settings_path = "config/settings_#{env}.yml.enc"
28
+ end
29
+
30
+ def read
31
+ File.binread(settings_path)
32
+ .then(&self.method(:decrypt))
33
+ .then(&YAML.method(:load))
34
+ .then(&JSON.method(:dump))
35
+ .then { |config| JSON.parse(config, object_class: OpenStruct) }
36
+ end
37
+
38
+ def edit
39
+ # TODO: refactor into a few smaller methods.
40
+ FileUtils.mkdir_p("config")
41
+ content = ""
42
+ content = File.binread(settings_path) if File.exist?(settings_path)
43
+ tmp = Tempfile.new(["settings", ".yml"])
44
+ tmp.rewind
45
+ tmp.write(YAML.safe_load(decrypt(content)).to_yaml) unless content.empty?
46
+ tmp.close
47
+ system("#{editor} #{tmp.path}")
48
+
49
+ File.unlink(settings_path) if File.exist?(settings_path)
50
+
51
+ File.binwrite(settings_path, encrypt(File.read(tmp.path)))
52
+
53
+ tmp.unlink
54
+ end
55
+
56
+ private
57
+
58
+ attr_reader :key, :editor, :settings_path
59
+
60
+ def encrypt(plain_text)
61
+ cipher = OpenSSL::Cipher::AES256.new(:CBC)
62
+ cipher.encrypt
63
+ cipher.iv = "\xAA6RC\x98:?o_\xBD\x95|\xF6\x0E\xD7\xC5" # cipher.random_iv
64
+ cipher.key = key
65
+ cipher.update(plain_text) + cipher.final
66
+ rescue ArgumentError
67
+ ""
68
+ end
69
+
70
+ def decrypt(cipher_text)
71
+ decipher = OpenSSL::Cipher::AES256.new(:CBC)
72
+ decipher.decrypt
73
+ decipher.iv = "\xAA6RC\x98:?o_\xBD\x95|\xF6\x0E\xD7\xC5"
74
+ decipher.key = key
75
+ decipher.update(cipher_text) + decipher.final
76
+ end
77
+ end
78
+
79
+
@@ -0,0 +1,3 @@
1
+ module SignedApiRequest
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift("./lib")
4
+
5
+ Encoding.default_external = "UTF-8"
6
+
7
+ require "encrypted_settings"
8
+
9
+ # module Settings
10
+ # class << self
11
+ # attr_accessor :config
12
+
13
+ # def configure
14
+ # self.config ||= UidsEncoder.new
15
+ # yield(config)
16
+ # end
17
+ # end
18
+ # end
19
+
20
+ environment = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || ENV["ENV"] || ENV["ENVIRONMENT"] || "development"
21
+ Settings = EncryptedSettings.new(environment).read
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: encrypted_settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Krzysztof Kaczmarczyk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: minitest-reporters
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: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Similar to Rails encrypted secrets but with multiple environments.
70
+ email:
71
+ executables:
72
+ - console
73
+ - setup
74
+ - test
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".ruby-version"
80
+ - ".settings_encryption_key"
81
+ - ".travis.yml"
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - bin/test
90
+ - encrypted_settings.gemspec
91
+ - lib/encrypted_settings.rb
92
+ - lib/encrypted_settings/version.rb
93
+ - lib/settings.rb
94
+ homepage: https://github.com/krzyczak/encrypted_settings
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubygems_version: 3.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Encrypts settings so that they can be stored in the repository.
117
+ test_files: []