secretmgr 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: 3963af6a19062adf86d445a63f946ed9a29adbee1d42658a3d1ba6bdcf1bf841
4
+ data.tar.gz: fdcc370e9a59e12624eb2d19088abeb671fe5ba6583d837d2c41f87cace4f4be
5
+ SHA512:
6
+ metadata.gz: 4b5878e6b755a2093b7f1c604c926bd25ab140eda458105857cd8bef2312af65563692c5fc9c87274bc267ef45f767d601aade4502b82e6615ba89ca9ee30deb
7
+ data.tar.gz: 7a118458b75959fbf289be98fd1ee297e8ff78c56a570e84acd5619e610f3a7d695dd344e24574e24149fcde50f123192af278a0c5410f330251cd2dfdb9f3ce
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.6
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 120
16
+
17
+ Documentation:
18
+ Enabled: false
19
+
20
+ #追加
21
+ Style/FrozenStringLiteralComment:
22
+ Enabled: false
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,46 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2023-11-30 09:19:30 UTC using RuboCop version 1.57.2.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 4
10
+ # Configuration parameters: EnforcedStyle.
11
+ # SupportedStyles: native, lf, crlf
12
+ Layout/EndOfLine:
13
+ Exclude:
14
+ - 'Rakefile'
15
+ - 'bin/console'
16
+ - 'lib/secretmgr/version.rb'
17
+ # - 'spec/secretmgr_spec.rb'
18
+
19
+ # Offense count: 1
20
+ # Configuration parameters: AllowComments.
21
+ Lint/EmptyWhen:
22
+ Exclude:
23
+ # - 'exe/secretmgr'
24
+
25
+ # Offense count: 2
26
+ # Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes.
27
+ Metrics/AbcSize:
28
+ Max: 27
29
+
30
+ # Offense count: 1
31
+ # Configuration parameters: CountComments, CountAsOne.
32
+ Metrics/ClassLength:
33
+ Max: 154
34
+
35
+ # Offense count: 5
36
+ # Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns.
37
+ Metrics/MethodLength:
38
+ Max: 21
39
+
40
+ # Offense count: 1
41
+ # Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
42
+ # SupportedStyles: snake_case, normalcase, non_integer
43
+ # AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
44
+ Naming/VariableNumber:
45
+ Exclude:
46
+ # - 'spec/spec_helper.rb'
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-09-20
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ykominami@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in secretmgr.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 1.21"
11
+
12
+ gem "multi_json"
13
+ gem "ykutils"
14
+ gem "ykxutils"
data/Gemfile.lock ADDED
@@ -0,0 +1,80 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ secretmgr (0.1.0)
5
+ multi_json
6
+ rake (~> 13.0)
7
+ rspec (~> 3.0)
8
+ rubocop (~> 1.21)
9
+ ykutils
10
+ ykxutils
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ ast (2.4.2)
16
+ diff-lcs (1.5.0)
17
+ erubi (1.11.0)
18
+ erubis (2.7.0)
19
+ filex (0.1.7)
20
+ erubis
21
+ messagex
22
+ json (2.6.2)
23
+ messagex (0.1.5)
24
+ multi_json (1.15.0)
25
+ parallel (1.22.1)
26
+ parser (3.1.2.1)
27
+ ast (~> 2.4.1)
28
+ rainbow (3.1.1)
29
+ rake (13.0.6)
30
+ regexp_parser (2.6.0)
31
+ rexml (3.2.5)
32
+ rspec (3.11.0)
33
+ rspec-core (~> 3.11.0)
34
+ rspec-expectations (~> 3.11.0)
35
+ rspec-mocks (~> 3.11.0)
36
+ rspec-core (3.11.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-expectations (3.11.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.11.0)
41
+ rspec-mocks (3.11.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.11.0)
44
+ rspec-support (3.11.1)
45
+ rubocop (1.36.0)
46
+ json (~> 2.3)
47
+ parallel (~> 1.10)
48
+ parser (>= 3.1.2.1)
49
+ rainbow (>= 2.2.2, < 4.0)
50
+ regexp_parser (>= 1.8, < 3.0)
51
+ rexml (>= 3.2.5, < 4.0)
52
+ rubocop-ast (>= 1.20.1, < 2.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 3.0)
55
+ rubocop-ast (1.21.0)
56
+ parser (>= 3.1.1.0)
57
+ ruby-progressbar (1.11.0)
58
+ tilt (2.0.11)
59
+ unicode-display_width (2.3.0)
60
+ ykutils (0.1.7)
61
+ erubi
62
+ filex
63
+ tilt
64
+ ykxutils (0.1.7)
65
+
66
+ PLATFORMS
67
+ x64-mingw-ucrt
68
+ x64-mingw32
69
+
70
+ DEPENDENCIES
71
+ multi_json
72
+ rake (~> 13.0)
73
+ rspec (~> 3.0)
74
+ rubocop (~> 1.21)
75
+ secretmgr!
76
+ ykutils
77
+ ykxutils
78
+
79
+ BUNDLED WITH
80
+ 2.4.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 ykominami
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,37 @@
1
+ # Secretmgr
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/secretmgr`. 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
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add secretmgr
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install secretmgr
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ 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.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ykominami/secretmgr. 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/ykominami/secretmgr/blob/main/CODE_OF_CONDUCT.md).
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the Secretmgr project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ykominami/secretmgr/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/exe/secretmgr ADDED
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "pathname"
5
+ require "secretmgr"
6
+
7
+ arg = ARGV
8
+ cmd = arg[0]
9
+ if cmd == "setup_data"
10
+ # p "setup_data 0"
11
+ arg.shift
12
+ secret_dir = arg.shift
13
+ plain_setting_file = arg.shift
14
+ plain_secret_file = arg.shift
15
+ secret_dir_pn = Pathname.new(secret_dir)
16
+ plain_setting_file_pn = Pathname.new(plain_setting_file)
17
+ plain_secret_file_pn = Pathname.new(plain_secret_file)
18
+
19
+ exit 10 unless secret_dir_pn.exist?
20
+ # p "setup_data 11"
21
+ exit 11 unless secret_dir_pn.directory?
22
+ # p "setup_data 12"
23
+ exit 12 unless plain_setting_file_pn.exist?
24
+ # p "setup_data 13"
25
+ exit 13 unless plain_setting_file_pn.file?
26
+ # p "setup_data 14"
27
+ exit 14 unless plain_secret_file_pn.exist?
28
+ # p "setup_data 15"
29
+ exit 15 unless plain_secret_file_pn.file?
30
+
31
+ # p "setup_data 100"
32
+ else
33
+ setting_txt_file = arg.shift
34
+ secret_txt_file = arg.shift
35
+
36
+ # p "setting_txt_file=#{setting_txt_file}"
37
+ if (arg[0] == "--template-dir") || (arg[0] == "-d")
38
+ template_dir = arg[1]
39
+ arg.shift
40
+ arg.shift
41
+ end
42
+
43
+ # p "template_dir=#{template_dir}"
44
+
45
+ if (arg[0] == "--target") || (arg[0] == "-t")
46
+ target = arg[1]
47
+ sub_target = arg[2]
48
+ arg.shift
49
+ arg.shift
50
+ arg.shift
51
+ end
52
+
53
+ # p "target=#{target}"
54
+ # p "sub_target=#{sub_target}"
55
+
56
+ setting_pn = Pathname.new(setting_txt_file)
57
+ exit 10 unless setting_pn.exist?
58
+ exit 10 unless setting_pn.file?
59
+
60
+ secret_dir_pn = setting_pn.parent
61
+
62
+ secret_pn = Pathname.new(secret_txt_file)
63
+ exit 10 unless secret_pn.exist?
64
+ exit 10 unless secret_pn.file?
65
+
66
+ # secret_txt_file
67
+ end
68
+
69
+ case cmd
70
+ when "setup_data"
71
+ inst = Secretmgr::Secretmgr.create(secret_dir_pn, plain_setting_file_pn, plain_secret_file_pn)
72
+ inst.setup
73
+ else
74
+ sm = Secretmgr::Secretmgr.new(secret_dir_pn)
75
+ sm.set_setting_for_query(target, sub_target)
76
+ sm.load
77
+ p sm.make(template_dir, target, sub_target)
78
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "yaml"
5
+
6
+ module Secretmgr
7
+ class Config
8
+ def initialize(parent_pn, format_filename = "format.txt")
9
+ format_pn = parent_pn + format_filename
10
+ file_content = File.read(format_pn)
11
+ @hash = YAML.safe_load(file_content)
12
+ end
13
+
14
+ def file_format(*keys)
15
+ result = keys.flatten.each_with_object([@hash]) do |item, memo|
16
+ hash = memo[0]
17
+ memo[0] = if hash
18
+ (hash.instance_of?(Hash) ? hash[item] : nil)
19
+ end
20
+ end
21
+ result ? (result[0] || @hash["default"]) : @hash["default"]
22
+ end
23
+
24
+ def get_file_path(parent_dir_pn, *keys)
25
+ flat_keys = keys.flatten
26
+ valid_keys = flat_keys.reject(&:nil?)
27
+ file_format = file_format(valid_keys)
28
+ case file_format
29
+ when "JSON_FILE"
30
+ flat_keys.unshift("JSON_FILE")
31
+ flat_keys.push("config.json")
32
+ when "YAML"
33
+ flat_keys = ["secret.yml"]
34
+ end
35
+ array = flat_keys.each_with_object([parent_dir_pn]) do |item, memo|
36
+ memo[0] = memo[0] + item if item
37
+ end
38
+ array[0]
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,200 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module Secretmgr
6
+ # 秘匿情報マネージャ
7
+ class Secretmgr
8
+ attr_reader :decrypted_text, :secret
9
+
10
+ class << self
11
+ def create(secret_dir_pn, plain_setting_file_pn, plain_secret_file_pn)
12
+ @inst = Secretmgr.new(secret_dir_pn)
13
+ @inst.set_setting_for_data(plain_setting_file_pn, plain_secret_file_pn)
14
+ @inst
15
+ end
16
+ end
17
+
18
+ def initialize(secret_dir_pn)
19
+ @content = nil
20
+ @secret_dir_pn = secret_dir_pn
21
+ @encrypted_setting_file_pn = "#{@secret_dir_pn}setting.yml"
22
+
23
+ @format_config = Config.new(@secret_dir_pn, "format.txt")
24
+ home_dir = ENV["HOME"]
25
+ @home_pn = Pathname.new(home_dir)
26
+ # pemフォーマットの公開鍵ファイルの内容を取得
27
+ path = File.join(home_dir, ".ssh", "pem")
28
+ pub_key = File.read(path)
29
+ # 鍵をOpenSSLのオブジェクトにする
30
+ @public_key = OpenSSL::PKey::RSA.new(pub_key)
31
+ path = File.join(home_dir, ".ssh", "id_rsa_no")
32
+ private_key = File.read(path)
33
+ @private_key = OpenSSL::PKey::RSA.new(private_key)
34
+
35
+ @mode = OpenSSL::PKey::RSA::PKCS1_PADDING
36
+ end
37
+
38
+ def set_setting_for_data(plain_setting_file_pn, plain_secret_file_pn)
39
+ @plain_setting_file_pn = plain_setting_file_pn
40
+ @plain_secret_file_pn = plain_secret_file_pn
41
+ @plain_dir_pn = @plain_setting_file_pn.parent
42
+ end
43
+
44
+ def setup
45
+ setup_setting
46
+ setup_secret
47
+ setup_secret_for_json_file
48
+ end
49
+
50
+ def setup_setting
51
+ content = File.read(@plain_setting_file_pn)
52
+ @setting = Ykxutils.yaml_load_compati(content)
53
+ # content = YAML.dump(@setting)
54
+ encrypted_text = encrypt_with_public_key(content)
55
+ dest_setting_file_pn = make_pair_file_pn(@secret_dir_pn, @plain_setting_file_pn, "yml")
56
+
57
+ File.write(dest_setting_file_pn, encrypted_text)
58
+ end
59
+
60
+ def make_pair_file_pn(dest_dir_pn, file_pn, ext)
61
+ basename = file_pn.basename
62
+ extname = basename.extname
63
+ return nil if extname == ext
64
+
65
+ basename = file_pn.basename(".*")
66
+ dest_dir_pn + %(#{basename}.#{ext})
67
+ end
68
+
69
+ def setup_secret
70
+ plaintext = File.read(@plain_secret_file_pn)
71
+ encrypted_text = encrypt_with_common_key(plaintext, @setting["key"], @setting["iv"])
72
+ dest_secret_file_pn = make_pair_file_pn(@secret_dir_pn, @plain_secret_file_pn, "yml")
73
+ dest_secret_file_pn.realpath
74
+ # puts "setup_secret dest_secret_file_pn=#{dest_secret_file_pn}"
75
+ # puts "real_pn=#{real_pn}"
76
+ File.write(dest_secret_file_pn, encrypted_text)
77
+ end
78
+
79
+ def setup_secret_for_json_file
80
+ top_pn = "#{@plain_dir_pn}JSON_FILE"
81
+ top_pn.find do |x|
82
+ # p x if x.file?
83
+ relative_path = x.relative_path_from(@plain_dir_pn)
84
+ # p relative_path
85
+ encrypt_and_copy(x, @secret_dir_pn, relative_path)
86
+ end
87
+ end
88
+
89
+ def encrypt_and_copy(src_pn, dest_top_dir_pn, relative_path)
90
+ dest_pn = dest_top_dir_pn + relative_path
91
+ return unless src_pn.exist? && src_pn.file?
92
+
93
+ puts "e_adn_c #{src_pn} -> #{dest_pn}"
94
+ plaintext = File.read(src_pn)
95
+ encrypted_text = encrypt_with_common_key(plaintext, @setting["key"], @setting["iv"])
96
+ File.write(dest_pn, encrypted_text)
97
+ end
98
+
99
+ def set_setting_for_query(*dirs)
100
+ @valid_dirs = dirs.flatten.reject(&:nil?)
101
+ @target, @sub_target, _tmp = @valid_dirs
102
+ # p "@valid_dirs=#{@valid_dirs}"
103
+ @file_format = @format_config.file_format(@target, @sub_target)
104
+ p "@file_format=#{@file_format}"
105
+ p "dirs=#{dirs}"
106
+ @encrypted_secret_file_pn = @format_config.get_file_path(@secret_dir_pn, dirs)
107
+ end
108
+
109
+ def load_setting
110
+ encrypted_text = File.read(@encrypted_setting_file_pn)
111
+ # puts "encrypted_text=#{encrypted_text}"
112
+ decrypted_text = decrypt_with_private_key(encrypted_text)
113
+ setting = YAML.safe_load(decrypted_text)
114
+ @key = setting["key"]
115
+ @iv = setting["iv"]
116
+ # p "load_settings @key=#{@key}"
117
+ # p "load_settings @iv=#{@iv}"
118
+ end
119
+
120
+ def load_secret
121
+ base64_text = File.read(@encrypted_secret_file_pn)
122
+ encrypted_content = Base64.decode64(base64_text)
123
+ begin
124
+ @decrpyted_content = decrypt_with_common_key(encrypted_content, @key, @iv)
125
+ @content = case @file_format
126
+ when "JSON_FILE"
127
+ @decrpyted_content
128
+ when "YAML"
129
+ @secret = YAML.safe_load(@decrpyted_content)
130
+ @sub_target ? @secret[@target][@sub_target] : @secret[@target]
131
+ else
132
+ ""
133
+ end
134
+ rescue StandardError => e
135
+ puts e
136
+ puts e.message
137
+ puts e.backtrace
138
+ puts "Can't dencrypt #{@encrypted_setting_file_pn}"
139
+ end
140
+ @content
141
+ end
142
+
143
+ def load
144
+ load_setting
145
+ load_secret
146
+ end
147
+
148
+ def encrypt_with_public_key(data)
149
+ Base64.encode64(
150
+ @public_key.public_encrypt(
151
+ data,
152
+ OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING
153
+ )
154
+ ).delete("\n")
155
+ end
156
+
157
+ def decrypt_with_private_key(base64_text)
158
+ @private_key.private_decrypt(
159
+ Base64.decode64(base64_text),
160
+ OpenSSL::PKey::RSA::PKCS1_OAEP_PADDING
161
+ )
162
+ end
163
+
164
+ # 引数 str を暗号化した結果を返す
165
+ def encrypt_with_common_key(plaintext, key, _ivalue)
166
+ encx = OpenSSL::Cipher.new(CIPHER_NAME)
167
+ encx.encrypt
168
+ encx.key = key
169
+ encx.iv = ivvalue
170
+ # str に与えた文字列を暗号化します。
171
+ encrypted_text = encx.update(plaintext) + encx.final
172
+
173
+ Base64.encode64(encrypted_text)
174
+ end
175
+
176
+ def decrypt_with_common_key(encrypted_data, key, ivalue)
177
+ decx = OpenSSL::Cipher.new(CIPHER_NAME)
178
+ decx.decrypt
179
+ decx.key = key
180
+ decx.iv = ivalue
181
+ data = decx.update(encrypted_data)
182
+ final_data = decx.final
183
+ decrypted_data = data + final_data
184
+ decrypted_data.force_encoding("UTF-8")
185
+ end
186
+
187
+ def make(_template_dir, _target, _sub_target)
188
+ case @file_format
189
+ when "JSON_FILE"
190
+ @content
191
+ when "YAML"
192
+ @content.map do |item|
193
+ %(export #{item[0]}=#{item[1]})
194
+ end.flatten
195
+ else
196
+ ""
197
+ end
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Secretmgr
4
+ VERSION = "0.1.0"
5
+ end
data/lib/secretmgr.rb ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+ require "base64"
5
+ require "multi_json"
6
+ require "ykxutils"
7
+ require "ykutils"
8
+
9
+ require_relative "secretmgr/version"
10
+ require_relative "secretmgr/secretmgr"
11
+ require_relative "secretmgr/config"
12
+
13
+ module Secretmgr
14
+ INSTALLED_APP = "installed"
15
+ WEB_APP = "web"
16
+ CLIENT_ID = "client_id"
17
+ CLIENT_SECRET = "client_secret"
18
+ CIPHER_NAME = "AES-256-CBC"
19
+
20
+ class Error < StandardError; end
21
+ # Your code goes here...
22
+ end
data/rubocoprefine.bat ADDED
@@ -0,0 +1,30 @@
1
+ REMM メソッド定義やメソッド呼び出しの()をいい感じに
2
+ rubocop -a --only Style/DefWithParentheses,\
3
+ Style/MethodCallParentheses,\
4
+ Style/MethodDefParentheses
5
+
6
+ REM インデント崩れを修正
7
+ rubocop -a --only Style/IndentationConsistency,\
8
+ Style/IndentationWidth,\
9
+ Style/MultilineOperationIndentation
10
+
11
+ REM 空行をいい感じに
12
+ rubocop -a --only Style/EmptyLineBetweenDefs,\
13
+ Style/EmptyLines,\
14
+ Style/EmptyLinesAroundAccessModifier,\
15
+ Style/EmptyLinesAroundBlockBody,\
16
+ Style/EmptyLinesAroundClassBody,\
17
+ Style/EmptyLinesAroundMethodBody,\
18
+ Style/EmptyLinesAroundModuleBody,\
19
+ Style/TrailingBlankLines
20
+
21
+ REM コロンやカンマの前後のスペースをいい感じにする
22
+ rubocop -a --only Style/SpaceAfterColon,\
23
+ Style/SpaceAfterComma,\
24
+ Style/SpaceAfterNot,\
25
+ Style/SpaceAfterSemicolon,\
26
+ Style/SpaceAroundEqualsInParameterDefault,\
27
+ Style/SpaceBeforeSemicolon
28
+
29
+ REM 行末のスペース削除
30
+ rubocop -a --only Style/TrailingWhitespace
data/secretmgr.gemspec ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/secretmgr/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "secretmgr"
7
+ spec.version = Secretmgr::VERSION
8
+ spec.authors = ["ykominami"]
9
+ spec.email = ["ykominami@gmail.com"]
10
+
11
+ spec.summary = "manage secret."
12
+ spec.description = "."
13
+ # spec.homepage = "TODO: Put your gem's website or public repo URL here."
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
18
+
19
+ # spec.metadata["homepage_uri"] = spec.homepage
20
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_runtime_dependency "multi_json"
35
+ spec.add_runtime_dependency "rake", "~> 13.0"
36
+ spec.add_runtime_dependency "rspec", "~> 3.0"
37
+ spec.add_runtime_dependency "rubocop", "~> 1.21"
38
+ spec.add_runtime_dependency "ykutils"
39
+ spec.add_runtime_dependency "ykxutils"
40
+
41
+ # spec.add_runtime_dependency ""
42
+ # spec.add_development_dependency "", "~> 1.1.3"
43
+
44
+ # Uncomment to register a new dependency of your gem
45
+ # spec.add_dependency "example-gem", "~> 1.0"
46
+
47
+ # For more information and examples about making a new gem, check out our
48
+ # guide at: https://bundler.io/guides/creating_gem.html
49
+ end
data/sig/secretmgr.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Secretmgr
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: secretmgr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ykominami
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
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: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
69
+ - !ruby/object:Gem::Dependency
70
+ name: ykutils
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ykxutils
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: "."
98
+ email:
99
+ - ykominami@gmail.com
100
+ executables:
101
+ - secretmgr
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".rubocop_todo.yml"
108
+ - CHANGELOG.md
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - exe/secretmgr
116
+ - lib/secretmgr.rb
117
+ - lib/secretmgr/config.rb
118
+ - lib/secretmgr/secretmgr.rb
119
+ - lib/secretmgr/version.rb
120
+ - rubocoprefine.bat
121
+ - secretmgr.gemspec
122
+ - sig/secretmgr.rbs
123
+ homepage:
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: 2.6.0
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.4.13
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: manage secret.
146
+ test_files: []