class Object

Constants

CONFIG_FILE_INIT_CONTENT
CONFIG_NAME
EX_USAGE

Copyright 2017 Wojciech Adam Koszek <wojciech@koszek.com>

LASTPASS_ANSIBLE_NAME
LASTPASS_ANSIBLE_PASSLEN
LASTPASS_ANSIBLE_PREFIX
LASTPASS_ANSIBLE_WRONG_PASS_SIM
NAME

Public Instance Methods

check_if_logged_in() click to toggle source
# File bin/lastpass-ansible, line 36
def check_if_logged_in
  if system("lpass status -q") == false
    errfail("You must login first with: lpass login <login@name.com>")
  end
end
errfail(s) click to toggle source
# File bin/lastpass-ansible, line 31
def errfail(s)
  STDERR.puts "#{NAME}: #{s}\n"
  exit EX_USAGE
end
fail_if_terminal_output() click to toggle source
# File bin/lastpass-ansible, line 107
def fail_if_terminal_output
  stdout = IO.try_convert(STDOUT)
  if stdout.nil? || stdout.isatty
    errfail("Won't print Ansible Vault password to terminal")
  end
end
guess_site_name_by_directory() click to toggle source
# File bin/lastpass-ansible, line 68
def guess_site_name_by_directory
  return LASTPASS_ANSIBLE_PREFIX + "/" + File.basename(Dir.pwd)
end
initialize_if_init() click to toggle source
# File bin/lastpass-ansible, line 42
def initialize_if_init
  has_init_passed = ARGV.select{|arg| arg =~ /^--init/ }.length > 0
  if !has_init_passed
    return
  end
  if File.exist?(CONFIG_NAME)
    errfail("File #{CONFIG_NAME} exists!")
  end
  initialize_project
  exit 0
end
initialize_project() click to toggle source
# File bin/lastpass-ansible, line 54
def initialize_project
  guessed_site_name = guess_site_name_by_directory
  if lastpass_has_password(guessed_site_name)
    errfail("LassPass already has password for #{guessed_site_name}")
  end
  c1 = system("lpass generate #{guessed_site_name} #{LASTPASS_ANSIBLE_PASSLEN} >/dev/null 2>&1")
  if c1 != true
    errfail("Couldn't initialize. Maybe #{guessed_site_name} exists?")
  end
  system("lpass sync") # --sync=now didn't work for me for 'lpass generate'
  File.write(CONFIG_NAME, CONFIG_FILE_INIT_CONTENT + guessed_site_name)
  print "Config file #{CONFIG_NAME} created; set password for #{guessed_site_name} in Ansible\n"
end
lastpass_ansible_name_get() click to toggle source
# File bin/lastpass-ansible, line 80
def lastpass_ansible_name_get
  lastpass_ansible_name = read_config_file_recurse(Dir.pwd)
  var_name = LASTPASS_ANSIBLE_NAME
  if ENV.key?(var_name)
    lastpass_ansible_name = ENV[var_name]
  end
  if lastpass_ansible_name.nil?
    lastpass_ansible_name = guess_site_name_by_directory
  end
  return lastpass_ansible_name
end
lastpass_has_password(site_name) click to toggle source
# File bin/lastpass-ansible, line 72
def lastpass_has_password(site_name)
  system("lpass sync")
  return `lpass ls --color=never`
    .split('\n')
    .select{ |out_line| out_line.split('[')[0].strip =~ /^#{site_name}$/ }
    .length > 0
end
main() click to toggle source
# File bin/lastpass-ansible, line 16
def main
  if `which lpass`.length == 0
    errfail("You don't have the 'lpass' command tool")
  end

  check_if_logged_in
  initialize_if_init
  lastpass_ansible_name = lastpass_ansible_name_get
  fail_if_terminal_output
  maybe_simulate_wrong_password
  system("lpass show --password #{lastpass_ansible_name}")
end
maybe_simulate_wrong_password() click to toggle source
# File bin/lastpass-ansible, line 114
def maybe_simulate_wrong_password
  var_name = LASTPASS_ANSIBLE_WRONG_PASS_SIM
  if ENV.key?(var_name)
    print 'WrongPassword'
    exit 0
  end
end
read_config_file_recurse(directory) click to toggle source
# File bin/lastpass-ansible, line 92
def read_config_file_recurse(directory)
  path_chunks = directory.split('/')
  config_file_body = nil
  path_chunks.length.downto(1) do |path_index|
    new_path_chunks = path_chunks
    new_path_chunks[path_index] = CONFIG_NAME
    cfg_file_name = new_path_chunks[0..path_index].join('/')
    if File.exist?(cfg_file_name)
      config_file_body =
        File.readlines(cfg_file_name).select{|l| l =~ /^[^#]/ }[0]
    end
  end
  return config_file_body
end