-
Notifications
You must be signed in to change notification settings - Fork 223
/
Copy pathquery.rb
executable file
·115 lines (107 loc) · 3.01 KB
/
query.rb
1
2
3
4
5
6
7
8
9
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env ruby
# == Synopsis
#
# Queries a remote smart proxy via https
#
# == Usage
#
# query.rb [options] url
#
# -h, --help
# show help
#
# --key [filename]
# The ssl private key
#
# --cert [filename]
# The ssl certificate file
#
# --ca [filename]
# The ssl Certificate Authority file.
# This will also contain the public keys of any host that you wish to grant access to this proxy
#
# --json
# Request the reply in json format rather than HTML
#
# -v, --verbose
# Operations are displayed in detail
#
# If the ssl keys are not specified then defaults are chosen based upon the platform
require 'English'
require 'rubygems'
require 'rest-client'
require 'getoptlong'
require 'rdoc/usage'
require 'pathname'
opts = GetoptLong.new(['--verbose', '-v', GetoptLong::NO_ARGUMENT],
['--help', '-h', GetoptLong::NO_ARGUMENT],
['--key', GetoptLong::REQUIRED_ARGUMENT],
['--cert', GetoptLong::REQUIRED_ARGUMENT],
['--ca', GetoptLong::REQUIRED_ARGUMENT],
['--verb', GetoptLong::REQUIRED_ARGUMENT],
['--json', GetoptLong::NO_ARGUMENT]
)
json = false
verb = :get
key = cert = ca = verbose = nil
opts.each do |opt, arg|
case opt
when '--help'
RDoc.usage
when '--key'
key = arg
when '--cert'
cert = arg
when '--ca'
ca = arg
when '--json'
json = true
when '--verbose'
verbose = true
when '--verb'
verb = arg.to_sym
end
end
unless key && cert && ca
if RUBY_PLATFORM =~ /mingw/
origin = Pathname.new(__dir__).parent.join "config"
key ||= origin.join "private.pem"
cert ||= origin.join "signed.pem"
ca ||= origin.join "ca.pem"
else
hostfile = `hostname -f`.chomp + ".pem"
key ||= "/var/lib/puppet/ssl/private_keys/" + hostfile
cert ||= "/var/lib/puppet/ssl/certs/" + hostfile
ca ||= "/var/lib/puppet/ssl/certs/ca.pem"
end
end
url = ARGV.shift
if url !~ /^https:\/\/.*:4567/
puts "Malformed or missing URL: " + (url.nil? ? "MISSING" : url.to_s)
puts "It should look something like this: " + 'https://brssa009.brs.someware.com:4567/dhcp/192.168.11.0'
exit(-1)
end
puts "#{$PROGRAM_NAME} --verb #{verb} --key #{key} --cert #{cert} --ca #{ca} #{json ? '--json' : ''} --verbose #{url}" if verbose
c = RestClient::Resource.new(
url,
:ssl_client_cert => OpenSSL::X509::Certificate.new(File.read(cert)),
:ssl_client_key => OpenSSL::PKey.read(File.read(key)),
:ssl_ca_file => ca
)
begin
json_args = {}
if json
json_args = {:accept => :json, :content_type => :json}
unless ARGV.empty?
# then merge any optional POST parameters
json_args.update(eval(ARGV.shift))
end
end
response = c.send(verb, json_args)
puts response.code
puts response.to_str
rescue => e
message = "Exception: '" + e.message + "'"
message += " with '#{e.response}'" if e.respond_to? :response
puts message
end