Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
* Added tests for DELETE
* Renamed to PSigner internally
* Removed undeeded configuration sets
* Changed api to /api/cert globally
  • Loading branch information
jamtur01 committed Jul 16, 2012
1 parent 46bd381 commit dd722ca
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 33 deletions.
6 changes: 3 additions & 3 deletions bin/psigner
Expand Up @@ -12,13 +12,13 @@ options = {}
optparse = OptionParser.new do |opts| optparse = OptionParser.new do |opts|
# Set a banner, displayed at the top # Set a banner, displayed at the top
# of the help screen. # of the help screen.
opts.banner = "Usage: Psigner [options] ..." opts.banner = "Usage: PSigner [options] ..."


opts.separator '' opts.separator ''
opts.separator "Configuration options:" opts.separator "Configuration options:"


opts.on_tail( "-v", "--version", "Show version") do opts.on_tail( "-v", "--version", "Show version") do
puts "Psigner version #{Psigner::VERSION}" puts "PSigner version #{PSigner::VERSION}"
exit exit
end end


Expand All @@ -33,7 +33,7 @@ end


begin begin
optparse.parse! optparse.parse!
Psigner::Application.run! PSigner::Application.run!
rescue OptionParser::InvalidArgument, OptionParser::InvalidOption, OptionParser::MissingArgument rescue OptionParser::InvalidArgument, OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s puts $!.to_s
puts optparse puts optparse
Expand Down
2 changes: 1 addition & 1 deletion config.ru
@@ -1,3 +1,3 @@
require './lib/psigner/app.rb' require './lib/psigner/app.rb'


run Psigner::Application run PSigner::Application
26 changes: 11 additions & 15 deletions lib/psigner/app.rb
Expand Up @@ -6,29 +6,29 @@ def load_configuration(file, name)
puts "There's no configuration file at #{file}!" puts "There's no configuration file at #{file}!"
exit! exit!
end end
Psigner.const_set(name, YAML.load_file(file)) PSigner.const_set(name, YAML.load_file(file))
end end


module Psigner module PSigner
class Application < Sinatra::Base class Application < Sinatra::Base


configure do configure do
load_configuration("config/config.yml", "APP_CONFIG") load_configuration("config/config.yml", "APP_CONFIG")
end end


set :public_folder, File.join(File.dirname(__FILE__), 'public')
set :views, File.join(File.dirname(__FILE__), 'views')

# Sign certificates /api/sign?host=hostname.to.be.signed?secret=sharedsecret # Sign certificates /api/sign?host=hostname.to.be.signed?secret=sharedsecret
post '/api/sign' do post '/api/cert' do
authenticated_only! authenticated_only!

requires_param :certname requires_param :certname


sign_cert(params[:certname]) success, output = sign_cert(params[:certname])
unless success
halt 500, {'Content-Type' => 'text/plain'}, output
end
"OK"
end end


get '/api/sign' do get '/api/cert' do
'You need to POST API signing requests' 'You need to POST API signing requests'
end end


Expand Down Expand Up @@ -62,12 +62,8 @@ def requires_params(*needed)
alias :requires_param :requires_params alias :requires_param :requires_params


def sign_cert(certname) def sign_cert(certname)
begin stdout = `puppet cert sign #{certname}`
signed = `puppet certificate --ca-location local --mode master sign #{certname}` [$?.exitstatus == 0, stdout]
rescue => e
return "Signing failed because: #{e}"
end
signed
end end


def clean_cert(certname) def clean_cert(certname)
Expand Down
2 changes: 1 addition & 1 deletion lib/version.rb
@@ -1,3 +1,3 @@
module Psigner module PSigner
VERSION = "0.0.1" VERSION = "0.0.1"
end end
2 changes: 1 addition & 1 deletion psigner.gemspec
Expand Up @@ -4,7 +4,7 @@ require "version"


Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "psigner" s.name = "psigner"
s.version = Psigner::VERSION s.version = PSigner::VERSION
s.authors = ["James Turnbull"] s.authors = ["James Turnbull"]
s.email = ["james@lovedthanlost.net"] s.email = ["james@lovedthanlost.net"]
s.homepage = "" s.homepage = ""
Expand Down
47 changes: 36 additions & 11 deletions spec/psigner_spec.rb
@@ -1,33 +1,58 @@
require 'spec_helper' require 'spec_helper'


describe Psigner::Application do describe PSigner::Application do


describe "GET '/api/sign'" do describe "GET '/api/cert'" do
it "should fail" do it "should fail" do
get '/' get '/'
last_response.should_not be_ok last_response.should_not be_ok
end end
end end


describe "POST '/api/sign'" do describe "POST '/api/cert'" do
it "should fail to get the API signing page without parameters" do it "should fail to sign the cert without parameters" do
post '/api/sign' post '/api/cert'
last_response.status.should == 400 last_response.status.should == 401
end end


it "should fail to get the API signing page with only one parameter" do it "should fail to sign via the API with only one parameter" do
post '/api/sign', params = { "secret" => "SHAREDSECRET" } post '/api/cert', params = { "secret" => "SHAREDSECRET" }
last_response.status.should == 400 last_response.status.should == 400
end end


it "should get the API signing page" do it "should fail with incorrect shared secret" do
post '/api/sign', params = { "secret" => "SHAREDSECRET", "certname" => "bob" } post '/api/cert', params = { "secret" => "NOSHAREDSECRET", "certname" => "bob" }
last_response.status.should == 401
end

it "should sign via the API with correct parameters" do
post '/api/cert', params = { "secret" => "SHAREDSECRET", "certname" => "bob" }
last_response.status.should == 200 last_response.status.should == 200
end end


end

describe "DELETE '/api/cert'" do
it "should fail to delete the cert without parameters" do
delete '/api/cert'
last_response.status.should == 401
end

it "should fail to delete the cert with only one parameter" do
delete '/api/cert', params = { "secret" => "SHAREDSECRET" }
last_response.status.should == 400
end

it "should fail with incorrect shared secret" do it "should fail with incorrect shared secret" do
post '/api/sign', params = { "secret" => "NOSHAREDSECRET", "certname" => "bob" } delete '/api/cert', params = { "secret" => "NOSHAREDSECRET", "certname" => "bob" }
last_response.status.should == 401 last_response.status.should == 401
end end

it "should delete via the API with correct parameters" do
delete '/api/cert', params = { "secret" => "SHAREDSECRET", "certname" => "bob" }
last_response.status.should == 200
end


end end
end end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -18,7 +18,7 @@
ENV['RACK_ENV'] = "test" ENV['RACK_ENV'] = "test"


def app def app
@app ||= Psigner::Application @app ||= PSigner::Application
end end


# quick convenience methods.. # quick convenience methods..
Expand Down

0 comments on commit dd722ca

Please sign in to comment.