momoka 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c0a179d6d7e406cbb7d520dc8561f6727d94a905da518117947796769bf0b1ca
4
+ data.tar.gz: 8d13ab926764a0c0e22b45018ab226dbc2a9f1833b070a891c6240582a8df067
5
+ SHA512:
6
+ metadata.gz: 04c68aac745f477360d5940002b922eb59edc1dca30916326545c55e3cdd07b20b20151f7067a7f1759fb99d0a0e2158065d58bd953e49633f0b53d8bfc206d8
7
+ data.tar.gz: 19a4ac13842e2c1b8a784631e47f36e56e03b43bc462a042cf5a1050e963c59811a8578dc3d438bdac32d6ddbfb21cb7460fa30d745d09ee7f9967ea5bfaccb1
data/.rubocop.yml ADDED
@@ -0,0 +1,42 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Metrics/AbcSize:
5
+ Max: 170
6
+
7
+ Metrics/BlockLength:
8
+ Max: 250
9
+
10
+ Metrics/ClassLength:
11
+ Max: 1000
12
+
13
+ Metrics/CyclomaticComplexity:
14
+ Max: 70
15
+
16
+ Metrics/MethodLength:
17
+ Max: 100
18
+
19
+ Metrics/ModuleLength:
20
+ Max: 1000
21
+
22
+ Metrics/ParameterLists:
23
+ Max: 50
24
+
25
+ Metrics/PerceivedComplexity:
26
+ Max: 80
27
+
28
+ Layout/SpaceInsideBlockBraces:
29
+ EnforcedStyle: no_space
30
+ SpaceBeforeBlockParameters: false
31
+
32
+ Style/Documentation:
33
+ Enabled: false
34
+
35
+ Style/NumericPredicate:
36
+ EnforcedStyle: comparison
37
+
38
+ Style/MultilineBlockChain:
39
+ Enabled: false
40
+
41
+ Style/SpecialGlobalVars:
42
+ EnforcedStyle: use_perl_names
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in momoka.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Ishotihadus
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.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # Momoka
2
+
3
+ 白薔薇のように無垢なドレス…普段のレディなわたくしより、少し幼く見えるかもしれません。ですがこの装いでこそ引き立つ表情もありますわ。
4
+
5
+ Encrypted dotenv file manager.
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add momoka
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install momoka
16
+
17
+ ## Usage
18
+
19
+ ### 1. Initialize
20
+
21
+ Generate a new key file `.momoka`.
22
+
23
+ ```sh
24
+ $ momoka-cli init
25
+ ```
26
+
27
+ By this command, a new key file `.momoka` is generated like:
28
+
29
+ ```
30
+ i7rxNWSlteGzfBK5WhVkt9Jy+Zgk+TsPnNAhvB2VGCj89dcXWYg5+ibLTpWv5IYV
31
+ ```
32
+
33
+ ### 2. Encrypt
34
+
35
+ Encrypt text from stdin using `.momoka`.
36
+
37
+ ```sh
38
+ $ echo 'sakurai momoka' | momoka-cli encrypt
39
+ # => :momoka:hLUcqXF5Td3ZR17xWHjIAQ==:
40
+ ```
41
+
42
+ Tips: If you use macOS, `pbcopy` command is useful.
43
+
44
+ ```sh
45
+ $ echo 'sakurai momoka' | momoka-cli encrypt | pbcopy
46
+ ```
47
+
48
+ ### 3. Write encrypted text to .env file
49
+
50
+ ```
51
+ TEST=:momoka:hLUcqXF5Td3ZR17xWHjIAQ==:
52
+ ```
53
+
54
+ ### 4. Load from ruby
55
+
56
+ ```rb
57
+ require 'momoka'
58
+
59
+ Momoka.load
60
+ ```
61
+
62
+ ### encenv mode
63
+
64
+ You can directly encrypt an env file.
65
+
66
+ ```sh
67
+ $ bundle exec momoka-cli encenv >> .env <<EOF
68
+ MOMOKA=sakurai momoka
69
+ ARISU=tachibana arisu
70
+ EOF
71
+ ```
72
+
73
+ ### Using environment variable
74
+
75
+ Momoka uses the `MOMOKA_KEY` environment variable, if it exists.
76
+
77
+ ```sh
78
+ $ MOMOKA_KEY=i7rxNWSlteGzfBK5WhVkt9Jy+Zgk+TsPnNAhvB2VGCj89dcXWYg5+ibLTpWv5IYV bundle exec momoka-cli decrypt
79
+ ```
80
+
81
+ ### Momoka command
82
+
83
+ Command `momoka` is a similar command to `dotenv` command.
84
+
85
+ ```sh
86
+ $ bundle exec momoka 'echo $TEST'
87
+ # => sakurai momoka
88
+ ```
89
+
90
+ ## License
91
+
92
+ This gem is licensed under the MIT License.
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
data/exe/momoka ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'momoka/cli'
4
+
5
+ Momoka::CLI.new(ARGV).run
data/exe/momoka-cli ADDED
@@ -0,0 +1,51 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'dotenv/parser'
4
+ require 'momoka'
5
+ require 'securerandom'
6
+ require 'yaml'
7
+
8
+ case ARGV[0]
9
+ when 'init'
10
+ if File.exist?('./.momoka')
11
+ warn '.momoka already exists!'
12
+ exit 1
13
+ end
14
+
15
+ key = SecureRandom.random_bytes(48)
16
+ File.write('./.momoka', "#{Base64.strict_encode64(key)}\n")
17
+
18
+ if File.file?('.gitignore')
19
+ gitignore = File.read('.gitignore')
20
+ File.open('.gitignore', 'a') {|f| f.puts "\n.momoka"} unless gitignore =~ (/^.momoka$/)
21
+ end
22
+ when 'encrypt'
23
+ text = $stdin.read.sub(/\n$/, '')
24
+ puts Momoka.encrypt(text, key_file: ARGV[1])
25
+ when 'encenv'
26
+ text = $stdin.read.sub(/\n$/, '')
27
+ hash = Dotenv::Parser.new(text).call
28
+ key_file = ARGV[2]
29
+
30
+ hash.transform_values! do |value|
31
+ Momoka.encrypt(value, key_file: key_file)
32
+ end
33
+
34
+ puts hash.map {|k, v| "#{k}=#{v}"}.join("\n")
35
+ when 'decrypt'
36
+ puts Momoka.load(ARGV[1] || '.env', key_file: ARGV[2]).to_yaml
37
+ else
38
+ warn 'usage: momoka-cli init|encrypt|decrypt'
39
+ warn ''
40
+ warn ' init - generate a new key file and save it to .momoka'
41
+ warn ' usage: momoka-cli init'
42
+ warn ''
43
+ warn ' encrypt - encrypt a string'
44
+ warn ' usage: momoka-cli encrypt [key_file] < [text]'
45
+ warn ''
46
+ warn ' encenv - encrypt a .env file'
47
+ warn ' usage: momoka-cli encenv [key_file] < [env file]'
48
+ warn ''
49
+ warn ' decrypt - decrypt .env file'
50
+ warn ' usage: momoka-cli decrypt [.env file] [key_file]'
51
+ end
data/lib/momoka/cli.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'momoka'
4
+ require 'dotenv/cli'
5
+
6
+ module Momoka
7
+ class CLI < Dotenv::CLI
8
+ def run
9
+ if @overload
10
+ Momoka.overload!(*@filenames)
11
+ else
12
+ Momoka.load!(*@filenames)
13
+ end
14
+ rescue Errno::ENOENT => e
15
+ abort e.message
16
+ else
17
+ exec(*@argv) unless @argv.empty?
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dotenv'
4
+
5
+ module Dotenv
6
+ def self.parse!(*filenames)
7
+ with(*filenames) do |f|
8
+ Environment.new(f, false)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'momoka'
4
+
5
+ Momoka.load
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Momoka
4
+ VERSION = '1.0.0'
5
+ end
data/lib/momoka.rb ADDED
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'dotenv'
5
+ require 'openssl'
6
+ require_relative 'momoka/dotenv_inject'
7
+ require_relative 'momoka/version'
8
+
9
+ module Momoka
10
+ class << self
11
+ def load(*filenames, key_file: nil)
12
+ env = Dotenv.parse(*filenames)
13
+ decrypt!(env, key_file: key_file)
14
+ env.each {|k, v| ENV[k] ||= v}
15
+ end
16
+
17
+ def load!(*filenames, key_file: nil)
18
+ env = Dotenv.parse!(*filenames)
19
+ decrypt!(env, key_file: key_file)
20
+ env.each {|k, v| ENV[k] ||= v}
21
+ end
22
+
23
+ def overload(*filenames, key_file: nil)
24
+ env = Dotenv.parse(*filenames)
25
+ decrypt!(env, key_file: key_file)
26
+ env.each {|k, v| ENV[k] = v}
27
+ end
28
+
29
+ def overload!(*filenames, key_file: nil)
30
+ env = Dotenv.parse!(*filenames)
31
+ decrypt!(env, key_file: key_file)
32
+ env.each {|k, v| ENV[k] = v}
33
+ end
34
+
35
+ def encrypt(text, key_file: nil)
36
+ key = default_key(key_file: key_file)
37
+
38
+ enc = OpenSSL::Cipher.new('AES-256-CBC').encrypt
39
+ enc.key = key.byteslice(0, 32)
40
+ enc.iv = key.byteslice(32, 16)
41
+
42
+ encrypted = Base64.strict_encode64(enc.update(text) + enc.final)
43
+ ":momoka:#{encrypted}:"
44
+ end
45
+
46
+ private
47
+
48
+ def default_key(key_file: nil)
49
+ key =
50
+ if key_file
51
+ File.read(key_file)
52
+ elsif ENV.key?('MOMOKA_KEY')
53
+ ENV['MOMOKA_KEY']
54
+ else
55
+ raise 'Neither .momoka file nor MOMOKA_KEY environment variable exists' unless File.exist?('./.momoka')
56
+
57
+ File.read('./.momoka')
58
+ end
59
+
60
+ key = Base64.decode64(key)
61
+ raise 'invalid key' unless key.bytesize >= 48
62
+
63
+ key
64
+ end
65
+
66
+ def decrypt!(env, key_file: nil)
67
+ key = default_key(key_file: key_file)
68
+
69
+ dec = OpenSSL::Cipher.new('AES-256-CBC').decrypt
70
+ dec.key = key.byteslice(0, 32)
71
+ dec.iv = key.byteslice(32, 16)
72
+
73
+ env.transform_values! do |v|
74
+ next v unless v.size >= 10 && v[0, 8] == ':momoka:' && v[-1] == ':'
75
+
76
+ _empty, _momoka, data = v.split(':')
77
+ data = Base64.decode64(data)
78
+ dec.reset
79
+ dec.update(data) + dec.final
80
+ end
81
+ end
82
+ end
83
+ end
data/momoka.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/momoka/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'momoka'
7
+ spec.version = Momoka::VERSION
8
+ spec.authors = ['Ishotihadus']
9
+ spec.email = ['hanachan.pao@gmail.com']
10
+
11
+ spec.summary = 'Dotenv encryptor with AES-256-CBC'
12
+ spec.description = 'Dotenv encryptor with AES-256-CBC'
13
+ spec.homepage = 'https://github.com/Ishotihadus/momoka'
14
+ spec.required_ruby_version = '>= 2.6.0'
15
+
16
+ spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['source_code_uri'] = spec.homepage
18
+
19
+ spec.files = Dir.chdir(__dir__) do
20
+ `git ls-files -z`.split("\x0").reject do |f|
21
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
22
+ end
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{\Aexe/}) {|f| File.basename(f)}
26
+ spec.require_paths = ['lib']
27
+
28
+ spec.add_dependency 'dotenv'
29
+ spec.add_dependency 'openssl'
30
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: momoka
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ishotihadus
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
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: openssl
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
+ description: Dotenv encryptor with AES-256-CBC
42
+ email:
43
+ - hanachan.pao@gmail.com
44
+ executables:
45
+ - momoka
46
+ - momoka-cli
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".rubocop.yml"
51
+ - Gemfile
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - exe/momoka
56
+ - exe/momoka-cli
57
+ - lib/momoka.rb
58
+ - lib/momoka/cli.rb
59
+ - lib/momoka/dotenv_inject.rb
60
+ - lib/momoka/load.rb
61
+ - lib/momoka/version.rb
62
+ - momoka.gemspec
63
+ homepage: https://github.com/Ishotihadus/momoka
64
+ licenses: []
65
+ metadata:
66
+ homepage_uri: https://github.com/Ishotihadus/momoka
67
+ source_code_uri: https://github.com/Ishotihadus/momoka
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 2.6.0
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubygems_version: 3.4.0.dev
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Dotenv encryptor with AES-256-CBC
87
+ test_files: []