Class: Fingerpuppet::RestAPI
- Inherits:
-
Object
- Object
- Fingerpuppet::RestAPI
- Defined in:
- lib/fingerpuppet/restapi.rb
Instance Attribute Summary collapse
-
#certname ⇒ Object
Returns the value of attribute certname.
-
#server ⇒ Object
Returns the value of attribute server.
Instance Method Summary collapse
- #catalog ⇒ Object
- #certificate(node) ⇒ Object
- #certificate_revocation_list ⇒ Object
- #certificate_status(node, state = nil) ⇒ Object
-
#command(opts = {}) ⇒ Object
a helper that allows one to use either commandline curl or Net::HTTP this is useful mostly with -dcn to just print out the curl commandline you would use to accomplish what you’re trying to do.
-
#curl(opts = {}) ⇒ Object
def command(action, argument=”, method=‘GET’, type=‘yaml’, output=false, file=false, data=false).
- #debug ⇒ Object
- #delete(node) ⇒ Object
- #facts(node) ⇒ Object
- #file_metadata(path) ⇒ Object
- #getfile(path) ⇒ Object
- #init(certname, server) ⇒ Object
-
#initialize(options = {}) ⇒ RestAPI
constructor
A new instance of RestAPI.
- #insert(node, path) ⇒ Object
- #install ⇒ Object
- #node(node) ⇒ Object
- #report(node, file, data = nil) ⇒ Object
- #resource(resource) ⇒ Object
- #rest(opts = {}) ⇒ Object
- #save(path, data) ⇒ Object
- #search(query) ⇒ Object
- #sign(node) ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RestAPI
Returns a new instance of RestAPI.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/fingerpuppet/restapi.rb', line 8 def initialize( ={} ) @debug = [:debug] @curl = [:curl] @nop = [:nop] @output = [:output] @configdir = File.('~/.fingerpuppet') begin config = YAML.load_file("#{@configdir}/config.yaml") @server = config['server'] @certname = config['certname'] rescue Exception => e puts 'Initializing API...' end end |
Instance Attribute Details
#certname ⇒ Object
Returns the value of attribute certname.
6 7 8 |
# File 'lib/fingerpuppet/restapi.rb', line 6 def certname @certname end |
#server ⇒ Object
Returns the value of attribute server.
6 7 8 |
# File 'lib/fingerpuppet/restapi.rb', line 6 def server @server end |
Instance Method Details
#catalog ⇒ Object
215 216 217 218 |
# File 'lib/fingerpuppet/restapi.rb', line 215 def catalog self.command( { :action => 'catalog', :argument => @certname } ) end |
#certificate(node) ⇒ Object
268 269 270 271 272 |
# File 'lib/fingerpuppet/restapi.rb', line 268 def certificate(node) self.command( { :action => 'certificate', :argument => node, :type => 's' } ) end |
#certificate_revocation_list ⇒ Object
282 283 284 285 286 |
# File 'lib/fingerpuppet/restapi.rb', line 282 def certificate_revocation_list self.command( { :action => 'certificate_revocation_list', :argument => 'ca', :type => 's' } ) end |
#certificate_status(node, state = nil) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/fingerpuppet/restapi.rb', line 288 def certificate_status(node, state=nil) if node == nil self.command( { :action => 'certificate_statuses', :action => 'no_key', :type => 'pson' } ) elsif state == nil self.command( { :action => 'certificate_status', :argument => node, :type => 'pson' } ) else self.command( { :action => 'certificate_status', :argument => node, :method => 'PUT', :type => 'pson', :data => "{\"desired_state\":\"#{state}\"}" } ) end end |
#command(opts = {}) ⇒ Object
a helper that allows one to use either commandline curl or Net::HTTP this is useful mostly with -dcn to just print out the curl commandline you would use to accomplish what you’re trying to do
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/fingerpuppet/restapi.rb', line 27 def command( opts={} ) # this allows a global @output var, but to also override that per call opts[:output] ||= @output if @curl curl(opts) else data = rest(opts) if opts[:output] save(opts[:output], data) else # When using the API, you will probably want to consume this data rather than just printing it. # You might use YAML::load(data) or JSON::parse(data) depending on what's being returned puts data end end end |
#curl(opts = {}) ⇒ Object
def command(action, argument=”, method=‘GET’, type=‘yaml’, output=false, file=false, data=false)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/fingerpuppet/restapi.rb', line 106 def curl( opts={} ) opts[:type] ||= 'yaml' if opts[:noauth] auth = '-k ' else auth = "--cert #{@configdir}/#{@certname}.pem --key #{@configdir}/#{@certname}.key --cacert #{@configdir}/ca_crt.pem" end output = opts[:output] ? "-o #{opts[:output]}" : '' header = "-H 'Accept: #{opts[:type]}'" case opts[:method] when 'PUT' methodstr = '-X PUT' header = "-H 'Content-Type: text/#{opts[:type]}'" if opts[:file] filestr = "--data-binary @#{opts[:file]}" end if opts[:data] datastr = "--data '#{opts[:data]}'" end when 'DELETE' methodstr = '-X DELETE' when 'HEAD' methodstr = '-I' else # default to a GET request methodstr = '' end uri = "https://#{@server}:8140/production/#{opts[:action]}/#{opts[:argument]}" cmd = "curl #{auth} #{methodstr} #{output} #{filestr} #{datastr} #{header} \"#{uri}\"" #quoted uri for fact ampersands if @debug puts cmd else if not system(cmd) raise StandardError, 'cURL execution failed.' end puts # newline after curl output end end |
#debug ⇒ Object
325 326 327 328 329 |
# File 'lib/fingerpuppet/restapi.rb', line 325 def debug puts "@configdir: #{@configdir}" puts " @server: #{@server}" puts " @certname: #{@certname}" end |
#delete(node) ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/fingerpuppet/restapi.rb', line 260 def delete(node) self.certificate_status(node, 'revoked') self.command( { :action => 'certificate_status', :argument => node, :method => 'DELETE', :type => 'pson' } ) end |
#facts(node) ⇒ Object
225 226 227 228 |
# File 'lib/fingerpuppet/restapi.rb', line 225 def facts(node) self.command( { :action => 'facts', :argument => node } ) end |
#file_metadata(path) ⇒ Object
248 249 250 251 252 |
# File 'lib/fingerpuppet/restapi.rb', line 248 def (path) self.command( { :action => 'file_content', :argument => path, :type => 'yaml' } ) end |
#getfile(path) ⇒ Object
254 255 256 257 258 |
# File 'lib/fingerpuppet/restapi.rb', line 254 def getfile(path) self.command( { :action => 'file_content', :argument => path, :type => 'raw' } ) end |
#init(certname, server) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/fingerpuppet/restapi.rb', line 152 def init(certname, server) if certname == nil || server == nil puts "Must set server and certname to initialize API" exit end @certname = certname @server = server if File::directory?( @configdir ) require 'fileutils' FileUtils.rm_rf( @configdir ) end Dir.mkdir( @configdir ) configfile = File.new("#{@configdir}/config.yaml", 'w') configfile.syswrite("version: 0.1\n") configfile.syswrite("certname: #{@certname}\n") configfile.syswrite("server: #{@server}\n") configfile.close begin if not system("openssl genrsa -out #{@configdir}/#{@certname}.key 1024") raise StandardError, 'Certificate generation failed.' end if not system("openssl req -new -key #{@configdir}/#{@certname}.key -subj '/CN=#{@certname}' -out #{@configdir}/#{@certname}.csr") raise StandardError, 'CSR generation failed.' end self.command( { :action => 'certificate_request', :argument => @certname, :file => "#{@configdir}/#{@certname}.csr", :type => 'plain', :method => 'PUT', :noauth => true } ) puts "CSR submitted. Now go sign it on the server and rerun this with --install." rescue Exception => e puts "Failure: #{e.message}" end end |
#insert(node, path) ⇒ Object
241 242 243 244 245 246 |
# File 'lib/fingerpuppet/restapi.rb', line 241 def insert(node, path) self.command( { :action => 'facts', :argument => node, :method => 'PUT', :file => path } ) end |
#install ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/fingerpuppet/restapi.rb', line 195 def install begin self.command( { :action => 'certificate', :argument => @certname, :output => "#{@configdir}/#{@certname}.pem", :type => 's', :noauth => true } ) self.command( { :action => 'certificate', :argument => 'ca', :output => "#{@configdir}/ca_crt.pem", :type => 's', :noauth => true } ) puts "Certificate installed. API ready for use." rescue Exception => e puts "Failure: #{e.message}" end end |
#node(node) ⇒ Object
236 237 238 239 |
# File 'lib/fingerpuppet/restapi.rb', line 236 def node(node) self.command( { :action => 'node', :argument => node } ) end |
#report(node, file, data = nil) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/fingerpuppet/restapi.rb', line 311 def report(node, file, data=nil) if data self.command( { :action => 'report', :argument => node, :method => 'PUT', :data => data } ) else self.command( { :action => 'report', :argument => node, :method => 'PUT', :file => file } ) end end |
#resource(resource) ⇒ Object
306 307 308 309 |
# File 'lib/fingerpuppet/restapi.rb', line 306 def resource(resource) self.command( { :action => 'resource', :argument => resource } ) end |
#rest(opts = {}) ⇒ Object
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 |
# File 'lib/fingerpuppet/restapi.rb', line 52 def rest( opts={} ) opts[:type] ||= 'yaml' uri = "/production/#{opts[:action]}/#{opts[:argument]}" http = Net::HTTP.new(@server, 8140) http.use_ssl = true unless opts[:noauth] http.verify_mode = OpenSSL::SSL::VERIFY_PEER store = OpenSSL::X509::Store.new store.add_cert(OpenSSL::X509::Certificate.new(File.read("#{@configdir}/ca_crt.pem"))) http.cert_store = store http.key = OpenSSL::PKey::RSA.new(File.read("#{@configdir}/#{@certname}.key")) http.cert = OpenSSL::X509::Certificate.new(File.read("#{@configdir}/#{@certname}.pem")) end case opts[:method] when 'PUT' request = Net::HTTP::Put.new(uri) request["Content-Type"] = "text/#{opts[:type]}" if opts[:file] # set the body to the binary contents of :file file = File.open(opts[:file], 'rb') request.body = file.read else # set the body to the string value of :data request.body = opts[:data] end when 'DELETE' request = Net::HTTP::Delete.new(uri) request["Accept"] = opts[:type] when 'HEAD' request = Net::HTTP::Head.new(uri) request["Accept"] = opts[:type] else # default to a GET request request = Net::HTTP::Get.new(uri) request["Accept"] = opts[:type] end if @debug puts '------ HTTP Request ------' puts request.to_yaml puts '--------------------------' end return @nop ? '' : http.request(request).body end |
#save(path, data) ⇒ Object
46 47 48 49 50 |
# File 'lib/fingerpuppet/restapi.rb', line 46 def save(path, data) file = File.new(path, 'w') file.syswrite(data) file.close end |
#search(query) ⇒ Object
230 231 232 233 234 |
# File 'lib/fingerpuppet/restapi.rb', line 230 def search(query) query.map!{ |item| "facts.#{item}"} self.command( { :action => 'facts_search', :argument => "search?#{query.join('&')}" } ) end |
#sign(node) ⇒ Object
274 275 276 277 278 279 280 |
# File 'lib/fingerpuppet/restapi.rb', line 274 def sign(node) self.command( { :action => 'certificate_status', :argument => node, :method => 'PUT', :type => 'pson', :data => "{\"desired_state\":\"signed\"}" } ) end |
#status ⇒ Object
220 221 222 223 |
# File 'lib/fingerpuppet/restapi.rb', line 220 def status self.command( { :action => 'status', :argument => 'no_key' } ) end |