Sha256: 36c6ccfa01296749bd7903e68995a27740f39cc5a2c49a1dd8b6cea455ab8525

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require_relative "../errors"

class MuchKeys::CLI::Validator

    def self.validate_primary_mode_option(options)
      raise MuchKeys::CLIOptionsError, primary_mode_error_message unless options_has_one_mode?(options)
    end

    def self.validate_encrypt(options)
      abort "--encrypt needs the --file option passed."        if !options[:file]
      abort "--encrypt needs the --public_key option passed."  if !options[:public_key]
    end

    def self.validate_decrypt(options)
      key_name = options[:consul_key]
      abort "--decrypt needs the --consul_key option passed." if !key_name

      if !secret_adapter.auto_certificates_exist_for_key?(key_name)
        certfile_expected = secret_adapter.certfile_name(key_name)
        abort "--decrypt needs the --public_key option passed or a PEM file needs to be at #{certfile_expected}."  if !options[:public_key]
        abort "--decrypt needs the --private_key option passed or a PEM file needs to be at #{certfile_expected}." if !options[:private_key]
      end
    end

    def self.validate_plain(options)
      abort "--plain needs the --consul_key option passed." if !options[:consul_key]
    end

    def self.options_has_one_mode?(options)
      [ options[:encrypt], options[:decrypt], options[:plain] ].count(true) == 1
    end

    def self.primary_mode_error_message
      "You must pass only one and at least one of the following flags: --encrypt, --decrypt, or --plain"
    end

    def self.secret_adapter
      MuchKeys::Secret
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
muchkeys-0.3.3 lib/muchkeys/cli/validator.rb