Class: Trixie::Load

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(file:, groups: [], format: "env") ⇒ Load

Returns a new instance of Load.



10
11
12
13
14
# File 'lib/trixie/load.rb', line 10

def initialize(file:, groups: [], format: "env")
  @file = file
  @groups = groups
  @formatter = Formatter.for(format)
end

Instance Method Details

#account_is_configured?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/trixie/load.rb', line 35

def 
  !Open3.capture2("op account list").first.empty?
end

#add_op_accountObject



64
65
66
67
68
69
70
71
# File 'lib/trixie/load.rb', line 64

def 
  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

#callObject



16
17
18
19
20
21
22
23
# File 'lib/trixie/load.rb', line 16

def call
  verify_op_installed!
  verify_secrets_config!

   unless 

  fetch_secrets
end

#create_accountObject



39
40
41
42
43
44
# File 'lib/trixie/load.rb', line 39

def 
  warn "* Configuring 1password Account"
  warn "To get the Secret Key take a look at https://support.1password.com/secret-key/"

  
end

#fetch_secretsObject



46
47
48
# File 'lib/trixie/load.rb', line 46

def fetch_secrets
  `eval $(op signin) && echo '#{formatted_secrets}' | op inject`
end

#filtered_secretsObject



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_secretsObject



60
61
62
# File 'lib/trixie/load.rb', line 60

def formatted_secrets
  @formatter.call(filtered_secrets)
end

#secrets_configObject



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