encrypt_env 1.3.6 → 1.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58a9ebf47dac3a056a4ec7d890463591f8ed9f0a749597cf8aa388b2359cc7bd
4
- data.tar.gz: 607a5c85ae34aa79a60983919f7d1c972769417bbd0ab59d9e2e1cfc19a791b7
3
+ metadata.gz: 3c2be77fd3504dd29034cbfc8c8fd64b337357bbb297f5510faf53805d46d8d2
4
+ data.tar.gz: a21a92c449fb73c43e66907837428026674b55d784242e8946e301727a0a0340
5
5
  SHA512:
6
- metadata.gz: ad618c7bd4b93b11f0eda7ad786d363b41bd4d1c9498426931f04e5859e3e4f6f9d6e7aabbd3f15d90ccb3a23e8cfdfe614e68fc63a4e013d5c2e54434212aac
7
- data.tar.gz: 41c5be08c7ee2c47b147564abeb12b83906f876f23efe50e22b0717e124cf3a30a78da1b9025859ded45a77771df6434324192d052d4dd83274e72a5efd5ab3f
6
+ metadata.gz: a3419ab5edeff49a4f40bd559f6da219630ebf8c7c3c44aa7ea5cf612a3bbb8e7223ef1bfb4c018a4ad21de1c7d83667f51de33c3275c3f3908035edbe975a3d
7
+ data.tar.gz: c2e59193da968fadaa428171b18844f58d2f80f966e779908cd28bb267cb22bc6d2b1b6409baf88f426f6dd5573837226dd9c8997188c87b8c5e1aeda42b0603
data/bin/encrypt_env CHANGED
@@ -7,11 +7,23 @@ require 'optparse'
7
7
  require_relative '../lib/encrypt_env/version'
8
8
 
9
9
  options = {}
10
- parsers = OptionParser.new do |parser|
11
- parser.on('-e', '--environment ENVIRONMENT', 'environment')
12
- parser.on('-s', '--set VALUE', 'value')
13
- parser.on('-t', '--type TYPE', 'type of variable')
14
- parser.on('-a', '--all', 'show all')
10
+ OptionParser.new do |parser|
11
+ parser.on('-e', '--environment ENVIRONMENT', 'environment') do |opts|
12
+ options[:environment] = opts
13
+ end
14
+
15
+ parser.on('-s', '--set VALUE', 'value') do |opts|
16
+ options[:set] = opts
17
+ end
18
+
19
+ parser.on('-t', '--type TYPE', 'type of variable') do |opts|
20
+ options[:type] = opts
21
+ end
22
+
23
+ parser.on('-a', '--all', 'show all') do |_opts|
24
+ options[:all] = true
25
+ end
26
+
15
27
  parser.on('-v', '--version', 'version') do
16
28
  puts Version::VERSION
17
29
  exit
@@ -43,9 +55,9 @@ parsers = OptionParser.new do |parser|
43
55
  HELP
44
56
  exit
45
57
  end
46
- end
58
+ end.parse!
47
59
 
48
- parsers.parse!(into: options)
60
+ # parsers.parse!
49
61
 
50
62
  argv = ARGV
51
63
  action = argv.shift
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Version
4
- VERSION = '1.3.6'
4
+ VERSION = '1.4.1'
5
5
  end
data/lib/encrypt_env.rb CHANGED
@@ -6,6 +6,7 @@ require 'yaml'
6
6
  require 'active_support/core_ext/hash/indifferent_access'
7
7
  require 'tempfile'
8
8
  require 'json'
9
+ require 'active_support/ordered_options'
9
10
 
10
11
  # gem 'encrypt_env'
11
12
  # rubocop:disable Metrics/ClassLength
@@ -19,7 +20,7 @@ class EncryptEnv
19
20
  puts '2. Generate master.key and encrypted file for each environment'
20
21
 
21
22
  loop do
22
- @opt = gets.chomp.to_i
23
+ @opt = $stdin.gets.chomp.to_i
23
24
  break if @opt == 1 || @opt == 2
24
25
 
25
26
  puts "Please enter '1' or '2'!"
@@ -29,9 +30,9 @@ class EncryptEnv
29
30
  end
30
31
 
31
32
  private_class_method def self.load_curr_opt
32
- if File.file?("#{@root_path}/config/secrets.yml.enc")
33
+ if File.file?("#{@root_path}/config/encrypt_enc/secrets.yml.enc")
33
34
  @opt = 1
34
- elsif Dir["#{@root_path}/config/secrets_*.yml.enc"].length.positive?
35
+ elsif Dir["#{@root_path}/config/encrypt_enc/secrets_*.yml.enc"].length.positive?
35
36
  @opt = 2
36
37
  else
37
38
  raise 'You must setup first to encrypt file!'
@@ -48,7 +49,7 @@ class EncryptEnv
48
49
 
49
50
  private_class_method def self.check_key_existence(env = nil)
50
51
  file_name = env.nil? ? 'master.key' : "master_#{env}.key"
51
- return if File.file?("#{@root_path}/config/#{file_name}")
52
+ return if File.file?("#{@root_path}/config/master_key/#{file_name}")
52
53
  return if ENV.key?('MASTER_KEY')
53
54
 
54
55
  message = env ? "Missing key of #{env} environment!" : 'Missing master key!'
@@ -62,7 +63,7 @@ class EncryptEnv
62
63
  raise e.message
63
64
  end
64
65
 
65
- file_path = env ? "#{@root_path}/config/master_#{env}.key" : "#{@root_path}/config/master.key"
66
+ file_path = env ? "#{@root_path}/config/master_key/master_#{env}.key" : "#{@root_path}/config/master_key/master.key"
66
67
  key = File.file?(file_path) ? File.read(file_path).strip : ENV['MASTER_KEY']
67
68
  @master_key = [key].pack('H*')
68
69
  end
@@ -70,13 +71,13 @@ class EncryptEnv
70
71
  private_class_method def self.generate_keys
71
72
  if @opt == 1
72
73
  key = OpenSSL::Random.random_bytes(16)
73
- File.open("#{@root_path}/config/master.key", 'w') { |file| file.write(key.unpack('H*')[0]) }
74
+ File.open("#{@root_path}/config/master_key/master.key", 'w') { |file| file.write(key.unpack('H*')[0]) }
74
75
  else
75
76
  to_hash_type(@content_to_encrypt).each_key do |env|
76
77
  next if env == 'default'
77
78
 
78
79
  key = OpenSSL::Random.random_bytes(16)
79
- File.open("#{@root_path}/config/master_#{env}.key", 'w') { |file| file.write(key.unpack('H*')[0]) }
80
+ File.open("#{@root_path}/config/master_key/master_#{env}.key", 'w') { |file| file.write(key.unpack('H*')[0]) }
80
81
  end
81
82
  end
82
83
  end
@@ -91,7 +92,7 @@ class EncryptEnv
91
92
  end
92
93
 
93
94
  private_class_method def self.load_encrypted_data(env = nil)
94
- file_path = env ? "#{@root_path}/config/secrets_#{env}.yml.enc" : "#{@root_path}/config/secrets.yml.enc"
95
+ file_path = env ? "#{@root_path}/config/encrypt_enc/secrets_#{env}.yml.enc" : "#{@root_path}/config/encrypt_enc/secrets.yml.enc"
95
96
  hex_string = File.read(file_path)
96
97
  raw_data = [hex_string].pack('H*')
97
98
 
@@ -102,7 +103,7 @@ class EncryptEnv
102
103
  end
103
104
 
104
105
  private_class_method def self.encrypt(content, typ = nil)
105
- file_path = typ ? "#{@root_path}/config/secrets_#{typ}.yml.enc" : "#{@root_path}/config/secrets.yml.enc"
106
+ file_path = typ ? "#{@root_path}/config/encrypt_enc/secrets_#{typ}.yml.enc" : "#{@root_path}/config/encrypt_enc/secrets.yml.enc"
106
107
  cipher = OpenSSL::Cipher.new('aes-128-gcm')
107
108
  cipher.encrypt
108
109
  cipher.key = @master_key
@@ -140,7 +141,7 @@ class EncryptEnv
140
141
 
141
142
  private_class_method def self.all_decrypted_object
142
143
  obj = {}
143
- env_lst = Dir["#{@root_path}/config/secrets_*.yml.enc"].map do |path|
144
+ env_lst = Dir["#{@root_path}/config/encrypt_enc/secrets_*.yml.enc"].map do |path|
144
145
  path.scan(/secrets_(.*)\.yml\.enc/).flatten.first
145
146
  end
146
147
  env_lst.each do |e|
@@ -157,7 +158,7 @@ class EncryptEnv
157
158
  @decrypted
158
159
  end
159
160
 
160
- def self.secrets(env = nil)
161
+ def self.hash_secrets(env = nil)
161
162
  load_curr_opt unless @opt
162
163
  if env == 'all'
163
164
  result = secrets_all
@@ -181,6 +182,8 @@ class EncryptEnv
181
182
  def self.setup
182
183
  define_option
183
184
  load_content_to_encrypt
185
+ system("mkdir -p #{@root_path}/config/master_key")
186
+ system("mkdir -p #{@root_path}/config/encrypt_enc")
184
187
  generate_keys
185
188
 
186
189
  if @opt == 1
@@ -196,7 +199,7 @@ class EncryptEnv
196
199
  end
197
200
 
198
201
  File.rename("#{@root_path}/config/secrets.yml", "#{@root_path}/config/secrets.yml.old")
199
- system("echo '/config/master*.key' >> #{@root_path}/.gitignore")
202
+ system("echo '/config/master_key/master*.key' >> #{@root_path}/.gitignore")
200
203
  system("echo '/config/secrets.yml.old' >> #{@root_path}/.gitignore")
201
204
  system("echo 'Set up complete!'")
202
205
  end
@@ -225,7 +228,7 @@ class EncryptEnv
225
228
 
226
229
  require 'awesome_print'
227
230
  require 'date'
228
- value = secrets(env)
231
+ value = hash_secrets(env)
229
232
  ap(value) unless @have_error
230
233
  # jj value unless @have_error
231
234
  @have_error = false
@@ -233,7 +236,7 @@ class EncryptEnv
233
236
 
234
237
  def self.valueof(key, env = nil)
235
238
  tail_msg = env ? " in '#{env}' environent" : nil
236
- value = secrets(env)
239
+ value = hash_secrets(env)
237
240
  unless value.key?(key)
238
241
  puts "key '#{key}' does not exist#{tail_msg}!"
239
242
  return
@@ -254,7 +257,7 @@ class EncryptEnv
254
257
  a = $stdin.gets.chomp
255
258
  return unless a == 'y'
256
259
 
257
- value = secrets(env)
260
+ value = hash_secrets(env)
258
261
 
259
262
  unless value.key?(key)
260
263
  puts "#{key} does not exist#{tail_msg}!"
@@ -322,7 +325,7 @@ class EncryptEnv
322
325
 
323
326
  tail_msg = env ? " in '#{env}' environment" : nil
324
327
 
325
- value = secrets(env)
328
+ value = hash_secrets(env)
326
329
 
327
330
  if value.key?(key)
328
331
  puts "Key existed#{tail_msg}!"
@@ -343,7 +346,7 @@ class EncryptEnv
343
346
  end
344
347
  tail_msg = env ? " in '#{env}' environment" : nil
345
348
 
346
- value = secrets(env)
349
+ value = hash_secrets(env)
347
350
 
348
351
  if !is_edit && value.key?(key)
349
352
  puts "Key existed#{tail_msg}!"
@@ -377,12 +380,12 @@ class EncryptEnv
377
380
  puts "#{key}\t=>\t#{value[key]}"
378
381
  end
379
382
 
380
- # def self.get
381
- # self
382
- # end
383
+ def self.secrets
384
+ ActiveSupport::OrderedOptions[hash_secrets.deep_symbolize_keys]
385
+ end
383
386
 
384
387
  def self.method_missing(key, *_args)
385
- secrets unless @decrypted
388
+ hash_secrets unless @decrypted
386
389
  @decrypted[key]
387
390
  end
388
391
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: encrypt_env
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.6
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nhu Tan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-16 00:00:00.000000000 Z
11
+ date: 2022-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print