Module: SshAuthorizedKeysCookbook::TemplateHelpers

Defined in:
libraries/template_helpers.rb

Overview

Some helpers to use from ssh_authorized_keys cookbook templates.

Examples:

self.class.send(:include, ::SshAuthorizedKeysCookbook::TemplateHelpers)
render_key('ssh-rsa', 'AAA...', 'sysadmin')
#=> "ssh-rsa AAA... sysadmin"

Instance Method Summary collapse

Instance Method Details

#render_key(keytype, key, comment) ⇒ String

Returns a rendered key line for the authorized_keys file.

Examples:

render_key('ssh-rsa', 'AAA...', 'sysadmin')
#=> "ssh-rsa AAA... sysadmin"

Parameters:

  • keytype (String)

    SSH key type.

  • key (String)

    SSH public key in base64.

  • comment (String)

    key comment or description.

Returns:

  • (String)

    the authorized_keys file line.


70
71
72
# File 'libraries/template_helpers.rb', line 70

def render_key(keytype, key, comment)
  "#{keytype} #{key} #{comment}"
end

#render_option_value(name, value) ⇒ String

Returns the SSH key option properly escaped.

Examples:

render_option_value('environment', 'NAME=value')
#=> "environment=\"NAME=value\""

Parameters:

  • name (String)

    option name.

  • value (String, true)

    option value. Use true for option flags.

Returns:

  • (String)

    string representation of the SSH key option.


39
40
41
42
43
# File 'libraries/template_helpers.rb', line 39

def render_option_value(name, value)
  return name.to_s if value == true
  value_escaped = value.to_s.gsub('\\', '\\\\\\\\').gsub('"', '\\"')
  %(#{name}="#{value_escaped}")
end

#render_options(options) ⇒ String

Returns the SSH key option list properly formated.

Examples:

options = {
  'no-agent-forwarding' => true,
  'environment' => 'DISPLAY=:0'
}
render_options(options)
#=> "no-agent-forwarding,environment=\"DISPLAY=:0\""

Parameters:

  • options (Hash)

    the option list as key value hash.

Returns:

  • (String)

    string representation of the option list properly escaped.


57
58
59
# File 'libraries/template_helpers.rb', line 57

def render_options(options)
  options.map { |name, value| render_option_value(name, value) }.join(',')
end