Commit aef782888f58ecc4ccc130120e8ff93add3ffa98

Authored by Dmitriy Zaporozhets
2 parents 1284f21c 4b9c28bc

Merge pull request #5261 from devaroop/retrieve_ssh_keys_by_ssh

ssh keys publically available for sysadmins via http, the github way
1 v 6.6.0 1 v 6.6.0
  2 + - Retrieving user ssh keys publically(github style): http://__HOST__/__USERNAME__.keys
2 - Permissions: Developer now can manage issue tracker (modify any issue) 3 - Permissions: Developer now can manage issue tracker (modify any issue)
3 - Improve Code Compare page performance 4 - Improve Code Compare page performance
4 - Group avatar 5 - Group avatar
app/controllers/profiles/keys_controller.rb
1 class Profiles::KeysController < ApplicationController 1 class Profiles::KeysController < ApplicationController
2 layout "profile" 2 layout "profile"
  3 + skip_before_filter :authenticate_user!, only: [:get_keys]
3 4
4 def index 5 def index
5 @keys = current_user.keys.order('id DESC') 6 @keys = current_user.keys.order('id DESC')
@@ -32,4 +33,24 @@ class Profiles::KeysController &lt; ApplicationController @@ -32,4 +33,24 @@ class Profiles::KeysController &lt; ApplicationController
32 format.js { render nothing: true } 33 format.js { render nothing: true }
33 end 34 end
34 end 35 end
  36 +
  37 + # Get all keys of a user(params[:username]) in a text format
  38 + # Helpful for sysadmins to put in respective servers
  39 + def get_keys
  40 + if params[:username].present?
  41 + begin
  42 + user = User.find_by_username(params[:username])
  43 + if user.present?
  44 + render text: user.all_ssh_keys.join('\n')
  45 + else
  46 + render_404 and return
  47 + end
  48 + rescue => e
  49 + render text: e.message
  50 + end
  51 + else
  52 + render_404 and return
  53 + end
  54 + end
  55 +
35 end 56 end
app/models/user.rb
@@ -435,4 +435,8 @@ class User &lt; ActiveRecord::Base @@ -435,4 +435,8 @@ class User &lt; ActiveRecord::Base
435 def short_website_url 435 def short_website_url
436 website_url.gsub(/https?:\/\//, '') 436 website_url.gsub(/https?:\/\//, '')
437 end 437 end
  438 +
  439 + def all_ssh_keys
  440 + keys.map(&:key)
  441 + end
438 end 442 end
config/routes.rb
@@ -12,6 +12,9 @@ Gitlab::Application.routes.draw do @@ -12,6 +12,9 @@ Gitlab::Application.routes.draw do
12 API::API.logger Rails.logger 12 API::API.logger Rails.logger
13 mount API::API => '/api' 13 mount API::API => '/api'
14 14
  15 + # Get all keys of user
  16 + get ':username.keys' => 'profiles/keys#get_keys' , constraints: { username: /.*/ }
  17 +
15 constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? } 18 constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? }
16 constraints constraint do 19 constraints constraint do
17 mount Sidekiq::Web, at: "/admin/sidekiq", as: :sidekiq 20 mount Sidekiq::Web, at: "/admin/sidekiq", as: :sidekiq
spec/models/user_spec.rb
@@ -303,6 +303,17 @@ describe User do @@ -303,6 +303,17 @@ describe User do
303 end 303 end
304 end 304 end
305 305
  306 + describe 'all_ssh_keys' do
  307 + it { should have_many(:keys).dependent(:destroy) }
  308 +
  309 + it "should have all ssh keys" do
  310 + user = create :user
  311 + key = create :key, key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD33bWLBxu48Sev9Fert1yzEO4WGcWglWF7K/AwblIUFselOt/QdOL9DSjpQGxLagO1s9wl53STIO8qGS4Ms0EJZyIXOEFMjFJ5xmjSy+S37By4sG7SsltQEHMxtbtFOaW5LV2wCrX+rUsRNqLMamZjgjcPO0/EgGCXIGMAYW4O7cwGZdXWYIhQ1Vwy+CsVMDdPkPgBXqK7nR/ey8KMs8ho5fMNgB5hBw/AL9fNGhRw3QTD6Q12Nkhl4VZES2EsZqlpNnJttnPdp847DUsT6yuLRlfiQfz5Cn9ysHFdXObMN5VYIiPFwHeYCZp1X2S4fDZooRE8uOLTfxWHPXwrhqSH", user_id: user.id
  312 +
  313 + user.all_ssh_keys.should include(key.key)
  314 + end
  315 + end
  316 +
306 describe :avatar_type do 317 describe :avatar_type do
307 let(:user) { create(:user) } 318 let(:user) { create(:user) }
308 319
spec/routing/routing_spec.rb
@@ -176,6 +176,11 @@ describe Profiles::KeysController, &quot;routing&quot; do @@ -176,6 +176,11 @@ describe Profiles::KeysController, &quot;routing&quot; do
176 it "to #destroy" do 176 it "to #destroy" do
177 delete("/profile/keys/1").should route_to('profiles/keys#destroy', id: '1') 177 delete("/profile/keys/1").should route_to('profiles/keys#destroy', id: '1')
178 end 178 end
  179 +
  180 + # get all the ssh-keys of a user
  181 + it "to #get_keys" do
  182 + get("/foo.keys").should route_to('profiles/keys#get_keys', username: 'foo')
  183 + end
179 end 184 end
180 185
181 # profile_avatar DELETE /profile/avatar(.:format) profiles/avatars#destroy 186 # profile_avatar DELETE /profile/avatar(.:format) profiles/avatars#destroy