encrypt_env 1.3.0 → 1.3.3

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: 1d5d82936ba2b4cf68e7ca1426185f039af068154afa4cd2b92261466b3089cf
4
- data.tar.gz: 81dfa2bc7beec3284a4ad93318d4ab637cad10cc68b19d7edc6600abb480712b
3
+ metadata.gz: da8b5db8cc944d9f2ec3f293efa316cb67dc54e56695ffef220303f9ceb566d5
4
+ data.tar.gz: 5baa2602d7d3e8b780d45c4c080b4ca49177f10d205072e72e859c15bb795f21
5
5
  SHA512:
6
- metadata.gz: c9adcf613352abd8834f2268eeb5476ee7842ce2249f7a3d255f848e7132b02a283d0e74cca5fa7792957700ef838121726e48e15e27ab5fb5bbbd5addf92407
7
- data.tar.gz: 4a603f84d80ab605a1fd7a1baa04772dd4a14e0f08c0d137e03eb7f307ac6490c113d5120645339cf6d909750fbb99775963dd322a351b6b586ec8b25bb2863d
6
+ metadata.gz: dc119a9d8343930f9ba286d88542084692cb6f4d3f820fc7c82e64f61b6d80a5fbf8216604635e14104118c58b23d0e7ad1ef49e20f15fb0732339010ca0e3d9
7
+ data.tar.gz: 48eee986594c2327f0cb38ebaf52c8362c881213bbe91e5fc9b6ae247d7a677c81f608ff3b9d4f0bec34430a4438369f22415e1956aabf8308a8a0e87f1c4d6b
data/bin/encrypt_env CHANGED
@@ -3,6 +3,7 @@
3
3
  # frozen_string_literal: true
4
4
 
5
5
  require 'encrypt_env'
6
+ require_relative '../lib/encrypt_env/version'
6
7
 
7
8
  argv = ARGV
8
9
  action = argv.shift
@@ -47,17 +48,26 @@ elsif action == 'delete'
47
48
  env = argv[1]
48
49
  EncryptEnv.delete_variable(key, env)
49
50
  exit 0
51
+ elsif ['-v', '--version', 'version'].include?(action)
52
+ puts Version::VERSION
53
+ exit 0
50
54
  elsif ['help', '--help', '-h'].include?(action)
51
55
  puts <<~HELP
52
56
  Usage:
53
- encrypt_env setup
54
- encrypt_env secrets
55
- encrypt_env secrets_all
56
- encrypt_env edit
57
+ encrypt_env setup # To setup for the firt time
58
+ encrypt_env show all # To show all environment variables
59
+ encrypt_env show / encrypt_env show [environment] # To show environment variables
60
+ encrypt_env edit / encrypt_env edit [environment] # To edit environment variables
61
+ encrypt_env add variable_name [environment] # To add environment variable
62
+ encrypt_env get variable_name [environment] # To show value of specific variable
63
+ encrypt_env update variable_name [environment] # To edit value of specific variable
64
+ encrypt_env delete variable_name [environment] # To delete specific variable
65
+ Or: Visit "https://github.com/nnhutan/encrypt_env" for more information
57
66
  HELP
58
67
 
59
68
  exit 0
60
69
  else
61
70
  puts "Unknown action: #{action}"
71
+ puts "Use 'encrypt_env -h' for more help"
62
72
  exit 1
63
73
  end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Version
4
+ VERSION = '1.3.3'
5
+ end
data/lib/encrypt_env.rb CHANGED
@@ -11,6 +11,8 @@ require 'json'
11
11
  # rubocop:disable Metrics/ClassLength
12
12
  # rubocop:disable Metrics/MethodLength
13
13
  class EncryptEnv
14
+ @root_path = Dir.pwd
15
+
14
16
  private_class_method def self.define_option
15
17
  puts "Options to 'encrypt secrets.yml' file"
16
18
  puts '1. Generate only one master.key and one encrypted file for all environment'
@@ -27,9 +29,9 @@ class EncryptEnv
27
29
  end
28
30
 
29
31
  private_class_method def self.load_curr_opt
30
- if File.file?("#{Dir.pwd}/config/secrets.yml.enc")
32
+ if File.file?("#{@root_path}/config/secrets.yml.enc")
31
33
  @opt = 1
32
- elsif Dir["#{Dir.pwd}/config/secrets_*.yml.enc"].length.positive?
34
+ elsif Dir["#{@root_path}/config/secrets_*.yml.enc"].length.positive?
33
35
  @opt = 2
34
36
  else
35
37
  raise 'You must setup first to encrypt file!'
@@ -46,7 +48,7 @@ class EncryptEnv
46
48
 
47
49
  private_class_method def self.check_key_existence(env = nil)
48
50
  file_name = env.nil? ? 'master.key' : "master_#{env}.key"
49
- return if File.file?("#{Dir.pwd}/config/#{file_name}")
51
+ return if File.file?("#{@root_path}/config/#{file_name}")
50
52
  return if ENV.key?('MASTER_KEY')
51
53
 
52
54
  message = env ? "Missing key of #{env} environment!" : 'Missing master key!'
@@ -60,7 +62,7 @@ class EncryptEnv
60
62
  raise e.message
61
63
  end
62
64
 
63
- file_path = env ? "#{Dir.pwd}/config/master_#{env}.key" : "#{Dir.pwd}/config/master.key"
65
+ file_path = env ? "#{@root_path}/config/master_#{env}.key" : "#{@root_path}/config/master.key"
64
66
  key = File.file?(file_path) ? File.read(file_path).strip : ENV['MASTER_KEY']
65
67
  @master_key = [key].pack('H*')
66
68
  end
@@ -68,19 +70,19 @@ class EncryptEnv
68
70
  private_class_method def self.generate_keys
69
71
  if @opt == 1
70
72
  key = OpenSSL::Random.random_bytes(16)
71
- File.open("#{Dir.pwd}/config/master.key", 'w') { |file| file.write(key.unpack('H*')[0]) }
73
+ File.open("#{@root_path}/config/master.key", 'w') { |file| file.write(key.unpack('H*')[0]) }
72
74
  else
73
75
  to_hash_type(@content_to_encrypt).each_key do |env|
74
76
  next if env == 'default'
75
77
 
76
78
  key = OpenSSL::Random.random_bytes(16)
77
- File.open("#{Dir.pwd}/config/master_#{env}.key", 'w') { |file| file.write(key.unpack('H*')[0]) }
79
+ File.open("#{@root_path}/config/master_#{env}.key", 'w') { |file| file.write(key.unpack('H*')[0]) }
78
80
  end
79
81
  end
80
82
  end
81
83
 
82
84
  private_class_method def self.load_content_to_encrypt
83
- secret_file = File.expand_path("#{Dir.pwd}/config/secrets.yml")
85
+ secret_file = File.expand_path("#{@root_path}/config/secrets.yml")
84
86
  @content_to_encrypt = File.read(secret_file)
85
87
  end
86
88
 
@@ -89,7 +91,7 @@ class EncryptEnv
89
91
  end
90
92
 
91
93
  private_class_method def self.load_encrypted_data(env = nil)
92
- file_path = env ? "#{Dir.pwd}/config/secrets_#{env}.yml.enc" : "#{Dir.pwd}/config/secrets.yml.enc"
94
+ file_path = env ? "#{@root_path}/config/secrets_#{env}.yml.enc" : "#{@root_path}/config/secrets.yml.enc"
93
95
  hex_string = File.read(file_path)
94
96
  raw_data = [hex_string].pack('H*')
95
97
 
@@ -100,7 +102,7 @@ class EncryptEnv
100
102
  end
101
103
 
102
104
  private_class_method def self.encrypt(content, typ = nil)
103
- file_path = typ ? "#{Dir.pwd}/config/secrets_#{typ}.yml.enc" : "#{Dir.pwd}/config/secrets.yml.enc"
105
+ file_path = typ ? "#{@root_path}/config/secrets_#{typ}.yml.enc" : "#{@root_path}/config/secrets.yml.enc"
104
106
  cipher = OpenSSL::Cipher.new('aes-128-gcm')
105
107
  cipher.encrypt
106
108
  cipher.key = @master_key
@@ -138,7 +140,7 @@ class EncryptEnv
138
140
 
139
141
  private_class_method def self.all_decrypted_object
140
142
  obj = {}
141
- env_lst = Dir["#{Dir.pwd}/config/secrets_*.yml.enc"].map do |path|
143
+ env_lst = Dir["#{@root_path}/config/secrets_*.yml.enc"].map do |path|
142
144
  path.scan(/secrets_(.*)\.yml\.enc/).flatten.first
143
145
  end
144
146
  env_lst.each do |e|
@@ -189,9 +191,9 @@ class EncryptEnv
189
191
  end
190
192
  end
191
193
 
192
- File.rename("#{Dir.pwd}/config/secrets.yml", "#{Dir.pwd}/config/secrets.yml.old")
193
- system("echo '/config/master*.key' >> #{Dir.pwd}/.gitignore")
194
- system("echo '/config/secrets.yml.old' >> #{Dir.pwd}/.gitignore")
194
+ File.rename("#{@root_path}/config/secrets.yml", "#{@root_path}/config/secrets.yml.old")
195
+ system("echo '/config/master*.key' >> #{@root_path}/.gitignore")
196
+ system("echo '/config/secrets.yml.old' >> #{@root_path}/.gitignore")
195
197
  system("echo 'Set up complete!'")
196
198
  end
197
199
 
@@ -213,18 +215,19 @@ class EncryptEnv
213
215
  end
214
216
 
215
217
  def self.show(env = nil)
216
- # require "awesome_print"
218
+ require 'awesome_print'
219
+ require 'date'
217
220
  value = secrets(env)
218
- # ap({})
219
- # ap(value) unless @have_error
220
- jj value unless @have_error
221
+ ap(value) unless @have_error
222
+ # jj value unless @have_error
221
223
  @have_error = false
222
224
  end
223
225
 
224
226
  def self.valueof(key, env = nil)
227
+ tail_msg = env ? " in '#{env}' environent" : nil
225
228
  value = secrets(env)
226
229
  unless value.key?(key)
227
- puts "key '#{key}' does not exist!"
230
+ puts "key '#{key}' does not exist#{tail_msg}!"
228
231
  return
229
232
  end
230
233
  puts value[key]
@@ -237,8 +240,8 @@ class EncryptEnv
237
240
  return
238
241
  end
239
242
 
240
- tail_confirm = env ? " in '#{env}' environent" : nil
241
- confirm = "Really? You want to delete '#{key}'#{tail_confirm}? (y/n)"
243
+ tail_msg = env ? " in '#{env}' environent" : nil
244
+ confirm = "Really? You want to delete '#{key}'#{tail_msg}? (y/n)"
242
245
  puts confirm
243
246
  a = $stdin.gets.chomp
244
247
  return unless a == 'y'
@@ -246,13 +249,13 @@ class EncryptEnv
246
249
  value = secrets(env)
247
250
 
248
251
  unless value.key?(key)
249
- puts "#{key} does not exist!"
252
+ puts "#{key} does not exist#{tail_msg}!"
250
253
  return
251
254
  end
252
255
 
253
256
  value.delete(key)
254
257
  encrypt(value.to_hash.to_yaml, env || current_env)
255
- puts "delete '#{key}' successfully!"
258
+ puts "Delete '#{key}' successfully!"
256
259
  end
257
260
 
258
261
  def self.update_variable(key, env = nil, add_variable = false)
@@ -261,20 +264,20 @@ class EncryptEnv
261
264
  puts 'Only for option 2!'
262
265
  return
263
266
  end
267
+ tail_msg = env ? " in '#{env}' environment" : nil
264
268
 
265
269
  value = secrets(env)
266
270
  if add_variable && value.key?(key)
267
- puts 'Key existed!'
271
+ puts "Key existed#{tail_msg}!"
268
272
  return
269
273
  end
270
274
 
271
275
  if !value.key?(key) && !add_variable
272
- tail_msg = env ? " in #{env} environment" : nil
273
276
  puts "'#{key}' does not exist#{tail_msg}. You want to add '#{key}' as the new key? (y/n)"
274
277
  a = $stdin.gets.chomp
275
278
  return unless a == 'y'
276
279
 
277
- add_variable = false
280
+ add_variable = true
278
281
  end
279
282
 
280
283
  action = add_variable && 'add' || 'edit'
@@ -285,11 +288,15 @@ class EncryptEnv
285
288
  f.flush
286
289
  f.rewind
287
290
  system("vim #{f.path}")
288
- new_value = File.read(f.path)
289
- value[key] = new_value.strip
291
+ # new_value = File.read(f.path)
292
+ new_value = YAML.load_file(f.path)
293
+ # value[key] = new_value.strip
294
+ value[key] = new_value
290
295
  encrypt(value.to_hash.to_yaml, env || current_env)
291
296
  @decrypted = nil
292
297
  end
298
+
299
+ puts "#{key}\t=>\t#{value[key]}"
293
300
  end
294
301
  end
295
302
  # rubocop:enable Metrics/ClassLength
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.0
4
+ version: 1.3.3
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-11 00:00:00.000000000 Z
11
+ date: 2022-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -39,6 +39,7 @@ extra_rdoc_files: []
39
39
  files:
40
40
  - bin/encrypt_env
41
41
  - lib/encrypt_env.rb
42
+ - lib/encrypt_env/version.rb
42
43
  homepage: https://github.com/nnhutan/encrypt_env.git
43
44
  licenses:
44
45
  - MIT