summaryrefslogtreecommitdiffstats
path: root/lib/puppet/network/http/rack/rest.rb
blob: 602927a78cbf1e5f256e5353ace6574d09c7acec (plain)
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
require 'puppet/network/http/handler'
require 'puppet/network/http/rack/httphandler'

class Puppet::Network::HTTP::RackREST < Puppet::Network::HTTP::RackHttpHandler

  include Puppet::Network::HTTP::Handler

  HEADER_ACCEPT = 'HTTP_ACCEPT'.freeze
  ContentType = 'Content-Type'.freeze

  CHUNK_SIZE = 8192

  class RackFile
    def initialize(file)
      @file = file
    end

    def each
      while chunk = @file.read(CHUNK_SIZE)
        yield chunk
      end
    end

    def close
      @file.close
    end
  end

  def initialize(args={})
    super()
    initialize_for_puppet(args)
  end

  def set_content_type(response, format)
    response[ContentType] = format_to_mime(format)
  end

  # produce the body of the response
  def set_response(response, result, status = 200)
    response.status = status
    unless result.is_a?(File)
      response.write result
    else
      response["Content-Length"] = result.stat.size.to_s
      response.body = RackFile.new(result)
    end
  end

  # Retrieve the accept header from the http request.
  def accept_header(request)
    request.env[HEADER_ACCEPT]
  end

  # Retrieve the accept header from the http request.
  def content_type_header(request)
    request.content_type
  end

  # Return which HTTP verb was used in this request.
  def http_method(request)
    request.request_method
  end

  # Return the query params for this request.
  def params(request)
    result = decode_params(request.params)
    result.merge(extract_client_info(request))
  end

  # what path was requested? (this is, without any query parameters)
  def path(request)
    request.path
  end

  # return the request body
  # request.body has some limitiations, so we need to concat it back
  # into a regular string, which is something puppet can use.
  def body(request)
    request.body.read
  end

  def extract_client_info(request)
    result = {}
    result[:ip] = request.ip

    # if we find SSL info in the headers, use them to get a hostname.
    # try this with :ssl_client_header, which defaults should work for
    # Apache with StdEnvVars.
    if dn = request.env[Puppet[:ssl_client_header]] and dn_matchdata = dn.match(/^.*?CN\s*=\s*(.*)/)
      result[:node] = dn_matchdata[1].to_str
      result[:authenticated] = (request.env[Puppet[:ssl_client_verify_header]] == 'SUCCESS')
    else
      result[:node] = resolve_node(result)
      result[:authenticated] = false
    end

    result
  end

end