Class: R509::Middleware::Certwriter

Inherits:
Object
  • Object
show all
Includes:
Dependo::Mixin
Defined in:
lib/r509/middleware/certwriter.rb,
lib/r509/middleware/certwriter/version.rb

Constant Summary collapse

VERSION =
"0.2"

Instance Method Summary collapse

Constructor Details

#initialize(app, config = nil) ⇒ Certwriter

Returns a new instance of Certwriter.



9
10
11
12
13
14
15
16
17
# File 'lib/r509/middleware/certwriter.rb', line 9

def initialize(app, config=nil)
    @app = app

    unless config
        @config = YAML.load_file("config.yaml")
    else
        @config = config
    end
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/r509/middleware/certwriter.rb', line 19

def call(env)
    status, headers, response = @app.call(env)

    # we only care about issuance, and we just want to pull out the cert and write it to disk
    if not (env["PATH_INFO"] =~ /^\/1\/certificate\/issue\/?$/).nil? and status == 200
        body = ""
        response.each do |part|
            body += part
        end
        begin
            params = parse_params(env)
            cert = R509::Cert.new(:cert => body)
            file_path = @config["certwriter"]["path"]
            filename = File.join(file_path, 
                "#{cert.subject_component("CN")}_#{params["ca"]}_#{cert.serial}.pem").
                gsub("*", "STAR").
                encode(Encoding.find("ASCII"), {:invalid => :replace, :undef => :replace, :replace => "", :universal_newline => true})
            log.info "Writing: #{filename}"
            File.open(filename, "w"){|f| f.write(cert.to_s)}
        rescue => e
            log.error "Writing failed"
            log.error e.inspect
        end
    end

    [status, headers, response]
end