require 'optparse' require 'fileutils' module SymmetricEncryption class CLI attr_reader :key_path, :app_name, :encrypt, :config_file_path, :decrypt, :random_password, :new_keys, :generate, :environment, :keystore, :re_encrypt, :version, :output_file_name, :compress, :environments, :cipher_name, :rolling_deploy, :rotate_keys, :rotate_kek, :prompt, :show_version, :cleanup_keys, :activate_key, :migrate KEYSTORES = [:heroku, :environment, :file] def self.run!(argv) new(argv).run! end def initialize(argv) @version = current_version @environment = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development' @config_file_path = File.expand_path(ENV['SYMMETRIC_ENCRYPTION_CONFIG'] || 'config/symmetric-encryption.yml') @app_name = 'symmetric-encryption' @key_path = '/etc/symmetric-encryption' @cipher_name = 'aes-256-cbc' @rolling_deploy = false @prompt = false @show_version = false @keystore = :file if argv.empty? puts parser exit -10 end parser.parse!(argv) end def run! raise(ArgumentError, 'Cannot cleanup keys and rotate keys at the same time') if cleanup_keys && rotate_keys if show_version puts "Symmetric Encryption v#{VERSION}" puts "OpenSSL v#{OpenSSL::VERSION}" puts "Environment: #{environment}" elsif encrypt load_config prompt ? encrypt_string : encrypt_file(encrypt) elsif decrypt load_config prompt ? decrypt_string : decrypt_file(decrypt) elsif random_password load_config gen_random_password(random_password) elsif migrate run_migrate elsif re_encrypt load_config SymmetricEncryption::Utils::ReEncryptFiles.new(version: version).process_directory(re_encrypt) elsif activate_key run_activate_key elsif rotate_kek run_rotate_kek elsif rotate_keys run_rotate_keys elsif cleanup_keys run_cleanup_keys elsif generate generate_new_config else puts parser end end def parser @parser ||= OptionParser.new do |opts| opts.banner = <