Skip to content

Instantly share code, notes, and snippets.

@rcrowley
Created Sep 29, 2010
Embed
What would you like to do?
Puppet master Rack middleware
$0 = "master"
ARGV << "--rack"
ARGV << "--certname=#{File.read("/etc/puppet/certname").chomp}"
require 'puppet/application/master'
require 'base64'
require 'json'
require 'rack/utils'
require 'yaml'
require 'zlib'
class StuckInTheMiddleWithYou
def initialize(app)
@app = app
end
def call(env)
params = Rack::Utils.parse_query(env["QUERY_STRING"], "&")
facts = case params["facts_format"]
when "b64_zlib_yaml"
YAML.load(Zlib::Inflate.inflate(Base64.decode64(
Rack::Utils.unescape(params["facts"]))))
end
# Change facts.
if Puppet::Node::Facts === facts
facts.values["foo"] = "bar"
end
params["facts"] = case params["facts_format"]
when "b64_zlib_yaml"
Rack::Utils.escape(Base64.encode64(Zlib::Deflate.deflate(
YAML.dump(facts), Zlib::BEST_COMPRESSION)))
end if facts
env["QUERY_STRING"] = Rack::Utils.build_query(params)
env["REQUEST_URI"] =
"#{env["PATH_INFO"]}?#{env["QUERY_STRING"]}"
status, headers, body = @app.call(env)
object = case headers["Content-Type"]
when /[\/-]pson$/ then JSON.parse(body.body.join)
when /[\/-]yaml$/ then YAML.load(body.body.join)
when "text/marshal" then Marshal.load(body.body.join)
else body.body.join
end
# Change catalog.
if Hash === object && "Catalog" == object["document_type"]
object["data"]["resources"].unshift({
"exported" => false,
"title" => "apt-get update",
"parameters" => {"path"=>"/usr/sbin:/usr/bin:/sbin:/bin"},
"type" => "Exec",
})
end
body = case headers["Content-Type"]
when /[\/-]pson$/ then [JSON.generate(object)]
when /[\/-]yaml$/ then [YAML.dump(object)]
when "text/marshal" then [Marshal.dump(object)]
else [object]
end
headers["Content-Length"] = Rack::Utils.bytesize(body.first)
[status, headers, body]
end
end
use StuckInTheMiddleWithYou
run Puppet::Application[:master].run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment