Skip to content
Snippets Groups Projects
Unverified Commit a55cc365 authored by azul's avatar azul
Browse files

keys: store type and rev in hash rather than serialized

Since the old keys used to be strings i started out by
json serializing the new keys with type, value, rev.

However storing serialized json in couch (json) does
not really make sense. So now we do not serialize but
instead have one json document. The lookup for a key of
type pgp may still return a string but for everything
that uses the new api it will return a hash with type
and revision.

This data structure is way easier to handle also on the
nickserver side.
parent 0a83272f
Branches
No related tags found
1 merge request!61couch: add identity view to get all keys
Pipeline #
...@@ -5,14 +5,11 @@ class Api::KeysController < ApiController ...@@ -5,14 +5,11 @@ class Api::KeysController < ApiController
# get /keys # get /keys
def index def index
keys = identity.keys.map do |k,v| render json: identity.keys
[k, JSON.parse(v)]
end
render json: keys.to_h
end end
def show def show
render json: JSON.parse(identity.keys[params[:id]]) render json: identity.keys[params[:id]]
end end
def create def create
......
...@@ -112,7 +112,7 @@ module Api ...@@ -112,7 +112,7 @@ module Api
PgpKey.new(key).tap do |key| PgpKey.new(key).tap do |key|
if key.valid? if key.valid?
identity = Identity.for(@user) identity = Identity.for(@user)
identity.set_key(:pgp, key) identity.set_key(:pgp, key.to_s)
identity.save identity.save
end end
end end
......
...@@ -131,9 +131,10 @@ class Identity < CouchRest::Model::Base ...@@ -131,9 +131,10 @@ class Identity < CouchRest::Model::Base
read_attribute('keys') || HashWithIndifferentAccess.new read_attribute('keys') || HashWithIndifferentAccess.new
end end
def set_key(type, key) def set_key(type, key_hash)
return if keys[type] == key.to_s key_hash.stringify_keys! if key_hash.respond_to? :stringify_keys!
write_attribute('keys', keys.merge(type => key.to_s)) return if keys[type] == key_hash
write_attribute('keys', keys.merge(type => key_hash))
end end
def delete_key(type) def delete_key(type)
......
...@@ -20,13 +20,13 @@ class Keyring ...@@ -20,13 +20,13 @@ class Keyring
def create(type, value) def create(type, value)
raise Error, "key already exists" if storage.keys[type].present? raise Error, "key already exists" if storage.keys[type].present?
storage.set_key type, {type: type, value: value, rev: new_rev}.to_json storage.set_key type, {type: type, value: value, rev: new_rev}
storage.save storage.save
end end
def update(type, rev:, value:) def update(type, rev:, value:)
check_rev type, rev check_rev type, rev
storage.set_key type, {type: type, value: value, rev: new_rev}.to_json storage.set_key type, {type: type, value: value, rev: new_rev}
storage.save storage.save
end end
...@@ -37,7 +37,7 @@ class Keyring ...@@ -37,7 +37,7 @@ class Keyring
end end
def key_of_type(type) def key_of_type(type)
JSON.parse(storage.keys[type]) if storage.keys[type] storage.keys[type]
end end
protected protected
...@@ -46,6 +46,9 @@ class Keyring ...@@ -46,6 +46,9 @@ class Keyring
def check_rev(type, rev) def check_rev(type, rev)
old = key_of_type(type) old = key_of_type(type)
raise NotFound, type unless old raise NotFound, type unless old
# We used to store plain strings. It's deprecated now.
# If we happen to run into them do not check their revision.
return if old.is_a? String
raise Error, "wrong revision: #{rev}" unless old['rev'] == rev raise Error, "wrong revision: #{rev}" unless old['rev'] == rev
end end
......
...@@ -16,7 +16,7 @@ Then /^I should have published an? "([^"]*)" key(?: with value "([^"]*)")?$/ do ...@@ -16,7 +16,7 @@ Then /^I should have published an? "([^"]*)" key(?: with value "([^"]*)")?$/ do
identity = Identity.for(@user) identity = Identity.for(@user)
keys = identity.keys keys = identity.keys
assert_includes keys.keys, type assert_includes keys.keys, type
assert_equal value, JSON.parse(keys[type])['value'] if value assert_equal value, keys[type]['value'] if value
end end
Then /^I should not have published an? "([^"]*)" key$/ do |type| Then /^I should not have published an? "([^"]*)" key$/ do |type|
......
...@@ -82,8 +82,8 @@ class KeyringTest < ActiveSupport::TestCase ...@@ -82,8 +82,8 @@ class KeyringTest < ActiveSupport::TestCase
def teststorage def teststorage
@teststorage ||= Hash.new.tap do |dummy| @teststorage ||= Hash.new.tap do |dummy|
def dummy.set_key(type, value) def dummy.set_key(type, hash)
self[type] = value self[type] = hash.stringify_keys
end end
def dummy.keys def dummy.keys
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment