Permalink
Browse files

Initial SSH known_host LWRP

  • Loading branch information...
0 parents commit e6aad0af96dd96383b64eef3248fe3b88455b3ca @markolson committed Jan 2, 2013
Showing with 136 additions and 0 deletions.
  1. +12 −0 CHANGELOG.md
  2. +12 −0 README.md
  3. +2 −0 attributes/default.rb
  4. +28 −0 libraries/ssh_path_helpers.rb
  5. +6 −0 metadata.rb
  6. +4 −0 providers/config.rb
  7. +34 −0 providers/known_hosts.rb
  8. +13 −0 recipes/default.rb
  9. +12 −0 resources/config.rb
  10. +13 −0 resources/known_hosts.rb
@@ -0,0 +1,12 @@
+# CHANGELOG for ssh
+
+This file is used to list changes made in each version of ssh.
+
+## 0.1.0:
+
+* Initial release of ssh
+
+- - -
+Check the [Markdown Syntax Guide](http://daringfireball.net/projects/markdown/syntax) for help with Markdown.
+
+The [Github Flavored Markdown page](http://github.github.com/github-flavored-markdown/) describes the differences between markdown on github and standard markdown.
@@ -0,0 +1,12 @@
+Description
+===========
+
+Requirements
+============
+
+Attributes
+==========
+
+Usage
+=====
+
@@ -0,0 +1,2 @@
+default['ssh']['known_hosts_path'] = '/etc/ssh/ssh_known_hosts'
+default['ssh']['config_path'] = '/etc/ssh/ssh_config'
@@ -0,0 +1,28 @@
+class Chef
+ module SSH
+ module PathHelpers
+
+ def default_or_user_path(default, ssh_user)
+ filename = File.basename(default)
+ ssh_path = nil
+ if (new_resource.user && !new_resource.path)
+ ssh_path = "/home/#{new_resource.user}/.ssh/#{filename}"
+ elsif new_resource.path
+ ssh_path = new_resource.path
+ else
+ ssh_path = default
+ end
+
+ directory "Creating #{ssh_path} for #{ssh_user}" do
+ owner ssh_user
+ mode '0700'
+ recursive true
+ path ::File.dirname(ssh_path)
+ user ssh_user
+ end
+
+ return ssh_path
+ end
+ end
+ end
+end
@@ -0,0 +1,6 @@
+maintainer "Mark Olson"
+maintainer_email "theothermarkolson@gmail.com"
+license "All rights reserved"
+description "Installs/Configures SSH"
+long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
+version "0.1.0"
@@ -0,0 +1,4 @@
+action :create do
+
+
+end
@@ -0,0 +1,34 @@
+require 'shellwords'
+
+include Chef::SSH::PathHelpers
+
+
+action :create do
+ ssh_user = new_resource.user || 'root'
+ known_hosts_path = default_or_user_path(node['ssh']['known_hosts_path'], ssh_user)
+
+ key = new_resource.key
+ if key.nil?
+ results = `ssh-keyscan #{new_resource.hashed ? '-H ' : ''} #{Shellwords.escape(new_resource.host)}`
+ Chef::Application.fatal! results.strip if key =~ /getaddrinfo/
+ key = results.strip
+ end
+
+ execute "add known_host entry for #{new_resource.host}" do
+ not_if "ssh-keygen -H -F #{Shellwords.escape(new_resource.host)} -f #{known_hosts_path} | grep 'Host #{new_resource.host} found'"
+ command "echo '#{key}' >> #{known_hosts_path}"
+ user ssh_user
+ end
+
+ log "An entry for #{new_resource.host} already exists in #{known_hosts_path}." do
+ only_if "ssh-keygen -H -F #{Shellwords.escape(new_resource.host)} -f #{known_hosts_path} | grep 'Host #{new_resource.host} found'"
+ end
+end
+
+action :delete do
+ known_hosts_path = node['ssh']['known_hosts_path']
+ execute "remove known_host entry for #{new_resource.host}" do
+ command "ssh-keygen -R #{Shellwords.escape(new_resource.host)}"
+ user ssh_user
+ end
+end
@@ -0,0 +1,13 @@
+#
+# Cookbook Name:: ssh
+# Recipe:: default
+#
+# Copyright 2013, Mark Olson
+#
+# All rights reserved - Do Not Redistribute
+#
+
+ssh_known_hosts "github.com" do
+ hashed true
+ user 'vagrant'
+end
@@ -0,0 +1,12 @@
+actions :create
+default_action :create
+
+attribute :host, :kind_of => String, :name_attribute => true
+attribute :options, :kind_of => Hash
+attribute :user, :kind_of => String
+attribute :path, :kind_of => String
+
+def initialize(*args)
+ super
+ @action = :create
+end
@@ -0,0 +1,13 @@
+actions :create
+default_action :create
+
+attribute :host, :kind_of => String, :name_attribute => true
+attribute :hashed, :kind_of => [TrueClass, FalseClass], :default => TrueClass
+attribute :key, :kind_of => String
+attribute :user, :kind_of => String
+attribute :path, :kind_of => String
+
+def initialize(*args)
+ super
+ @action = :create
+end

0 comments on commit e6aad0a

Please sign in to comment.