diff options
Diffstat (limited to 'certs')
27 files changed, 326 insertions, 0 deletions
diff --git a/certs/Gemfile b/certs/Gemfile new file mode 100644 index 0000000..9028024 --- /dev/null +++ b/certs/Gemfile @@ -0,0 +1,17 @@ +source "http://rubygems.org" + +# Declare your gem's dependencies in leap_web_certs.gemspec. +# Bundler will treat runtime dependencies like base dependencies, and +# development dependencies will be added by default to the :development group. +gemspec + +# jquery-rails is used by the dummy application +gem "jquery-rails" + +# Declare any dependencies that are still in development here instead of in +# your gemspec. These might include edge Rails or gems from your path or +# Git. Remember to move these dependencies to your gemspec before releasing +# your gem to rubygems.org. + +# To use debugger +# gem 'ruby-debug' diff --git a/certs/Rakefile b/certs/Rakefile new file mode 100644 index 0000000..dd38158 --- /dev/null +++ b/certs/Rakefile @@ -0,0 +1,38 @@ +#!/usr/bin/env rake +begin + require 'bundler/setup' +rescue LoadError + puts 'You must `gem install bundler` and `bundle install` to run rake tasks' +end +begin + require 'rdoc/task' +rescue LoadError + require 'rdoc/rdoc' + require 'rake/rdoctask' + RDoc::Task = Rake::RDocTask +end + +RDoc::Task.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'LeapWebCerts' + rdoc.options << '--line-numbers' + rdoc.rdoc_files.include('README.rdoc') + rdoc.rdoc_files.include('lib/**/*.rb') +end + + + + +Bundler::GemHelper.install_tasks + +require 'rake/testtask' + +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.libs << 'test' + t.pattern = 'test/**/*_test.rb' + t.verbose = false +end + + +task :default => :test diff --git a/certs/Readme.md b/certs/Readme.md new file mode 100644 index 0000000..4ea8d9d --- /dev/null +++ b/certs/Readme.md @@ -0,0 +1,9 @@ +LeapWebCerts +========= + + +Configuration +------------- + + +Currently LeapWebCerts falls back to handing out a cert in /config/cert if the cert pool is empty. You need to add that file in the application that includes this engine. diff --git a/certs/app/assets/images/leap_web_certs/.gitkeep b/certs/app/assets/images/leap_web_certs/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/certs/app/assets/images/leap_web_certs/.gitkeep diff --git a/certs/app/assets/javascripts/leap_web_certs/.gitkeep b/certs/app/assets/javascripts/leap_web_certs/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/certs/app/assets/javascripts/leap_web_certs/.gitkeep diff --git a/certs/app/assets/stylesheets/leap_web_certs/.gitkeep b/certs/app/assets/stylesheets/leap_web_certs/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/certs/app/assets/stylesheets/leap_web_certs/.gitkeep diff --git a/certs/app/controllers/.gitkeep b/certs/app/controllers/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/certs/app/controllers/.gitkeep diff --git a/certs/app/controllers/certs_controller.rb b/certs/app/controllers/certs_controller.rb new file mode 100644 index 0000000..6988a38 --- /dev/null +++ b/certs/app/controllers/certs_controller.rb @@ -0,0 +1,9 @@ +class CertsController < ApplicationController + + # GET /cert + def show + @cert = Cert.pick_from_pool + render :text => @cert.zipped, :content_type => 'text/plain' + end + +end diff --git a/certs/app/helpers/.gitkeep b/certs/app/helpers/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/certs/app/helpers/.gitkeep diff --git a/certs/app/helpers/certs_helper.rb b/certs/app/helpers/certs_helper.rb new file mode 100644 index 0000000..94e76b8 --- /dev/null +++ b/certs/app/helpers/certs_helper.rb @@ -0,0 +1,2 @@ +module CertsHelper +end diff --git a/certs/app/mailers/.gitkeep b/certs/app/mailers/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/certs/app/mailers/.gitkeep diff --git a/certs/app/models/.gitkeep b/certs/app/models/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/certs/app/models/.gitkeep diff --git a/certs/app/models/cert.rb b/certs/app/models/cert.rb new file mode 100644 index 0000000..40efde9 --- /dev/null +++ b/certs/app/models/cert.rb @@ -0,0 +1,57 @@ +class Cert < CouchRest::Model::Base + + use_database 'certs' + + timestamps! + + property :random, Float, :accessible => false + + before_validation :set_random, :attach_zip, :on => :create + + validates :random, :presence => true, + :numericality => {:greater_than => 0, :less_than => 1} + + validates :zip_attachment, :presence => true + + design do + view :by_random + end + + class << self + def sample + self.by_random.startkey(rand).first || self.by_random.first + end + + def pick_from_pool + cert = self.sample || self.create! + cert.destroy + return cert + rescue RESOURCE_NOT_FOUND + retry if Cert.by_random.count > 0 + raise RECORD_NOT_FOUND + end + + end + + def set_random + self.random = rand + end + + def attach_zip + file = File.open(Rails.root.join("config", "cert")) + self.create_attachment :file => file, :name => zipname + end + + def zipname + 'cert.txt' + end + + def zip_attachment + attachments[zipname] + end + + def zipped + read_attachment(zipname) + end + +end diff --git a/certs/app/views/.gitkeep b/certs/app/views/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/certs/app/views/.gitkeep diff --git a/certs/config/routes.rb b/certs/config/routes.rb new file mode 100644 index 0000000..7f1f31d --- /dev/null +++ b/certs/config/routes.rb @@ -0,0 +1,3 @@ +Rails.application.routes.draw do + resource :cert, :only => [:show] +end diff --git a/certs/leap_web_certs.gemspec b/certs/leap_web_certs.gemspec new file mode 100644 index 0000000..28ef392 --- /dev/null +++ b/certs/leap_web_certs.gemspec @@ -0,0 +1,24 @@ +$:.push File.expand_path("../lib", __FILE__) + +# Maintain your gem's version: +require "leap_web_certs/version" + +# Describe your gem and declare its dependencies: +Gem::Specification.new do |s| + s.name = "leap_web_certs" + s.version = LeapWebCerts::VERSION + s.authors = ["Azul"] + s.email = ["azul@leap.se"] + s.homepage = "http://www.leap.se" + s.summary = "Cert distribution for the leap platform" + s.description = "This plugin for the leap platform distributes certs for the EIP client. It fetches the certs from a pool in CouchDB that is filled by leap-ca." + + s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "Readme.md"] + s.test_files = Dir["test/**/*"] + + s.add_dependency "rails", "~> 3.2.8" + s.add_dependency "leap_web_core", "~> 0.0.1" + + s.add_development_dependency "mocha" + +end diff --git a/certs/lib/leap_web_certs.rb b/certs/lib/leap_web_certs.rb new file mode 100644 index 0000000..beb683d --- /dev/null +++ b/certs/lib/leap_web_certs.rb @@ -0,0 +1,4 @@ +require "leap_web_certs/engine" + +module LeapWebCerts +end diff --git a/certs/lib/leap_web_certs/engine.rb b/certs/lib/leap_web_certs/engine.rb new file mode 100644 index 0000000..7dbc572 --- /dev/null +++ b/certs/lib/leap_web_certs/engine.rb @@ -0,0 +1,8 @@ +# thou shall require all your dependencies in an engine. +require "leap_web_core" + +module LeapWebCerts + class Engine < ::Rails::Engine + + end +end diff --git a/certs/lib/leap_web_certs/version.rb b/certs/lib/leap_web_certs/version.rb new file mode 100644 index 0000000..800ca0b --- /dev/null +++ b/certs/lib/leap_web_certs/version.rb @@ -0,0 +1,3 @@ +module LeapWebCerts + VERSION = "0.0.4" +end diff --git a/certs/lib/tasks/leap_web_certs_tasks.rake b/certs/lib/tasks/leap_web_certs_tasks.rake new file mode 100644 index 0000000..e8fb7ff --- /dev/null +++ b/certs/lib/tasks/leap_web_certs_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :leap_web_certs do +# # Task goes here +# end diff --git a/certs/script/rails b/certs/script/rails new file mode 100755 index 0000000..616d3c9 --- /dev/null +++ b/certs/script/rails @@ -0,0 +1,8 @@ +#!/usr/bin/env ruby1.8 +# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. + +ENGINE_ROOT = File.expand_path('../..', __FILE__) +ENGINE_PATH = File.expand_path('../../lib/leap_web_certs/engine', __FILE__) + +require 'rails/all' +require 'rails/engine/commands' diff --git a/certs/test/functional/certs_controller_test.rb b/certs/test/functional/certs_controller_test.rb new file mode 100644 index 0000000..04669f5 --- /dev/null +++ b/certs/test/functional/certs_controller_test.rb @@ -0,0 +1,15 @@ +require 'test_helper' + +class CertsControllerTest < ActionController::TestCase + setup do + end + + test "should send cert" do + cert = stub :zipped => "adsf", :zipname => "cert_stub.zip" + Cert.expects(:pick_from_pool).returns(cert) + get :show + assert_response :success + assert_equal cert.zipped, @response.body + assert_attachement_filename "cert_stub.zip" + end +end diff --git a/certs/test/integration/navigation_test.rb b/certs/test/integration/navigation_test.rb new file mode 100644 index 0000000..eec8c0e --- /dev/null +++ b/certs/test/integration/navigation_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class NavigationTest < ActionDispatch::IntegrationTest + + # test "the truth" do + # assert true + # end +end + diff --git a/certs/test/leap_web_certs_test.rb b/certs/test/leap_web_certs_test.rb new file mode 100644 index 0000000..ee2058b --- /dev/null +++ b/certs/test/leap_web_certs_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class LeapWebCertsTest < ActiveSupport::TestCase + test "truth" do + assert_kind_of Module, LeapWebCerts + end +end diff --git a/certs/test/test_helper.rb b/certs/test/test_helper.rb new file mode 100644 index 0000000..b268c51 --- /dev/null +++ b/certs/test/test_helper.rb @@ -0,0 +1,10 @@ +ENV["RAILS_ENV"] = "test" +require File.expand_path('../dummy/config/environment', __FILE__) +require 'rails/test_help' +require 'mocha' + +Rails.backtrace_cleaner.remove_silencers! + +# Load support files +Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } + diff --git a/certs/test/unit/cert_pool_test.rb b/certs/test/unit/cert_pool_test.rb new file mode 100644 index 0000000..24ace57 --- /dev/null +++ b/certs/test/unit/cert_pool_test.rb @@ -0,0 +1,51 @@ +require 'test_helper' + +class CertPoolTest < ActiveSupport::TestCase + + setup do + 2.times { Cert.create! } + end + + teardown do + Cert.all.each {|c| c.destroy} + end + + test "picks random sample" do + Cert.create! # with 3 certs chances are pretty low we pick the same one 40 times. + picked = [] + first = Cert.sample.id + current = Cert.sample.id + 40.times do + break if current != first + current = Cert.sample.id + end + assert_not_equal current, first + end + + test "picks cert from the pool" do + assert_difference "Cert.count", -1 do + cert = Cert.pick_from_pool + end + end + + test "err's out if all certs have been destroyed" do + sample = Cert.first.tap{|c| c.destroy} + Cert.all.each {|c| c.destroy} + assert_raises RECORD_NOT_FOUND do + Cert.expects(:sample).returns(sample) + cert = Cert.pick_from_pool + end + end + + test "picks other cert if first pick has been destroyed" do + first = Cert.first.tap{|c| c.destroy} + second = Cert.first + Cert.expects(:sample).at_least_once. + returns(first). + then.returns(second) + cert = Cert.pick_from_pool + assert_equal second, cert + assert_nil Cert.first + end + +end diff --git a/certs/test/unit/cert_test.rb b/certs/test/unit/cert_test.rb new file mode 100644 index 0000000..21ef169 --- /dev/null +++ b/certs/test/unit/cert_test.rb @@ -0,0 +1,48 @@ +require 'test_helper' + +class CertTest < ActiveSupport::TestCase + + setup do + @sample = Cert.new + @sample.set_random + @sample.attach_zip + end + + test "certs come with attachments" do + assert @sample.has_attachment? "cert.txt" + end + + test "cert.zip_attachment returns couchDB attachment" do + assert_equal "text/plain", @sample.zip_attachment["content_type"] + end + + test "cert.zipped returns the actual data" do + @sample.save # This is required! + assert lines = @sample.zipped.split("\n") + assert_equal "-----BEGIN RSA PRIVATE KEY-----", lines.first.chomp + assert_equal "-----END CERTIFICATE-----", lines.last.chomp + end + + test "cert.zipname returns name for the zip file" do + assert_equal "cert.txt", @sample.zipname + end + + test "test data is valid" do + assert @sample.valid? + end + + test "validates random" do + @sample.stubs(:set_random) + [0, 1, nil, "asdf"].each do |invalid| + @sample.random = invalid + assert !@sample.valid?, "#{invalid} should not be a valid value for random" + end + end + + test "validates attachment" do + @sample.stubs(:attach_zip) + @sample.delete_attachment(@sample.zipname) + assert !@sample.valid?, "Cert should require zipped attachment" + end + +end |