From b6bd4856a33df3d144be66c4ed1f1396009bb08b Mon Sep 17 00:00:00 2001 From: devaroop Date: Wed, 2 Oct 2013 20:39:29 +0530 Subject: [PATCH 1/8] getting user keys publically through http without any authentication, the github way. E.g: http://github.com/devaroop.keys --- app/controllers/profiles/keys_controller.rb | 18 ++++++++++++++++++ app/models/user.rb | 4 ++++ config/routes.rb | 3 +++ 3 files changed, 25 insertions(+) diff --git a/app/controllers/profiles/keys_controller.rb b/app/controllers/profiles/keys_controller.rb index c36dae2abd..2b991957b7 100644 --- a/app/controllers/profiles/keys_controller.rb +++ b/app/controllers/profiles/keys_controller.rb @@ -1,5 +1,6 @@ class Profiles::KeysController < ApplicationController layout "profile" + skip_before_filter :authenticate_user!, only: [:get_keys] def index @keys = current_user.keys.order('id DESC').all @@ -32,4 +33,21 @@ class Profiles::KeysController < ApplicationController format.js { render nothing: true } end end + + #get all keys of a user(params[:username]) in a text format + #helpful for sysadmins to put in respective servers + def get_keys + if params[:username].present? + begin + user = User.find_by_username(params[:username]) + user.present? ? (render :text => user.all_ssh_keys) : + (render_404 and return) + rescue => e + render text: e.message + end + else + render_404 and return + end + end + end diff --git a/app/models/user.rb b/app/models/user.rb index f1f93eadc1..225c97d35f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -391,4 +391,8 @@ class User < ActiveRecord::Base self end + + def all_ssh_keys + keys.collect{|x| x.key}.join("\n") + end end diff --git a/config/routes.rb b/config/routes.rb index 612a7327ec..1d2b4d7373 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,9 @@ Gitlab::Application.routes.draw do API::API.logger Rails.logger mount API::API => '/api' + #get all keys of user + get ':username.keys' => 'profiles/keys#get_keys' , constraints: { username: /.*/ } + constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? } constraints constraint do mount Sidekiq::Web, at: "/admin/sidekiq", as: :sidekiq From b9d58c4cecd06be74c3cc32ccfb522b31544ab2e Mon Sep 17 00:00:00 2001 From: devaroop Date: Wed, 2 Oct 2013 20:39:29 +0530 Subject: [PATCH 2/8] getting user keys publically through http without any authentication, the github way. E.g: http://github.com/devaroop.keys changelog updated to include ssh key retrieval feature update --- CHANGELOG | 1 + app/controllers/profiles/keys_controller.rb | 18 ++++++++++++++++++ app/models/user.rb | 4 ++++ config/routes.rb | 3 +++ 4 files changed, 26 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index c1107717fc..1459ef84ae 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,5 @@ v 6.2.0 + - Retrieving user ssh keys publically(github style): http://__HOST__/__USERNAME__.keys - Public projects are visible from the outside - Add group access to permissions page - Require current password to change one diff --git a/app/controllers/profiles/keys_controller.rb b/app/controllers/profiles/keys_controller.rb index c36dae2abd..2b991957b7 100644 --- a/app/controllers/profiles/keys_controller.rb +++ b/app/controllers/profiles/keys_controller.rb @@ -1,5 +1,6 @@ class Profiles::KeysController < ApplicationController layout "profile" + skip_before_filter :authenticate_user!, only: [:get_keys] def index @keys = current_user.keys.order('id DESC').all @@ -32,4 +33,21 @@ class Profiles::KeysController < ApplicationController format.js { render nothing: true } end end + + #get all keys of a user(params[:username]) in a text format + #helpful for sysadmins to put in respective servers + def get_keys + if params[:username].present? + begin + user = User.find_by_username(params[:username]) + user.present? ? (render :text => user.all_ssh_keys) : + (render_404 and return) + rescue => e + render text: e.message + end + else + render_404 and return + end + end + end diff --git a/app/models/user.rb b/app/models/user.rb index f1f93eadc1..225c97d35f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -391,4 +391,8 @@ class User < ActiveRecord::Base self end + + def all_ssh_keys + keys.collect{|x| x.key}.join("\n") + end end diff --git a/config/routes.rb b/config/routes.rb index 612a7327ec..1d2b4d7373 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,9 @@ Gitlab::Application.routes.draw do API::API.logger Rails.logger mount API::API => '/api' + #get all keys of user + get ':username.keys' => 'profiles/keys#get_keys' , constraints: { username: /.*/ } + constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? } constraints constraint do mount Sidekiq::Web, at: "/admin/sidekiq", as: :sidekiq From 1c9a41e0d5cac3ee937555ae4189ecd1ad597004 Mon Sep 17 00:00:00 2001 From: GitLab Date: Thu, 6 Feb 2014 14:42:59 +0530 Subject: [PATCH 3/8] adding tests for the ssh keys feature --- app/controllers/profiles/keys_controller.rb | 2 +- app/models/user.rb | 2 +- spec/models/user_spec.rb | 12 ++++++++++++ spec/routing/routing_spec.rb | 5 +++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/controllers/profiles/keys_controller.rb b/app/controllers/profiles/keys_controller.rb index 2b991957b7..3bb1b0c2f2 100644 --- a/app/controllers/profiles/keys_controller.rb +++ b/app/controllers/profiles/keys_controller.rb @@ -40,7 +40,7 @@ class Profiles::KeysController < ApplicationController if params[:username].present? begin user = User.find_by_username(params[:username]) - user.present? ? (render :text => user.all_ssh_keys) : + user.present? ? (render :text => user.all_ssh_keys.join('\n')) : (render_404 and return) rescue => e render text: e.message diff --git a/app/models/user.rb b/app/models/user.rb index 225c97d35f..4c58effaf3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -393,6 +393,6 @@ class User < ActiveRecord::Base end def all_ssh_keys - keys.collect{|x| x.key}.join("\n") + keys.collect{|x| x.key} end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index f6c9f82c4e..f7e242af00 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -276,4 +276,16 @@ describe User do User.by_username_or_id('bar').should be_nil end end + + describe 'all_ssh_keys' do + it { should have_many(:keys).dependent(:destroy) } + + it "should have all ssh keys" do + user = create :user + key = create :key, key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD33bWLBxu48Sev9Fert1yzEO4WGcWglWF7K/AwblIUFselOt/QdOL9DSjpQGxLagO1s9wl53STIO8qGS4Ms0EJZyIXOEFMjFJ5xmjSy+S37By4sG7SsltQEHMxtbtFOaW5LV2wCrX+rUsRNqLMamZjgjcPO0/EgGCXIGMAYW4O7cwGZdXWYIhQ1Vwy+CsVMDdPkPgBXqK7nR/ey8KMs8ho5fMNgB5hBw/AL9fNGhRw3QTD6Q12Nkhl4VZES2EsZqlpNnJttnPdp847DUsT6yuLRlfiQfz5Cn9ysHFdXObMN5VYIiPFwHeYCZp1X2S4fDZooRE8uOLTfxWHPXwrhqSH", user_id: user.id + + user.all_ssh_keys.should include(key.key) + end + + end end diff --git a/spec/routing/routing_spec.rb b/spec/routing/routing_spec.rb index 946ef7c28c..f5ffb21153 100644 --- a/spec/routing/routing_spec.rb +++ b/spec/routing/routing_spec.rb @@ -183,6 +183,11 @@ describe Profiles::KeysController, "routing" do it "to #destroy" do delete("/profile/keys/1").should route_to('profiles/keys#destroy', id: '1') end + + # get all the ssh-keys of a user + it "to #get_keys" do + get("/foo.keys").should route_to('profiles/keys#get_keys', username: 'foo') + end end # dashboard GET /dashboard(.:format) dashboard#show From 16d6eb3f9a6d97e17b501079c8770ba68ab999eb Mon Sep 17 00:00:00 2001 From: Devaroop Bhattacharya Date: Thu, 6 Feb 2014 19:51:31 +0530 Subject: [PATCH 4/8] Update Gemfile --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 6f4e1f4d4a..446ee73a4b 100644 --- a/Gemfile +++ b/Gemfile @@ -17,7 +17,7 @@ gem 'actionpack-action_caching' # Supported DBs gem "mysql2", group: :mysql -#gem "pg", group: :postgres +gem "pg", group: :postgres # Auth gem "devise", '3.0.4' From 5101ff7bec35704d844df8e608f604abd22df57a Mon Sep 17 00:00:00 2001 From: Devaroop Bhattacharya Date: Thu, 6 Feb 2014 19:53:29 +0530 Subject: [PATCH 5/8] Update Gemfile.lock --- Gemfile.lock | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index 19d2fcf6b5..0428a9df48 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -311,6 +311,7 @@ GEM multi_json (~> 1.3) omniauth-oauth (~> 1.0) orm_adapter (0.5.0) + pg (0.15.1) phantomjs (1.9.2.0) poltergeist (1.4.1) capybara (~> 2.1.0) @@ -602,6 +603,7 @@ DEPENDENCIES omniauth-github omniauth-google-oauth2 omniauth-twitter + pg poltergeist (~> 1.4.1) protected_attributes pry From 42083b19aee3ee2bc3688ff1a31006a4b5e31b38 Mon Sep 17 00:00:00 2001 From: Devaroop Bhattacharya Date: Thu, 6 Feb 2014 20:31:27 +0530 Subject: [PATCH 6/8] update manual error in changelog while merging --- CHANGELOG | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index c152fead16..cf1b09a416 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -106,7 +106,6 @@ v 6.2.1 - Security: Fix issue with generated passwords for new users v 6.2.0 - - Public projects are visible from the outside - Public project pages are now visible to everyone (files, issues, wik, etc.) THIS MEANS YOUR ISSUES AND WIKI FOR PUBLIC PROJECTS ARE PUBLICLY VISIBLE AFTER THE UPGRADE - Add group access to permissions page From 46ad09bf70400eeebab5495ee6a5ded4da86012e Mon Sep 17 00:00:00 2001 From: GitLab Date: Tue, 11 Feb 2014 19:04:47 +0530 Subject: [PATCH 7/8] code refactor as per standards --- app/controllers/profiles/keys_controller.rb | 11 +++++++---- app/models/user.rb | 2 +- config/routes.rb | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/controllers/profiles/keys_controller.rb b/app/controllers/profiles/keys_controller.rb index 7c97987d00..e8237a1f22 100644 --- a/app/controllers/profiles/keys_controller.rb +++ b/app/controllers/profiles/keys_controller.rb @@ -34,14 +34,17 @@ class Profiles::KeysController < ApplicationController end end - #get all keys of a user(params[:username]) in a text format - #helpful for sysadmins to put in respective servers + # Get all keys of a user(params[:username]) in a text format + # Helpful for sysadmins to put in respective servers def get_keys if params[:username].present? begin user = User.find_by_username(params[:username]) - user.present? ? (render :text => user.all_ssh_keys.join('\n')) : - (render_404 and return) + if user.present? + render text: user.all_ssh_keys.join('\n') + else + render_404 and return + end rescue => e render text: e.message end diff --git a/app/models/user.rb b/app/models/user.rb index 2a58692375..10f21d2350 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -437,6 +437,6 @@ class User < ActiveRecord::Base end def all_ssh_keys - keys.collect{|x| x.key}.join("\n") + keys.map(&:key) end end diff --git a/config/routes.rb b/config/routes.rb index 1cc6242c62..8c66ad741f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,7 +12,7 @@ Gitlab::Application.routes.draw do API::API.logger Rails.logger mount API::API => '/api' - #get all keys of user + # Get all keys of user get ':username.keys' => 'profiles/keys#get_keys' , constraints: { username: /.*/ } constraint = lambda { |request| request.env["warden"].authenticate? and request.env['warden'].user.admin? } From 4b9c28bccded2064fc95cf891b87a375d5bcdcf7 Mon Sep 17 00:00:00 2001 From: GitLab Date: Tue, 11 Feb 2014 21:12:27 +0530 Subject: [PATCH 8/8] remove unwanted spaces, reduce diff, clean before merge --- spec/models/user_spec.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3b09f7978b..fd8d7133ae 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -367,10 +367,8 @@ describe User do it 'does not begin with https if website url begins with https' do user.website_url = 'https://test.com' - + expect(user.short_website_url).to eq 'test.com' end end - end -