encrypt_env 0.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 +7 -0
- data/bin/encrypt_env +40 -0
- data/lib/encrypt_env.rb +110 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d2e5975e1531956cbfed23c4941007447ea37e1be9665f2d3918f23905edda0d
|
4
|
+
data.tar.gz: b9aa751c784ed18b7191a128b262630e57ea0c8c748eca0429ce87de83fdcbb3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75b384706fec3025c57d6ca4e856d5615e059ed3760eaa4a7c966235ebcabd55a7b0c9b812fa6693ff040593bbc635471a250077bb2ddfd7ab6a5288070aa432
|
7
|
+
data.tar.gz: 2ea4150d0f108e0806490b4cf19c778c71b892ace09d2ba3afe2812f30cb5e87d1d8bf85fbe6a339c563a2cf9389efaad2bb10d4d110d6772c293007398a303e
|
data/bin/encrypt_env
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require 'encrypt_env'
|
6
|
+
|
7
|
+
COMMANDS = {
|
8
|
+
'setup' => EncryptEnv.setup,
|
9
|
+
'secrets' => EncryptEnv.secrets,
|
10
|
+
'secrets_all' => EncryptEnv.secrets_all,
|
11
|
+
'edit' => EncryptEnv.edit
|
12
|
+
}.freeze
|
13
|
+
|
14
|
+
argv = ARGV
|
15
|
+
action = argv.shift
|
16
|
+
command_class = COMMANDS[action]
|
17
|
+
unless command_class
|
18
|
+
if ['help', '--help', '-h'].include?(action)
|
19
|
+
puts <<~HELP
|
20
|
+
Usage:
|
21
|
+
encrypt_env setup
|
22
|
+
encrypt_env secrets
|
23
|
+
encrypt_env secrets_all
|
24
|
+
encrypt_env edit
|
25
|
+
HELP
|
26
|
+
|
27
|
+
exit 0
|
28
|
+
else
|
29
|
+
puts "Unknown action: #{action}"
|
30
|
+
exit 1
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
command = command_class.new(argv)
|
36
|
+
command.run!
|
37
|
+
rescue ArgumentError => e
|
38
|
+
puts e.message
|
39
|
+
exit 1
|
40
|
+
end
|
data/lib/encrypt_env.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
require 'openssl'
|
5
|
+
require 'yaml'
|
6
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
7
|
+
|
8
|
+
# gem 'encrypt_env'
|
9
|
+
class EncryptEnv
|
10
|
+
private_class_method def self.master_key
|
11
|
+
key = File.read("#{@path_root}/config/master.key")
|
12
|
+
[key].pack('H*')
|
13
|
+
end
|
14
|
+
|
15
|
+
private_class_method def self.data_decrypt(raw_data)
|
16
|
+
encrypted = raw_data.slice(0, raw_data.length - 28)
|
17
|
+
iv = raw_data.slice(raw_data.length - 28, 12)
|
18
|
+
tag = raw_data.slice(raw_data.length - 16, 16)
|
19
|
+
{ encrypted: encrypted, iv: iv, tag: tag }
|
20
|
+
end
|
21
|
+
|
22
|
+
private_class_method def self.encrypt(content)
|
23
|
+
cipher = OpenSSL::Cipher.new('aes-128-gcm')
|
24
|
+
cipher.encrypt
|
25
|
+
cipher.key = master_key
|
26
|
+
iv = cipher.random_iv
|
27
|
+
encrypted = cipher.update(content) + cipher.final
|
28
|
+
tag = cipher.auth_tag
|
29
|
+
hex_string = (encrypted + iv + tag).unpack1('H*')
|
30
|
+
File.open("#{@path_root}/config/secrets.yml.enc", 'w') { |file| file.write(hex_string) }
|
31
|
+
end
|
32
|
+
|
33
|
+
private_class_method def self.decrypt
|
34
|
+
decipher = OpenSSL::Cipher.new('aes-128-gcm')
|
35
|
+
decipher.decrypt
|
36
|
+
hex_string = File.read("#{@path_root}/config/secrets.yml.enc")
|
37
|
+
data_decrypt = self.data_decrypt([hex_string].pack('H*'))
|
38
|
+
decipher.iv = data_decrypt[:iv]
|
39
|
+
decipher.key = master_key
|
40
|
+
decipher.auth_tag = data_decrypt[:tag]
|
41
|
+
|
42
|
+
decipher.update(data_decrypt[:encrypted]) + decipher.final
|
43
|
+
end
|
44
|
+
|
45
|
+
private_class_method def self.path_root
|
46
|
+
@path_root = if defined?(Rails)
|
47
|
+
Rails.root.to_s
|
48
|
+
elsif defined?(Bundler)
|
49
|
+
Bundler.root.to_s
|
50
|
+
else
|
51
|
+
Dir.pwd
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.setup
|
56
|
+
path_root
|
57
|
+
@secret_file = File.expand_path("#{@path_root}/config/secrets.yml")
|
58
|
+
key = OpenSSL::Random.random_bytes(16)
|
59
|
+
# save key in master.key file
|
60
|
+
File.open("#{@path_root}/config/master.key", 'w') { |file| file.write(key.unpack1('H*')) }
|
61
|
+
encrypt(File.read(@secret_file))
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.edit
|
65
|
+
path_root unless @path_root
|
66
|
+
secrets unless @decrypted
|
67
|
+
Tempfile.create do |f|
|
68
|
+
f.write(decrypt)
|
69
|
+
f.flush
|
70
|
+
f.rewind
|
71
|
+
system("vim #{f.path}")
|
72
|
+
encrypt(File.read(f.path))
|
73
|
+
@decrypted = nil
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.secrets_all
|
78
|
+
path_root unless @path_root
|
79
|
+
secrets unless @decrypted
|
80
|
+
@decrypted
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.secrets
|
84
|
+
path_root unless @path_root
|
85
|
+
@decrypted = HashWithIndifferentAccess.new(YAML.safe_load(
|
86
|
+
decrypt, aliases: true
|
87
|
+
))
|
88
|
+
@decrypted[Rails.env.to_sym] || @decrypted[:default]
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.secrets_production
|
92
|
+
secrets unless @decrypted
|
93
|
+
@decrypted[:production]
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.secrets_development
|
97
|
+
secrets unless @decrypted
|
98
|
+
@decrypted[:development]
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.secrets_test
|
102
|
+
secrets unless @decrypted
|
103
|
+
@decrypted[:test]
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.secrets_staging
|
107
|
+
secrets unless @decrypted
|
108
|
+
@decrypted[:staging]
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: encrypt_env
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nhu Tan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-07-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Encrypts and decrypts environment variables
|
14
|
+
email: nhutan2001@gmail.com
|
15
|
+
executables:
|
16
|
+
- encrypt_env
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- bin/encrypt_env
|
21
|
+
- lib/encrypt_env.rb
|
22
|
+
homepage: https://rubygems.org/gems/encrypt_env
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.6.0
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubygems_version: 3.3.7
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Ecrypt secrets.yml file
|
45
|
+
test_files: []
|