class Hiera::Backend::Osxkeychain_backend::Keychain

Constants

SECURITY_PATH

Attributes

service[R]

Public Class Methods

new(service = nil) click to toggle source
# File lib/hiera/backend/osxkeychain_backend.rb, line 9
def initialize(service = nil)
  @service = service
end

Public Instance Methods

lookup(options = {}) click to toggle source
# File lib/hiera/backend/osxkeychain_backend.rb, line 13
def lookup(options = {})
  # See security(1) for these arguments.
  args = ["-w"]

  if service
    args += ["-s", service]
  end

  account = options[:account]
  if account
    args += ["-a", account]
  end

  label = options[:label]
  if label
    args += ["-l", label]
  end

  command = [SECURITY_PATH, "find-generic-password"] + args
  status, out, error = run(*command)
  if status.success?
    out.chomp
  else
    Hiera.warn("Fail to lookup #{options}: #{error.chomp}")
    nil
  end
end

Private Instance Methods

run(*cmd) click to toggle source

Fork and exec command, then return stdout, stderr and exit status. There are no such methods working on all ruby versions.

# File lib/hiera/backend/osxkeychain_backend.rb, line 45
def run(*cmd)
  Hiera.debug("exec #{cmd.join(" ")}")

  pipes = [IO.pipe, IO.pipe]

  stdout_read, stdout_write = pipes[0]
  stderr_read, stderr_write = pipes[1]

  pid = fork do
    stdout_read.close
    stderr_read.close
    STDOUT.reopen(stdout_write)
    STDERR.reopen(stderr_write)

    # Close file descriptors on exec(3).
    # This is for ruby prior to 1.9.1.
    set_close_on_exec

    # Ruby prior to 1.9.0 exec doesn't take any options.
    if RUBY_VERSION < "1.9.0"
      exec(*cmd)
    else
      # Give `:close_others` option for ruby 1.9.x.
      # This is by default on ruby 2.0.x and later.
      args = cmd + [{:close_others => true}]
      exec(*args)
    end
  end
  stdout_write.close
  stderr_write.close
  _, status = Process.waitpid2(pid)

  return [status, stdout_read.read, stderr_read.read]
ensure
  pipes.flatten.each do |io|
    io.close unless io.closed?
  end
end
set_close_on_exec() click to toggle source
# File lib/hiera/backend/osxkeychain_backend.rb, line 84
def set_close_on_exec
  ObjectSpace.each_object(IO) do |io|
    if ![STDIN, STDOUT, STDERR].include?(io) && !io.closed?
      io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) rescue SystemCallError
    end
  end
end