dinomischus 0.1.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: 45e3b2434da2f959d4aed159cbdc8397b6f22074c1be58d0efee583288136f68
4
+ data.tar.gz: 3dd3a6a60a78071de80c31b9124ee23900f5c24b3088e7e1dd61790bc41b2d50
5
+ SHA512:
6
+ metadata.gz: b7a092cf1aaa89ba600295e6e0eccff52b41760308acf097522d8bd6b9240768c78acd7cd0dec74f15b796065ca38dfd4f8d883e5912801e7a2e8fa461fb3b17
7
+ data.tar.gz: 784296a83738f6dc1cd4f34079092967c289598007c397e6230c05d64e46dcbaa3018af1abc414ec8bd5c52990bf3f4de7321951a016558600348279b8f02aef
Binary file
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at TODO: Write your email address. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
6
+ gemspec
7
+
8
+ gem "rake", "~> 12.0"
9
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 TODO: Write your name
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,89 @@
1
+ # Dinomischus
2
+
3
+ This gem is Configuration file utility.
4
+ The setting value is encrypted and saved.
5
+ You can divide the configuration file into multiple files and save them.
6
+
7
+ このgemは設定ファイルユーティリティです。
8
+ 設定値を暗号化して保存します。
9
+ 設定ファイルを複数に分割して保存できます。
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'dinomischus'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install dinomischus
26
+
27
+ ## Usage
28
+
29
+ ### For Reading
30
+ ```
31
+ require 'dinomischus'
32
+
33
+ # ex1
34
+ hash = Dinomischus.load('project_name_config_index.yml') # also project_name_config.yml
35
+ p hash[:key] # => decrypted-value
36
+ p hash[:key] # => raw-description
37
+
38
+ # ex2
39
+ hash = Dinomischus.load('project_name_config_index.yml', true) # also project_name_config.yml
40
+ p hash[:key][:value] # => decrypted-value
41
+ p hash[:key][:desc] # => raw-description
42
+ ```
43
+
44
+ ### Create Configure File. First Use
45
+ 設定ファイル作成(初回設定)
46
+
47
+ When you execute the following command
48
+ ```
49
+ $ dinomischus
50
+ ```
51
+
52
+ A menu will appear.
53
+ ```
54
+ ****** Welcome Egoistic Config ******
55
+ 1. Make Template
56
+ 2. Add or Update Crypted Value
57
+ 3. List Configs Simple
58
+ 4. List Configs Specify
59
+ 8. Ruby Command List
60
+ 9. End
61
+ -----------> Select Menu [1-4,8,9]:
62
+ ```
63
+
64
+ 1 テンプレート作成
65
+ 2 暗号化設定追加
66
+ 3 設定ファイルの設定値一覧
67
+ 4 設定ファイルの設定値一覧(説明付き)
68
+ 8 プログラム中にファイルを読む際のサンプル
69
+ 9 プログラム終了
70
+
71
+
72
+ ## Development
73
+
74
+ 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.
75
+
76
+ 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).
77
+
78
+ ## Contributing
79
+
80
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dinomischus. 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]/dinomischus/blob/master/CODE_OF_CONDUCT.md).
81
+
82
+
83
+ ## License
84
+
85
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
86
+
87
+ ## Code of Conduct
88
+
89
+ Everyone interacting in the Dinomischus project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dinomischus/blob/master/CODE_OF_CONDUCT.md).
@@ -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 "dinomischus"
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,30 @@
1
+ require_relative 'lib/dinomischus/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "dinomischus"
5
+ spec.version = Dinomischus::VERSION
6
+ spec.authors = ["Kuroko Sin"]
7
+ spec.email = ["Kuroko.Sin@gmail.com"]
8
+
9
+ spec.summary = %q{Helpful Secure Configuration File Utility}
10
+ # spec.description = %q{Write a longer description or delete this line.}
11
+ spec.homepage = "https://github.com/kurokoSin/Dinomischus"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ # spec.metadata["source_code_uri"] = "Put your gem's public repo URL here."
19
+ spec.metadata["source_code_uri"] = "https://github.com/kurokoSin/Dinomischus"
20
+ # spec.metadata["changelog_uri"] = "Put your gem's CHANGELOG.md URL here."
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dinomischus'
4
+
5
+ Dinomischus::Menu.menu
@@ -0,0 +1,61 @@
1
+ require 'yaml'
2
+
3
+ require File.expand_path("../dinomischus/version", __FILE__)
4
+
5
+ require File.expand_path("../dinomischus/crypt_aes.rb", __FILE__)
6
+ require File.expand_path("../dinomischus/merge_yaml.rb", __FILE__)
7
+ require File.expand_path("../dinomischus/l_key.rb", __FILE__)
8
+ require File.expand_path("../dinomischus/l_def.rb", __FILE__)
9
+ require File.expand_path("../dinomischus/l_conf.rb", __FILE__)
10
+ require File.expand_path("../dinomischus/menu.rb", __FILE__)
11
+
12
+
13
+ module Dinomischus
14
+ class Error < StandardError; end
15
+
16
+ # Menu
17
+ def self.menu()
18
+ Dinomischus::Menu.menu
19
+ end
20
+
21
+ # Create the key file
22
+ def self.create_key_file(path, password = "")
23
+ Dinomischus::KeyFile.create(path, password)
24
+ end
25
+
26
+ # Create the define file
27
+ def self.create_def_file(def_path, conf_path)
28
+ Dinomischus::DefFile.create(def_path, conf_path)
29
+ end
30
+
31
+ def self.create_conf_file(conf_path, key_path)
32
+ Dinomischus::ConfFile.create(conf_path, key_path)
33
+ end
34
+
35
+ def self.set_config(conf_path, key, value, desc = "", do_encrypt = false)
36
+ Dinomischus::ConfFile.set_item(conf_path, key, value, desc, do_encrypt)
37
+ end
38
+
39
+ # Read Config File
40
+ def self.load_file( path, specify = false )
41
+ # get loading target
42
+ yml = YAML.load_file(path)
43
+ files = yml[0].has_key?(:conf_path) ? yml : [ {conf_path: path} ]
44
+
45
+ # loading config files...
46
+ config_list = {}
47
+ files.each do |p|
48
+ items = {}
49
+ items = load_conf(p[:conf_path], specify)
50
+ merge_yaml(config_list, items)
51
+ end
52
+ config_list
53
+ end
54
+
55
+ private
56
+
57
+ def self.load_conf(conf_path, specify )
58
+ Dinomischus::ConfFile.load_file(conf_path, specify)
59
+ end
60
+ end
61
+
@@ -0,0 +1,75 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ module Crypter
5
+ class CryptAes
6
+ # ======================================
7
+ # <暗号化>
8
+ # ======================================
9
+ # plain_text: 暗号化したい文字列
10
+ # password : 好きなパスワード
11
+ # salt : ソルト:Base64でエンコードされた文字列
12
+ # ======================================
13
+ def self.encrypt(plain_text, password, use_salt = false)
14
+ raise RuntimeError.new("暗号化対象の文字列がありません。") if plain_text.nil?
15
+ raise RuntimeError.new("暗号化対象の文字列がありません。") if plain_text.empty?
16
+ raise RuntimeError.new("パスワードを設定していません。" ) if password.nil?
17
+ raise RuntimeError.new("パスワードを設定していません。" ) if password.empty?
18
+
19
+ # saltを生成
20
+ # salt = OpenSSL::Random.random_bytes(8)
21
+ salt = use_salt ? OpenSSL::Random.random_bytes(8) : ""
22
+
23
+ # 暗号器を生成
24
+ enc = OpenSSL::Cipher::AES.new(256, :CBC)
25
+ enc.encrypt
26
+
27
+ # パスワードとsaltをもとに鍵とivを生成し、設定
28
+ key_iv = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, 2000, enc.key_len + enc.iv_len, "sha256")
29
+ enc.key = key_iv[0, enc.key_len]
30
+ enc.iv = key_iv[enc.key_len, enc.iv_len]
31
+
32
+ # 文字列を暗号化
33
+ encrypted_text = enc.update(plain_text) + enc.final
34
+
35
+ # Base64でエンコード
36
+ encrypted_text = Base64.encode64(encrypted_text).chomp
37
+ salt = Base64.encode64(salt).chomp
38
+
39
+ # 暗号とsaltを返す
40
+ [encrypted_text, salt]
41
+ end
42
+
43
+
44
+ # ======================================
45
+ # <復号>
46
+ # ======================================
47
+ # encrypted_text: 復号したい文字列
48
+ # password : 暗号化した時に指定した文字列
49
+ # salt : 暗号化した時に生成されたsalt
50
+ # ======================================
51
+ def self.decrypt(encrypted_text, password, salt_base64 = "")
52
+ raise RuntimeError.new("復号対象の文字列がありません。") if encrypted_text.nil?
53
+ raise RuntimeError.new("復号対象の文字列がありません。") if encrypted_text.empty?
54
+ raise RuntimeError.new("パスワードがありません。") if password.nil?
55
+ raise RuntimeError.new("パスワードがありません。") if password.empty?
56
+
57
+
58
+ # Base64でデコード
59
+ encrypted_text = Base64.decode64(encrypted_text)
60
+ salt = Base64.decode64(salt_base64) # if !salt_base64.nil? # 空文字でもbase64処理を施す
61
+
62
+ # 復号器を生成
63
+ dec = OpenSSL::Cipher::AES.new(256, :CBC)
64
+ dec.decrypt
65
+
66
+ # パスワードとsaltをもとに鍵とivを生成し、設定
67
+ key_iv = OpenSSL::PKCS5.pbkdf2_hmac(password, salt, 2000, dec.key_len + dec.iv_len, "sha256")
68
+ dec.key = key_iv[0, dec.key_len]
69
+ dec.iv = key_iv[dec.key_len, dec.iv_len]
70
+
71
+ # 暗号を復号
72
+ dec.update(encrypted_text) + dec.final
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,119 @@
1
+ require 'yaml'
2
+ require 'json'
3
+ require 'optparse'
4
+ require 'securerandom'
5
+ require 'base64'
6
+
7
+ require File.expand_path('../crypt_aes.rb', __FILE__)
8
+
9
+ module Dinomischus
10
+
11
+ class ConfFile
12
+ # create config file
13
+ def self.create(conf_path, key_path)
14
+ raise RuntimeError.new("鍵ファイルが存在しません。#{key_path}") if !File.exist?(key_path)
15
+ raise RuntimeError.new("設定ファイルが既に存在します。#{conf_path}") if File.exist?(conf_path)
16
+
17
+ base = []
18
+ hash = {"key_path": key_path}
19
+ base.push( hash )
20
+ base.push( {"dummy": { "value": "", "desc": "" }} )
21
+ File.open(conf_path, 'w') do |f|
22
+ YAML.dump(base, f)
23
+ end
24
+ end
25
+
26
+
27
+ def self.set_item(conf_path, key, value, desc = "", do_encrypt = false)
28
+ raise RuntimeError.new("設定ファイルが存在しません。#{conf_path}") if !File.exist?(conf_path)
29
+
30
+ yml = YAML.load_file(conf_path)
31
+
32
+ key_path = yml[0][:key_path]
33
+ raise RuntimeError.new("鍵ファイルが存在しません。#{key_path}") if !File.exist?(key_path)
34
+
35
+ val_text = do_encrypt ? "?#{exec_encrypt( key_path, value)}" : value
36
+
37
+ yml[1][key.to_sym] = {"value": val_text, "desc": desc}
38
+ File.open(conf_path, 'w') do |f|
39
+ YAML.dump( yml, f )
40
+ end
41
+ true
42
+ end
43
+
44
+
45
+ def self.load_file(conf_path, specify = false )
46
+ raise RuntimeError.new("設定ファイルが存在しません。#{conf_path}") if !File.exist?(conf_path)
47
+
48
+ conf_file = YAML.load_file(conf_path)
49
+ key_path = conf_file[0][:key_path]
50
+ raw_items = conf_file[1]
51
+ items = {}
52
+ raw_items.keys.each do |key|
53
+ continue if key == "dummy" && raw_items[key][:value] == ""
54
+ keyval = get(key_path, raw_items[key][:value] )
55
+ keydesc = raw_items[key][:desc]
56
+ items[key] = specify ? {"value": keyval, "desc": keydesc} : keyval
57
+ end
58
+ items
59
+ end
60
+
61
+
62
+ # private class method -----------------
63
+
64
+ def self.blank?(obj)
65
+ obj.nil? || obj.empty?
66
+ end
67
+
68
+ def self.get(key_path, value)
69
+ ret = value
70
+ if value.match /^\?.*/
71
+ ret.gsub!("\n","")
72
+ ret = ret[/\?(.*)/, 1]
73
+ ret = exec_decrypt(key_path, ret)
74
+ else
75
+ ret = value
76
+ end
77
+
78
+ ret
79
+ end
80
+
81
+ def self.exec_encrypt( key_path, value)
82
+ raise RuntimeError.new("鍵ファイルが存在しません。#{key_path}") if !File.exist?(key_path)
83
+
84
+ keys = YAML.load_file(key_path)
85
+ enctype = keys[:key][:type]
86
+ if enctype == "sha256"
87
+ # sha256の処理。
88
+ pass = keys[:key][:value]
89
+ enc1 = Crypter::CryptAes.encrypt( value, pass, true )
90
+ enc2 = Crypter::CryptAes.encrypt( enc1.to_s, pass, false )
91
+ else
92
+ raise RuntimeError.new("未サポートの暗号です。#{enctype}")
93
+ end
94
+ enc2[0]
95
+ end
96
+
97
+ def self.exec_decrypt( key_path, value)
98
+ raise RuntimeError.new("鍵ファイルが存在しません。#{key_path}") if !File.exist?(key_path)
99
+
100
+ keys = YAML.load_file(key_path)
101
+ enctype = keys[:key][:type]
102
+ if enctype == "sha256"
103
+ pass = keys[:key][:value]
104
+ v2 = Crypter::CryptAes.decrypt( value, pass )
105
+ v1raw = YAML.load(v2)
106
+ v1 = Crypter::CryptAes.decrypt( v1raw[0], pass, v1raw[1])
107
+ else
108
+ raise RuntimeError.new("未サポートの暗号です。#{enctype}")
109
+ end
110
+ end
111
+
112
+ private_class_method :blank?
113
+ private_class_method :get
114
+ private_class_method :exec_encrypt
115
+ private_class_method :exec_decrypt
116
+
117
+ end
118
+ end
119
+
@@ -0,0 +1,48 @@
1
+ require 'yaml'
2
+ require 'json'
3
+ require 'optparse'
4
+ require 'securerandom'
5
+ require 'base64'
6
+
7
+ require File.expand_path('../crypt_aes.rb', __FILE__)
8
+ require File.expand_path('../l_conf.rb', __FILE__)
9
+
10
+ module Dinomischus
11
+ attr_reader :items
12
+ class DefFile
13
+ # Create the define file
14
+ def self.create(def_path, conf_path )
15
+ raise RuntimeError.new("定義ファイルの指定がありません。#{def_path}" ) if def_path.nil?
16
+ raise RuntimeError.new("定義ファイルの指定がありません。#{def_path}" ) if def_path.empty?
17
+ raise RuntimeError.new("定義ファイルが既に存在します。#{def_path}" ) if File.exist?(def_path)
18
+ raise RuntimeError.new("設定ファイルの指定がありません。#{conf_path}" ) if conf_path.nil?
19
+ raise RuntimeError.new("設定ファイルの指定がありません。#{conf_path}" ) if conf_path.empty?
20
+ raise RuntimeError.new("設定ファイルが既に存在します。#{conf_path}" ) if File.exist?(conf_path)
21
+
22
+ def_hash = [ {"conf_path": conf_path} ]
23
+ File.open(def_path, 'w') do |f|
24
+ YAML.dump(def_hash, f)
25
+ end
26
+ true
27
+ end
28
+
29
+ # Load All Config
30
+ def self.load_config(def_path)
31
+ files = load_file(def_path)
32
+ configs = {}
33
+ files.each do |f|
34
+ cfg = Dinomischus::ConfFile.load_file(f[:conf_path])
35
+ configs.merge!(cfg)
36
+ end
37
+ configs
38
+ end
39
+
40
+ # load from file to yaml
41
+ def self.load_file(def_path)
42
+ yml = YAML.load_file(def_path)
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
@@ -0,0 +1,28 @@
1
+ require 'yaml'
2
+ require 'json'
3
+ require 'optparse'
4
+ require 'securerandom'
5
+ require 'base64'
6
+
7
+ require File.expand_path('../crypt_aes.rb', __FILE__)
8
+
9
+ module Dinomischus
10
+ class KeyFile
11
+
12
+ # Create the key file
13
+ def self.create(path, password = "")
14
+ raise RuntimeError.new("鍵ファイルが既に存在します。#{path}") if File.exist?(path)
15
+ password = SecureRandom.urlsafe_base64 if password.empty?
16
+ hash = {"key": {"type": "sha256", "value": password}}
17
+ File.open(path, 'w') do |f|
18
+ YAML.dump(hash, f)
19
+ end
20
+ true
21
+ end
22
+
23
+ def self.load_file(path)
24
+ yml = YAML.load_file(path)
25
+ end
26
+ end
27
+ end
28
+
@@ -0,0 +1,164 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pp'
4
+ require 'fileutils'
5
+ # require File.expand_path('../../dinomischus.rb', __FILE__)
6
+
7
+ module Dinomischus
8
+ class Menu
9
+
10
+ def self.menu()
11
+ menu_clear_screen()
12
+ loop {
13
+ print "-----------> Select Menu [1-4,c,h,z]: "
14
+ sel_num = gets.chomp
15
+
16
+ case sel_num
17
+ when "1" then menu_template()
18
+ when "2" then menu_add_config()
19
+ when "3" then list_config(false) # simple
20
+ when "4" then list_config(true) # specify
21
+ when "c" then menu_clear_screen()
22
+ when "h" then command_help()
23
+ when "z" then exit
24
+ end
25
+ }
26
+ end
27
+
28
+ def self.menu_clear_screen()
29
+ puts "\e[H\e[2J" # 画面のクリア
30
+ menu_select()
31
+ end
32
+
33
+ def self.menu_select()
34
+ puts "****** Welcome Egoistic Config ******"
35
+ puts " 1. Make Template"
36
+ puts " 2. Add or Update Crypted Value"
37
+ puts " 3. List Configs Simple"
38
+ puts " 4. List Configs Specify"
39
+ puts " c. Clear Screen"
40
+ puts " h. Ruby Command List"
41
+ puts " z. End "
42
+ end
43
+
44
+ def self.menu_template()
45
+ puts " "
46
+ puts "****** Make Template ******"
47
+ key_path = ""; conf_path = ""; def_path = "";
48
+ loop{
49
+ puts " "
50
+ print "Input Your Config Prefix Name : "
51
+ prj = gets.chomp
52
+ key_path = File.expand_path("~/.crypt_config/#{prj}_key.yml" )
53
+ conf_path = File.expand_path("./config/#{prj}_config.yml", Dir.pwd )
54
+ def_path = File.expand_path("./config/#{prj}_config_index.yml", Dir.pwd )
55
+ continue if prj == ""
56
+
57
+ puts "Make File Default Value is ... "
58
+ puts " Key File Place [#{key_path}]"
59
+ puts " Config File Place [#{conf_path}]"
60
+ puts " Define File Place [#{def_path}]"
61
+ puts " "
62
+ print "Press Any Key to Next Step... "
63
+ ans = gets.chomp
64
+ break
65
+ }
66
+
67
+ make_template(key_path, conf_path, def_path)
68
+ puts "Done! "
69
+ puts "Next Step is Add Crypt Config|Plain Config to ""#{conf_path}"" . "
70
+ puts "Add Config Value then You Select Menu No.2 ."
71
+ end
72
+
73
+ def self.menu_add_config()
74
+ puts " "
75
+ puts "****** Crypted Value Setting ******"
76
+ conf_path = ""
77
+ loop {
78
+ print " Input Your Config Path : "
79
+ conf_path = gets.chomp
80
+ if !File.exist?(conf_path)
81
+ puts "Error. No Exists Config Path : [#{conf_path}]"
82
+ else
83
+ break
84
+ end
85
+ }
86
+ key = ""; value = ""; desc = "";
87
+ (print " Input Your Key : "; key = gets.chomp ) while key.empty?
88
+ (print " Input Your Value : "; value = gets.chomp ) while value.empty?
89
+ (print " Input Your Description : "; desc = gets.chomp )
90
+
91
+ add_crypted_value(conf_path, key, value, desc)
92
+
93
+ puts " "
94
+ puts "Done! "
95
+ end
96
+
97
+ def self.list_config(specify = false)
98
+ puts "****** List Configs #{specify ? "Specify" : "Simple"} ******"
99
+ conf_path = ""
100
+ loop {
101
+ print " Input Your Config Path : "
102
+ conf_path = gets.chomp
103
+ if !File.exist?(conf_path)
104
+ puts "Error. No Exists Config Path : [#{conf_path}]"
105
+ else
106
+ break
107
+ end
108
+ }
109
+ yml = list_config_file( conf_path, specify )
110
+ puts " "
111
+ pp yml
112
+ puts " "
113
+ end
114
+
115
+ # class private method -----------------------------
116
+
117
+ def self.make_template( key_path, conf_path, def_path)
118
+ check_and_make_dir(key_path)
119
+ check_and_make_dir(def_path)
120
+ check_and_make_dir(conf_path)
121
+
122
+
123
+ Dinomischus.create_key_file(key_path) rescue p "Exist Already. Skip Create. [#{key_path}]"
124
+ Dinomischus.create_def_file(def_path, conf_path) rescue p "Exist Already. Skip Create. [#{def_path}]"
125
+ Dinomischus.create_conf_file(conf_path, key_path) rescue p "Exist Already. Skip Create. [#{conf_path}]"
126
+ end
127
+
128
+ def self.add_crypted_value(conf_path, key, value, desc)
129
+ Dinomischus.set_config(conf_path, key, value, desc, true)
130
+ end
131
+
132
+ def self.list_config_file(path, specify)
133
+ Dinomischus.load_file(path, specify)
134
+ end
135
+
136
+ def self.check_and_make_dir(file_path)
137
+ p = File.expand_path('..', file_path )
138
+ FileUtils.mkdir_p( p ) unless Dir.exist?( p )
139
+ end
140
+
141
+ def self.command_help()
142
+ puts "require 'dinomischus'"
143
+ puts " "
144
+ puts "# ex1 "
145
+ puts "hash = Dinomischus.load_file('project_name_config_index.yml') # also project_name_config.yml "
146
+ puts "p hash[:key] # => decrypted-value "
147
+ puts "p hash[:key] # => raw-description "
148
+ puts " "
149
+ puts "# ex2 "
150
+ puts "hash = Dinomischus.load_file('project_name_config_index.yml', true) # also project_name_config.yml "
151
+ puts "p hash[:key][:value] # => decrypted-value "
152
+ puts "p hash[:key][:desc] # => raw-description "
153
+ puts " "
154
+ end
155
+
156
+ private_class_method :make_template
157
+ private_class_method :add_crypted_value
158
+ private_class_method :list_config_file
159
+ private_class_method :command_help
160
+ end
161
+
162
+ end
163
+
164
+
@@ -0,0 +1,16 @@
1
+ # Qiitaの記事からいただきました。
2
+ # https://qiita.com/MasahiroMorita@github/items/3679c760fdc7717d84aa
3
+ # author: @MasahiroMorita@github
4
+
5
+ require 'yaml'
6
+
7
+ def merge_yaml(yaml1, yaml2)
8
+ yaml2.each do |key, value|
9
+ if value.class == Hash && yaml1.key?(key)
10
+ yaml1[key] = merge_yaml(yaml1[key], value)
11
+ else
12
+ yaml1[key] = value
13
+ end
14
+ end
15
+ return yaml1
16
+ end
@@ -0,0 +1,3 @@
1
+ module Dinomischus
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dinomischus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Kuroko Sin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - Kuroko.Sin@gmail.com
16
+ executables:
17
+ - dinomischus
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".README.md.swp"
22
+ - ".gitignore"
23
+ - ".rspec"
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - dinomischus.gemspec
32
+ - exe/dinomischus
33
+ - lib/dinomischus.rb
34
+ - lib/dinomischus/crypt_aes.rb
35
+ - lib/dinomischus/l_conf.rb
36
+ - lib/dinomischus/l_def.rb
37
+ - lib/dinomischus/l_key.rb
38
+ - lib/dinomischus/menu.rb
39
+ - lib/dinomischus/merge_yaml.rb
40
+ - lib/dinomischus/version.rb
41
+ homepage: https://github.com/kurokoSin/Dinomischus
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://github.com/kurokoSin/Dinomischus
46
+ source_code_uri: https://github.com/kurokoSin/Dinomischus
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: 2.3.0
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.0.3
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: Helpful Secure Configuration File Utility
66
+ test_files: []