From bf46209cefa5d09041865e52f9f78721b10e7dd0 Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 14 Dec 2012 12:53:39 +0100 Subject: adopt certs to changes in the leap ca --- certs/app/models/leap_ca/cert.rb | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 certs/app/models/leap_ca/cert.rb (limited to 'certs/app/models/leap_ca') diff --git a/certs/app/models/leap_ca/cert.rb b/certs/app/models/leap_ca/cert.rb new file mode 100644 index 0000000..0c32721 --- /dev/null +++ b/certs/app/models/leap_ca/cert.rb @@ -0,0 +1,46 @@ +# +# Model for certificates stored in CouchDB. +# +# This file must be loaded after Config has been loaded. +# + +module LeapCA + class Cert < CouchRest::Model::Base + +# No config yet. use_database LeapCA::Config.db_name + use_database 'client_certificates' + + timestamps! + + property :key, String # the client private RSA key + property :cert, String # the client x509 certificate, signed by the CA + property :valid_until, Time # expiration time of the client certificate + property :random, Float, :accessible => false # used to help pick a random cert by the webapp + + validates :key, :presence => true + validates :cert, :presence => true + validates :random, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 1} + + 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 + raise RECORD_NOT_FOUND unless cert + cert.destroy + return cert + rescue RESOURCE_NOT_FOUND + retry if self.by_random.count > 0 + raise RECORD_NOT_FOUND + end + + end + + end +end -- cgit v1.2.3 From 1eeec0808886e305d5113a20bf6ea5c1921e633b Mon Sep 17 00:00:00 2001 From: Azul Date: Fri, 14 Dec 2012 14:33:04 +0100 Subject: fixed most of the unit tests no idea why the numericality validatoin with greater_than_or_equal 0 does not catch negative numbers --- certs/app/models/leap_ca/cert.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'certs/app/models/leap_ca') diff --git a/certs/app/models/leap_ca/cert.rb b/certs/app/models/leap_ca/cert.rb index 0c32721..7e4b49a 100644 --- a/certs/app/models/leap_ca/cert.rb +++ b/certs/app/models/leap_ca/cert.rb @@ -17,6 +17,8 @@ module LeapCA property :valid_until, Time # expiration time of the client certificate property :random, Float, :accessible => false # used to help pick a random cert by the webapp + before_validation :set_random, :on => :create + validates :key, :presence => true validates :cert, :presence => true validates :random, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 1} @@ -25,6 +27,10 @@ module LeapCA view :by_random end + def set_random + self.random = rand + end + class << self def sample self.by_random.startkey(rand).first || self.by_random.first @@ -40,6 +46,9 @@ module LeapCA raise RECORD_NOT_FOUND end + def valid_attributes_hash + {:key => "ABCD", :cert => "A123"} + end end end -- cgit v1.2.3 From a8f5a1ec486d5ee378f7b820c9f2c046e5c03672 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 17 Dec 2012 11:07:42 +0100 Subject: adopted test to before_validation callback The before validation hook will overwrite whatever is in random on create. This is what we want - just need to test it properly --- certs/app/models/leap_ca/cert.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'certs/app/models/leap_ca') diff --git a/certs/app/models/leap_ca/cert.rb b/certs/app/models/leap_ca/cert.rb index 7e4b49a..9d4f15e 100644 --- a/certs/app/models/leap_ca/cert.rb +++ b/certs/app/models/leap_ca/cert.rb @@ -21,7 +21,8 @@ module LeapCA validates :key, :presence => true validates :cert, :presence => true - validates :random, :presence => true, :numericality => {:greater_than_or_equal_to => 0, :less_than => 1} + validates :random, :presence => true + validates :random, :numericality => {:greater_than => 0, :less_than => 1} design do view :by_random -- cgit v1.2.3