encrypt_env 1.3.1 → 1.3.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.
- checksums.yaml +4 -4
- data/bin/encrypt_env +14 -4
- data/lib/encrypt_env/version.rb +5 -0
- data/lib/encrypt_env.rb +28 -25
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1c84dbd7de0bdce6a21e412fbadf6652fa6828283e034478408f0d1ac9e7a26
|
4
|
+
data.tar.gz: 8b40b35df40234c55fc6210bbb720564b3a342195636d2b3e8c4b17d0bb38fd7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 871c3408a490cdc32e486eff60e787961c1915aa65fc3d7bba6cd10b15b3439b0a9d4b941abf0459427019ca0736bec381c567958c88c86d5542f35e63880cce
|
7
|
+
data.tar.gz: 0bcbbe97212f1ef1ce7c49b6184dd07ab1b3beb09a2abfc0e290abe5951b5c794e2df0a1340a38389dd825b563f966a5ae897ccecb361271165e31cc0130f46c
|
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
|
55
|
-
encrypt_env
|
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
|
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?("#{
|
32
|
+
if File.file?("#{@root_path}/config/secrets.yml.enc")
|
31
33
|
@opt = 1
|
32
|
-
elsif Dir["#{
|
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?("#{
|
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 ? "#{
|
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("#{
|
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("#{
|
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("#{
|
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 ? "#{
|
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 ? "#{
|
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["#{
|
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("#{
|
193
|
-
system("echo '/config/master*.key' >> #{
|
194
|
-
system("echo '/config/secrets.yml.old' >> #{
|
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
|
-
|
218
|
+
require 'awesome_print'
|
219
|
+
require 'date'
|
217
220
|
value = secrets(env)
|
218
|
-
|
219
|
-
#
|
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
|
-
|
241
|
-
confirm = "Really? You want to delete '#{key}'#{
|
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 "
|
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
|
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 =
|
280
|
+
add_variable = true
|
278
281
|
end
|
279
282
|
|
280
283
|
action = add_variable && 'add' || 'edit'
|
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.
|
4
|
+
version: 1.3.2
|
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
|
+
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
|