Class: Scout::Command::Sign

Inherits:
Scout::Command show all
Defined in:
lib/scout/command/sign.rb

Constant Summary collapse

HELP_URL =
"https://server-monitor.readme.io/docs/custom-plugins"

Constants inherited from Scout::Command

HTTP_HEADERS

Constants included from HTTP

HTTP::CA_FILE, HTTP::VERIFY_MODE

Instance Attribute Summary

Attributes inherited from Scout::Command

#config_dir, #history, #hostname, #log_path, #server, #server_name

Instance Method Summary collapse

Methods inherited from Scout::Command

#create_pid_file_or_exit, dispatch, #initialize, #level, #log, program_name, #program_name, program_path, #program_path, usage, #usage, user, #user, #verbose?

Methods included from HTTP

#build_http

Constructor Details

This class inherits a constructor from Scout::Command

Instance Method Details

#fetch_code(url) ⇒ Object

run



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/scout/command/sign.rb', line 54

def fetch_code(url)
  puts "Fetching code..."

  uri = URI.parse(url)
  http = build_http(uri)

  request = Net::HTTP::Get.new(uri.request_uri)
  res = http.request(request)
  if !res.is_a?(Net::HTTPOK)
    puts "ERROR - Unable to fetch code: #{res.class}."
    return
  end
  res.body
end

#load_private_keyObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/scout/command/sign.rb', line 69

def load_private_key
  private_key_path=File.expand_path("~/.scout/scout_rsa")
  if !File.exist?(private_key_path)
    puts "ERROR - Unable to find the private key at #{private_key_path} for code signing.\nSee #{HELP_URL} for help creating your account's key pair."
    return nil
  else
    begin
      OpenSSL::PKey::RSA.new(File.read(private_key_path))
    rescue
      puts "Error - Found a private key at #{private_key_path}, but unable to load the key:"
      puts $!.message
      puts "See #{HELP_URL} for help creating your account's key pair."
      return nil
    end
  end
end

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/scout/command/sign.rb', line 10

def run
  url = @args.first
  # read the plugin_code from the file specified
  if url.nil? or url == ''
    puts "Please specify the path to the plugin (scout sign /path/to/plugin.rb)"
    return
  end
  
  code=fetch_code(url)
  if code.nil?
    return
  end
  
  private_key = load_private_key
  if private_key.nil?
    return
  end
  
  puts "Signing code..."
  code=code.gsub(/ +$/,'')
  code_signature = private_key.sign( OpenSSL::Digest::SHA1.new, code)
  sig=Base64.encode64(code_signature)
  
  puts "Posting Signature..."

  uri = URI.parse(url)
  http = build_http(uri)

  request = Net::HTTP::Post.new(uri.request_uri)
  request.set_form_data({'signature' => sig})
  res = http.request(request)
  if !res.is_a?(Net::HTTPOK)
    puts "ERROR - Unable to post signature" 
    return
  end
  puts "...Success!"
rescue Timeout::Error
  puts "ERROR - Unable to sign code (Timeout)"
rescue
  puts "ERROR - Unable to sign code:"
  puts $!
  puts $!.backtrace
end