Class: Puppet::Indirector::REST

Inherits:
Terminus show all
Includes:
Network::HTTP::API::V1
Defined in:
lib/puppet/indirector/rest.rb

Overview

Access objects via REST

Constant Summary

Constants included from Network::HTTP::API::V1

Network::HTTP::API::V1::METHOD_MAP

Constants included from Util

Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE

Constants included from Util::Docs

Util::Docs::HEADER_LEVELS

Class Attribute Summary collapse

Attributes included from Util::Docs

#doc, #nodoc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Network::HTTP::API::V1

#indirection2uri, #indirection_method, #plurality, #pluralize, #request_to_uri_and_body, #uri2indirection

Methods inherited from Terminus

abstract_terminus?, const2name, #indirection, indirection_name, inherited, #initialize, mark_as_abstract_terminus, #model, model, #name, name2const, register_terminus_class, terminus_class, terminus_classes, #terminus_type, #validate, #validate_model

Methods included from Util::InstanceLoader

#instance_docs, #instance_hash, #instance_load, #instance_loader, #instance_loading?, #loaded_instance, #loaded_instances

Methods included from Util

absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask

Methods included from Util::POSIX

#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid

Methods included from Util::Docs

#desc, #dochook, #doctable, #markdown_definitionlist, #markdown_header, #nodoc?, #pad, scrub

Constructor Details

This class inherits a constructor from Puppet::Indirector::Terminus

Class Attribute Details

.port_settingObject (readonly)

Returns the value of attribute port_setting.



14
15
16
# File 'lib/puppet/indirector/rest.rb', line 14

def port_setting
  @port_setting
end

.server_settingObject (readonly)

Returns the value of attribute server_setting.



14
15
16
# File 'lib/puppet/indirector/rest.rb', line 14

def server_setting
  @server_setting
end

Class Method Details

.portObject



31
32
33
# File 'lib/puppet/indirector/rest.rb', line 31

def self.port
  Puppet.settings[port_setting || :masterport].to_i
end

.serverObject



22
23
24
# File 'lib/puppet/indirector/rest.rb', line 22

def self.server
  Puppet.settings[server_setting || :server]
end

.use_port_setting(setting) ⇒ Object

Specify the setting that we should use to get the port.



27
28
29
# File 'lib/puppet/indirector/rest.rb', line 27

def self.use_port_setting(setting)
  @port_setting = setting
end

.use_server_setting(setting) ⇒ Object

Specify the setting that we should use to get the server name.



18
19
20
# File 'lib/puppet/indirector/rest.rb', line 18

def self.use_server_setting(setting)
  @server_setting = setting
end

Instance Method Details

#destroy(request) ⇒ Object

Raises:

  • (ArgumentError)


128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/puppet/indirector/rest.rb', line 128

def destroy(request)
  raise ArgumentError, "DELETE does not accept options" unless request.options.empty?

  response = http_delete(request, indirection2uri(request), headers)

  if is_http_200?(response)
    content_type, body = parse_response(response)
    deserialize_destroy(content_type, body)
  else
    nil
  end
end

#find(request) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet/indirector/rest.rb', line 90

def find(request)
  uri, body = request_to_uri_and_body(request)
  uri_with_query_string = "#{uri}?#{body}"
  # WEBrick in Ruby 1.9.1 only supports up to 1024 character lines in an HTTP request
  # http://redmine.ruby-lang.org/issues/show/3991
  response = if "GET #{uri_with_query_string} HTTP/1.1\r\n".length > 1024
    http_post(request, uri, body, headers)
  else
    http_get(request, uri_with_query_string, headers)
  end

  if is_http_200?(response)
    content_type, body = parse_response(response)
    result = deserialize_find(content_type, body)
    result.name = request.key if result.respond_to?(:name=)
    result
  else
    nil
  end
end

#head(request) ⇒ Object



111
112
113
114
115
# File 'lib/puppet/indirector/rest.rb', line 111

def head(request)
  response = http_head(request, indirection2uri(request), headers)

  !!is_http_200?(response)
end

#headersObject

Provide appropriate headers.



36
37
38
# File 'lib/puppet/indirector/rest.rb', line 36

def headers
  add_accept_encoding({"Accept" => model.supported_formats.join(", ")})
end

#http_request(method, request, *args) ⇒ Object



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
# File 'lib/puppet/indirector/rest.rb', line 50

def http_request(method, request, *args)
  http_connection = network(request)
  peer_certs = []
  verify_errors = []

  http_connection.verify_callback = proc do |preverify_ok, ssl_context|
    # We use the callback to collect the certificates for use in constructing
    # the error message if the verification failed.  This is necessary since we
    # don't have direct access to the cert that we expected the connection to
    # use otherwise.
    peer_certs << Puppet::SSL::Certificate.from_s(ssl_context.current_cert.to_pem)
    # And also keep the detailed verification error if such an error occurs
    if ssl_context.error_string and not preverify_ok
      verify_errors << "#{ssl_context.error_string} for #{ssl_context.current_cert.subject}"
    end
    preverify_ok
  end

  http_connection.send(method, *args)
rescue OpenSSL::SSL::SSLError => error
  if error.message.include? "certificate verify failed"
    msg = error.message
    msg << ": [" + verify_errors.join('; ') + "]"
    raise Puppet::Error, msg
  elsif error.message =~ /hostname (was )?not match/
    raise unless cert = peer_certs.find { |c| c.name !~ /^puppet ca/i }

    valid_certnames = [cert.name, *cert.subject_alt_names].uniq
    msg = valid_certnames.length > 1 ? "one of #{valid_certnames.join(', ')}" : valid_certnames.first

    raise Puppet::Error, "Server hostname '#{http_connection.address}' did not match server certificate; expected #{msg}"
  elsif error.message.empty?
    # This may be because the server is speaking SSLv2 and we
    # monkey patch OpenSSL::SSL:SSLContext to reject SSLv2.
    raise error.exception("#{error.class} with no message")
  else
    raise
  end
end

#network(request) ⇒ Object



40
41
42
# File 'lib/puppet/indirector/rest.rb', line 40

def network(request)
  Puppet::Network::HttpPool.http_instance(request.server || self.class.server, request.port || self.class.port)
end

#save(request) ⇒ Object

Raises:

  • (ArgumentError)


141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/puppet/indirector/rest.rb', line 141

def save(request)
  raise ArgumentError, "PUT does not accept options" unless request.options.empty?

  response = http_put(request, indirection2uri(request), request.instance.render, headers.merge({ "Content-Type" => request.instance.mime }))

  if is_http_200?(response)
    content_type, body = parse_response(response)
    deserialize_save(content_type, body)
  else
    nil
  end
end

#search(request) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/puppet/indirector/rest.rb', line 117

def search(request)
  response = http_get(request, indirection2uri(request), headers)

  if is_http_200?(response)
    content_type, body = parse_response(response)
    deserialize_search(content_type, body) || []
  else
    []
  end
end

#validate_key(request) ⇒ Object



154
155
156
# File 'lib/puppet/indirector/rest.rb', line 154

def validate_key(request)
  # Validation happens on the remote end
end