Module: Bridge::KeyTools
- Defined in:
- lib/bridge/key_tools.rb
Class Method Summary collapse
-
.find_nearest(path, name) ⇒ Object
Finds a parent path with name in it recursively.
-
.global_path ⇒ Object
Returns the path to the global key file.
-
.load_keys(for_hosts) ⇒ Object
Returns an array of saved keys for the hosts passed in.
-
.save_key(key, type = :user) ⇒ Object
Saves
key
in the path identified bytype
(:global, :user, :site). -
.site_path(path = Dir.getwd) ⇒ Object
Returns the path to a key file in the local site if it can be determined.
-
.user_path ⇒ Object
Returns the path to a key file in the user’s home directory if one exists.
Class Method Details
.find_nearest(path, name) ⇒ Object
Finds a parent path with name in it recursively. Must pass a normalized path in (see File.expand_path)
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/bridge/key_tools.rb', line 5 def self.find_nearest(path, name) if (Dir.glob(File.join(path, name)).empty?) parent = File.dirname(path) if (parent == path) # can't collapse any further, no match return nil end return find_nearest(parent, name) else return path end end |
.global_path ⇒ Object
Returns the path to the global key file
39 40 41 |
# File 'lib/bridge/key_tools.rb', line 39 def self.global_path File.join("/etc", "bridge_keys") end |
.load_keys(for_hosts) ⇒ Object
Returns an array of saved keys for the hosts passed in. Note that it will never load a key for a full wildcard host (“*”) as that would be a serious security concern.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bridge/key_tools.rb', line 46 def self.load_keys(for_hosts) paths = [site_path, user_path, global_path].compact keys = [] paths.each do |path| if (File.exists? path) lines = File.readlines(path) lines.each do |key| key.gsub!(%r{\n$}, '') key_string, , host = key.split(":", 3) next if host == '*' for_hosts.each do |for_host| if (%r{(^|\*|\.)#{Regexp.escape(host)}$}.match(for_host)) keys << key end end end end end return keys.uniq end |
.save_key(key, type = :user) ⇒ Object
Saves key
in the path identified by type
(:global, :user, :site). Will not save a key for host ‘*’
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bridge/key_tools.rb', line 70 def self.save_key(key, type = :user) key_string, , host = key.split(":", 3) return if host == '*' path = send(:"#{type}_path") if (path) File.open(path, "a") do |f| f.puts(key) end end end |
.site_path(path = Dir.getwd) ⇒ Object
Returns the path to a key file in the local site if it can be determined. Specifically, it’ll look for the nearest parent directory with any of the following under it (in priority order) script/server *.ru .git .hg
24 25 26 27 28 29 30 |
# File 'lib/bridge/key_tools.rb', line 24 def self.site_path(path = Dir.getwd) nearest = find_nearest(path, "script/server") || find_nearest(path, "*.ru") || find_nearest(path, ".git") || find_nearest(path, ".hg") return nearest && File.join(nearest, ".bridge_keys") end |
.user_path ⇒ Object
Returns the path to a key file in the user’s home directory if one exists. Returns nil if not found
34 35 36 |
# File 'lib/bridge/key_tools.rb', line 34 def self.user_path File.join(ENV["HOME"], ".bridge_keys") if (ENV["HOME"]) end |