Merge pull request #568 from warpc/441-key_pairs

[refs #441] Add signatures for repositories in platform
This commit is contained in:
Vladimir Sharshov 2012-07-31 06:54:07 -07:00
commit c33aad252b
23 changed files with 488 additions and 54 deletions

View File

@ -0,0 +1,29 @@
class Platforms::KeyPairsController < ApplicationController
before_filter :authenticate_user!
load_and_authorize_resource :platform, :only => [:index]
load_and_authorize_resource :only => [:create, :destroy]
def create
@key_pair.user_id = current_user.id
if @key_pair.save
flash[:notice] = t('flash.key_pairs.saved')
else
flash[:error] = t('flash.key_pairs.save_error')
flash[:warning] = @key_pair.errors.full_messages.join('. ') unless @key_pair.errors.blank?
end
redirect_to platform_key_pairs_path(@key_pair.repository.platform)
end
def destroy
if @key_pair.destroy
flash[:notice] = t('flash.key_pairs.destroyed')
else
flash[:error] = t('flash.key_pairs.destroy_error')
end
redirect_to platform_key_pairs_path(@key_pair.repository.platform)
end
end

View File

@ -97,6 +97,8 @@ class Ability
can(:clear, Platform) {|platform| local_admin?(platform) && platform.personal?}
can([:change_visibility, :settings, :destroy], Repository) {|repository| owner? repository.platform}
can([:create, :destroy], KeyPair) {|key_pair| owner?(key_pair.repository.platform) || local_admin?(key_pair.repository.platform)}
can :read, Product, :platform => {:visibility => 'open'}
can :read, Product, :platform => {:owner_type => 'User', :owner_id => user.id, :platform_type => 'main'}
can :read, Product, :platform => {:owner_type => 'Group', :owner_id => user.group_ids, :platform_type => 'main'}

35
app/models/key_pair.rb Normal file
View File

@ -0,0 +1,35 @@
class KeyPair < ActiveRecord::Base
belongs_to :repository
belongs_to :user
attr_accessor :secret
attr_accessible :public, :secret, :repository_id
validates :repository_id, :public, :user_id, :presence => true
validates :secret, :presence => true, :on => :create
validates :repository_id, :uniqueness => {:message => I18n.t("activerecord.errors.key_pair.repo_key_exists")}
before_create :key_create_call
before_destroy :rm_key_call
protected
def key_create_call
result, self.key_id = BuildServer.import_gpg_key_pair(public, secret)
raise "Failed to create key_pairs for repository #{repository_id} with code #{result}." if result == 4
if result != 0 || self.key_id.nil?
errors.add(:public, I18n.t("activerecord.errors.key_pair.rpc_error_#{result}"))
return false
end
result = BuildServer.set_repository_key(repository.platform.name, repository.name, self.key_id)
raise "Failed to sign repository #{repository.name} in platform #{repository.platform.name}
using key_id #{self.key_id} with code #{result}." unless result.zero?
end
def rm_key_call
result = BuildServer.rm_repository_key(repository.platform.name, repository.name)
raise "Failed to desroy repository key #{repository.name} in platform
#{repository.platform.name} with code #{result}." unless result.zero?
end
end

View File

@ -4,6 +4,7 @@ class Repository < ActiveRecord::Base
has_many :project_to_repositories, :dependent => :destroy, :validate => true
has_many :projects, :through => :project_to_repositories
has_one :key_pair, :dependent => :destroy
validates :description, :presence => true
validates :name, :uniqueness => {:scope => :platform_id, :case_sensitive => false}, :presence => true, :format => {:with => /^[a-z0-9_\-]+$/}

View File

@ -35,6 +35,8 @@ class User < ActiveRecord::Base
has_many :own_groups, :foreign_key => :owner_id, :class_name => 'Group', :dependent => :destroy
has_many :own_platforms, :as => :owner, :class_name => 'Platform', :dependent => :destroy
has_many :key_pairs
validates :uname, :presence => true, :uniqueness => {:case_sensitive => false}, :format => {:with => /^[a-z0-9_]+$/}, :reserved_name => true
validate { errors.add(:uname, :taken) if Group.by_uname(uname).present? }
validates :role, :inclusion => {:in => ROLES}, :allow_blank => true

View File

@ -25,6 +25,9 @@
- if can? :members, @platform
%li{:class => (act == :members && contr == :platforms) ? 'active' : nil}
= link_to t("layout.platforms.members"), members_platform_path(@platform)
- if can? :edit, @platform
%li{:class => (act == :index && contr == :key_pairs) ? 'active' : ''}
= link_to t("layout.key_pairs.header"), platform_key_pairs_path(@platform)
-#- if current_user.owner_of? @platform or current_user.admin?
%li{:class => (act == :index && contr == :private_users) ? 'active' : ''}
= link_to t("layout.platforms.private_users"), platform_private_users_path(@platform)

View File

@ -0,0 +1,18 @@
%table#myTable.tablesorter.platform-repos{:cellspacing => "0", :cellpadding => "0"}
%thead
%tr
%th.th1= t("activerecord.attributes.key_pair.repository_id")
%th.th2= t("activerecord.attributes.key_pair.key_id")
%th.th3= t("activerecord.attributes.key_pair.user_id")
%th= t("layout.delete")
%tbody
- @platform.repositories.each do |repository|
- if repository.key_pair
%tr{:class => cycle("odd", "even")}
%td= repository.name
%td= repository.key_pair.key_id
%td= link_to repository.key_pair.user.fullname, user_path(repository.key_pair.user)
%td.buttons
- if can? :destroy, repository.key_pair
= link_to platform_key_pair_path(@platform, repository.key_pair), :method => :delete, :confirm => t("layout.key_pairs.confirm_delete") do
%span.delete &nbsp;

View File

@ -0,0 +1,17 @@
= render 'platforms/base/sidebar'
%h3= t("layout.key_pairs.header")
= form_for :key_pair, :url => platform_key_pairs_path(@platform), :method => :post, :html => { :class => :form } do |f|
.leftlist= f.label :public, t("activerecord.attributes.key_pair.public"), :class => :label
.rightlist= f.text_area :public, :class => 'text_field resizable', :cols => 80
.both
.leftlist= f.label :secret, t("activerecord.attributes.key_pair.secret"), :class => :label
.rightlist= f.text_area :secret, :class => 'text_field resizable', :cols => 80
.both
.leftlist= f.label :repository_id, t("activerecord.attributes.key_pair.repository_id"), :class => :label
.rightlist= f.select :repository_id, options_from_collection_for_select(@platform.repositories, 'id', 'name')
.both
.button_block
= submit_tag t("layout.save")

View File

@ -0,0 +1,2 @@
= render 'new' if can? :edit, @platform
= render 'list'

View File

@ -48,7 +48,7 @@ module Rosa
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
config.filter_parameters += [:password, :secret]
# Enable the asset pipeline
config.assets.enabled = true

View File

@ -0,0 +1,35 @@
en:
layout:
key_pairs:
repository_id: Repository
user_id: User
public: Public key
secret: Secret key
confirm_delete: Are you sure you want to delete this signature?
header: Signatures
flash:
key_pairs:
saved: Repository successfully signed
save_error: Signature save error
destroyed: Signature succefully destroyed
destroy_error: Signature destroy error
activerecord:
errors:
key_pair:
repo_key_exists: Repository has been signed already! Please remove old signature and try again
rpc_error_0: an unexpected error
rpc_error_1: could not import public key
rpc_error_2: could not import secret key
rpc_error_3: keys are imported, but it is not a key pair (ids differ)
models:
key_pair: Key Pair
attributes:
key_pair:
id: Id
created_at: Created
updated_at: Updated
user_id: User
repository_id: Repository
public: Public key
secret: Secret key
key_id: Signature

View File

@ -0,0 +1,35 @@
ru:
layout:
key_pairs:
repository_id: Репозиторий
user_id: Пользователь
public: Публичный ключ
secret: Секретный ключ
confirm_delete: Вы уверены, что хотите удалить подпись?
header: Подписи
flash:
key_pairs:
saved: Репозиторий успешно подписан
save_error: Ошибка создания подписи
destroyed: Подпись успешно удалена
destroy_error: Ошибка удаления подписи
activerecord:
errors:
key_pair:
repo_key_exists: Репозиторий уже подписан! Пожалуйста, удалите старую подпись и попробуйте снова
rpc_error_0: Неизвестная ошибка
rpc_error_1: Проблемы с импортром публичного ключа
rpc_error_2: Проблемы с импортром секретного ключа
rpc_error_3: Ключи импортированы, но не являются парой (идентификаторы не совпадают)
models:
key_pair: Подпись
attributes:
key_pair:
id: Id
created_at: Создано
updated_at: Обновлено
user_id: Пользователь
repository_id: Репозиторий
public: Публичный ключ
secret: Секретный ключ
key_id: Подпись

View File

@ -72,6 +72,7 @@ Rosa::Application.routes.draw do
get :projects_list
end
end
resources :key_pairs, :only => [:create, :index, :destroy]
resources :products do
resources :product_build_lists, :only => [:create, :destroy]
end

View File

@ -0,0 +1,11 @@
class CreateKeyPairs < ActiveRecord::Migration
def change
create_table :key_pairs do |t|
t.integer :repository_id
t.integer :user_id
t.integer :key_id
t.string :public
t.timestamps
end
end
end

View File

@ -0,0 +1,9 @@
class SetTextTypeForKeyPairsPublic < ActiveRecord::Migration
def up
change_column :key_pairs, :public, :text
end
def down
change_column :key_pairs, :public, :string
end
end

View File

@ -0,0 +1,9 @@
class SetStringTypeForKeyPairsKeyid < ActiveRecord::Migration
def up
change_column :key_pairs, :key_id, :string
end
def down
change_column :key_pairs, :key_id, :integer
end
end

View File

@ -0,0 +1,17 @@
class DisableNullValueForKeyPairs < ActiveRecord::Migration
def up
change_column_null :key_pairs, :repository_id, false
change_column_null :key_pairs, :user_id, false
change_column_null :key_pairs, :key_id, false
change_column_null :key_pairs, :public, false
add_index :key_pairs, :repository_id, :unique => true
end
def down
change_column_null :key_pairs, :repository_id, true
change_column_null :key_pairs, :user_id, true
change_column_null :key_pairs, :key_id, true
change_column_null :key_pairs, :public, true
remove_index :key_pairs, :repository_id
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120719045806) do
ActiveRecord::Schema.define(:version => 20120730214052) do
create_table "activity_feeds", :force => true do |t|
t.integer "user_id", :null => false
@ -53,8 +53,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
create_table "arches", :force => true do |t|
t.string "name", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "arches", ["name"], :name => "index_arches_on_name", :unique => true
@ -63,8 +63,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.integer "user_id"
t.string "provider"
t.string "uid"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "authentications", ["provider", "uid"], :name => "index_authentications_on_provider_and_uid", :unique => true
@ -75,8 +75,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.integer "level"
t.integer "status"
t.integer "build_list_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "version"
end
@ -106,9 +106,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.string "project_version"
t.integer "project_id"
t.integer "arch_id"
t.datetime "notified_at"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "is_circle", :default => false
t.text "additional_repos"
t.string "name"
@ -137,8 +136,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.string "commentable_type"
t.integer "user_id"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.decimal "commentable_id", :precision => 50, :scale => 0
t.integer "project_id"
end
@ -155,23 +154,25 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.string "controller"
t.string "action"
t.text "message"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "flash_notifies", :force => true do |t|
t.text "body_ru"
t.text "body_en"
t.string "status"
t.boolean "published", :default => true
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "flash_notifies", :force => true do |t|
t.text "body_ru", :null => false
t.text "body_en", :null => false
t.string "status", :null => false
t.boolean "published", :default => true, :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "projects", ["owner_id"], :name => "index_projects_on_name_and_owner_id_and_owner_type", :unique => true, :case_sensitive => false
create_table "groups", :force => true do |t|
t.integer "owner_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "uname"
t.integer "own_projects_count", :default => 0, :null => false
t.text "description"
@ -184,8 +185,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.string "title"
t.text "body"
t.string "status", :default => "open"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
t.datetime "closed_at"
t.integer "closed_by"
@ -194,14 +195,16 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
add_index "issues", ["project_id", "serial_id"], :name => "index_issues_on_project_id_and_serial_id", :unique => true
create_table "key_pairs", :force => true do |t|
t.integer "repository_id"
t.integer "user_id"
t.integer "key_id"
t.string "public"
t.integer "repository_id", :null => false
t.integer "user_id", :null => false
t.string "key_id", :null => false
t.text "public", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "key_pairs", ["repository_id"], :name => "index_key_pairs_on_repository_id", :unique => true
create_table "labelings", :force => true do |t|
t.integer "label_id", :null => false
t.integer "issue_id"
@ -259,16 +262,16 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.integer "platform_id"
t.string "login"
t.string "password"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
end
create_table "product_build_lists", :force => true do |t|
t.integer "product_id"
t.integer "status", :default => 2, :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "product_build_lists", ["product_id"], :name => "index_product_build_lists_on_product_id"
@ -276,8 +279,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
create_table "products", :force => true do |t|
t.string "name", :null => false
t.integer "platform_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "build_script"
t.text "counter"
t.text "ks"
@ -296,8 +299,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.string "name"
t.string "version"
t.datetime "file_mtime"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "platform_id"
end
@ -306,14 +309,14 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
create_table "project_to_repositories", :force => true do |t|
t.integer "project_id"
t.integer "repository_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "projects", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "owner_id"
t.string "owner_type"
t.string "visibility", :default => "open"
@ -351,16 +354,16 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.string "actor_type"
t.integer "target_id"
t.string "target_type"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "role"
end
create_table "repositories", :force => true do |t|
t.string "description", :null => false
t.integer "platform_id", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "name", :null => false
end
@ -371,8 +374,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.boolean "new_comment_reply", :default => true
t.boolean "new_issue", :default => true
t.boolean "issue_assign", :default => true
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "new_comment_commit_owner", :default => true
t.boolean "new_comment_commit_repo_owner", :default => true
t.boolean "new_comment_commit_commentor", :default => true
@ -381,8 +384,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
create_table "subscribes", :force => true do |t|
t.string "subscribeable_type"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.boolean "status", :default => true
t.integer "project_id"
t.decimal "subscribeable_id", :precision => 50, :scale => 0
@ -395,8 +398,8 @@ ActiveRecord::Schema.define(:version => 20120719045806) do
t.string "password_salt", :default => "", :null => false
t.string "reset_password_token"
t.datetime "remember_created_at"
t.datetime "created_at"
t.datetime "updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "uname"
t.string "role"
t.string "language", :default => "en"

View File

@ -87,11 +87,11 @@ class BuildServer
# raise include_repos_hash.inspect
self.client.call('add_build_list', project_name, project_version, plname, arch, bplname, update_type, build_requires, id_web, include_repos_hash, priority)
end
def self.delete_build_list idlist
self.client.call('delete_build_list', idlist)
end
def self.get_status
self.client.call('get_status')
end
@ -99,4 +99,17 @@ class BuildServer
def self.freeze platform_name
self.client.call('freeze_platform', platform_name)
end
# Repository key pair calls
def self.import_gpg_key_pair key_pub, key_secret
self.client.call('import_gpg_key_pair', key_pub, key_secret)
end
def self.set_repository_key platform, repository, key_id
self.client.call('set_repository_key', platform, repository, key_id)
end
def self.rm_repository_key platform, repository
self.client.call('rm_repository_key', platform, repository)
end
end

View File

@ -0,0 +1,155 @@
require 'spec_helper'
def create_key_pair(repository, user)
@key_pair = FactoryGirl.create(:key_pair, :repository => repository, :user => user)
end
shared_examples_for 'key_pair platform owner' do
it 'should be able to perform index action' do
get :index, :platform_id => @platform
response.should render_template(:index)
end
it 'should be able to perform create action' do
post :create, @create_params
response.should redirect_to(platform_key_pairs_path(@platform))
end
it 'should create key pair into db on create action' do
lambda { post :create, @create_params }.should change{KeyPair.count}.by(1)
end
context "on destroy" do
before(:each) do
create_key_pair @repository, @user
end
it 'should be able to perform action' do
delete :destroy, :platform_id => @platform, :id => @key_pair
response.should redirect_to(platform_key_pairs_path(@platform))
end
it 'should delete key pair into db' do
lambda { delete :destroy, :platform_id => @platform, :id => @key_pair }.should change{KeyPair.count}.by(-1)
end
end
end
shared_examples_for 'key_pair platform reader' do
it 'should be able to perform index action' do
get :index, :platform_id => @platform
response.should render_template(:index)
end
it 'should not be able to perform create action' do
post :create, @create_params
response.should redirect_to(forbidden_path)
end
it 'should not change objects count on create success' do
lambda { post :create, @create_params }.should change{ KeyPair.count }.by(0)
end
context "on destroy" do
before(:each) do
create_key_pair @repository, @user
end
it 'should not be able to perform action' do
delete :destroy, :platform_id => @platform, :id => @key_pair
response.should redirect_to(forbidden_path)
end
it 'should not change objects count on destroy success' do
lambda { delete :destroy, :platform_id => @platform, :id => @key_pair }.should change{KeyPair.count}.by(0)
end
end
end
describe Platforms::KeyPairsController do
before(:each) do
stub_symlink_methods
stub_key_pairs_calls
@platform = FactoryGirl.create(:platform)
@repository = FactoryGirl.create(:repository, :platform => @platform)
@user = FactoryGirl.create(:user)
@create_params = {
:platform_id => @platform,
:key_pair => {
:repository_id => @repository,
:public => "iampublic",
:secret => "iamsecret"
}
}
end
context 'for guest' do
[:index, :create].each do |action|
it "should not be able to perform #{ action } action" do
get action, :platform_id => @platform
response.should redirect_to(new_user_session_path)
end
end
it 'should not change objects count on create success' do
lambda { post :create, @create_params }.should change{ KeyPair.count }.by(0)
end
context 'on destroy' do
before(:each) do
create_key_pair @repository, @user
end
it 'should not change objects count on destroy success' do
lambda { delete :destroy, :platform_id => @platform, :id => @key_pair }.should change{KeyPair.count}.by(0)
end
it "should not be able to perform destroy action" do
delete :destroy, :platform_id => @platform, :id => @key_pair
response.should redirect_to(new_user_session_path)
end
end
end
context 'for global admin' do
before(:each) do
@admin = FactoryGirl.create(:admin)
@user = FactoryGirl.create(:user)
set_session_for(@admin)
end
it_should_behave_like 'key_pair platform owner'
end
context 'for owner user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
@platform.update_attribute(:owner, @user)
end
it_should_behave_like 'key_pair platform owner'
end
context 'for admin user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
@platform.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'admin')
end
it_should_behave_like 'key_pair platform owner'
end
context 'for reader user' do
before(:each) do
@user = FactoryGirl.create(:user)
set_session_for(@user)
@platform.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'reader')
end
it_should_behave_like 'key_pair platform reader'
end
end

View File

@ -0,0 +1,10 @@
# -*- encoding : utf-8 -*-
FactoryGirl.define do
factory :key_pair do
association :repository
association :user
public FactoryGirl.generate(:string)
secret FactoryGirl.generate(:string)
end
end

View File

@ -0,0 +1,21 @@
require 'spec_helper'
describe KeyPair do
before(:all) do
stub_symlink_methods
FactoryGirl.create(:key_pair)
end
it { should belong_to(:repository) }
it { should belong_to(:user)}
it { should_not allow_mass_assignment_of(:user) }
it { should_not allow_mass_assignment_of(:key_id) }
after(:all) do
Platform.delete_all
User.delete_all
Product.delete_all
FileUtils.rm_rf(APP_CONFIG['root_path'])
end
end

View File

@ -38,6 +38,12 @@ def stub_symlink_methods
any_instance_of(Platform, :remove_symlink_directory => true)
end
def stub_key_pairs_calls
stub(BuildServer).import_gpg_key_pair { [0,"1a2b3c"] }
stub(BuildServer).set_repository_key { 0 }
stub(BuildServer).rm_repository_key { 0 }
end
def test_git_commit(project)
project.repo.index.add('test', 'TEST')
project.repo.index.commit('Test commit')