Class: Trixie::Load
- Inherits:
-
Object
- Object
- Trixie::Load
- Defined in:
- lib/trixie/load.rb
Overview
Fetches the specified secrets with op cli and returns them formatted
Constant Summary collapse
- OP_NOT_INSTALLED =
"op cli is not installed please download and install at https://developer.1password.com/docs/cli/get-started#install"
- OP_ADDRESS_ENV =
"TRIXIE_OP_ADDRESS"
- OP_EMAIL_ENV =
"TRIXIE_OP_EMAIL"
Instance Method Summary collapse
- #account_is_configured? ⇒ Boolean
- #add_op_account ⇒ Object
- #call ⇒ Object
- #create_account ⇒ Object
- #fetch_secrets ⇒ Object
- #filtered_secrets ⇒ Object
- #formatted_secrets ⇒ Object
-
#initialize(file:, groups: [], format: "env") ⇒ Load
constructor
A new instance of Load.
- #secrets_config ⇒ Object
- #verify_op_installed! ⇒ Object
- #verify_secrets_config! ⇒ Object
Constructor Details
Instance Method Details
#account_is_configured? ⇒ Boolean
35 36 37 |
# File 'lib/trixie/load.rb', line 35 def account_is_configured? !Open3.capture2("op account list").first.empty? end |
#add_op_account ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'lib/trixie/load.rb', line 64 def add_op_account cmd = "op account add" cmd += " --address #{ENV[OP_ADDRESS_ENV]}" if ENV[OP_ADDRESS_ENV] cmd += " --email #{ENV[OP_EMAIL_ENV]}" if ENV[OP_EMAIL_ENV] `#{cmd}` end |
#call ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/trixie/load.rb', line 16 def call verify_op_installed! verify_secrets_config! create_account unless account_is_configured? fetch_secrets end |
#create_account ⇒ Object
39 40 41 42 43 44 |
# File 'lib/trixie/load.rb', line 39 def create_account warn "* Configuring 1password Account" warn "To get the Secret Key take a look at https://support.1password.com/secret-key/" add_op_account end |
#fetch_secrets ⇒ Object
46 47 48 |
# File 'lib/trixie/load.rb', line 46 def fetch_secrets `eval $(op signin) && echo '#{formatted_secrets}' | op inject` end |
#filtered_secrets ⇒ Object
54 55 56 57 58 |
# File 'lib/trixie/load.rb', line 54 def filtered_secrets return secrets_config["secrets"] if @groups.empty? secrets_config["secrets"].select { |secret| secret["groups"].any? { |group| @groups.include?(group) } } end |
#formatted_secrets ⇒ Object
60 61 62 |
# File 'lib/trixie/load.rb', line 60 def formatted_secrets @formatter.call(filtered_secrets) end |
#secrets_config ⇒ Object
50 51 52 |
# File 'lib/trixie/load.rb', line 50 def secrets_config @secrets_config ||= YAML.load_file(@file) end |
#verify_op_installed! ⇒ Object
25 26 27 |
# File 'lib/trixie/load.rb', line 25 def verify_op_installed! raise Trixie::OpCLINotInstalledError, OP_NOT_INSTALLED unless system("which op > /dev/null") end |
#verify_secrets_config! ⇒ Object
29 30 31 32 33 |
# File 'lib/trixie/load.rb', line 29 def verify_secrets_config! result = Trixie::Contracts::TrixieYml.new.call(secrets_config) raise Trixie::InvalidConfigError, "Invalid .trixie.yml: #{result.errors.to_h}" if result.errors.any? end |