Class: EncryptEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/encrypt_env.rb

Overview

gem ‘encrypt_env’ rubocop:disable Metrics/ClassLength rubocop:disable Metrics/MethodLength

Class Method Summary collapse

Class Method Details

.create(key, env = nil, is_edit = false) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/encrypt_env.rb', line 343

def self.create(key, env = nil, is_edit = false)
  load_curr_opt unless @opt
  if @opt == 1
    puts 'Only for option 2!'
    return
  end
  tail_msg = env ? " in '#{env}' environment" : nil

  value = hash_secrets(env)

  if !is_edit && value.key?(key)
    puts "Key existed#{tail_msg}!"
    return
  end

  if !value.key?(key) && is_edit
    puts "'#{key}' does not exist#{tail_msg}. You want to create '#{key}' as the new key? (y/n)"
    a = $stdin.gets.chomp
    return unless a == 'y'

    is_edit = false
  end

  action = is_edit && 'edit' || 'create'
  file_name = env ? "#{action}_#{key}_#{env}" : "#{action}_#{key}"

  Tempfile.create(file_name) do |f|
    f.write(value[key])
    f.flush
    f.rewind
    system("vim #{f.path}")
    # new_value = File.read(f.path)
    new_value = YAML.load_file(f.path)
    # value[key] = new_value.strip
    value[key] = new_value
    encrypt(value.to_hash.to_yaml, env || current_env)
    @decrypted = nil
    @result = nil
  end

  puts "#{key}\t=>\t#{value[key]}"
end

.create_with_value(key, new_value, env = nil, type = nil) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/encrypt_env.rb', line 318

def self.create_with_value(key, new_value, env = nil, type = nil)
  load_curr_opt unless @opt
  if @opt == 1
    puts 'Only for option 2!'
    return
  end

  new_value = type_coercion(new_value, type)

  tail_msg = env ? " in '#{env}' environment" : nil

  value = hash_secrets(env)

  if value.key?(key)
    puts "Key existed#{tail_msg}!"
    return
  end

  value[key] = new_value
  encrypt(value.to_hash.to_yaml, env || current_env)
  @decrypted = nil
  @result = nil
  puts "#{key}\t=>\t#{value[key]}"
end

.delete(key, env = nil) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/encrypt_env.rb', line 248

def self.delete(key, env = nil)
  load_curr_opt unless @opt
  if @opt == 1
    puts 'Only for option 2!'
    return
  end

  tail_msg = env ? " in '#{env}' environent" : nil
  confirm = "Really? You want to delete '#{key}'#{tail_msg}? (y/n)"
  puts confirm
  a = $stdin.gets.chomp
  return unless a == 'y'

  value = hash_secrets(env)

  unless value.key?(key)
    puts "#{key} does not exist#{tail_msg}!"
    return
  end

  tmp_value = value[key]
  value.delete(key)
  encrypt(value.to_hash.to_yaml, env || current_env)
  puts "Delete '#{key}' with value '#{tmp_value}' successfully!"
end

.edit(env = nil, variable_name = nil) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/encrypt_env.rb', line 207

def self.edit(env = nil, variable_name = nil)
  variable_name && (return create(variable_name, env, true))

  load_curr_opt unless @opt
  env ||= current_env if @opt == 2
  return unless decrypt(env)

  Tempfile.create("secrets_#{env}.yml") do |f|
    f.write(@raw_decrypted)
    f.flush
    f.rewind
    system("vim #{f.path}")
    encrypt(File.read(f.path), env)
    @decrypted = nil
    @result = nil
  end
rescue StandardError => e
  puts e.message
end

.hash_secrets(env = nil) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/encrypt_env.rb', line 161

def self.hash_secrets(env = nil)
  load_curr_opt unless @opt
  if env == 'all'
    result = secrets_all
    @decrypted = nil
    return result
  end

  if @opt == 1
    decrypt unless @decrypted
    @decrypted[env || current_env]
  else
    decrypt(env || current_env) unless @decrypted
    @decrypted
  end
rescue StandardError => e
  puts e.message
  @have_error = true
  {}
end

.method_missing(key, *_args) ⇒ Object



394
395
396
397
# File 'lib/encrypt_env.rb', line 394

def self.method_missing(key, *_args)
  hash_secrets unless @decrypted
  @decrypted[key]
end

.secretsObject



386
387
388
389
390
391
392
# File 'lib/encrypt_env.rb', line 386

def self.secrets
  return @result if @result

  @result = ActiveSupport::OrderedOptions[hash_secrets.deep_symbolize_keys]

  @result
end

.secrets_allObject



154
155
156
157
158
159
# File 'lib/encrypt_env.rb', line 154

def self.secrets_all
  return all_decrypted_object if @opt == 2

  decrypt unless @decrypted
  @decrypted
end

.setupObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/encrypt_env.rb', line 182

def self.setup
  define_option
  load_content_to_encrypt
  system("mkdir -p #{@root_path}/config/master_key")
  system("mkdir -p #{@root_path}/config/encrypt_enc")
  generate_keys

  if @opt == 1
    load_master_key
    encrypt(@content_to_encrypt)
  else
    to_hash_type(@content_to_encrypt).each do |env, value|
      next if env == 'default'

      load_master_key(env)
      encrypt(value.to_hash.to_yaml, env)
    end
  end

  File.rename("#{@root_path}/config/secrets.yml", "#{@root_path}/config/secrets.yml.old")
  system("echo '/config/master_key/master*.key' >> #{@root_path}/.gitignore")
  system("echo '/config/secrets.yml.old' >> #{@root_path}/.gitignore")
  system("echo 'Set up complete!'")
end

.show(env = nil, variable_name = nil) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'lib/encrypt_env.rb', line 227

def self.show(env = nil, variable_name = nil)
  variable_name && (return valueof(variable_name, env))

  require 'awesome_print'
  require 'date'
  value = hash_secrets(env)
  ap(value) unless @have_error
  # jj value unless @have_error
  @have_error = false
end

.valueof(key, env = nil) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'lib/encrypt_env.rb', line 238

def self.valueof(key, env = nil)
  tail_msg = env ? " in '#{env}' environent" : nil
  value = hash_secrets(env)
  unless value.key?(key)
    puts "key '#{key}' does not exist#{tail_msg}!"
    return
  end
  puts value[key]
end